• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "helpers.h"
17 #include "nodeHasType.h"
18 #include "ir/base/classDefinition.h"
19 #include "ir/expressions/identifier.h"
20 #include "ir/ts/tsEnumDeclaration.h"
21 #include "ir/ts/tsInterfaceBody.h"
22 #include "ir/ts/tsInterfaceDeclaration.h"
23 
24 namespace ark::es2panda::compiler::ast_verifier {
25 
operator ()(CheckContext & ctx,const ir::AstNode * ast)26 CheckResult NodeHasType::operator()(CheckContext &ctx, const ir::AstNode *ast)
27 {
28     // NOTE(orlovskymaxim) In TS some ETS constructs are expressions (i.e. class/interface definition)
29     // Because ETS uses some AST classes from TS this introduces semantical problem
30     // Solution for now - manually filter expressions that are statements in ETS
31     if (ast->IsETSPackageDeclaration()) {
32         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
33     }
34     if (IsImportLike(ast)) {
35         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
36     }
37     if (IsExportLike(ast)) {
38         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
39     }
40 
41     if (ast->IsTSTypeAliasDeclaration()) {
42         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
43     }
44     if (auto [decision, action] = CheckCompound(ctx, ast); action == CheckAction::SKIP_SUBTREE) {
45         return {decision, action};
46     }
47 
48     if (ast->IsTyped() && ast->IsExpression()) {
49         if (ast->IsClassDefinition() && ast->AsClassDefinition()->Ident()->Name() == "ETSGLOBAL") {
50             return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
51         }
52         if (ast->IsIdentifier() && ast->AsIdentifier()->Name() == "") {
53             return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
54         }
55         const auto *typed = static_cast<const ir::TypedAstNode *>(ast);
56         if (typed->TsType() == nullptr) {
57             ctx.AddCheckMessage("NULL_TS_TYPE", *ast, ast->Start());
58             return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
59         }
60     }
61     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
62 }
63 
CheckCompound(CheckContext & ctx,const ir::AstNode * ast)64 CheckResult NodeHasType::CheckCompound(CheckContext &ctx, const ir::AstNode *ast)
65 {
66     if (ast->IsTSInterfaceDeclaration()) {
67         for (const auto &member : ast->AsTSInterfaceDeclaration()->Body()->Body()) {
68             [[maybe_unused]] auto _ = (*this)(ctx, member);
69         }
70         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
71     }
72     if (ast->IsTSEnumDeclaration()) {
73         for (const auto &member : ast->AsTSEnumDeclaration()->Members()) {
74             [[maybe_unused]] auto _ = (*this)(ctx, member);
75         }
76         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
77     }
78     if (ast->IsClassDefinition()) {
79         for (const auto &member : ast->AsClassDefinition()->Body()) {
80             [[maybe_unused]] auto _ = (*this)(ctx, member);
81         }
82         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
83     }
84     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
85 }
86 
87 }  // namespace ark::es2panda::compiler::ast_verifier
88