Skip to content

Commit

Permalink
Merge pull request #3211 from yebblies/typetodt
Browse files Browse the repository at this point in the history
[DDMD] [refactor] Move Type::toDt into a visitor class
  • Loading branch information
AndrejMitrovic committed Feb 5, 2014
2 parents 9ae6f9d + 3f19631 commit a7e523e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
6 changes: 0 additions & 6 deletions src/mtype.h
Expand Up @@ -325,7 +325,6 @@ class Type : public RootObject
virtual Expression *defaultInitLiteral(Loc loc);
virtual Expression *voidInitLiteral(VarDeclaration *var);
virtual int isZeroInit(Loc loc = Loc()); // if initializer is 0
virtual dt_t **toDt(dt_t **pdt);
Identifier *getTypeInfoIdent(int internal);
virtual MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm = NULL);
virtual void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false);
Expand Down Expand Up @@ -463,7 +462,6 @@ class TypeVector : public Type
Expression *defaultInitLiteral(Loc loc);
TypeBasic *elementType();
int isZeroInit(Loc loc);
dt_t **toDt(dt_t **pdt);
TypeInfoDeclaration *getTypeInfoDeclaration();
TypeTuple *toArgTypes();

Expand Down Expand Up @@ -502,8 +500,6 @@ class TypeSArray : public TypeArray
Expression *defaultInit(Loc loc);
Expression *defaultInitLiteral(Loc loc);
Expression *voidInitLiteral(VarDeclaration *var);
dt_t **toDt(dt_t **pdt);
dt_t **toDtElem(dt_t **pdt, Expression *e);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm = NULL);
TypeInfoDeclaration *getTypeInfoDeclaration();
Expression *toExpression();
Expand Down Expand Up @@ -841,7 +837,6 @@ class TypeStruct : public Type
int checkBoolean();
int needsDestruction();
bool needsNested();
dt_t **toDt(dt_t **pdt);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm = NULL);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
Expand Down Expand Up @@ -933,7 +928,6 @@ class TypeTypedef : public Type
Expression *defaultInit(Loc loc);
Expression *defaultInitLiteral(Loc loc);
int isZeroInit(Loc loc);
dt_t **toDt(dt_t **pdt);
MATCH deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm = NULL);
TypeInfoDeclaration *getTypeInfoDeclaration();
int hasPointers();
Expand Down
108 changes: 65 additions & 43 deletions src/todt.c
Expand Up @@ -35,6 +35,9 @@
// Back end
#include "dt.h"

dt_t **Type_toDt(Type *t, dt_t **pdt);
dt_t **toDtElem(TypeSArray *tsa, dt_t **pdt, Expression *e);

/* ================================================================ */

dt_t *Initializer_toDt(Initializer *init)
Expand Down Expand Up @@ -443,7 +446,7 @@ dt_t **StructLiteralExp::toDt(dt_t **pdt)

Type *tb = vd->type->toBasetype();
if (tb->ty == Tsarray)
((TypeSArray *)tb)->toDtElem(pdt, e);
toDtElem(((TypeSArray *)tb), pdt, e);
else
e->toDt(pdt); // convert e to an initializer dt

