Peter Klausler 1bea0347bf
[flang] Fix mod file generation of derived type initializers... (#70511)
... when the derived type used in the structure constructor(s) is from
another module and not use-associated into the current module.

This came up in a test with a derived type component default initializer
of "c_null_ptr", which is replaced with the expression
"__builtin_c_ptr(address=0_8)"; the derived type name "__builtin_c_ptr"
is not available in the current scope, and the module file would fail
semantic analysis when USE'd.

The best solution that I found was to extend module file generation to
detect this case and handle it by inserting the right USE association to
the ultimate derived type symbol, possibly with renaming to a
compiler-created name in the case of a conflict.

To implement this transformation, it was necessary to fix the utility
evaluate::CollectSymbols() to include the derived type symbol from a
structure constructor. This involved extending the expression traversal
framework to visit the derived type spec of a structure constructor.
Extending CollectSymbols() caused a lowering test to fail mysteriously,
so I tracked down the code in PFTBuilder that didn't expect to see a
DerivedTypeDetails symbol and dealt with it there.
2023-10-31 12:17:00 -07:00

31 lines
1.1 KiB
Fortran

! RUN: %python %S/test_modfile.py %s %flang_fc1
! Test derived type renaming in initializers necessary to avoid
! clashing with local names
module m
use, intrinsic :: iso_c_binding, only: &
c_ptr, c_funptr, c_null_ptr, c_null_funptr
real, private :: __builtin_c_ptr, __builtin_c_funptr
type mydt
type(c_funptr) :: component = c_null_funptr
end type
type(c_ptr), parameter :: namedConst = c_null_ptr
end
!Expect: m.mod
!module m
!use,intrinsic::__fortran_builtins,only:__fortran_builtins$__builtin_c_ptr=>__builtin_c_ptr
!use,intrinsic::__fortran_builtins,only:__fortran_builtins$__builtin_c_funptr=>__builtin_c_funptr
!use,intrinsic::iso_c_binding,only:c_ptr
!use,intrinsic::iso_c_binding,only:c_funptr
!use,intrinsic::iso_c_binding,only:c_null_ptr
!use,intrinsic::iso_c_binding,only:c_null_funptr
!private::__fortran_builtins$__builtin_c_ptr
!private::__fortran_builtins$__builtin_c_funptr
!real(4),private::__builtin_c_ptr
!real(4),private::__builtin_c_funptr
!type::mydt
!type(c_funptr)::component=__fortran_builtins$__builtin_c_funptr(__address=0_8)
!end type
!type(c_ptr),parameter::namedconst=__fortran_builtins$__builtin_c_ptr(__address=0_8)
!end