1/*
2 * Copyright (c) 2024 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 "checker/types/signature.h"
21
22namespace ark::es2panda::compiler::ast_verifier {
23
24bool IsImportLike(const ir::AstNode *ast);
25bool IsExportLike(const ir::AstNode *ast);
26bool IsBooleanType(const ir::AstNode *ast);
27bool IsValidTypeForBinaryOp(const ir::AstNode *ast, bool isBitwise);
28bool IsStringType(const ir::AstNode *ast);
29bool IsVisibleInternalNode(const ir::AstNode *ast, const ir::AstNode *objTypeDeclNode);
30const checker::Type *GetClassDefinitionType(const ir::AstNode *ast);
31const checker::Type *GetTSInterfaceDeclarationType(const ir::AstNode *ast);
32bool ValidateMethodAccessForClass(const ir::AstNode *ast, const ir::AstNode *ownerSignDeclNode,
33                                  checker::Signature *signature, const ir::AstNode *memberObjTypeDeclNode);
34bool ValidateMethodAccessForTSInterface(const ir::AstNode *ast, const ir::AstNode *ownerSignDeclNode,
35                                        checker::Signature *signature, const ir::AstNode *memberObjTypeDeclNode);
36bool ValidatePropertyAccessForClass(const ir::AstNode *ast, const ir::AstNode *propVarDeclNode,
37                                    const ir::AstNode *propVarDeclNodeParent, const varbinder::LocalVariable *propVar,
38                                    const ir::AstNode *objTypeDeclNode);
39bool ValidateVariableAccess(const varbinder::LocalVariable *propVar, const ir::MemberExpression *ast);
40bool ValidateMethodAccess(const ir::MemberExpression *memberExpression, const ir::CallExpression *ast);
41
42template <typename T>
43bool IsContainedIn(const T *child, const T *parent)
44{
45    if (child == nullptr || parent == nullptr) {
46        return false;
47    }
48
49    std::unordered_set<const T *> savedNodes;
50    while (child != nullptr && child != parent) {
51        savedNodes.emplace(child);
52        child = child->Parent();
53        if (savedNodes.find(child) != savedNodes.end()) {
54            return false;
55        }
56    }
57    return child == parent;
58}
59
60}  // namespace ark::es2panda::compiler::ast_verifier
61
62#endif  // ES2PANDA_COMPILER_CORE_AST_VERIFIER_HELPERS_H
63