• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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_COMPILER_CHECKER_TYPES_SIGNATURE_H
17 #define ES2PANDA_COMPILER_CHECKER_TYPES_SIGNATURE_H
18 
19 #include "ir/ets/etsParameterExpression.h"
20 #include "type.h"
21 
22 #include "varbinder/declaration.h"
23 #include "varbinder/variable.h"
24 
25 namespace ark::es2panda::public_lib {
26 struct Context;
27 }  // namespace ark::es2panda::public_lib
28 
29 namespace ark::es2panda::checker {
30 
31 class SignatureInfo final {
32 public:
SignatureInfo(ArenaAllocator * allocator)33     explicit SignatureInfo(ArenaAllocator *allocator) : typeParams {allocator->Adapter()}, params {allocator->Adapter()}
34     {
35     }
36 
SignatureInfo(const SignatureInfo * other,ArenaAllocator * allocator)37     SignatureInfo(const SignatureInfo *other, ArenaAllocator *allocator)
38         : typeParams(allocator->Adapter()), params(allocator->Adapter())
39     {
40         for (auto *it : other->typeParams) {
41             typeParams.push_back(it);
42         }
43         for (auto *it : other->params) {
44             params.push_back(it->Copy(allocator, it->Declaration()));
45             params.back()->SetTsType(it->TsType());
46         }
47 
48         minArgCount = other->minArgCount;
49 
50         if (other->restVar != nullptr) {
51             restVar = other->restVar->Copy(allocator, other->restVar->Declaration());
52             ES2PANDA_ASSERT(restVar != nullptr);
53             restVar->SetTsType(other->restVar->TsType());
54         }
55     }
56 
57     SignatureInfo() = delete;
58     ~SignatureInfo() = default;
59     NO_COPY_SEMANTIC(SignatureInfo);
60     NO_MOVE_SEMANTIC(SignatureInfo);
61 
62     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
63     ArenaVector<Type *> typeParams;
64     uint32_t minArgCount {};
65     varbinder::LocalVariable *restVar {};
66     ArenaVector<varbinder::LocalVariable *> params;
67     // NOLINTEND(misc-non-private-member-variables-in-classes)
68 };
69 
70 using ENUMBITOPS_OPERATORS;
71 
72 enum class SignatureFlags : uint32_t {
73     NO_OPTS = 0U,
74     ABSTRACT = 1U << 0U,
75     CALL = 1U << 1U,
76     CONSTRUCT = 1U << 2U,
77     PUBLIC = 1U << 3U,
78     PROTECTED = 1U << 4U,
79     PRIVATE = 1U << 5U,
80     STATIC = 1U << 6U,
81     FINAL = 1U << 7U,
82     CONSTRUCTOR = 1U << 8U,
83     PROXY = 1U << 9U,
84     INTERNAL = 1U << 10U,
85     NEED_RETURN_TYPE = 1U << 11U,
86     INFERRED_RETURN_TYPE = 1U << 12U,
87     THIS_RETURN_TYPE = 1U << 13U,
88     GETTER = 1U << 14U,
89     SETTER = 1U << 15U,
90     THROWS = 1U << 16U,
91     RETHROWS = 1U << 17U,
92     EXTENSION_FUNCTION = 1U << 18U,
93     DUPLICATE_ASM = 1U << 19U,
94 
95     INTERNAL_PROTECTED = INTERNAL | PROTECTED,
96     GETTER_OR_SETTER = GETTER | SETTER,
97     THROWING = THROWS | RETHROWS,
98 };
99 
100 }  // namespace ark::es2panda::checker
101 
102 template <>
103 struct enumbitops::IsAllowedType<ark::es2panda::checker::SignatureFlags> : std::true_type {
104 };
105 
106 namespace ark::es2panda::checker {
107 
108 class Signature final {
109 public:
110     Signature(SignatureInfo *signatureInfo, Type *returnType, ir::ScriptFunction *func = nullptr)
111         : signatureInfo_(signatureInfo), returnType_(returnType), func_(func)
112     {
113     }
114 
115     Signature() = delete;
116     ~Signature() = default;
117     NO_COPY_SEMANTIC(Signature);
118     NO_MOVE_SEMANTIC(Signature);
119 
120     [[nodiscard]] const SignatureInfo *GetSignatureInfo() const noexcept
121     {
122         return signatureInfo_;
123     }
124 
125     [[nodiscard]] SignatureInfo *GetSignatureInfo() noexcept
126     {
127         return signatureInfo_;
128     }
129 
130     [[nodiscard]] const ArenaVector<Type *> &TypeParams() const noexcept
131     {
132         return signatureInfo_->typeParams;
133     }
134 
135     [[nodiscard]] ArenaVector<Type *> &TypeParams() noexcept
136     {
137         return signatureInfo_->typeParams;
138     }
139 
140     [[nodiscard]] const ArenaVector<varbinder::LocalVariable *> &Params() const noexcept
141     {
142         return signatureInfo_->params;
143     }
144 
145     [[nodiscard]] ArenaVector<varbinder::LocalVariable *> &Params() noexcept
146     {
147         return signatureInfo_->params;
148     }
149 
150     [[nodiscard]] const Type *ReturnType() const noexcept
151     {
152         return returnType_;
153     }
154 
155     [[nodiscard]] Type *ReturnType() noexcept
156     {
157         return returnType_;
158     }
159 
160     [[nodiscard]] uint32_t MinArgCount() const noexcept
161     {
162         return signatureInfo_->minArgCount;
163     }
164 
165     [[nodiscard]] size_t ArgCount() const
166     {
167         return signatureInfo_->params.size();
168     }
169 
170     void SetReturnType(Type *type) noexcept
171     {
172         returnType_ = type;
173     }
174 
175     void SetOwner(ETSObjectType *owner) noexcept
176     {
177         ownerObj_ = owner;
178     }
179 
180     void SetOwnerVar(varbinder::Variable *owner) noexcept
181     {
182         ownerVar_ = owner;
183     }
184 
185     [[nodiscard]] bool HasFunction() const noexcept
186     {
187         return func_ != nullptr;
188     }
189 
190     [[nodiscard]] ir::ScriptFunction *Function() noexcept
191     {
192         ES2PANDA_ASSERT(HasFunction());
193         return func_;
194     }
195 
196     [[nodiscard]] const ir::ScriptFunction *Function() const noexcept
197     {
198         ES2PANDA_ASSERT(HasFunction());
199         return func_;
200     }
201 
202     void SetInternalName(util::StringView internalName)
203     {
204         internalName_ = internalName;
205     }
206 
207     [[nodiscard]] ETSObjectType *Owner()
208     {
209         return ownerObj_;
210     }
211 
212     [[nodiscard]] const ETSObjectType *Owner() const noexcept
213     {
214         return ownerObj_;
215     }
216 
217     [[nodiscard]] varbinder::Variable *OwnerVar() noexcept
218     {
219         return ownerVar_;
220     }
221 
222     [[nodiscard]] const varbinder::LocalVariable *RestVar() const noexcept
223     {
224         return signatureInfo_->restVar;
225     }
226 
227     [[nodiscard]] uint8_t ProtectionFlag() const noexcept
228     {
229         if ((flags_ & SignatureFlags::PRIVATE) != 0) {
230             return 2U;
231         }
232 
233         if ((flags_ & SignatureFlags::PROTECTED) != 0) {
234             return 1U;
235         }
236 
237         return 0;
238     }
239 
240     [[nodiscard]] bool HasProtectionFlagInternal() const noexcept
241     {
242         return (flags_ & SignatureFlags::INTERNAL) != 0;
243     }
244 
245     [[nodiscard]] SignatureFlags Flags() const noexcept
246     {
247         return flags_;
248     }
249 
250     void AddSignatureFlag(SignatureFlags const flag) noexcept
251     {
252         flags_ |= flag;
253     }
254 
255     void RemoveSignatureFlag(SignatureFlags const flag) noexcept
256     {
257         flags_ &= ~flag;
258     }
259 
260     [[nodiscard]] bool HasSignatureFlag(SignatureFlags const flag) const noexcept
261     {
262         return (flags_ & flag) != 0U;
263     }
264 
265     [[nodiscard]] bool HasRestParameter() const noexcept
266     {
267         return signatureInfo_->restVar != nullptr;
268     }
269 
270     [[nodiscard]] bool IsFinal() const noexcept
271     {
272         return HasSignatureFlag(SignatureFlags::FINAL);
273     }
274 
275     [[nodiscard]] bool IsExtensionFunction() const noexcept
276     {
277         return HasSignatureFlag(SignatureFlags::EXTENSION_FUNCTION);
278     }
279 
280     [[nodiscard]] bool IsExtensionAccessor() const noexcept
281     {
282         return IsExtensionFunction() && HasSignatureFlag(SignatureFlags::GETTER_OR_SETTER);
283     }
284 
285     void ToAssemblerType(std::stringstream &ss) const;
286 
287     [[nodiscard]] util::StringView InternalName() const;
288 
289     [[nodiscard]] Signature *Copy(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes);
290     [[nodiscard]] Signature *Substitute(TypeRelation *relation, const Substitution *substitution);
291 
292     void ToString(std::stringstream &ss, const varbinder::Variable *variable, bool printAsMethod = false,
293                   bool precise = false) const;
294     std::string ToString() const;
295     void IsSubtypeOf(TypeRelation *relation, Signature *super);
296     void AssignmentTarget(TypeRelation *relation, Signature *source);
297     [[nodiscard]] Signature *ToArrowSignature(ETSChecker *checker);
298     friend class ETSExtensionFuncHelperType;
299 
300 private:
301     Signature *CreateSignatureForSubstitute(ArenaAllocator *allocator, SignatureInfo *sigInfo, Type *returnType);
302 
303     checker::SignatureInfo *const signatureInfo_;
304     Type *returnType_;
305     ir::ScriptFunction *const func_ {};
306     SignatureFlags flags_ {SignatureFlags::NO_OPTS};
307     util::StringView internalName_ {};
308     ETSObjectType *ownerObj_ {};
309     varbinder::Variable *ownerVar_ {};
310 };
311 }  // namespace ark::es2panda::checker
312 
313 #endif /* TYPESCRIPT_TYPES_SIGNATURE_H */
314