diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp --- a/flang/lib/Lower/ConvertExpr.cpp +++ b/flang/lib/Lower/ConvertExpr.cpp @@ -2466,7 +2466,21 @@ mlir::Type argTy = callSiteType.getInput(arg.firArgument); if (!actual) { // Optional dummy argument for which there is no actual argument. - caller.placeInput(arg, builder.create(loc, argTy)); + if (arg.passBy != PassBy::CharProcTuple) { + caller.placeInput(arg, builder.create(loc, argTy)); + continue; + } + + mlir::Type funcTy = argTy.cast() + .getType(0) + .cast() + .getEleTy(); + mlir::Value funcAddr = builder.create(loc, funcTy); + mlir::Value charLen = + builder.createIntegerConstant(loc, builder.getCharacterLengthType(), + fir::CharacterType::singleton()); + caller.placeInput( + arg, createBoxProcCharTuple(converter, argTy, funcAddr, charLen)); continue; } const auto *expr = actual->UnwrapExpr(); diff --git a/flang/test/Lower/dummy-argument-optional.f90 b/flang/test/Lower/dummy-argument-optional.f90 --- a/flang/test/Lower/dummy-argument-optional.f90 +++ b/flang/test/Lower/dummy-argument-optional.f90 @@ -1,4 +1,5 @@ ! RUN: bbc -emit-fir %s -o - | FileCheck %s +! RUN: flang-new -fc1 -fdefault-integer-8 -emit-fir %s -o - | FileCheck %s ! Test OPTIONAL lowering on caller/callee and PRESENT intrinsic. module opt @@ -68,6 +69,54 @@ call character_scalar() end subroutine +! Test optional character function +! CHECK-LABEL: func @_QMoptPchar_proc( +! CHECK-SAME: %[[arg0:.*]]: !fir.ref>, +character(len=3) function char_proc(i) + integer :: i + char_proc = "XYZ" +end function +! CHECK-LABEL: func @_QMoptPuse_char_proc( +! CHECK-SAME: %[[arg0:.*]]: tuple ()>, i64> {fir.char_proc}, +subroutine use_char_proc(f, c) + optional :: f + interface + character(len=3) function f(i) + integer :: i + end function + end interface + character(len=3) :: c +! CHECK: %[[boxProc:.*]] = fir.extract_value %[[arg0]], [0 : index] : (tuple ()>, i64>) -> !fir.boxproc<() -> ()> +! CHECK: %[[procAddr:.*]] = fir.box_addr %[[boxProc]] : (!fir.boxproc<() -> ()>) -> (() -> ()) +! CHECK: %{{.*}} = fir.is_present %[[procAddr]] : (() -> ()) -> i1 + if (present(f)) then + c = f(0) + else + c = "ABC" + end if +end subroutine +! CHECK-LABEL: func @_QMoptPcall_use_char_proc( +subroutine call_use_char_proc() + character(len=3) :: c +! CHECK: %[[absent:.*]] = fir.absent () -> () +! CHECK: %[[c1:.*]] = arith.constant 1 : index +! CHECK: %[[boxProc:.*]] = fir.emboxproc %[[absent]] : (() -> ()) -> !fir.boxproc<() -> ()> +! CHECK: %[[charLen:.*]] = fir.convert %[[c1]] : (index) -> i64 +! CHECK: %[[tuple:.*]] = fir.undefined tuple ()>, i64> +! CHECK: %[[tuple2:.*]] = fir.insert_value %[[tuple]], %[[boxProc]], [0 : index] : (tuple ()>, i64>, !fir.boxproc<() -> ()>) -> tuple ()>, i64> +! CHECK: %[[tuple3:.*]] = fir.insert_value %[[tuple2]], %[[charLen]], [1 : index] : (tuple ()>, i64>, i64) -> tuple ()>, i64> +! CHECK: fir.call @_QMoptPuse_char_proc(%[[tuple3]], %{{.*}}){{.*}} : (tuple ()>, i64>, !fir.boxchar<1>) -> () + call use_char_proc(c=c) +! CHECK: %[[funcAddr:.*]] = fir.address_of(@_QMoptPchar_proc) : (!fir.ref>, index, {{.*}}) -> !fir.boxchar<1> +! CHECK: %[[c3:.*]] = arith.constant 3 : i64 +! CHECK: %[[boxProc2:.*]] = fir.emboxproc %[[funcAddr]] : ((!fir.ref>, index, {{.*}}) -> !fir.boxchar<1>) -> !fir.boxproc<() -> ()> +! CHECK: %[[tuple4:.*]] = fir.undefined tuple ()>, i64> +! CHECK: %[[tuple5:.*]] = fir.insert_value %[[tuple4]], %[[boxProc2]], [0 : index] : (tuple ()>, i64>, !fir.boxproc<() -> ()>) -> tuple ()>, i64> +! CHECK: %[[tuple6:.*]] = fir.insert_value %[[tuple5]], %[[c3]], [1 : index] : (tuple ()>, i64>, i64) -> tuple ()>, i64> +! CHECK: fir.call @_QMoptPuse_char_proc(%[[tuple6]], {{.*}}){{.*}} : (tuple ()>, i64>, !fir.boxchar<1>) -> () + call use_char_proc(char_proc, c) +end subroutine + ! Test optional assumed shape ! CHECK-LABEL: func @_QMoptPassumed_shape( ! CHECK-SAME: %[[arg0:.*]]: !fir.box> {fir.bindc_name = "x", fir.optional}) {