atilaneves / dpp

Directly include C headers in D source code
Boost Software License 1.0
230 stars 31 forks source link

Nested aggregates in C are now accessible outside of their parent when translated into D #226

Closed cbecerescu closed 4 years ago

cbecerescu commented 4 years ago

Solution for an issue that occured in the case below:

struct A {
    struct B {
        int a;
        int c;
    } b_obj;
    int other;
};

void f(struct B*); // works in C, in D it should be A.B
// Also applicable to field types

The f function, when translated, would have the parameter of type B instead of A.B. The solution only applies to C (as C++ has namespaces), to struct, union and enum, at any level of nested aggregates.

The solution is based on an associative array which keeps track of aggregate's spelling and their parent's spelling (as well as the line number in which they appear), so that, in the end, we can go to those lines and modify the parameter type (or field type, depending on the case).

EDIT: Another possible solution could be recording the child-parent aggregates like before, and just specifying some aliases (in the case above, alias B = A.B), instead of remembering every line where that aggregate type appeared and modify B to A.B. I didn't think that long for this solution (so I don't know if there are cases where it won't work), but if it is considered better, I can modify the current solution.