• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_CHECKER_ETSANALYZER_H
17 #define ES2PANDA_CHECKER_ETSANALYZER_H
18 
19 #include "checker/SemanticAnalyzer.h"
20 #include "checker/ETSchecker.h"
21 #include "ETSAnalyzerHelpers.h"
22 
23 namespace ark::es2panda::checker {
24 
25 class ETSAnalyzer final : public SemanticAnalyzer {
26 public:
ETSAnalyzer(Checker * checker)27     explicit ETSAnalyzer(Checker *checker) : SemanticAnalyzer(checker) {};
28 // CC-OFFNXT(G.PRE.02,G.PRE.09) name part
29 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
30 #define DECLARE_ETSANALYZER_CHECK_METHOD(_, nodeType) checker::Type *Check(ir::nodeType *node) const override;
31     AST_NODE_MAPPING(DECLARE_ETSANALYZER_CHECK_METHOD)
32 #undef DECLARE_ETSANALYZER_CHECK_METHOD
33 
34 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
35 #define DECLARE_ETSANALYZER_CHECK_METHOD(_, __, nodeType, ___) \
36     virtual checker::Type *Check(ir::nodeType *node) const override;  // CC-OFF(G.PRE.02,G.PRE.09) name part
37     AST_NODE_REINTERPRET_MAPPING(DECLARE_ETSANALYZER_CHECK_METHOD)
38 #undef DECLARE_ETSANALYZER_CHECK_METHOD
39     checker::Type *PreferredType(ir::ObjectExpression *expr) const;
40     checker::Type *CheckDynamic(ir::ObjectExpression *expr) const;
41     checker::Type *GetPreferredType(ir::ArrayExpression *expr) const;
42     void GetUnionPreferredType(ir::Expression *expr, Type *originalType) const;
43     void CheckObjectExprProps(const ir::ObjectExpression *expr, checker::ETSObjectType *objectTypeForProperties,
44                               checker::PropertySearchFlags searchFlags) const;
45     std::tuple<Type *, ir::Expression *> CheckAssignmentExprOperatorType(ir::AssignmentExpression *expr,
46                                                                          Type *leftType) const;
47     [[nodiscard]] checker::Type *ReturnTypeForStatement([[maybe_unused]] const ir::Statement *const st) const;
48 
49 private:
50     ETSChecker *GetETSChecker() const;
51     void CheckInstantatedClass(ir::ETSNewClassInstanceExpression *expr, ETSObjectType *&calleeObj) const;
52     void CheckMethodModifiers(ir::MethodDefinition *node) const;
53     checker::Signature *ResolveSignature(ETSChecker *checker, ir::CallExpression *expr,
54                                          checker::Type *calleeType) const;
55     checker::Type *GetReturnType(ir::CallExpression *expr, checker::Type *calleeType) const;
56     checker::Type *GetFunctionReturnType(ir::ReturnStatement *st, ir::ScriptFunction *containingFunc) const;
57     checker::Type *GetCallExpressionReturnType(ir::CallExpression *expr, checker::Type *calleeType) const;
58     checker::Type *UnwrapPromiseType(checker::Type *type) const;
59     checker::Type *GetSmartType(ir::AssignmentExpression *expr, checker::Type *leftType,
60                                 checker::Type *rightType) const;
61     bool CheckInferredFunctionReturnType(ir::ReturnStatement *st, ir::ScriptFunction *containingFunc,
62                                          checker::Type *&funcReturnType, ir::TypeNode *returnTypeAnnotation,
63                                          ETSChecker *checker) const;
64     void CheckClassProperty(ETSChecker *checker, ir::ScriptFunction *scriptFunc) const;
65 
66     checker::Type *ResolveMemberExpressionByBaseType(ETSChecker *checker, checker::Type *baseType,
67                                                      ir::MemberExpression *expr) const;
68 
CheckVoidTypeExpression(ETSChecker * checker,const ir::Expression * expr)69     void CheckVoidTypeExpression(ETSChecker *checker, const ir::Expression *expr) const
70     {
71         // Existing void expression inconsistency,refer to #17762
72         if (expr->TsType() == nullptr || !expr->TsType()->IsETSVoidType() || expr->Parent() == nullptr) {
73             return;
74         }
75         auto parent = expr->Parent();
76         while (parent->IsConditionalExpression()) {
77             parent = parent->Parent();
78             if (parent == nullptr) {
79                 return;
80             }
81         }
82         bool acceptVoid = parent->IsExpressionStatement() || parent->IsReturnStatement();
83         if (!acceptVoid) {
84             checker->LogError(diagnostic::VOID_VALUE, {}, expr->Start());
85         }
86     }
87     mutable std::vector<const varbinder::Variable *> catchParamStack_ {};
88 };
89 
90 }  // namespace ark::es2panda::checker
91 
92 #endif  // ES2PANDA_CHECKER_ETSANALYZER_H
93