Skip to content

Commit

Permalink
Improve stcToBuffer() and stcToChars() to handle last one space
Browse files Browse the repository at this point in the history
And move them into hdrgen.c, that's more better location.
  • Loading branch information
9rnsr committed Jun 7, 2015
1 parent 6d3fc41 commit 6b64a46
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 139 deletions.
89 changes: 0 additions & 89 deletions src/attrib.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,95 +391,6 @@ Scope *StorageClassDeclaration::newScope(Scope *sc)
return createNewScope(sc, scstc, sc->linkage, sc->protection, sc->explicitProtection, sc->structalign);
}

/*************************************************
* Pick off one of the storage classes from stc,
* and return a pointer to a string representation of it.
* stc is reduced by the one picked.
* tmp[] is a buffer big enough to hold that string.
*/
const char *StorageClassDeclaration::stcToChars(char tmp[], StorageClass& stc)
{
struct SCstring
{
StorageClass stc;
TOK tok;
const char *id;
};

static SCstring table[] =
{
{ STCauto, TOKauto },
{ STCscope, TOKscope },
{ STCstatic, TOKstatic },
{ STCextern, TOKextern },
{ STCconst, TOKconst },
{ STCfinal, TOKfinal },
{ STCabstract, TOKabstract },
{ STCsynchronized, TOKsynchronized },
{ STCdeprecated, TOKdeprecated },
{ STCoverride, TOKoverride },
{ STClazy, TOKlazy },
{ STCalias, TOKalias },
{ STCout, TOKout },
{ STCin, TOKin },
{ STCmanifest, TOKenum },
{ STCimmutable, TOKimmutable },
{ STCshared, TOKshared },
{ STCnothrow, TOKnothrow },
{ STCwild, TOKwild },
{ STCpure, TOKpure },
{ STCref, TOKref },
{ STCtls },
{ STCgshared, TOKgshared },
{ STCnogc, TOKat, "nogc" },
{ STCproperty, TOKat, "property" },
{ STCsafe, TOKat, "safe" },
{ STCtrusted, TOKat, "trusted" },
{ STCsystem, TOKat, "system" },
{ STCdisable, TOKat, "disable" },
{ 0, TOKreserved }
};

for (int i = 0; table[i].stc; i++)
{
StorageClass tbl = table[i].stc;
assert(tbl & STCStorageClass);
if (stc & tbl)
{
stc &= ~tbl;
if (tbl == STCtls) // TOKtls was removed
return "__thread";

TOK tok = table[i].tok;
if (tok == TOKat)
{
tmp[0] = '@';
strcpy(tmp + 1, table[i].id);
return tmp;
}
else
return Token::toChars(tok);
}
}
//printf("stc = %llx\n", (unsigned long long)stc);
return NULL;
}

void StorageClassDeclaration::stcToCBuffer(OutBuffer *buf, StorageClass stc)
{
while (stc)
{
const size_t BUFFER_LEN = 20;
char tmp[BUFFER_LEN];
const char *p = stcToChars(tmp, stc);
if (!p)
break;
assert(strlen(p) < BUFFER_LEN);
buf->writestring(p);
buf->writeByte(' ');
}
}

/********************************* DeprecatedDeclaration ****************************/

DeprecatedDeclaration::DeprecatedDeclaration(Expression *msg, Dsymbols *decl)
Expand Down
2 changes: 0 additions & 2 deletions src/attrib.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ class StorageClassDeclaration : public AttribDeclaration
Scope *newScope(Scope *sc);
bool oneMember(Dsymbol **ps, Identifier *ident);

static const char *stcToChars(char tmp[], StorageClass& stc);
static void stcToCBuffer(OutBuffer *buf, StorageClass stc);
void accept(Visitor *v) { v->visit(this); }
};

Expand Down
138 changes: 120 additions & 18 deletions src/hdrgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class PrettyPrintVisitor : public Visitor
Parameter *p = (*s->parameters)[i];
if (i)
buf->writestring(", ");
StorageClassDeclaration::stcToCBuffer(buf, p->storageClass);
if (stcToBuffer(buf, p->storageClass))
buf->writeByte(' ');
if (p->type)
typeToBuffer(p->type, p->ident);
else
Expand Down Expand Up @@ -309,7 +310,8 @@ class PrettyPrintVisitor : public Visitor
StorageClass stc = p->storageClass;
if (!p->type && !stc)
stc = STCauto;
StorageClassDeclaration::stcToCBuffer(buf, stc);
if (stcToBuffer(buf, stc))
buf->writeByte(' ');
if (p->type)
typeToBuffer(p->type, p->ident);
else
Expand Down Expand Up @@ -1197,7 +1199,8 @@ class PrettyPrintVisitor : public Visitor

