13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "importExportAccessValid.h"
173af6ab5fSopenharmony_ci#include "helpers.h"
183af6ab5fSopenharmony_ci#include "ir/expressions/callExpression.h"
193af6ab5fSopenharmony_ci#include "checker/types/ets/etsObjectType.h"
203af6ab5fSopenharmony_ci#include "ir/module/importSpecifier.h"
213af6ab5fSopenharmony_ci#include "ir/module/importNamespaceSpecifier.h"
223af6ab5fSopenharmony_ci#include "ir/module/importDefaultSpecifier.h"
233af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h"
243af6ab5fSopenharmony_ci#include "ir/ets/etsImportDeclaration.h"
253af6ab5fSopenharmony_ci#include "checker/types/signature.h"
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier {
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci[[nodiscard]] CheckResult ImportExportAccessValid::operator()(CheckContext &ctx, const ir::AstNode *ast)
303af6ab5fSopenharmony_ci{
313af6ab5fSopenharmony_ci    std::unordered_set<std::string> importedVariables {};
323af6ab5fSopenharmony_ci    if (ast->IsETSImportDeclaration()) {
333af6ab5fSopenharmony_ci        const auto importDecl = ast->AsETSImportDeclaration()->Specifiers();
343af6ab5fSopenharmony_ci        const auto name = [](ir::AstNode *const specifier) {
353af6ab5fSopenharmony_ci            if (specifier->IsImportNamespaceSpecifier()) {
363af6ab5fSopenharmony_ci                return specifier->AsImportNamespaceSpecifier()->Local()->Name();
373af6ab5fSopenharmony_ci            }
383af6ab5fSopenharmony_ci            if (specifier->IsImportSpecifier()) {
393af6ab5fSopenharmony_ci                return specifier->AsImportSpecifier()->Local()->Name();
403af6ab5fSopenharmony_ci            }
413af6ab5fSopenharmony_ci            return specifier->AsImportDefaultSpecifier()->Local()->Name();
423af6ab5fSopenharmony_ci        };
433af6ab5fSopenharmony_ci        for (const auto import : importDecl) {
443af6ab5fSopenharmony_ci            importedVariables.emplace(name(import));
453af6ab5fSopenharmony_ci        }
463af6ab5fSopenharmony_ci    }
473af6ab5fSopenharmony_ci    if (ast->IsCallExpression()) {
483af6ab5fSopenharmony_ci        const auto *callExpr = ast->AsCallExpression();
493af6ab5fSopenharmony_ci        const auto *callee = callExpr->Callee();
503af6ab5fSopenharmony_ci        if (callee != nullptr && callee->IsIdentifier() &&
513af6ab5fSopenharmony_ci            !HandleImportExportIdentifier(importedVariables, callee->AsIdentifier(), callExpr)) {
523af6ab5fSopenharmony_ci            ctx.AddCheckMessage("PROPERTY_NOT_VISIBLE_HERE(NOT_EXPORTED)", *callee, callee->Start());
533af6ab5fSopenharmony_ci            return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
543af6ab5fSopenharmony_ci        }
553af6ab5fSopenharmony_ci    }
563af6ab5fSopenharmony_ci    if (ast->IsIdentifier() && !HandleImportExportIdentifier(importedVariables, ast->AsIdentifier(), nullptr)) {
573af6ab5fSopenharmony_ci        ctx.AddCheckMessage("PROPERTY_NOT_VISIBLE_HERE(NOT_EXPORTED)", *ast, ast->Start());
583af6ab5fSopenharmony_ci        return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
593af6ab5fSopenharmony_ci    }
603af6ab5fSopenharmony_ci    return {CheckDecision::CORRECT, CheckAction::CONTINUE};
613af6ab5fSopenharmony_ci}
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_cibool ImportExportAccessValid::ValidateExport(const varbinder::Variable *var)
643af6ab5fSopenharmony_ci{
653af6ab5fSopenharmony_ci    const auto *decl = var->Declaration();
663af6ab5fSopenharmony_ci    if (decl == nullptr) {
673af6ab5fSopenharmony_ci        return false;
683af6ab5fSopenharmony_ci    }
693af6ab5fSopenharmony_ci    const auto *node = decl->Node();
703af6ab5fSopenharmony_ci    if (node == nullptr) {
713af6ab5fSopenharmony_ci        return false;
723af6ab5fSopenharmony_ci    }
733af6ab5fSopenharmony_ci    return node->IsExported() || node->IsExportedType();
743af6ab5fSopenharmony_ci}
753af6ab5fSopenharmony_ci
763af6ab5fSopenharmony_cibool ImportExportAccessValid::InvariantImportExportMethod(const std::unordered_set<std::string> &importedVariables,
773af6ab5fSopenharmony_ci                                                          const varbinder::Variable *varCallee,
783af6ab5fSopenharmony_ci                                                          const ir::AstNode *callExpr, util::StringView name)
793af6ab5fSopenharmony_ci{
803af6ab5fSopenharmony_ci    auto *signature = callExpr->AsCallExpression()->Signature();
813af6ab5fSopenharmony_ci    if (signature == nullptr || signature->Owner() == nullptr) {
823af6ab5fSopenharmony_ci        // NOTE(vpukhov): Add a synthetic owner for dynamic signatures
833af6ab5fSopenharmony_ci        ASSERT(callExpr->AsCallExpression()->Callee()->TsType()->HasTypeFlag(checker::TypeFlag::ETS_DYNAMIC_FLAG));
843af6ab5fSopenharmony_ci        return true;
853af6ab5fSopenharmony_ci    }
863af6ab5fSopenharmony_ci
873af6ab5fSopenharmony_ci    if (signature != nullptr && varCallee->Declaration() != nullptr && varCallee->Declaration()->Node() != nullptr &&
883af6ab5fSopenharmony_ci        !IsContainedIn(varCallee->Declaration()->Node(), signature->Owner()->GetDeclNode()) &&
893af6ab5fSopenharmony_ci        varCallee->Declaration()->Node() != signature->Owner()->GetDeclNode()) {
903af6ab5fSopenharmony_ci        if (importedVariables.find(name.Mutf8()) != importedVariables.end() ||
913af6ab5fSopenharmony_ci            importedVariables.find("") != importedVariables.end()) {
923af6ab5fSopenharmony_ci            return ValidateExport(varCallee);
933af6ab5fSopenharmony_ci        }
943af6ab5fSopenharmony_ci        return false;
953af6ab5fSopenharmony_ci    }
963af6ab5fSopenharmony_ci    return true;
973af6ab5fSopenharmony_ci}
983af6ab5fSopenharmony_ci
993af6ab5fSopenharmony_cibool ImportExportAccessValid::InvariantImportExportVariable(const std::unordered_set<std::string> &importedVariables,
1003af6ab5fSopenharmony_ci                                                            const varbinder::Variable *var, const ir::Identifier *ident,
1013af6ab5fSopenharmony_ci                                                            util::StringView name)
1023af6ab5fSopenharmony_ci{
1033af6ab5fSopenharmony_ci    if (!var->HasFlag(varbinder::VariableFlags::LOCAL) && !var->HasFlag(varbinder::VariableFlags::VAR) &&
1043af6ab5fSopenharmony_ci        var->HasFlag(varbinder::VariableFlags::INITIALIZED) && var->Declaration() != nullptr &&
1053af6ab5fSopenharmony_ci        var->Declaration()->Node() != nullptr && !var->Declaration()->Node()->IsMethodDefinition() &&
1063af6ab5fSopenharmony_ci        !var->Declaration()->Node()->IsClassProperty()) {
1073af6ab5fSopenharmony_ci        auto varParent = var->Declaration()->Node()->Parent();
1083af6ab5fSopenharmony_ci        if (varParent != nullptr && !IsContainedIn(ident->Parent(), varParent) && ident->Parent() != varParent) {
1093af6ab5fSopenharmony_ci            if (var->GetScope() != nullptr && var->GetScope()->Parent() != nullptr &&
1103af6ab5fSopenharmony_ci                var->GetScope()->Parent()->IsGlobalScope() &&
1113af6ab5fSopenharmony_ci                ident->GetTopStatement() == varParent->GetTopStatement()) {
1123af6ab5fSopenharmony_ci                return true;
1133af6ab5fSopenharmony_ci            }
1143af6ab5fSopenharmony_ci            if (importedVariables.find(name.Mutf8()) != importedVariables.end() ||
1153af6ab5fSopenharmony_ci                importedVariables.find("") != importedVariables.end()) {
1163af6ab5fSopenharmony_ci                return ValidateExport(var);
1173af6ab5fSopenharmony_ci            }
1183af6ab5fSopenharmony_ci            return false;
1193af6ab5fSopenharmony_ci        }
1203af6ab5fSopenharmony_ci    }
1213af6ab5fSopenharmony_ci    return true;
1223af6ab5fSopenharmony_ci}
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_cibool ImportExportAccessValid::HandleImportExportIdentifier(std::unordered_set<std::string> &importedVariables,
1253af6ab5fSopenharmony_ci                                                           const ir::Identifier *ident, const ir::AstNode *callExpr)
1263af6ab5fSopenharmony_ci{
1273af6ab5fSopenharmony_ci    if (ident->IsReference()) {
1283af6ab5fSopenharmony_ci        const auto *var = ident->Variable();
1293af6ab5fSopenharmony_ci        if (var != nullptr) {
1303af6ab5fSopenharmony_ci            if (var->HasFlag(varbinder::VariableFlags::METHOD) && callExpr != nullptr) {
1313af6ab5fSopenharmony_ci                return InvariantImportExportMethod(importedVariables, var, callExpr, ident->Name());
1323af6ab5fSopenharmony_ci            }
1333af6ab5fSopenharmony_ci            return InvariantImportExportVariable(importedVariables, var, ident, ident->Name());
1343af6ab5fSopenharmony_ci        }
1353af6ab5fSopenharmony_ci    }
1363af6ab5fSopenharmony_ci    return true;
1373af6ab5fSopenharmony_ci}
1383af6ab5fSopenharmony_ci
1393af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler::ast_verifier
140