1 /**
2  * Copyright (c) 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_DUMMYNODE_H
17 #define ES2PANDA_IR_EXPRESSION_DUMMYNODE_H
18 
19 #include "ir/astNode.h"
20 #include "ir/visitor/AstVisitor.h"
21 #include "util/ustring.h"
22 
23 namespace ark::es2panda::ir {
24 
25 using ENUMBITOPS_OPERATORS;
26 
27 enum class DummyNodeFlag : uint32_t { NONE = 0U, INDEXER = 1U << 0U };
28 
29 }  // namespace ark::es2panda::ir
30 
31 template <>
32 struct enumbitops::IsAllowedType<ark::es2panda::ir::DummyNodeFlag> : std::true_type {
33 };
34 
35 namespace ark::es2panda::ir {
36 
37 class DummyNode : public AstNode {
38 public:
39     DummyNode() = delete;
40     ~DummyNode() override = default;
41 
42     NO_COPY_SEMANTIC(DummyNode);
43     NO_MOVE_SEMANTIC(DummyNode);
44 
45 public:
DummyNode(util::StringView const name, util::StringView indexName, TypeNode *returnType, DummyNodeFlag flag)46     explicit DummyNode(util::StringView const name, util::StringView indexName, TypeNode *returnType,
47                        DummyNodeFlag flag)
48         : AstNode(AstNodeType::DUMMYNODE), name_(name), indexName_(indexName), returnType_(returnType), flag_(flag)
49     {
50     }
51 
52     [[nodiscard]] bool IsAmbientIndexer() const noexcept
53     {
54         return (flag_ & ir::DummyNodeFlag::INDEXER) != 0;
55     }
56 
57     void SetAmbientIndexer(bool const ambientIndexer) noexcept
58     {
59         if (ambientIndexer) {
60             flag_ |= DummyNodeFlag::INDEXER;
61         } else {
62             flag_ &= ~DummyNodeFlag::INDEXER;
63         }
64     }
65 
GetIndexName() const66     [[nodiscard]] util::StringView GetIndexName() const
67     {
68         return indexName_;
69     }
70 
SetIndexName(const util::StringView &indexName)71     void SetIndexName(const util::StringView &indexName)
72     {
73         indexName_ = indexName;
74     }
75 
GetReturnTypeLiteral() const76     [[nodiscard]] TypeNode *GetReturnTypeLiteral() const
77     {
78         return returnType_;
79     }
80 
SetReturnTypeLiteral(TypeNode *returnType)81     void SetReturnTypeLiteral(TypeNode *returnType)
82     {
83         returnType_ = returnType;
84     }
85 
Name() const86     util::StringView Name() const
87     {
88         return name_;
89     }
90 
operator ==(const DummyNode &node) const91     bool operator==(const DummyNode &node) const
92     {
93         return name_.Is(std::string(node.Name().Bytes()));
94     }
95 
96     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
97     void Iterate(const NodeTraverser &cb) const override;
98     void Dump(ir::AstDumper *dumper) const override;
99     void Dump(ir::SrcDumper *dumper) const override;
100     void Compile(compiler::PandaGen *pg) const override;
101     void Compile(compiler::ETSGen *etsg) const override;
102     checker::Type *Check(checker::TSChecker *checker) override;
103     checker::Type *Check(checker::ETSChecker *checker) override;
104 
105     void Accept(ASTVisitorT *v) override
106     {
107         v->Accept(this);
108     }
109 
110 private:
111     util::StringView name_;
112     util::StringView indexName_;
113     TypeNode *returnType_;
114     DummyNodeFlag flag_ {DummyNodeFlag::NONE};
115 };
116 }  // namespace ark::es2panda::ir
117 
118 #endif
119