void EnumDeclaration::semantic(Scope *sc) { //printf("EnumDeclaration::semantic(sd = %p, '%s') %s\n", sc->scopesym, sc->scopesym->toChars(), toChars()); //printf("EnumDeclaration::semantic() %p %s\n", this, toChars()); if (semanticRun >= PASSsemanticdone) return; // semantic() already completed if (semanticRun == PASSsemantic) { assert(memtype); ::error(loc, "circular reference to enum base type %s", memtype->toChars()); errors = true; semanticRun = PASSsemanticdone; return; } unsigned dprogress_save = Module::dprogress; Scope *scx = NULL; if (scope) { sc = scope; scx = scope; // save so we don't make redundant copies scope = NULL; } parent = sc->parent; type = type->semantic(loc, sc); protection = sc->protection; if (sc->stc & STCdeprecated) isdeprecated = true; userAttribDecl = sc->userAttribDecl; semanticRun = PASSsemantic; if (!members && !memtype) // enum ident; { semanticRun = PASSsemanticdone; return; } if (!symtab) symtab = new DsymbolTable(); /* The separate, and distinct, cases are: * 1. enum { ... } * 2. enum : memtype { ... } * 3. enum ident { ... } * 4. enum ident : memtype { ... } * 5. enum ident : memtype; * 6. enum ident; */ if (memtype) { memtype = memtype->semantic(loc, sc); /* Check to see if memtype is forward referenced */ if (memtype->ty == Tenum) { EnumDeclaration *sym = (EnumDeclaration *)memtype->toDsymbol(sc); if (!sym->memtype || !sym->members || !sym->symtab || sym->scope) { // memtype is forward referenced, so try again later scope = scx ? scx : sc->copy(); scope->setNoFree(); scope->module->addDeferredSemantic(this); Module::dprogress = dprogress_save; //printf("\tdeferring %s\n", toChars()); semanticRun = PASSinit; return; } } if (memtype->ty == Tvoid) { error("base type must not be void"); memtype = Type::terror; } if (memtype->ty == Terror) { errors = true; if (members) { for (size_t i = 0; i < members->dim; i++) { Dsymbol *s = (*members)[i]; s->errors = true; // poison all the members } } semanticRun = PASSsemanticdone; return; } } semanticRun = PASSsemanticdone; if (!members) // enum ident : memtype; return; if (members->dim == 0) { error("enum %s must have at least one member", toChars()); errors = true; return; } Module::dprogress++; Scope *sce; if (isAnonymous()) sce = sc; else { sce = sc->push(this); sce->parent = this; } sce = sce->startCTFE(); sce->setNoFree(); // needed for getMaxMinValue() /* Each enum member gets the sce scope */ for (size_t i = 0; i < members->dim; i++) { EnumMember *em = (*members)[i]->isEnumMember(); if (em) em->scope = sce; } if (!added) { /* addMember() is not called when the EnumDeclaration appears as a function statement, * so we have to do what addMember() does and install the enum members in the right symbol * table */ ScopeDsymbol *scopesym = NULL; if (isAnonymous()) { /* Anonymous enum members get added to enclosing scope. */ for (Scope *sct = sce; 1; sct = sct->enclosing) { assert(sct); if (sct->scopesym) { scopesym = sct->scopesym; if (!sct->scopesym->symtab) sct->scopesym->symtab = new DsymbolTable(); break; } } } else { // Otherwise enum members are in the EnumDeclaration's symbol table scopesym = this; } for (size_t i = 0; i < members->dim; i++) { EnumMember *em = (*members)[i]->isEnumMember(); if (em) { em->ed = this; em->addMember(sc, scopesym, 1); } } } for (size_t i = 0; i < members->dim; i++) { EnumMember *em = (*members)[i]->isEnumMember(); if (em) em->semantic(em->scope); } //printf("defaultval = %lld\n", defaultval); //if (defaultval) printf("defaultval: %s %s\n", defaultval->toChars(), defaultval->type->toChars()); //printf("members = %s\n", members->toChars()); }
void EnumMember::semantic(Scope *sc) { //printf("EnumMember::semantic() %s\n", toChars()); if (errors || semanticRun >= PASSsemanticdone) return; if (semanticRun == PASSsemantic) { error("circular reference to enum member"); Lerrors: errors = true; semanticRun = PASSsemanticdone; return; } assert(ed); ed->semantic(sc); if (ed->errors) goto Lerrors; if (errors || semanticRun >= PASSsemanticdone) return; semanticRun = PASSsemantic; if (scope) sc = scope; // The first enum member is special bool first = (this == (*ed->members)[0]); if (type) { type = type->semantic(loc, sc); assert(value); // "type id;" is not a valid enum member declaration } if (value) { Expression *e = value; assert(e->dyncast() == DYNCAST_EXPRESSION); e = e->semantic(sc); e = resolveProperties(sc, e); e = e->ctfeInterpret(); if (e->op == TOKerror) goto Lerrors; if (first && !ed->memtype && !ed->isAnonymous()) { ed->memtype = e->type; if (ed->memtype->ty == Terror) { ed->errors = true; goto Lerrors; } if (ed->memtype->ty != Terror) { /* Bugzilla 11746: All of named enum members should have same type * with the first member. If the following members were referenced * during the first member semantic, their types should be unified. */ for (size_t i = 0; i < ed->members->dim; i++) { EnumMember *em = (*ed->members)[i]->isEnumMember(); if (!em || em == this || em->semanticRun < PASSsemanticdone || em->type) continue; //printf("[%d] em = %s, em->semanticRun = %d\n", i, toChars(), em->semanticRun); Expression *e = em->value; e = e->implicitCastTo(sc, ed->memtype); e = e->ctfeInterpret(); e = e->castTo(sc, ed->type); if (e->op == TOKerror) ed->errors = true; em->value = e; } if (ed->errors) { ed->memtype = Type::terror; goto Lerrors; } } } if (ed->memtype && !type) { 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); } else if (type) { e = e->implicitCastTo(sc, type); e = e->ctfeInterpret(); assert(ed->isAnonymous()); // save origValue for better json output origValue = e; } value = e; } else if (first) { Type *t; if (ed->memtype) t = ed->memtype; else { t = Type::tint32; if (!ed->isAnonymous()) ed->memtype = t; } 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; } else { /* Find the previous enum member, * and set this to be the previous value + 1 */ EnumMember *emprev = NULL; for (size_t i = 0; i < ed->members->dim; i++) { EnumMember *em = (*ed->members)[i]->isEnumMember(); if (em) { if (em == this) break; emprev = em; } } assert(emprev); if (emprev->semanticRun < PASSsemanticdone) // if forward reference emprev->semantic(emprev->scope); // resolve it if (emprev->errors) goto Lerrors; Expression *eprev = emprev->value; Type *tprev = eprev->type->equals(ed->type) ? ed->memtype : eprev->type; Expression *emax = tprev->getProperty(ed->loc, Id::max, 0); emax = emax->semantic(sc); emax = emax->ctfeInterpret(); // Set value to (eprev + 1). // But first check that (eprev != emax) assert(eprev); Expression *e = new EqualExp(TOKequal, loc, eprev, emax); e = e->semantic(sc); e = e->ctfeInterpret(); if (e->toInteger()) { error("initialization with (%s.%s + 1) causes overflow for type '%s'", emprev->ed->toChars(), emprev->toChars(), ed->type->toBasetype()->toChars()); goto Lerrors; } // Now set e to (eprev + 1) e = new AddExp(loc, eprev, new IntegerExp(loc, 1, Type::tint32)); e = e->semantic(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()) { // Check that e != eprev (not always true for floats) Expression *etest = new EqualExp(TOKequal, loc, e, eprev); etest = etest->semantic(sc); etest = etest->ctfeInterpret(); if (etest->toInteger()) { error("has inexact value, due to loss of precision"); goto Lerrors; } } value = e; } assert(origValue); semanticRun = PASSsemanticdone; }