void visit(StorageClassDeclaration *d)
{
StorageClassDeclaration::stcToCBuffer(buf, d->stc);
if (stcToBuffer(buf, d->stc))
buf->writeByte(' ');
visit((AttribDeclaration *)d);
}

Expand Down Expand Up @@ -1374,7 +1377,8 @@ class PrettyPrintVisitor : public Visitor
if (FuncDeclaration *fd = onemember->isFuncDeclaration())
{
assert(fd->type);
StorageClassDeclaration::stcToCBuffer(buf, fd->storage_class);
if (stcToBuffer(buf, fd->storage_class))
buf->writeByte(' ');
functionToBufferFull((TypeFunction *)fd->type, buf, d->ident, hgs, d);
visitTemplateConstraint(d->constraint);

Expand Down Expand Up @@ -1417,7 +1421,8 @@ class PrettyPrintVisitor : public Visitor
if (d->constraint)
return false;

StorageClassDeclaration::stcToCBuffer(buf, vd->storage_class);
if (stcToBuffer(buf, vd->storage_class))
buf->writeByte(' ');
if (vd->type)
typeToBuffer(vd->type, vd->ident);
else
Expand Down Expand Up @@ -1714,20 +1719,23 @@ class PrettyPrintVisitor : public Visitor
{
buf->writestring(d->ident->toChars());
buf->writestring(" = ");
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class);
if (stcToBuffer(buf, d->storage_class))
buf->writeByte(' ');
d->aliassym->accept(this);
}
else if (d->type->ty == Tfunction)
{
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class);
if (stcToBuffer(buf, d->storage_class))
buf->writeByte(' ');
typeToBuffer(d->type, d->ident);
}
else
{
declstring = (d->ident == Id::string || d->ident == Id::wstring || d->ident == Id::dstring);
buf->writestring(d->ident->toChars());
buf->writestring(" = ");
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class);
if (stcToBuffer(buf, d->storage_class))
buf->writeByte(' ');
typeToBuffer(d->type, NULL);
declstring = false;
}
Expand All @@ -1750,7 +1758,8 @@ class PrettyPrintVisitor : public Visitor
}
else
{
StorageClassDeclaration::stcToCBuffer(buf, v->storage_class);
if (stcToBuffer(buf, v->storage_class))
buf->writeByte(' ');
if (v->type)
typeToBuffer(v->type, v->ident);
else
Expand All @@ -1771,7 +1780,8 @@ class PrettyPrintVisitor : public Visitor
{
//printf("FuncDeclaration::toCBuffer() '%s'\n", f->toChars());

StorageClassDeclaration::stcToCBuffer(buf, f->storage_class);
if (stcToBuffer(buf, f->storage_class))
buf->writeByte(' ');
typeToBuffer(f->type, f->ident);
if (hgs->hdrgen == 1)
{
Expand Down Expand Up @@ -1905,7 +1915,8 @@ class PrettyPrintVisitor : public Visitor

void visit(StaticCtorDeclaration *d)
{
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class & ~STCstatic);
if (stcToBuffer(buf, d->storage_class & ~STCstatic))
buf->writeByte(' ');
if (d->isSharedStaticCtorDeclaration())
buf->writestring("shared ");
buf->writestring("static this()");
Expand All @@ -1922,7 +1933,8 @@ class PrettyPrintVisitor : public Visitor
{
if (hgs->hdrgen)
return;
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class & ~STCstatic);
if (stcToBuffer(buf, d->storage_class & ~STCstatic))
buf->writeByte(' ');
if (d->isSharedStaticDtorDeclaration())
buf->writestring("shared ");
buf->writestring("static ~this()");
Expand All @@ -1933,7 +1945,8 @@ class PrettyPrintVisitor : public Visitor
{
if (hgs->hdrgen)
return;
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class);
if (stcToBuffer(buf, d->storage_class))
buf->writeByte(' ');
buf->writestring("invariant");
bodyToBuffer(d);
}
Expand All @@ -1942,22 +1955,25 @@ class PrettyPrintVisitor : public Visitor
{
if (hgs->hdrgen)
return;
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class);
if (stcToBuffer(buf, d->storage_class))
buf->writeByte(' ');
buf->writestring("unittest");
bodyToBuffer(d);
}

void visit(NewDeclaration *d)
{
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class & ~STCstatic);
if (stcToBuffer(buf, d->storage_class & ~STCstatic))
buf->writeByte(' ');
buf->writestring("new");
parametersToBuffer(d->parameters, d->varargs);
bodyToBuffer(d);
}

