Skip to content

Commit

Permalink
switch to apply()
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Mar 13, 2012
1 parent 2c3e9a8 commit 6f28553
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 45 deletions.
19 changes: 19 additions & 0 deletions src/attrib.c
Expand Up @@ -47,6 +47,24 @@ Dsymbols *AttribDeclaration::include(Scope *sc, ScopeDsymbol *sd)
return decl;
}

int AttribDeclaration::apply(Dsymbol_apply_ft_t fp, void *param)
{
Dsymbols *d = include(NULL, NULL);

if (d)
{
for (size_t i = 0; i < d->dim; i++)
{ Dsymbol *s = (*d)[i];
if (s)
{
if (s->apply(fp, param))
return 1;
}
}
}
return 0;
}

int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
{
int m = 0;
Expand Down Expand Up @@ -246,6 +264,7 @@ void AttribDeclaration::toObjFile(int multiobj)

int AttribDeclaration::cvMember(unsigned char *p)
{
assert(0);
int nwritten = 0;
int n;
Dsymbols *d = include(NULL, NULL);
Expand Down
3 changes: 2 additions & 1 deletion src/attrib.h
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2011 by Digital Mars
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -33,6 +33,7 @@ struct AttribDeclaration : Dsymbol

AttribDeclaration(Dsymbols *decl);
virtual Dsymbols *include(Scope *sc, ScopeDsymbol *s);
int apply(Dsymbol_apply_ft_t fp, void *param);
int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
void setScopeNewSc(Scope *sc,
StorageClass newstc, enum LINK linkage, enum PROT protection, int explictProtection,
Expand Down
5 changes: 5 additions & 0 deletions src/dsymbol.c
Expand Up @@ -558,6 +558,11 @@ int Dsymbol::needThis()
return FALSE;
}

int Dsymbol::apply(Dsymbol_apply_ft_t fp, void *param)
{
return (*fp)(this, param);
}

int Dsymbol::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
{
//printf("Dsymbol::addMember('%s')\n", toChars());
Expand Down
3 changes: 3 additions & 0 deletions src/dsymbol.h
Expand Up @@ -109,6 +109,8 @@ enum PASS
PASSobj, // toObjFile() run
};

typedef int (*Dsymbol_apply_ft_t)(Dsymbol *, void *);

struct Dsymbol : Object
{
Identifier *ident;
Expand Down Expand Up @@ -146,6 +148,7 @@ struct Dsymbol : Object
virtual const char *toPrettyChars();
virtual const char *kind();
virtual Dsymbol *toAlias(); // resolve real symbol
virtual int apply(Dsymbol_apply_ft_t fp, void *param);
virtual int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
virtual void setScope(Scope *sc);
virtual void importAll(Scope *sc);
Expand Down
99 changes: 55 additions & 44 deletions src/tocvdebug.c
@@ -1,5 +1,5 @@

// Copyright (c) 2004-2011 by Digital Mars
// Copyright (c) 2004-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -281,6 +281,34 @@ void EnumDeclaration::toDebug()
}
}

// Closure variables for Lambda cv_mem_count
struct CvMemberCount
{
unsigned nfields;
unsigned fnamelen;
};

// Lambda function
int cv_mem_count(Dsymbol *s, void *param)
{ CvMemberCount *pmc = (CvMemberCount *)param;

int nwritten = s->cvMember(NULL);
if (nwritten)
{
pmc->fnamelen += nwritten;
pmc->nfields++;
}
return 0;
}

// Lambda function
int cv_mem_p(Dsymbol *s, void *param)
{
unsigned char **pp = (unsigned char **)param;
*pp += s->cvMember(*pp);
return 0;
}


void StructDeclaration::toDebug()
{
Expand Down Expand Up @@ -354,24 +382,18 @@ void StructDeclaration::toDebug()
return /*typidx*/;
}

// Compute the number of fields, and the length of the fieldlist record
nfields = 0;
fnamelen = 2;

count = nfields;
// Compute the number of fields (nfields), and the length of the fieldlist record (fnamelen)
CvMemberCount mc;
mc.nfields = 0;
mc.fnamelen = 2;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
int nwritten;

nwritten = s->cvMember(NULL);
if (nwritten)
{
fnamelen += nwritten;
nfields++;
count++;
}
{ Dsymbol *s = (*members)[i];
s->apply(&cv_mem_count, &mc);
}
nfields = mc.nfields;
fnamelen = mc.fnamelen;

count = nfields;
TOWORD(d->data + 2,count);
TOWORD(d->data + 6,property);

Expand All @@ -383,9 +405,8 @@ void StructDeclaration::toDebug()
TOWORD(p,LF_FIELDLIST);
p += 2;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];

p += s->cvMember(p);
{ Dsymbol *s = (*members)[i];
s->apply(&cv_mem_p, &p);
}

//dbg_printf("fnamelen = %d, p-dt->data = %d\n",fnamelen,p-dt->data);
Expand Down Expand Up @@ -516,32 +537,25 @@ void ClassDeclaration::toDebug()
return /*typidx*/;
}

// Compute the number of fields, and the length of the fieldlist record
nfields = 0;
fnamelen = 2;

// Compute the number of fields (nfields), and the length of the fieldlist record (fnamelen)
CvMemberCount mc;
mc.nfields = 0;
mc.fnamelen = 2;
// Add in base classes
for (size_t i = 0; i < baseclasses->dim; i++)
{ BaseClass *bc = baseclasses->tdata()[i];
{ BaseClass *bc = (*baseclasses)[i];

nfields++;
fnamelen += 6 + cv4_numericbytes(bc->offset);
mc.nfields++;
mc.fnamelen += 6 + cv4_numericbytes(bc->offset);
}

count = nfields;
for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];
int nwritten;

nwritten = s->cvMember(NULL);
if (nwritten)
{
fnamelen += nwritten;
nfields++;
count++;
}
{ Dsymbol *s = (*members)[i];
s->apply(&cv_mem_count, &mc);
}
nfields = mc.nfields;
fnamelen = mc.fnamelen;

count = nfields;
TOWORD(d->data + 2,count);
TOWORD(d->data + 6,property);

Expand All @@ -555,7 +569,7 @@ void ClassDeclaration::toDebug()

// Add in base classes
for (size_t i = 0; i < baseclasses->dim; i++)
{ BaseClass *bc = baseclasses->tdata()[i];
{ BaseClass *bc = (*baseclasses)[i];
idx_t typidx;
unsigned attribute;

Expand All @@ -572,12 +586,9 @@ void ClassDeclaration::toDebug()
p += cv4_numericbytes(bc->offset);
}



for (size_t i = 0; i < members->dim; i++)
{ Dsymbol *s = members->tdata()[i];

p += s->cvMember(p);
{ Dsymbol *s = (*members)[i];
s->apply(&cv_mem_p, &p);
}

//dbg_printf("fnamelen = %d, p-dt->data = %d\n",fnamelen,p-dt->data);
Expand Down

0 comments on commit 6f28553

Please sign in to comment.