www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixin()-ed code not treated the same as surrounding code

reply Roman Hargrave <roman hargrave.info> writes:
I have a mixin & generator I defined in order to mimic a 
corresponding CPP macro, DefineList(string name, string typename):

// See Torch - List.h, macro DEFINE_NEW_LIST(NAME, TYPE)
string DefineList(string name, string typename)
{
    return
       "class " ~ name ~ " : TorchObject {"
       ~ "public: "
       ~ typename ~ "** nodes;"
       ~ "int n_nodes;"
       ~ " disable this();"
       ~ "final void add(" ~ name ~ "* list);"
       ~ "final void addNode(" ~ typename ~ "* node);"
       ~ "}";
}


When used in an extern block, the compiler does not use the 
requested linkage when generating symbols for the class.

Given the following:

extern(C++, Torch)
{
    private import CXXTorch.TorchObject;
    private import CXXTorch.List;
    private import CXXTorch.general;

    class Sequence : TorchObject
    {
       // body
    };

    mixin(DefineList("SequenceList", "Sequence"));
}

I would expect the following linker symbols:

_ZN5Torch12SequenceList7addNodeEPNS_8SequenceE
_ZN5Torch12SequenceList3addEPS0_
_ZN5Torch12SequenceListC1Ev
_ZN5Torch12SequenceListC2Ev

But instead I get:

_D8Sequence5Torch12SequenceList6__initZ
_D8Sequence5Torch12SequenceList7__ClassZ
_D8Sequence5Torch12SequenceList6__vtblZ

Which dont reflect any of the classifiers, nor the linkage 
specified for SequenceList by DefineList.

Firstly, add() and addNode() are potentially virtual (even though 
marked final), and a constructor is present (even though it has 
 disable, though I'm unfamiliar with the ABI semantics of 
 disable).

Adding the linkage specifier to the return value of DefineList 
makes no difference.

The compiler does use the requested linkage for class Sequence, 
though.
Aug 13 2017
next sibling parent Roman Hargrave <roman hargrave.info> writes:
Update:

Manually defining the class yields the same results (a class with 
D linkage).

What's going on here?
Aug 13 2017
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 13 August 2017 at 19:50:57 UTC, Roman Hargrave wrote:
 But instead I get:

 _D8Sequence5Torch12SequenceList6__initZ
 _D8Sequence5Torch12SequenceList7__ClassZ
 _D8Sequence5Torch12SequenceList6__vtblZ
How are you getting that list? Those particular things are hidden D specific symbols so it might be generating those AND the ones you actually want. Of course, it is also possible that the extern(C++) just needs to be explicitly written on each class. I don't really know.
Aug 13 2017