Skip to content

Commit

Permalink
Refactor TemplateMixin symbol resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Feb 17, 2013
1 parent 90a8f97 commit 2f9b8c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 82 deletions.
28 changes: 17 additions & 11 deletions src/parse.c
Expand Up @@ -2057,9 +2057,8 @@ Dsymbol *Parser::parseMixin()
{
TemplateMixin *tm;
Identifier *id;
Type *tqual;
TypeQualified *tqual;
Objects *tiargs;
Identifiers *idents;

//printf("parseMixin()\n");
nextToken();
Expand All @@ -2085,7 +2084,6 @@ Dsymbol *Parser::parseMixin()
nextToken();
}

idents = new Identifiers();
while (1)
{
tiargs = NULL;
Expand All @@ -2098,16 +2096,25 @@ Dsymbol *Parser::parseMixin()
tiargs = parseTemplateArgument();
}

if (token.value != TOKdot)
break;

if (tiargs)
if (tiargs && token.value == TOKdot)
{ TemplateInstance *tempinst = new TemplateInstance(loc, id);
tempinst->tiargs = tiargs;
id = (Identifier *)tempinst;
if (!tqual)
tqual = new TypeInstance(loc, tempinst);
else
tqual->addIdent((Identifier *)tempinst);
tiargs = NULL;
}
idents->push(id);
else
{
if (!tqual)
tqual = new TypeIdentifier(loc, id);
else
tqual->addIdent(id);
}

if (token.value != TOKdot)
break;

nextToken();
if (token.value != TOKidentifier)
Expand All @@ -2117,7 +2124,6 @@ Dsymbol *Parser::parseMixin()
id = token.ident;
nextToken();
}
idents->push(id);

if (token.value == TOKidentifier)
{
Expand All @@ -2127,7 +2133,7 @@ Dsymbol *Parser::parseMixin()
else
id = NULL;

tm = new TemplateMixin(loc, id, tqual, idents, tiargs);
tm = new TemplateMixin(loc, id, tqual, tiargs);
if (token.value != TOKsemicolon)
error("';' expected after mixin");
nextToken();
Expand Down
80 changes: 12 additions & 68 deletions src/template.c
Expand Up @@ -6319,38 +6319,20 @@ char *TemplateInstance::toChars()

/* ======================== TemplateMixin ================================ */

TemplateMixin::TemplateMixin(Loc loc, Identifier *ident, Type *tqual,
Identifiers *idents, Objects *tiargs)
: TemplateInstance(loc, (*idents)[idents->dim - 1])
TemplateMixin::TemplateMixin(Loc loc, Identifier *ident, TypeQualified *tqual, Objects *tiargs)
: TemplateInstance(loc, tqual->idents.dim ? tqual->idents[tqual->idents.dim - 1]
: ((TypeIdentifier *)tqual)->ident)
{
//printf("TemplateMixin(ident = '%s')\n", ident ? ident->toChars() : "");
this->ident = ident;
this->tqual = tqual;
this->idents = idents;
this->tiargs = tiargs ? tiargs : new Objects();
}

Dsymbol *TemplateMixin::syntaxCopy(Dsymbol *s)
{ TemplateMixin *tm;

Identifiers *ids = new Identifiers();
ids->setDim(idents->dim);
for (size_t i = 0; i < idents->dim; i++)
{ // Matches TypeQualified::syntaxCopyHelper()
Identifier *id = (*idents)[i];
if (id->dyncast() == DYNCAST_DSYMBOL)
{
TemplateInstance *ti = (TemplateInstance *)id;

ti = (TemplateInstance *)ti->syntaxCopy(NULL);
id = (Identifier *)ti;
}
(*ids)[i] = id;
}

tm = new TemplateMixin(loc, ident,
(Type *)(tqual ? tqual->syntaxCopy() : NULL),
ids, tiargs);
{
TemplateMixin *tm = new TemplateMixin(loc, ident,
(TypeQualified *)tqual->syntaxCopy(), tiargs);
TemplateInstance::syntaxCopy(tm);
return tm;
}
Expand Down Expand Up @@ -6397,43 +6379,11 @@ void TemplateMixin::semantic(Scope *sc)

// Follow qualifications to find the TemplateDeclaration
if (!tempdecl)
{ Dsymbol *s;
size_t i;
Identifier *id;

if (tqual)
{ s = tqual->toDsymbol(sc);
i = 0;
}
else
{
i = 1;
id = (*idents)[0];
switch (id->dyncast())
{
case DYNCAST_IDENTIFIER:
s = sc->search(loc, id, NULL);
break;

case DYNCAST_DSYMBOL:
{
TemplateInstance *ti = (TemplateInstance *)id;
ti->semantic(sc);
s = ti;
break;
}
default:
assert(0);
}
}

for (; i < idents->dim; i++)
{
if (!s)
break;
id = (*idents)[i];
s = s->searchX(loc, sc, id);
}
{
Expression *e;
Type *t;
Dsymbol *s;
tqual->resolve(loc, sc, &e, &t, &s);
if (!s)
{
error("is not defined");
Expand Down Expand Up @@ -6805,13 +6755,7 @@ void TemplateMixin::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
buf->writestring("mixin ");

for (size_t i = 0; i < idents->dim; i++)
{ Identifier *id = (*idents)[i];

if (i)
buf->writeByte('.');
buf->writestring(id->toChars());
}
tqual->toCBuffer(buf, NULL, hgs);
buf->writestring("!(");
if (tiargs)
{
Expand Down
6 changes: 3 additions & 3 deletions src/template.h
Expand Up @@ -30,6 +30,7 @@ struct TemplateValueParameter;
struct TemplateAliasParameter;
struct TemplateTupleParameter;
struct Type;
struct TypeQualified;
struct TypeTypeof;
struct Scope;
struct Expression;
Expand Down Expand Up @@ -343,10 +344,9 @@ struct TemplateInstance : ScopeDsymbol

struct TemplateMixin : TemplateInstance
{
Identifiers *idents;
Type *tqual;
TypeQualified *tqual;

TemplateMixin(Loc loc, Identifier *ident, Type *tqual, Identifiers *idents, Objects *tiargs);
TemplateMixin(Loc loc, Identifier *ident, TypeQualified *tqual, Objects *tiargs);
Dsymbol *syntaxCopy(Dsymbol *s);
void semantic(Scope *sc);
void semantic2(Scope *sc);
Expand Down

0 comments on commit 2f9b8c4

Please sign in to comment.