ref: 6f2043e9eb7f2eb1a4ab6d4ac58d59f2e36f77a6
parent: b9df40911c422cfbfddcda16321dab9736e9d1c1
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu May 29 04:50:41 EDT 2014
Merge common code in assign and incdec This functions must check if the left value is a correct one, so this part is common to them, so the best option is to create a new function.
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -58,6 +58,17 @@
*p2 = np2;
}
+static void
+chklvalue(Node *np, Type *tp)
+{
+ if (!np->b.lvalue)
+ error("lvalue required in operation");
+ if (np->utype == voidtype)
+ error("invalid use of void expression");
+ if (isconst(tp->op))
+ error("const value modified");
+}
+
Node *
eval(Node *np)
{
@@ -341,12 +352,7 @@
{
Type *tp = np->utype;
- if (!np->b.lvalue)
- error("lvalue required in operation");
- if (np->utype == voidtype)
- error("invalid use of void expression");
- if (isconst(tp->op))
- error("const value modified");
+ chklvalue(np, np->utype);
switch (np->typeop) {
case PTR:
@@ -533,7 +539,7 @@
switch (yytoken) {
case '*': op = OMUL; fun = arithmetic; break;
case '/': op = ODIV; fun = arithmetic; break;
- case '%': op = OMOD; fun = integerop; break;
+ case '%': op = OMOD; fun = integerop; break;
default: return np;
}
next();
@@ -708,12 +714,7 @@
case OR_EQ: op = OA_OR; fun = integerop; break;
default: return np;
}
- if (!np->b.lvalue)
- error("lvalue required as left operand of assignment");
- if (np->utype == voidtype)
- error("invalid use of void expression");
- if (isconst(np->type->op))
- error("const value modified");
+ chklvalue(np, np->type);
next();
np = (fun)(op, np, eval(assign()));
}
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -29,16 +29,12 @@
{
Symbol *sym;
- if (s) {
- if ((sym = lookup(s, NS_LABEL)) != NULL) {
- if (define && sym->s.isdefined)
- error("label '%s' already defined", s);
- else
- sym->s.isdefined = 1;
- return sym;
- }
- } else {
- s = "";
+ if ((sym = lookup(s, NS_LABEL)) != NULL) {
+ if (define && sym->s.isdefined)
+ error("label '%s' already defined", s);
+ else
+ sym->s.isdefined = 1;
+ return sym;
}
sym = install(s, NS_LABEL);
@@ -72,9 +68,9 @@
Symbol *begin, *cond, *end;
Node *np;
- begin = label(NULL, 1);
- end = label(NULL, 1);
- cond = label(NULL, 1);
+ begin = label("", 1);
+ end = label("", 1);
+ cond = label("", 1);
expect(WHILE);
np = condition();
@@ -94,9 +90,9 @@
Symbol *begin, *cond, *end;
Node *econd = NULL, *einc = NULL;
- begin = label(NULL, 1);
- end = label(NULL, 1);
- cond = label(NULL, 1);
+ begin = label("", 1);
+ end = label("", 1);
+ cond = label("", 1);
expect(FOR);
expect('(');
@@ -124,9 +120,8 @@
static void
Dowhile(Caselist *lswitch)
{
- Symbol *begin= label(NULL, 1), *end = label(NULL, 1);
+ Symbol *begin = label("", 1), *end = label("", 1);
-
expect(DO);
emitbloop();
emitlabel(begin);
@@ -206,7 +201,7 @@
{
Caselist lcase = {.nr = 0, .head = NULL, .deflabel = NULL};
struct scase *p;
- Symbol *lbreak = label(NULL, 1), *lcond = label(NULL, 1);
+ Symbol *lbreak = label("", 1), *lcond = label("", 1);
Node *cond;
expect(SWITCH);
@@ -230,7 +225,7 @@
Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
{
Node *np;
- Symbol *lcase = label(NULL, 1);
+ Symbol *lcase = label("", 1);
struct scase *pcase;
if (!lswitch)
@@ -253,7 +248,7 @@
static void
Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
{
- Symbol *ldefault = label(NULL, 1);
+ Symbol *ldefault = label("", 1);
expect(DEFAULT);
expect(':');
@@ -264,7 +259,7 @@
static void
If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
{
- Symbol *end, *lelse = label(NULL, 1);
+ Symbol *end, *lelse = label("", 1);
Node *np;
expect(IF);
@@ -273,7 +268,7 @@
emitjump(lelse, np);
stmt(lbreak, lcont, lswitch);
if (accept(ELSE)) {
- end = label(NULL, 1);
+ end = label("", 1);
emitjump(end, NULL);
emitlabel(lelse);
stmt(lbreak, lcont, lswitch);