• 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_IR_TYPED_H
17 #define ES2PANDA_IR_TYPED_H
18 
19 #include "ir/astNode.h"
20 #include "ir/statement.h"
21 
22 namespace ark::es2panda::checker {
23 // NOLINTBEGIN(readability-redundant-declaration)
24 bool IsTypeError(Type const *tp);
25 // NOLINTEND(readability-redundant-declaration)
26 }  // namespace ark::es2panda::checker
27 
28 namespace ark::es2panda::ir {
29 
30 template <typename T>
31 class Typed : public T {
32 public:
33     Typed() = delete;
34     ~Typed() override = default;
35 
36     NO_COPY_OPERATOR(Typed);
37     NO_MOVE_SEMANTIC(Typed);
38 
TsType()39     [[nodiscard]] checker::Type const *TsType() const
40     {
41         return tsType_;
42     }
43 
TsType()44     [[nodiscard]] checker::Type *TsType()
45     {
46         return tsType_;
47     }
48 
SetTsType(checker::Type * tsType)49     checker::Type *SetTsType(checker::Type *tsType) noexcept
50     {
51         return (tsType_ = tsType);
52     }
53 
IsTyped()54     bool IsTyped() const override
55     {
56         return true;
57     }
58 
59 protected:
Typed(AstNodeType const type)60     explicit Typed(AstNodeType const type) : T(type) {}
Typed(AstNodeType const type,ModifierFlags const flags)61     explicit Typed(AstNodeType const type, ModifierFlags const flags) : T(type, flags) {}
62 
63     // NOTE: when cloning node its type is not copied but removed empty so that it can be re-checked further.
Typed(Typed const & other)64     Typed(Typed const &other) : T(static_cast<T const &>(other)) {}
65 
CopyTo(AstNode * other)66     void CopyTo(AstNode *other) const override
67     {
68         auto otherImpl = static_cast<Typed<T> *>(other);
69         otherImpl->tsType_ = tsType_;
70         T::CopyTo(other);
71     }
72 
73 private:
74     friend class SizeOfNodeTest;
75     checker::Type *tsType_ {};
76 };
77 
78 class TypedAstNode : public Typed<AstNode> {
79 public:
80     TypedAstNode() = delete;
81     ~TypedAstNode() override = default;
82 
83     NO_COPY_OPERATOR(TypedAstNode);
84     NO_MOVE_SEMANTIC(TypedAstNode);
85 
CopyTo(AstNode * other)86     void CopyTo(AstNode *other) const override
87     {
88         Typed<AstNode>::CopyTo(other);
89     };
90 
91 protected:
TypedAstNode(AstNodeType const type)92     explicit TypedAstNode(AstNodeType const type) : Typed<AstNode>(type) {}
TypedAstNode(AstNodeType const type,ModifierFlags const flags)93     explicit TypedAstNode(AstNodeType const type, ModifierFlags const flags) : Typed<AstNode>(type, flags) {}
94 
TypedAstNode(TypedAstNode const & other)95     TypedAstNode(TypedAstNode const &other) : Typed<AstNode>(static_cast<Typed<AstNode> const &>(other)) {}
96 };
97 
98 class AnnotatedAstNode : public Annotated<AstNode> {
99 public:
100     AnnotatedAstNode() = delete;
101     ~AnnotatedAstNode() override = default;
102 
103     NO_COPY_OPERATOR(AnnotatedAstNode);
104     NO_MOVE_SEMANTIC(AnnotatedAstNode);
105 
106 protected:
AnnotatedAstNode(AstNodeType const type,TypeNode * const typeAnnotation)107     explicit AnnotatedAstNode(AstNodeType const type, TypeNode *const typeAnnotation)
108         : Annotated<AstNode>(type, typeAnnotation)
109     {
110     }
AnnotatedAstNode(AstNodeType const type)111     explicit AnnotatedAstNode(AstNodeType const type) : Annotated<AstNode>(type) {}
AnnotatedAstNode(AstNodeType const type,ModifierFlags const flags)112     explicit AnnotatedAstNode(AstNodeType const type, ModifierFlags const flags) : Annotated<AstNode>(type, flags) {}
113 
AnnotatedAstNode(AnnotatedAstNode const & other)114     AnnotatedAstNode(AnnotatedAstNode const &other) : Annotated<AstNode>(static_cast<Annotated<AstNode> const &>(other))
115     {
116     }
117 };
118 
119 class TypedStatement : public Typed<Statement> {
120 public:
121     TypedStatement() = delete;
122     ~TypedStatement() override = default;
123 
124     NO_COPY_OPERATOR(TypedStatement);
125     NO_MOVE_SEMANTIC(TypedStatement);
126 
CopyTo(AstNode * other)127     void CopyTo(AstNode *other) const override
128     {
129         Typed<Statement>::CopyTo(other);
130     };
131 
132 protected:
TypedStatement(AstNodeType type)133     explicit TypedStatement(AstNodeType type) : Typed<Statement>(type) {};
TypedStatement(AstNodeType type,ModifierFlags flags)134     explicit TypedStatement(AstNodeType type, ModifierFlags flags) : Typed<Statement>(type, flags) {};
135 
TypedStatement(TypedStatement const & other)136     TypedStatement(TypedStatement const &other) : Typed<Statement>(static_cast<Typed<Statement> const &>(other)) {}
137 
138     inline static checker::Type *const CHECKED = reinterpret_cast<checker::Type *>(0x01);
139 };
140 
141 class AnnotatedStatement : public Annotated<Statement> {
142 public:
143     AnnotatedStatement() = delete;
144     ~AnnotatedStatement() override = default;
145 
146     NO_COPY_OPERATOR(AnnotatedStatement);
147     NO_MOVE_SEMANTIC(AnnotatedStatement);
148 
CopyTo(AstNode * other)149     void CopyTo(AstNode *other) const override
150     {
151         Annotated<Statement>::CopyTo(other);
152     }
153 
154 protected:
AnnotatedStatement(AstNodeType type,TypeNode * typeAnnotation)155     explicit AnnotatedStatement(AstNodeType type, TypeNode *typeAnnotation) : Annotated<Statement>(type, typeAnnotation)
156     {
157     }
158 
AnnotatedStatement(AstNodeType type)159     explicit AnnotatedStatement(AstNodeType type) : Annotated<Statement>(type) {}
AnnotatedStatement(AstNodeType type,ModifierFlags flags)160     explicit AnnotatedStatement(AstNodeType type, ModifierFlags flags) : Annotated<Statement>(type, flags) {}
161 
AnnotatedStatement(AnnotatedStatement const & other)162     AnnotatedStatement(AnnotatedStatement const &other)
163         : Annotated<Statement>(static_cast<Annotated<Statement> const &>(other))
164     {
165     }
166 };
167 
168 }  // namespace ark::es2panda::ir
169 
170 #endif
171