Skip to content

Commit 57c71dd

Browse files
committed
Enabled parent method calls via der obj and improved tests
1 parent 211633b commit 57c71dd

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

integration_tests/class_05.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def main():
3333
assert op2 == "Whiskers is a 3-year-old Cat."
3434
print(cat.is_domestic)
3535
assert cat.is_domestic == True
36+
3637
main()

integration_tests/class_06.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
from lpython import i32
2+
23
class Base():
34
def __init__(self:"Base"):
45
self.x : i32 = 10
56

7+
def get_x(self:"Base")->i32:
8+
print(self.x)
9+
return self.x
10+
11+
#Testing polymorphic fn calls
12+
def get_x_static(d: Base)->i32:
13+
print(d.x)
14+
return d.x
15+
616
class Derived(Base):
717
def __init__(self: "Derived"):
818
super().__init__()
9-
fn()
10-
self.y : i32 = 20
19+
self.y : i32 = 20
1120

12-
def f(i:Base) -> i32:
13-
print(i.x)
14-
return i.x
21+
def get_y(self:"Derived")->i32:
22+
print(self.y)
23+
return self.y
1524

16-
def fn():
17-
print("Inside fn")
1825

1926
def main():
2027
d : Derived = Derived()
21-
op :i32 = f(d)
22-
assert op == 10
28+
x : i32 = get_x_static(d)
29+
assert x == 10
30+
# Testing parent method call using der obj
31+
x = d.get_x()
32+
assert x == 10
33+
y: i32 = d.get_y()
34+
assert y == 20
2335

2436
main()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8146,7 +8146,8 @@ we will have to use something else.
81468146
//TODO: Correct Class and ClassType
81478147
// call to struct member function
81488148
// modifying args to pass the object as self
8149-
ASR::symbol_t* der = ASR::down_cast<ASR::StructType_t>(var->m_type)->m_derived_type;
8149+
ASR::symbol_t* der_sym = ASR::down_cast<ASR::StructType_t>(var->m_type)->m_derived_type;
8150+
ASR::Struct_t* der = ASR::down_cast<ASR::Struct_t>(der_sym);
81508151
Vec<ASR::call_arg_t> new_args; new_args.reserve(al, args.n + 1);
81518152
ASR::call_arg_t self_arg;
81528153
self_arg.loc = args[0].loc;
@@ -8155,7 +8156,19 @@ we will have to use something else.
81558156
for (size_t i=0; i<args.n; i++) {
81568157
new_args.push_back(al, args[i]);
81578158
}
8158-
st = get_struct_member(der, call_name, loc);
8159+
if ( der->m_symtab->get_symbol(call_name) ) {
8160+
st = get_struct_member(der_sym, call_name, loc);
8161+
} else if ( der->m_parent ) {
8162+
ASR::Struct_t* parent = ASR::down_cast<ASR::Struct_t>(der->m_parent);
8163+
if ( !parent->m_symtab->get_symbol(call_name) ) {
8164+
throw SemanticError("Method not found in the class or parents",loc);
8165+
} else {
8166+
st = get_struct_member(der->m_parent, call_name, loc);
8167+
}
8168+
} else {
8169+
throw SemanticError("Method not found in the class "+std::string(der->m_name)+
8170+
" or it's parents",loc);
8171+
}
81598172
tmp = make_call_helper(al, st, current_scope, new_args, call_name, loc);
81608173
return;
81618174
} else {

tests/reference/asr-structs_09-f3ffe08.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-structs_09-f3ffe08.stderr",
11-
"stderr_hash": "f59ab2d213f6423e0a891e43d5a19e83d4405391b1c7bf481b4b939e",
11+
"stderr_hash": "14119a0bc6420ad242b99395d457f2092014d96d2a1ac81d376c649d",
1212
"returncode": 2
1313
}

tests/reference/asr-structs_09-f3ffe08.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semantic error: read not present in StringIO dataclass
1+
semantic error: Method not found in the class StringIO or it's parents
22
--> tests/errors/structs_09.py:13:23
33
|
44
13 | bytes_read: i32 = fd.read()

0 commit comments

Comments
 (0)