• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ES2PANDA_COMPILER_CORE_AST_VERIFIER_INVARIANTS_NODEHASTYPE_H
17 #define ES2PANDA_COMPILER_CORE_AST_VERIFIER_INVARIANTS_NODEHASTYPE_H
18 
19 #include "ast_verifier/invariantBase.h"
20 #include "checker/types/type.h"
21 
22 namespace ark::es2panda::compiler::ast_verifier {
23 
24 class NodeHasType : public InvariantBase<VerifierInvariants::NODE_HAS_TYPE> {
25 public:
26     using Base::Base;
27     [[nodiscard]] CheckResult operator()(const ir::AstNode *ast);
28 
29 private:
30     const checker::Type *type_ {};
31 
32     class ExceptionsMatcher;
33 
34     friend class NoPrimitiveTypes;
35 };
36 
37 class NoPrimitiveTypes : public InvariantBase<VerifierInvariants::NO_PRIMITIVE_TYPES, NodeHasType> {
38 public:
39     using Base::Base;
40 
41     void SetNumberLoweringOccured(bool v = true)
42     {
43         numberLoweringOccurred_ = v;
44     }
45 
operator()46     [[nodiscard]] CheckResult operator()(const ir::AstNode *ast)
47     {
48         const auto *type = Get<NodeHasType>().type_;
49         if (type == nullptr) {
50             return {CheckDecision::CORRECT, CheckAction::CONTINUE};
51         }
52         if (!numberLoweringOccurred_ && type->IsETSPrimitiveType()) {
53             AddCheckMessage("PRIMITIVE_BEFORE_LOWERING", *ast);
54             return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
55         }
56         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
57     }
58 
59 private:
60     bool numberLoweringOccurred_ {false};
61 };
62 }  // namespace ark::es2panda::compiler::ast_verifier
63 
64 #endif  // ES2PANDA_COMPILER_CORE_AST_VERIFIER_INVARIANTS_NODEHASTYPE_H
65