diff --git a/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp index 488ac39e57bdf568507eaf2095140e28d9c9ec96..7f0b016fd06e4578296edf45188f4729ecb84c23 100644 --- a/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp @@ -217,13 +217,18 @@ static void ExpandOptionalParameterAnnotationsToUnions(public_lib::Context *ctx, auto param = p->AsETSParameterExpression(); if (param->IsOptional() && param->Initializer() == nullptr) { ArenaVector typeNodes(allocator->Adapter()); - if (param->TypeAnnotation() != nullptr) { - typeNodes.emplace_back(param->TypeAnnotation()); - } + auto *typeAnnotation = param->TypeAnnotation(); + + typeNodes.emplace_back(typeAnnotation); typeNodes.emplace_back(allocator->New(allocator)); + param->SetTypeAnnotation( util::NodeAllocator::ForceSetParent(allocator, std::move(typeNodes), allocator)); param->TypeAnnotation()->SetParent(param->Ident()); + + // NOTE (DZ): temporary solution until node history starts working properly + param->TypeAnnotation()->SetOriginalNode(typeAnnotation); + typeAnnotation->SetParent(param->TypeAnnotation()); } } } @@ -247,6 +252,10 @@ static void ClearOptionalParameters(public_lib::Context *ctx, ir::ScriptFunction allocator); ES2PANDA_ASSERT(param); param->SetParent(function); + + // NOTE (DZ): temporary solution until node history starts working properly + param->SetOriginalNode(oldParam); + oldParam->SetParent(param); param->SetOriginalNode(oldParam); } ES2PANDA_ASSERT(!param->AsETSParameterExpression()->IsOptional()); @@ -309,12 +318,12 @@ bool DefaultParametersInConstructorLowering::PerformForModule(public_lib::Contex // store all nodes (which is function definition with default/optional parameters) // to specific list, to process them later, as for now we can't modify AST in the // middle of walking through it - foundNodes.push_back(ast->AsMethodDefinition()); + foundNodes.emplace_back(ast->AsMethodDefinition()); } } }); - for (auto &it : foundNodes) { + for (auto *it : foundNodes) { ProcessGlobalFunctionDefinition(it, ctx); } return true; diff --git a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp index 2c7afb91de09f6858c4fe0525b746f225d4ed6b4..202af9d21ad15e670bee02185a4e4d9c356198aa 100644 --- a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp @@ -21,6 +21,10 @@ namespace ark::es2panda::compiler { static ir::Statement *TransformInitializer(ArenaAllocator *allocator, parser::ETSParser *parser, ir::ETSParameterExpression *param) { + // NOTE (DZ): temporary solution until node history starts working properly + auto *oldParam = param->Clone(allocator, param); + param->SetOriginalNode(oldParam); + auto const ident = param->Ident(); auto const init = param->Initializer(); auto const typeAnnotation = param->TypeAnnotation(); @@ -44,15 +48,14 @@ static void TransformDefaultParameters(public_lib::Context *ctx, ir::ScriptFunct { auto validateDefaultParamInDeclare = [ctx, function, ¶ms]() { for (auto param : params) { - if (param->Initializer() == nullptr) { - continue; - } + ES2PANDA_ASSERT(param->Initializer() != nullptr); param->SetInitializer(nullptr); if ((function->Flags() & ir::ScriptFunctionFlags::EXTERNAL) != 0U) { ctx->GetChecker()->AsETSChecker()->LogError(diagnostic::DEFAULT_PARAM_IN_DECLARE, param->Start()); } } }; + if (isInterfaceFunction) { for (const auto param : params) { TransformInitializer(ctx->allocator, ctx->parser->AsETSParser(), param); @@ -72,8 +75,8 @@ static void TransformDefaultParameters(public_lib::Context *ctx, ir::ScriptFunct bodyStmt.insert(bodyStmt.begin(), params.size(), nullptr); - for (size_t dfltIdx = 0; dfltIdx < params.size(); ++dfltIdx) { - auto const param = params.at(dfltIdx); + for (std::size_t dfltIdx = 0U; dfltIdx < params.size(); ++dfltIdx) { + auto *const param = params[dfltIdx]; auto stmt = TransformInitializer(allocator, parser, param); bodyStmt[dfltIdx] = stmt; // From a developer's perspective, this locational information is more intuitive. @@ -101,7 +104,7 @@ static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *func param->AsETSParameterExpression()->SetInitializer(nullptr); continue; } - defaultParams.push_back(param->AsETSParameterExpression()); + defaultParams.emplace_back(param->AsETSParameterExpression()); } if (defaultParams.empty()) { diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index 2dbccd6b9f9b116a96a841649a08325ce8533095..398d648b265a750391fcfb1675e9aa5ca69b14c1 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -224,20 +224,28 @@ void ETSParameterExpression::Dump(ir::SrcDumper *const dumper) const if (IsRestParameter()) { Spread()->Dump(dumper); } else { - auto const ident = Ident(); - auto const initializer = Initializer(); - if (ident != nullptr) { - ES2PANDA_ASSERT(ident_->IsAnnotatedExpression()); - ident->Dump(dumper); - if (IsOptional() && initializer == nullptr) { - dumper->Add("?"); - } - auto typeAnnotation = ident->AsAnnotatedExpression()->TypeAnnotation(); - if (typeAnnotation != nullptr) { - dumper->Add(": "); - typeAnnotation->Dump(dumper); + // NOTE (DZ): temporary solution until node history starts working properly + ETSParameterExpression const *node = + OriginalNode() == nullptr ? this : OriginalNode()->AsETSParameterExpression(); + + auto const ident = node->Ident(); + ES2PANDA_ASSERT(ident != nullptr); + auto const initializer = node->Initializer(); + + ident->Dump(dumper); + if (node->IsOptional() && initializer == nullptr) { + dumper->Add("?"); + } + + auto typeAnnotation = ident->AsAnnotatedExpression()->TypeAnnotation(); + if (typeAnnotation != nullptr) { + if (typeAnnotation->OriginalNode() != nullptr) { + typeAnnotation = typeAnnotation->OriginalNode()->AsExpression()->AsTypeNode(); } + dumper->Add(": "); + typeAnnotation->Dump(dumper); } + if (initializer != nullptr) { dumper->Add(" = "); initializer->Dump(dumper);