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