Skip to content

Commit 4739c88

Browse files
authored
[flang] Make NULL() initializers explicit for allocatables in DATA co… (#69753)
…nversion As requested by people working on lowering: when semantics converts the contents of DATA statements into explicit object initializers, ensure that structure constructors for derived types contain explicit NULL() values for their allocatable components.
1 parent b2bdc45 commit 4739c88

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

flang/lib/Evaluate/initial-image.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,16 @@ class AsConstantHelper {
119119
for (std::size_t j{0}; j < elements; ++j, at += stride) {
120120
if (Result value{image_.AsConstantPointer(at)}) {
121121
typedValue[j].emplace(component, std::move(*value));
122+
} else {
123+
typedValue[j].emplace(component, Expr<SomeType>{NullPointer{}});
122124
}
123125
}
124-
} else if (!IsAllocatable(component)) {
126+
} else if (IsAllocatable(component)) {
127+
// Lowering needs an explicit NULL() for allocatables
128+
for (std::size_t j{0}; j < elements; ++j, at += stride) {
129+
typedValue[j].emplace(component, Expr<SomeType>{NullPointer{}});
130+
}
131+
} else {
125132
auto componentType{DynamicType::From(component)};
126133
CHECK(componentType.has_value());
127134
auto componentExtents{GetConstantExtents(context_, component)};

flang/test/Semantics/data19.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
22
! Test truncation/padding in DATA statement.
3-
3+
program main
44
character(len=3) :: c1, c2, c3(2), c4(2)
55
data c1(1:2), c1(3:3) /'123', '4'/
66
data c2(1:2), c2(3:3) /'1', '2'/

flang/test/Semantics/data20.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
! RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
2+
! Verify that allocatable components have explicit NULL() initializers
3+
! when converted from DATA statements
4+
!CHECK: x1 (InDataStmt) size=32 offset=0: ObjectEntity type: TYPE(t) init:t(a=NULL(),n=1_4)
5+
!CHECK: x2 (InDataStmt) size=64 offset=32: ObjectEntity type: TYPE(t) shape: 1_8:2_8 init:[t::t(a=NULL(),n=2_4),t(a=NULL(),n=3_4)]
6+
!CHECK: x3 (InDataStmt) size=64 offset=96: ObjectEntity type: TYPE(t2) init:t2(b=[t::t(a=NULL(),n=4_4),t(a=NULL(),n=5_4)])
7+
program main
8+
type t
9+
real, allocatable :: a
10+
integer n
11+
end type
12+
type t2
13+
type(t) b(2)
14+
end type
15+
type(t) x1, x2(2)
16+
type(t2) x3
17+
data x1%n/1/, x2(:)%n/2, 3/, x3%b(:)%n/4, 5/
18+
end

0 commit comments

Comments
 (0)