1 /** 2 * Copyright (c) 2021-2024 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 "checker/checkerContext.h" 20 #include "ir/expression.h" 21 #include "ir/validationInfo.h" 22 23 namespace ark::es2panda::varbinder { 24 class Variable; 25 } // namespace ark::es2panda::varbinder 26 27 namespace ark::es2panda::ir { 28 29 using ENUMBITOPS_OPERATORS; 30 31 enum class IdentifierFlags : uint32_t { 32 NONE = 0U, 33 OPTIONAL = 1U << 0U, 34 TDZ = 1U << 1U, 35 PRIVATE = 1U << 2U, 36 GET = 1U << 3U, 37 SET = 1U << 4U, 38 IGNORE_BOX = 1U << 5U, 39 ANNOTATIONDECL = 1U << 6U, 40 ANNOTATIONUSAGE = 1U << 7U, 41 }; 42 43 } // namespace ark::es2panda::ir 44 45 template <> 46 struct enumbitops::IsAllowedType<ark::es2panda::ir::IdentifierFlags> : std::true_type { 47 }; 48 49 namespace ark::es2panda::ir { 50 51 class Identifier : public AnnotatedExpression { 52 private: 53 struct Tag {}; 54 55 public: 56 Identifier() = delete; 57 ~Identifier() override = default; 58 59 NO_COPY_SEMANTIC(Identifier); 60 NO_MOVE_SEMANTIC(Identifier); 61 62 public: 63 explicit Identifier(ArenaAllocator *const allocator) : Identifier("", allocator) {} 64 explicit Identifier(util::StringView const name, ArenaAllocator *const allocator) 65 : AnnotatedExpression(AstNodeType::IDENTIFIER), name_(name), decorators_(allocator->Adapter()) 66 { 67 } 68 69 explicit Identifier(util::StringView const name, TypeNode *const typeAnnotation, ArenaAllocator *const allocator) 70 : AnnotatedExpression(AstNodeType::IDENTIFIER, typeAnnotation), name_(name), decorators_(allocator->Adapter()) 71 { 72 } 73 74 explicit Identifier(Tag tag, Identifier const &other, ArenaAllocator *allocator); 75 76 [[nodiscard]] const util::StringView &Name() const noexcept 77 { 78 return name_; 79 } 80 81 [[nodiscard]] util::StringView &Name() noexcept 82 { 83 return name_; 84 } 85 86 void SetName(const util::StringView &newName) noexcept 87 { 88 name_ = newName; 89 } 90 91 [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept 92 { 93 return decorators_; 94 } 95 96 const ArenaVector<Decorator *> *DecoratorsPtr() const override 97 { 98 return &Decorators(); 99 } 100 101 [[nodiscard]] bool IsOptional() const noexcept 102 { 103 return (flags_ & IdentifierFlags::OPTIONAL) != 0; 104 } 105 106 void SetOptional(bool const optional) noexcept 107 { 108 if (optional) { 109 flags_ |= IdentifierFlags::OPTIONAL; 110 } else { 111 flags_ &= ~IdentifierFlags::OPTIONAL; 112 } 113 } 114 115 [[nodiscard]] bool IsReference(ScriptExtension ext) const noexcept 116 { 117 return !IsDeclaration(ext); 118 } 119 120 [[nodiscard]] bool IsTdz() const noexcept 121 { 122 return (flags_ & IdentifierFlags::TDZ) != 0; 123 } 124 125 void SetTdz() noexcept 126 { 127 flags_ |= IdentifierFlags::TDZ; 128 } 129 130 void SetAccessor() noexcept 131 { 132 flags_ |= IdentifierFlags::GET; 133 } 134 135 [[nodiscard]] bool IsAccessor() const noexcept 136 { 137 return (flags_ & IdentifierFlags::GET) != 0; 138 } 139 140 void SetMutator() noexcept 141 { 142 flags_ |= IdentifierFlags::SET; 143 } 144 145 [[nodiscard]] bool IsMutator() const noexcept 146 { 147 return (flags_ & IdentifierFlags::SET) != 0; 148 } 149 150 [[nodiscard]] bool IsPrivateIdent() const noexcept 151 { 152 return (flags_ & IdentifierFlags::PRIVATE) != 0; 153 } 154 155 void SetPrivate(bool const isPrivate) noexcept 156 { 157 if (isPrivate) { 158 flags_ |= IdentifierFlags::PRIVATE; 159 } else { 160 flags_ &= ~IdentifierFlags::PRIVATE; 161 } 162 } 163 164 [[nodiscard]] bool IsIgnoreBox() const noexcept 165 { 166 return (flags_ & IdentifierFlags::IGNORE_BOX) != 0; 167 } 168 169 void SetIgnoreBox() noexcept 170 { 171 flags_ |= IdentifierFlags::IGNORE_BOX; 172 } 173 174 [[nodiscard]] bool IsAnnotationDecl() const noexcept 175 { 176 return (flags_ & IdentifierFlags::ANNOTATIONDECL) != 0; 177 } 178 179 void SetAnnotationDecl() noexcept 180 { 181 flags_ |= IdentifierFlags::ANNOTATIONDECL; 182 } 183 184 [[nodiscard]] bool IsAnnotationUsage() const noexcept 185 { 186 return (flags_ & IdentifierFlags::ANNOTATIONUSAGE) != 0; 187 } 188 189 void SetAnnotationUsage() noexcept 190 { 191 flags_ |= IdentifierFlags::ANNOTATIONUSAGE; 192 } 193 194 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 195 { 196 decorators_ = std::move(decorators); 197 } 198 199 [[nodiscard]] Identifier *Clone(ArenaAllocator *allocator, AstNode *parent) override; 200 [[nodiscard]] Identifier *CloneReference(ArenaAllocator *allocator, AstNode *parent); 201 202 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 203 { 204 return true; 205 } 206 207 [[nodiscard]] ValidationInfo ValidateExpression(); 208 209 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 210 void Iterate(const NodeTraverser &cb) const override; 211 void Dump(ir::AstDumper *dumper) const override; 212 void Dump(ir::SrcDumper *dumper) const override; 213 void Compile(compiler::PandaGen *pg) const override; 214 void Compile(compiler::ETSGen *etsg) const override; 215 checker::Type *Check(checker::TSChecker *checker) override; 216 checker::Type *Check(checker::ETSChecker *checker) override; 217 218 void Accept(ASTVisitorT *v) override 219 { 220 v->Accept(this); 221 } 222 223 private: 224 bool CheckDeclarationsPart2(const ir::AstNode *parent, ScriptExtension ext) const; 225 bool CheckDeclarationsPart1(const ir::AstNode *parent, ScriptExtension ext) const; 226 bool CheckNotDeclarations(const ir::AstNode *parent, ScriptExtension ext) const; 227 bool CheckDefinitions(const ir::AstNode *parent, ScriptExtension ext) const; 228 bool IsDeclaration(ScriptExtension ext) const; 229 230 util::StringView name_; 231 IdentifierFlags flags_ {IdentifierFlags::NONE}; 232 ArenaVector<Decorator *> decorators_; 233 }; 234 } // namespace ark::es2panda::ir 235 236 #endif 237