Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterised init and methods #2761

Closed
wants to merge 5 commits into from
Closed

Conversation

tanay-man
Copy link
Contributor

@tanay-man tanay-man commented Jul 7, 2024

Enabled generation of parameterised init and methods in the ASR. Adding self after symtab visit in each function symtabs so as to have the correct type of the class.

TODO:

  • Enable function code gen in the ASR->LLVM
  • Call init instead of the StructConstructor
  • Modify the method calls to replace self with the calling object
    @certik @Thirumalai-Shaktivel

@Thirumalai-Shaktivel
Copy link
Collaborator

We need to translate to the following:

! Fortran code after applying the pass: implied_do_loops
module __main__
implicit none
type :: coord
    integer(4) :: x
    integer(4) :: y
end type coord

contains

subroutine __init__(self, x, y)
class(coord), intent(inout) :: self
integer, intent(in) :: x, y
self%x = x
self%y = y
end subroutine

subroutine __main__global_stmts()
    type(coord) :: p1
    ! p1 =     coord(6, 4)
    call __init__(p1, 6, 4)
    p1%x = 10
    print *, p1%x
    print *, p1%y
end subroutine __main__global_stmts

end module __main__

program main_program
use __main__, only: __main__global_stmts
implicit none
call __main__global_stmts()
end program main_program

@Thirumalai-Shaktivel
Copy link
Collaborator

Passing the argument:

class test:
	def __init__(self: "coord"):
		self.x = 3
		self.y = 4

t: test = test()
t.x = 10
print(t.x)
print(t.y)

Fortran

! Fortran code after applying the pass: implied_do_loops
module __main__
implicit none
type :: coord
    integer(4) :: x
    integer(4) :: y
end type coord

contains

subroutine __init__(self, x, y)
class(coord), intent(inout) :: self
integer, intent(in) :: x, y
self%x = x
self%y = y
end subroutine

subroutine __main__global_stmts()
    type(coord) :: p1
    ! p1 = coord(3, 4)
    call p1%__init__(3, 4)
    p1%x = 10
    print *, p1%x
    print *, p1%y
end subroutine __main__global_stmts

end module __main__

program main_program
use __main__, only: __main__global_stmts
implicit none
call __main__global_stmts()
end program main_program

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as draft July 9, 2024 18:38
@Thirumalai-Shaktivel
Copy link
Collaborator

Simple fortran example, check the ASR:

module mod
type :: t
contains
    procedure :: f
end type

contains

subroutine f(self)
class(t), intent(inout):: self
end subroutine

end module

program expr2
use mod
implicit none

type(t) :: t_
call t_%f()

end program

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Jul 9, 2024

  • We will create an empty Class and then visit the data members and init function.
  • Handle the body of the init function in the SymbolTable visitor itself.
  • Self will be ClassType for now (Modify later)

How to handle:

from lpython import i32,i64,f32

class coord:
    # ClassType(coord)
    def __init__(self: "coord"):
        self.x: i32 = 3
        self.y: i32 = 4

p1: coord = coord()

# p1: coord
# ExternalSymbol p1_init => __init__
# p1_init(p1)

p1.x = 10
print(p1.x)
print(p1.y)

@tanay-man tanay-man closed this Jul 19, 2024
@tanay-man tanay-man deleted the w/o-self branch July 19, 2024 10:21
@tanay-man tanay-man restored the w/o-self branch July 19, 2024 10:21
@tanay-man tanay-man deleted the w/o-self branch July 24, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants