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, ArenaAllocator *allocator)
49 : Expression(AstNodeType::IDENTIFIER), name_(name), decorators_(allocator->Adapter())
50 {
51 }
52
53 explicit Identifier(util::StringView name, ArenaVector<Decorator *> &&decorators)
54 : Expression(AstNodeType::IDENTIFIER), name_(name), decorators_(std::move(decorators))
55 {
56 }
57
58 explicit Identifier(util::StringView name, Expression *typeAnnotation, ArenaAllocator *allocator)
59 : Expression(AstNodeType::IDENTIFIER),
60 name_(name),
61 typeAnnotation_(typeAnnotation),
62 decorators_(allocator->Adapter())
63 {
64 }
65
66 const Expression *TypeAnnotation() const
67 {
68 return typeAnnotation_;
69 }
70
71 Expression *TypeAnnotation()
72 {
73 return typeAnnotation_;
74 }
75
76 const util::StringView &Name() const
77 {
78 return name_;
79 }
80
81 const ArenaVector<Decorator *> &Decorators() const
82 {
83 return decorators_;
84 }
85
86 bool IsOptional() const
87 {
88 return (flags_ & IdentifierFlags::OPTIONAL) != 0;
89 }
90
91 void SetOptional(bool optional)
92 {
93 if (optional) {
94 flags_ |= IdentifierFlags::OPTIONAL;
95 } else {
96 flags_ &= ~IdentifierFlags::OPTIONAL;
97 }
98 }
99
100 bool IsReference() const
101 {
102 return (flags_ & IdentifierFlags::REFERENCE) != 0;
103 }
104
105 void SetReference()
106 {
107 flags_ |= IdentifierFlags::REFERENCE;
108 }
109
110 bool IsTdz() const
111 {
112 return (flags_ & IdentifierFlags::TDZ) != 0;
113 }
114
115 void SetTdz()
116 {
117 flags_ |= IdentifierFlags::TDZ;
118 }
119
120 void SetTsTypeAnnotation(Expression *typeAnnotation) override
121 {
122 typeAnnotation_ = typeAnnotation;
123 }
124
125 void Iterate(const NodeTraverser &cb) const override;
126 void Dump(ir::AstDumper *dumper) const override;
127 void Compile(compiler::PandaGen *pg) const override;
128 checker::Type *Check(checker::Checker *checker) const override;
129 void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
130
131 private:
132 util::StringView name_;
133 Expression *typeAnnotation_ {};
134 IdentifierFlags flags_ {IdentifierFlags::NONE};
135 ArenaVector<Decorator *> decorators_;
136 };
137
138 } // namespace panda::es2panda::ir
139
140 #endif
141