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