1 /**
2  * Copyright (c) 2021 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 <binder/variable.h>
20 #include <ir/expression.h>
21 #include <ir/validationInfo.h>
22 
23 namespace panda::es2panda::compiler {
24 class PandaGen;
25 class LiteralBuffer;
26 }  // namespace panda::es2panda::compiler
27 
28 namespace panda::es2panda::checker {
29 class Checker;
30 class Type;
31 }  // namespace panda::es2panda::checker
32 
33 namespace panda::es2panda::util {
34 class BitSet;
35 }  // namespace panda::es2panda::util
36 
37 namespace panda::es2panda::ir {
38 
39 class ObjectExpression : public Expression {
40 public:
ObjectExpression(AstNodeType nodeType, ArenaVector<Expression *> &&properties, bool trailingComma)41     explicit ObjectExpression(AstNodeType nodeType, ArenaVector<Expression *> &&properties, bool trailingComma)
42         : Expression(nodeType),
43           properties_(std::move(properties)),
44           typeAnnotation_(nullptr),
45           trailingComma_(trailingComma)
46     {
47     }
48 
Properties() const49     const ArenaVector<Expression *> &Properties() const
50     {
51         return properties_;
52     }
53 
TypeAnnotation() const54     const Expression *TypeAnnotation() const
55     {
56         return typeAnnotation_;
57     }
58 
TypeAnnotation()59     Expression *TypeAnnotation()
60     {
61         return typeAnnotation_;
62     }
63 
IsDeclaration() const64     bool IsDeclaration() const
65     {
66         return isDeclaration_;
67     }
68 
Optional() const69     bool Optional() const
70     {
71         return optional_;
72     }
73 
74     ValidationInfo ValidateExpression();
75     bool ConvertibleToObjectPattern();
76 
77     void SetDeclaration();
78     void SetTsTypeAnnotation(Expression *typeAnnotation) override;
79     void SetOptional(bool optional);
80     void Iterate(const NodeTraverser &cb) const override;
81     void Dump(ir::AstDumper *dumper) const override;
82     void Compile(compiler::PandaGen *pg) const override;
83     checker::Type *Check(checker::Checker *checker) const override;
84     checker::Type *CheckPattern(checker::Checker *checker) const;
85     void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
86 
87 private:
88     void FillInLiteralBuffer(compiler::LiteralBuffer *buf,
89                              std::vector<std::vector<const Literal *>> &tempLiteralBuffer) const;
90     void EmitCreateObjectWithBuffer(compiler::PandaGen *pg, compiler::LiteralBuffer *buf, bool hasMethod) const;
91     void CompileStaticProperties(compiler::PandaGen *pg, util::BitSet *compiled) const;
92     void CompileRemainingProperties(compiler::PandaGen *pg, const util::BitSet *compiled, compiler::VReg objReg) const;
93     void CompilePropertyOfGetterOrSetter(compiler::PandaGen *pg, const ir::Property *prop,
94         compiler::VReg objReg) const;
95     void CompilePropertyWithInit(compiler::PandaGen *pg, const ir::Property *prop, compiler::VReg objReg) const;
96 
97     ArenaVector<Expression *> properties_;
98     Expression *typeAnnotation_;
99     bool isDeclaration_ {};
100     bool trailingComma_ {};
101     bool optional_ {false};
102 };
103 
104 }  // namespace panda::es2panda::ir
105 
106 #endif
107