• 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_ETS_FUNCTION_HELPERS_H
17 #define ES2PANDA_COMPILER_CHECKER_ETS_FUNCTION_HELPERS_H
18 
19 #include "checker/ETSchecker.h"
20 #include "checker/ets/typeRelationContext.h"
21 #include "checker/types/ets/etsObjectType.h"
22 #include "checker/types/type.h"
23 #include "ir/base/catchClause.h"
24 #include "ir/base/classDefinition.h"
25 #include "ir/base/classProperty.h"
26 #include "ir/base/methodDefinition.h"
27 #include "ir/base/scriptFunction.h"
28 #include "ir/base/spreadElement.h"
29 #include "ir/expressions/arrowFunctionExpression.h"
30 #include "ir/expressions/callExpression.h"
31 #include "ir/expressions/functionExpression.h"
32 #include "ir/expressions/memberExpression.h"
33 #include "ir/statements/blockStatement.h"
34 #include "ir/statements/doWhileStatement.h"
35 #include "ir/statements/expressionStatement.h"
36 #include "ir/statements/forInStatement.h"
37 #include "ir/statements/forOfStatement.h"
38 #include "ir/statements/forUpdateStatement.h"
39 #include "ir/statements/switchStatement.h"
40 #include "ir/statements/whileStatement.h"
41 #include "ir/ts/tsTypeParameterInstantiation.h"
42 #include "parser/program/program.h"
43 #include "utils/arena_containers.h"
44 #include "util/helpers.h"
45 #include "util/language.h"
46 #include "varbinder/declaration.h"
47 #include "varbinder/ETSBinder.h"
48 #include "varbinder/scope.h"
49 #include "varbinder/varbinder.h"
50 #include "varbinder/variable.h"
51 #include "varbinder/variableFlags.h"
52 
53 namespace ark::es2panda::checker {
54 
MaybeBoxedType(ETSChecker * checker,Type * type,ir::Expression * expr)55 static Type *MaybeBoxedType(ETSChecker *checker, Type *type, ir::Expression *expr)
56 {
57     ES2PANDA_ASSERT(type != nullptr);
58     if (!type->IsETSPrimitiveType()) {
59         return type;
60     }
61     auto *relation = checker->Relation();
62     auto *oldNode = relation->GetNode();
63     relation->SetNode(expr);
64     auto *res = checker->MaybeBoxInRelation(type);
65     relation->SetNode(oldNode);
66     return res;
67 }
68 
InferUntilFail(Signature const * const signature,const ArenaVector<ir::Expression * > & arguments,ETSChecker * checker,Substitution * substitution)69 static void InferUntilFail(Signature const *const signature, const ArenaVector<ir::Expression *> &arguments,
70                            ETSChecker *checker, Substitution *substitution)
71 {
72     auto *sigInfo = signature->GetSignatureInfo();
73     auto &sigParams = signature->GetSignatureInfo()->typeParams;
74     ArenaVector<bool> inferStatus(checker->Allocator()->Adapter());
75     inferStatus.assign(arguments.size(), false);
76     bool anyChange = true;
77     size_t lastSubsititutionSize = 0;
78 
79     checker->AddStatus(checker::CheckerStatus::IN_TYPE_INFER);
80     // some ets lib files require type infer from arg index 0,1,... , not fit to build graph
81     ES2PANDA_ASSERT(substitution != nullptr);
82     while (anyChange && substitution->size() < sigParams.size()) {
83         anyChange = false;
84         for (size_t ix = 0; ix < arguments.size(); ++ix) {
85             if (inferStatus[ix]) {
86                 continue;
87             }
88 
89             auto *arg = arguments[ix];
90             if (arg->IsObjectExpression()) {
91                 continue;
92             }
93 
94             auto *const argType = arg->IsSpreadElement()
95                                       ? MaybeBoxedType(checker, arg->AsSpreadElement()->Argument()->Check(checker),
96                                                        arg->AsSpreadElement()->Argument())
97                                       : MaybeBoxedType(checker, arg->Check(checker), arg);
98             auto *const paramType = (ix < signature->ArgCount())  ? sigInfo->params[ix]->TsType()
99                                     : sigInfo->restVar != nullptr ? sigInfo->restVar->TsType()
100                                                                   : nullptr;
101 
102             if (paramType == nullptr) {
103                 continue;
104             }
105             if (arg->IsArrowFunctionExpression()) {
106                 checker->Relation()->SetNode(arg);
107             }
108 
109             if (checker->EnhanceSubstitutionForType(sigInfo->typeParams, paramType, argType, substitution)) {
110                 inferStatus[ix] = true;
111             }
112             if (lastSubsititutionSize != substitution->size()) {
113                 lastSubsititutionSize = substitution->size();
114                 anyChange = true;
115             }
116         }
117     }
118     checker->RemoveStatus(checker::CheckerStatus::IN_TYPE_INFER);
119 }
120 
BuildImplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::Expression * > & arguments)121 static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
122                                                                  const ArenaVector<ir::Expression *> &arguments)
123 {
124     Substitution *substitution = checker->NewSubstitution();
125     auto *sigInfo = signature->GetSignatureInfo();
126     auto &sigParams = signature->GetSignatureInfo()->typeParams;
127 
128     InferUntilFail(signature, arguments, checker, substitution);
129 
130     if (substitution->size() != sigParams.size()) {
131         for (const auto typeParam : sigParams) {
132             auto newTypeParam = typeParam->AsETSTypeParameter();
133             if (auto it = substitution->find(newTypeParam); it != substitution->cend()) {
134                 continue;
135             }
136             if (newTypeParam->GetDefaultType() == nullptr) {
137                 checker->EmplaceSubstituted(substitution, newTypeParam, checker->GlobalETSNeverType());
138                 continue;
139             }
140             auto dflt = newTypeParam->GetDefaultType()->Substitute(checker->Relation(), substitution);
141             if (!checker->EnhanceSubstitutionForType(sigInfo->typeParams, newTypeParam, dflt, substitution)) {
142                 return nullptr;
143             }
144         }
145     }
146     if (substitution->size() != sigParams.size() &&
147         (signature->Function()->ReturnTypeAnnotation() == nullptr ||
148          !checker->EnhanceSubstitutionForType(sigInfo->typeParams,
149                                               signature->Function()->ReturnTypeAnnotation()->TsType(),
150                                               signature->ReturnType(), substitution))) {
151         return nullptr;
152     }
153 
154     return substitution;
155 }
156 
BuildExplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::TypeNode * > & params,const lexer::SourcePosition & pos,TypeRelationFlag flags)157 static const Substitution *BuildExplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
158                                                                  const ArenaVector<ir::TypeNode *> &params,
159                                                                  const lexer::SourcePosition &pos,
160                                                                  TypeRelationFlag flags)
161 {
162     auto &sigParams = signature->GetSignatureInfo()->typeParams;
163     auto *substitution = checker->NewSubstitution();
164     auto *constraintsSubstitution = checker->NewSubstitution();
165     ArenaVector<Type *> instArgs {checker->Allocator()->Adapter()};
166 
167     for (size_t ix = 0; ix < params.size(); ++ix) {
168         instArgs.push_back(MaybeBoxedType(checker, params[ix]->GetType(checker), params[ix]));
169         if (ix < sigParams.size()) {
170             checker->EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
171         }
172     }
173     for (size_t ix = instArgs.size(); ix < sigParams.size(); ++ix) {
174         auto *dflt = sigParams[ix]->AsETSTypeParameter()->GetDefaultType();
175         if (dflt == nullptr) {
176             break;
177         }
178 
179         dflt = dflt->Substitute(checker->Relation(), constraintsSubstitution);
180         instArgs.push_back(dflt);
181         checker->EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
182     }
183     if (sigParams.size() != instArgs.size()) {
184         if ((flags & TypeRelationFlag::NO_THROW) == static_cast<std::underlying_type_t<TypeRelationFlag>>(0U)) {
185             checker->LogError(diagnostic::RTYPE_PARAM_COUNT_MISMATCH, {sigParams.size(), instArgs.size()}, pos);
186         }
187         return nullptr;
188     }
189 
190     for (size_t ix = 0; ix < sigParams.size(); ix++) {
191         if (!checker->IsCompatibleTypeArgument(sigParams[ix]->AsETSTypeParameter(), instArgs[ix],
192                                                constraintsSubstitution)) {
193             return nullptr;
194         }
195         checker->EmplaceSubstituted(substitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
196     }
197     return substitution;
198 }
199 
MaybeSubstituteTypeParameters(ETSChecker * checker,Signature * signature,const ir::TSTypeParameterInstantiation * typeArguments,const ArenaVector<ir::Expression * > & arguments,const lexer::SourcePosition & pos,TypeRelationFlag flags)200 static Signature *MaybeSubstituteTypeParameters(ETSChecker *checker, Signature *signature,
201                                                 const ir::TSTypeParameterInstantiation *typeArguments,
202                                                 const ArenaVector<ir::Expression *> &arguments,
203                                                 const lexer::SourcePosition &pos, TypeRelationFlag flags)
204 {
205     if (typeArguments == nullptr && signature->GetSignatureInfo()->typeParams.empty()) {
206         return signature;
207     }
208 
209     const Substitution *substitution =
210         (typeArguments != nullptr)
211             ? BuildExplicitSubstitutionForArguments(checker, signature, typeArguments->Params(), pos, flags)
212             : BuildImplicitSubstitutionForArguments(checker, signature, arguments);
213 
214     return (substitution == nullptr) ? nullptr : signature->Substitute(checker->Relation(), substitution);
215 }
216 
CheckInterfaceOverride(ETSChecker * const checker,ETSObjectType * const interface,Signature * const signature)217 static bool CheckInterfaceOverride(ETSChecker *const checker, ETSObjectType *const interface,
218                                    Signature *const signature)
219 {
220     bool isOverriding = checker->CheckOverride(signature, interface);
221 
222     for (auto *const superInterface : interface->Interfaces()) {
223         isOverriding |= CheckInterfaceOverride(checker, superInterface, signature);
224     }
225 
226     return isOverriding;
227 }
228 
NodeScope(ir::AstNode * ast)229 static varbinder::Scope *NodeScope(ir::AstNode *ast)
230 {
231     if (ast->IsBlockStatement()) {
232         return ast->AsBlockStatement()->Scope();
233     }
234     if (ast->IsBlockExpression()) {
235         return ast->AsBlockExpression()->Scope();
236     }
237     if (ast->IsDoWhileStatement()) {
238         return ast->AsDoWhileStatement()->Scope();
239     }
240     if (ast->IsForInStatement()) {
241         return ast->AsForInStatement()->Scope();
242     }
243     if (ast->IsForOfStatement()) {
244         return ast->AsForOfStatement()->Scope();
245     }
246     if (ast->IsForUpdateStatement()) {
247         return ast->AsForUpdateStatement()->Scope();
248     }
249     if (ast->IsSwitchStatement()) {
250         return ast->AsSwitchStatement()->Scope();
251     }
252     if (ast->IsWhileStatement()) {
253         return ast->AsWhileStatement()->Scope();
254     }
255     if (ast->IsCatchClause()) {
256         return ast->AsCatchClause()->Scope();
257     }
258     if (ast->IsClassDefinition()) {
259         return ast->AsClassDefinition()->Scope();
260     }
261     if (ast->IsScriptFunction()) {
262         return ast->AsScriptFunction()->Scope()->ParamScope();
263     }
264     return nullptr;
265 }
266 
267 }  // namespace ark::es2panda::checker
268 
269 #endif
270