Skip to content

Commit

Permalink
destructor building should be done in declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Mar 12, 2011
1 parent 610064e commit 242dc93
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
45 changes: 21 additions & 24 deletions src/declaration.c
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2010 by Digital Mars
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -666,7 +666,10 @@ VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer
onstack = 0;
canassign = 0;
value = NULL;
#if DMDV2
rundtor = NULL;
edtor = NULL;
#endif
}

Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
Expand Down Expand Up @@ -1034,7 +1037,7 @@ void VarDeclaration::semantic(Scope *sc)
e = new ConstructExp(loc, e1, e);
e->type = e1->type; // don't type check this, it would fail
init = new ExpInitializer(loc, e);
return;
goto Ldtor;
}
else if (type->ty == Ttypedef)
{ TypeTypedef *td = (TypeTypedef *)type;
Expand Down Expand Up @@ -1340,6 +1343,16 @@ void VarDeclaration::semantic(Scope *sc)
}
sc = sc->pop();
}

Ldtor:
/* Build code to execute destruction, if necessary
*/
edtor = callScopeDtor(sc);
if (edtor)
{
edtor = edtor->semantic(sc);
}

sem = SemanticDone;
}

Expand Down Expand Up @@ -1630,29 +1643,10 @@ int VarDeclaration::needsAutoDtor()
{
//printf("VarDeclaration::needsAutoDtor() %s\n", toChars());

if (noscope || storage_class & STCnodtor)
if (noscope || !edtor)
return FALSE;

// Destructors for structs and arrays of structs
Type *tv = type->toBasetype();
while (tv->ty == Tsarray)
{ TypeSArray *ta = (TypeSArray *)tv;
tv = tv->nextOf()->toBasetype();
}
if (tv->ty == Tstruct)
{ TypeStruct *ts = (TypeStruct *)tv;
StructDeclaration *sd = ts->sym;
if (sd->dtor)
return TRUE;
}

// Destructors for classes
if (storage_class & (STCauto | STCscope))
{
if (type->isClassHandle())
return TRUE;
}
return FALSE;
return TRUE;
}


Expand All @@ -1666,8 +1660,11 @@ Expression *VarDeclaration::callScopeDtor(Scope *sc)

//printf("VarDeclaration::callScopeDtor() %s\n", toChars());

if (noscope || storage_class & STCnodtor)
// Destruction of STCfield's is handled by buildDtor()
if (noscope || storage_class & (STCnodtor | STCref | STCout | STCfield))
{
return NULL;
}

// Destructors for structs and arrays of structs
bool array = false;
Expand Down
3 changes: 2 additions & 1 deletion src/declaration.h
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2010 by Digital Mars
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -258,6 +258,7 @@ struct VarDeclaration : Declaration
VarDeclaration *rundtor; // if !NULL, rundtor is tested at runtime to see
// if the destructor should be run. Used to prevent
// dtor calls on postblitted vars
Expression *edtor; // if !=NULL, does the destruction of the variable
#endif

VarDeclaration(Loc loc, Type *t, Identifier *id, Initializer *init);
Expand Down
19 changes: 11 additions & 8 deletions src/dump.c
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2006 by Digital Mars
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -33,13 +33,16 @@ static char *type_print(Type *type)

void dumpExpressions(int i, Expressions *exps)
{
for (size_t j = 0; j < exps->dim; j++)
{ Expression *e = (Expression *)exps->data[j];
indent(i);
printf("(\n");
e->dump(i + 2);
indent(i);
printf(")\n");
if (exps)
{
for (size_t j = 0; j < exps->dim; j++)
{ Expression *e = (Expression *)exps->data[j];
indent(i);
printf("(\n");
e->dump(i + 2);
indent(i);
printf(")\n");
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/func.c
Expand Up @@ -1557,7 +1557,10 @@ void FuncDeclaration::semantic3(Scope *sc)
if (v->type->toBasetype()->ty == Tsarray)
continue;

Expression *e = v->callScopeDtor(sc2);
if (v->noscope)
continue;

Expression *e = v->edtor;
if (e)
{ Statement *s = new ExpStatement(0, e);
s = s->semantic(sc2);
Expand Down
7 changes: 3 additions & 4 deletions src/statement.c
Expand Up @@ -317,10 +317,9 @@ Statement *ExpStatement::scopeCode(Scope *sc, Statement **sentry, Statement **se
{
DeclarationExp *de = (DeclarationExp *)(exp);
VarDeclaration *v = de->declaration->isVarDeclaration();
if (v)
{ Expression *e;

e = v->callScopeDtor(sc);
if (v && !v->noscope)
{
Expression *e = v->edtor;
if (e)
{
//printf("dtor is: "); e->print();
Expand Down

0 comments on commit 242dc93

Please sign in to comment.