• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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 panda::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 
Key()55     [[nodiscard]] Expression *Key() noexcept
56     {
57         return key_;
58     }
59 
Key()60     [[nodiscard]] const Expression *Key() const noexcept
61     {
62         return key_;
63     }
64 
Value()65     [[nodiscard]] const Expression *Value() const noexcept
66     {
67         return value_;
68     }
69 
Value()70     [[nodiscard]] Expression *Value() noexcept
71     {
72         return value_;
73     }
74 
Kind()75     [[nodiscard]] PropertyKind Kind() const noexcept
76     {
77         return kind_;
78     }
79 
IsMethod()80     [[nodiscard]] bool IsMethod() const noexcept
81     {
82         return isMethod_;
83     }
84 
IsShorthand()85     [[nodiscard]] bool IsShorthand() const noexcept
86     {
87         return isShorthand_;
88     }
89 
IsComputed()90     [[nodiscard]] bool IsComputed() const noexcept
91     {
92         return isComputed_;
93     }
94 
IsAccessor()95     [[nodiscard]] bool IsAccessor() const noexcept
96     {
97         return IsAccessorKind(kind_);
98     }
99 
IsAccessorKind(PropertyKind kind)100     [[nodiscard]] static bool IsAccessorKind(PropertyKind kind) noexcept
101     {
102         return kind == PropertyKind::GET || kind == PropertyKind::SET;
103     }
104 
105     // NOLINTNEXTLINE(google-default-arguments)
106     [[nodiscard]] Property *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override;
107 
108     bool ConvertibleToPatternProperty();
109     ValidationInfo ValidateExpression();
110 
111     void TransformChildren(const NodeTransformer &cb) override;
112     void Iterate(const NodeTraverser &cb) const override;
113     void Dump(ir::AstDumper *dumper) const override;
114     void Dump(ir::SrcDumper *dumper) const override;
115     void Compile(compiler::PandaGen *pg) const override;
116     void Compile(compiler::ETSGen *etsg) const override;
117     checker::Type *Check(checker::TSChecker *checker) override;
118     checker::Type *Check(checker::ETSChecker *checker) override;
119 
Accept(ASTVisitorT * v)120     void Accept(ASTVisitorT *v) override
121     {
122         v->Accept(this);
123     }
124 
125 protected:
Property(Property const & other)126     Property(Property const &other) : Expression(static_cast<Expression const &>(other))
127     {
128         kind_ = other.kind_;
129         isMethod_ = other.isMethod_;
130         isShorthand_ = other.isShorthand_;
131         isComputed_ = other.isComputed_;
132     }
133 
134 private:
135     PropertyKind kind_;
136     Expression *key_ = nullptr;
137     Expression *value_ = nullptr;
138     bool isMethod_ = false;
139     bool isShorthand_ = true;
140     bool isComputed_ = false;
141 };
142 }  // namespace panda::es2panda::ir
143 
144 #endif
145