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 #include "everyChildHasValidParent.h"
17 #include "ir/base/methodDefinition.h"
18
19 namespace ark::es2panda::compiler::ast_verifier {
20
operator ()(const ir::AstNode * ast)21 CheckResult EveryChildHasValidParent::operator()(const ir::AstNode *ast)
22 {
23 auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
24 if (ast->IsETSModule()) {
25 return result;
26 }
27
28 auto maybeOverloadMethod = [](const ir::AstNode *root, const ir::AstNode *node) -> bool {
29 if ((root->IsClassDefinition() || root->IsTSInterfaceBody()) && node->IsMethodDefinition() &&
30 node->Parent()->IsMethodDefinition()) {
31 auto maybeBaseOverloadMethod = node->Parent()->AsMethodDefinition();
32 auto overloads = maybeBaseOverloadMethod->Overloads();
33 auto res =
34 std::find_if(overloads.begin(), overloads.end(), [node](ir::MethodDefinition *m) { return m == node; });
35 return res != overloads.end() || maybeBaseOverloadMethod->AsyncPairMethod() == node;
36 }
37 return false;
38 };
39
40 ast->Iterate([this, ast, maybeOverloadMethod, &result](const ir::AstNode *node) {
41 if (ir::AstNode const *parent = node->Parent(); ast != parent) {
42 // NOTE: Temporary suppress.
43 // Should be removed after proper overload method's parent setting
44 // For now there is no right decision who is parent of overload method:
45 // baseOverloadMethod or ClassDefininiton, but the first is set
46 if (maybeOverloadMethod(ast, node)) {
47 return;
48 }
49
50 AddCheckMessage("INCORRECT_PARENT_REF", *node);
51 result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
52 }
53 });
54
55 return result;
56 }
57
58 } // namespace ark::es2panda::compiler::ast_verifier
59