STABS: Virtual Methods

Next: Inheritance, Previous: Method Modifiers, Up: Cplusplus [Contents][Index]

### 8.11 Virtual Methods

<< The following examples are based on a4.C >>

The presence of virtual methods in a class definition adds additional data to the class description. The extra data is appended to the description of the virtual method and to the end of the class description. Consider the class definition below:

    class A {
    public:
            int Adat;
            virtual int A_virt (int arg) { return arg; };
    };

This results in the stab below describing class A. It defines a new type (20) which is an 8 byte structure. The first field of the class struct is ‘`Adat`’, an integer, starting at structure offset 0 and occupying 32 bits.

The second field in the class struct is not explicitly defined by the C`++` class definition but is implied by the fact that the class contains a virtual method. This field is the vtable pointer. The name of the vtable pointer field starts with ‘`$vf`’ and continues with a type reference to the class it is part of. In this example the type reference for class A is 20 so the name of its vtable pointer field is ‘`$vf20`’, followed by the usual colon.

Next there is a type definition for the vtable pointer type (21). This is in turn defined as a pointer to another new type (22).

Type 22 is the vtable itself, which is defined as an array, indexed by a range of integers between 0 and 1, and whose elements are of type 17. Type 17 was the vtable record type defined by the boilerplate C`++` type definitions, as shown earlier.

The bit offset of the vtable pointer field is 32. The number of bits in the field are not specified when the field is a vtable pointer.

Next is the method definition for the virtual member function `A_virt`. Its description starts out using the same format as the non-virtual member functions described above, except instead of a dot after the ‘`A`’ there is an asterisk, indicating that the function is virtual. Since is is virtual some addition information is appended to the end of the method description.

The first number represents the vtable index of the method. This is a 32 bit unsigned number with the high bit set, followed by a semi-colon.

The second number is a type reference to the first base class in the inheritance hierarchy defining the virtual member function. In this case the class stab describes a base class so the virtual function is not overriding any other definition of the method. Therefore the reference is to the type number of the class that the stab is describing (20).

This is followed by three semi-colons. One marks the end of the current sub-section, one marks the end of the method field, and the third marks the end of the struct definition.

For classes containing virtual functions the very last section of the string part of the stab holds a type reference to the first base class. This is preceded by ‘`~%`’ and followed by a final semi-colon.

    .stabs "class_name(A):type_def(20)=sym_desc(struct)struct_bytes(8)
            field_name(Adat):type_ref(int),bit_offset(0),field_bits(32);
            field_name(A virt func ptr):type_def(21)=type_desc(ptr to)type_def(22)=
            sym_desc(array)index_type_ref(range of int from 0 to 1);
            elem_type_ref(vtbl elem type),
            bit_offset(32);
            meth_name(A_virt)::typedef(23)=sym_desc(method)returning(int);
            :arg_type(int),protection(public)normal(yes)virtual(yes)
            vtable_index(1);class_first_defining(A);;;~%first_base(A);",
            N_LSYM,NIL,NIL,NIL

    .stabs "A:t20=s8Adat:1,0,32;$vf20:21=*22=ar1;0;1;17,32;
            A_virt::23=##1;:i;2A*-2147483647;20;;;~%20;",128,0,0,0

Next: Inheritance, Previous: Method Modifiers, Up: Cplusplus [Contents][Index]