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_IR_TS_INTERFACE_DECLARATION_H
17 #define ES2PANDA_IR_TS_INTERFACE_DECLARATION_H
18 
19 #include "varbinder/scope.h"
20 #include "ir/statement.h"
21 
22 namespace ark::es2panda::varbinder {
23 class Variable;
24 }  // namespace ark::es2panda::varbinder
25 
26 namespace ark::es2panda::ir {
27 class Identifier;
28 class TSInterfaceBody;
29 class TSInterfaceHeritage;
30 class TSTypeParameterDeclaration;
31 
32 class TSInterfaceDeclaration : public TypedStatement {
33 public:
34     // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
35     struct ConstructorData {
36         Identifier *id;
37         TSTypeParameterDeclaration *typeParams;
38         TSInterfaceBody *body;
39         bool isStatic;
40         bool isExternal;
41         es2panda::Language lang;
42     };
43     // NOLINTEND(cppcoreguidelines-pro-type-member-init)
44 
TSInterfaceDeclaration(ArenaAllocator *allocator, ArenaVector<TSInterfaceHeritage *> &&extends, ConstructorData &&data)45     explicit TSInterfaceDeclaration(ArenaAllocator *allocator, ArenaVector<TSInterfaceHeritage *> &&extends,
46                                     ConstructorData &&data)
47         : TypedStatement(AstNodeType::TS_INTERFACE_DECLARATION),
48           decorators_(allocator->Adapter()),
49           id_(data.id),
50           typeParams_(data.typeParams),
51           body_(data.body),
52           extends_(std::move(extends)),
53           isStatic_(data.isStatic),
54           isExternal_(data.isExternal),
55           lang_(data.lang)
56     {
57         if (isStatic_) {
58             AddModifier(ir::ModifierFlags::STATIC);
59         }
60     }
61 
62     [[nodiscard]] bool IsScopeBearer() const noexcept override
63     {
64         return true;
65     }
66 
67     [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override
68     {
69         return scope_;
70     }
71 
SetScope(varbinder::LocalScope *scope)72     void SetScope(varbinder::LocalScope *scope)
73     {
74         ASSERT(scope_ == nullptr);
75         scope_ = scope;
76     }
77 
78     void ClearScope() noexcept override
79     {
80         scope_ = nullptr;
81     }
82 
Body()83     TSInterfaceBody *Body()
84     {
85         return body_;
86     }
87 
Body() const88     const TSInterfaceBody *Body() const
89     {
90         return body_;
91     }
92 
Id()93     Identifier *Id()
94     {
95         return id_;
96     }
97 
Id() const98     const Identifier *Id() const
99     {
100         return id_;
101     }
102 
InternalName() const103     const util::StringView &InternalName() const
104     {
105         return internalName_;
106     }
107 
SetInternalName(util::StringView internalName)108     void SetInternalName(util::StringView internalName)
109     {
110         internalName_ = internalName;
111     }
112 
IsStatic() const113     bool IsStatic() const
114     {
115         return isStatic_;
116     }
117 
IsFromExternal() const118     bool IsFromExternal() const
119     {
120         return isExternal_;
121     }
122 
TypeParams() const123     const TSTypeParameterDeclaration *TypeParams() const
124     {
125         return typeParams_;
126     }
127 
TypeParams()128     TSTypeParameterDeclaration *TypeParams()
129     {
130         return typeParams_;
131     }
132 
Extends()133     ArenaVector<TSInterfaceHeritage *> &Extends()
134     {
135         return extends_;
136     }
137 
Extends() const138     const ArenaVector<TSInterfaceHeritage *> &Extends() const
139     {
140         return extends_;
141     }
142 
Decorators() const143     const ArenaVector<Decorator *> &Decorators() const
144     {
145         return decorators_;
146     }
147 
148     const ArenaVector<Decorator *> *DecoratorsPtr() const override
149     {
150         return &Decorators();
151     }
152 
153     void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override
154     {
155         decorators_ = std::move(decorators);
156     }
157 
158     bool CanHaveDecorator([[maybe_unused]] bool inTs) const override
159     {
160         return !inTs;
161     }
162 
163     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
164 
Language() const165     es2panda::Language Language() const
166     {
167         return lang_;
168     }
169 
170     ClassDeclaration *GetAnonClass() noexcept
171     {
172         return anonClass_;
173     }
174 
175     ClassDeclaration *GetAnonClass() const noexcept
176     {
177         return anonClass_;
178     }
179 
180     void SetAnonClass(ClassDeclaration *anonClass) noexcept
181     {
182         anonClass_ = anonClass;
183     }
184 
185     void Iterate(const NodeTraverser &cb) const override;
186     void Dump(ir::AstDumper *dumper) const override;
187     void Dump(ir::SrcDumper *dumper) const override;
188     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
189     void Compile(compiler::ETSGen *etsg) const override;
190     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
191     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
192     checker::Type *InferType(checker::TSChecker *checker, varbinder::Variable *bindingVar) const;
193 
194     void Accept(ASTVisitorT *v) override
195     {
196         v->Accept(this);
197     }
198 
199 private:
200     ArenaVector<Decorator *> decorators_;
201     varbinder::LocalScope *scope_ {nullptr};
202     Identifier *id_;
203     TSTypeParameterDeclaration *typeParams_;
204     TSInterfaceBody *body_;
205     ArenaVector<TSInterfaceHeritage *> extends_;
206     util::StringView internalName_ {};
207     bool isStatic_;
208     bool isExternal_;
209     es2panda::Language lang_;
210     ClassDeclaration *anonClass_ {nullptr};
211 };
212 }  // namespace ark::es2panda::ir
213 
214 #endif
215