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