[flang-commits] [PATCH] D144960: [flang][hlfir] Support type descriptor for initialized character component

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Feb 28 06:16:23 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGd8d91b2a25db: [flang][hlfir] Support type descriptor for initialized character component (authored by jeanPerier).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144960/new/

https://reviews.llvm.org/D144960

Files:
  flang/lib/Lower/ConvertType.cpp
  flang/test/Lower/HLFIR/tdesc-character-comp-init.f90


Index: flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
===================================================================
--- /dev/null
+++ flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
@@ -0,0 +1,13 @@
+! Test lowering of derived type descriptor for types with
+! a default initialized character component.
+
+! RUN: bbc -emit-fir -hlfir -o - %s | FileCheck %s
+
+subroutine test()
+  type t
+    character(5) :: character_comp = "hello"
+  end type
+  type(t) :: x
+end subroutine
+! CHECK-LABEL: fir.global {{.*}} @_QFtestE.c.t constant
+! CHECK: fir.address_of(@_QFtestE.di.t.character_comp) : !fir.ref<!fir.char<1,5>>
Index: flang/lib/Lower/ConvertType.cpp
===================================================================
--- flang/lib/Lower/ConvertType.cpp
+++ flang/lib/Lower/ConvertType.cpp
@@ -433,11 +433,22 @@
     // Do not use dynamic type length here. We would miss constant
     // lengths opportunities because dynamic type only has the length
     // if it comes from a declaration.
-    auto charExpr =
-        std::get<Fortran::evaluate::Expr<Fortran::evaluate::SomeCharacter>>(
-            expr.u);
-    if (auto constantLen = toInt64(charExpr.LEN()))
-      return *constantLen;
+    if (const auto *charExpr = std::get_if<
+            Fortran::evaluate::Expr<Fortran::evaluate::SomeCharacter>>(
+            &expr.u)) {
+      if (auto constantLen = toInt64(charExpr->LEN()))
+        return *constantLen;
+    } else if (auto dynamicType = expr.GetType()) {
+      // When generating derived type type descriptor as structure constructor,
+      // semantics wraps designators to data component initialization into
+      // CLASS(*), regardless of their actual type.
+      // GetType() will recover the actual symbol type as the dynamic type, so
+      // getCharacterLength may be reached even if expr is packaged as an
+      // Expr<SomeDerived> instead of an Expr<SomeChar>.
+      // Just use the dynamic type here again to retrieve the length.
+      if (auto constantLen = toInt64(dynamicType->GetCharLength()))
+        return *constantLen;
+    }
     return fir::SequenceType::getUnknownExtent();
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144960.501114.patch
Type: text/x-patch
Size: 2158 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230228/8b84e938/attachment-0001.bin>


More information about the flang-commits mailing list