Method calling now properly passes pointer instead of value

This commit is contained in:
Sasha Koshka 2023-12-14 01:23:34 -05:00
parent 6fa8adf871
commit f72bf533e0
5 changed files with 42 additions and 8 deletions

View File

@ -51,7 +51,7 @@ func (this *Tree) analyzeMethod (
if err != nil { return method, err }
// add owner to method
thisVariable := &entity.Declaration {
method.This = &entity.Declaration {
Pos: method.Pos,
Name: "this",
Ty: &entity.TypePointer {
@ -63,7 +63,7 @@ func (this *Tree) analyzeMethod (
},
},
}
this.addVariable(thisVariable)
this.addVariable(method.This)
// add incomplete method to complete methods because there is enough
// information for it to be complete from the point of view of other

View File

@ -60,7 +60,7 @@ func (this *generator) generateAssignmentSource (
fromBase, source,
irFromType, name)
toBehaviorField := this.getInterfaceBehavior (
toBase, source,
toBase, destination,
irToType, name)
fromBehavior := this.blockManager.NewLoad(new(llvm.TypePointer), fromBehaviorField)
this.blockManager.NewStore(fromBehavior, toBehaviorField)

View File

@ -39,9 +39,6 @@ func (this *generator) generateCall (call *entity.Call) (llvm.Value, error) {
}
func (this *generator) generateMethodCall (call *entity.MethodCall) (llvm.Value, error) {
source, err := this.generateExpression(call.Source)
if err != nil { return nil, err }
var signature *entity.Signature
if call.Method != nil { signature = call.Method.Signature }
if call.Behavior != nil { signature = call.Behavior }
@ -61,6 +58,8 @@ func (this *generator) generateMethodCall (call *entity.MethodCall) (llvm.Value,
method, err := this.method(sourceType.Name, call.Name)
if err != nil { return nil, err }
source, err := this.generateExpressionLoc(call.Source)
if err != nil { return nil, err }
args[0] = source
return this.blockManager.NewCall(method, method.Signature, args...), nil
}
@ -71,7 +70,9 @@ func (this *generator) generateMethodCall (call *entity.MethodCall) (llvm.Value,
method, err := this.method(sourceType.Name, call.Name)
if err != nil { return nil, err }
args[0] = this.blockManager.NewLoad(source.Type(), source)
source, err := this.generateExpression(call.Source)
if err != nil { return nil, err }
args[0] = source
return this.blockManager.NewCall(method, method.Signature, args...), nil
}
}
@ -80,6 +81,8 @@ func (this *generator) generateMethodCall (call *entity.MethodCall) (llvm.Value,
if sourceType := getInterface(call.Source.Type()); sourceType != nil {
irSourceType, err := this.generateType(sourceType)
if err != nil { return nil, err }
source, err := this.generateExpression(call.Source)
if err != nil { return nil, err }
irBehavior := this.getInterfaceBehavior(sourceType, source, irSourceType, call.Name)
if irBehavior != nil {

View File

@ -74,6 +74,37 @@ define void @h() {
[h] = { }
`)
}
func TestMethod (test *testing.T) {
testString (test,
`%Number = type i64
define i64 @main() {
0:
%1 = alloca %Number
store i64 5, ptr %1
%2 = call i64 @Number.number(ptr %1)
ret i64 %2
}
define i64 @Number.number(ptr %this) {
0:
%1 = alloca ptr
store ptr %this, ptr %1
%2 = load ptr, ptr %1
%3 = load i64, ptr %2
ret i64 %3
}
`,
`
Number: Int
Number.[number]: Int = [.this]
[main]: Int = {
num:Number = 5
num.[number]
}
`)
}
// [write 1 (* 72 101 108 108 111 114 108 100 0) 7]
// [puts string:*Byte]:I32

View File

@ -11,7 +11,7 @@ Numbered: ([number]: Int)
Number: Int
Number.[number]: Int = [.this]
[main]:Index = {
[main]: Int = {
num:Number = 5
if:Numbered = num
if.[number]