From d9a4f71b448cc12c3a06939424fac9b76e44b6db Mon Sep 17 00:00:00 2001 From: nikozer Date: Fri, 20 Jun 2025 20:31:55 +0300 Subject: [PATCH] history and original to one field Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICH8E8 Test: ninja es2panda-plugin-test Signed-off-by: nikozer --- ets2panda/ir/astNode.cpp | 45 +++++++++++-------- ets2panda/ir/astNode.h | 13 ++++-- ets2panda/ir/astNodeFlags.h | 1 + ets2panda/ir/base/classDefinition.cpp | 2 +- ets2panda/ir/base/classDefinition.h | 4 +- ets2panda/ir/base/classElement.h | 4 +- ets2panda/ir/base/methodDefinition.h | 4 +- ets2panda/ir/base/scriptFunction.cpp | 4 +- ets2panda/ir/ets/etsModule.h | 4 +- ets2panda/ir/ets/etsParameterExpression.cpp | 8 ++-- ets2panda/ir/ets/etsStructDeclaration.h | 4 +- ets2panda/ir/ets/etsTypeReference.h | 4 +- ets2panda/ir/ets/etsTypeReferencePart.h | 8 ++-- ets2panda/ir/module/importDeclaration.h | 4 +- ets2panda/ir/statements/classDeclaration.h | 4 +- ets2panda/ir/statements/functionDeclaration.h | 4 +- .../ir/statements/variableDeclaration.cpp | 4 +- ets2panda/ir/statements/variableDeclaration.h | 4 +- ets2panda/ir/ts/tsEnumDeclaration.h | 4 +- ets2panda/ir/ts/tsInterfaceDeclaration.h | 4 +- ets2panda/ir/ts/tsTypeParameter.h | 8 ++-- ets2panda/ir/ts/tsTypeParameterDeclaration.h | 4 +- ets2panda/test/unit/sizeof_node_test.cpp | 5 +-- 23 files changed, 82 insertions(+), 68 deletions(-) diff --git a/ets2panda/ir/astNode.cpp b/ets2panda/ir/astNode.cpp index 696841c7b6..9b4d36c5c1 100644 --- a/ets2panda/ir/astNode.cpp +++ b/ets2panda/ir/astNode.cpp @@ -154,14 +154,28 @@ std::string AstNode::DumpDecl() const void AstNode::SetOriginalNode(AstNode *originalNode) noexcept { - if (OriginalNode() != originalNode) { - GetOrCreateHistoryNode()->originalNode_ = originalNode; + if (originalNode == nullptr) { + return; + } + GetHistoryNode()->AddModifier(ModifierFlags::HAS_ORIGINAL_NODE); + GetHistoryNode()->InitHistory(); + if (GetHistoryNode()->HistoryInitialized()) { + GetHistoryNode()->historyOrOriginal_.history->Set(originalNode, compiler::GetPhaseManager()->PreviousPhaseId()); + return; } + GetHistoryNode()->historyOrOriginal_.originalNode = originalNode; } AstNode *AstNode::OriginalNode() const noexcept { - return GetHistoryNode()->originalNode_; + if ((GetHistoryNode()->Modifiers() & ModifierFlags::HAS_ORIGINAL_NODE) == 0) { + return nullptr; + } + if (HistoryInitialized()) { + return GetHistoryNode()->historyOrOriginal_.history->Get( + GetHistoryNode()->historyOrOriginal_.history->FirstCreated()); + } + return GetHistoryNode()->historyOrOriginal_.originalNode; } void AstNode::SetTransformedNode([[maybe_unused]] std::string_view const transformationName, AstNode *transformedNode) @@ -229,9 +243,8 @@ void AstNode::CopyTo(AstNode *other) const other->range_ = range_; other->flags_ = flags_; other->astNodeFlags_ = astNodeFlags_; - other->history_ = history_; + other->historyOrOriginal_ = historyOrOriginal_; other->variable_ = variable_; - other->originalNode_ = originalNode_; } AstNode *AstNode::Construct([[maybe_unused]] ArenaAllocator *allocator) @@ -249,13 +262,14 @@ bool AstNode::IsValidInCurrentPhase() const compiler::PhaseId AstNode::GetFirstCreated() const { - return history_->FirstCreated(); + ES2PANDA_ASSERT(HistoryInitialized()); + return historyOrOriginal_.history->FirstCreated(); } AstNode *AstNode::GetFromExistingHistory() const { - ES2PANDA_ASSERT(HistoryInitialized()); - auto node = history_->Get(compiler::GetPhaseManager()->CurrentPhaseId()); + ES2PANDA_ASSERT((historyOrOriginal_.history != nullptr) && g_enableContextHistory); + auto node = historyOrOriginal_.history->Get(compiler::GetPhaseManager()->CurrentPhaseId()); if (UNLIKELY(node == nullptr)) { // the callee assumes the nullptr might be returned, but // the caller asserts it is not possible, so the explicit check is inserted @@ -268,13 +282,13 @@ AstNode *AstNode::GetOrCreateHistoryNode() const { AstNode *node = nullptr; - if (HistoryInitialized()) { - node = history_->At(compiler::GetPhaseManager()->CurrentPhaseId()); + if ((historyOrOriginal_.history != nullptr) && g_enableContextHistory) { + node = historyOrOriginal_.history->At(compiler::GetPhaseManager()->CurrentPhaseId()); if (node == nullptr) { - node = history_->Get(compiler::GetPhaseManager()->PreviousPhaseId()); + node = historyOrOriginal_.history->Get(compiler::GetPhaseManager()->PreviousPhaseId()); ES2PANDA_ASSERT(node != nullptr); node = node->ShallowClone(compiler::GetPhaseManager()->Allocator()); - history_->Set(node, compiler::GetPhaseManager()->CurrentPhaseId()); + historyOrOriginal_.history->Set(node, compiler::GetPhaseManager()->CurrentPhaseId()); } } else { node = const_cast(this); @@ -303,13 +317,8 @@ void AstNode::InitHistory() return; } - history_ = compiler::GetPhaseManager()->Allocator()->New( + historyOrOriginal_.history = compiler::GetPhaseManager()->Allocator()->New( this, compiler::GetPhaseManager()->CurrentPhaseId(), compiler::GetPhaseManager()->Allocator()); } -bool AstNode::HistoryInitialized() const -{ - return history_ != nullptr; -} - } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index 3f5a8edd9a..07bf49e9cc 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -682,7 +682,7 @@ public: AstNode *GetHistoryNode() const { - if (UNLIKELY(history_ != nullptr)) { + if (UNLIKELY(historyOrOriginal_.history != nullptr) && g_enableContextHistory) { return GetFromExistingHistory(); } return const_cast(this); @@ -705,7 +705,10 @@ protected: } void InitHistory(); - bool HistoryInitialized() const; + inline bool HistoryInitialized() const noexcept + { + return (historyOrOriginal_.history != nullptr) && g_enableContextHistory; + } AstNode *GetFromExistingHistory() const; @@ -724,7 +727,10 @@ protected: friend class SizeOfNodeTest; // NOLINTBEGIN(misc-non-private-member-variables-in-classes) AstNode *parent_ {}; - AstNodeHistory *history_ {nullptr}; + union { + AstNodeHistory *history; + AstNode *originalNode; + } historyOrOriginal_ {nullptr}; lexer::CompressedSourceRange range_ {}; ModifierFlags flags_ {}; mutable AstNodeFlags astNodeFlags_ {}; @@ -736,7 +742,6 @@ private: AstNode &operator=(const AstNode &) = default; varbinder::Variable *variable_ {}; - AstNode *originalNode_ = nullptr; }; template diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index ea8783348a..987330ed89 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -72,6 +72,7 @@ enum class ModifierFlags : uint32_t { ANNOTATION_USAGE = 1U << 28U, READONLY_PARAMETER = 1U << 29U, ARRAY_SETTER = 1U << 30U, + HAS_ORIGINAL_NODE = 1U << 31U, ACCESS = PUBLIC | PROTECTED | PRIVATE | INTERNAL, ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT, ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY, diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 5fe57979b3..7247e25180 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -471,7 +471,7 @@ ClassDefinition *ClassDefinition::Construct(ArenaAllocator *allocator) { ArenaVector body {allocator->Adapter()}; return allocator->New(allocator, nullptr, std::move(body), ClassDefinitionModifiers::NONE, - ModifierFlags::NONE, Language::Id::COUNT, history_); + ModifierFlags::NONE, Language::Id::COUNT, historyOrOriginal_.history); } void ClassDefinition::CopyTo(AstNode *other) const diff --git a/ets2panda/ir/base/classDefinition.h b/ets2panda/ir/base/classDefinition.h index d1a6f415bb..8d69dd42a3 100644 --- a/ets2panda/ir/base/classDefinition.h +++ b/ets2panda/ir/base/classDefinition.h @@ -156,8 +156,8 @@ public: localPrefix_("$" + std::to_string(localIndex_)), exportedClasses_(body_.get_allocator()) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/base/classElement.h b/ets2panda/ir/base/classElement.h index 45101a888b..d87eb75e98 100644 --- a/ets2panda/ir/base/classElement.h +++ b/ets2panda/ir/base/classElement.h @@ -51,8 +51,8 @@ public: decorators_(allocator->Adapter()), isComputed_(isComputed) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index af54efb580..8ccfe4b18e 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -79,8 +79,8 @@ public: baseOverloadMethod_(nullptr), asyncPairMethod_(nullptr) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index 71fe36ba69..aefee0c7a9 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -150,8 +150,8 @@ ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&d if (auto *typeParams = irSignature_.TypeParams(); typeParams != nullptr) { typeParams->SetParent(this); } - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ets/etsModule.h b/ets2panda/ir/ets/etsModule.h index 8f2bb0ecd8..1371772a66 100644 --- a/ets2panda/ir/ets/etsModule.h +++ b/ets2panda/ir/ets/etsModule.h @@ -66,8 +66,8 @@ public: program_(program) { type_ = AstNodeType::ETS_MODULE; - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index 300573755b..60ae8178aa 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -65,8 +65,8 @@ ETSParameterExpression::ETSParameterExpression(AnnotatedExpression *const identO ES2PANDA_UNREACHABLE(); } - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } @@ -84,8 +84,8 @@ ETSParameterExpression::ETSParameterExpression(AnnotatedExpression *const identO { SetInitializer(initializer); - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ets/etsStructDeclaration.h b/ets2panda/ir/ets/etsStructDeclaration.h index 1ee72040a3..e20981ca64 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.h +++ b/ets2panda/ir/ets/etsStructDeclaration.h @@ -36,8 +36,8 @@ public: explicit ETSStructDeclaration(ClassDefinition *const def, ArenaAllocator *const allocator, AstNodeHistory *history) : ClassDeclaration(AstNodeType::STRUCT_DECLARATION, def, allocator) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ets/etsTypeReference.h b/ets2panda/ir/ets/etsTypeReference.h index a69e3679fe..987243e818 100644 --- a/ets2panda/ir/ets/etsTypeReference.h +++ b/ets2panda/ir/ets/etsTypeReference.h @@ -31,8 +31,8 @@ public: explicit ETSTypeReference(ir::ETSTypeReferencePart *part, ArenaAllocator *const allocator, AstNodeHistory *history) : TypeNode(AstNodeType::ETS_TYPE_REFERENCE, allocator), part_(part) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ets/etsTypeReferencePart.h b/ets2panda/ir/ets/etsTypeReferencePart.h index 40af6ab2b6..1fde367810 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.h +++ b/ets2panda/ir/ets/etsTypeReferencePart.h @@ -38,8 +38,8 @@ public: explicit ETSTypeReferencePart(ir::Expression *name, ArenaAllocator *const allocator, AstNodeHistory *history) : TypeNode(AstNodeType::ETS_TYPE_REFERENCE_PART, allocator), name_(name) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } @@ -50,8 +50,8 @@ public: AstNodeHistory *history) : TypeNode(AstNodeType::ETS_TYPE_REFERENCE_PART, allocator), name_(name), typeParams_(typeParams), prev_(prev) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/module/importDeclaration.h b/ets2panda/ir/module/importDeclaration.h index 2da6374989..365c7d39f9 100644 --- a/ets2panda/ir/module/importDeclaration.h +++ b/ets2panda/ir/module/importDeclaration.h @@ -47,8 +47,8 @@ public: specifiers_(std::move(specifiers)), importKinds_(importKinds) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/statements/classDeclaration.h b/ets2panda/ir/statements/classDeclaration.h index 5ea6d0e8d0..3a5480fdf5 100644 --- a/ets2panda/ir/statements/classDeclaration.h +++ b/ets2panda/ir/statements/classDeclaration.h @@ -30,8 +30,8 @@ public: explicit ClassDeclaration(ClassDefinition *def, ArenaAllocator *allocator, AstNodeHistory *history) : Statement(AstNodeType::CLASS_DECLARATION), def_(def), decorators_(allocator->Adapter()) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/statements/functionDeclaration.h b/ets2panda/ir/statements/functionDeclaration.h index 25a69766e6..cb95ab6643 100644 --- a/ets2panda/ir/statements/functionDeclaration.h +++ b/ets2panda/ir/statements/functionDeclaration.h @@ -61,8 +61,8 @@ public: func_(func), isAnonymous_(isAnonymous) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/statements/variableDeclaration.cpp b/ets2panda/ir/statements/variableDeclaration.cpp index adbe853c54..92403b608d 100644 --- a/ets2panda/ir/statements/variableDeclaration.cpp +++ b/ets2panda/ir/statements/variableDeclaration.cpp @@ -236,8 +236,8 @@ VariableDeclaration::VariableDeclaration([[maybe_unused]] Tag const tag, Variabl declarators_.back()->SetParent(this); } - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/statements/variableDeclaration.h b/ets2panda/ir/statements/variableDeclaration.h index cb3703b27d..3cd41fd38e 100644 --- a/ets2panda/ir/statements/variableDeclaration.h +++ b/ets2panda/ir/statements/variableDeclaration.h @@ -50,8 +50,8 @@ public: decorators_(allocator->Adapter()), declarators_(std::move(declarators)) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ts/tsEnumDeclaration.h b/ets2panda/ir/ts/tsEnumDeclaration.h index 87bb7c13fd..d71719558b 100644 --- a/ets2panda/ir/ts/tsEnumDeclaration.h +++ b/ets2panda/ir/ts/tsEnumDeclaration.h @@ -69,8 +69,8 @@ public: if (flags.isDeclare) { AddModifier(ModifierFlags::DECLARE); } - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.h b/ets2panda/ir/ts/tsInterfaceDeclaration.h index 64e6a30e70..b92e29abaa 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.h +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.h @@ -76,8 +76,8 @@ public: if (isStatic_) { AddModifier(ir::ModifierFlags::STATIC); } - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ts/tsTypeParameter.h b/ets2panda/ir/ts/tsTypeParameter.h index edb1249209..a9a5693f68 100644 --- a/ets2panda/ir/ts/tsTypeParameter.h +++ b/ets2panda/ir/ts/tsTypeParameter.h @@ -54,8 +54,8 @@ public: defaultType_(defaultType) { ES2PANDA_ASSERT(flags == ModifierFlags::NONE || flags == ModifierFlags::IN || flags == ModifierFlags::OUT); - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } @@ -68,8 +68,8 @@ public: constraint_(constraint), defaultType_(defaultType) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/ir/ts/tsTypeParameterDeclaration.h b/ets2panda/ir/ts/tsTypeParameterDeclaration.h index 282d0a2f2e..5feba0d1b0 100644 --- a/ets2panda/ir/ts/tsTypeParameterDeclaration.h +++ b/ets2panda/ir/ts/tsTypeParameterDeclaration.h @@ -38,8 +38,8 @@ public: params_(std::move(params)), requiredParams_(requiredParams) { - if (history != nullptr) { - history_ = history; + if (HistoryInitialized()) { + historyOrOriginal_.history = history; } else { InitHistory(); } diff --git a/ets2panda/test/unit/sizeof_node_test.cpp b/ets2panda/test/unit/sizeof_node_test.cpp index 5f150147f1..f0d3cc7b2b 100644 --- a/ets2panda/test/unit/sizeof_node_test.cpp +++ b/ets2panda/test/unit/sizeof_node_test.cpp @@ -68,9 +68,8 @@ size_t SizeOfNodeTest::SizeOf() sizeof(node->type_) + sizeof(node->flags_) + sizeof(node->astNodeFlags_) + - sizeof(node->history_) + - sizeof(node->variable_) + - sizeof(node->originalNode_)); + sizeof(node->historyOrOriginal_) + + sizeof(node->variable_)); // clang-format on } -- Gitee