Dynamically define a virtual method with GCC plugin api

153 views Asked by At

I'm having a hard time creating a GCC (8.3.0) plugin.

I'm trying to define a virtual method for a user-defined class using some informations provided by [[c++11::attributes]]. So far what I've been trying to do - before jumping into more complex stuff - is defining a virtual function that given the following definitions and code:

class Base
{
    virtual void toJson() = 0;
}

class [[ns::object]] UserClass : public Base
{
    [[ns::field]]
    int a;

    [[ns::field("beta")]]
    int b;

    int c;

    [[ns::field]]
    int d;
}

int main()
{
    Base * obj = new UserClass;
    obj->toJson();

    return 0;
}

writes a fake json to the stdout like this:

{
    "a": "value",
    "beta": "value",
    "d": "value"
}

I'm capable of registering the attributes and collecting info about the user defined class and its fields, but I have no idea how to inject the virtual method declaration in the type nor how to define its body. I searched in the GCC plugin documentation (which is extremly poor) and online but I had no luck.

EDIT

Ok, so I managed to define a method for the given class using build_method_type_directly and add_method, but I don't know how to define the body of the method. Any help?

0

There are 0 answers