ELENA-LANG / elena-lang

ELENA is a general-purpose language with late binding. It is multi-paradigm, combining features of functional and object-oriented programming. Rich set of tools are provided to deal with message dispatching : multi-methods, message qualifying, generic message handlers, run-time interfaces
https://elena-lang.github.io/
MIT License
236 stars 26 forks source link

Typecast the argument list to the expected ones and call it directly if there is only a single strong-typed method #523

Closed arakov closed 3 years ago

arakov commented 4 years ago

Typecast the argument list to the expected ones and call it directly if there is only a single strong-typed method. The default multi-method call is not needed if all arguments and the target are known in run-time

arakov commented 3 years ago

Use case:

import extensions;

A;

B
{
    foo(A a)
    {
        console.printLine("bar")
    }
}

C
{
    A cast() = new A();
}

public program()
{
    auto c := new C();
    auto b := new B();

    b.foo(c);
}

The program code is:

@method program.#invoke
    open       1h
    pusha      
    pushr      0
    pushr      0
    movr       class : '$private'C
    pusha      
    movm       mssgconst : "#constructor[1]"
    callrm     '$private'C#class mssgconst : "#constructor[1]"
    storefi    2
    movr       class : '$private'B
    pusha      
    movm       mssgconst : "#constructor[1]"
    callrm     '$private'B#class mssgconst : "#constructor[1]"
    storefi    3
    pushfi     2
    pushfi     3
    peeksi     0
    movm       mssgconst : "foo[2]"
    callvi     0
    peekfi     1
    close      
    quit       
@end

A resolved message should be used (instead of multi-one) with explicit typecasting

// ...
    pushfi     2
    peeksi     0
    movm       mssgconst : "#cast<'$private'A>[1]"
    callvi     0
    pusha      
    pushfi     3
    peeksi     0
    movm       mssgconst : "foo<'$private'A>[2]"
    callvi     0
// ...
arakov commented 3 years ago

The optimization can be applied only if the class is closed or sealed, or method is sealed

arakov commented 3 years ago

done