Search code examples
vb.netil

Why does the behavior of overloaded member functions differ from the behavior of non-member functions


I notice that parameters passed in to global functions always use "call", but overloaded member function calls always use "callvirt".

Why does it always call the overloaded function pertaining to the base class and not the derived class when that class is passed into a global function (Print())?

It seems like it chooses which overloaded function to run of Print at compile time, is there something in particular I did to make it decide that this shouldn't be resolved at run time?

Here's some code that demonstrates what I mean:

Module Module1

    Class BaseClass
        Friend Overridable Sub Print()
            Console.WriteLine("BaseClass.Print")
        End Sub
    End Class

    Class DerivedClass
        Inherits BaseClass

        Friend Overrides Sub Print()
            Console.WriteLine("DerivedClass.Print")
        End Sub
    End Class

    Sub Print(iObject As Object)
        Console.WriteLine("Object")
    End Sub

    Sub Print(iClass1 As BaseClass)
        Console.WriteLine("BaseClass")
    End Sub

    Sub Print(iClass2 As DerivedClass)
        Console.WriteLine("DerivedClass")
    End Sub

    Sub Main()
        Dim tBaseClass As New BaseClass
        Dim tDerivedClass As New DerivedClass
        Dim tBaseClassRef As BaseClass
        Dim tObjPtr As Object

        Console.WriteLine()
        Console.WriteLine("Test 1")
        Console.WriteLine()

        'in IL it always uses callvirt for an overloaded member function

        'from MSDN:     The callvirt instruction calls a late-bound method on an object. 
        '               That is, the method is chosen based on the runtime type of obj rather than the compile-time class visible in the method pointer.

        'prints "BaseClass.print"
        'callvirt   instance void Overloading.Module1/BaseClass::Print()
        tBaseClass.Print()

        'in IL it uses "call", even though Print() is overloaded? Why is this?

        'We slip by here because this type is not late bound

        'prints "BaseClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)
        Print(tBaseClass)




        Console.WriteLine()
        Console.WriteLine("Test 2")
        Console.WriteLine()

        'prints "DerivedClass.print"
        'callvirt   instance void Overloading.Module1/BaseClass::Print()
        tDerivedClass.Print()

        'call works out okay here too because we're still not late bound

        'prints "DerivedClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)
        Print(tDerivedClass)




        Console.WriteLine()
        Console.WriteLine("Test 3")
        Console.WriteLine()

        tBaseClassRef = tBaseClass



        tBaseClassRef.Print()
        'prints "BaseClass.print"
        'callvirt   instance void Overloading.Module1/DerivedClass::Print()

        'call took our word for it that tBaseClassRef is BaseClass typed
        'which is correct

        Print(tBaseClassRef)
        'prints "BaseClass"
        'call       void Overloading.Module1::Print(class Overloading.Module1/DerivedClass)



        Console.WriteLine()
        Console.WriteLine("Test 4")
        Console.WriteLine()


        tBaseClassRef = tDerivedClass



        tBaseClassRef.Print()
        'prints "DerivedClass.print"
        'IL_0098:  callvirt   instance void Overloading.Module1/BaseClass::Print()

        'Callvirt correctly handles our tBaseClass having a derived class's type


        Print(tBaseClassRef)
        'prints "BaseClass" <!>

        'IL_009f:  call       void Overloading.Module1::Print(class Overloading.Module1/BaseClass)

        '"Call" is ill-equipped to handle our Derived class

        Console.WriteLine()
        Console.WriteLine("Test 5")
        Console.WriteLine()

        tObjPtr = tDerivedClass

        tObjPtr.Print() 
            '(I don't expect this to work, but the error message surprised me)
            '   -- unhandled exception -- "Public member 'Print' on type 'DerivedClass' <??> not found.

        'IL instructions: http://en.wikipedia.org/wiki/List_of_CIL_instructions

        'a. where is it getting DerivedClass from for the exception? 
        'b. Why did LateCall know what type it was, but was unable to find the method?

        '  [ILDASM]
        '  IL_00be:  ldloc.3    //Load local [3] object tObjPtr)
        '  IL_00bf:  ldnull     // push NULL to stack (no string)
        '  IL_00c0:  ldstr      "Print" // push string object for literal string -- is this what we're using as our object?
        '  IL_00c5:  ldc.i4.0   //Push False
        '  IL_00c6:  newarr     [mscorlib]System.Object
        '  IL_00cb:  ldnull     // no string array
        '  IL_00cc:  ldnull     // no class array
        '  IL_00cd:  ldnull     // no bool array
        '  IL_00ce:  ldc.i4.1   //Push True
        '  IL_00cf:  call       object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.NewLateBinding::LateCall(object,
        '                                                                                                                    class [mscorlib]System.Type,
        '                                                                                                                    string,
        '                                                                                                                    object[],
        '                                                                                                                    string[],
        '                                                                                                                    class [mscorlib]System.Type[],
        '                                                                                                                    bool[],
        '                                                                                                                    bool)

        Console.WriteLine("Marker") 'divider so I can figure out what it is in idasm

        Print(tObjPtr)                                                                                                           
        ' prints "object"

        'again, why use "call" instead of "callvirt"? There is a more specific definition of Print that can handle this
        '[ILDASM]
        '   IL_00e0:  ldloc.3
        '   IL_00e1:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
        '   IL_00e6:  call       void Overloading.Module1::Print(object)

    End Sub

End Module

EDIT: Also, please share your sources, I'd like to read more about how the IL works. This is using .NET 4.0 if it matters.


Solution

  • The callvirt instruction calls a late-bound method on an object.

    That means : on the instance objet that method "belongs" ; in the case of a Module method (a Shared Method if it was a class) there is no instance involved -> no need for callvirt.

    There is 2 notion at play here, overloading and overriding (which is a specific form of overloading) ; callvirt is really needed only for the latter.

    As for how are chosen Print overload you can see related documentation