• 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_IR_EXPRESSION_H
17 #define ES2PANDA_IR_EXPRESSION_H
18 
19 #include "ir/typed.h"
20 
21 namespace ark::es2panda::ir {
22 class Literal;
23 class TypeNode;
24 class AnnotatedExpression;
25 
26 class Expression : public TypedAstNode {
27 public:
28     Expression() = delete;
29     ~Expression() override = default;
30 
31     Expression &operator=(const Expression &) = delete;
32     NO_MOVE_SEMANTIC(Expression);
33 
IsGrouped()34     [[nodiscard]] bool IsGrouped() const noexcept
35     {
36         return grouped_;
37     }
38 
SetGrouped()39     void SetGrouped() noexcept
40     {
41         grouped_ = true;
42     }
43 
AsLiteral()44     [[nodiscard]] const Literal *AsLiteral() const
45     {
46         ES2PANDA_ASSERT(IsLiteral());
47         return reinterpret_cast<const Literal *>(this);
48     }
49 
AsLiteral()50     [[nodiscard]] Literal *AsLiteral()
51     {
52         ES2PANDA_ASSERT(IsLiteral());
53         return reinterpret_cast<Literal *>(this);
54     }
55 
IsLiteral()56     [[nodiscard]] virtual bool IsLiteral() const noexcept
57     {
58         return false;
59     }
60 
IsTypeNode()61     [[nodiscard]] virtual bool IsTypeNode() const noexcept
62     {
63         return false;
64     }
65 
IsAnnotatedExpression()66     [[nodiscard]] virtual bool IsAnnotatedExpression() const noexcept
67     {
68         return false;
69     }
70 
IsExpression()71     [[nodiscard]] bool IsExpression() const noexcept override
72     {
73         return true;
74     }
75 
AsTypeNode()76     [[nodiscard]] TypeNode *AsTypeNode()
77     {
78         ES2PANDA_ASSERT(IsTypeNode());
79         return reinterpret_cast<TypeNode *>(this);
80     }
81 
AsTypeNode()82     [[nodiscard]] const TypeNode *AsTypeNode() const
83     {
84         ES2PANDA_ASSERT(IsTypeNode());
85         return reinterpret_cast<const TypeNode *>(this);
86     }
87 
AsAnnotatedExpression()88     [[nodiscard]] AnnotatedExpression *AsAnnotatedExpression()
89     {
90         ES2PANDA_ASSERT(IsAnnotatedExpression());
91         return reinterpret_cast<AnnotatedExpression *>(this);
92     }
93 
AsAnnotatedExpression()94     [[nodiscard]] const AnnotatedExpression *AsAnnotatedExpression() const
95     {
96         ES2PANDA_ASSERT(IsAnnotatedExpression());
97         return reinterpret_cast<const AnnotatedExpression *>(this);
98     }
99 
100     bool IsBrokenExpression() const noexcept;
101 
102     [[nodiscard]] virtual std::string ToString() const;
103 
104     void CopyTo(AstNode *other) const override;
105 
106 protected:
Expression(AstNodeType const type)107     explicit Expression(AstNodeType const type) : TypedAstNode(type) {}
Expression(AstNodeType const type,ModifierFlags const flags)108     explicit Expression(AstNodeType const type, ModifierFlags const flags) : TypedAstNode(type, flags) {}
109 
Expression(Expression const & other)110     Expression(Expression const &other) : TypedAstNode(static_cast<TypedAstNode const &>(other))
111     {
112         grouped_ = other.grouped_;
113     }
114 
115 private:
116     friend class SizeOfNodeTest;
117     bool grouped_ {};
118 };
119 
120 class AnnotatedExpression : public Annotated<Expression> {
121 public:
122     AnnotatedExpression() = delete;
123     ~AnnotatedExpression() override = default;
124 
125     NO_COPY_SEMANTIC(AnnotatedExpression);
126     NO_MOVE_SEMANTIC(AnnotatedExpression);
127 
IsAnnotatedExpression()128     [[nodiscard]] bool IsAnnotatedExpression() const noexcept override
129     {
130         return true;
131     }
132 
133 protected:
AnnotatedExpression(AstNodeType const type,TypeNode * const typeAnnotation)134     explicit AnnotatedExpression(AstNodeType const type, TypeNode *const typeAnnotation)
135         : Annotated<Expression>(type, typeAnnotation)
136     {
137     }
AnnotatedExpression(AstNodeType const type)138     explicit AnnotatedExpression(AstNodeType const type) : Annotated<Expression>(type) {}
139 
140     explicit AnnotatedExpression(AnnotatedExpression const &other, ArenaAllocator *allocator);
141 };
142 
143 class MaybeOptionalExpression : public Expression {
144 public:
145     MaybeOptionalExpression() = delete;
146     ~MaybeOptionalExpression() override = default;
147 
148     MaybeOptionalExpression &operator=(const MaybeOptionalExpression &) = delete;
149     NO_MOVE_SEMANTIC(MaybeOptionalExpression);
150 
IsOptional()151     [[nodiscard]] bool IsOptional() const noexcept
152     {
153         return optional_;
154     }
155 
ClearOptional()156     void ClearOptional() noexcept
157     {
158         optional_ = false;
159     }
160 
161 protected:
MaybeOptionalExpression(AstNodeType type,bool optional)162     explicit MaybeOptionalExpression(AstNodeType type, bool optional) : Expression(type), optional_(optional) {}
MaybeOptionalExpression(AstNodeType type,ModifierFlags flags,bool optional)163     explicit MaybeOptionalExpression(AstNodeType type, ModifierFlags flags, bool optional)
164         : Expression(type, flags), optional_(optional)
165     {
166     }
167 
MaybeOptionalExpression(MaybeOptionalExpression const & other)168     MaybeOptionalExpression(MaybeOptionalExpression const &other) : Expression(static_cast<Expression const &>(other))
169     {
170         optional_ = other.optional_;
171     }
172 
173 private:
174     bool optional_;
175 };
176 
177 }  // namespace ark::es2panda::ir
178 
179 #endif /* ES2PANDA_IR_EXPRESSION_H */
180