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

Polymorphic function args dont work #2799

Closed
tanay-man opened this issue Aug 10, 2024 · 3 comments
Closed

Polymorphic function args dont work #2799

tanay-man opened this issue Aug 10, 2024 · 3 comments

Comments

@tanay-man
Copy link
Contributor

tanay-man commented Aug 10, 2024

from lpython import i32

class Base:
    def __init__(self:"Base"):
        self.x_A : i32 = 10

class Derived(Base):
    pass

def check(x: Base):
    print(x.x_A)

def main():
    d : Derived = Derived()
    check(d)
main()

Op:

(base) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_05.py 
semantic error: Type mismatch in procedure call; the types must be compatible
  --> integration_tests/class_05.py:21:11
   |
21 |     check(d)
   |           ^ type mismatch (passed argument type is struct Derived but required type is struct Base)
   |
15 | def check(x: Base):
   |              ^^^^ type mismatch (passed argument type is struct Derived but required type is struct Base)
@certik
Copy link
Contributor

certik commented Aug 11, 2024

I think the subclass has to be automatically cast to the base class in this case.

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Aug 13, 2024

Let's represent the Cast node in the ASR

def main():
    d : Derived = Derived()
    check(d)

will become:

def main():
    d : Derived = Derived()
    check(Cast(d, DerivedToBase, Base, nullptr))

Using the Base name, get the Base type in the asr to llvm backend.

@tanay-man
Copy link
Contributor Author

tanay-man commented Aug 15, 2024

@Thirumalai-Shaktivel I have implemented the ASR level cast node for this. How to get the cast working in the llvm backend?
Current op-

(lp) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_06.py 
code generation error: Cast kind not implemented

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

No branches or pull requests

3 participants