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_STATEMENT_VARIABLE_DECLARATION_H 17 #define ES2PANDA_IR_STATEMENT_VARIABLE_DECLARATION_H 18 19 #include "ir/annotationAllowed.h" 20 #include "ir/jsDocAllowed.h" 21 #include "ir/statement.h" 22 #include "ir/expressions/identifier.h" 23 #include "ir/statements/annotationUsage.h" 24 #include "variableDeclarator.h" 25 26 namespace ark::es2panda::ir { 27 class VariableDeclarator; 28 29 class VariableDeclaration : public JsDocAllowed<AnnotationAllowed<Statement>> { 30 private: 31 struct Tag {}; 32 33 public: 34 enum class VariableDeclarationKind { CONST, LET, VAR }; 35 VariableDeclaration(VariableDeclarationKind kind,ArenaAllocator * allocator,ArenaVector<VariableDeclarator * > && declarators)36 explicit VariableDeclaration(VariableDeclarationKind kind, ArenaAllocator *allocator, 37 ArenaVector<VariableDeclarator *> &&declarators) 38 : JsDocAllowed<AnnotationAllowed<Statement>>(AstNodeType::VARIABLE_DECLARATION, allocator), 39 kind_(kind), 40 decorators_(allocator->Adapter()), 41 declarators_(std::move(declarators)) 42 { 43 } 44 45 explicit VariableDeclaration(Tag tag, VariableDeclaration const &other, ArenaAllocator *allocator); 46 Declarators()47 const ArenaVector<VariableDeclarator *> &Declarators() const 48 { 49 return declarators_; 50 } 51 Kind()52 VariableDeclarationKind Kind() const 53 { 54 return kind_; 55 } 56 Decorators()57 const ArenaVector<Decorator *> &Decorators() const 58 { 59 return decorators_; 60 } 61 GetDeclaratorByName(util::StringView name)62 VariableDeclarator *GetDeclaratorByName(util::StringView name) const 63 { 64 for (VariableDeclarator *declarator : declarators_) { 65 if (declarator->Id()->AsIdentifier()->Name().Compare(name) == 0) { 66 return declarator; 67 } 68 } 69 return nullptr; 70 } 71 DecoratorsPtr()72 const ArenaVector<Decorator *> *DecoratorsPtr() const override 73 { 74 return &Decorators(); 75 } 76 AddDecorators(ArenaVector<ir::Decorator * > && decorators)77 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 78 { 79 decorators_ = std::move(decorators); 80 } 81 CanHaveDecorator(bool inTs)82 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 83 { 84 return true; 85 } 86 87 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 88 void Iterate(const NodeTraverser &cb) const override; 89 void Dump(ir::AstDumper *dumper) const override; 90 void Dump(ir::SrcDumper *dumper) const override; 91 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 92 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 93 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 94 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 95 Accept(ASTVisitorT * v)96 void Accept(ASTVisitorT *v) override 97 { 98 v->Accept(this); 99 } 100 101 [[nodiscard]] VariableDeclaration *Clone(ArenaAllocator *allocator, AstNode *parent) override; 102 103 VariableDeclaration *Construct(ArenaAllocator *allocator) override; 104 void CopyTo(AstNode *other) const override; 105 106 private: 107 friend class SizeOfNodeTest; 108 VariableDeclarationKind kind_; 109 ArenaVector<Decorator *> decorators_; 110 ArenaVector<VariableDeclarator *> declarators_; 111 }; 112 } // namespace ark::es2panda::ir 113 114 #endif 115