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_OBJECT_EXPRESSION_H 17 #define ES2PANDA_IR_EXPRESSION_OBJECT_EXPRESSION_H 18 19 #include "varbinder/variable.h" 20 #include "ir/expression.h" 21 #include "ir/validationInfo.h" 22 namespace ark::es2panda::checker { 23 class ETSAnalyzer; 24 } // namespace ark::es2panda::checker 25 namespace ark::es2panda::util { 26 class BitSet; 27 } // namespace ark::es2panda::util 28 29 namespace ark::es2panda::ir { 30 class ObjectExpression : public AnnotatedExpression { 31 private: 32 struct Tag {}; 33 34 public: 35 ObjectExpression() = delete; 36 ~ObjectExpression() override = default; 37 38 NO_COPY_SEMANTIC(ObjectExpression); 39 NO_MOVE_SEMANTIC(ObjectExpression); 40 ObjectExpression(AstNodeType nodeType, ArenaAllocator *allocator, ArenaVector<Expression *> &&properties, bool trailingComma)41 explicit ObjectExpression(AstNodeType nodeType, ArenaAllocator *allocator, ArenaVector<Expression *> &&properties, 42 bool trailingComma) 43 : AnnotatedExpression(nodeType), 44 decorators_(allocator->Adapter()), 45 properties_(std::move(properties)), 46 trailingComma_(trailingComma) 47 { 48 } 49 explicit ObjectExpression(Tag tag, ObjectExpression const &other, ArenaAllocator *allocator); 50 51 // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields 52 friend class checker::ETSAnalyzer; 53 54 [[nodiscard]] const ArenaVector<Expression *> &Properties() const noexcept 55 { 56 return properties_; 57 } 58 59 [[nodiscard]] bool IsDeclaration() const noexcept 60 { 61 return isDeclaration_; 62 } 63 64 [[nodiscard]] bool IsOptional() const noexcept 65 { 66 return optional_; 67 } 68 69 void SetPreferredType(checker::Type *const preferredType) noexcept 70 { 71 preferredType_ = preferredType; 72 } 73 74 [[nodiscard]] checker::Type *PreferredType() const noexcept 75 { 76 return preferredType_; 77 } 78 79 [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept 80 { 81 return decorators_; 82 } 83 84 const ArenaVector<Decorator *> *DecoratorsPtr() const override 85 { 86 return &Decorators(); 87 } 88 89 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 90 { 91 decorators_ = std::move(decorators); 92 } 93 94 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 95 { 96 return true; 97 } 98 99 [[nodiscard]] ObjectExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override; 100 101 [[nodiscard]] ValidationInfo ValidateExpression(); 102 [[nodiscard]] bool ConvertibleToObjectPattern(); 103 void SetDeclaration(); 104 void SetOptional(bool optional); 105 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 106 void Iterate(const NodeTraverser &cb) const override; 107 void Dump(ir::AstDumper *dumper) const override; 108 void Dump(ir::SrcDumper *dumper) const override; 109 void Compile(compiler::PandaGen *pg) const override; 110 void Compile(compiler::ETSGen *etsg) const override; 111 checker::Type *Check(checker::TSChecker *checker) override; 112 checker::Type *Check(checker::ETSChecker *checker) override; 113 checker::Type *CheckPattern(checker::TSChecker *checker); 114 115 bool CheckAssignmentPattern(Property *prop, varbinder::Variable *&bindingVar, checker::Type *&patternParamType, 116 checker::TSChecker *checker, varbinder::LocalVariable *foundVar); 117 118 void Accept(ASTVisitorT *v) override 119 { 120 v->Accept(this); 121 } 122 123 private: 124 ArenaVector<Decorator *> decorators_; 125 ArenaVector<Expression *> properties_; 126 checker::Type *preferredType_ {}; 127 bool isDeclaration_ {}; 128 bool trailingComma_ {}; 129 bool optional_ {}; 130 }; 131 } // namespace ark::es2panda::ir 132 133 #endif 134