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_IDENTIFIER_H 17 #define ES2PANDA_IR_EXPRESSION_IDENTIFIER_H 18 19 #include "ir/expression.h" 20 #include "ir/validationInfo.h" 21 #include <varbinder/varbinder.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 ERROR_PLACEHOLDER = 1U << 8U, 42 }; 43 44 } // namespace ark::es2panda::ir 45 46 template <> 47 struct enumbitops::IsAllowedType<ark::es2panda::ir::IdentifierFlags> : std::true_type { 48 }; 49 50 namespace ark::es2panda::ir { 51 52 class Identifier : public AnnotatedExpression { 53 private: 54 struct Tag {}; 55 56 public: 57 Identifier() = delete; 58 ~Identifier() override = default; 59 60 NO_COPY_SEMANTIC(Identifier); 61 NO_MOVE_SEMANTIC(Identifier); 62 63 public: 64 explicit Identifier(ArenaAllocator *const allocator); 65 explicit Identifier(util::StringView const name, ArenaAllocator *const allocator); 66 explicit Identifier(util::StringView const name, TypeNode *const typeAnnotation, ArenaAllocator *const allocator); 67 explicit Identifier(Tag tag, Identifier const &other, ArenaAllocator *allocator); 68 69 [[nodiscard]] const util::StringView &Name() const noexcept 70 { 71 return name_; 72 } 73 74 [[nodiscard]] util::StringView &Name() noexcept 75 { 76 return name_; 77 } 78 79 void SetName(const util::StringView &newName) noexcept; 80 81 [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept 82 { 83 return decorators_; 84 } 85 86 const ArenaVector<Decorator *> *DecoratorsPtr() const override 87 { 88 return &Decorators(); 89 } 90 91 bool IsErrorPlaceHolder() const noexcept 92 { 93 return (flags_ & IdentifierFlags::ERROR_PLACEHOLDER) != 0; 94 } 95 96 [[nodiscard]] bool IsOptional() const noexcept 97 { 98 return (flags_ & IdentifierFlags::OPTIONAL) != 0; 99 } 100 101 void SetOptional(bool const optional) noexcept 102 { 103 if (optional) { 104 flags_ |= IdentifierFlags::OPTIONAL; 105 } else { 106 flags_ &= ~IdentifierFlags::OPTIONAL; 107 } 108 } 109 110 [[nodiscard]] bool IsReference(ScriptExtension ext) const noexcept 111 { 112 return !IsDeclaration(ext); 113 } 114 115 [[nodiscard]] bool IsTdz() const noexcept 116 { 117 return (flags_ & IdentifierFlags::TDZ) != 0; 118 } 119 120 void SetTdz() noexcept 121 { 122 flags_ |= IdentifierFlags::TDZ; 123 } 124 125 void SetAccessor() noexcept 126 { 127 flags_ |= IdentifierFlags::GET; 128 } 129 130 [[nodiscard]] bool IsAccessor() const noexcept 131 { 132 return (flags_ & IdentifierFlags::GET) != 0; 133 } 134 135 void SetMutator() noexcept 136 { 137 flags_ |= IdentifierFlags::SET; 138 } 139 140 [[nodiscard]] bool IsMutator() const noexcept 141 { 142 return (flags_ & IdentifierFlags::SET) != 0; 143 } 144 145 [[nodiscard]] bool IsReceiver() const noexcept 146 { 147 return name_ == varbinder::VarBinder::MANDATORY_PARAM_THIS; 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::VerifiedType Check(checker::ETSChecker *checker) override; 217 218 std::string ToString() const override; 219 220 void Accept(ASTVisitorT *v) override 221 { 222 v->Accept(this); 223 } 224 225 private: 226 bool CheckDeclarationsPart2(const ir::AstNode *parent, ScriptExtension ext) const; 227 bool CheckDeclarationsPart1(const ir::AstNode *parent, ScriptExtension ext) const; 228 bool CheckNotDeclarations(const ir::AstNode *parent, ScriptExtension ext) const; 229 bool CheckDefinitions(const ir::AstNode *parent, ScriptExtension ext) const; 230 bool IsDeclaration(ScriptExtension ext) const; 231 232 util::StringView name_; 233 IdentifierFlags flags_ {IdentifierFlags::NONE}; 234 ArenaVector<Decorator *> decorators_; 235 }; 236 } // namespace ark::es2panda::ir 237 238 #endif 239