Expand Down Expand Up @@ -598,13 +601,13 @@ void ClassDeclaration::toDt2(dt_t **pdt, ClassDeclaration *cd)
if (init->isVoidInitializer())
;
else if (ei && tb->ty == Tsarray)
((TypeSArray *)tb)->toDtElem(&dt, ei->exp);
toDtElem(((TypeSArray *)tb), &dt, ei->exp);
else
dt = Initializer_toDt(init);
}
else if (v->offset >= offset)
{ //printf("\t\tdefault initializer\n");
v->type->toDt(&dt);
Type_toDt(v->type, &dt);
}
if (dt)
{
Expand Down Expand Up @@ -661,32 +664,70 @@ void StructDeclaration::toDt(dt_t **pdt)

/* ================================================================= */

dt_t **Type::toDt(dt_t **pdt)
dt_t **Type_toDt(Type *t, dt_t **pdt)
{
//printf("Type::toDt()\n");
Expression *e = defaultInit();
return e->toDt(pdt);
}
class TypeToDt : public Visitor
{
public:
dt_t **pdt;

dt_t **TypeVector::toDt(dt_t **pdt)
{
assert(basetype->ty == Tsarray);
return ((TypeSArray *)basetype)->toDtElem(pdt, NULL);
}
TypeToDt(dt_t **pdt)
: pdt(pdt)
{
}

dt_t **TypeSArray::toDt(dt_t **pdt)
{
return toDtElem(pdt, NULL);
void visit(Type *t)
{
//printf("Type::toDt()\n");
Expression *e = t->defaultInit();
pdt = e->toDt(pdt);
}

void visit(TypeVector *t)
{
assert(t->basetype->ty == Tsarray);
pdt = toDtElem((TypeSArray *)t->basetype, pdt, NULL);
}

void visit(TypeSArray *t)
{
pdt = toDtElem(t, pdt, NULL);
}

void visit(TypeStruct *t)
{
t->sym->toDt(pdt);
}

void visit(TypeTypedef *t)
{
if (t->sym->init)
{
dt_t *dt = Initializer_toDt(t->sym->init);

pdt = dtend(pdt);
*pdt = dt;
}
else
{
Type_toDt(t->sym->basetype, pdt);
}
}
};

TypeToDt v(pdt);
t->accept(&v);
return v.pdt;
}

dt_t **TypeSArray::toDtElem(dt_t **pdt, Expression *e)
dt_t **toDtElem(TypeSArray *tsa, dt_t **pdt, Expression *e)
{
//printf("TypeSArray::toDtElem()\n");
size_t len = dim->toInteger();
size_t len = tsa->dim->toInteger();
if (len)
{
pdt = dtend(pdt);
Type *tnext = next;
Type *tnext = tsa->next;
Type *tbn = tnext->toBasetype();
while (tbn->ty == Tsarray && (!e || tbn != e->type->nextOf()))
{
Expand All @@ -710,7 +751,8 @@ dt_t **TypeSArray::toDtElem(dt_t **pdt, Expression *e)
for (size_t i = 1; i < len; i++)
{
if (tbn->ty == Tstruct)
{ pdt = tnext->toDt(pdt);
{
pdt = Type_toDt(tnext, pdt);
pdt = dtend(pdt);
}
else
Expand All @@ -721,26 +763,6 @@ dt_t **TypeSArray::toDtElem(dt_t **pdt, Expression *e)
return pdt;
}

dt_t **TypeStruct::toDt(dt_t **pdt)
{
sym->toDt(pdt);
return pdt;
}

dt_t **TypeTypedef::toDt(dt_t **pdt)
{
if (sym->init)
{
dt_t *dt = Initializer_toDt(sym->init);

pdt = dtend(pdt);
*pdt = dt;
return pdt;
}
sym->basetype->toDt(pdt);
return pdt;
}

/*****************************************************/
/* CTFE stuff */
/*****************************************************/
Expand Down Expand Up @@ -839,13 +861,13 @@ dt_t **ClassReferenceExp::toDt2(dt_t **pdt, ClassDeclaration *cd, Dts *dts)
if (init->isVoidInitializer())
;
else if (ei && tb->ty == Tsarray)
((TypeSArray *)tb)->toDtElem(&dt, ei->exp);
toDtElem((TypeSArray *)tb, &dt, ei->exp);
else
dt = Initializer_toDt(init);
}
else if (v->offset >= offset)
{ //printf("\t\tdefault initializer\n");
v->type->toDt(&dt);
Type_toDt(v->type, &dt);
}
if (dt)
{
Expand Down Expand Up @@ -895,7 +917,7 @@ dt_t **ClassReferenceExp::toDt2(dt_t **pdt, ClassDeclaration *cd, Dts *dts)
if (v->init)
d = Initializer_toDt(v->init);
else
vt->toDt(&d);
Type_toDt(vt, &d);
}
pdt = dtcat(pdt, d);
d = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/toobj.c
Expand Up @@ -43,6 +43,7 @@ void obj_lzext(Symbol *s1,Symbol *s2);

void TypeInfo_toDt(dt_t **pdt, TypeInfoDeclaration *d);
dt_t *Initializer_toDt(Initializer *init);
dt_t **Type_toDt(Type *t, dt_t **pdt);

/* ================================================================== */

Expand Down Expand Up @@ -948,7 +949,7 @@ void VarDeclaration::toObjFile(int multiobj)
}
else
{
type->toDt(&s->Sdt);
Type_toDt(type, &s->Sdt);
}
dt_optimize(s->Sdt);

Expand Down

0 comments on commit a7e523e

Please sign in to comment.