1 /*
2 * Copyright (c) 2021 - 2023 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 "varbinder/varbinder.h"
20 #include "varbinder/declaration.h"
21 #include "varbinder/ETSBinder.h"
22 #include "varbinder/scope.h"
23 #include "varbinder/variable.h"
24 #include "varbinder/variableFlags.h"
25 #include "checker/ETSchecker.h"
26 #include "checker/ets/typeRelationContext.h"
27 #include "checker/types/ets/etsAsyncFuncReturnType.h"
28 #include "checker/types/ets/etsObjectType.h"
29 #include "checker/types/type.h"
30 #include "checker/types/typeFlag.h"
31 #include "ir/astNode.h"
32 #include "ir/typeNode.h"
33 #include "ir/base/catchClause.h"
34 #include "ir/base/classDefinition.h"
35 #include "ir/base/classProperty.h"
36 #include "ir/base/methodDefinition.h"
37 #include "ir/base/scriptFunction.h"
38 #include "ir/base/spreadElement.h"
39 #include "ir/ets/etsFunctionType.h"
40 #include "ir/ets/etsParameterExpression.h"
41 #include "ir/ets/etsTypeReference.h"
42 #include "ir/ets/etsTypeReferencePart.h"
43 #include "ir/expressions/arrowFunctionExpression.h"
44 #include "ir/expressions/assignmentExpression.h"
45 #include "ir/expressions/callExpression.h"
46 #include "ir/expressions/functionExpression.h"
47 #include "ir/expressions/identifier.h"
48 #include "ir/expressions/literals/numberLiteral.h"
49 #include "ir/expressions/memberExpression.h"
50 #include "ir/expressions/objectExpression.h"
51 #include "ir/expressions/thisExpression.h"
52 #include "ir/statements/blockStatement.h"
53 #include "ir/statements/doWhileStatement.h"
54 #include "ir/statements/expressionStatement.h"
55 #include "ir/statements/forInStatement.h"
56 #include "ir/statements/forOfStatement.h"
57 #include "ir/statements/forUpdateStatement.h"
58 #include "ir/statements/returnStatement.h"
59 #include "ir/statements/switchStatement.h"
60 #include "ir/statements/whileStatement.h"
61 #include "ir/ts/tsArrayType.h"
62 #include "ir/ts/tsInterfaceBody.h"
63 #include "ir/ts/tsTypeAliasDeclaration.h"
64 #include "ir/ts/tsTypeParameter.h"
65 #include "ir/ts/tsTypeParameterInstantiation.h"
66 #include "parser/program/program.h"
67 #include "util/helpers.h"
68 #include "util/language.h"
69
70 namespace panda::es2panda::checker {
71
MaybeBoxedType(ETSChecker * checker,Type * type,ir::Expression * expr)72 static Type *MaybeBoxedType(ETSChecker *checker, Type *type, ir::Expression *expr)
73 {
74 if (!type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) {
75 return type;
76 }
77 auto *relation = checker->Relation();
78 auto *oldNode = relation->GetNode();
79 relation->SetNode(expr);
80 auto *res = checker->PrimitiveTypeAsETSBuiltinType(type);
81 relation->SetNode(oldNode);
82 return res;
83 }
84
BuildImplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::Expression * > & arguments)85 static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
86 const ArenaVector<ir::Expression *> &arguments)
87 {
88 Substitution *substitution = checker->NewSubstitution();
89 auto *instantiatedTypeParams = checker->NewInstantiatedTypeParamsSet();
90 auto *sigInfo = signature->GetSignatureInfo();
91 auto &typeParams = sigInfo->typeParams;
92 for (size_t ix = 0; ix < arguments.size(); ix++) {
93 auto *arg = arguments[ix];
94 if (arg->IsObjectExpression()) {
95 continue;
96 }
97 auto *argType = arg->Check(checker);
98 argType = MaybeBoxedType(checker, argType, arg);
99 auto *paramType = (ix < signature->MinArgCount()) ? sigInfo->params[ix]->TsType() : sigInfo->restVar->TsType();
100 if (paramType == nullptr) {
101 continue;
102 }
103 if (!checker->EnhanceSubstitutionForType(typeParams, paramType, argType, substitution,
104 instantiatedTypeParams)) {
105 return nullptr;
106 }
107 }
108 return substitution;
109 }
110
BuildExplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::TypeNode * > & params,const lexer::SourcePosition & pos,TypeRelationFlag flags)111 static const Substitution *BuildExplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
112 const ArenaVector<ir::TypeNode *> ¶ms,
113 const lexer::SourcePosition &pos,
114 TypeRelationFlag flags)
115 {
116 auto &sigParams = signature->GetSignatureInfo()->typeParams;
117 auto *substitution = checker->NewSubstitution();
118 ArenaVector<Type *> instArgs {checker->Allocator()->Adapter()};
119
120 for (auto *taExpr : params) {
121 instArgs.push_back(MaybeBoxedType(checker, taExpr->GetType(checker), taExpr));
122 }
123 for (size_t ix = instArgs.size(); ix < sigParams.size(); ++ix) {
124 auto *dflt = sigParams[ix]->AsETSTypeParameter()->GetDefaultType();
125 if (dflt == nullptr) {
126 break;
127 }
128 instArgs.push_back(dflt);
129 }
130 if (sigParams.size() != instArgs.size()) {
131 if ((flags & TypeRelationFlag::NO_THROW) != 0) {
132 return nullptr;
133 }
134 checker->ThrowTypeError({"Expected ", sigParams.size(), " type arguments, got ", instArgs.size(), " ."}, pos);
135 }
136
137 auto *constraintsSubstitution = checker->NewSubstitution();
138
139 for (size_t ix = 0; ix < sigParams.size(); ix++) {
140 ETSChecker::EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
141 }
142 for (size_t ix = 0; ix < sigParams.size(); ix++) {
143 if (!checker->IsCompatibleTypeArgument(sigParams[ix]->AsETSTypeParameter(), instArgs[ix],
144 constraintsSubstitution)) {
145 return nullptr;
146 }
147 ETSChecker::EmplaceSubstituted(substitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
148 }
149 return substitution;
150 }
151
MaybeSubstituteTypeParameters(ETSChecker * checker,Signature * signature,const ir::TSTypeParameterInstantiation * typeArguments,const ArenaVector<ir::Expression * > & arguments,const lexer::SourcePosition & pos,TypeRelationFlag flags)152 static Signature *MaybeSubstituteTypeParameters(ETSChecker *checker, Signature *signature,
153 const ir::TSTypeParameterInstantiation *typeArguments,
154 const ArenaVector<ir::Expression *> &arguments,
155 const lexer::SourcePosition &pos, TypeRelationFlag flags)
156 {
157 if (typeArguments == nullptr && signature->GetSignatureInfo()->typeParams.empty()) {
158 return signature;
159 }
160
161 const Substitution *substitution =
162 (typeArguments != nullptr)
163 ? BuildExplicitSubstitutionForArguments(checker, signature, typeArguments->Params(), pos, flags)
164 : BuildImplicitSubstitutionForArguments(checker, signature, arguments);
165 return (substitution == nullptr) ? nullptr : signature->Substitute(checker->Relation(), substitution);
166 }
167
CmpAssemblerTypesWithRank(Signature * sig1,Signature * sig2)168 static bool CmpAssemblerTypesWithRank(Signature *sig1, Signature *sig2)
169 {
170 for (size_t ix = 0; ix < sig1->MinArgCount(); ix++) {
171 std::stringstream s1;
172 std::stringstream s2;
173 sig1->Params()[ix]->TsType()->ToAssemblerTypeWithRank(s1);
174 sig2->Params()[ix]->TsType()->ToAssemblerTypeWithRank(s2);
175 if (s1.str() != s2.str()) {
176 return false;
177 break;
178 }
179 }
180 return true;
181 }
182
HasSameAssemblySignature(ETSFunctionType * func1,ETSFunctionType * func2)183 static bool HasSameAssemblySignature(ETSFunctionType *func1, ETSFunctionType *func2)
184 {
185 for (auto *sig1 : func1->CallSignatures()) {
186 for (auto *sig2 : func2->CallSignatures()) {
187 if (sig1->MinArgCount() != sig2->MinArgCount()) {
188 continue;
189 }
190 bool allSame = CmpAssemblerTypesWithRank(sig1, sig2);
191 if (!allSame) {
192 continue;
193 }
194 auto *rv1 = sig1->RestVar();
195 auto *rv2 = sig2->RestVar();
196 if (rv1 == nullptr && rv2 == nullptr) {
197 return true;
198 }
199 if (rv1 == nullptr || rv2 == nullptr) { // exactly one of them is null
200 return false;
201 }
202 std::stringstream s1;
203 std::stringstream s2;
204 rv1->TsType()->ToAssemblerTypeWithRank(s1);
205 rv2->TsType()->ToAssemblerTypeWithRank(s2);
206 if (s1.str() == s2.str()) {
207 return true;
208 }
209 }
210 }
211 return false;
212 }
213
CheckInterfaceOverride(ETSChecker * const checker,ETSObjectType * const interface,Signature * const signature)214 static bool CheckInterfaceOverride(ETSChecker *const checker, ETSObjectType *const interface,
215 Signature *const signature)
216 {
217 bool isOverriding = checker->CheckOverride(signature, interface);
218
219 for (auto *const superInterface : interface->Interfaces()) {
220 isOverriding |= CheckInterfaceOverride(checker, superInterface, signature);
221 }
222
223 return isOverriding;
224 }
225
NodeScope(ir::AstNode * ast)226 static varbinder::Scope *NodeScope(ir::AstNode *ast)
227 {
228 if (ast->IsBlockStatement()) {
229 return ast->AsBlockStatement()->Scope();
230 }
231 if (ast->IsDoWhileStatement()) {
232 return ast->AsDoWhileStatement()->Scope();
233 }
234 if (ast->IsForInStatement()) {
235 return ast->AsForInStatement()->Scope();
236 }
237 if (ast->IsForOfStatement()) {
238 return ast->AsForOfStatement()->Scope();
239 }
240 if (ast->IsForUpdateStatement()) {
241 return ast->AsForUpdateStatement()->Scope();
242 }
243 if (ast->IsSwitchStatement()) {
244 return ast->AsSwitchStatement()->Scope();
245 }
246 if (ast->IsWhileStatement()) {
247 return ast->AsWhileStatement()->Scope();
248 }
249 if (ast->IsCatchClause()) {
250 return ast->AsCatchClause()->Scope();
251 }
252 if (ast->IsClassDefinition()) {
253 return ast->AsClassDefinition()->Scope();
254 }
255 if (ast->IsScriptFunction()) {
256 return ast->AsScriptFunction()->Scope()->ParamScope();
257 }
258 return nullptr;
259 }
260
261 } // namespace panda::es2panda::checker
262
263 #endif