Lines Matching +full:parserprogram +full:-
7 * http://www.apache.org/licenses/LICENSE-2.0
58 for (ir::AstNode *curr = ast->Parent(); curr != nullptr; curr = curr->Parent()) { in FindEnclosingClassAndFunction()
59 if (curr->IsClassDeclaration()) { in FindEnclosingClassAndFunction()
60 return {curr->AsClassDeclaration(), function}; in FindEnclosingClassAndFunction()
62 if (curr->IsScriptFunction()) { in FindEnclosingClassAndFunction()
63 function = curr->AsScriptFunction(); in FindEnclosingClassAndFunction()
71 return lambda->IsAnyChild([](ir::AstNode *ast) { return ast->IsThisExpression(); }); in CheckIfNeedThis()
97 auto *allocator = ctx->allocator; in CloneTypeParams()
98 auto *checker = ctx->checker->AsETSChecker(); in CloneTypeParams()
100 auto *newScope = allocator->New<varbinder::LocalScope>(allocator, enclosingScope); in CloneTypeParams()
101 auto newTypeParams = ArenaVector<checker::ETSTypeParameter *>(allocator->Adapter()); in CloneTypeParams()
102 auto newTypeParamNodes = ArenaVector<ir::TSTypeParameter *>(allocator->Adapter()); in CloneTypeParams()
103 auto *substitution = checker->NewSubstitution(); in CloneTypeParams()
105 for (size_t ix = 0; ix < oldIrTypeParams->Params().size(); ix++) { in CloneTypeParams()
106 auto *oldTypeParamNode = oldIrTypeParams->Params()[ix]; in CloneTypeParams()
107 auto *oldTypeParam = enclosingFunction->Signature()->TypeParams()[ix]->AsETSTypeParameter(); in CloneTypeParams()
108 …auto *newTypeParamId = allocator->New<ir::Identifier>(oldTypeParamNode->Name()->Name(), allocator); in CloneTypeParams()
111 auto *newTypeParam = allocator->New<checker::ETSTypeParameter>(); in CloneTypeParams()
112 newTypeParam->SetDeclNode(newTypeParamNode); in CloneTypeParams()
114 … auto *newTypeParamDecl = allocator->New<varbinder::TypeParameterDecl>(newTypeParamId->Name()); in CloneTypeParams()
115 newTypeParamDecl->BindNode(newTypeParamNode); in CloneTypeParams()
117 …allocator->New<varbinder::LocalVariable>(newTypeParamDecl, varbinder::VariableFlags::TYPE_PARAMETE… in CloneTypeParams()
119 newTypeParamVar->SetTsType(newTypeParam); in CloneTypeParams()
120 newScope->InsertBinding(newTypeParamId->Name(), newTypeParamVar); in CloneTypeParams()
121 newTypeParamId->SetVariable(newTypeParamVar); in CloneTypeParams()
125 substitution->emplace(oldTypeParam, newTypeParam); in CloneTypeParams()
128 for (size_t ix = 0; ix < oldIrTypeParams->Params().size(); ix++) { in CloneTypeParams()
129 auto *oldTypeParam = enclosingFunction->Signature()->TypeParams()[ix]->AsETSTypeParameter(); in CloneTypeParams()
131 if (auto *oldConstraint = oldTypeParam->GetConstraintType(); oldConstraint != nullptr) { in CloneTypeParams()
132 auto *newConstraint = oldConstraint->Substitute(checker->Relation(), substitution); in CloneTypeParams()
133 newTypeParams[ix]->SetConstraintType(newConstraint); in CloneTypeParams()
134 newTypeParamNodes[ix]->SetConstraint(allocator->New<ir::OpaqueTypeNode>(newConstraint)); in CloneTypeParams()
135 newTypeParamNodes[ix]->Constraint()->SetParent(newTypeParamNodes[ix]); in CloneTypeParams()
137 if (auto *oldDefault = oldTypeParam->GetDefaultType(); oldDefault != nullptr) { in CloneTypeParams()
138 auto *newDefault = oldDefault->Substitute(checker->Relation(), substitution); in CloneTypeParams()
139 newTypeParams[ix]->SetDefaultType(newDefault); in CloneTypeParams()
140 newTypeParamNodes[ix]->SetDefaultType(allocator->New<ir::OpaqueTypeNode>(newDefault)); in CloneTypeParams()
141 newTypeParamNodes[ix]->DefaultType()->SetParent(newTypeParamNodes[ix]); in CloneTypeParams()
146 allocator, std::move(newTypeParamNodes), oldIrTypeParams->RequiredParams()); in CloneTypeParams()
147 newIrTypeParams->SetScope(newScope); in CloneTypeParams()
156 auto allocator = ctx->allocator; in CreateLambdaCalleeParameters()
157 auto checker = ctx->checker->AsETSChecker(); in CreateLambdaCalleeParameters()
158 auto varBinder = ctx->checker->VarBinder(); in CreateLambdaCalleeParameters()
159 auto resParams = ArenaVector<ir::Expression *>(allocator->Adapter()); in CreateLambdaCalleeParameters()
160 auto varMap = ArenaMap<varbinder::Variable *, varbinder::Variable *>(allocator->Adapter()); in CreateLambdaCalleeParameters()
166 …auto *newType = capturedVar->TsType()->Substitute(checker->Relation(), calleeParameterInfo.substit… in CreateLambdaCalleeParameters()
168 allocator, capturedVar->Name(), allocator->New<ir::OpaqueTypeNode>(newType), allocator); in CreateLambdaCalleeParameters()
170 auto [_, var] = varBinder->AddParamDecl(param); in CreateLambdaCalleeParameters()
172 var->SetTsType(newType); in CreateLambdaCalleeParameters()
173 var->SetScope(calleeParameterInfo.paramScope); in CreateLambdaCalleeParameters()
174 param->SetVariable(var); in CreateLambdaCalleeParameters()
175 param->SetTsType(newType); in CreateLambdaCalleeParameters()
182 for (auto *oldParam : calleeParameterInfo.lambda->Function()->Params()) { in CreateLambdaCalleeParameters()
187 … auto *oldParamType = oldParam->AsETSParameterExpression()->Ident()->TypeAnnotation()->TsType(); in CreateLambdaCalleeParameters()
188 …auto *newParamType = oldParamType->Substitute(checker->Relation(), calleeParameterInfo.substitutio… in CreateLambdaCalleeParameters()
189 auto *newParam = oldParam->AsETSParameterExpression()->Clone(allocator, nullptr); in CreateLambdaCalleeParameters()
190 newParam->Ident()->SetVariable(nullptr); // Remove the cloned variable. in CreateLambdaCalleeParameters()
191 auto [_, var] = varBinder->AddParamDecl(newParam); in CreateLambdaCalleeParameters()
193 var->SetTsType(newParamType); in CreateLambdaCalleeParameters()
194 var->SetScope(calleeParameterInfo.paramScope); in CreateLambdaCalleeParameters()
195 newParam->SetVariable(var); in CreateLambdaCalleeParameters()
196 newParam->SetTsType(newParamType); in CreateLambdaCalleeParameters()
197 newParam->Ident()->SetTsType(newParamType); in CreateLambdaCalleeParameters()
199 varMap[oldParam->AsETSParameterExpression()->Variable()] = var; in CreateLambdaCalleeParameters()
202 if (newParam->TypeAnnotation()->IsETSFunctionType()) { in CreateLambdaCalleeParameters()
205 InitScopesPhaseETS::RunExternalNode(newParam->TypeAnnotation(), varBinder); in CreateLambdaCalleeParameters()
216 if (body == nullptr || body->Scope() == nullptr) { in ProcessCalleeMethodBody()
219 body->Scope()->SetParent(paramScope); in ProcessCalleeMethodBody()
220 body->IterateRecursively([&](ir::AstNode *node) { in ProcessCalleeMethodBody()
221 if (node->IsIdentifier()) { in ProcessCalleeMethodBody()
222 auto *id = node->AsIdentifier(); in ProcessCalleeMethodBody()
223 if (auto ref = varMap.find(id->Variable()); ref != varMap.end()) { in ProcessCalleeMethodBody()
224 id->SetVariable(ref->second); in ProcessCalleeMethodBody()
230 if (node->IsTyped() && node->AsTyped()->TsType() != nullptr) { in ProcessCalleeMethodBody()
231 …node->AsTyped()->SetTsType(node->AsTyped()->TsType()->Substitute(checker->Relation(), substitution… in ProcessCalleeMethodBody()
233 if (node->IsCallExpression()) { in ProcessCalleeMethodBody()
234 node->AsCallExpression()->SetSignature( in ProcessCalleeMethodBody()
235 … node->AsCallExpression()->Signature()->Substitute(checker->Relation(), substitution)); in ProcessCalleeMethodBody()
237 if (node->IsETSNewClassInstanceExpression()) { in ProcessCalleeMethodBody()
238 node->AsETSNewClassInstanceExpression()->SetSignature( in ProcessCalleeMethodBody()
239 …node->AsETSNewClassInstanceExpression()->GetSignature()->Substitute(checker->Relation(), substitut… in ProcessCalleeMethodBody()
241 if (node->IsScriptFunction()) { in ProcessCalleeMethodBody()
242 node->AsScriptFunction()->SetSignature( in ProcessCalleeMethodBody()
243 … node->AsScriptFunction()->Signature()->Substitute(checker->Relation(), substitution)); in ProcessCalleeMethodBody()
245 if (node->IsVariableDeclarator()) { in ProcessCalleeMethodBody()
246 auto *id = node->AsVariableDeclarator()->Id(); in ProcessCalleeMethodBody()
247 …id->Variable()->SetTsType(id->Variable()->TsType()->Substitute(checker->Relation(), substitution)); in ProcessCalleeMethodBody()
256 auto *allocator = ctx->allocator; in SetUpCalleeMethod()
257 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in SetUpCalleeMethod()
259 auto *calleeClass = info->calleeClass; in SetUpCalleeMethod()
260 auto *funcScope = func->Scope(); in SetUpCalleeMethod()
261 auto *paramScope = funcScope->ParamScope(); in SetUpCalleeMethod()
263 … (info->callReceiver != nullptr ? ir::ModifierFlags::NONE : ir::ModifierFlags::STATIC) | in SetUpCalleeMethod()
264 cmInfo->auxModifierFlags; in SetUpCalleeMethod()
266 auto *calleeNameId = allocator->New<ir::Identifier>(cmInfo->calleeName, allocator); in SetUpCalleeMethod()
267 func->SetIdent(calleeNameId); in SetUpCalleeMethod()
268 calleeNameId->SetParent(func); in SetUpCalleeMethod()
270 auto *calleeNameClone = calleeNameId->Clone(allocator, nullptr); in SetUpCalleeMethod()
274 calleeClass->Definition()->Body().push_back(method); in SetUpCalleeMethod()
275 method->SetParent(calleeClass->Definition()); in SetUpCalleeMethod()
279 …varBinder->NewVarDecl<varbinder::FunctionDecl>(func->Start(), allocator, cmInfo->calleeName, func); in SetUpCalleeMethod()
281 var->AddFlag(varbinder::VariableFlags::METHOD); in SetUpCalleeMethod()
282 var->SetScope(scopeForMethod); in SetUpCalleeMethod()
283 func->Id()->SetVariable(var); in SetUpCalleeMethod()
284 method->Id()->SetVariable(var); in SetUpCalleeMethod()
285 if (info->callReceiver != nullptr) { in SetUpCalleeMethod()
287 varBinder->AddMandatoryParam(varbinder::TypedBinder::MANDATORY_PARAM_THIS); in SetUpCalleeMethod()
288 …calleeClass->Definition()->TsType()->AsETSObjectType()->AddProperty<checker::PropertyType::INSTANC… in SetUpCalleeMethod()
289 var->AsLocalVariable()); in SetUpCalleeMethod()
291 …calleeClass->Definition()->TsType()->AsETSObjectType()->AddProperty<checker::PropertyType::STATIC_… in SetUpCalleeMethod()
292 var->AsLocalVariable()); in SetUpCalleeMethod()
295 varbinder::BoundContext bctx {varBinder->GetRecordTable(), calleeClass->Definition(), true}; in SetUpCalleeMethod()
296 varBinder->ResolveReferencesForScopeWithContext(func, funcScope); in SetUpCalleeMethod()
298 … auto checkerCtx = checker::SavedCheckerContext(ctx->checker, checker::CheckerStatus::IN_CLASS, in SetUpCalleeMethod()
299 … calleeClass->Definition()->TsType()->AsETSObjectType()); in SetUpCalleeMethod()
300 method->Check(ctx->checker->AsETSChecker()); in SetUpCalleeMethod()
302 func->Id()->SetVariable(variable); in SetUpCalleeMethod()
303 method->Id()->SetVariable(variable); in SetUpCalleeMethod()
304 method->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); in SetUpCalleeMethod()
314 auto *allocator = ctx->allocator; in GetAndApplyFunctionScope()
315 …auto *funcScope = cmInfo->body == nullptr ? allocator->New<varbinder::FunctionScope>(allocator, pa… in GetAndApplyFunctionScope()
316 : (cmInfo->body->Scope() == nullptr in GetAndApplyFunctionScope()
317 … ? allocator->New<varbinder::FunctionScope>(allocator, paramScope) in GetAndApplyFunctionScope()
318 : cmInfo->body->Scope()->AsFunctionScope()); in GetAndApplyFunctionScope()
319 …funcScope->BindName(info->calleeClass->Definition()->TsType()->AsETSObjectType()->AssemblerName()); in GetAndApplyFunctionScope()
320 func->SetScope(funcScope); in GetAndApplyFunctionScope()
322 if (cmInfo->body != nullptr) { in GetAndApplyFunctionScope()
323 cmInfo->body->AsBlockStatement()->SetScope(funcScope); in GetAndApplyFunctionScope()
324 cmInfo->body->SetParent(func); in GetAndApplyFunctionScope()
335 auto *allocator = ctx->allocator; in CreateCalleeMethod()
336 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in CreateCalleeMethod()
337 auto *checker = ctx->checker->AsETSChecker(); in CreateCalleeMethod()
339 auto *classScope = info->calleeClass->Definition()->Scope()->AsClassScope(); in CreateCalleeMethod()
341 …auto *oldTypeParams = (info->enclosingFunction != nullptr) ? info->enclosingFunction->TypeParams()… in CreateCalleeMethod()
343 …info->callReceiver != nullptr ? classScope->InstanceMethodScope() : classScope->StaticMethodScope(… in CreateCalleeMethod()
345 …auto [newTypeParams, subst0] = CloneTypeParams(ctx, oldTypeParams, info->enclosingFunction, enclos… in CreateCalleeMethod()
347 auto *scopeForMethod = newTypeParams != nullptr ? newTypeParams->Scope() : enclosingScope; in CreateCalleeMethod()
350 auto paramScope = allocator->New<varbinder::FunctionParamScope>(allocator, scopeForMethod); in CreateCalleeMethod()
352 CalleeParameterInfo cpi {lambda, *info->capturedVars, paramScope, substitution, limit}; in CreateCalleeMethod()
358 cmInfo->forcedReturnType != nullptr in CreateCalleeMethod()
359 ? cmInfo->forcedReturnType in CreateCalleeMethod()
360 … : lambda->Function()->Signature()->ReturnType()->Substitute(checker->Relation(), substitution); in CreateCalleeMethod()
361 auto returnTypeAnnotation = allocator->New<ir::OpaqueTypeNode>(returnType); in CreateCalleeMethod()
363 auto funcFlags = ir::ScriptFunctionFlags::METHOD | cmInfo->auxFunctionFlags; in CreateCalleeMethod()
365 … (info->callReceiver != nullptr ? ir::ModifierFlags::NONE : ir::ModifierFlags::STATIC) | in CreateCalleeMethod()
366 cmInfo->auxModifierFlags; in CreateCalleeMethod()
371 …cmInfo->body, ir::FunctionSignature(newTypeParams, std::move(params), returnTypeAnnotation), funcF… in CreateCalleeMethod()
374 ProcessCalleeMethodBody(cmInfo->body, checker, paramScope, substitution, varMap); in CreateCalleeMethod()
376 for (auto *param : func->Params()) { in CreateCalleeMethod()
377 param->SetParent(func); in CreateCalleeMethod()
381 funcScope->BindNode(func); in CreateCalleeMethod()
382 paramScope->BindNode(func); in CreateCalleeMethod()
383 funcScope->AssignParamScope(paramScope); in CreateCalleeMethod()
384 paramScope->BindFunctionScope(funcScope); in CreateCalleeMethod()
390 ASSERT(ov->Name() == nv->Name()); in CreateCalleeMethod()
391 auto name = ov->Name(); in CreateCalleeMethod()
392 funcScope->EraseBinding(name); in CreateCalleeMethod()
393 funcScope->InsertBinding(name, nv); in CreateCalleeMethod()
402 auto *allocator = ctx->allocator; in CreateCalleeDefault()
403 auto *checker = ctx->checker->AsETSChecker(); in CreateCalleeDefault()
404 auto *body = lambda->Function()->Body()->AsBlockStatement(); in CreateCalleeDefault()
405 auto calleeName = lambda->Function()->IsAsyncFunc() in CreateCalleeDefault()
406 … ? (util::UString {checker::ETSChecker::GetAsyncImplName(info->name), allocator}).View() in CreateCalleeDefault()
407 : info->name; in CreateCalleeDefault()
408 …auto *forcedReturnType = lambda->Function()->IsAsyncFunc() ? checker->GlobalETSNullishObjectType()… in CreateCalleeDefault()
416 if (lambda->Function()->IsAsyncFunc()) { in CreateCalleeDefault()
418 cmInfoAsync.calleeName = info->name; in CreateCalleeDefault()
433 auto *checker = ctx->checker->AsETSChecker(); in ValidateDefaultParameters()
436 for (auto *param : defaultMethod->Function()->Params()) { in ValidateDefaultParameters()
437 if (param->AsETSParameterExpression()->IsDefault()) { in ValidateDefaultParameters()
444 for (; i < lambda->Function()->Params().size(); i++) { in ValidateDefaultParameters()
445 auto *param = lambda->Function()->Params()[i]->AsETSParameterExpression(); in ValidateDefaultParameters()
446 if (param->Initializer() == nullptr) { in ValidateDefaultParameters()
447 …checker->LogTypeError({"Expected initializer for parameter ", param->Ident()->Name(), "."}, param-… in ValidateDefaultParameters()
463 auto *allocator = ctx->allocator; in CreateLambdaClassFields()
464 auto *parser = ctx->parser->AsETSParser(); in CreateLambdaClassFields()
465 auto *checker = ctx->checker->AsETSChecker(); in CreateLambdaClassFields()
466 auto props = ArenaVector<ir::AstNode *>(allocator->Adapter()); in CreateLambdaClassFields()
468 if (info->callReceiver != nullptr) { in CreateLambdaClassFields()
469 auto *outerThisDeclaration = parser->CreateFormattedClassFieldDefinition( in CreateLambdaClassFields()
471 … info->calleeClass->Definition()->TsType()->Substitute(checker->Relation(), substitution)); in CreateLambdaClassFields()
475 for (auto *captured : *info->capturedVars) { in CreateLambdaClassFields()
476 auto *varDeclaration = parser->CreateFormattedClassFieldDefinition( in CreateLambdaClassFields()
477 "@@I1: @@T2", AvoidMandatoryThis(captured->Name()), in CreateLambdaClassFields()
478 captured->TsType()->Substitute(checker->Relation(), substitution)); in CreateLambdaClassFields()
482 classDefinition->AddProperties(std::move(props)); in CreateLambdaClassFields()
488 auto *allocator = ctx->allocator; in CreateLambdaClassConstructor()
489 auto *parser = ctx->parser->AsETSParser(); in CreateLambdaClassConstructor()
490 auto *checker = ctx->checker->AsETSChecker(); in CreateLambdaClassConstructor()
492 auto params = ArenaVector<ir::Expression *>(allocator->Adapter()); in CreateLambdaClassConstructor()
494 auto *substitutedType = type->Substitute(checker->Relation(), substitution); in CreateLambdaClassConstructor()
496 allocator, name, allocator->New<ir::OpaqueTypeNode>(substitutedType), allocator); in CreateLambdaClassConstructor()
501 if (info->callReceiver != nullptr) { in CreateLambdaClassConstructor()
502 makeParam("$this", info->calleeClass->Definition()->TsType()); in CreateLambdaClassConstructor()
504 for (auto *var : *info->capturedVars) { in CreateLambdaClassConstructor()
505 makeParam(AvoidMandatoryThis(var->Name()), var->TsType()); in CreateLambdaClassConstructor()
508 auto bodyStmts = ArenaVector<ir::Statement *>(allocator->Adapter()); in CreateLambdaClassConstructor()
511 …auto *statement = parser->CreateFormattedStatement("this.@@I1 = @@I2", adjustedName, adjustedName); in CreateLambdaClassConstructor()
514 if (info->callReceiver != nullptr) { in CreateLambdaClassConstructor()
517 for (auto *var : *info->capturedVars) { in CreateLambdaClassConstructor()
518 makeStatement(var->Name()); in CreateLambdaClassConstructor()
522 auto *constructorId = allocator->New<ir::Identifier>("constructor", allocator); in CreateLambdaClassConstructor()
523 auto *constructorIdClone = constructorId->Clone(allocator, nullptr); in CreateLambdaClassConstructor()
530 func->SetIdent(constructorId); in CreateLambdaClassConstructor()
537 classDefinition->Body().push_back(ctor); in CreateLambdaClassConstructor()
538 ctor->SetParent(classDefinition); in CreateLambdaClassConstructor()
544 auto *allocator = ctx->allocator; in CreateCallForLambdaClassInvoke()
545 auto *parser = ctx->parser->AsETSParser(); in CreateCallForLambdaClassInvoke()
546 auto *checker = ctx->checker->AsETSChecker(); in CreateCallForLambdaClassInvoke()
548 auto callArguments = ArenaVector<ir::Expression *>(allocator->Adapter()); in CreateCallForLambdaClassInvoke()
549 for (auto *captured : *info->capturedVars) { in CreateCallForLambdaClassInvoke()
550 … auto *arg = parser->CreateFormattedExpression("this.@@I1", AvoidMandatoryThis(captured->Name())); in CreateCallForLambdaClassInvoke()
553 for (auto *lambdaParam : lciInfo->lambdaSignature->Params()) { in CreateCallForLambdaClassInvoke()
554 auto argName = lambdaParam->Name(); in CreateCallForLambdaClassInvoke()
555 auto *type = lambdaParam->TsType()->Substitute(checker->Relation(), lciInfo->substitution); in CreateCallForLambdaClassInvoke()
556 … auto *arg = wrapToObject ? parser->CreateFormattedExpression("@@I1 as @@T2 as @@T3", argName, in CreateCallForLambdaClassInvoke()
557 … checker->MaybeBoxType(type), type) in CreateCallForLambdaClassInvoke()
558 : allocator->New<ir::Identifier>(argName, allocator); in CreateCallForLambdaClassInvoke()
563 if (info->callReceiver != nullptr) { in CreateCallForLambdaClassInvoke()
564 calleeReceiver = parser->CreateFormattedExpression("this.@@I1", "$this"); in CreateCallForLambdaClassInvoke()
566 …calleeReceiver = lciInfo->callee->Parent()->AsClassDefinition()->Ident()->Clone(allocator, nullptr… in CreateCallForLambdaClassInvoke()
570 … allocator, calleeReceiver, lciInfo->callee->Key()->Clone(allocator, nullptr)->AsExpression(), in CreateCallForLambdaClassInvoke()
572 …auto *call = parser->CreateFormattedExpression("@@E1(@@[E2)", calleeMemberExpr, std::move(callArgu… in CreateCallForLambdaClassInvoke()
573 ->AsCallExpression(); in CreateCallForLambdaClassInvoke()
575 if (lciInfo->classDefinition->TypeParams() != nullptr) { in CreateCallForLambdaClassInvoke()
576 auto typeArgs = ArenaVector<ir::TypeNode *>(allocator->Adapter()); in CreateCallForLambdaClassInvoke()
577 for (auto *tp : lciInfo->classDefinition->TypeParams()->Params()) { in CreateCallForLambdaClassInvoke()
578 …typeArgs.push_back(allocator->New<ir::OpaqueTypeNode>(tp->Name()->AsIdentifier()->Variable()->TsTy… in CreateCallForLambdaClassInvoke()
582 call->SetTypeParams(typeArg); in CreateCallForLambdaClassInvoke()
583 typeArg->SetParent(call); in CreateCallForLambdaClassInvoke()
593 auto *allocator = ctx->allocator; in CreateLambdaClassInvoke()
594 auto *parser = ctx->parser->AsETSParser(); in CreateLambdaClassInvoke()
595 auto *checker = ctx->checker->AsETSChecker(); in CreateLambdaClassInvoke()
596 auto *anyType = checker->GlobalETSNullishObjectType(); in CreateLambdaClassInvoke()
598 auto params = ArenaVector<ir::Expression *>(allocator->Adapter()); in CreateLambdaClassInvoke()
599 for (auto *lparam : lciInfo->lambdaSignature->Params()) { in CreateLambdaClassInvoke()
600 …auto *type = wrapToObject ? anyType : lparam->TsType()->Substitute(checker->Relation(), lciInfo->s… in CreateLambdaClassInvoke()
602 allocator, lparam->Name(), allocator->New<ir::OpaqueTypeNode>(type), allocator); in CreateLambdaClassInvoke()
609 auto bodyStmts = ArenaVector<ir::Statement *>(allocator->Adapter()); in CreateLambdaClassInvoke()
610 if (lciInfo->lambdaSignature->ReturnType() == checker->GlobalVoidType()) { in CreateLambdaClassInvoke()
615 allocator, allocator->New<ir::UndefinedLiteral>()); in CreateLambdaClassInvoke()
619 …auto *returnExpr = wrapToObject ? parser->CreateFormattedExpression("@@E1 as @@T2", call, anyType)… in CreateLambdaClassInvoke()
625 auto *returnType2 = allocator->New<ir::OpaqueTypeNode>( in CreateLambdaClassInvoke()
627 … : lciInfo->lambdaSignature->ReturnType()->Substitute(checker->Relation(), lciInfo->substitution)); in CreateLambdaClassInvoke()
633 auto *invokeId = allocator->New<ir::Identifier>(methodName, allocator); in CreateLambdaClassInvoke()
634 func->SetIdent(invokeId); in CreateLambdaClassInvoke()
638 auto *invokeIdClone = invokeId->Clone(allocator, nullptr); in CreateLambdaClassInvoke()
643 lciInfo->classDefinition->Body().push_back(invokeMethod); in CreateLambdaClassInvoke()
644 invokeMethod->SetParent(lciInfo->classDefinition); in CreateLambdaClassInvoke()
650 auto *checker = ctx->checker->AsETSChecker(); in BuildLambdaClass()
656 funcInterfaces.push_back(checker->FunctionTypeToFunctionalInterfaceType( in BuildLambdaClass()
657 lambdaSigs[i]->Substitute(checker->Relation(), substitution))); in BuildLambdaClass()
671 auto *allocator = ctx->allocator; in CreateLambdaClass()
672 auto *parser = ctx->parser->AsETSParser(); in CreateLambdaClass()
673 auto *checker = ctx->checker->AsETSChecker(); in CreateLambdaClass()
674 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in CreateLambdaClass()
676 …auto *oldTypeParams = (info->enclosingFunction != nullptr) ? info->enclosingFunction->TypeParams()… in CreateLambdaClass()
678 …CloneTypeParams(ctx, oldTypeParams, info->enclosingFunction, ctx->parserProgram->GlobalClassScope(… in CreateLambdaClass()
681 …auto lexScope = varbinder::LexicalScope<varbinder::Scope>::Enter(varBinder, ctx->parserProgram->Gl… in CreateLambdaClass()
683 auto lambdaClassName = util::UString {std::string_view {"LambdaObject-"}, allocator}; in CreateLambdaClass()
684 …lambdaClassName.Append(info->calleeClass->Definition()->Ident()->Name()).Append("$").Append(info->… in CreateLambdaClass()
686 ArenaVector<checker::Type *> funcInterfaces(allocator->Adapter()); in CreateLambdaClass()
690 …->CreateFormattedTopLevelStatement(BuildLambdaClass(ctx, lambdaSigs, substitution, funcInterfaces), in CreateLambdaClass()
692 ->AsClassDeclaration(); in CreateLambdaClass()
693 auto *classDefinition = classDeclaration->Definition(); in CreateLambdaClass()
696 classDefinition->Body().clear(); // remove the default empty constructor in CreateLambdaClass()
697 classDefinition->AddModifier(ir::ModifierFlags::PUBLIC | ir::ModifierFlags::FUNCTIONAL); in CreateLambdaClass()
699 classDefinition->SetTypeParams(newTypeParams); in CreateLambdaClass()
700 newTypeParams->SetParent(classDefinition); in CreateLambdaClass()
703 auto *program = varBinder->GetRecordTable()->Program(); in CreateLambdaClass()
704 program->Ast()->Statements().push_back(classDeclaration); in CreateLambdaClass()
705 classDeclaration->SetParent(program->Ast()); in CreateLambdaClass()
722 varBinder->ResolveReferencesForScopeWithContext(classDeclaration, varBinder->TopScope()); in CreateLambdaClass()
723 classDeclaration->Check(checker); in CreateLambdaClass()
732 auto *allocator = ctx->allocator; in CreateConstructorCall()
733 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in CreateConstructorCall()
734 auto *checker = ctx->checker->AsETSChecker(); in CreateConstructorCall()
736 auto args = ArenaVector<ir::Expression *>(allocator->Adapter()); in CreateConstructorCall()
737 if (info->callReceiver != nullptr) { in CreateConstructorCall()
738 args.push_back(info->callReceiver); in CreateConstructorCall()
740 for (auto captured : *info->capturedVars) { in CreateConstructorCall()
741 auto *id = allocator->New<ir::Identifier>(captured->Name(), allocator); in CreateConstructorCall()
745 … checker::ETSObjectType *constructedType = lambdaClass->Definition()->TsType()->AsETSObjectType(); in CreateConstructorCall()
746 if (info->enclosingFunction != nullptr) { in CreateConstructorCall()
747 constructedType = constructedType->SubstituteArguments(checker->Relation(), in CreateConstructorCall()
748 … info->enclosingFunction->Signature()->TypeParams()); in CreateConstructorCall()
751 allocator, allocator->New<ir::OpaqueTypeNode>(constructedType), std::move(args), nullptr); in CreateConstructorCall()
752 auto *lambdaOrFuncRefParent = lambdaOrFuncRef->Parent(); in CreateConstructorCall()
753 newExpr->SetParent(lambdaOrFuncRefParent); in CreateConstructorCall()
754 // NOTE(dslynko, #19869): Required for correct debug-info generation in CreateConstructorCall()
755 …newExpr->SetRange(lambdaOrFuncRefParent != nullptr ? lambdaOrFuncRefParent->Range() : lambdaOrFunc… in CreateConstructorCall()
759 varBinder->ResolveReferencesForScopeWithContext(newExpr, nearestScope); in CreateConstructorCall()
761 auto checkerCtx = checker::SavedCheckerContext(ctx->checker, checker::CheckerStatus::IN_CLASS, in CreateConstructorCall()
762 … info->calleeClass->Definition()->TsType()->AsETSObjectType()); in CreateConstructorCall()
763 auto scopeCtx = checker::ScopeContext(ctx->checker, nearestScope); in CreateConstructorCall()
764 newExpr->Check(checker); in CreateConstructorCall()
771 auto *allocator = ctx->allocator; in ConvertLambda()
772 auto *checker = ctx->checker->AsETSChecker(); in ConvertLambda()
779 info.callReceiver = CheckIfNeedThis(lambda) ? allocator->New<ir::ThisExpression>() : nullptr; in ConvertLambda()
781 if (lambda->Function()->Signature() == nullptr) { in ConvertLambda()
782 lambda->Check(checker); in ConvertLambda()
788 ASSERT(lambda->TsType()->IsETSFunctionType()); in ConvertLambda()
789 auto *lambdaType = lambda->TsType()->AsETSFunctionType(); in ConvertLambda()
790 auto *lambdaClass = CreateLambdaClass(ctx, lambdaType->CallSignatures(), callee, &info); in ConvertLambda()
797 ASSERT(ast->TsType()->IsETSFunctionType()); in GuessSignature()
798 auto *type = ast->TsType()->AsETSFunctionType(); in GuessSignature()
800 if (type->IsETSArrowType()) { in GuessSignature()
801 return type->CallSignatures()[0]; in GuessSignature()
804 if (!ast->Parent()->IsCallExpression()) { in GuessSignature()
805 …checker->LogTypeError(std::initializer_list<checker::TypeErrorMessageElement> {"Cannot deduce call… in GuessSignature()
806 ast->Start()); in GuessSignature()
810 auto &args = ast->Parent()->AsCallExpression()->Arguments(); in GuessSignature()
816 auto *argType = ast->Parent()->AsCallExpression()->Signature()->Params()[ix]->TsType(); in GuessSignature()
819 for (auto *sig : type->CallSignatures()) { in GuessSignature()
820 …auto *tmpFunType = checker->Allocator()->New<checker::ETSFunctionType>("", sig, checker->Allocator… in GuessSignature()
822 …checker->Relation(), ast, tmpFunType, argType, ast->Start(), {}, checker::TypeRelationFlag::NO_THR… in GuessSignature()
828 checker->LogTypeError( in GuessSignature()
830 ast->Start()); in GuessSignature()
840 checker->LogTypeError({"Cannot deduce call signature"}, ast->Start()); in GuessSignature()
847 auto *allocator = ctx->allocator; in GetWrappingLambdaParentFunction()
848 ArenaVector<ir::Expression *> params {allocator->Adapter()}; in GetWrappingLambdaParentFunction()
849 for (auto *p : signature->Params()) { in GetWrappingLambdaParentFunction()
852 …allocator->New<ir::Identifier>(p->Name(), allocator->New<ir::OpaqueTypeNode>(p->TsType()), allocat… in GetWrappingLambdaParentFunction()
860 allocator->New<ir::OpaqueTypeNode>(signature->ReturnType())}, in GetWrappingLambdaParentFunction()
863 ArenaVector<ir::Statement *> bodyStmts {allocator->Adapter()}; in GetWrappingLambdaParentFunction()
864 ArenaVector<ir::Expression *> callArgs {allocator->Adapter()}; in GetWrappingLambdaParentFunction()
866 for (auto *p : func->Params()) { in GetWrappingLambdaParentFunction()
867 ir::Identifier *clone = p->AsETSParameterExpression()->Ident()->Clone(allocator, nullptr); in GetWrappingLambdaParentFunction()
868 if (clone->IsIdentifier() && (clone->IsReference(ScriptExtension::ETS)) && in GetWrappingLambdaParentFunction()
869 (clone->TypeAnnotation() != nullptr)) { in GetWrappingLambdaParentFunction()
870 clone->SetTsTypeAnnotation(nullptr); in GetWrappingLambdaParentFunction()
877 if (signature->ReturnType() == ctx->checker->AsETSChecker()->GlobalVoidType()) { in GetWrappingLambdaParentFunction()
883 …func->SetBody(util::NodeAllocator::ForceSetParent<ir::BlockStatement>(allocator, allocator, std::m… in GetWrappingLambdaParentFunction()
884 func->Body()->SetParent(func); in GetWrappingLambdaParentFunction()
890 auto *allocator = ctx->allocator; in CreateWrappingLambda()
891 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in CreateWrappingLambda()
892 auto *signature = GuessSignature(ctx->checker->AsETSChecker(), funcRef); in CreateWrappingLambda()
897 auto *parent = funcRef->Parent(); in CreateWrappingLambda()
902 lambda->SetParent(parent); in CreateWrappingLambda()
907 varBinder->ResolveReferencesForScopeWithContext(lambda, nearestScope); in CreateWrappingLambda()
912 auto checkerCtx = checker::SavedCheckerContext(ctx->checker, checker::CheckerStatus::IN_CLASS, in CreateWrappingLambda()
913 … enclosingClass->Definition()->TsType()->AsETSObjectType()); in CreateWrappingLambda()
914 auto scopeCtx = checker::ScopeContext(ctx->checker, nearestScope); in CreateWrappingLambda()
915 lambda->Check(ctx->checker->AsETSChecker()); in CreateWrappingLambda()
922 auto *allocator = ctx->allocator; in ConvertFunctionReference()
923 ASSERT(funcRef->IsIdentifier() || in ConvertFunctionReference()
924 (funcRef->IsMemberExpression() && in ConvertFunctionReference()
925 funcRef->AsMemberExpression()->Kind() == ir::MemberExpressionKind::PROPERTY_ACCESS && in ConvertFunctionReference()
926 funcRef->AsMemberExpression()->Property()->IsIdentifier())); in ConvertFunctionReference()
928 if (funcRef->IsIdentifier()) { in ConvertFunctionReference()
929 var = funcRef->AsIdentifier()->Variable(); in ConvertFunctionReference()
931 auto *mexpr = funcRef->AsMemberExpression(); in ConvertFunctionReference()
932 …// NOTE(gogabr): mexpr->PropVar() is a synthetic variable wwith no reference to the method definit… in ConvertFunctionReference()
933 var = mexpr->Object()->TsType()->AsETSObjectType()->GetProperty( in ConvertFunctionReference()
934 mexpr->Property()->AsIdentifier()->Name(), in ConvertFunctionReference()
940 ASSERT(var->Declaration()->Node()->IsMethodDefinition()); in ConvertFunctionReference()
941 auto *method = var->Declaration()->Node()->AsMethodDefinition(); in ConvertFunctionReference()
943 if (method->IsPrivate() || method->IsProtected()) { in ConvertFunctionReference()
954 info.calleeClass = method->Parent()->Parent()->AsClassDeclaration(); in ConvertFunctionReference()
957 auto emptySet = ArenaSet<varbinder::Variable *>(allocator->Adapter()); in ConvertFunctionReference()
959 if (method->IsStatic()) { in ConvertFunctionReference()
962 ASSERT(funcRef->IsMemberExpression()); in ConvertFunctionReference()
963 info.callReceiver = funcRef->AsMemberExpression()->Object(); in ConvertFunctionReference()
966 auto *signature = GuessSignature(ctx->checker->AsETSChecker(), funcRef); in ConvertFunctionReference()
970 ArenaVector<checker::Signature *> signatures(allocator->Adapter()); in ConvertFunctionReference()
979 ASSERT(node->IsCallExpression()); in IsFunctionOrMethodCall()
980 auto const *callee = node->AsCallExpression()->Callee(); in IsFunctionOrMethodCall()
982 if (callee->TsType() != nullptr && callee->TsType()->IsETSExtensionFuncHelperType()) { in IsFunctionOrMethodCall()
987 … if (callee->IsMemberExpression() && callee->AsMemberExpression()->Object()->TsType() != nullptr && in IsFunctionOrMethodCall()
988 (callee->AsMemberExpression()->Object()->TsType()->IsETSEnumType())) { in IsFunctionOrMethodCall()
993 if (callee->IsMemberExpression() && in IsFunctionOrMethodCall()
994 callee->AsMemberExpression()->Kind() == ir::MemberExpressionKind::PROPERTY_ACCESS) { in IsFunctionOrMethodCall()
995 var = callee->AsMemberExpression()->Property()->Variable(); in IsFunctionOrMethodCall()
996 } else if (callee->IsIdentifier()) { in IsFunctionOrMethodCall()
997 var = callee->AsIdentifier()->Variable(); in IsFunctionOrMethodCall()
1000 (var->Flags() & varbinder::VariableFlags::METHOD) != 0; in IsFunctionOrMethodCall()
1005 auto *allocator = ctx->allocator; in InsertInvokeCall()
1006 auto *checker = ctx->checker->AsETSChecker(); in InsertInvokeCall()
1007 auto *varBinder = checker->VarBinder()->AsETSBinder(); in InsertInvokeCall()
1009 auto *oldCallee = call->Callee(); in InsertInvokeCall()
1010 auto *ifaceType = oldCallee->TsType() != nullptr && oldCallee->TsType()->IsETSObjectType() in InsertInvokeCall()
1011 ? oldCallee->TsType()->AsETSObjectType() in InsertInvokeCall()
1012 : checker->FunctionTypeToFunctionalInterfaceType(call->Signature()); in InsertInvokeCall()
1013 if (ifaceType->IsETSDynamicType()) { in InsertInvokeCall()
1016 auto *prop = ifaceType->GetProperty(checker::FUNCTIONAL_INTERFACE_INVOKE_METHOD_NAME, in InsertInvokeCall()
1020 …auto *invoke0Id = allocator->New<ir::Identifier>(checker::FUNCTIONAL_INTERFACE_INVOKE_METHOD_NAME,… in InsertInvokeCall()
1021 invoke0Id->SetTsType(prop->TsType()); in InsertInvokeCall()
1022 invoke0Id->SetVariable(prop); in InsertInvokeCall()
1026 newCallee->SetTsType(prop->TsType()); in InsertInvokeCall()
1027 newCallee->SetObjectType(ifaceType); in InsertInvokeCall()
1029 call->SetCallee(newCallee); in InsertInvokeCall()
1030 call->SetSignature(prop->TsType()->AsETSFunctionType()->CallSignatures()[0]); in InsertInvokeCall()
1036 for (auto *arg : call->Arguments()) { in InsertInvokeCall()
1037 auto boxingFlags = arg->GetBoxingUnboxingFlags(); in InsertInvokeCall()
1039 arg->SetBoxingUnboxingFlags(boxingFlags); in InsertInvokeCall()
1047 return expr->Callee()->IsThisExpression() || expr->Callee()->IsSuperExpression(); in IsRedirectingConstructorCall()
1052 … return expr->Parent()->IsCallExpression() && expr->Parent()->AsCallExpression()->Callee() == expr; in IsInCalleePosition()
1057 if (id->Parent() != nullptr && id->Parent()->IsMemberExpression()) { in IsEnumFunctionCall()
1058 const auto *const expr = id->Parent()->AsMemberExpression(); in IsEnumFunctionCall()
1059 if (expr->Object()->TsType()->IsETSEnumType()) { in IsEnumFunctionCall()
1069 if (node->IsArrowFunctionExpression()) { in BuildLambdaClassWhenNeeded()
1070 return ConvertLambda(ctx, node->AsArrowFunctionExpression()); in BuildLambdaClassWhenNeeded()
1073 if (node->IsIdentifier()) { in BuildLambdaClassWhenNeeded()
1074 auto *id = node->AsIdentifier(); in BuildLambdaClassWhenNeeded()
1075 auto *var = id->Variable(); in BuildLambdaClassWhenNeeded()
1078 …if (id->IsReference(ScriptExtension::ETS) && id->TsType() != nullptr && id->TsType()->IsETSFunctio… in BuildLambdaClassWhenNeeded()
1079 var != nullptr && var->Declaration()->IsFunctionDecl() && !IsInCalleePosition(id) && in BuildLambdaClassWhenNeeded()
1084 if (node->IsMemberExpression()) { in BuildLambdaClassWhenNeeded()
1085 auto *mexpr = node->AsMemberExpression(); in BuildLambdaClassWhenNeeded()
1086 … if (mexpr->Kind() == ir::MemberExpressionKind::PROPERTY_ACCESS && mexpr->TsType() != nullptr && in BuildLambdaClassWhenNeeded()
1087 mexpr->TsType()->IsETSFunctionType() && mexpr->Object()->TsType()->IsETSObjectType()) { in BuildLambdaClassWhenNeeded()
1088 ASSERT(mexpr->Property()->IsIdentifier()); in BuildLambdaClassWhenNeeded()
1089 auto *var = mexpr->Object()->TsType()->AsETSObjectType()->GetProperty( in BuildLambdaClassWhenNeeded()
1090 mexpr->Property()->AsIdentifier()->Name(), in BuildLambdaClassWhenNeeded()
1094 … if (var != nullptr && var->Declaration()->IsFunctionDecl() && !IsInCalleePosition(mexpr)) { in BuildLambdaClassWhenNeeded()
1104 auto *varBinder = ctx->checker->VarBinder()->AsETSBinder(); in CallPerformForExtSources()
1105 for (auto &[_, extPrograms] : program->ExternalSources()) { in CallPerformForExtSources()
1109 phase->Perform(ctx, extProg); in CallPerformForExtSources()
1116 …parser::SavedFormattingFileName savedFormattingName(ctx->parser->AsETSParser(), "lambda-conversion… in Perform()
1120 if (program == ctx->parserProgram) { in Perform()
1124 if (ctx->config->options->CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB) { in Perform()
1128 program->Ast()->TransformChildrenRecursivelyPostorder( in Perform()
1132 if (node->IsCallExpression() && !IsFunctionOrMethodCall(node) && in Perform()
1133 !IsRedirectingConstructorCall(node->AsCallExpression())) { in Perform()
1134 return InsertInvokeCall(ctx, node->AsCallExpression()); in Perform()
1138 program->Ast()->TransformChildrenRecursively(insertInvokeIfNeeded, Name()); in Perform()
1145 …return !program->Ast()->IsAnyChild([](ir::AstNode const *node) { return node->IsArrowFunctionExpre… in Postcondition()