diff --git a/ets2panda/compiler/core/emitter.cpp b/ets2panda/compiler/core/emitter.cpp index 95b7d45aee091ec90d5931fa8cacf7c17d9a676f..0dc1bd6d3a87482a671ac357ea637337fc944fe2 100644 --- a/ets2panda/compiler/core/emitter.cpp +++ b/ets2panda/compiler/core/emitter.cpp @@ -188,14 +188,14 @@ static size_t GetIRNodeWholeLength(const IRNode *node) static std::string WholeLine(const lexer::SourceRange &range) { - // NOTE(rsipka, #24105): The program shouldn't be nullptr auto program = range.start.Program(); - if (program == nullptr || program->SourceCode().Empty()) { + ES2PANDA_ASSERT(program != nullptr); + auto source = program->SourceCode(); + + if (source.Empty()) { return {}; } - auto source = program->SourceCode(); - ES2PANDA_ASSERT(range.end.index <= source.Length()); ES2PANDA_ASSERT(range.end.index >= range.start.index); return source.Substr(range.start.index, range.end.index).EscapeSymbol(); diff --git a/ets2panda/compiler/lowering/ets/enumLowering.cpp b/ets2panda/compiler/lowering/ets/enumLowering.cpp index ffe220155673878c451f11b2edb5d5a9d50dd962..4f5d9e47bd08894a666c639c398a192b90e4af38 100644 --- a/ets2panda/compiler/lowering/ets/enumLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumLowering.cpp @@ -308,7 +308,6 @@ void EnumLoweringPhase::CreateCCtorForEnumClass(ir::ClassDefinition *const enumC ir::ModifierFlags::STATIC, Language(Language::Id::ETS)}); func->SetIdent(id); - id->SetParent(func); auto *funcExpr = AllocNode(func); diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp index 80d07a9441b36076b2aa30a8ddfe0877cd9aa7e1..9484f87715d67c3cb391287ae766c0e56c91981a 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp @@ -358,7 +358,7 @@ ir::MethodDefinition *GlobalClassHandler::CreateGlobalMethod(const std::string_v auto *methodDef = NodeAllocator::Alloc(allocator_, ir::MethodDefinitionKind::METHOD, ident->Clone(allocator_, nullptr)->AsExpression(), funcExpr, functionModifiers, allocator_, false); - methodDef->SetRange({lexer::SourcePosition(globalProgram_), lexer::SourcePosition(globalProgram_)}); + methodDef->SetProgram(globalProgram_); return methodDef; } diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index 99b589133fc097ff076bdff4c4edfebe0c1807c0..f92ff2bf01ebc64a9a7464d3d1ccab022ba54a66 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -286,6 +286,13 @@ public: } } + void SetProgram(const parser::Program *program) noexcept + { + if (program != nullptr) { + range_.SetProgram(program); + } + } + void SetStart(const lexer::SourcePosition &start) noexcept { if (GetHistoryNode()->range_.start != start) { @@ -300,6 +307,11 @@ public: } } + [[nodiscard]] const parser::Program *Program() const noexcept + { + return range_.start.Program(); + } + [[nodiscard]] const lexer::SourcePosition &Start() const noexcept { return GetHistoryNode()->range_.start; @@ -335,6 +347,9 @@ public: if (GetHistoryNode()->parent_ != parent) { GetOrCreateHistoryNode()->parent_ = parent; } + if (parent != nullptr && Program() == nullptr) { + SetProgram(parent->Program()); + } } [[nodiscard]] varbinder::Variable *Variable() const noexcept diff --git a/ets2panda/ir/ets/etsTypeReference.cpp b/ets2panda/ir/ets/etsTypeReference.cpp index 7986198c729d4d33902ce54239119b9df91e4c72..030191a350916f5eab3c4916ff430aa2c4a7c3ea 100644 --- a/ets2panda/ir/ets/etsTypeReference.cpp +++ b/ets2panda/ir/ets/etsTypeReference.cpp @@ -118,6 +118,7 @@ ETSTypeReference *ETSTypeReference::Clone(ArenaAllocator *const allocator, AstNo { auto *const partClone = Part() != nullptr ? Part()->Clone(allocator, nullptr)->AsETSTypeReferencePart() : nullptr; auto *const clone = allocator->New(partClone, allocator); + clone->SetRange(Range()); if (partClone != nullptr) { partClone->SetParent(clone); @@ -137,7 +138,6 @@ ETSTypeReference *ETSTypeReference::Clone(ArenaAllocator *const allocator, AstNo clone->SetAnnotations(std::move(annotationUsages)); } - clone->SetRange(Range()); return clone; } diff --git a/ets2panda/ir/ets/etsTypeReferencePart.cpp b/ets2panda/ir/ets/etsTypeReferencePart.cpp index 9dbef072a275a2e41a81acddb471b649f2ec868a..d7025a2e93490f0dad157aaf4700b65dec814ffe 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.cpp +++ b/ets2panda/ir/ets/etsTypeReferencePart.cpp @@ -251,6 +251,8 @@ ETSTypeReferencePart *ETSTypeReferencePart::Clone(ArenaAllocator *const allocato Previous() != nullptr ? Previous()->Clone(allocator, nullptr)->AsETSTypeReferencePart() : nullptr; auto *const clone = allocator->New(nameClone, typeParamsClone, prevClone, allocator); + clone->SetRange(Range()); + if (nameClone != nullptr) { nameClone->SetParent(clone); } @@ -267,7 +269,6 @@ ETSTypeReferencePart *ETSTypeReferencePart::Clone(ArenaAllocator *const allocato clone->SetParent(parent); } - clone->SetRange(Range()); return clone; } diff --git a/ets2panda/ir/ets/etsUnionType.cpp b/ets2panda/ir/ets/etsUnionType.cpp index de035bcd614e25de7767a2deb57d10ecdda0008f..2af0df9fa14ca4c800737fe32d7d44504379320f 100644 --- a/ets2panda/ir/ets/etsUnionType.cpp +++ b/ets2panda/ir/ets/etsUnionType.cpp @@ -128,11 +128,10 @@ ETSUnionType *ETSUnionType::Clone(ArenaAllocator *const allocator, AstNode *cons } clone->SetAnnotations(std::move(annotationUsages)); } + clone->SetRange(Range()); for (auto *it : clone->Types()) { it->SetParent(clone); } - - clone->SetRange(Range()); return clone; } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/expressions/binaryExpression.cpp b/ets2panda/ir/expressions/binaryExpression.cpp index 21babd5626bdef88c2f2888de6318c7058a1bfec..acd89f8b484418697cba4d177b30b3197006e6a2 100644 --- a/ets2panda/ir/expressions/binaryExpression.cpp +++ b/ets2panda/ir/expressions/binaryExpression.cpp @@ -99,6 +99,7 @@ BinaryExpression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNo auto *const left = left_ != nullptr ? left_->Clone(allocator, nullptr)->AsExpression() : nullptr; auto *const right = right_ != nullptr ? right_->Clone(allocator, nullptr)->AsExpression() : nullptr; auto *const clone = allocator->New(left, right, operator_); + clone->SetRange(Range()); if (operationType_ != nullptr) { clone->SetOperationType(operationType_); @@ -116,7 +117,6 @@ BinaryExpression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNo clone->SetParent(parent); } - clone->SetRange(Range()); return clone; } } // namespace ark::es2panda::ir diff --git a/ets2panda/lexer/token/sourceLocation.cpp b/ets2panda/lexer/token/sourceLocation.cpp index ab6475fefa3aa51472423bad75f90ea047604ba3..cb094910fcd01414e8d1cdcd24b9dffa7610aef1 100644 --- a/ets2panda/lexer/token/sourceLocation.cpp +++ b/ets2panda/lexer/token/sourceLocation.cpp @@ -149,4 +149,15 @@ const parser::Program *SourceLocation::Program() const return program_; } +void SourcePosition::SetProgram(const parser::Program *program) +{ + program_ = program; +} + +void SourceRange::SetProgram(const parser::Program *program) +{ + start.SetProgram(program); + end.SetProgram(program); +} + } // namespace ark::es2panda::lexer diff --git a/ets2panda/lexer/token/sourceLocation.h b/ets2panda/lexer/token/sourceLocation.h index b6557e4d6320509ddfce93860c717d38ec5a289e..7cb61ec17a7664a99e579f7a80bef65a12a85707 100644 --- a/ets2panda/lexer/token/sourceLocation.h +++ b/ets2panda/lexer/token/sourceLocation.h @@ -51,6 +51,7 @@ public: // NOLINTEND(misc-non-private-member-variables-in-classes) const parser::Program *Program() const; + void SetProgram(const parser::Program *program); bool operator!=(const SourcePosition &other) const { @@ -78,6 +79,8 @@ public: { return start != other.start || end != other.end; } + + void SetProgram(const parser::Program *program); }; class SourceLocation { diff --git a/ets2panda/parser/ETSparserNamespaces.cpp b/ets2panda/parser/ETSparserNamespaces.cpp index 50240599974741629a8b0ec7b6ceb6b993125256..33a5b226597338c2b598f858115e6872bf92a787 100644 --- a/ets2panda/parser/ETSparserNamespaces.cpp +++ b/ets2panda/parser/ETSparserNamespaces.cpp @@ -76,7 +76,6 @@ ir::ETSModule *ETSParser::ParseNamespaceImp(ir::ModifierFlags flags) auto start = Lexer()->GetToken().Start(); child = AllocNode(Allocator(), ArenaVector(Allocator()->Adapter()), ExpectIdentifier(), ir::ModuleFlag::NAMESPACE, globalProgram_); - child->SetParent(parent); child->SetRange({start, Lexer()->GetToken().Start()}); child->AddModifier(ir::ModifierFlags::EXPORT); if ((flags & ir::ModifierFlags::DECLARE) != 0) {