Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / Intrinsics / get_command_argument-optional.f90
blobebb82578864b23aec102719639141eb4756da395
1 ! Test GET_COMMAND_ARGUMENT with dynamically optional arguments.
2 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
4 ! CHECK-LABEL: func @_QPtest(
5 ! CHECK-SAME: %[[numberParam:.*]]: !fir.ref<i32> {fir.bindc_name = "number", fir.optional},
6 ! CHECK-SAME: %[[valueParam:.*]]: !fir.boxchar<1> {fir.bindc_name = "value", fir.optional},
7 ! CHECK-SAME: %[[lengthParam:.*]]: !fir.ref<i32> {fir.bindc_name = "length", fir.optional},
8 ! CHECK-SAME: %[[statusParam:.*]]: !fir.ref<i32> {fir.bindc_name = "status", fir.optional},
9 ! CHECK-SAME: %[[errmsgParam:.*]]: !fir.boxchar<1> {fir.bindc_name = "errmsg", fir.optional}) {
10 subroutine test(number, value, length, status, errmsg)
11 integer, optional :: number, status, length
12 character(*), optional :: value, errmsg
13 ! Note: number cannot be absent
14 call get_command_argument(number, value, length, status, errmsg)
15 ! CHECK: %[[errmsgUnboxed:.*]]:2 = fir.unboxchar %[[errmsgParam]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
16 ! CHECK: %[[valueUnboxed:.*]]:2 = fir.unboxchar %[[valueParam]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
17 ! CHECK: %[[number:.*]] = fir.load %[[numberParam]] : !fir.ref<i32>
18 ! CHECK: %[[valueIsPresent:.*]] = fir.is_present %[[valueUnboxed]]#0 : (!fir.ref<!fir.char<1,?>>) -> i1
19 ! CHECK: %[[valueReboxed:.*]] = fir.embox %[[valueUnboxed]]#0 typeparams %[[valueUnboxed]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
20 ! CHECK: %[[valueAbsent:.*]] = fir.absent !fir.box<!fir.char<1,?>>
21 ! CHECK: %[[valueOrAbsent:.*]] = arith.select %[[valueIsPresent]], %[[valueReboxed]], %[[valueAbsent]] : !fir.box<!fir.char<1,?>>
22 ! CHECK: %[[lengthIsPresent:.*]] = fir.is_present %[[lengthParam]] : (!fir.ref<i32>) -> i1
23 ! CHECK: %[[lengthBoxed:.*]] = fir.embox %[[lengthParam]] : (!fir.ref<i32>) -> !fir.box<i32>
24 ! CHECK: %[[lengthAbsent:.*]] = fir.absent !fir.box<i32>
25 ! CHECK: %[[lengthOrAbsent:.*]] = arith.select %[[lengthIsPresent]], %[[lengthBoxed]], %[[lengthAbsent]] : !fir.box<i32>
26 ! CHECK: %[[errmsgIsPresent:.*]] = fir.is_present %[[errmsgUnboxed]]#0 : (!fir.ref<!fir.char<1,?>>) -> i1
27 ! CHECK: %[[errmsgReboxed:.*]] = fir.embox %[[errmsgUnboxed]]#0 typeparams %[[errmsgUnboxed]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
28 ! CHECK: %[[errmsgAbsent:.*]] = fir.absent !fir.box<!fir.char<1,?>>
29 ! CHECK: %[[errmsgOrAbsent:.*]] = arith.select %[[errmsgIsPresent]], %[[errmsgReboxed]], %[[errmsgAbsent]] : !fir.box<!fir.char<1,?>>
30 ! CHECK: %[[sourceFileString:.*]] = fir.address_of(@_QQcl{{.*}}) : !fir.ref<!fir.char<1,[[sourceFileLength:.*]]>>
31 ! CHECK: %[[sourceLine:.*]] = arith.constant [[# @LINE - 17]] : i32
32 ! CHECK: %[[value:.*]] = fir.convert %[[valueOrAbsent]] : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
33 ! CHECK: %[[length:.*]] = fir.convert %[[lengthOrAbsent]] : (!fir.box<i32>) -> !fir.box<none>
34 ! CHECK: %[[errmsg:.*]] = fir.convert %[[errmsgOrAbsent]] : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
35 ! CHECK: %[[sourceFile:.*]] = fir.convert %[[sourceFileString]] : (!fir.ref<!fir.char<1,[[sourceFileLength]]>>) -> !fir.ref<i8>
36 ! CHECK: %[[status:.*]] = fir.call @_FortranAGetCommandArgument(%[[number]], %[[value]], %[[length]], %[[errmsg]], %[[sourceFile]], %[[sourceLine]]) {{.*}}: (i32, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> i32
37 ! CHECK: %[[statusI64:.*]] = fir.convert %[[statusParam]] : (!fir.ref<i32>) -> i64
38 ! CHECK: %[[zero:.*]] = arith.constant 0 : i64
39 ! CHECK: %[[statusIsNonNull:.*]] = arith.cmpi ne, %[[statusI64]], %[[zero]] : i64
40 ! CHECK: fir.if %[[statusIsNonNull]] {
41 ! CHECK: fir.store %[[status]] to %[[statusParam]] : !fir.ref<i32>
42 ! CHECK: }
43 end subroutine