xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.fortran/function-calls.f90 (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos! Copyright 2019-2023 Free Software Foundation, Inc.
27d62b00eSchristos!
37d62b00eSchristos! This program is free software; you can redistribute it and/or modify
47d62b00eSchristos! it under the terms of the GNU General Public License as published by
57d62b00eSchristos! the Free Software Foundation; either version 3 of the License, or
67d62b00eSchristos! (at your option) any later version.
77d62b00eSchristos!
87d62b00eSchristos! This program is distributed in the hope that it will be useful,
97d62b00eSchristos! but WITHOUT ANY WARRANTY; without even the implied warranty of
107d62b00eSchristos! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
117d62b00eSchristos! GNU General Public License for more details.
127d62b00eSchristos!
137d62b00eSchristos! You should have received a copy of the GNU General Public License
147d62b00eSchristos! along with this program.  If not, see <http://www.gnu.org/licenses/> .
157d62b00eSchristos
167d62b00eSchristos! Source code for function-calls.exp.
177d62b00eSchristos
187d62b00eSchristossubroutine no_arg_subroutine()
197d62b00eSchristosend subroutine
207d62b00eSchristos
217d62b00eSchristoslogical function no_arg()
227d62b00eSchristos    no_arg = .TRUE.
237d62b00eSchristosend function
247d62b00eSchristos
257d62b00eSchristossubroutine run(a)
267d62b00eSchristos    external :: a
277d62b00eSchristos    call a()
287d62b00eSchristosend subroutine
297d62b00eSchristos
307d62b00eSchristoslogical function one_arg(x)
317d62b00eSchristos    logical, intent(in) :: x
327d62b00eSchristos    one_arg = x
337d62b00eSchristosend function
347d62b00eSchristos
357d62b00eSchristosinteger(kind=4) function one_arg_value(x)
367d62b00eSchristos    integer(kind=4), value :: x
377d62b00eSchristos    one_arg_value = x
387d62b00eSchristosend function
397d62b00eSchristos
407d62b00eSchristosinteger(kind=4) function several_arguments(a, b, c)
417d62b00eSchristos    integer(kind=4), intent(in) :: a
427d62b00eSchristos    integer(kind=4), intent(in) :: b
437d62b00eSchristos    integer(kind=4), intent(in) :: c
447d62b00eSchristos    several_arguments = a + b + c
457d62b00eSchristosend function
467d62b00eSchristos
477d62b00eSchristosinteger(kind=4) function mix_of_scalar_arguments(a, b, c)
487d62b00eSchristos    integer(kind=4), intent(in) :: a
497d62b00eSchristos    logical(kind=4), intent(in) :: b
507d62b00eSchristos    real(kind=8), intent(in) :: c
517d62b00eSchristos    mix_of_scalar_arguments = a + floor(c)
527d62b00eSchristos    if (b) then
537d62b00eSchristos        mix_of_scalar_arguments=mix_of_scalar_arguments+1
547d62b00eSchristos    end if
557d62b00eSchristosend function
567d62b00eSchristos
577d62b00eSchristosreal(kind=4) function real4_argument(a)
587d62b00eSchristos    real(kind=4), intent(in) :: a
597d62b00eSchristos    real4_argument = a
607d62b00eSchristosend function
617d62b00eSchristos
627d62b00eSchristosinteger(kind=4) function return_constant()
637d62b00eSchristos    return_constant = 17
647d62b00eSchristosend function
657d62b00eSchristos
667d62b00eSchristoscharacter(40) function return_string()
677d62b00eSchristos    return_string='returned in hidden first argument'
687d62b00eSchristosend function
697d62b00eSchristos
707d62b00eSchristosrecursive function fibonacci(n) result(item)
717d62b00eSchristos    integer(kind=4) :: item
727d62b00eSchristos    integer(kind=4), intent(in) :: n
737d62b00eSchristos    select case (n)
747d62b00eSchristos        case (0:1)
757d62b00eSchristos            item = n
767d62b00eSchristos        case default
777d62b00eSchristos            item = fibonacci(n-1) + fibonacci(n-2)
787d62b00eSchristos    end select
797d62b00eSchristosend function
807d62b00eSchristos
817d62b00eSchristoscomplex function complex_argument(a)
827d62b00eSchristos    complex, intent(in) :: a
837d62b00eSchristos    complex_argument = a
847d62b00eSchristosend function
857d62b00eSchristos
867d62b00eSchristosinteger(kind=4) function array_function(a)
877d62b00eSchristos    integer(kind=4), dimension(11) :: a
887d62b00eSchristos    array_function = a(ubound(a, 1, 4))
897d62b00eSchristosend function
907d62b00eSchristos
917d62b00eSchristosinteger(kind=4) function pointer_function(int_pointer)
927d62b00eSchristos    integer, pointer :: int_pointer
937d62b00eSchristos    pointer_function = int_pointer
947d62b00eSchristosend function
957d62b00eSchristos
967d62b00eSchristosinteger(kind=4) function hidden_string_length(string)
977d62b00eSchristos  character*(*) :: string
987d62b00eSchristos  hidden_string_length = len(string)
997d62b00eSchristosend function
1007d62b00eSchristos
1017d62b00eSchristosinteger(kind=4) function sum_some(a, b, c)
1027d62b00eSchristos    integer :: a, b
1037d62b00eSchristos    integer, optional :: c
1047d62b00eSchristos    sum_some = a + b
1057d62b00eSchristos    if (present(c)) then
1067d62b00eSchristos        sum_some = sum_some + c
1077d62b00eSchristos    end if
1087d62b00eSchristosend function
1097d62b00eSchristos
1107d62b00eSchristosmodule derived_types_and_module_calls
1117d62b00eSchristos    type cart
1127d62b00eSchristos        integer :: x
1137d62b00eSchristos        integer :: y
1147d62b00eSchristos    end type
1157d62b00eSchristos    type cart_nd
1167d62b00eSchristos        integer :: x
1177d62b00eSchristos        integer, allocatable :: d(:)
1187d62b00eSchristos    end type
1197d62b00eSchristos    type nested_cart_3d
1207d62b00eSchristos        type(cart) :: d
1217d62b00eSchristos        integer :: z
1227d62b00eSchristos    end type
1237d62b00eSchristoscontains
1247d62b00eSchristos    type(cart) function pass_cart(c)
1257d62b00eSchristos        type(cart) :: c
1267d62b00eSchristos        pass_cart = c
1277d62b00eSchristos    end function
1287d62b00eSchristos    integer(kind=4) function pass_cart_nd(c)
1297d62b00eSchristos        type(cart_nd) :: c
1307d62b00eSchristos        pass_cart_nd = ubound(c%d,1,4)
1317d62b00eSchristos    end function
1327d62b00eSchristos    type(nested_cart_3d) function pass_nested_cart(c)
1337d62b00eSchristos        type(nested_cart_3d) :: c
1347d62b00eSchristos        pass_nested_cart = c
1357d62b00eSchristos    end function
1367d62b00eSchristos    type(cart) function build_cart(x,y)
1377d62b00eSchristos        integer :: x, y
1387d62b00eSchristos        build_cart%x = x
1397d62b00eSchristos        build_cart%y = y
1407d62b00eSchristos    end function
1417d62b00eSchristosend module
1427d62b00eSchristos
1437d62b00eSchristosprogram function_calls
1447d62b00eSchristos    use derived_types_and_module_calls
1457d62b00eSchristos    implicit none
1467d62b00eSchristos    interface
1477d62b00eSchristos        logical function no_arg()
1487d62b00eSchristos        end function
1497d62b00eSchristos        logical function one_arg(x)
1507d62b00eSchristos            logical, intent(in) :: x
1517d62b00eSchristos        end function
1527d62b00eSchristos        integer(kind=4) function pointer_function(int_pointer)
1537d62b00eSchristos            integer, pointer :: int_pointer
1547d62b00eSchristos        end function
1557d62b00eSchristos        integer(kind=4) function several_arguments(a, b, c)
1567d62b00eSchristos            integer(kind=4), intent(in) :: a
1577d62b00eSchristos            integer(kind=4), intent(in) :: b
1587d62b00eSchristos            integer(kind=4), intent(in) :: c
1597d62b00eSchristos        end function
1607d62b00eSchristos        complex function complex_argument(a)
1617d62b00eSchristos            complex, intent(in) :: a
1627d62b00eSchristos        end function
1637d62b00eSchristos            real(kind=4) function real4_argument(a)
1647d62b00eSchristos            real(kind=4), intent(in) :: a
1657d62b00eSchristos        end function
1667d62b00eSchristos        integer(kind=4) function return_constant()
1677d62b00eSchristos        end function
1687d62b00eSchristos        character(40) function return_string()
1697d62b00eSchristos        end function
1707d62b00eSchristos        integer(kind=4) function one_arg_value(x)
1717d62b00eSchristos            integer(kind=4), value :: x
1727d62b00eSchristos        end function
1737d62b00eSchristos        integer(kind=4) function sum_some(a, b, c)
1747d62b00eSchristos            integer :: a, b
1757d62b00eSchristos            integer, optional :: c
1767d62b00eSchristos        end function
1777d62b00eSchristos        integer(kind=4) function mix_of_scalar_arguments(a, b, c)
1787d62b00eSchristos            integer(kind=4), intent(in) :: a
1797d62b00eSchristos            logical(kind=4), intent(in) :: b
1807d62b00eSchristos            real(kind=8), intent(in) :: c
1817d62b00eSchristos        end function
1827d62b00eSchristos        integer(kind=4) function array_function(a)
1837d62b00eSchristos            integer(kind=4), dimension(11) :: a
1847d62b00eSchristos        end function
1857d62b00eSchristos        integer(kind=4) function hidden_string_length(string)
1867d62b00eSchristos            character*(*) :: string
1877d62b00eSchristos        end function
1887d62b00eSchristos    end interface
1897d62b00eSchristos    logical :: untrue, no_arg_return
1907d62b00eSchristos    complex :: fft, fft_result
1917d62b00eSchristos    integer(kind=4), dimension (11) :: integer_array
1927d62b00eSchristos    real(kind=8) :: real8
1937d62b00eSchristos    real(kind=4) :: real4
1947d62b00eSchristos    integer, pointer :: int_pointer
1957d62b00eSchristos    integer, target :: pointee, several_arguments_return
1967d62b00eSchristos    integer(kind=4) :: integer_return
1977d62b00eSchristos    type(cart) :: c, cout
1987d62b00eSchristos    type(cart_nd) :: c_nd
1997d62b00eSchristos    type(nested_cart_3d) :: nested_c
2007d62b00eSchristos    character(40) :: returned_string, returned_string_debugger
201*6881a400Schristos    external no_arg_subroutine
2027d62b00eSchristos    real8 = 3.00
2037d62b00eSchristos    real4 = 9.3
2047d62b00eSchristos    integer_array = 17
2057d62b00eSchristos    fft = cmplx(2.1, 3.3)
2067d62b00eSchristos    print *, fft
2077d62b00eSchristos    untrue = .FALSE.
2087d62b00eSchristos    int_pointer => pointee
2097d62b00eSchristos    pointee = 87
2107d62b00eSchristos    c%x = 2
2117d62b00eSchristos    c%y = 4
2127d62b00eSchristos    c_nd%x = 4
2137d62b00eSchristos    allocate(c_nd%d(4))
2147d62b00eSchristos    c_nd%d = 6
2157d62b00eSchristos    nested_c%z = 3
2167d62b00eSchristos    nested_c%d%x = 1
2177d62b00eSchristos    nested_c%d%y = 2
2187d62b00eSchristos    ! Use everything so it is not elided by the compiler.
2197d62b00eSchristos    call no_arg_subroutine()
2207d62b00eSchristos    no_arg_return = no_arg() .AND. one_arg(.FALSE.)
2217d62b00eSchristos    several_arguments_return = several_arguments(1,2,3) + return_constant()
2227d62b00eSchristos    integer_return = array_function(integer_array)
2237d62b00eSchristos    integer_return = mix_of_scalar_arguments(2, untrue, real8)
2247d62b00eSchristos    real4 = real4_argument(3.4)
2257d62b00eSchristos    integer_return = pointer_function(int_pointer)
2267d62b00eSchristos    c = pass_cart(c)
2277d62b00eSchristos    integer_return = pass_cart_nd(c_nd)
2287d62b00eSchristos    nested_c = pass_nested_cart(nested_c)
2297d62b00eSchristos    integer_return = hidden_string_length('string of implicit length')
2307d62b00eSchristos    call run(no_arg_subroutine)
2317d62b00eSchristos    integer_return = one_arg_value(10)
2327d62b00eSchristos    integer_return = sum_some(1,2,3)
2337d62b00eSchristos    returned_string = return_string()
2347d62b00eSchristos    cout = build_cart(4,5)
2357d62b00eSchristos    fft_result = complex_argument(fft)
2367d62b00eSchristos    print *, cout
2377d62b00eSchristos    print *, several_arguments_return
2387d62b00eSchristos    print *, fft_result
2397d62b00eSchristos    print *, real4
2407d62b00eSchristos    print *, integer_return
2417d62b00eSchristos    print *, returned_string_debugger
2427d62b00eSchristos    deallocate(c_nd%d) ! post_init
2437d62b00eSchristosend program
244