Skip to content

Commit

Permalink
[Refactoring] instantiatingModule should be derived from sc->tinst
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jan 20, 2014
1 parent 3c3c43e commit 8287473
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/attrib.c
Expand Up @@ -1061,7 +1061,7 @@ void PragmaDeclaration::semantic(Scope *sc)
if (global.params.moduleDeps && !global.params.moduleDepsFile)
{
OutBuffer *ob = global.params.moduleDeps;
Module* imod = sc->instantiatingModule ? sc->instantiatingModule : sc->module;
Module *imod = sc->instantiatingModule();
ob->writestring("depsLib ");
ob->writestring(imod->toPrettyChars());
ob->writestring(" (");
Expand Down
2 changes: 0 additions & 2 deletions src/clone.c
Expand Up @@ -76,7 +76,6 @@ FuncDeclaration *AggregateDeclaration::hasIdentityOpAssign(Scope *sc)
unsigned oldspec = global.speculativeGag; // template opAssign fbody makes it.
global.speculativeGag = global.gag;
sc = sc->push();
sc->tinst = NULL;
sc->speculative = true;

for (size_t i = 0; i < 2; i++)
Expand Down Expand Up @@ -416,7 +415,6 @@ FuncDeclaration *AggregateDeclaration::hasIdentityOpEquals(Scope *sc)
unsigned oldspec = global.speculativeGag; // template opAssign fbody makes it.
global.speculativeGag = global.gag;
sc = sc->push();
sc->tinst = NULL;
sc->speculative = true;

for (size_t j = 0; j < 2; j++)
Expand Down
2 changes: 1 addition & 1 deletion src/cond.c
Expand Up @@ -90,7 +90,7 @@ void printDepsConditional(Scope *sc, DVCondition* condition, const char* depType
if (!global.params.moduleDeps || global.params.moduleDepsFile)
return;
OutBuffer *ob = global.params.moduleDeps;
Module* imod = sc ? (sc->instantiatingModule ? sc->instantiatingModule : sc->module) : condition->mod;
Module* imod = sc ? sc->instantiatingModule() : condition->mod;
if (!imod)
return;
ob->writestring(depType);
Expand Down
6 changes: 3 additions & 3 deletions src/ctfeexpr.c
Expand Up @@ -546,9 +546,9 @@ TypeAArray *toBuiltinAAType(Type *t)
assert(t->ty == Tstruct);
StructDeclaration *sym = ((TypeStruct *)t)->sym;
assert(sym->ident == Id::AssociativeArray);
TemplateInstance *tinst = sym->parent->isTemplateInstance();
assert(tinst);
return new TypeAArray((Type *)(*tinst->tiargs)[1], (Type *)(*tinst->tiargs)[0]);
TemplateInstance *ti = sym->parent->isTemplateInstance();
assert(ti);
return new TypeAArray((Type *)(*ti->tiargs)[1], (Type *)(*ti->tiargs)[0]);
#else
assert(0);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/expression.c
Expand Up @@ -7283,7 +7283,7 @@ Expression *FileExp::semantic(Scope *sc)
if (global.params.moduleDeps != NULL)
{
OutBuffer *ob = global.params.moduleDeps;
Module* imod = sc->instantiatingModule ? sc->instantiatingModule : sc->module;
Module* imod = sc->instantiatingModule();

if (!global.params.moduleDepsFile)
ob->writestring("depsFile ");
Expand Down
2 changes: 1 addition & 1 deletion src/import.c
Expand Up @@ -291,7 +291,7 @@ void Import::semantic(Scope *sc)
*/

OutBuffer *ob = global.params.moduleDeps;
Module* imod = sc->instantiatingModule ? sc->instantiatingModule : sc->module;
Module* imod = sc->instantiatingModule();
if (!global.params.moduleDepsFile)
ob->writestring("depsImport ");
ob->writestring(imod->toPrettyChars());
Expand Down
7 changes: 5 additions & 2 deletions src/mtype.c
Expand Up @@ -4899,8 +4899,11 @@ StructDeclaration *TypeAArray::getImpl()
TemplateInstance *ti = dti->ti;
#endif
// Instantiate on the root module of import dependency graph.
Scope *scx = sc->push(sc->module->importedFrom);
scx->instantiatingModule = sc->module->importedFrom;
Module *mi = sc->module->importedFrom;
Scope *scx = sc->push(mi);
scx->module = mi;
scx->tinst = NULL;
assert(scx->instantiatingModule() == mi);
ti->semantic(scx);
ti->semantic2(scx);
ti->semantic3(scx);
Expand Down
13 changes: 10 additions & 3 deletions src/scope.c
Expand Up @@ -27,6 +27,7 @@
#include "module.h"
#include "id.h"
#include "lexer.h"
#include "template.h"

Scope *Scope::freelist = NULL;

Expand All @@ -48,11 +49,11 @@ void *Scope::operator new(size_t size)
}

Scope::Scope()
{ // Create root scope
{
// Create root scope

//printf("Scope::Scope() %p\n", this);
this->module = NULL;
this->instantiatingModule = NULL;
this->scopesym = NULL;
this->sd = NULL;
this->enclosing = NULL;
Expand Down Expand Up @@ -94,7 +95,6 @@ Scope::Scope(Scope *enclosing)
//printf("Scope::Scope(enclosing = %p) %p\n", enclosing, this);
assert(!(enclosing->flags & SCOPEfree));
this->module = enclosing->module;
this->instantiatingModule = enclosing->instantiatingModule;
this->func = enclosing->func;
this->parent = enclosing->parent;
this->scopesym = NULL;
Expand Down Expand Up @@ -399,6 +399,13 @@ void Scope::mergeFieldInit(Loc loc, unsigned *fies)
}
}

Module *Scope::instantiatingModule()
{
if (tinst && tinst->instantiatingModule)
return tinst->instantiatingModule;
return module;
}

Dsymbol *Scope::search(Loc loc, Identifier *ident, Dsymbol **pscopesym)
{ Dsymbol *s;
Scope *sc;
Expand Down
3 changes: 2 additions & 1 deletion src/scope.h
Expand Up @@ -66,7 +66,6 @@ struct Scope
Scope *enclosing; // enclosing Scope

Module *module; // Root module
Module *instantiatingModule; // top level module that started a chain of template instantiations
ScopeDsymbol *scopesym; // current symbol
ScopeDsymbol *sd; // if in static if, and declaring new symbols,
// sd gets the addMember()
Expand Down Expand Up @@ -132,6 +131,8 @@ struct Scope
unsigned *saveFieldInit();
void mergeFieldInit(Loc loc, unsigned *cses);

Module *instantiatingModule();

Dsymbol *search(Loc loc, Identifier *ident, Dsymbol **pscopesym);
Dsymbol *search_correct(Identifier *ident);
Dsymbol *insert(Dsymbol *s);
Expand Down
2 changes: 1 addition & 1 deletion src/struct.c
Expand Up @@ -169,7 +169,7 @@ void AggregateDeclaration::semantic3(Scope *sc)
Expression *e = new DsymbolExp(Loc(), s, 0);

Scope *sc2 = ti->tempdecl->scope->startCTFE();
sc2->instantiatingModule = sc->instantiatingModule ? sc->instantiatingModule : sc->module;
sc2->tinst = sc->tinst;
e = e->semantic(sc2);
sc2->endCTFE();

Expand Down
63 changes: 36 additions & 27 deletions src/template.c
Expand Up @@ -782,8 +782,7 @@ MATCH TemplateDeclaration::matchWithInstance(Scope *sc, TemplateInstance *ti,
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = scope->parent;
Scope *paramscope = scope->push(paramsym);
Module *mi = ti->instantiatingModule ? ti->instantiatingModule : sc->instantiatingModule;
paramscope->instantiatingModule = mi;
paramscope->tinst = ti;
paramscope->callsc = sc;
paramscope->stc = 0;

Expand Down Expand Up @@ -1089,16 +1088,15 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(FuncDeclaration *f, Loc l
if (errors || f->errors)
return MATCHnomatch;

TemplateInstance *ti = new TemplateInstance(loc, this, NULL);
ti->tinst = getInstantiating(sc);
ti->instantiatingModule = sc->instantiatingModule();

// Set up scope for parameters
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = scope->parent;
Scope *paramscope = scope->push(paramsym);

paramscope->instantiatingModule = sc->instantiatingModule;
Module *mi = sc->instantiatingModule ? sc->instantiatingModule : sc->module;
if (!sc->instantiatingModule || sc->instantiatingModule->isRoot())
paramscope->instantiatingModule = mi;

paramscope->tinst = ti;
paramscope->callsc = sc;
paramscope->stc = 0;

Expand Down Expand Up @@ -2518,7 +2516,8 @@ FuncDeclaration *TemplateDeclaration::doHeaderInstantiation(Scope *sc,

assert(scope);
TemplateInstance *ti = new TemplateInstance(loc, this, tdargs);
ti->tinst = sc->tinst;
ti->tinst = getInstantiating(sc);
ti->instantiatingModule = sc->instantiatingModule();
{
ti->tdtypes.setDim(parameters->dim);
if (matchWithInstance(sc, ti, &ti->tdtypes, fargs, 2) <= MATCHnomatch)
Expand All @@ -2535,13 +2534,11 @@ FuncDeclaration *TemplateDeclaration::doHeaderInstantiation(Scope *sc,
fd = new FuncDeclaration(fd->loc, fd->endloc, fd->ident, fd->storage_class, fd->type->syntaxCopy());
fd->parent = ti;

Module *mi = sc->instantiatingModule ? sc->instantiatingModule : sc->module;

Scope *scope = this->scope;
ti->argsym = new ScopeDsymbol();
ti->argsym->parent = scope->parent;
scope = scope->push(ti->argsym);
scope->instantiatingModule = mi;
scope->tinst = ti;

bool hasttp = false;

Expand Down Expand Up @@ -2907,6 +2904,21 @@ void TemplateDeclaration::removeInstance(TemplateInstance *handle)
--numinstances;
}

/*******************************************
* Returns template instance which instantiating this template declaration.
*/

TemplateInstance *TemplateDeclaration::getInstantiating(Scope *sc)
{
/* If this is instantiated declaration in root module, Return it.
*/
TemplateInstance *tinst = isInstantiated();
if (tinst && (!tinst->instantiatingModule || tinst->instantiatingModule->isRoot()))
return tinst;

return sc->tinst;
}

/* ======================== Type ============================================ */

/****
Expand Down Expand Up @@ -5367,20 +5379,10 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
printf("Scope\n");
for (Scope *scx = sc; scx; scx = scx->enclosing)
{
printf("\t%s parent %s instantiatingModule %p\n", scx->module ? scx->module->toChars() : "null", scx->parent ? scx->parent->toChars() : "null", scx->instantiatingModule);
printf("\t%s parent %s\n", scx->module ? scx->module->toChars() : "null", scx->parent ? scx->parent->toChars() : "null");
}
#endif

Module *mi = sc->instantiatingModule ? sc->instantiatingModule : sc->module;

/* If a TemplateInstance is ever instantiated by non-root modules,
* we do not have to generate code for it,
* because it will be generated when the non-root module is compiled.
*/
if (!instantiatingModule || instantiatingModule->isRoot())
instantiatingModule = mi;
//printf("mi = %s\n", mi->toChars());

#if LOG
printf("\n+TemplateInstance::semantic('%s', this=%p)\n", toChars(), this);
#endif
Expand All @@ -5395,6 +5397,16 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
// get the enclosing template instance from the scope tinst
tinst = sc->tinst;

Module *mi = sc->instantiatingModule();

/* If a TemplateInstance is ever instantiated by non-root modules,
* we do not have to generate code for it,
* because it will be generated when the non-root module is compiled.
*/
if (!instantiatingModule || instantiatingModule->isRoot())
instantiatingModule = mi;
//printf("mi = %s\n", mi->toChars());

if (semanticRun != PASSinit)
{
#if LOG
Expand Down Expand Up @@ -5620,7 +5632,7 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
argsym = new ScopeDsymbol();
argsym->parent = scope->parent;
scope = scope->push(argsym);
scope->instantiatingModule = mi;
scope->tinst = this;
//scope->stc = 0;

// Declare each template parameter as an alias for the argument type
Expand Down Expand Up @@ -6946,7 +6958,6 @@ void TemplateInstance::semantic2(Scope *sc)
sc = tempdecl->scope;
assert(sc);
sc = sc->push(argsym);
sc->instantiatingModule = instantiatingModule;
sc = sc->push(this);
sc->tinst = this;
for (size_t i = 0; i < members->dim; i++)
Expand Down Expand Up @@ -6978,7 +6989,6 @@ void TemplateInstance::semantic3(Scope *sc)
{
sc = tempdecl->scope;
sc = sc->push(argsym);
sc->instantiatingModule = instantiatingModule;
sc = sc->push(this);
sc->tinst = this;
int needGagging = (speculative && !global.gag);
Expand Down Expand Up @@ -7716,7 +7726,6 @@ void TemplateMixin::semantic3(Scope *sc)
if (members)
{
sc = sc->push(argsym);
sc->instantiatingModule = instantiatingModule;
sc = sc->push(this);
for (size_t i = 0; i < members->dim; i++)
{
Expand Down
2 changes: 2 additions & 0 deletions src/template.h
Expand Up @@ -105,6 +105,8 @@ class TemplateDeclaration : public ScopeDsymbol
TemplateInstance *addInstance(TemplateInstance *ti);
void removeInstance(TemplateInstance *handle);

TemplateInstance *getInstantiating(Scope *sc);

TemplateDeclaration *isTemplateDeclaration() { return this; }

TemplateTupleParameter *isVariadic();
Expand Down

0 comments on commit 8287473

Please sign in to comment.