void visit(DeleteDeclaration *d)
{
StorageClassDeclaration::stcToCBuffer(buf, d->storage_class & ~STCstatic);
if (stcToBuffer(buf, d->storage_class & ~STCstatic))
buf->writeByte(' ');
buf->writestring("delete");
parametersToBuffer(d->parameters, 0);
bodyToBuffer(d);
Expand Down Expand Up @@ -2918,8 +2934,8 @@ class PrettyPrintVisitor : public Visitor
if (p->type && p->type->mod & MODshared)
stc &= ~STCshared;

StorageClassDeclaration::stcToCBuffer(buf,
stc & (STCconst | STCimmutable | STCwild | STCshared | STCscope));
if (stcToBuffer(buf, stc & (STCconst | STCimmutable | STCwild | STCshared | STCscope)))
buf->writeByte(' ');

if (p->storageClass & STCalias)
{
Expand Down Expand Up @@ -3034,6 +3050,92 @@ void toCBuffer(Initializer *iz, OutBuffer *buf, HdrGenState *hgs)
iz->accept(&v);
}

bool stcToBuffer(OutBuffer *buf, StorageClass stc)
{
bool result = false;
while (stc)
{
if (!result)
result = true;
else
buf->writeByte(' ');
const char *p = stcToChars(stc);
if (!p)
break;
buf->writestring(p);
}
return result;
}

/*************************************************
* Pick off one of the storage classes from stc,
* and return a pointer to a string representation of it.
* stc is reduced by the one picked.
*/
const char *stcToChars(StorageClass& stc)
{
struct SCstring
{
StorageClass stc;
TOK tok;
const char *id;
};

static SCstring table[] =
{
{ STCauto, TOKauto },
{ STCscope, TOKscope },
{ STCstatic, TOKstatic },
{ STCextern, TOKextern },
{ STCconst, TOKconst },
{ STCfinal, TOKfinal },
{ STCabstract, TOKabstract },
{ STCsynchronized, TOKsynchronized },
{ STCdeprecated, TOKdeprecated },
{ STCoverride, TOKoverride },
{ STClazy, TOKlazy },
{ STCalias, TOKalias },
{ STCout, TOKout },
{ STCin, TOKin },
{ STCmanifest, TOKenum },
{ STCimmutable, TOKimmutable },
{ STCshared, TOKshared },
{ STCnothrow, TOKnothrow },
{ STCwild, TOKwild },
{ STCpure, TOKpure },
{ STCref, TOKref },
{ STCtls },
{ STCgshared, TOKgshared },
{ STCnogc, TOKat, "@nogc" },
{ STCproperty, TOKat, "@property" },
{ STCsafe, TOKat, "@safe" },
{ STCtrusted, TOKat, "@trusted" },
{ STCsystem, TOKat, "@system" },
{ STCdisable, TOKat, "@disable" },
{ 0, TOKreserved }
};

for (int i = 0; table[i].stc; i++)
{
StorageClass tbl = table[i].stc;
assert(tbl & STCStorageClass);
if (stc & tbl)
{
stc &= ~tbl;
if (tbl == STCtls) // TOKtls was removed
return "__thread";

TOK tok = table[i].tok;
if (tok == TOKat)
return table[i].id;
else
return Token::toChars(tok);
}
}
//printf("stc = %llx\n", stc);
return NULL;
}

void trustToBuffer(OutBuffer *buf, TRUST trust)
{
const char *p = trustToChars(trust);
Expand Down
2 changes: 2 additions & 0 deletions src/hdrgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ void arrayObjectsToBuffer(OutBuffer *buf, Objects *objects);

const char *parametersTypeToChars(Parameters *parameters, int varargs);

bool stcToBuffer(OutBuffer *buf, StorageClass stc);
const char *stcToChars(StorageClass& stc);
const char *linkageToChars(LINK linkage);
6 changes: 5 additions & 1 deletion src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "declaration.h"
#include "id.h"
#include "attrib.h"
#include "hdrgen.h"

/********************************* Import ****************************/

Expand Down Expand Up @@ -323,7 +324,10 @@ void Import::semantic(Scope *sc)
protectionToBuffer(ob, Prot(protection));
ob->writeByte(' ');
if (isstatic)
StorageClassDeclaration::stcToCBuffer(ob, STCstatic);
{
stcToBuffer(ob, STCstatic);
ob->writeByte(' ');
}
ob->writestring(": ");

if (packages)
Expand Down
Loading

0 comments on commit 6b64a46

Please sign in to comment.