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 #include "ETSchecker.h"
17
18 #include "es2panda.h"
19 #include "ir/base/classDefinition.h"
20 #include "ir/expression.h"
21 #include "ir/expressions/callExpression.h"
22 #include "ir/ts/tsInterfaceDeclaration.h"
23 #include "ir/statements/blockStatement.h"
24 #include "varbinder/ETSBinder.h"
25 #include "parser/program/program.h"
26 #include "checker/ets/aliveAnalyzer.h"
27 #include "checker/ets/assignAnalyzer.h"
28 #include "checker/ets/etsWarningAnalyzer.h"
29 #include "checker/types/globalTypesHolder.h"
30 #include "ir/base/scriptFunction.h"
31 #include "util/helpers.h"
32 #include "evaluate/scopedDebugInfoPlugin.h"
33
34 namespace ark::es2panda::checker {
35
ETSChecker(util::DiagnosticEngine & diagnosticEngine)36 ETSChecker::ETSChecker(util::DiagnosticEngine &diagnosticEngine)
37 // NOLINTNEXTLINE(readability-redundant-member-init)
38 : Checker(diagnosticEngine),
39 arrayTypes_(Allocator()->Adapter()),
40 pendingConstraintCheckRecords_(Allocator()->Adapter()),
41 globalArraySignatures_(Allocator()->Adapter()),
42 dynamicIntrinsics_ {DynamicCallIntrinsicsMap {Allocator()->Adapter()},
43 DynamicCallIntrinsicsMap {Allocator()->Adapter()}},
44 dynamicClasses_ {DynamicClassIntrinsicsMap(Allocator()->Adapter()),
45 DynamicClassIntrinsicsMap(Allocator()->Adapter())},
46 dynamicLambdaSignatureCache_(Allocator()->Adapter()),
47 functionalInterfaceCache_(Allocator()->Adapter()),
48 apparentTypes_(Allocator()->Adapter()),
49 dynamicCallNames_ {{DynamicCallNamesMap(Allocator()->Adapter()), DynamicCallNamesMap(Allocator()->Adapter())}},
50 overloadSigContainer_(Allocator()->Adapter())
51 {
52 }
53
ETSChecker(util::DiagnosticEngine & diagnosticEngine,ArenaAllocator * programAllocator)54 ETSChecker::ETSChecker(util::DiagnosticEngine &diagnosticEngine, ArenaAllocator *programAllocator)
55 // NOLINTNEXTLINE(readability-redundant-member-init)
56 : Checker(diagnosticEngine, programAllocator),
57 arrayTypes_(Allocator()->Adapter()),
58 pendingConstraintCheckRecords_(Allocator()->Adapter()),
59 globalArraySignatures_(Allocator()->Adapter()),
60 dynamicIntrinsics_ {DynamicCallIntrinsicsMap {Allocator()->Adapter()},
61 DynamicCallIntrinsicsMap {Allocator()->Adapter()}},
62 dynamicClasses_ {DynamicClassIntrinsicsMap(Allocator()->Adapter()),
63 DynamicClassIntrinsicsMap(Allocator()->Adapter())},
64 dynamicLambdaSignatureCache_(Allocator()->Adapter()),
65 functionalInterfaceCache_(Allocator()->Adapter()),
66 apparentTypes_(Allocator()->Adapter()),
67 dynamicCallNames_ {{DynamicCallNamesMap(Allocator()->Adapter()), DynamicCallNamesMap(Allocator()->Adapter())}},
68 overloadSigContainer_(Allocator()->Adapter())
69 {
70 }
71
InitBuiltin(ETSChecker * checker,std::string_view signature)72 static util::StringView InitBuiltin(ETSChecker *checker, std::string_view signature)
73 {
74 const auto varMap = checker->VarBinder()->TopScope()->Bindings();
75 const auto iterator = varMap.find(signature);
76 ES2PANDA_ASSERT(iterator != varMap.end());
77 auto *var = iterator->second;
78 Type *type {nullptr};
79 if (var->HasFlag(varbinder::VariableFlags::BUILTIN_TYPE)) {
80 if (var->Declaration()->Node()->IsClassDefinition()) {
81 type = checker->BuildBasicClassProperties(var->Declaration()->Node()->AsClassDefinition());
82 } else {
83 ES2PANDA_ASSERT(var->Declaration()->Node()->IsTSInterfaceDeclaration());
84 type = checker->BuildBasicInterfaceProperties(var->Declaration()->Node()->AsTSInterfaceDeclaration());
85 }
86 checker->GetGlobalTypesHolder()->InitializeBuiltin(iterator->first, type);
87 }
88 return iterator->first;
89 }
90
CheckObjectLiteralKeys(const ArenaVector<ir::Expression * > & properties)91 void ETSChecker::CheckObjectLiteralKeys(const ArenaVector<ir::Expression *> &properties)
92 {
93 static std::set<util::StringView> names;
94 names.clear();
95
96 for (auto property : properties) {
97 if (!property->IsProperty()) {
98 continue;
99 }
100 auto propertyDecl = property->AsProperty();
101 auto propKey = propertyDecl->Key();
102 if (!propKey->IsIdentifier() && !propKey->IsStringLiteral()) {
103 continue;
104 }
105
106 // number kind only used here
107 auto propName = propKey->IsIdentifier() ? propKey->AsIdentifier()->Name() : propKey->AsStringLiteral()->Str();
108 if (names.find(propName) != names.end()) {
109 LogError(diagnostic::OBJ_LIT_PROPERTY_REDECLARATION, {}, property->Start());
110 }
111 names.insert(propName);
112 }
113 }
114
SetupBuiltinMember(varbinder::Variable * var)115 static void SetupBuiltinMember(varbinder::Variable *var)
116 {
117 auto *type = var->TsType();
118 if (type == nullptr || !type->IsETSObjectType()) {
119 return;
120 }
121 }
122
123 // clang-format off
124 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
125 static constexpr std::string_view BUILTINS_TO_INIT[] = {
126 compiler::Signatures::BUILTIN_OBJECT_CLASS,
127 compiler::Signatures::BUILTIN_STRING_CLASS,
128 compiler::Signatures::BUILTIN_BIGINT_CLASS,
129 compiler::Signatures::BUILTIN_EXCEPTION_CLASS,
130 compiler::Signatures::BUILTIN_ERROR_CLASS,
131 compiler::Signatures::BUILTIN_TYPE_CLASS,
132 compiler::Signatures::BUILTIN_PROMISE_CLASS,
133 compiler::Signatures::BUILTIN_BOOLEAN_CLASS,
134 compiler::Signatures::BUILTIN_BYTE_CLASS,
135 compiler::Signatures::BUILTIN_CHAR_CLASS,
136 compiler::Signatures::BUILTIN_SHORT_CLASS,
137 compiler::Signatures::BUILTIN_INT_CLASS,
138 compiler::Signatures::BUILTIN_LONG_CLASS,
139 compiler::Signatures::BUILTIN_FLOAT_CLASS,
140 compiler::Signatures::BUILTIN_DOUBLE_CLASS,
141 compiler::Signatures::BUILTIN_FUNCTION0_CLASS,
142 compiler::Signatures::BUILTIN_FUNCTION1_CLASS,
143 compiler::Signatures::BUILTIN_FUNCTION2_CLASS,
144 compiler::Signatures::BUILTIN_FUNCTION3_CLASS,
145 compiler::Signatures::BUILTIN_FUNCTION4_CLASS,
146 compiler::Signatures::BUILTIN_FUNCTION5_CLASS,
147 compiler::Signatures::BUILTIN_FUNCTION6_CLASS,
148 compiler::Signatures::BUILTIN_FUNCTION7_CLASS,
149 compiler::Signatures::BUILTIN_FUNCTION8_CLASS,
150 compiler::Signatures::BUILTIN_FUNCTION9_CLASS,
151 compiler::Signatures::BUILTIN_FUNCTION10_CLASS,
152 compiler::Signatures::BUILTIN_FUNCTION11_CLASS,
153 compiler::Signatures::BUILTIN_FUNCTION12_CLASS,
154 compiler::Signatures::BUILTIN_FUNCTION13_CLASS,
155 compiler::Signatures::BUILTIN_FUNCTION14_CLASS,
156 compiler::Signatures::BUILTIN_FUNCTION15_CLASS,
157 compiler::Signatures::BUILTIN_FUNCTION16_CLASS,
158 compiler::Signatures::BUILTIN_LAMBDA0_CLASS,
159 compiler::Signatures::BUILTIN_LAMBDA1_CLASS,
160 compiler::Signatures::BUILTIN_LAMBDA2_CLASS,
161 compiler::Signatures::BUILTIN_LAMBDA3_CLASS,
162 compiler::Signatures::BUILTIN_LAMBDA4_CLASS,
163 compiler::Signatures::BUILTIN_LAMBDA5_CLASS,
164 compiler::Signatures::BUILTIN_LAMBDA6_CLASS,
165 compiler::Signatures::BUILTIN_LAMBDA7_CLASS,
166 compiler::Signatures::BUILTIN_LAMBDA8_CLASS,
167 compiler::Signatures::BUILTIN_LAMBDA9_CLASS,
168 compiler::Signatures::BUILTIN_LAMBDA10_CLASS,
169 compiler::Signatures::BUILTIN_LAMBDA11_CLASS,
170 compiler::Signatures::BUILTIN_LAMBDA12_CLASS,
171 compiler::Signatures::BUILTIN_LAMBDA13_CLASS,
172 compiler::Signatures::BUILTIN_LAMBDA14_CLASS,
173 compiler::Signatures::BUILTIN_LAMBDA15_CLASS,
174 compiler::Signatures::BUILTIN_LAMBDA16_CLASS,
175 compiler::Signatures::BUILTIN_FUNCTIONR0_CLASS,
176 compiler::Signatures::BUILTIN_FUNCTIONR1_CLASS,
177 compiler::Signatures::BUILTIN_FUNCTIONR2_CLASS,
178 compiler::Signatures::BUILTIN_FUNCTIONR3_CLASS,
179 compiler::Signatures::BUILTIN_FUNCTIONR4_CLASS,
180 compiler::Signatures::BUILTIN_FUNCTIONR5_CLASS,
181 compiler::Signatures::BUILTIN_FUNCTIONR6_CLASS,
182 compiler::Signatures::BUILTIN_FUNCTIONR7_CLASS,
183 compiler::Signatures::BUILTIN_FUNCTIONR8_CLASS,
184 compiler::Signatures::BUILTIN_FUNCTIONR9_CLASS,
185 compiler::Signatures::BUILTIN_FUNCTIONR10_CLASS,
186 compiler::Signatures::BUILTIN_FUNCTIONR11_CLASS,
187 compiler::Signatures::BUILTIN_FUNCTIONR12_CLASS,
188 compiler::Signatures::BUILTIN_FUNCTIONR13_CLASS,
189 compiler::Signatures::BUILTIN_FUNCTIONR14_CLASS,
190 compiler::Signatures::BUILTIN_FUNCTIONR15_CLASS,
191 compiler::Signatures::BUILTIN_FUNCTIONR16_CLASS,
192 compiler::Signatures::BUILTIN_FUNCTIONN_CLASS,
193 compiler::Signatures::BUILTIN_LAMBDAR0_CLASS,
194 compiler::Signatures::BUILTIN_LAMBDAR1_CLASS,
195 compiler::Signatures::BUILTIN_LAMBDAR2_CLASS,
196 compiler::Signatures::BUILTIN_LAMBDAR3_CLASS,
197 compiler::Signatures::BUILTIN_LAMBDAR4_CLASS,
198 compiler::Signatures::BUILTIN_LAMBDAR5_CLASS,
199 compiler::Signatures::BUILTIN_LAMBDAR6_CLASS,
200 compiler::Signatures::BUILTIN_LAMBDAR7_CLASS,
201 compiler::Signatures::BUILTIN_LAMBDAR8_CLASS,
202 compiler::Signatures::BUILTIN_LAMBDAR9_CLASS,
203 compiler::Signatures::BUILTIN_LAMBDAR10_CLASS,
204 compiler::Signatures::BUILTIN_LAMBDAR11_CLASS,
205 compiler::Signatures::BUILTIN_LAMBDAR12_CLASS,
206 compiler::Signatures::BUILTIN_LAMBDAR13_CLASS,
207 compiler::Signatures::BUILTIN_LAMBDAR14_CLASS,
208 compiler::Signatures::BUILTIN_LAMBDAR15_CLASS,
209 compiler::Signatures::BUILTIN_LAMBDAR16_CLASS,
210 compiler::Signatures::BUILTIN_LAMBDAN_CLASS,
211 compiler::Signatures::BUILTIN_TUPLE0_CLASS,
212 compiler::Signatures::BUILTIN_TUPLE1_CLASS,
213 compiler::Signatures::BUILTIN_TUPLE2_CLASS,
214 compiler::Signatures::BUILTIN_TUPLE3_CLASS,
215 compiler::Signatures::BUILTIN_TUPLE4_CLASS,
216 compiler::Signatures::BUILTIN_TUPLE5_CLASS,
217 compiler::Signatures::BUILTIN_TUPLE6_CLASS,
218 compiler::Signatures::BUILTIN_TUPLE7_CLASS,
219 compiler::Signatures::BUILTIN_TUPLE8_CLASS,
220 compiler::Signatures::BUILTIN_TUPLE9_CLASS,
221 compiler::Signatures::BUILTIN_TUPLE10_CLASS,
222 compiler::Signatures::BUILTIN_TUPLE11_CLASS,
223 compiler::Signatures::BUILTIN_TUPLE12_CLASS,
224 compiler::Signatures::BUILTIN_TUPLE13_CLASS,
225 compiler::Signatures::BUILTIN_TUPLE14_CLASS,
226 compiler::Signatures::BUILTIN_TUPLE15_CLASS,
227 compiler::Signatures::BUILTIN_TUPLE16_CLASS,
228 compiler::Signatures::BUILTIN_TUPLEN_CLASS,
229 };
230 // clang-format on
231
IntializeFunctionInterfaces(GlobalTypesHolder * typeHolder)232 static void IntializeFunctionInterfaces(GlobalTypesHolder *typeHolder)
233 {
234 auto const getItf = [typeHolder](size_t arity, bool hasRest) {
235 return typeHolder->GlobalFunctionBuiltinType(arity, hasRest)->AsETSObjectType();
236 };
237
238 for (size_t arity = 0; arity < typeHolder->VariadicFunctionTypeThreshold(); arity++) {
239 getItf(arity, false)->AddObjectFlag(ETSObjectFlags::FUNCTIONAL);
240 getItf(arity, true)->AddObjectFlag(ETSObjectFlags::FUNCTIONAL);
241 }
242 }
243
InitializeBuiltins(varbinder::ETSBinder * varbinder)244 void ETSChecker::InitializeBuiltins(varbinder::ETSBinder *varbinder)
245 {
246 if (HasStatus(CheckerStatus::BUILTINS_INITIALIZED)) {
247 return;
248 }
249
250 const auto varMap = varbinder->TopScope()->Bindings();
251
252 auto const objectName = InitBuiltin(this, compiler::Signatures::BUILTIN_OBJECT_CLASS);
253
254 for (auto sig : BUILTINS_TO_INIT) {
255 InitBuiltin(this, sig);
256 }
257
258 IntializeFunctionInterfaces(GetGlobalTypesHolder());
259
260 for (const auto &[name, var] : varMap) {
261 (void)name;
262 SetupBuiltinMember(var);
263 }
264
265 for (const auto &[name, var] : varMap) {
266 if (name == objectName) {
267 continue;
268 }
269
270 if (var->HasFlag(varbinder::VariableFlags::BUILTIN_TYPE)) {
271 if (var->TsType() == nullptr) {
272 InitializeBuiltin(var, name);
273 } else {
274 GetGlobalTypesHolder()->InitializeBuiltin(name, var->TsType());
275 }
276 }
277 }
278
279 AddStatus(CheckerStatus::BUILTINS_INITIALIZED);
280 }
281
InitializeBuiltin(varbinder::Variable * var,const util::StringView & name)282 void ETSChecker::InitializeBuiltin(varbinder::Variable *var, const util::StringView &name)
283 {
284 Type *type {nullptr};
285 if (var->Declaration()->Node()->IsClassDefinition()) {
286 type = BuildBasicClassProperties(var->Declaration()->Node()->AsClassDefinition());
287 } else {
288 ES2PANDA_ASSERT(var->Declaration()->Node()->IsTSInterfaceDeclaration());
289 type = BuildBasicInterfaceProperties(var->Declaration()->Node()->AsTSInterfaceDeclaration());
290 }
291 GetGlobalTypesHolder()->InitializeBuiltin(name, type);
292 }
293
StartChecker(varbinder::VarBinder * varbinder,const util::Options & options)294 bool ETSChecker::StartChecker(varbinder::VarBinder *varbinder, const util::Options &options)
295 {
296 Initialize(varbinder);
297
298 if (options.IsParseOnly()) {
299 return false;
300 }
301
302 auto *etsBinder = varbinder->AsETSBinder();
303 InitializeBuiltins(etsBinder);
304
305 for (auto &entry : etsBinder->DynamicImportVars()) {
306 auto &data = entry.second;
307 if (data.import->IsPureDynamic()) {
308 data.variable->SetTsType(GlobalBuiltinDynamicType(data.import->Language()));
309 }
310 }
311
312 bool isEvalMode = (debugInfoPlugin_ != nullptr);
313 if (UNLIKELY(isEvalMode)) {
314 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
315 debugInfoPlugin_->PreCheck();
316 }
317
318 CheckProgram(Program(), true);
319
320 if (UNLIKELY(isEvalMode)) {
321 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
322 debugInfoPlugin_->PostCheck();
323 }
324
325 #ifndef NDEBUG
326 for (auto *func : varbinder->Functions()) {
327 ES2PANDA_ASSERT(!func->Node()->AsScriptFunction()->Scope()->Name().Empty());
328 }
329 #endif
330
331 if (options.IsDumpDynamicAst()) {
332 std::cout << Program()->Dump() << std::endl;
333 }
334
335 CheckWarnings(Program(), options);
336
337 return !IsAnyError();
338 }
339
GetDebugInfoPlugin()340 evaluate::ScopedDebugInfoPlugin *ETSChecker::GetDebugInfoPlugin()
341 {
342 return debugInfoPlugin_;
343 }
344
GetDebugInfoPlugin() const345 const evaluate::ScopedDebugInfoPlugin *ETSChecker::GetDebugInfoPlugin() const
346 {
347 return debugInfoPlugin_;
348 }
349
SetDebugInfoPlugin(evaluate::ScopedDebugInfoPlugin * debugInfo)350 void ETSChecker::SetDebugInfoPlugin(evaluate::ScopedDebugInfoPlugin *debugInfo)
351 {
352 debugInfoPlugin_ = debugInfo;
353 }
354
CheckProgram(parser::Program * program,bool runAnalysis)355 void ETSChecker::CheckProgram(parser::Program *program, bool runAnalysis)
356 {
357 if (program->IsASTChecked()) {
358 return;
359 }
360
361 auto *savedProgram = Program();
362 SetProgram(program);
363
364 for (auto &[_, extPrograms] : program->ExternalSources()) {
365 (void)_;
366 for (auto *extProg : extPrograms) {
367 if (extProg->IsASTChecked()) {
368 continue;
369 }
370
371 auto *savedProgram2 = VarBinder()->AsETSBinder()->Program();
372 VarBinder()->AsETSBinder()->SetProgram(extProg);
373 VarBinder()->AsETSBinder()->ResetTopScope(extProg->GlobalScope());
374 checker::SavedCheckerContext savedContext(this, Context().Status(), Context().ContainingClass());
375 AddStatus(checker::CheckerStatus::IN_EXTERNAL);
376 CheckProgram(extProg, VarBinder()->IsGenStdLib());
377 extProg->SetFlag(parser::ProgramFlags::AST_CHECK_PROCESSED);
378 VarBinder()->AsETSBinder()->SetProgram(savedProgram2);
379 VarBinder()->AsETSBinder()->ResetTopScope(savedProgram2->GlobalScope());
380 }
381 }
382
383 ES2PANDA_ASSERT(Program()->Ast()->IsProgram());
384
385 Program()->Ast()->Check(this);
386
387 if (runAnalysis && !IsAnyError()) {
388 AliveAnalyzer aliveAnalyzer(Program()->Ast(), this);
389 AssignAnalyzer(this).Analyze(Program()->Ast());
390 }
391
392 ES2PANDA_ASSERT(VarBinder()->AsETSBinder()->GetExternalRecordTable().find(program)->second);
393
394 SetProgram(savedProgram);
395 }
396
CheckWarnings(parser::Program * program,const util::Options & options)397 void ETSChecker::CheckWarnings(parser::Program *program, const util::Options &options)
398 {
399 const auto &etsWarningCollection = options.GetEtsWarningCollection();
400 for (const auto warning : etsWarningCollection) {
401 ETSWarningAnalyzer(Program()->Ast(), program, warning, DiagnosticEngine());
402 }
403 }
404
CheckTypeCached(ir::Expression * expr)405 Type *ETSChecker::CheckTypeCached(ir::Expression *expr)
406 {
407 if (expr->TsType() == nullptr) {
408 expr->SetTsType(expr->Check(this));
409 }
410
411 return expr->TsType();
412 }
413
IsClassStaticMethod(checker::ETSObjectType * objType,checker::Signature * signature)414 bool ETSChecker::IsClassStaticMethod(checker::ETSObjectType *objType, checker::Signature *signature)
415 {
416 return objType->HasObjectFlag(checker::ETSObjectFlags::CLASS) &&
417 signature->HasSignatureFlag(checker::SignatureFlags::STATIC);
418 }
419
TypeKind(const Type * const type)420 [[nodiscard]] TypeFlag ETSChecker::TypeKind(const Type *const type) noexcept
421 {
422 // These types were not present in the ETS_TYPE list. Some of them are omited intentionally, other are just bugs
423 static constexpr auto TO_CLEAR = TypeFlag::CONSTANT | TypeFlag::GENERIC | TypeFlag::ETS_INT_ENUM |
424 TypeFlag::ETS_STRING_ENUM | TypeFlag::READONLY | TypeFlag::BIGINT_LITERAL |
425 TypeFlag::ETS_TYPE_ALIAS | TypeFlag::TYPE_ERROR;
426
427 // Bugs: these types do not appear as a valid TypeKind, as the TypeKind has more then one bit set
428 [[maybe_unused]] static constexpr auto NOT_A_TYPE_KIND = TypeFlag::ETS_DYNAMIC_FLAG;
429 CHECK_NOT_NULL(type);
430 auto res = static_cast<checker::TypeFlag>(type->TypeFlags() & ~(TO_CLEAR));
431 ES2PANDA_ASSERT_POS(res == TypeFlag::NONE || helpers::math::IsPowerOfTwo(res & ~(NOT_A_TYPE_KIND)),
432 ark::es2panda::GetPositionForDiagnostic());
433 return res;
434 }
435
436 template <typename... Args>
AsETSObjectType(Type * (GlobalTypesHolder::* typeFunctor)(Args...),Args...args) const437 ETSObjectType *ETSChecker::AsETSObjectType(Type *(GlobalTypesHolder::*typeFunctor)(Args...), Args... args) const
438 {
439 auto *ret = (GetGlobalTypesHolder()->*typeFunctor)(args...);
440 return ret != nullptr ? ret->AsETSObjectType() : nullptr;
441 }
442
GlobalByteType() const443 Type *ETSChecker::GlobalByteType() const
444 {
445 return GetGlobalTypesHolder()->GlobalByteType();
446 }
447
GlobalByteBuiltinType() const448 Type *ETSChecker::GlobalByteBuiltinType() const
449 {
450 return GetGlobalTypesHolder()->GlobalByteBuiltinType();
451 }
452
GlobalShortType() const453 Type *ETSChecker::GlobalShortType() const
454 {
455 return GetGlobalTypesHolder()->GlobalShortType();
456 }
457
GlobalShortBuiltinType() const458 Type *ETSChecker::GlobalShortBuiltinType() const
459 {
460 return GetGlobalTypesHolder()->GlobalShortBuiltinType();
461 }
462
GlobalIntType() const463 Type *ETSChecker::GlobalIntType() const
464 {
465 return GetGlobalTypesHolder()->GlobalIntType();
466 }
467
GlobalIntBuiltinType() const468 Type *ETSChecker::GlobalIntBuiltinType() const
469 {
470 return GetGlobalTypesHolder()->GlobalIntegerBuiltinType();
471 }
472
GlobalLongType() const473 Type *ETSChecker::GlobalLongType() const
474 {
475 return GetGlobalTypesHolder()->GlobalLongType();
476 }
477
GlobalLongBuiltinType() const478 Type *ETSChecker::GlobalLongBuiltinType() const
479 {
480 return GetGlobalTypesHolder()->GlobalLongBuiltinType();
481 }
482
GlobalFloatType() const483 Type *ETSChecker::GlobalFloatType() const
484 {
485 return GetGlobalTypesHolder()->GlobalFloatType();
486 }
487
GlobalFloatBuiltinType() const488 Type *ETSChecker::GlobalFloatBuiltinType() const
489 {
490 return GetGlobalTypesHolder()->GlobalFloatBuiltinType();
491 }
492
GlobalDoubleType() const493 Type *ETSChecker::GlobalDoubleType() const
494 {
495 return GetGlobalTypesHolder()->GlobalDoubleType();
496 }
497
GlobalDoubleBuiltinType() const498 Type *ETSChecker::GlobalDoubleBuiltinType() const
499 {
500 return GetGlobalTypesHolder()->GlobalDoubleBuiltinType();
501 }
502
GlobalCharType() const503 Type *ETSChecker::GlobalCharType() const
504 {
505 return GetGlobalTypesHolder()->GlobalCharType();
506 }
GlobalCharBuiltinType() const507 Type *ETSChecker::GlobalCharBuiltinType() const
508 {
509 return GetGlobalTypesHolder()->GlobalCharBuiltinType();
510 }
511
GlobalETSBooleanType() const512 Type *ETSChecker::GlobalETSBooleanType() const
513 {
514 return GetGlobalTypesHolder()->GlobalETSBooleanType();
515 }
516
GlobalETSBooleanBuiltinType() const517 Type *ETSChecker::GlobalETSBooleanBuiltinType() const
518 {
519 return GetGlobalTypesHolder()->GlobalETSBooleanBuiltinType();
520 }
521
GlobalVoidType() const522 Type *ETSChecker::GlobalVoidType() const
523 {
524 return GetGlobalTypesHolder()->GlobalETSVoidType();
525 }
526
GlobalETSNullType() const527 Type *ETSChecker::GlobalETSNullType() const
528 {
529 return GetGlobalTypesHolder()->GlobalETSNullType();
530 }
531
GlobalETSUndefinedType() const532 Type *ETSChecker::GlobalETSUndefinedType() const
533 {
534 return GetGlobalTypesHolder()->GlobalETSUndefinedType();
535 }
536
GlobalETSAnyType() const537 Type *ETSChecker::GlobalETSAnyType() const
538 {
539 return GetGlobalTypesHolder()->GlobalETSAnyType();
540 }
541
GlobalETSNeverType() const542 Type *ETSChecker::GlobalETSNeverType() const
543 {
544 return GetGlobalTypesHolder()->GlobalETSNeverType();
545 }
546
GlobalETSStringLiteralType() const547 Type *ETSChecker::GlobalETSStringLiteralType() const
548 {
549 return GetGlobalTypesHolder()->GlobalETSStringLiteralType();
550 }
551
GlobalETSBigIntType() const552 Type *ETSChecker::GlobalETSBigIntType() const
553 {
554 return GetGlobalTypesHolder()->GlobalETSBigIntBuiltinType();
555 }
556
GlobalWildcardType() const557 Type *ETSChecker::GlobalWildcardType() const
558 {
559 return GetGlobalTypesHolder()->GlobalWildcardType();
560 }
561
GlobalETSObjectType() const562 ETSObjectType *ETSChecker::GlobalETSObjectType() const
563 {
564 return AsETSObjectType(&GlobalTypesHolder::GlobalETSObjectType);
565 }
566
GlobalETSUnionUndefinedNull() const567 ETSUnionType *ETSChecker::GlobalETSUnionUndefinedNull() const
568 {
569 auto *ret = (GetGlobalTypesHolder()->*&GlobalTypesHolder::GlobalETSUnionUndefinedNull)();
570 return ret != nullptr ? ret->AsETSUnionType() : nullptr;
571 }
572
GlobalETSUnionUndefinedNullObject() const573 ETSUnionType *ETSChecker::GlobalETSUnionUndefinedNullObject() const
574 {
575 auto *ret = (GetGlobalTypesHolder()->*&GlobalTypesHolder::GlobalETSUnionUndefinedNullObject)();
576 return ret != nullptr ? ret->AsETSUnionType() : nullptr;
577 }
578
GlobalBuiltinETSResizableArrayType() const579 ETSObjectType *ETSChecker::GlobalBuiltinETSResizableArrayType() const
580 {
581 return AsETSObjectType(&GlobalTypesHolder::GlobalArrayBuiltinType);
582 }
583
GlobalBuiltinETSStringType() const584 ETSObjectType *ETSChecker::GlobalBuiltinETSStringType() const
585 {
586 return AsETSObjectType(&GlobalTypesHolder::GlobalETSStringBuiltinType);
587 }
588
GlobalBuiltinETSBigIntType() const589 ETSObjectType *ETSChecker::GlobalBuiltinETSBigIntType() const
590 {
591 return AsETSObjectType(&GlobalTypesHolder::GlobalETSBigIntBuiltinType);
592 }
593
GlobalBuiltinTypeType() const594 ETSObjectType *ETSChecker::GlobalBuiltinTypeType() const
595 {
596 return AsETSObjectType(&GlobalTypesHolder::GlobalTypeBuiltinType);
597 }
598
GlobalBuiltinExceptionType() const599 ETSObjectType *ETSChecker::GlobalBuiltinExceptionType() const
600 {
601 return AsETSObjectType(&GlobalTypesHolder::GlobalExceptionBuiltinType);
602 }
603
GlobalBuiltinErrorType() const604 ETSObjectType *ETSChecker::GlobalBuiltinErrorType() const
605 {
606 return AsETSObjectType(&GlobalTypesHolder::GlobalErrorBuiltinType);
607 }
608
GlobalStringBuilderBuiltinType() const609 ETSObjectType *ETSChecker::GlobalStringBuilderBuiltinType() const
610 {
611 return AsETSObjectType(&GlobalTypesHolder::GlobalStringBuilderBuiltinType);
612 }
613
GlobalBuiltinPromiseType() const614 ETSObjectType *ETSChecker::GlobalBuiltinPromiseType() const
615 {
616 return AsETSObjectType(&GlobalTypesHolder::GlobalPromiseBuiltinType);
617 }
618
GlobalBuiltinFunctionType() const619 ETSObjectType *ETSChecker::GlobalBuiltinFunctionType() const
620 {
621 return AsETSObjectType(&GlobalTypesHolder::GlobalFunctionBuiltinType);
622 }
623
GlobalBuiltinJSRuntimeType() const624 ETSObjectType *ETSChecker::GlobalBuiltinJSRuntimeType() const
625 {
626 return AsETSObjectType(&GlobalTypesHolder::GlobalJSRuntimeBuiltinType);
627 }
628
GlobalBuiltinJSValueType() const629 ETSObjectType *ETSChecker::GlobalBuiltinJSValueType() const
630 {
631 return AsETSObjectType(&GlobalTypesHolder::GlobalJSValueBuiltinType);
632 }
633
GlobalBuiltinFunctionType(size_t nargs,bool hasRest) const634 ETSObjectType *ETSChecker::GlobalBuiltinFunctionType(size_t nargs, bool hasRest) const
635 {
636 return AsETSObjectType(&GlobalTypesHolder::GlobalFunctionBuiltinType, nargs, hasRest);
637 }
638
GlobalBuiltinLambdaType(size_t nargs,bool hasRest) const639 ETSObjectType *ETSChecker::GlobalBuiltinLambdaType(size_t nargs, bool hasRest) const
640 {
641 return AsETSObjectType(&GlobalTypesHolder::GlobalLambdaBuiltinType, nargs, hasRest);
642 }
643
GlobalBuiltinTupleType(size_t nargs) const644 ETSObjectType *ETSChecker::GlobalBuiltinTupleType(size_t nargs) const
645 {
646 return AsETSObjectType(&GlobalTypesHolder::GlobalTupleBuiltinType, nargs);
647 }
648
GlobalBuiltinFunctionTypeVariadicThreshold() const649 size_t ETSChecker::GlobalBuiltinFunctionTypeVariadicThreshold() const
650 {
651 return GetGlobalTypesHolder()->VariadicFunctionTypeThreshold();
652 }
653
GlobalBuiltinDynamicType(Language lang) const654 ETSObjectType *ETSChecker::GlobalBuiltinDynamicType(Language lang) const
655 {
656 if (lang.GetId() == Language::Id::JS) {
657 return GlobalBuiltinJSValueType();
658 }
659 return nullptr;
660 }
661
GlobalBuiltinBoxType(Type * contents)662 ETSObjectType *ETSChecker::GlobalBuiltinBoxType(Type *contents)
663 {
664 switch (TypeKind(contents)) {
665 case TypeFlag::ETS_BOOLEAN:
666 return AsETSObjectType(&GlobalTypesHolder::GlobalBooleanBoxBuiltinType);
667 case TypeFlag::BYTE:
668 return AsETSObjectType(&GlobalTypesHolder::GlobalByteBoxBuiltinType);
669 case TypeFlag::CHAR:
670 return AsETSObjectType(&GlobalTypesHolder::GlobalCharBoxBuiltinType);
671 case TypeFlag::SHORT:
672 return AsETSObjectType(&GlobalTypesHolder::GlobalShortBoxBuiltinType);
673 case TypeFlag::INT:
674 return AsETSObjectType(&GlobalTypesHolder::GlobalIntBoxBuiltinType);
675 case TypeFlag::LONG:
676 return AsETSObjectType(&GlobalTypesHolder::GlobalLongBoxBuiltinType);
677 case TypeFlag::FLOAT:
678 return AsETSObjectType(&GlobalTypesHolder::GlobalFloatBoxBuiltinType);
679 case TypeFlag::DOUBLE:
680 return AsETSObjectType(&GlobalTypesHolder::GlobalDoubleBoxBuiltinType);
681 default: {
682 auto *base = AsETSObjectType(&GlobalTypesHolder::GlobalBoxBuiltinType);
683 auto *substitution = NewSubstitution();
684 ES2PANDA_ASSERT(base != nullptr);
685 substitution->emplace(base->TypeArguments()[0]->AsETSTypeParameter(), contents);
686 return base->Substitute(Relation(), substitution);
687 }
688 }
689 }
690
GlobalArrayTypes()691 GlobalArraySignatureMap &ETSChecker::GlobalArrayTypes()
692 {
693 return globalArraySignatures_;
694 }
695
GlobalArrayTypes() const696 const GlobalArraySignatureMap &ETSChecker::GlobalArrayTypes() const
697 {
698 return globalArraySignatures_;
699 }
700
GlobalTypeError() const701 Type *ETSChecker::GlobalTypeError() const
702 {
703 return GetGlobalTypesHolder()->GlobalTypeError();
704 }
705
InvalidateType(ir::Typed<ir::AstNode> * node)706 Type *ETSChecker::InvalidateType(ir::Typed<ir::AstNode> *node)
707 {
708 return node->SetTsType(GlobalTypeError());
709 }
710
TypeError(ir::Typed<ir::AstNode> * node,const diagnostic::DiagnosticKind & diagKind,const lexer::SourcePosition & at)711 Type *ETSChecker::TypeError(ir::Typed<ir::AstNode> *node, const diagnostic::DiagnosticKind &diagKind,
712 const lexer::SourcePosition &at)
713 {
714 return TypeError(node, diagKind, util::DiagnosticMessageParams {}, at);
715 }
716
TypeError(ir::Typed<ir::AstNode> * node,const diagnostic::DiagnosticKind & diagKind,const util::DiagnosticMessageParams & list,const lexer::SourcePosition & at)717 Type *ETSChecker::TypeError(ir::Typed<ir::AstNode> *node, const diagnostic::DiagnosticKind &diagKind,
718 const util::DiagnosticMessageParams &list, const lexer::SourcePosition &at)
719 {
720 LogError(diagKind, list, at);
721 return InvalidateType(node);
722 }
723
TypeError(varbinder::Variable * var,const diagnostic::DiagnosticKind & diagKind,const lexer::SourcePosition & at)724 Type *ETSChecker::TypeError(varbinder::Variable *var, const diagnostic::DiagnosticKind &diagKind,
725 const lexer::SourcePosition &at)
726 {
727 return TypeError(var, diagKind, {}, at);
728 }
729
TypeError(varbinder::Variable * var,const diagnostic::DiagnosticKind & diagKind,const util::DiagnosticMessageParams & list,const lexer::SourcePosition & at)730 Type *ETSChecker::TypeError(varbinder::Variable *var, const diagnostic::DiagnosticKind &diagKind,
731 const util::DiagnosticMessageParams &list, const lexer::SourcePosition &at)
732 {
733 LogError(diagKind, list, at);
734 var->SetTsType(GlobalTypeError());
735 return var->TsType();
736 }
737
HandleUpdatedCallExpressionNode(ir::CallExpression * callExpr)738 void ETSChecker::HandleUpdatedCallExpressionNode(ir::CallExpression *callExpr)
739 {
740 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
741 VarBinder()->AsETSBinder()->HandleCustomNodes(callExpr);
742 }
743
SelectGlobalIntegerTypeForNumeric(Type * type)744 Type *ETSChecker::SelectGlobalIntegerTypeForNumeric(Type *type)
745 {
746 switch (ETSType(type)) {
747 case checker::TypeFlag::FLOAT: {
748 return GlobalIntType();
749 }
750 case checker::TypeFlag::DOUBLE: {
751 return GlobalLongType();
752 }
753 default: {
754 return type;
755 }
756 }
757 }
758
FindExtensionSetterInMap(util::StringView name,ETSObjectType * type)759 Signature *ETSChecker::FindExtensionSetterInMap(util::StringView name, ETSObjectType *type)
760 {
761 return GetGlobalTypesHolder()->FindExtensionSetterInMap(name, type);
762 }
763
FindExtensionGetterInMap(util::StringView name,ETSObjectType * type)764 Signature *ETSChecker::FindExtensionGetterInMap(util::StringView name, ETSObjectType *type)
765 {
766 return GetGlobalTypesHolder()->FindExtensionGetterInMap(name, type);
767 }
768
InsertExtensionSetterToMap(util::StringView name,ETSObjectType * type,Signature * sig)769 void ETSChecker::InsertExtensionSetterToMap(util::StringView name, ETSObjectType *type, Signature *sig)
770 {
771 GetGlobalTypesHolder()->InsertExtensionSetterToMap(name, type, sig);
772 }
773
InsertExtensionGetterToMap(util::StringView name,ETSObjectType * type,Signature * sig)774 void ETSChecker::InsertExtensionGetterToMap(util::StringView name, ETSObjectType *type, Signature *sig)
775 {
776 GetGlobalTypesHolder()->InsertExtensionGetterToMap(name, type, sig);
777 }
778 } // namespace ark::es2panda::checker
779