Skip to content

Commit

Permalink
Merge pull request #3472 from AndrejMitrovic/Fix11620
Browse files Browse the repository at this point in the history
Issue 11620 - Emit enum member values to json output.
  • Loading branch information
9rnsr committed Apr 24, 2014
2 parents 9871b57 + 6fb1c2b commit 6642adc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/enum.c
Expand Up @@ -513,6 +513,7 @@ EnumMember::EnumMember(Loc loc, Identifier *id, Expression *value, Type *type)
{
this->ed = NULL;
this->value = value;
this->origValue = value;
this->type = type;
this->loc = loc;
this->vd = NULL;
Expand All @@ -534,9 +535,13 @@ Dsymbol *EnumMember::syntaxCopy(Dsymbol *s)
em->loc = loc;
em->value = e;
em->type = t;
em->origValue = origValue ? origValue->syntaxCopy() : NULL;
}
else
{
em = new EnumMember(loc, ident, e, t);
em->origValue = origValue ? origValue->syntaxCopy() : NULL;
}
return em;
}

Expand Down Expand Up @@ -613,6 +618,10 @@ void EnumMember::semantic(Scope *sc)
{
e = e->implicitCastTo(sc, ed->memtype);
e = e->ctfeInterpret();

// save origValue for better json output
origValue = e;

if (!ed->isAnonymous())
e = e->castTo(sc, ed->type);
}
Expand All @@ -621,6 +630,9 @@ void EnumMember::semantic(Scope *sc)
e = e->implicitCastTo(sc, type);
e = e->ctfeInterpret();
assert(ed->isAnonymous());

// save origValue for better json output
origValue = e;
}
value = e;
}
Expand All @@ -638,6 +650,10 @@ void EnumMember::semantic(Scope *sc)
Expression *e = new IntegerExp(loc, 0, Type::tint32);
e = e->implicitCastTo(sc, t);
e = e->ctfeInterpret();

// save origValue for better json output
origValue = e;

if (!ed->isAnonymous())
e = e->castTo(sc, ed->type);
value = e;
Expand Down Expand Up @@ -689,6 +705,15 @@ void EnumMember::semantic(Scope *sc)
e = e->castTo(sc, eprev->type);
e = e->ctfeInterpret();

// save origValue (without cast) for better json output
if (e->op != TOKerror) // avoid duplicate diagnostics
{
assert(emprev->origValue);
origValue = new AddExp(loc, emprev->origValue, new IntegerExp(loc, 1, Type::tint32));
origValue = origValue->semantic(sc);
origValue = origValue->ctfeInterpret();
}

if (e->op == TOKerror)
goto Lerrors;
if (e->type->isfloating())
Expand All @@ -706,6 +731,7 @@ void EnumMember::semantic(Scope *sc)
value = e;
}

assert(origValue);
semanticRun = PASSsemanticdone;
}

Expand Down
3 changes: 3 additions & 0 deletions src/enum.h
Expand Up @@ -84,6 +84,9 @@ class EnumMember : public Dsymbol
* 3. type id = value
*/
Expression *value;
Expression *origValue; // A cast() is injected to 'value' after semantic(),
// but 'origValue' will preserve the original value,
// or previous value + 1 if none was specified.
Type *type;

EnumDeclaration *ed;
Expand Down
6 changes: 6 additions & 0 deletions src/json.c
Expand Up @@ -451,6 +451,12 @@ class ToJsonVisitor : public Visitor
if (s->prot() != PROTpublic)
property("protection", Pprotectionnames[s->prot()]);

if (EnumMember *em = s->isEnumMember())
{
if (em->origValue)
property("value", em->origValue->toChars());
}

property("comment", (const char *)s->comment);

property("line", "char", &s->loc);
Expand Down
58 changes: 58 additions & 0 deletions test/compilable/extra-files/json.out
Expand Up @@ -507,6 +507,64 @@
"deco" : "xC6Object",
"originalType" : "Object",
"init" : "Object()"
},
{
"name" : "Numbers",
"kind" : "enum",
"line" : 101,
"char" : 1,
"baseDeco" : "i",
"members" : [
{
"name" : "unspecified1",
"kind" : "enum member",
"value" : "0",
"line" : 103,
"char" : 5
},
{
"name" : "one",
"kind" : "enum member",
"value" : "2",
"line" : 104,
"char" : 5
},
{
"name" : "two",
"kind" : "enum member",
"value" : "3",
"line" : 105,
"char" : 5
},
{
"name" : "FILE_NOT_FOUND",
"kind" : "enum member",
"value" : "101",
"line" : 106,
"char" : 5
},
{
"name" : "unspecified3",
"kind" : "enum member",
"value" : "102",
"line" : 107,
"char" : 5
},
{
"name" : "unspecified4",
"kind" : "enum member",
"value" : "103",
"line" : 108,
"char" : 5
},
{
"name" : "four",
"kind" : "enum member",
"value" : "4",
"line" : 109,
"char" : 5
}
]
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions test/compilable/json.d
Expand Up @@ -96,3 +96,15 @@ class C_9755

/** Issue 10011 - init property is wrong for object initializer. */
const Object c_10011 = new Object();

///
enum Numbers
{
unspecified1,
one = 2,
two = 3,
FILE_NOT_FOUND = 101,
unspecified3,
unspecified4,
four = 4,
}

0 comments on commit 6642adc

Please sign in to comment.