1! RUN: %python %S/test_folding.py %s %flang_fc1
2! Test folding of LBOUND and UBOUND
3
4module m
5  real :: a3(42:52)
6  real :: empty(52:42, 2:3, 10:1)
7  integer, parameter :: lba3(*) = lbound(a3)
8  logical, parameter :: test_lba3 = all(lba3 == [42])
9  type :: t
10    real :: a
11  end type
12  type(t) :: ta(0:2)
13  character(len=2) :: ca(-1:1)
14  integer, parameter :: lbtadim = lbound(ta,1)
15  logical, parameter :: test_lbtadim = lbtadim == 0
16  integer, parameter :: ubtadim = ubound(ta,1)
17  logical, parameter :: test_ubtadim = ubtadim == 2
18  integer, parameter :: lbta1(*) = lbound(ta)
19  logical, parameter :: test_lbta1 = all(lbta1 == [0])
20  integer, parameter :: ubta1(*) = ubound(ta)
21  logical, parameter :: test_ubta1 = all(ubta1 == [2])
22  integer, parameter :: lbta2(*) = lbound(ta(:))
23  logical, parameter :: test_lbta2 = all(lbta2 == [1])
24  integer, parameter :: ubta2(*) = ubound(ta(:))
25  logical, parameter :: test_ubta2 = all(ubta2 == [3])
26  integer, parameter :: lbta3(*) = lbound(ta%a)
27  logical, parameter :: test_lbta3 = all(lbta3 == [1])
28  integer, parameter :: ubta3(*) = ubound(ta%a)
29  logical, parameter :: test_ubta3 = all(ubta3 == [3])
30  integer, parameter :: lbca1(*) = lbound(ca)
31  logical, parameter :: test_lbca1 = all(lbca1 == [-1])
32  integer, parameter :: ubca1(*) = ubound(ca)
33  logical, parameter :: test_ubca1 = all(ubca1 == [1])
34  integer, parameter :: lbca2(*) = lbound(ca(:)(1:1))
35  logical, parameter :: test_lbca2 = all(lbca2 == [1])
36  integer, parameter :: ubca2(*) = ubound(ca(:)(1:1))
37  logical, parameter :: test_ubca2 = all(ubca2 == [3])
38  integer, parameter :: lbfoo(*) = lbound(foo())
39  logical, parameter :: test_lbfoo = all(lbfoo == [1,1])
40  integer, parameter :: ubfoo(*) = ubound(foo())
41  logical, parameter :: test_ubfoo = all(ubfoo == [2,3])
42
43  integer, parameter :: lbs_empty(*) = lbound(empty)
44  logical, parameter :: test_lbs_empty = all(lbs_empty == [1, 2, 1])
45  integer, parameter :: ubs_empty(*) = ubound(empty)
46  logical, parameter :: test_ubs_empty = all(ubs_empty == [0, 3, 0])
47  logical, parameter :: test_lb_empty_dim = lbound(empty, 1) == 1
48  logical, parameter :: test_ub_empty_dim = ubound(empty, 1) == 0
49 contains
50  function foo()
51    real :: foo(2:3,4:6)
52  end function
53  subroutine test(n1,a1,a2)
54    integer, intent(in) :: n1
55    real, intent(in) :: a1(1:n1), a2(0:*)
56    integer, parameter :: lba1(*) = lbound(a1)
57    logical, parameter :: test_lba1 = all(lba1 == [1])
58    integer, parameter :: lba2(*) = lbound(a2)
59    logical, parameter :: test_lba2 = all(lba2 == [0])
60  end subroutine
61  subroutine test2
62    real :: a(2:3,4:6)
63    associate (b => a)
64      block
65        integer, parameter :: lbb(*) = lbound(b)
66        logical, parameter :: test_lbb = all(lbb == [2,4])
67        integer, parameter :: ubb(*) = ubound(b)
68        logical, parameter :: test_ubb = all(ubb == [3,6])
69      end block
70    end associate
71    associate (b => a + 0)
72      block
73        integer, parameter :: lbb(*) = lbound(b)
74        logical, parameter :: test_lbb = all(lbb == [1,1])
75        integer, parameter :: ubb(*) = ubound(b)
76        logical, parameter :: test_ubb = all(ubb == [2,3])
77      end block
78    end associate
79  end subroutine
80  subroutine test3_bound_parameter
81    ! Test [ul]bound with parameter arrays
82    integer, parameter :: a1(1) = 0
83    integer, parameter :: lba1(*) = lbound(a1)
84    logical, parameter :: test_lba1 = all(lba1 == [1])
85    integer, parameter :: uba1(*) = ubound(a1)
86    logical, parameter :: test_uba1 = all(lba1 == [1])
87
88    integer, parameter :: a2(0:1) = 0
89    integer, parameter :: lba2(*) = lbound(a2)
90    logical, parameter :: test_lba2 = all(lba2 == [0])
91    integer, parameter :: uba2(*) = ubound(a2)
92    logical, parameter :: test_uba2 = all(uba2 == [1])
93
94    integer, parameter :: a3(-10:-5,1,4:6) = 0
95    integer, parameter :: lba3(*) = lbound(a3)
96    logical, parameter :: test_lba3 = all(lba3 == [-10, 1, 4])
97    integer, parameter :: uba3(*) = ubound(a3)
98    logical, parameter :: test_uba3 = all(uba3 == [-5, 1, 6])
99
100    ! Exercise with DIM=
101    logical, parameter :: test_lba3_dim = lbound(a3, 1) == -10 .and. &
102         lbound(a3, 2) == 1 .and. &
103         lbound(a3, 3) == 4
104    logical, parameter :: test_uba3_dim = ubound(a3, 1) == -5 .and. &
105         ubound(a3, 2) == 1 .and. &
106         ubound(a3, 3) == 6
107  end subroutine
108  subroutine test4_bound_parentheses
109    ! Test [ul]bound with (x) expressions
110    integer :: a1(1) = 0
111    logical, parameter :: test_lba1 = all(lbound((a1)) == [1])
112    logical, parameter :: test_uba1 = all(ubound((a1)) == [1])
113    integer :: a2(0:2) = 0
114    logical, parameter :: test_lba2 = all(lbound((a2)) == [1])
115    logical, parameter :: test_uba2 = all(ubound((a2)) == [3])
116    integer :: a3(-1:0) = 0
117    logical, parameter :: test_lba3 = all(lbound((a3)) == [1])
118    logical, parameter :: test_uba3 = all(ubound((a3)) == [2])
119    integer :: a4(-5:-1, 2:5) = 0
120    logical, parameter :: test_lba4 = all(lbound((a4)) == [1, 1])
121    logical, parameter :: test_uba4 = all(ubound((a4)) == [5, 4])
122
123    ! Exercise with DIM=
124    logical, parameter :: test_lba4_dim = lbound((a4), 1) == 1 .and. &
125         lbound((a4), 2) == 1
126    logical, parameter :: test_uba4_dim = ubound((a4), 1) == 5 .and. &
127         ubound((a4), 2) == 4
128
129    ! Exercise with parameter types
130    integer, parameter :: pa1(1) = 0
131    logical, parameter :: test_lbpa1 = all(lbound((pa1)) == [1])
132    logical, parameter :: test_ubpa1 = all(ubound((pa1)) == [1])
133    integer, parameter :: pa2(0:2) = 0
134    logical, parameter :: test_lbpa2 = all(lbound((pa2)) == [1])
135    logical, parameter :: test_ubpa2 = all(ubound((pa2)) == [3])
136    integer, parameter :: pa3(-1:0) = 0
137    logical, parameter :: test_lbpa3 = all(lbound((pa3)) == [1])
138    logical, parameter :: test_ubpa3 = all(ubound((pa3)) == [2])
139    integer, parameter :: pa4(-5:-1, 2:5) = 0
140    logical, parameter :: test_lbpa4 = all(lbound((pa4)) == [1, 1])
141    logical, parameter :: test_ubpa4 = all(ubound((pa4)) == [5, 4])
142
143    ! Exercise with DIM=
144    logical, parameter :: test_lbpa4_dim = lbound((pa4), 1) == 1 .and. &
145         lbound((pa4), 2) == 1
146    logical, parameter :: test_ubpa4_dim = ubound((pa4), 1) == 5 .and. &
147         ubound((pa4), 2) == 4
148  end
149end
150