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_PROPERTY_H
17 #define ES2PANDA_PARSER_INCLUDE_AST_PROPERTY_H
18 
19 #include "ir/expression.h"
20 #include "ir/validationInfo.h"
21 
22 namespace ark::es2panda::ir {
23 enum class PropertyKind { INIT, GET, SET, PROTO };
24 
25 class Property : public Expression {
26 private:
27     struct Tag {};
28 
29 public:
30     Property() = delete;
31     ~Property() override = default;
32 
33     NO_COPY_OPERATOR(Property);
34     NO_MOVE_SEMANTIC(Property);
35 
Property(Expression *const key, Expression *const value)36     explicit Property(Expression *const key, Expression *const value)
37         : Expression(AstNodeType::PROPERTY), kind_(PropertyKind::INIT), key_(key), value_(value)
38     {
39     }
40 
Property(PropertyKind const kind, Expression *const key, Expression *const value, bool const isMethod, bool const isComputed)41     explicit Property(PropertyKind const kind, Expression *const key, Expression *const value, bool const isMethod,
42                       bool const isComputed)
43         : Expression(AstNodeType::PROPERTY),
44           kind_(kind),
45           key_(key),
46           value_(value),
47           isMethod_(isMethod),
48           isShorthand_(false),
49           isComputed_(isComputed)
50     {
51     }
52 
53     explicit Property(Tag tag, Property const &other, Expression *key, Expression *value);
54 
55     [[nodiscard]] Expression *Key() noexcept
56     {
57         return key_;
58     }
59 
60     [[nodiscard]] const Expression *Key() const noexcept
61     {
62         return key_;
63     }
64 
65     [[nodiscard]] const Expression *Value() const noexcept
66     {
67         return value_;
68     }
69 
70     [[nodiscard]] Expression *Value() noexcept
71     {
72         return value_;
73     }
74 
75     [[nodiscard]] PropertyKind Kind() const noexcept
76     {
77         return kind_;
78     }
79 
80     [[nodiscard]] bool IsMethod() const noexcept
81     {
82         return isMethod_;
83     }
84 
85     [[nodiscard]] bool IsShorthand() const noexcept
86     {
87         return isShorthand_;
88     }
89 
90     [[nodiscard]] bool IsComputed() const noexcept
91     {
92         return isComputed_;
93     }
94 
95     [[nodiscard]] bool IsAccessor() const noexcept
96     {
97         return IsAccessorKind(kind_);
98     }
99 
100     [[nodiscard]] static bool IsAccessorKind(PropertyKind kind) noexcept
101     {
102         return kind == PropertyKind::GET || kind == PropertyKind::SET;
103     }
104 
105     [[nodiscard]] Property *Clone(ArenaAllocator *allocator, AstNode *parent) override;
106 
107     bool ConvertibleToPatternProperty();
108     ValidationInfo ValidateExpression();
109 
110     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
111     void Iterate(const NodeTraverser &cb) const override;
112     void Dump(ir::AstDumper *dumper) const override;
113     void Dump(ir::SrcDumper *dumper) const override;
114     void Compile(compiler::PandaGen *pg) const override;
115     void Compile(compiler::ETSGen *etsg) const override;
116     checker::Type *Check(checker::TSChecker *checker) override;
117     checker::Type *Check(checker::ETSChecker *checker) override;
118 
119     void Accept(ASTVisitorT *v) override
120     {
121         v->Accept(this);
122     }
123 
124 protected:
Property(Property const &other)125     Property(Property const &other) : Expression(static_cast<Expression const &>(other))
126     {
127         kind_ = other.kind_;
128         isMethod_ = other.isMethod_;
129         isShorthand_ = other.isShorthand_;
130         isComputed_ = other.isComputed_;
131     }
132 
133 private:
134     PropertyKind kind_;
135     Expression *key_ = nullptr;
136     Expression *value_ = nullptr;
137     bool isMethod_ = false;
138     bool isShorthand_ = true;
139     bool isComputed_ = false;
140 };
141 }  // namespace ark::es2panda::ir
142 
143 #endif
144