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_PARSER_INCLUDE_AST_SPREAD_ELEMENT_H 17 #define ES2PANDA_PARSER_INCLUDE_AST_SPREAD_ELEMENT_H 18 19 #include "ir/expression.h" 20 #include "ir/validationInfo.h" 21 22 namespace ark::es2panda::ir { 23 class SpreadElement : public AnnotatedExpression { 24 private: 25 struct Tag {}; 26 27 public: 28 SpreadElement() = delete; 29 ~SpreadElement() override = default; 30 31 NO_COPY_SEMANTIC(SpreadElement); 32 NO_MOVE_SEMANTIC(SpreadElement); 33 SpreadElement(AstNodeType const nodeType, ArenaAllocator *const allocator, Expression *const argument)34 explicit SpreadElement(AstNodeType const nodeType, ArenaAllocator *const allocator, Expression *const argument) 35 : AnnotatedExpression(nodeType), decorators_(allocator->Adapter()), argument_(argument) 36 { 37 } 38 39 explicit SpreadElement(Tag tag, SpreadElement const &other, ArenaAllocator *allocator); 40 41 [[nodiscard]] const Expression *Argument() const noexcept 42 { 43 return argument_; 44 } 45 46 [[nodiscard]] Expression *Argument() noexcept 47 { 48 return argument_; 49 } 50 51 [[nodiscard]] bool IsOptional() const noexcept 52 { 53 return optional_; 54 } 55 56 [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept 57 { 58 return decorators_; 59 } 60 61 const ArenaVector<Decorator *> *DecoratorsPtr() const override 62 { 63 return &Decorators(); 64 } 65 66 void AddDecorators(ArenaVector<Decorator *> &&decorators) override 67 { 68 decorators_ = std::move(decorators); 69 } 70 71 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 72 { 73 return true; 74 } 75 76 void SetOptional(bool optional) noexcept 77 { 78 optional_ = optional; 79 } 80 81 [[nodiscard]] SpreadElement *Clone(ArenaAllocator *allocator, AstNode *parent) override; 82 83 ValidationInfo ValidateExpression(); 84 [[nodiscard]] bool ConvertibleToRest(bool isDeclaration, bool allowPattern = true); 85 86 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 87 void Iterate(const NodeTraverser &cb) const override; 88 void Dump(ir::AstDumper *dumper) const override; 89 void Dump(ir::SrcDumper *dumper) const override; 90 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 91 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 92 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 93 checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; 94 95 void Accept(ASTVisitorT *v) override 96 { 97 v->Accept(this); 98 } 99 100 private: 101 ArenaVector<Decorator *> decorators_; 102 Expression *argument_ = nullptr; 103 bool optional_ {false}; 104 }; 105 } // namespace ark::es2panda::ir 106 107 #endif 108