• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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/astNode.h"
20 
21 namespace panda::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     NO_COPY_OPERATOR(Expression);
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         ASSERT(IsLiteral());
47         return reinterpret_cast<const Literal *>(this);
48     }
49 
AsLiteral()50     [[nodiscard]] Literal *AsLiteral()
51     {
52         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         ASSERT(IsTypeNode());
79         return reinterpret_cast<TypeNode *>(this);
80     }
81 
AsTypeNode()82     [[nodiscard]] const TypeNode *AsTypeNode() const
83     {
84         ASSERT(IsTypeNode());
85         return reinterpret_cast<const TypeNode *>(this);
86     }
87 
AsAnnotatedExpression()88     [[nodiscard]] AnnotatedExpression *AsAnnotatedExpression()
89     {
90         ASSERT(IsAnnotatedExpression());
91         return reinterpret_cast<AnnotatedExpression *>(this);
92     }
93 
AsAnnotatedExpression()94     [[nodiscard]] const AnnotatedExpression *AsAnnotatedExpression() const
95     {
96         ASSERT(IsAnnotatedExpression());
97         return reinterpret_cast<const AnnotatedExpression *>(this);
98     }
99 
100 protected:
Expression(AstNodeType const type)101     explicit Expression(AstNodeType const type) : TypedAstNode(type) {}
Expression(AstNodeType const type,ModifierFlags const flags)102     explicit Expression(AstNodeType const type, ModifierFlags const flags) : TypedAstNode(type, flags) {}
103 
Expression(Expression const & other)104     Expression(Expression const &other) : TypedAstNode(static_cast<TypedAstNode const &>(other))
105     {
106         grouped_ = other.grouped_;
107     }
108 
109 private:
110     bool grouped_ {};
111 };
112 
113 class AnnotatedExpression : public Annotated<Expression> {
114 public:
115     AnnotatedExpression() = delete;
116     ~AnnotatedExpression() override = default;
117 
118     NO_COPY_SEMANTIC(AnnotatedExpression);
119     NO_MOVE_SEMANTIC(AnnotatedExpression);
120 
IsAnnotatedExpression()121     [[nodiscard]] bool IsAnnotatedExpression() const noexcept override
122     {
123         return true;
124     }
125 
126 protected:
AnnotatedExpression(AstNodeType const type,TypeNode * const typeAnnotation)127     explicit AnnotatedExpression(AstNodeType const type, TypeNode *const typeAnnotation)
128         : Annotated<Expression>(type, typeAnnotation)
129     {
130     }
AnnotatedExpression(AstNodeType const type)131     explicit AnnotatedExpression(AstNodeType const type) : Annotated<Expression>(type) {}
132 
133     explicit AnnotatedExpression(AnnotatedExpression const &other, ArenaAllocator *allocator);
134 };
135 
136 class MaybeOptionalExpression : public Expression {
137 public:
138     MaybeOptionalExpression() = delete;
139     ~MaybeOptionalExpression() override = default;
140 
141     NO_COPY_OPERATOR(MaybeOptionalExpression);
142     NO_MOVE_SEMANTIC(MaybeOptionalExpression);
143 
IsOptional()144     [[nodiscard]] bool IsOptional() const noexcept
145     {
146         return optional_;
147     }
148 
SetOptionalType(checker::Type * optionalType)149     void SetOptionalType(checker::Type *optionalType)
150     {
151         optionalType_ = optionalType;
152     }
153 
OptionalType()154     [[nodiscard]] const checker::Type *OptionalType() const noexcept
155     {
156         return optionalType_ != nullptr ? optionalType_ : TsType();
157     }
158 
159 protected:
MaybeOptionalExpression(AstNodeType type,bool optional)160     explicit MaybeOptionalExpression(AstNodeType type, bool optional) : Expression(type), optional_(optional) {}
MaybeOptionalExpression(AstNodeType type,ModifierFlags flags,bool optional)161     explicit MaybeOptionalExpression(AstNodeType type, ModifierFlags flags, bool optional)
162         : Expression(type, flags), optional_(optional)
163     {
164     }
165 
MaybeOptionalExpression(MaybeOptionalExpression const & other)166     MaybeOptionalExpression(MaybeOptionalExpression const &other) : Expression(static_cast<Expression const &>(other))
167     {
168         optionalType_ = other.optionalType_;
169         optional_ = other.optional_;
170     }
171 
172 private:
173     checker::Type *optionalType_ {};
174     bool optional_;
175 };
176 
177 }  // namespace panda::es2panda::ir
178 
179 #endif /* ES2PANDA_IR_EXPRESSION_H */
180