lfortran icon indicating copy to clipboard operation
lfortran copied to clipboard

Idea-refactor : implicit-interface with implicit-procedure variables

Open assem2002 opened this issue 5 months ago • 6 comments

General Idea : interface in procedure() :: foo_var is kinda of smartly deducing the required interface (to make the backend lowering easier and not erroneous), while it shouldn't be that strict -- See example below.


I can see that for variable like procedure( ) , intent(in) :: x -- We create an ASR::Function node that's an INTERFACE and we set variable->m_declaration_sym = interface_func_SYMBOL and variable->m_type = type_of_interface_func. Those are 2 different things. While visiting body we start mutating that on the go. and sometimes they get mismatched.


I think Variables whose type is implicit procedure procedure( ) should act a bit different.

  • That variable should be identified as implicit-unknown type void* -- to be set in variable->m_type .
  • On call spots use ASR::Cast to cast that variable into the correct functionType (proper argument/returntype specified)
  • At compileTime keep an interface that shapes the assumed compile-time-function-signature, so we can raise compile-time errors -- to be set in variable->m_declaration_sym

That way we freely invoke calls to the variable (even when interface isn't deduced) + we don't link the 2 variable members, they point to 2 unrelated things and the mutation of the interface (while we visit the body) doesn't hurt anyone, because noone will depend on it.

assem2002 avatar Jun 19 '26 16:06 assem2002

example see this

program p
    call upper_caller(foo, 1)
    call upper_caller(boo, 2)
contains

    subroutine upper_caller(f, b)
        procedure() :: f
        integer :: b
        real :: a

        if(a == 1) then
          a = f()
        else
          a = f(2.0)
        end if
        print *, a
    end subroutine

    function foo() result(r)
      real :: r
      r  = 1010
      print *, "hello"
    end function

    function boo(m) result(r)
      real :: r
      real :: m
      r  = m
      print *, "hello"
    end function
end program

it runs with gfortran -- I'm not sure that's a correct fortran code or not, but still it runs properly and no interface conflicts (it feels like calls to the variable functions are not bound to an interface).

Here we should either raise a compile-time error OR make it run, unfortunately we don't do both now

assem2002 avatar Jun 19 '26 16:06 assem2002

related #11874

assem2002 avatar Jun 19 '26 17:06 assem2002

Thanks @assem2002 for this idea, working on it.

srinjoy933 avatar Jun 20 '26 07:06 srinjoy933

Thanks @assem2002 for this idea, working on it.

Hi @srinjoy933 sorry but we are already trying to implementthis in pr #11806.

Jatinagarwal24 avatar Jun 20 '26 07:06 Jatinagarwal24

No problem @Jatinagarwal24 ,I will work on another issue.

srinjoy933 avatar Jun 20 '26 07:06 srinjoy933

I think the backend shouldn't know about implicit interfaces at all, everything has to be explicit in ASR.

So one option is to allow procedure() only with implicit interfaces enabled (as we do now). And if enabled, represent it as some kind of a pointer in ASR.

Then at every place that assigns to it, cast it. So in the example above, this line:

        procedure() :: f

will declare f as just a pointer (some kind to be figured out). And then assigning to it:

          a = f(2.0)

We know the interface from how the f is called, so we cast the f pointer into an actual interface, and call it (in LLVM). So the LLVM backend wouldn't need any special handling of this case.

The key is to do the casting in ASR. That will clean it up.

certik avatar Jul 23 '26 15:07 certik