Skip to content

Commit

Permalink
[Refactoring] Sync template constraint processing between TemplateDec…
Browse files Browse the repository at this point in the history
…laration::matchWithInstance and deduceFunctionTemplateMatch
  • Loading branch information
9rnsr committed Oct 2, 2013
1 parent bd75cde commit 5f84cf2
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/template.c
Expand Up @@ -897,19 +897,20 @@ MATCH TemplateDeclaration::matchWithInstance(Scope *sc, TemplateInstance *ti,

#if DMDV2
if (m && constraint && !flag)
{ /* Check to see if constraint is satisfied.
{
/* Check to see if constraint is satisfied.
*/
makeParamNamesVisibleInConstraint(paramscope, fargs);
Expression *e = constraint->syntaxCopy();
Scope *sc = paramscope->push();

/* There's a chicken-and-egg problem here. We don't know yet if this template
* instantiation will be a local one (enclosing is set), and we won't know until
* after selecting the correct template. Thus, function we're nesting inside
* is not on the sc scope chain, and this can cause errors in FuncDeclaration::getLevel().
* Workaround the problem by setting a flag to relax the checking on frame errors.
*/
sc->flags |= SCOPEstaticif;

int nerrors = global.errors;

FuncDeclaration *fd = onemember && onemember->toAlias() ?
onemember->toAlias()->isFuncDeclaration() : NULL;
Expand All @@ -924,17 +925,20 @@ MATCH TemplateDeclaration::matchWithInstance(Scope *sc, TemplateInstance *ti,
fd->vthis = fd->declareThis(paramscope, ad);
}

sc = sc->startCTFE();
e = e->semantic(sc);
e = resolveProperties(sc, e);
sc = sc->endCTFE();
if (e->op == TOKerror)
goto Lnomatch;
Scope *scx = paramscope->startCTFE();
scx->flags |= SCOPEstaticif;
e = e->semantic(scx);
e = resolveProperties(scx, e);
scx = scx->endCTFE();

if (fd && fd->vthis)
fd->vthis = vthissave;

sc->pop();
if (nerrors != global.errors) // if any errors from evaluating the constraint, no match
goto Lnomatch;
if (e->op == TOKerror)
goto Lnomatch;

e = e->ctfeInterpret();
if (e->isBool(TRUE))
;
Expand Down Expand Up @@ -1951,12 +1955,12 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(FuncDeclaration *f, Loc l

#if DMDV2
if (constraint)
{ /* Check to see if constraint is satisfied.
{
/* Check to see if constraint is satisfied.
* Most of this code appears twice; this is a good candidate for refactoring.
*/
makeParamNamesVisibleInConstraint(paramscope, fargs);
Expression *e = constraint->syntaxCopy();
paramscope->flags |= SCOPEstaticif;

/* Detect recursive attempts to instantiate this template declaration,
* Bugzilla 4072
Expand Down Expand Up @@ -2007,6 +2011,7 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(FuncDeclaration *f, Loc l
}

Scope *scx = paramscope->startCTFE();
scx->flags |= SCOPEstaticif;
e = e->semantic(scx);
e = resolveProperties(scx, e);
scx->endCTFE();
Expand Down

0 comments on commit 5f84cf2

Please sign in to comment.