From: Peter Klausler Date: Thu, 16 Feb 2023 00:39:14 +0000 (-0800) Subject: [flang] Always incorporate parent types' special generic bindings X-Git-Tag: upstream/17.0.6~17263 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7ed26ad10cef4d8c115a30d59352981efe7765b5;p=platform%2Fupstream%2Fllvm.git [flang] Always incorporate parent types' special generic bindings The runtime type information table generator was broken when dealing with an extension derived type that didn't include a special generic procedure binding for ASSIGNMENT(=) or user-defined I/O, but one of whose ancestor types did. Ensure that the runtime derived type info tables have complete subtables for all of these special bindings, and respect any overrides that may have been defined. Motivating example: type parent contains procedure :: dtWrite => dtWrite1 generic :: write(formatted) => dtWrite end type type, extends(parent) :: extended contains procedure :: dtWrite => dtWrite2 end type The runtime derived type information table for "extended" must include a special generic procedure entry for "write(formatted)" that points to "dtWrite2" even though "extend" has no generic procedure for "write(formatted)". Differential Revision: https://reviews.llvm.org/D144148 --- diff --git a/flang/lib/Semantics/runtime-type-info.cpp b/flang/lib/Semantics/runtime-type-info.cpp index 18b701f..e5fb2d9 100644 --- a/flang/lib/Semantics/runtime-type-info.cpp +++ b/flang/lib/Semantics/runtime-type-info.cpp @@ -67,11 +67,16 @@ private: SomeExpr PackageIntValueExpr(const SomeExpr &genre, std::int64_t = 0) const; std::vector DescribeBindings( const Scope &dtScope, Scope &); - void DescribeGeneric(const GenericDetails &, - std::map &, const DerivedTypeSpec *); + std::map DescribeSpecialGenerics( + const Scope &dtScope, const Scope &thisScope, + const DerivedTypeSpec *) const; + void DescribeSpecialGeneric(const GenericDetails &, + std::map &, const Scope &, + const DerivedTypeSpec *) const; void DescribeSpecialProc(std::map &, const Symbol &specificOrBinding, bool isAssignment, bool isFinal, - std::optional, const DerivedTypeSpec *); + std::optional, const Scope *, + const DerivedTypeSpec *) const; void IncorporateDefinedIoGenericInterfaces( std::map &, GenericKind::DefinedIo, const Scope *, const DerivedTypeSpec *); @@ -498,7 +503,6 @@ const Symbol *RuntimeTableBuilder::DescribeType(Scope &dtScope) { if (!isPDTdefinitionWithKindParameters) { std::vector dataComponentSymbols; std::vector procPtrComponents; - std::map specials; for (const auto &pair : dtScope) { const Symbol &symbol{*pair.second}; auto locationRestorer{common::ScopedSet(location_, symbol.name())}; @@ -518,8 +522,7 @@ const Symbol *RuntimeTableBuilder::DescribeType(Scope &dtScope) { }, [&](const ProcBindingDetails &) { // handled in a later pass }, - [&](const GenericDetails &generic) { - DescribeGeneric(generic, specials, derivedTypeSpec); + [&](const GenericDetails &) { // ditto }, [&](const auto &) { common::die( @@ -565,11 +568,15 @@ const Symbol *RuntimeTableBuilder::DescribeType(Scope &dtScope) { evaluate::ConstantSubscripts{ static_cast(bindings.size())})); // Describe "special" bindings to defined assignments, FINAL subroutines, - // and user-defined derived type I/O subroutines. + // and user-defined derived type I/O subroutines. Defined assignments + // and I/O subroutines override any parent bindings; FINAL subroutines + // do not (the runtime will call all of them). + std::map specials{ + DescribeSpecialGenerics(dtScope, dtScope, derivedTypeSpec)}; const DerivedTypeDetails &dtDetails{dtSymbol->get()}; for (const auto &pair : dtDetails.finals()) { DescribeSpecialProc(specials, *pair.second, false /*!isAssignment*/, - true, std::nullopt, derivedTypeSpec); + true, std::nullopt, nullptr, derivedTypeSpec); } if (derivedTypeSpec) { IncorporateDefinedIoGenericInterfaces(specials, @@ -986,15 +993,33 @@ RuntimeTableBuilder::DescribeBindings(const Scope &dtScope, Scope &scope) { return result; } -void RuntimeTableBuilder::DescribeGeneric(const GenericDetails &generic, +std::map +RuntimeTableBuilder::DescribeSpecialGenerics(const Scope &dtScope, + const Scope &thisScope, const DerivedTypeSpec *derivedTypeSpec) const { + std::map specials; + if (const Scope * parentScope{dtScope.GetDerivedTypeParent()}) { + specials = + DescribeSpecialGenerics(*parentScope, thisScope, derivedTypeSpec); + } + for (auto pair : dtScope) { + const Symbol &symbol{*pair.second}; + if (const auto *generic{symbol.detailsIf()}) { + DescribeSpecialGeneric(*generic, specials, thisScope, derivedTypeSpec); + } + } + return specials; +} + +void RuntimeTableBuilder::DescribeSpecialGeneric(const GenericDetails &generic, std::map &specials, - const DerivedTypeSpec *derivedTypeSpec) { + const Scope &dtScope, const DerivedTypeSpec *derivedTypeSpec) const { common::visit(common::visitors{ [&](const GenericKind::OtherKind &k) { if (k == GenericKind::OtherKind::Assignment) { for (auto ref : generic.specificProcs()) { DescribeSpecialProc(specials, *ref, true, - false /*!final*/, std::nullopt, derivedTypeSpec); + false /*!final*/, std::nullopt, &dtScope, + derivedTypeSpec); } } }, @@ -1006,7 +1031,7 @@ void RuntimeTableBuilder::DescribeGeneric(const GenericDetails &generic, case GenericKind::DefinedIo::WriteUnformatted: for (auto ref : generic.specificProcs()) { DescribeSpecialProc(specials, *ref, false, - false /*!final*/, io, derivedTypeSpec); + false /*!final*/, io, &dtScope, derivedTypeSpec); } break; } @@ -1019,9 +1044,13 @@ void RuntimeTableBuilder::DescribeGeneric(const GenericDetails &generic, void RuntimeTableBuilder::DescribeSpecialProc( std::map &specials, const Symbol &specificOrBinding, bool isAssignment, bool isFinal, - std::optional io, - const DerivedTypeSpec *derivedTypeSpec) { + std::optional io, const Scope *dtScope, + const DerivedTypeSpec *derivedTypeSpec) const { const auto *binding{specificOrBinding.detailsIf()}; + if (binding && dtScope) { // use most recent override + binding = &DEREF(dtScope->FindComponent(specificOrBinding.name())) + .get(); + } const Symbol &specific{*(binding ? &binding->symbol() : &specificOrBinding)}; if (auto proc{evaluate::characteristics::Procedure::Characterize( specific, context_.foldingContext())}) { @@ -1123,9 +1152,10 @@ void RuntimeTableBuilder::DescribeSpecialProc( IntExpr<1>(isArgDescriptorSet)); AddValue(values, specialSchema_, "proc"s, SomeExpr{evaluate::ProcedureDesignator{specific}}); - auto pair{specials.try_emplace( - *index, DEREF(specialSchema_.AsDerived()), std::move(values))}; - CHECK(pair.second); // ensure not already present + // index might already be present in the case of an override + specials.emplace(*index, + evaluate::StructureConstructor{ + DEREF(specialSchema_.AsDerived()), std::move(values)}); } } @@ -1144,7 +1174,7 @@ void RuntimeTableBuilder::IncorporateDefinedIoGenericInterfaces( definedIo); for (auto ref : genericDetails.specificProcs()) { DescribeSpecialProc( - specials, *ref, false, false, definedIo, derivedTypeSpec); + specials, *ref, false, false, definedIo, nullptr, derivedTypeSpec); } } } diff --git a/flang/module/__fortran_type_info.f90 b/flang/module/__fortran_type_info.f90 index 554038c..b33d210 100644 --- a/flang/module/__fortran_type_info.f90 +++ b/flang/module/__fortran_type_info.f90 @@ -23,7 +23,7 @@ module __Fortran_type_info ! "TBP" bindings appear first. Inherited bindings, with overrides already ! applied, appear in the initial entries in the same order as they ! appear in the parent type's bindings, if any. They are followed - ! by new local bindings in alphabetic order of theing binding names. + ! by new local bindings in alphabetic order of their binding names. type(Binding), pointer, contiguous :: binding(:) character(len=:), pointer :: name integer(kind=int64) :: sizeInBytes diff --git a/flang/runtime/type-info.h b/flang/runtime/type-info.h index 747f034..0b8efd58 100644 --- a/flang/runtime/type-info.h +++ b/flang/runtime/type-info.h @@ -208,7 +208,7 @@ public: const DerivedType *GetParentType() const; - // Finds a data component by name in this derived type or tis ancestors. + // Finds a data component by name in this derived type or its ancestors. const Component *FindDataComponent( const char *name, std::size_t nameLen) const; diff --git a/flang/test/Semantics/typeinfo02.f90 b/flang/test/Semantics/typeinfo02.f90 new file mode 100644 index 0000000..076f1b3 --- /dev/null +++ b/flang/test/Semantics/typeinfo02.f90 @@ -0,0 +1,33 @@ +!RUN: bbc --dump-symbols %s | FileCheck %s +!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s + +module m1 + type base + contains + procedure :: wf => wf1 + generic :: write(formatted) => wf + end type + type, extends(base) :: extended + contains + procedure :: wf => wf2 + end type + contains + subroutine wf1(x,u,iot,v,iostat,iomsg) + class(base), intent(in) :: x + integer, intent(in) :: u + character(len=*), intent(in) :: iot + integer, intent(in) :: v(:) + integer, intent(out) :: iostat + character(len=*), intent(inout) :: iomsg + end subroutine + subroutine wf2(x,u,iot,v,iostat,iomsg) + class(extended), intent(in) :: x + integer, intent(in) :: u + character(len=*), intent(in) :: iot + integer, intent(in) :: v(:) + integer, intent(out) :: iostat + character(len=*), intent(inout) :: iomsg + end subroutine +end module +!CHECK: .s.base, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(specialbinding) shape: 0_8:0_8 init:[specialbinding::specialbinding(which=5_1,isargdescriptorset=1_1,proc=wf1)] +!CHECK: .s.extended, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(specialbinding) shape: 0_8:0_8 init:[specialbinding::specialbinding(which=5_1,isargdescriptorset=1_1,proc=wf2)]