• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
Properties()54     [[nodiscard]] const ArenaVector<Expression *> &Properties() const noexcept
55     {
56         return properties_;
57     }
58 
IsDeclaration()59     [[nodiscard]] bool IsDeclaration() const noexcept
60     {
61         return isDeclaration_;
62     }
63 
IsOptional()64     [[nodiscard]] bool IsOptional() const noexcept
65     {
66         return optional_;
67     }
68 
SetPreferredType(checker::Type * const preferredType)69     void SetPreferredType(checker::Type *const preferredType) noexcept
70     {
71         preferredType_ = preferredType;
72     }
73 
PreferredType()74     [[nodiscard]] checker::Type *PreferredType() const noexcept
75     {
76         return preferredType_;
77     }
78 
Decorators()79     [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept
80     {
81         return decorators_;
82     }
83 
DecoratorsPtr()84     const ArenaVector<Decorator *> *DecoratorsPtr() const override
85     {
86         return &Decorators();
87     }
88 
AddDecorators(ArenaVector<ir::Decorator * > && decorators)89     void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override
90     {
91         decorators_ = std::move(decorators);
92     }
93 
CanHaveDecorator(bool inTs)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::VerifiedType Check(checker::ETSChecker *checker) override;
113     struct CheckPatternIsShorthandArgs {
114         checker::TSChecker *checker;
115         checker::Type *patternParamType;
116         varbinder::Variable *bindingVar;
117         ir::Property *prop;
118         varbinder::LocalVariable *foundVar;
119         bool isOptional;
120     };
121     checker::Type *CheckPattern(checker::TSChecker *checker);
122 
123     bool CheckAssignmentPattern(Property *prop, varbinder::Variable *&bindingVar, checker::Type *&patternParamType,
124                                 checker::TSChecker *checker, varbinder::LocalVariable *foundVar);
125 
Accept(ASTVisitorT * v)126     void Accept(ASTVisitorT *v) override
127     {
128         v->Accept(this);
129     }
130 
CleanUp()131     void CleanUp() override
132     {
133         AstNode::CleanUp();
134         preferredType_ = nullptr;
135     }
136 
137 private:
138     std::tuple<bool, varbinder::Variable *, checker::Type *, varbinder::LocalVariable *> CheckPatternIsShorthand(
139         CheckPatternIsShorthandArgs *args);
140 
141     ArenaVector<Decorator *> decorators_;
142     ArenaVector<Expression *> properties_;
143     checker::Type *preferredType_ {};
144     bool isDeclaration_ {};
145     bool trailingComma_ {};
146     bool optional_ {};
147 };
148 }  // namespace ark::es2panda::ir
149 
150 #endif
151