From a94083b511eea13826bde466ffee2c4fcbcec3ea Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Fri, 10 Mar 2023 11:24:23 -0800 Subject: [PATCH] [flang] Break shared library cyclic dependence I inadvertently introduced a cycle of dependences between the Evaluate and Semantics libraries; this patch breaks that cycle and reenables the flang build bots that rely on shared library builds. --- flang/lib/Evaluate/type.cpp | 69 +++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/flang/lib/Evaluate/type.cpp b/flang/lib/Evaluate/type.cpp index acedf59328c2..4cc1cb173f22 100644 --- a/flang/lib/Evaluate/type.cpp +++ b/flang/lib/Evaluate/type.cpp @@ -262,6 +262,71 @@ static bool AreSameComponent(const semantics::Symbol &x, y.has(); } +// TODO: These utilities were cloned out of Semantics to avoid a cyclic +// dependency and should be repackaged into then "namespace semantics" +// part of Evaluate/tools.cpp. + +static const semantics::Symbol *GetParentComponent( + const semantics::DerivedTypeDetails &details, + const semantics::Scope &scope) { + if (auto extends{details.GetParentComponentName()}) { + if (auto iter{scope.find(*extends)}; iter != scope.cend()) { + if (const Symbol & symbol{*iter->second}; + symbol.test(semantics::Symbol::Flag::ParentComp)) { + return &symbol; + } + } + } + return nullptr; +} + +static const semantics::Symbol *GetParentComponent( + const semantics::Symbol *symbol, const semantics::Scope &scope) { + if (symbol) { + if (const auto *dtDetails{ + symbol->detailsIf()}) { + return GetParentComponent(*dtDetails, scope); + } + } + return nullptr; +} + +static const semantics::DerivedTypeSpec *GetParentTypeSpec( + const semantics::Symbol *symbol, const semantics::Scope &scope) { + if (const Symbol * parentComponent{GetParentComponent(symbol, scope)}) { + return &parentComponent->get() + .type() + ->derivedTypeSpec(); + } else { + return nullptr; + } +} + +static const semantics::Scope *GetDerivedTypeParent( + const semantics::Scope *scope) { + if (scope) { + CHECK(scope->IsDerivedType()); + if (const auto *parent{GetParentTypeSpec(scope->GetSymbol(), *scope)}) { + return parent->scope(); + } + } + return nullptr; +} + +static const semantics::Symbol *FindComponent( + const semantics::Scope *scope, parser::CharBlock name) { + if (!scope) { + return nullptr; + } + CHECK(scope->IsDerivedType()); + auto found{scope->find(name)}; + if (found != scope->end()) { + return &*found->second; + } else { + return FindComponent(GetDerivedTypeParent(scope), name); + } +} + static bool AreTypeParamCompatible(const semantics::DerivedTypeSpec &x, const semantics::DerivedTypeSpec &y, bool ignoreLenParameters) { const auto *xScope{x.typeSymbol().scope()}; @@ -271,8 +336,8 @@ static bool AreTypeParamCompatible(const semantics::DerivedTypeSpec &x, if (!yValue) { return false; } - const auto *xParm{xScope ? xScope->FindComponent(paramName) : nullptr}; - const auto *yParm{yScope ? yScope->FindComponent(paramName) : nullptr}; + const auto *xParm{FindComponent(xScope, paramName)}; + const auto *yParm{FindComponent(yScope, paramName)}; if (xParm && yParm) { const auto *xTPD{xParm->detailsIf()}; const auto *yTPD{yParm->detailsIf()};