+2004-01-06 Erik Petrich <epetrich@ivorytower.norman.ok.us>
+
+ * src/SDCC.y (enum_specifier, enumerator_list, opt_assign_expr),
+ * src/SDCCsymt.c (newEnumType),
+ * src/SDCCsymt.h
+ * support/Util/SDCCerr.c,
+ * support/Util/SDCCerr.h: fixed bug #871258 and some other unreported
+ enum related bugs.
+ * support/regression/tests/enum.c: added test for enum values that
+ require at least 2 bytes of storage.
+
2004-01-06 Vangelis Rokas <vrokas@otenet.gr>
* src/common.h: added ifndef/define/endif macros
}
if (IS_SPEC(sym->etype) && SPEC_SCLS(sym->etype)) {
werrorfl(filename, sym->lineDef, E_NOT_ALLOWED, "storage class");
+ printTypeChainRaw (sym->type,NULL);
SPEC_SCLS(sym->etype) = 0;
}
for (dsym=sym->next; dsym; dsym=dsym->next) {
enum_specifier
: ENUM '{' enumerator_list '}' {
- symbol *sym, *dsym;
- char _error=0;
-
- // check for duplicate enums
- for (sym=$3; sym; sym=sym->next) {
- for (dsym=sym->next; dsym; dsym=dsym->next) {
- if (strcmp(sym->name, dsym->name)==0) {
- werrorfl(filename, sym->lineDef, E_DUPLICATE_MEMBER, "enum", sym->name);
- _error++;
- }
- }
- }
- if (_error==0) {
- $$ = copyLinkChain(cenum->type);
- } else {
- $$ = newIntLink();
- SPEC_NOUN($$)=0;
- }
+ $$ = newEnumType ($3); //copyLinkChain(cenum->type);
+ SPEC_SCLS(getSpec($$)) = 0;
}
| ENUM identifier '{' enumerator_list '}' {
symbol *csym ;
+ sym_link *enumtype;
+
+ csym=findSym(enumTab,$2,$2->name);
+ if ((csym && csym->level == $2->level))
+ werrorfl(filename, $2->lineDef, E_DUPLICATE_TYPEDEF,csym->name);
- $2->type = copyLinkChain(cenum->type);
- $2->etype = getSpec($2->type);
- /* add this to the enumerator table */
- if (!(csym=findSym(enumTab,$2,$2->name)) &&
- (csym && csym->level == $2->level))
- werror(E_DUPLICATE_TYPEDEF,csym->name);
+ enumtype = newEnumType ($4); //copyLinkChain(cenum->type);
+ SPEC_SCLS(getSpec(enumtype)) = 0;
+ $2->type = enumtype;
- addSym ( enumTab,$2,$2->name,$2->level,$2->block, 0);
- //addSymChain ($4);
- //allocVariables (reverseSyms($4));
- $$ = copyLinkChain(cenum->type);
- SPEC_SCLS(getSpec($$)) = 0 ;
+ /* add this to the enumerator table */
+ if (!csym)
+ addSym ( enumTab,$2,$2->name,$2->level,$2->block, 0);
+ $$ = copyLinkChain(enumtype);
}
| ENUM identifier {
symbol *csym ;
$$ = newLink(SPECIFIER) ;
SPEC_NOUN($$) = V_INT ;
}
-
- SPEC_SCLS(getSpec($$)) = 0 ;
}
;
: enumerator
| enumerator_list ',' {
}
- | enumerator_list ',' enumerator {
- $3->next = $1 ;
- $$ = $3 ;
- }
+ | enumerator_list ',' enumerator
+ {
+ symbol *dsym;
+
+ for (dsym=$1; dsym; dsym=dsym->next)
+ {
+ if (strcmp($3->name, dsym->name)==0)
+ werrorfl(filename, $3->lineDef, E_DUPLICATE_MEMBER, "enum", $3->name);
+ }
+
+ $3->next = $1 ;
+ $$ = $3 ;
+ }
;
enumerator
opt_assign_expr
: '=' constant_expr {
value *val ;
-
- val = constExprValue($2,TRUE);
+
+ val = constExprValue($2,TRUE);
+ if (!IS_INT(val->type) && !IS_CHAR(val->type))
+ {
+ werror(E_ENUM_NON_INTEGER);
+ SNPRINTF(lbuff, sizeof(lbuff),
+ "%d",(int) floatFromVal(val));
+ val = constVal(lbuff);
+ }
$$ = cenum = val ;
}
| {
exit(-1);
return l; // never reached, makes compiler happy.
}
+
+/*--------------------------------------------------------------------*/
+/* newEnumType - create an integer type compatible with enumerations */
+/*--------------------------------------------------------------------*/
+sym_link *
+newEnumType (symbol *enumlist)
+{
+ int min, max, v;
+ symbol *sym;
+ sym_link *type;
+
+ if (!enumlist)
+ {
+ type = newLink (SPECIFIER);
+ SPEC_NOUN (type) = V_INT;
+ return type;
+ }
+
+ /* Determine the range of the enumerated values */
+ sym = enumlist;
+ min = max = (int) floatFromVal (valFromType (sym->type));
+ for (sym = sym->next; sym; sym = sym->next)
+ {
+ v = (int) floatFromVal (valFromType (sym->type));
+ if (v<min)
+ min = v;
+ if (v>max)
+ max = v;
+ }
+
+ /* Determine the smallest integer type that is compatible with this range */
+ type = newLink (SPECIFIER);
+ if (min>=0 && max<=255)
+ {
+ SPEC_NOUN (type) = V_CHAR;
+ SPEC_USIGN (type) = 1;
+ }
+ else if (min>=-128 && max<=127)
+ {
+ SPEC_NOUN (type) = V_CHAR;
+ }
+ else if (min>=0 && max<=65535)
+ {
+ SPEC_NOUN (type) = V_INT;
+ SPEC_USIGN (type) = 1;
+ }
+ else if (min>=-32768 && max<=32767)
+ {
+ SPEC_NOUN (type) = V_INT;
+ }
+ else
+ {
+ SPEC_NOUN (type) = V_INT;
+ SPEC_LONG (type) = 1;
+ if (min>=0)
+ SPEC_USIGN (type) = 1;
+ }
+
+ return type;
+}