• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_COMPILER_CORE_AST_VERIFIER_HELPERS_H
17 #define ES2PANDA_COMPILER_CORE_AST_VERIFIER_HELPERS_H
18 
19 #include "ir/astNode.h"
20 #include "ir/expressions/identifier.h"
21 #include "checker/types/signature.h"
22 
23 namespace ark::es2panda::compiler::ast_verifier {
24 
25 bool IsImportLike(const ir::AstNode *ast);
26 bool IsExportLike(const ir::AstNode *ast);
27 bool IsBooleanType(const ir::AstNode *ast);
28 bool IsValidTypeForBinaryOp(const ir::AstNode *ast, bool isBitwise);
29 bool IsStringType(const ir::AstNode *ast);
30 bool IsVisibleInternalNode(const ir::AstNode *ast, const ir::AstNode *objTypeDeclNode);
31 const checker::Type *GetClassDefinitionType(const ir::AstNode *ast);
32 const checker::Type *GetTSInterfaceDeclarationType(const ir::AstNode *ast);
33 bool ValidateMethodAccessForClass(const ir::AstNode *ast, const ir::AstNode *ownerSignDeclNode,
34                                   checker::Signature *signature, const ir::AstNode *memberObjTypeDeclNode);
35 bool ValidateMethodAccessForTSInterface(const ir::AstNode *ast, const ir::AstNode *ownerSignDeclNode,
36                                         checker::Signature *signature, const ir::AstNode *memberObjTypeDeclNode);
37 bool ValidatePropertyAccessForClass(const ir::AstNode *ast, const ir::AstNode *propVarDeclNode,
38                                     const ir::AstNode *propVarDeclNodeParent, const varbinder::LocalVariable *propVar,
39                                     const ir::AstNode *objTypeDeclNode);
40 bool ValidateVariableAccess(const varbinder::LocalVariable *propVar, const ir::MemberExpression *ast);
41 bool ValidateMethodAccess(const ir::MemberExpression *memberExpression, const ir::CallExpression *ast);
42 
TryGetLocalScopeVariable(const ir::Identifier * id)43 inline const varbinder::LocalVariable *TryGetLocalScopeVariable(const ir::Identifier *id)
44 {
45     const auto *var = id->Variable();
46     if ((var != nullptr) && var->IsLocalVariable()) {
47         if (const auto *localVar = var->AsLocalVariable(); localVar->HasFlag(varbinder::VariableFlags::LOCAL)) {
48             return localVar;
49         }
50     }
51 
52     return nullptr;
53 }
54 
55 template <typename T>
IsContainedIn(const T * child,const T * parent)56 bool IsContainedIn(const T *child, const T *parent)
57 {
58     if (child == nullptr || parent == nullptr) {
59         return false;
60     }
61 
62     std::unordered_set<const T *> savedNodes;
63     while (child != nullptr && child != parent) {
64         savedNodes.emplace(child);
65         child = child->Parent();
66         if (savedNodes.find(child) != savedNodes.end()) {
67             return false;
68         }
69     }
70     return child == parent;
71 }
72 
73 }  // namespace ark::es2panda::compiler::ast_verifier
74 
75 #endif  // ES2PANDA_COMPILER_CORE_AST_VERIFIER_HELPERS_H
76