Showing with 122 additions and 129 deletions.
  1. +59 −61 src/cppmangle.d
  2. +7 −6 src/dmangle.d
  3. +56 −58 src/mtype.d
  4. +0 −4 src/mtype.h
120 changes: 59 additions & 61 deletions src/cppmangle.d
Original file line number Diff line number Diff line change
Expand Up @@ -465,43 +465,42 @@ static if (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TAR
}
}

static int paramsCppMangleDg(void* ctx, size_t n, Parameter fparam)
void argsCppMangle(Parameters* parameters, int varargs)
{
CppMangleVisitor mangler = cast(CppMangleVisitor)ctx;
Type t = fparam.type.merge2();
if (fparam.storageClass & (STCout | STCref))
t = t.referenceTo();
else if (fparam.storageClass & STClazy)
{
// Mangle as delegate
Type td = new TypeFunction(null, t, 0, LINKd);
td = new TypeDelegate(td);
t = t.merge();
}
if (t.ty == Tsarray)
{
// Mangle static arrays as pointers
t.error(Loc(), "Internal Compiler Error: unable to pass static array to extern(C++) function.");
t.error(Loc(), "Use pointer instead.");
assert(0);
//t = t->nextOf()->pointerTo();
int paramsCppMangleDg(size_t n, Parameter fparam)
{
Type t = fparam.type.merge2();
if (fparam.storageClass & (STCout | STCref))
t = t.referenceTo();
else if (fparam.storageClass & STClazy)
{
// Mangle as delegate
Type td = new TypeFunction(null, t, 0, LINKd);
td = new TypeDelegate(td);
t = t.merge();
}
if (t.ty == Tsarray)
{
// Mangle static arrays as pointers
t.error(Loc(), "Internal Compiler Error: unable to pass static array to extern(C++) function.");
t.error(Loc(), "Use pointer instead.");
assert(0);
//t = t->nextOf()->pointerTo();
}
/* If it is a basic, enum or struct type,
* then don't mark it const
*/
this.is_top_level = true;
if ((t.ty == Tenum || t.ty == Tstruct || t.ty == Tpointer || t.isTypeBasic()) && t.isConst())
t.mutableOf().accept(this);
else
t.accept(this);
this.is_top_level = false;
return 0;
}
/* If it is a basic, enum or struct type,
* then don't mark it const
*/
mangler.is_top_level = true;
if ((t.ty == Tenum || t.ty == Tstruct || t.ty == Tpointer || t.isTypeBasic()) && t.isConst())
t.mutableOf().accept(mangler);
else
t.accept(mangler);
mangler.is_top_level = false;
return 0;
}

void argsCppMangle(Parameters* parameters, int varargs)
{
if (parameters)
Parameter._foreach(parameters, &paramsCppMangleDg, cast(void*)this);
Parameter._foreach(parameters, &paramsCppMangleDg);
if (varargs)
buf.writestring("z");
else if (!parameters || !parameters.dim)
Expand Down Expand Up @@ -1794,33 +1793,6 @@ else static if (TARGET_WINDOS)
cur.accept(this);
}

static int mangleParameterDg(void* ctx, size_t n, Parameter p)
{
VisualCPPMangler mangler = cast(VisualCPPMangler)ctx;
Type t = p.type;
if (p.storageClass & (STCout | STCref))
{
t = t.referenceTo();
}
else if (p.storageClass & STClazy)
{
// Mangle as delegate
Type td = new TypeFunction(null, t, 0, LINKd);
td = new TypeDelegate(td);
t = t.merge();
}
if (t.ty == Tsarray)
{
t.error(Loc(), "Internal Compiler Error: unable to pass static array to extern(C++) function.");
t.error(Loc(), "Use pointer instead.");
assert(0);
}
mangler.flags &= ~IS_NOT_TOP_TYPE;
mangler.flags &= ~IGNORE_CONST;
t.accept(mangler);
return 0;
}

const(char)* mangleFunctionType(TypeFunction type, bool needthis = false, bool noreturn = false)
{
scope VisualCPPMangler tmp = new VisualCPPMangler(this);
Expand Down Expand Up @@ -1886,7 +1858,33 @@ else static if (TARGET_WINDOS)
}
else
{
Parameter._foreach(type.parameters, &mangleParameterDg, cast(void*)tmp);
int mangleParameterDg(size_t n, Parameter p)
{
Type t = p.type;
if (p.storageClass & (STCout | STCref))
{
t = t.referenceTo();
}
else if (p.storageClass & STClazy)
{
// Mangle as delegate
Type td = new TypeFunction(null, t, 0, LINKd);
td = new TypeDelegate(td);
t = t.merge();
}
if (t.ty == Tsarray)
{
t.error(Loc(), "Internal Compiler Error: unable to pass static array to extern(C++) function.");
t.error(Loc(), "Use pointer instead.");
assert(0);
}
tmp.flags &= ~IS_NOT_TOP_TYPE;
tmp.flags &= ~IGNORE_CONST;
t.accept(tmp);
return 0;
}

Parameter._foreach(type.parameters, &mangleParameterDg);
if (type.varargs == 1)
{
tmp.buf.writeByte('Z');
Expand Down
13 changes: 7 additions & 6 deletions src/dmangle.d
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,14 @@ public:
void paramsToDecoBuffer(Parameters* parameters)
{
//printf("Parameter::paramsToDecoBuffer()\n");
Parameter._foreach(parameters, &paramsToDecoBufferDg, cast(void*)this);
}

static int paramsToDecoBufferDg(void* ctx, size_t n, Parameter p)
{
p.accept(cast(Visitor)ctx);
return 0;
int paramsToDecoBufferDg(size_t n, Parameter p)
{
p.accept(this);
return 0;
}

Parameter._foreach(parameters, &paramsToDecoBufferDg);
}

override void visit(Parameter p)
Expand Down
114 changes: 56 additions & 58 deletions src/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -345,50 +345,6 @@ extern (C++) Expression semanticLength(Scope* sc, Type t, Expression exp)
return exp;
}

/****************************************
* Determine if parameter list is really a template parameter list
* (i.e. it has auto or alias parameters)
*/
extern (C++) static int isTPLDg(void* ctx, size_t n, Parameter p)
{
if (p.storageClass & (STCalias | STCauto | STCstatic))
return 1;
return 0;
}

/***************************************
* Determine number of arguments, folding in tuples.
*/
extern (C++) static int dimDg(void* ctx, size_t n, Parameter)
{
++*cast(size_t*)ctx;
return 0;
}

/***************************************
* Get nth Parameter, folding in tuples.
* Returns:
* Parameter* nth Parameter
* NULL not found, *pn gets incremented by the number
* of Parameters
*/
struct GetNthParamCtx
{
size_t nth;
Parameter param;
}

extern (C++) static int getNthParamDg(void* ctx, size_t n, Parameter p)
{
GetNthParamCtx* c = cast(GetNthParamCtx*)ctx;
if (n == c.nth)
{
c.param = p;
return 1;
}
return 0;
}

enum ENUMTY : int
{
Tarray, // slice array, aka T[]
Expand Down Expand Up @@ -9503,27 +9459,69 @@ public:
return params;
}

extern (D):

/****************************************
* Determine if parameter list is really a template parameter list
* (i.e. it has auto or alias parameters)
*/
static int isTPL(Parameters* parameters)
{
//printf("Parameter::isTPL()\n");
return _foreach(parameters, &isTPLDg, null);

int isTPLDg(size_t n, Parameter p)
{
if (p.storageClass & (STCalias | STCauto | STCstatic))
return 1;
return 0;
}

return _foreach(parameters, &isTPLDg);
}

static size_t dim(Parameters* parameters)
/***************************************
* Determine number of arguments, folding in tuples.
*/
extern (C++) static size_t dim(Parameters* parameters)
{
size_t n = 0;
_foreach(parameters, &dimDg, &n);
return n;
size_t nargs = 0;

int dimDg(size_t n, Parameter p)
{
++nargs;
return 0;
}

_foreach(parameters, &dimDg);
return nargs;
}

static Parameter getNth(Parameters* parameters, size_t nth, size_t* pn = null)
/***************************************
* Get nth Parameter, folding in tuples.
* Returns:
* Parameter* nth Parameter
* NULL not found, *pn gets incremented by the number
* of Parameters
*/
extern (C++) static Parameter getNth(Parameters* parameters, size_t nth, size_t* pn = null)
{
GetNthParamCtx ctx = GetNthParamCtx(nth, null);
int res = _foreach(parameters, &getNthParamDg, &ctx);
return res ? ctx.param : null;
Parameter param;

int getNthParamDg(size_t n, Parameter p)
{
if (n == nth)
{
param = p;
return 1;
}
return 0;
}

int res = _foreach(parameters, &getNthParamDg);
return res ? param : null;
}

extern (C++) alias ForeachDg = int function(void* ctx, size_t paramidx, Parameter param);
alias ForeachDg = int delegate(size_t paramidx, Parameter param);

/***************************************
* Expands tuples in args in depth first order. Calls
Expand All @@ -9532,24 +9530,24 @@ public:
* Use this function to avoid the O(N + N^2/2) complexity of
* calculating dim and calling N times getNth.
*/
static int _foreach(Parameters* parameters, ForeachDg dg, void* ctx, size_t* pn = null)
static int _foreach(Parameters* parameters, scope ForeachDg dg, size_t* pn = null)
{
assert(dg);
if (!parameters)
return 0;
size_t n = pn ? *pn : 0; // take over index
int result = 0;
for (size_t i = 0; i < parameters.dim; i++)
foreach (i; 0 .. parameters.dim)
{
Parameter p = (*parameters)[i];
Type t = p.type.toBasetype();
if (t.ty == Ttuple)
{
TypeTuple tu = cast(TypeTuple)t;
result = _foreach(tu.arguments, dg, ctx, &n);
result = _foreach(tu.arguments, dg, &n);
}
else
result = dg(ctx, n++, p);
result = dg(n++, p);
if (result)
break;
}
Expand Down
4 changes: 0 additions & 4 deletions src/mtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,8 @@ class Parameter : public RootObject
virtual void accept(Visitor *v) { v->visit(this); }

static Parameters *arraySyntaxCopy(Parameters *parameters);
static int isTPL(Parameters *parameters);
static size_t dim(Parameters *parameters);
static Parameter *getNth(Parameters *parameters, d_size_t nth, d_size_t *pn = NULL);

typedef int (*ForeachDg)(void *ctx, size_t paramidx, Parameter *param);
static int foreach(Parameters *parameters, ForeachDg dg, void *ctx, size_t *pn=NULL);
};

bool arrayTypeCompatible(Loc loc, Type *t1, Type *t2);
Expand Down