1 /**
2 * Copyright (c) 2021 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_IDENTIFIER_H
17 #define ES2PANDA_IR_EXPRESSION_IDENTIFIER_H
18
19 #include <ir/expression.h>
20 #include <util/ustring.h>
21
22 namespace panda::es2panda::compiler {
23 class PandaGen;
24 } // namespace panda::es2panda::compiler
25
26 namespace panda::es2panda::checker {
27 class Checker;
28 class Type;
29 } // namespace panda::es2panda::checker
30
31 namespace panda::es2panda::binder {
32 class Variable;
33 } // namespace panda::es2panda::binder
34
35 namespace panda::es2panda::ir {
36
37 enum class IdentifierFlags {
38 NONE,
39 OPTIONAL = 1 << 0,
40 REFERENCE = 1 << 1,
41 TDZ = 1 << 2,
42 };
43
DEFINE_BITOPS(IdentifierFlags)44 DEFINE_BITOPS(IdentifierFlags)
45
46 class Identifier : public Expression {
47 public:
48 explicit Identifier(util::StringView name)
49 : Expression(AstNodeType::IDENTIFIER), name_(name)
50 {
51 }
52
53 explicit Identifier(util::StringView name, Expression *typeAnnotation)
54 : Expression(AstNodeType::IDENTIFIER),
55 name_(name),
56 typeAnnotation_(typeAnnotation)
57 {
58 }
59
60 const Expression *TypeAnnotation() const
61 {
62 return typeAnnotation_;
63 }
64
65 Expression *TypeAnnotation()
66 {
67 return typeAnnotation_;
68 }
69
70 const util::StringView &Name() const
71 {
72 return name_;
73 }
74
75 void SetName(util::StringView name)
76 {
77 name_ = name;
78 }
79
80 bool IsOptional() const
81 {
82 return (flags_ & IdentifierFlags::OPTIONAL) != 0;
83 }
84
85 void SetOptional(bool optional)
86 {
87 if (optional) {
88 flags_ |= IdentifierFlags::OPTIONAL;
89 } else {
90 flags_ &= ~IdentifierFlags::OPTIONAL;
91 }
92 }
93
94 bool IsReference() const
95 {
96 return (flags_ & IdentifierFlags::REFERENCE) != 0;
97 }
98
99 void SetReference()
100 {
101 flags_ |= IdentifierFlags::REFERENCE;
102 }
103
104 const std::vector<binder::Variable *> &TSVariables() const
105 {
106 return tsVariables_;
107 }
108
109 void SetTSVariables(const std::vector<binder::Variable *> &tsVariables)
110 {
111 tsVariables_ = tsVariables;
112 }
113
114 bool IsTdz() const
115 {
116 return (flags_ & IdentifierFlags::TDZ) != 0;
117 }
118
119 void SetTdz()
120 {
121 flags_ |= IdentifierFlags::TDZ;
122 }
123
124 void SetTsTypeAnnotation(Expression *typeAnnotation) override
125 {
126 typeAnnotation_ = typeAnnotation;
127 }
128
129 void Iterate(const NodeTraverser &cb) const override;
130 void Dump(ir::AstDumper *dumper) const override;
131 void Compile(compiler::PandaGen *pg) const override;
132 checker::Type *Check(checker::Checker *checker) const override;
133 void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
134
135 private:
136 util::StringView name_;
137 Expression *typeAnnotation_ {};
138 IdentifierFlags flags_ {IdentifierFlags::NONE};
139 std::vector<binder::Variable *> tsVariables_;
140 };
141
142 } // namespace panda::es2panda::ir
143
144 #endif
145