From 2d46264ca6d850ff34d1b1d18d0814e236316ec8 Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Fri, 30 Jun 2023 20:24:46 +0200 Subject: [PATCH] [flang] add nested DEC STRUCTURE in DerivedTypeDetails component names Currently, when a (legacy) DEC structure contained other DEC structure declarations, the related component names were not added to the containing DerivedTypeDetails component_names. This lead to bugs in later phase when visiting the components (like in when lowering the type to FIR/MLIR). When an EntityDecl is visited and the scope is a DEC structure, add the entity to the component names of this DEC structure. Differential Revision: https://reviews.llvm.org/D154216 --- flang/lib/Lower/ConvertExprToHLFIR.cpp | 1 + flang/lib/Semantics/resolve-names.cpp | 4 ++++ flang/test/Semantics/struct02.f90 | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 flang/test/Semantics/struct02.f90 diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp index eb48df41104b79..29e85231a61d5e 100644 --- a/flang/lib/Lower/ConvertExprToHLFIR.cpp +++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -719,6 +719,7 @@ class HlfirDesignatorBuilder { if (recordType.isDependentType()) TODO(getLoc(), "Designate derived type with length parameters in HLFIR"); mlir::Type fieldType = recordType.getType(partInfo.componentName); + assert(fieldType && "component name is not known"); mlir::Type fieldBaseType = hlfir::getFortranElementOrSequenceType(fieldType); partInfo.componentShape = genComponentShape(componentSym, fieldBaseType); diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp index a3fba9b9ff23fc..f21aba59c7c505 100644 --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -4354,6 +4354,10 @@ void DeclarationVisitor::Post(const parser::EntityDecl &x) { } else if (attrs.test(Attr::PARAMETER)) { // C882, C883 Say(name, "Missing initialization for parameter '%s'"_err_en_US); } + if (auto *scopeSymbol{currScope().symbol()}) + if (auto *details{scopeSymbol->detailsIf()}) + if (details->isDECStructure()) + details->add_component(symbol); } void DeclarationVisitor::Post(const parser::PointerDecl &x) { diff --git a/flang/test/Semantics/struct02.f90 b/flang/test/Semantics/struct02.f90 new file mode 100644 index 00000000000000..84441423e61bba --- /dev/null +++ b/flang/test/Semantics/struct02.f90 @@ -0,0 +1,21 @@ +! Test component name resolution with nested legacy DEC structures. +!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s + + structure /a/ + integer :: a_first_comp + structure /b/ b1, b2(100) + integer :: i + end structure + structure /c/ z + integer :: i + structure /d/ d1, d2(10) + real :: x + end structure + end structure + integer :: a_last_comp + end structure +end +! CHECK: /a/: DerivedType sequence components: a_first_comp,b1,b2,z,a_last_comp +! CHECK: /b/: DerivedType sequence components: i +! CHECK: /c/: DerivedType sequence components: i,d1,d2 +! CHECK: /d/: DerivedType sequence components: x