shithub: scc

Download patch

ref: 585dc6d025dabe3e95271084e27e6c833c8c3dbf
parent: 0e78d39ed924847e4e78662581c43ffdbe0d1ef4
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Jul 22 06:51:43 EDT 2015

Make enumerations usable

--- a/cc1/TODO
+++ b/cc1/TODO
@@ -2,7 +2,6 @@
 * Verify correctness in initializators
 * emit initializators
 * emit structures definition
-* Assign const expression value to enum members
 * Define array types based in the value of constant expressions
 * Rewrite decl.c and use only one decl function with a function pointer
   parameter
@@ -13,7 +12,6 @@
 * Rewrite error recovery code, and ensure correct state after recovery
 * Allow comparisions between pointers and 0
 * Implement function calls
-* Implement enum type in eqtype()
 * Parse correctly all integer and float constants
 * Add C99 features (almost all the new features of C99 are missed)
 * Add correct emit for any kind of constant
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -130,7 +130,8 @@
 	ISFIELD    =      32,
 	ISPARAM    =      64,
 	ISEXTERN   =     128,
-	ISUSED     =     256
+	ISUSED     =     256,
+	ISCONSTANT =     512
 };
 
 
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -295,7 +295,7 @@
 newtag(void)
 {
 	Symbol *sym;
-	unsigned tag = yylval.token;
+	int op, tag = yylval.token;
 	static unsigned ns = NS_STRUCTS;
 
 	setnamespace(NS_TAG);
@@ -304,6 +304,7 @@
 	case IDEN:
 	case TYPEIDEN:
 		sym = yylval.sym;
+		install(NS_TAG);
 		next();
 		break;
 	default:
@@ -318,8 +319,8 @@
 	}
 
 	sym->flags |= ISDEFINED;
-	if (sym->type->op != tag)
-		error("'%s' defined as wrong kind of tag", yytext);
+	if ((op = sym->type->op) != tag &&  op != INT)
+		error("'%s' defined as wrong kind of tag", sym->name);
 	return sym;
 }
 
@@ -390,28 +391,36 @@
 enumdcl(void)
 {
 	Type *tp;
-	Symbol *sym;
-	int val = 0;
+	Symbol *sym, *tagsym;
+	int val;
 
-	tp = newtag()->type;
+	tagsym = newtag();
+	tp = tagsym->type;
 
-	if (yytoken == ';')
+	if (!accept('{'))
 		return tp;
-
-	expect('{');
 	if (tp->defined)
-		error("redefinition of enumeration '%s'", yytext);
+		error("redefinition of enumeration '%s'", tagsym->name);
 	tp->defined = 1;
-	while (yytoken != '}') {
+	for (val = 0; yytoken != ')'; ++val) {
 		if (yytoken != IDEN)
 			unexpected();
 		if ((sym = install(NS_IDEN)) == NULL)
 			error("'%s' redeclared as different kind of symbol", yytext);
 		next();
+		sym->flags |= ISCONSTANT;
 		sym->type = inttype;
-		if (accept('='))
-			constexpr();
-		sym->u.i = val++;
+		if (accept('=')) {
+			Node *np = constexpr();
+			/*
+			 * TODO: check that the type of the constant
+			 * expression is the correct, that in this
+			 * case should be int
+			 */
+			val = np->sym->u.i;
+			freetree(np);
+		}
+		sym->u.i = val;
 		if (!accept(','))
 			break;
 	}
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -231,6 +231,7 @@
 	tp = ctype(INT, sign, size);
 	sym = newsym(NS_IDEN);
 	sym->type = tp;
+	sym->flags |= ISCONSTANT;
 	v = strtol(s, NULL, base);
 	if (tp == inttype)
 		sym->u.i = v;
@@ -358,6 +359,7 @@
 
 	yylen = bp - yytext + 1;
 	yylval.sym = newsym(NS_IDEN);
+	yylval.sym->flags |= ISCONSTANT;
 	yylval.sym->u.s = xstrdup(yytext+1);
 	yylval.sym->type = mktype(chartype, ARY, yylen - 2, NULL);
 	*bp++ = '"';
@@ -387,6 +389,8 @@
 		 */
 		sym = nextsym(sym, lex_ns);
 	}
+	if (sym->flags & ISCONSTANT)
+		return CONSTANT;
 	if (sym->token != IDEN)
 		yylval.token = sym->u.token;
 	return sym->token;
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -292,10 +292,25 @@
 	type.n.elem = nelem;
 	type.ns = 0;
 
-	if (op == ARY && nelem == 0 || op == STRUCT || op == UNION)
-		type.defined = 0;
-	else
+
+	switch (op) {
+	case ARY:
+		if (nelem == 0)
+			goto no_defined;
+		/* PASSTROUGH */
+	case FTN:
+	case PTR:
 		type.defined = 1;
+		break;
+	case ENUM:
+		type.printed = 1;
+		/* PASSTROUGH */
+	case STRUCT:
+	case UNION:
+	no_defined:
+		type.defined = 0;
+		break;
+	}
 
 	t = (op ^ (uintptr_t) tp >> 3) & NR_TYPE_HASH-1;
 	tbl = &typetab[t];
@@ -336,7 +351,7 @@
 		}
 		return 1;
 	case ENUM:
-		/* TODO: Check when two enum are the same type */
+		break;
 	case INT: case FLOAT:
 		return tp1->letter == tp2->letter;
 	default: