Skip to content

Commit

Permalink
Merge pull request #4657 from yebblies/TypeidExp
Browse files Browse the repository at this point in the history
[refactor] Create typeid() expressions instead of invoking getTypeInfo directly
  • Loading branch information
9rnsr committed May 17, 2015
2 parents 191fee6 + e2995f5 commit 895eb9c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 47 deletions.
10 changes: 4 additions & 6 deletions src/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "template.h"
#include "tokens.h"

Expression *getTypeInfo(Type *t, Scope *sc);

/*******************************************
* Merge function attributes pure, nothrow, @safe, @nogc, and @disable
*/
Expand Down Expand Up @@ -828,11 +826,11 @@ FuncDeclaration *buildPostBlit(StructDeclaration *sd, Scope *sc)
}
else
{
// Typeinfo.postblit(cast(void*)&this.v);
// typeid(typeof(v)).postblit(cast(void*)&this.v);
Expression *ea = new AddrExp(loc, ex);
ea = new CastExp(loc, ea, Type::tvoid->pointerTo());

Expression *et = getTypeInfo(v->type, sc);
Expression *et = new TypeidExp(loc, v->type);
et = new DotIdExp(loc, et, Identifier::idPool("postblit"));
ex = new CallExp(loc, et, ea);
}
Expand All @@ -857,7 +855,7 @@ FuncDeclaration *buildPostBlit(StructDeclaration *sd, Scope *sc)
Expression *ea = new AddrExp(loc, ex);
ea = new CastExp(loc, ea, Type::tvoid->pointerTo());

Expression *et = getTypeInfo(v->type, sc);
Expression *et = new TypeidExp(loc, v->type);
et = new DotIdExp(loc, et, Id::destroy);
ex = new CallExp(loc, et, ea);
}
Expand Down Expand Up @@ -969,7 +967,7 @@ FuncDeclaration *buildDtor(AggregateDeclaration *ad, Scope *sc)
Expression *ea = new AddrExp(loc, ex);
ea = new CastExp(loc, ea, Type::tvoid->pointerTo());

Expression *et = getTypeInfo(v->type, sc);
Expression *et = new TypeidExp(loc, v->type);
et = new DotIdExp(loc, et, Id::destroy);
ex = new CallExp(loc, et, ea);
}
Expand Down
4 changes: 1 addition & 3 deletions src/declaration.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "ctfe.h"
#include "target.h"

Expression *getTypeInfo(Type *t, Scope *sc);

/************************************
* Check to see the aggregate type is nested and its context pointer is
* accessible from the current scope.
Expand Down Expand Up @@ -2048,7 +2046,7 @@ Expression *VarDeclaration::callScopeDtor(Scope *sc)
Expressions *args = new Expressions();
args->push(ea);

Expression *et = getTypeInfo(type, sc);
Expression *et = new TypeidExp(loc, type);
et = new DotIdExp(loc, et, Id::destroy);

e = new CallExp(loc, et, args);
Expand Down
44 changes: 12 additions & 32 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

bool typeMerge(Scope *sc, TOK op, Type **pt, Expression **pe1, Expression **pe2);
bool isArrayOpValid(Expression *e);
Expression *createTypeInfoArray(Scope *sc, Expression *args[], size_t dim);
Expression *expandVar(int result, VarDeclaration *v);
TypeTuple *toArgTypes(Type *t);
bool checkFrameAccess(Loc loc, Scope *sc, AggregateDeclaration *ad, size_t istart = 0);
Expand Down Expand Up @@ -1912,8 +1911,18 @@ bool functionParameters(Loc loc, Scope *sc, TypeFunction *tf,
if (tf->linkage == LINKd && tf->varargs == 1)
{
assert(arguments->dim >= nparams);
Expression *e = createTypeInfoArray(sc, (Expression **)&arguments->tdata()[nparams],
arguments->dim - nparams);

Parameters *args = new Parameters;
args->setDim(arguments->dim - nparams);
for (size_t i = 0; i < arguments->dim - nparams; i++)
{
Parameter *arg = new Parameter(STCin, (*arguments)[nparams + i]->type, NULL, NULL);
(*args)[i] = arg;
}

TypeTuple *tup = new TypeTuple(args);
Expression *e = new TypeidExp(loc, tup);
e = e->semantic(sc);
arguments->insert(0, e);
}

Expand Down Expand Up @@ -14141,32 +14150,3 @@ Expression *BinExp::reorderSettingAAElem(Scope *sc)
//printf("-de = %s, be = %s\n", de->toChars(), be->toChars());
return Expression::combine(de, be);
}

/***************************************
* Create a static array of TypeInfo references
* corresponding to an array of Expression's.
* Used to supply hidden _arguments[] value for variadic D functions.
*/

Expression *createTypeInfoArray(Scope *sc, Expression *exps[], size_t dim)
{
/*
* Pass a reference to the TypeInfo_Tuple corresponding to the types of the
* arguments. Source compatibility is maintained by computing _arguments[]
* at the start of the called function by offseting into the TypeInfo_Tuple
* reference.
*/
Parameters *args = new Parameters;
args->setDim(dim);
for (size_t i = 0; i < dim; i++)
{
Parameter *arg = new Parameter(STCin, exps[i]->type, NULL, NULL);
(*args)[i] = arg;
}
TypeTuple *tup = new TypeTuple(args);
Expression *e = getTypeInfo(tup, sc);
e = e->optimize(WANTvalue);
assert(e->op == TOKsymoff); // should be SymOffExp

return e;
}
5 changes: 1 addition & 4 deletions src/magicport.json
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@
[
"aggregate",
"arraytypes",
"backend",
"declaration",
"dscope",
"dstruct",
Expand Down Expand Up @@ -1506,7 +1505,6 @@
"aggregate",
"arraytypes",
"attrib",
"backend",
"core.stdc.stdio",
"cppmangle",
"ctfeexpr",
Expand Down Expand Up @@ -2845,8 +2843,7 @@
"struct LineInitExp",
"struct ModuleInitExp",
"struct FuncInitExp",
"struct PrettyFuncInitExp",
"function createTypeInfoArray"
"struct PrettyFuncInitExp"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/mtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ int Tsize_t = Tuns32;
int Tptrdiff_t = Tint32;

Symbol *toInitializer(AggregateDeclaration *ad);
Expression *getTypeInfo(Type *t, Scope *sc);

/***************************** Type *****************************/

Expand Down Expand Up @@ -3787,7 +3786,9 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f
arguments = new Expressions();
arguments->push(e);
// don't convert to dynamic array
arguments->push(getTypeInfo(n, sc));
Expression *tid = new TypeidExp(e->loc, n);
tid = tid->semantic(sc);
arguments->push(tid);
e = new CallExp(e->loc, ec, arguments);
e->type = next->arrayOf();
}
Expand Down

0 comments on commit 895eb9c

Please sign in to comment.