From 88542777dd6b0cb5854e84df07d3e350cca4f570 Mon Sep 17 00:00:00 2001 From: Brad Roberts Date: Wed, 12 May 2010 04:32:04 +0000 Subject: [PATCH] Add svn property svn:eol-style native to new files. Has the side effect of fixing the eol markers as needed. No functional changes anywhere. --- src/imphint.c | 164 ++++----- src/json.c | 878 ++++++++++++++++++++++++------------------------ src/root/aav.c | 376 ++++++++++----------- src/unittests.c | 34 +- 4 files changed, 726 insertions(+), 726 deletions(-) diff --git a/src/imphint.c b/src/imphint.c index 2151d427c86e..4e6313041385 100644 --- a/src/imphint.c +++ b/src/imphint.c @@ -1,82 +1,82 @@ - - -// Compiler implementation of the D programming language -// Copyright (c) 2010 by Digital Mars -// All Rights Reserved -// written by Walter Bright -// http://www.digitalmars.com -// License for redistribution is by either the Artistic License -// in artistic.txt, or the GNU General Public License in gnu.txt. -// See the included readme.txt for details. - -#include -#include -#include -#include -#include - -/****************************************** - * Looks for undefined identifier s to see - * if it might be undefined because an import - * was not specified. - * Not meant to be a comprehensive list of names in each module, - * just the most common ones. - */ - -const char *importHint(const char *s) -{ -#if DMDV1 - static const char *modules[] = - { "std.c.stdio", - "std.stdio", - "std.math", - }; - static const char *names[] = - { - "printf", NULL, - "writefln", NULL, - "sin", "cos", "sqrt", "fabs", NULL, - }; -#else - static const char *modules[] = - { "core.stdc.stdio", - "std.stdio", - "std.math", - }; - static const char *names[] = - { - "printf", NULL, - "writeln", NULL, - "sin", "cos", "sqrt", "fabs", NULL, - }; -#endif - int m = 0; - for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++) - { - const char *p = names[n]; - if (p == NULL) - { m++; - continue; - } - assert(m < sizeof(modules)/sizeof(modules[0])); - if (strcmp(s, p) == 0) - return modules[m]; - } - return NULL; // didn't find it -} - -#if UNITTEST - -void unittest_importHint() -{ - const char *p; - - p = importHint("printf"); - assert(p); - p = importHint("fabs"); - assert(p); - p = importHint("xxxxx"); - assert(!p); -} - -#endif + + +// Compiler implementation of the D programming language +// Copyright (c) 2010 by Digital Mars +// All Rights Reserved +// written by Walter Bright +// http://www.digitalmars.com +// License for redistribution is by either the Artistic License +// in artistic.txt, or the GNU General Public License in gnu.txt. +// See the included readme.txt for details. + +#include +#include +#include +#include +#include + +/****************************************** + * Looks for undefined identifier s to see + * if it might be undefined because an import + * was not specified. + * Not meant to be a comprehensive list of names in each module, + * just the most common ones. + */ + +const char *importHint(const char *s) +{ +#if DMDV1 + static const char *modules[] = + { "std.c.stdio", + "std.stdio", + "std.math", + }; + static const char *names[] = + { + "printf", NULL, + "writefln", NULL, + "sin", "cos", "sqrt", "fabs", NULL, + }; +#else + static const char *modules[] = + { "core.stdc.stdio", + "std.stdio", + "std.math", + }; + static const char *names[] = + { + "printf", NULL, + "writeln", NULL, + "sin", "cos", "sqrt", "fabs", NULL, + }; +#endif + int m = 0; + for (int n = 0; n < sizeof(names)/sizeof(names[0]); n++) + { + const char *p = names[n]; + if (p == NULL) + { m++; + continue; + } + assert(m < sizeof(modules)/sizeof(modules[0])); + if (strcmp(s, p) == 0) + return modules[m]; + } + return NULL; // didn't find it +} + +#if UNITTEST + +void unittest_importHint() +{ + const char *p; + + p = importHint("printf"); + assert(p); + p = importHint("fabs"); + assert(p); + p = importHint("xxxxx"); + assert(!p); +} + +#endif diff --git a/src/json.c b/src/json.c index 7db921b00e94..d4791bc95867 100644 --- a/src/json.c +++ b/src/json.c @@ -1,439 +1,439 @@ - -// Compiler implementation of the D programming language -// Copyright (c) 1999-2009 by Digital Mars -// All Rights Reserved -// written by Walter Bright -// http://www.digitalmars.com -// License for redistribution is by either the Artistic License -// in artistic.txt, or the GNU General Public License in gnu.txt. -// See the included readme.txt for details. - -// This implements the JSON capability. - -#include -#include -#include -#include -#include - -#include "rmem.h" -#include "root.h" - -#include "mars.h" -#include "dsymbol.h" -#include "macro.h" -#include "template.h" -#include "lexer.h" -#include "aggregate.h" -#include "declaration.h" -#include "enum.h" -#include "id.h" -#include "module.h" -#include "scope.h" -#include "hdrgen.h" -#include "json.h" -#include "mtype.h" -#include "attrib.h" -#include "cond.h" - -const char Pname[] = "name"; -const char Pkind[] = "kind"; -const char Pfile[] = "file"; -const char Pline[] = "line"; -const char Ptype[] = "type"; -const char Pcomment[] = "comment"; -const char Pmembers[] = "members"; - -void JsonRemoveComma(OutBuffer *buf); - -void json_generate(Array *modules) -{ OutBuffer buf; - - buf.writestring("[\n"); - for (int i = 0; i < modules->dim; i++) - { Module *m = (Module *)modules->data[i]; - if (global.params.verbose) - printf("json gen %s\n", m->toChars()); - m->toJsonBuffer(&buf); - buf.writestring(",\n"); - } - JsonRemoveComma(&buf); - buf.writestring("]\n"); - - // Write buf to file - char *arg = global.params.xfilename; - if (!arg || !*arg) - { // Generate lib file name from first obj name - char *n = (char *)global.params.objfiles->data[0]; - - n = FileName::name(n); - FileName *fn = FileName::forceExt(n, global.json_ext); - arg = fn->toChars(); - } - else if (arg[0] == '-' && arg[1] == 0) - { // Write to stdout - fwrite(buf.data, 1, buf.offset, stdout); - return; - } -// if (!FileName::absolute(arg)) -// arg = FileName::combine(dir, arg); - FileName *jsonfilename = FileName::defaultExt(arg, global.json_ext); - File *jsonfile = new File(jsonfilename); - assert(jsonfile); - jsonfile->setbuffer(buf.data, buf.offset); - jsonfile->ref = 1; - char *pt = FileName::path(jsonfile->toChars()); - if (*pt) - FileName::ensurePathExists(pt); - mem.free(pt); - jsonfile->writev(); -} - - -/********************************* - * Encode string into buf, and wrap it in double quotes. - */ -void JsonString(OutBuffer *buf, const char *s) -{ - buf->writeByte('\"'); - for (; *s; s++) - { - unsigned char c = (unsigned char) *s; - switch (c) - { - case '\n': - buf->writestring("\\n"); - break; - - case '\r': - buf->writestring("\\r"); - break; - - case '\t': - buf->writestring("\\t"); - break; - - case '\"': - buf->writestring("\\\""); - break; - - case '\\': - buf->writestring("\\\\"); - break; - - case '/': - buf->writestring("\\/"); - break; - - case '\b': - buf->writestring("\\b"); - break; - - case '\f': - buf->writestring("\\f"); - break; - - default: - if (c < 0x20) - buf->printf("\\u%04x", c); - else - // Note that UTF-8 chars pass through here just fine - buf->writeByte(c); - break; - } - } - buf->writeByte('\"'); -} - -void JsonProperty(OutBuffer *buf, const char *name, const char *value) -{ - JsonString(buf, name); - buf->writestring(" : "); - JsonString(buf, value); - buf->writestring(",\n"); -} - -void JsonProperty(OutBuffer *buf, const char *name, int value) -{ - JsonString(buf, name); - buf->writestring(" : "); - buf->printf("%d", value); - buf->writestring(",\n"); -} - -void JsonRemoveComma(OutBuffer *buf) -{ - if (buf->offset >= 2 && - buf->data[buf->offset - 2] == ',' && - buf->data[buf->offset - 1] == '\n') - buf->offset -= 2; -} - -void Dsymbol::toJsonBuffer(OutBuffer *buf) -{ -} - -void Module::toJsonBuffer(OutBuffer *buf) -{ - buf->writestring("{\n"); - - if (md) - JsonProperty(buf, Pname, md->toChars()); - - JsonProperty(buf, Pkind, kind()); - - JsonProperty(buf, Pfile, srcfile->toChars()); - - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - - JsonString(buf, Pmembers); - buf->writestring(" : [\n"); - - size_t offset = buf->offset; - for (int i = 0; i < members->dim; i++) - { Dsymbol *s = (Dsymbol *)members->data[i]; - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - s->toJsonBuffer(buf); - } - - JsonRemoveComma(buf); - buf->writestring("]\n"); - - buf->writestring("}\n"); -} - -void AttribDeclaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("AttribDeclaration::toJsonBuffer()\n"); - - Array *d = include(NULL, NULL); - - if (d) - { - size_t offset = buf->offset; - for (unsigned i = 0; i < d->dim; i++) - { Dsymbol *s = (Dsymbol *)d->data[i]; - //printf("AttribDeclaration::toJsonBuffer %s\n", s->toChars()); - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - s->toJsonBuffer(buf); - } - JsonRemoveComma(buf); - } -} - - -void ConditionalDeclaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("ConditionalDeclaration::toJsonBuffer()\n"); - if (condition->inc) - { - AttribDeclaration::toJsonBuffer(buf); - } -} - - -void InvariantDeclaration::toJsonBuffer(OutBuffer *buf) { } -void DtorDeclaration::toJsonBuffer(OutBuffer *buf) { } -void StaticCtorDeclaration::toJsonBuffer(OutBuffer *buf) { } -void StaticDtorDeclaration::toJsonBuffer(OutBuffer *buf) { } -void ClassInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } -void ModuleInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } -void TypeInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } -void UnitTestDeclaration::toJsonBuffer(OutBuffer *buf) { } -#if DMDV2 -void PostBlitDeclaration::toJsonBuffer(OutBuffer *buf) { } -#endif - -void Declaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("Declaration::toJsonBuffer()\n"); - buf->writestring("{\n"); - - JsonProperty(buf, Pname, toChars()); - JsonProperty(buf, Pkind, kind()); - if (type) - JsonProperty(buf, Ptype, type->toChars()); - - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - - if (loc.linnum) - JsonProperty(buf, Pline, loc.linnum); - - TypedefDeclaration *td = isTypedefDeclaration(); - if (td) - { - JsonProperty(buf, "base", td->basetype->toChars()); - } - - JsonRemoveComma(buf); - buf->writestring("}\n"); -} - -void AggregateDeclaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("AggregateDeclaration::toJsonBuffer()\n"); - buf->writestring("{\n"); - - JsonProperty(buf, Pname, toChars()); - JsonProperty(buf, Pkind, kind()); - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - if (loc.linnum) - JsonProperty(buf, Pline, loc.linnum); - - ClassDeclaration *cd = isClassDeclaration(); - if (cd) - { - if (cd->baseClass) - { - JsonProperty(buf, "base", cd->baseClass->toChars()); - } - if (cd->interfaces_dim) - { - JsonString(buf, "interfaces"); - buf->writestring(" : [\n"); - size_t offset = buf->offset; - for (int i = 0; i < cd->interfaces_dim; i++) - { BaseClass *b = cd->interfaces[i]; - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - JsonString(buf, b->base->toChars()); - } - JsonRemoveComma(buf); - buf->writestring("],\n"); - } - } - - if (members) - { - JsonString(buf, Pmembers); - buf->writestring(" : [\n"); - size_t offset = buf->offset; - for (int i = 0; i < members->dim; i++) - { Dsymbol *s = (Dsymbol *)members->data[i]; - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - s->toJsonBuffer(buf); - } - JsonRemoveComma(buf); - buf->writestring("]\n"); - } - JsonRemoveComma(buf); - - buf->writestring("}\n"); -} - -void TemplateDeclaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("TemplateDeclaration::toJsonBuffer()\n"); - - buf->writestring("{\n"); - - JsonProperty(buf, Pname, toChars()); - JsonProperty(buf, Pkind, kind()); - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - - if (loc.linnum) - JsonProperty(buf, Pline, loc.linnum); - - JsonString(buf, Pmembers); - buf->writestring(" : [\n"); - size_t offset = buf->offset; - for (int i = 0; i < members->dim; i++) - { Dsymbol *s = (Dsymbol *)members->data[i]; - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - s->toJsonBuffer(buf); - } - JsonRemoveComma(buf); - buf->writestring("]\n"); - - buf->writestring("}\n"); -} - -void EnumDeclaration::toJsonBuffer(OutBuffer *buf) -{ - //printf("EnumDeclaration::toJsonBuffer()\n"); - if (isAnonymous()) - { - if (members) - { - for (int i = 0; i < members->dim; i++) - { - Dsymbol *s = (Dsymbol *)members->data[i]; - s->toJsonBuffer(buf); - buf->writestring(",\n"); - } - JsonRemoveComma(buf); - } - return; - } - - buf->writestring("{\n"); - - JsonProperty(buf, Pname, toChars()); - JsonProperty(buf, Pkind, kind()); - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - - if (loc.linnum) - JsonProperty(buf, Pline, loc.linnum); - - if (memtype) - JsonProperty(buf, "base", memtype->toChars()); - - if (members) - { - JsonString(buf, Pmembers); - buf->writestring(" : [\n"); - size_t offset = buf->offset; - for (int i = 0; i < members->dim; i++) - { Dsymbol *s = (Dsymbol *)members->data[i]; - if (offset != buf->offset) - { buf->writestring(",\n"); - offset = buf->offset; - } - s->toJsonBuffer(buf); - } - JsonRemoveComma(buf); - buf->writestring("]\n"); - } - JsonRemoveComma(buf); - - buf->writestring("}\n"); -} - -void EnumMember::toJsonBuffer(OutBuffer *buf) -{ - //printf("EnumMember::toJsonBuffer()\n"); - buf->writestring("{\n"); - - JsonProperty(buf, Pname, toChars()); - JsonProperty(buf, Pkind, kind()); - - if (comment) - JsonProperty(buf, Pcomment, (const char *)comment); - - if (loc.linnum) - JsonProperty(buf, Pline, loc.linnum); - - JsonRemoveComma(buf); - buf->writestring("}\n"); -} - - + +// Compiler implementation of the D programming language +// Copyright (c) 1999-2009 by Digital Mars +// All Rights Reserved +// written by Walter Bright +// http://www.digitalmars.com +// License for redistribution is by either the Artistic License +// in artistic.txt, or the GNU General Public License in gnu.txt. +// See the included readme.txt for details. + +// This implements the JSON capability. + +#include +#include +#include +#include +#include + +#include "rmem.h" +#include "root.h" + +#include "mars.h" +#include "dsymbol.h" +#include "macro.h" +#include "template.h" +#include "lexer.h" +#include "aggregate.h" +#include "declaration.h" +#include "enum.h" +#include "id.h" +#include "module.h" +#include "scope.h" +#include "hdrgen.h" +#include "json.h" +#include "mtype.h" +#include "attrib.h" +#include "cond.h" + +const char Pname[] = "name"; +const char Pkind[] = "kind"; +const char Pfile[] = "file"; +const char Pline[] = "line"; +const char Ptype[] = "type"; +const char Pcomment[] = "comment"; +const char Pmembers[] = "members"; + +void JsonRemoveComma(OutBuffer *buf); + +void json_generate(Array *modules) +{ OutBuffer buf; + + buf.writestring("[\n"); + for (int i = 0; i < modules->dim; i++) + { Module *m = (Module *)modules->data[i]; + if (global.params.verbose) + printf("json gen %s\n", m->toChars()); + m->toJsonBuffer(&buf); + buf.writestring(",\n"); + } + JsonRemoveComma(&buf); + buf.writestring("]\n"); + + // Write buf to file + char *arg = global.params.xfilename; + if (!arg || !*arg) + { // Generate lib file name from first obj name + char *n = (char *)global.params.objfiles->data[0]; + + n = FileName::name(n); + FileName *fn = FileName::forceExt(n, global.json_ext); + arg = fn->toChars(); + } + else if (arg[0] == '-' && arg[1] == 0) + { // Write to stdout + fwrite(buf.data, 1, buf.offset, stdout); + return; + } +// if (!FileName::absolute(arg)) +// arg = FileName::combine(dir, arg); + FileName *jsonfilename = FileName::defaultExt(arg, global.json_ext); + File *jsonfile = new File(jsonfilename); + assert(jsonfile); + jsonfile->setbuffer(buf.data, buf.offset); + jsonfile->ref = 1; + char *pt = FileName::path(jsonfile->toChars()); + if (*pt) + FileName::ensurePathExists(pt); + mem.free(pt); + jsonfile->writev(); +} + + +/********************************* + * Encode string into buf, and wrap it in double quotes. + */ +void JsonString(OutBuffer *buf, const char *s) +{ + buf->writeByte('\"'); + for (; *s; s++) + { + unsigned char c = (unsigned char) *s; + switch (c) + { + case '\n': + buf->writestring("\\n"); + break; + + case '\r': + buf->writestring("\\r"); + break; + + case '\t': + buf->writestring("\\t"); + break; + + case '\"': + buf->writestring("\\\""); + break; + + case '\\': + buf->writestring("\\\\"); + break; + + case '/': + buf->writestring("\\/"); + break; + + case '\b': + buf->writestring("\\b"); + break; + + case '\f': + buf->writestring("\\f"); + break; + + default: + if (c < 0x20) + buf->printf("\\u%04x", c); + else + // Note that UTF-8 chars pass through here just fine + buf->writeByte(c); + break; + } + } + buf->writeByte('\"'); +} + +void JsonProperty(OutBuffer *buf, const char *name, const char *value) +{ + JsonString(buf, name); + buf->writestring(" : "); + JsonString(buf, value); + buf->writestring(",\n"); +} + +void JsonProperty(OutBuffer *buf, const char *name, int value) +{ + JsonString(buf, name); + buf->writestring(" : "); + buf->printf("%d", value); + buf->writestring(",\n"); +} + +void JsonRemoveComma(OutBuffer *buf) +{ + if (buf->offset >= 2 && + buf->data[buf->offset - 2] == ',' && + buf->data[buf->offset - 1] == '\n') + buf->offset -= 2; +} + +void Dsymbol::toJsonBuffer(OutBuffer *buf) +{ +} + +void Module::toJsonBuffer(OutBuffer *buf) +{ + buf->writestring("{\n"); + + if (md) + JsonProperty(buf, Pname, md->toChars()); + + JsonProperty(buf, Pkind, kind()); + + JsonProperty(buf, Pfile, srcfile->toChars()); + + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + + JsonString(buf, Pmembers); + buf->writestring(" : [\n"); + + size_t offset = buf->offset; + for (int i = 0; i < members->dim; i++) + { Dsymbol *s = (Dsymbol *)members->data[i]; + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + s->toJsonBuffer(buf); + } + + JsonRemoveComma(buf); + buf->writestring("]\n"); + + buf->writestring("}\n"); +} + +void AttribDeclaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("AttribDeclaration::toJsonBuffer()\n"); + + Array *d = include(NULL, NULL); + + if (d) + { + size_t offset = buf->offset; + for (unsigned i = 0; i < d->dim; i++) + { Dsymbol *s = (Dsymbol *)d->data[i]; + //printf("AttribDeclaration::toJsonBuffer %s\n", s->toChars()); + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + s->toJsonBuffer(buf); + } + JsonRemoveComma(buf); + } +} + + +void ConditionalDeclaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("ConditionalDeclaration::toJsonBuffer()\n"); + if (condition->inc) + { + AttribDeclaration::toJsonBuffer(buf); + } +} + + +void InvariantDeclaration::toJsonBuffer(OutBuffer *buf) { } +void DtorDeclaration::toJsonBuffer(OutBuffer *buf) { } +void StaticCtorDeclaration::toJsonBuffer(OutBuffer *buf) { } +void StaticDtorDeclaration::toJsonBuffer(OutBuffer *buf) { } +void ClassInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } +void ModuleInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } +void TypeInfoDeclaration::toJsonBuffer(OutBuffer *buf) { } +void UnitTestDeclaration::toJsonBuffer(OutBuffer *buf) { } +#if DMDV2 +void PostBlitDeclaration::toJsonBuffer(OutBuffer *buf) { } +#endif + +void Declaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("Declaration::toJsonBuffer()\n"); + buf->writestring("{\n"); + + JsonProperty(buf, Pname, toChars()); + JsonProperty(buf, Pkind, kind()); + if (type) + JsonProperty(buf, Ptype, type->toChars()); + + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + + if (loc.linnum) + JsonProperty(buf, Pline, loc.linnum); + + TypedefDeclaration *td = isTypedefDeclaration(); + if (td) + { + JsonProperty(buf, "base", td->basetype->toChars()); + } + + JsonRemoveComma(buf); + buf->writestring("}\n"); +} + +void AggregateDeclaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("AggregateDeclaration::toJsonBuffer()\n"); + buf->writestring("{\n"); + + JsonProperty(buf, Pname, toChars()); + JsonProperty(buf, Pkind, kind()); + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + if (loc.linnum) + JsonProperty(buf, Pline, loc.linnum); + + ClassDeclaration *cd = isClassDeclaration(); + if (cd) + { + if (cd->baseClass) + { + JsonProperty(buf, "base", cd->baseClass->toChars()); + } + if (cd->interfaces_dim) + { + JsonString(buf, "interfaces"); + buf->writestring(" : [\n"); + size_t offset = buf->offset; + for (int i = 0; i < cd->interfaces_dim; i++) + { BaseClass *b = cd->interfaces[i]; + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + JsonString(buf, b->base->toChars()); + } + JsonRemoveComma(buf); + buf->writestring("],\n"); + } + } + + if (members) + { + JsonString(buf, Pmembers); + buf->writestring(" : [\n"); + size_t offset = buf->offset; + for (int i = 0; i < members->dim; i++) + { Dsymbol *s = (Dsymbol *)members->data[i]; + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + s->toJsonBuffer(buf); + } + JsonRemoveComma(buf); + buf->writestring("]\n"); + } + JsonRemoveComma(buf); + + buf->writestring("}\n"); +} + +void TemplateDeclaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("TemplateDeclaration::toJsonBuffer()\n"); + + buf->writestring("{\n"); + + JsonProperty(buf, Pname, toChars()); + JsonProperty(buf, Pkind, kind()); + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + + if (loc.linnum) + JsonProperty(buf, Pline, loc.linnum); + + JsonString(buf, Pmembers); + buf->writestring(" : [\n"); + size_t offset = buf->offset; + for (int i = 0; i < members->dim; i++) + { Dsymbol *s = (Dsymbol *)members->data[i]; + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + s->toJsonBuffer(buf); + } + JsonRemoveComma(buf); + buf->writestring("]\n"); + + buf->writestring("}\n"); +} + +void EnumDeclaration::toJsonBuffer(OutBuffer *buf) +{ + //printf("EnumDeclaration::toJsonBuffer()\n"); + if (isAnonymous()) + { + if (members) + { + for (int i = 0; i < members->dim; i++) + { + Dsymbol *s = (Dsymbol *)members->data[i]; + s->toJsonBuffer(buf); + buf->writestring(",\n"); + } + JsonRemoveComma(buf); + } + return; + } + + buf->writestring("{\n"); + + JsonProperty(buf, Pname, toChars()); + JsonProperty(buf, Pkind, kind()); + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + + if (loc.linnum) + JsonProperty(buf, Pline, loc.linnum); + + if (memtype) + JsonProperty(buf, "base", memtype->toChars()); + + if (members) + { + JsonString(buf, Pmembers); + buf->writestring(" : [\n"); + size_t offset = buf->offset; + for (int i = 0; i < members->dim; i++) + { Dsymbol *s = (Dsymbol *)members->data[i]; + if (offset != buf->offset) + { buf->writestring(",\n"); + offset = buf->offset; + } + s->toJsonBuffer(buf); + } + JsonRemoveComma(buf); + buf->writestring("]\n"); + } + JsonRemoveComma(buf); + + buf->writestring("}\n"); +} + +void EnumMember::toJsonBuffer(OutBuffer *buf) +{ + //printf("EnumMember::toJsonBuffer()\n"); + buf->writestring("{\n"); + + JsonProperty(buf, Pname, toChars()); + JsonProperty(buf, Pkind, kind()); + + if (comment) + JsonProperty(buf, Pcomment, (const char *)comment); + + if (loc.linnum) + JsonProperty(buf, Pline, loc.linnum); + + JsonRemoveComma(buf); + buf->writestring("}\n"); +} + + diff --git a/src/root/aav.c b/src/root/aav.c index f3036701dc54..a4873bc7db8c 100644 --- a/src/root/aav.c +++ b/src/root/aav.c @@ -1,188 +1,188 @@ -/** - * Implementation of associative arrays. - * - */ - -#include -#include -#include - -#include "aav.h" - -static const size_t prime_list[] = { - 31UL, - 97UL, 389UL, - 1543UL, 6151UL, - 24593UL, 98317UL, - 393241UL, 1572869UL, - 6291469UL, 25165843UL, - 100663319UL, 402653189UL, - 1610612741UL, 4294967291UL, -}; - -struct aaA -{ - aaA *next; - Key key; - Value value; -}; - -struct AA -{ - aaA* *b; - size_t b_length; - size_t nodes; // total number of aaA nodes - aaA* binit[4]; // initial value of b[] -}; - -static const AA bbinit = { NULL, }; - -/**************************************************** - * Determine number of entries in associative array. - */ - -size_t _aaLen(AA* aa) -{ - return aa ? aa->nodes : 0; -} - - -/************************************************* - * Get pointer to value in associative array indexed by key. - * Add entry for key if it is not already there. - */ - -Value* _aaGet(AA** paa, Key key) -{ - //printf("paa = %p\n", paa); - - if (!*paa) - { AA *a = new AA(); - *a = bbinit; - a->b = a->binit; - a->b_length = sizeof(a->binit) / sizeof(a->binit[0]); - *paa = a; - assert((*paa)->b_length == 4); - } - //printf("paa = %p, *paa = %p\n", paa, *paa); - - assert((*paa)->b_length); - size_t i = (size_t)key % (*paa)->b_length; - aaA** pe = &(*paa)->b[i]; - aaA *e; - while ((e = *pe) != NULL) - { - if (key == e->key) - return &e->value; - pe = &e->next; - } - - // Not found, create new elem - //printf("create new one\n"); - e = new aaA(); - e->next = NULL; - e->key = key; - e->value = NULL; - *pe = e; - - size_t nodes = ++(*paa)->nodes; - //printf("length = %d, nodes = %d\n", paa.a.b.length, nodes); - if (nodes > (*paa)->b_length * 4) - { - //printf("rehash\n"); - _aaRehash(paa); - } - - return &e->value; -} - - -/************************************************* - * Get value in associative array indexed by key. - * Returns NULL if it is not already there. - */ - -Value _aaGetRvalue(AA* aa, Key key) -{ - //printf("_aaGetRvalue(key = %p)\n", key); - if (!aa) - return NULL; - - size_t len = aa->b_length; - - if (len) - { - size_t i = (size_t)key % len; - aaA* e = aa->b[i]; - while (e) - { - if (key == e->key) - return e->value; - e = e->next; - } - } - return NULL; // not found -} - - -/******************************************** - * Rehash an array. - */ - -void _aaRehash(AA** paa) -{ - //printf("Rehash\n"); - if (*paa) - { - AA newb = bbinit; - AA *aa = *paa; - size_t len = _aaLen(*paa); - if (len) - { size_t i; - - for (i = 0; i < sizeof(prime_list)/sizeof(prime_list[0]) - 1; i++) - { - if (len <= prime_list[i]) - break; - } - len = prime_list[i]; - newb.b = new aaA*[len]; - memset(newb.b, 0, len * sizeof(aaA*)); - newb.b_length = len; - - for (size_t k = 0; k < aa->b_length; k++) - { aaA *e = aa->b[k]; - while (e) - { aaA* enext = e->next; - size_t j = (size_t)e->key % len; - e->next = newb.b[j]; - newb.b[j] = e; - e = enext; - } - } - if (aa->b != aa->binit) - delete aa->b; - - newb.nodes = aa->nodes; - } - - **paa = newb; - } -} - - -#if UNITTEST - -void unittest_aa() -{ - AA* aa = NULL; - Value v = _aaGetRvalue(aa, NULL); - assert(!v); - Value *pv = _aaGet(&aa, NULL); - assert(pv); - *pv = (void *)3; - v = _aaGetRvalue(aa, NULL); - assert(v == (void *)3); -} - -#endif +/** + * Implementation of associative arrays. + * + */ + +#include +#include +#include + +#include "aav.h" + +static const size_t prime_list[] = { + 31UL, + 97UL, 389UL, + 1543UL, 6151UL, + 24593UL, 98317UL, + 393241UL, 1572869UL, + 6291469UL, 25165843UL, + 100663319UL, 402653189UL, + 1610612741UL, 4294967291UL, +}; + +struct aaA +{ + aaA *next; + Key key; + Value value; +}; + +struct AA +{ + aaA* *b; + size_t b_length; + size_t nodes; // total number of aaA nodes + aaA* binit[4]; // initial value of b[] +}; + +static const AA bbinit = { NULL, }; + +/**************************************************** + * Determine number of entries in associative array. + */ + +size_t _aaLen(AA* aa) +{ + return aa ? aa->nodes : 0; +} + + +/************************************************* + * Get pointer to value in associative array indexed by key. + * Add entry for key if it is not already there. + */ + +Value* _aaGet(AA** paa, Key key) +{ + //printf("paa = %p\n", paa); + + if (!*paa) + { AA *a = new AA(); + *a = bbinit; + a->b = a->binit; + a->b_length = sizeof(a->binit) / sizeof(a->binit[0]); + *paa = a; + assert((*paa)->b_length == 4); + } + //printf("paa = %p, *paa = %p\n", paa, *paa); + + assert((*paa)->b_length); + size_t i = (size_t)key % (*paa)->b_length; + aaA** pe = &(*paa)->b[i]; + aaA *e; + while ((e = *pe) != NULL) + { + if (key == e->key) + return &e->value; + pe = &e->next; + } + + // Not found, create new elem + //printf("create new one\n"); + e = new aaA(); + e->next = NULL; + e->key = key; + e->value = NULL; + *pe = e; + + size_t nodes = ++(*paa)->nodes; + //printf("length = %d, nodes = %d\n", paa.a.b.length, nodes); + if (nodes > (*paa)->b_length * 4) + { + //printf("rehash\n"); + _aaRehash(paa); + } + + return &e->value; +} + + +/************************************************* + * Get value in associative array indexed by key. + * Returns NULL if it is not already there. + */ + +Value _aaGetRvalue(AA* aa, Key key) +{ + //printf("_aaGetRvalue(key = %p)\n", key); + if (!aa) + return NULL; + + size_t len = aa->b_length; + + if (len) + { + size_t i = (size_t)key % len; + aaA* e = aa->b[i]; + while (e) + { + if (key == e->key) + return e->value; + e = e->next; + } + } + return NULL; // not found +} + + +/******************************************** + * Rehash an array. + */ + +void _aaRehash(AA** paa) +{ + //printf("Rehash\n"); + if (*paa) + { + AA newb = bbinit; + AA *aa = *paa; + size_t len = _aaLen(*paa); + if (len) + { size_t i; + + for (i = 0; i < sizeof(prime_list)/sizeof(prime_list[0]) - 1; i++) + { + if (len <= prime_list[i]) + break; + } + len = prime_list[i]; + newb.b = new aaA*[len]; + memset(newb.b, 0, len * sizeof(aaA*)); + newb.b_length = len; + + for (size_t k = 0; k < aa->b_length; k++) + { aaA *e = aa->b[k]; + while (e) + { aaA* enext = e->next; + size_t j = (size_t)e->key % len; + e->next = newb.b[j]; + newb.b[j] = e; + e = enext; + } + } + if (aa->b != aa->binit) + delete aa->b; + + newb.nodes = aa->nodes; + } + + **paa = newb; + } +} + + +#if UNITTEST + +void unittest_aa() +{ + AA* aa = NULL; + Value v = _aaGetRvalue(aa, NULL); + assert(!v); + Value *pv = _aaGet(&aa, NULL); + assert(pv); + *pv = (void *)3; + v = _aaGetRvalue(aa, NULL); + assert(v == (void *)3); +} + +#endif diff --git a/src/unittests.c b/src/unittests.c index 8de73f85dbd0..1b3c2770293f 100644 --- a/src/unittests.c +++ b/src/unittests.c @@ -1,17 +1,17 @@ - -#include - -#include "mars.h" - -void unittest_speller(); -void unittest_importHint(); -void unittest_aa(); - -void unittests() -{ -#if UNITTEST - unittest_speller(); - unittest_importHint(); - unittest_aa(); -#endif -} + +#include + +#include "mars.h" + +void unittest_speller(); +void unittest_importHint(); +void unittest_aa(); + +void unittests() +{ +#if UNITTEST + unittest_speller(); + unittest_importHint(); + unittest_aa(); +#endif +}