Incorrect function in FunctionCall of `len` expr of Character type.
Code
File - debug.f90
module string_module
implicit none
type :: string_type
sequence
private
character(len=:), allocatable :: raw
end type string_type
interface len
module procedure :: len_string
end interface
contains
elemental function len_string(string) result(length)
type(string_type), intent(in) :: string
integer :: length
if (allocated(string%raw)) then
length = len(string%raw)
else
length = 0
end if
end function len_string
pure function maybe(string) result(maybe_string)
type(string_type), intent(in) :: string
character(len=len(string)) :: maybe_string
if (allocated(string%raw)) then
maybe_string = string%raw
else
maybe_string = ''
end if
end function maybe
end module
Command
lfortran --show-asr debug.f90
ASR
Notice that in symbol table of maybe
, the intrinsic len
is being used in character type of maybe_string
variable. This is incorrect because what we need there is the generic procedure len
defined in the code. The reason behind this is that generic procedure len
is made available after maybe
in the symbol table because all generic procedures (interface
things I mean) are added to the symbol table after declaring all other symbols. Hence, the intrinsic len
gets used instead.
(TranslationUnit (SymbolTable 1 {lfortran_intrinsic_builtin: (IntrinsicModule lfortran_intrinsic_builtin), string_module: (Module (SymbolTable 2 {len: (GenericProcedure 2 len [2 len_string] Public), len_string: (Function (SymbolTable 4 {allocated: (ExternalSymbol 4 allocated 7 allocated lfortran_intrinsic_builtin [] allocated Private), length: (Variable 4 length ReturnVar () () Default (Integer 4 []) Source Private Required .false.), string: (Variable 4 string In () () Default (Derived 2 string_type []) Source Private Required .false.)}) len_string [(Var 4 string)] [(If (FunctionCall 4 allocated () [((DerivedRef (Var 4 string) 3 raw (Character 1 -2 () []) ()))] (Logical 4 []) () ()) [(= (Var 4 length) (FunctionCall 2 len_string 2 len [((DerivedRef (Var 4 string) 3 raw (Character 1 -2 () []) ()))] (Integer 4 []) () ()) ())] [(= (Var 4 length) (ConstantInteger 0 (Integer 4 [])) ())])] (Var 4 length) Source Private Implementation ()), maybe: (Function (SymbolTable 5 {allocated: (ExternalSymbol 5 allocated 7 allocated lfortran_intrinsic_builtin [] allocated Private), len: (ExternalSymbol 5 len 7 len lfortran_intrinsic_builtin [] len Private), maybe_string: (Variable 5 maybe_string ReturnVar () () Default (Character 1 -3 (FunctionCall 5 len () [((Var 5 string))] (Integer 4 []) () ()) []) Source Private Required .false.), string: (Variable 5 string In () () Default (Derived 2 string_type []) Source Private Required .false.)}) maybe [(Var 5 string)] [(If (FunctionCall 5 allocated () [((DerivedRef (Var 5 string) 3 raw (Character 1 -2 () []) ()))] (Logical 4 []) () ()) [(= (Var 5 maybe_string) (DerivedRef (Var 5 string) 3 raw (Character 1 -2 () []) ()) ())] [(= (Var 5 maybe_string) (ConstantString "" (Character 1 0 () [])) ())])] (Var 5 maybe_string) Source Private Implementation ()), string_type: (DerivedType (SymbolTable 3 {raw: (Variable 3 raw Local () () Allocatable (Character 1 -2 () []) Source Private Required .false.)}) string_type [raw] Source Private ())}) string_module [lfortran_intrinsic_builtin] .false. .false.)}) [])
Edited by Gagandeep Singh