shithub: scc

Download patch

ref: fa095decfa65ce7fa2d03ad6fb1d75640c0803b1
parent: 7954fd897af0f78b514d09ed801cafafee4e8df5
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Apr 6 12:31:25 EDT 2022

cc1: Move defined function to cpp.c

Defined() enables and disables the macro expansion, and as it is
a very delicated thing to do it is better to keep all the code
that modifies disexpand in only one file.

While this patch was written it was discovered that the function
notdefined() was not acutally needed because the code to handle
preprocessor defined was ran from unary().

--- a/src/cmd/cc/cc1/cc1.h
+++ b/src/cmd/cc/cc1/cc1.h
@@ -196,6 +196,7 @@
 	BREAK,
 	RETURN,
 	DEFINE,
+	DEFINED,
 	INCLUDE,
 	LINE,
 	PRAGMA,
@@ -504,6 +505,7 @@
 extern void ppragmaln(void);
 extern void delmacro(Macro *);
 extern Macro *newmacro(Symbol *);
+extern Node *defined(void);
 
 
 /* builtin.c */
--- a/src/cmd/cc/cc1/cpp.c
+++ b/src/cmd/cc/cc1/cpp.c
@@ -338,7 +338,7 @@
 	DBG("MACRO '%s' detected disexpand=%d hide=%d",
 	    sym->name, disexpand, sym->hide);
 
-	if (disexpand || sym->hide)
+	if (disexpand || sym->hide || sym->token != IDEN)
 		return 0;
 
 	mp = newmacro(sym);
@@ -678,6 +678,29 @@
 	cpperror("#error %s", input->p);
 	*input->p = '\0';
 	next();
+}
+
+
+Node *
+defined(void)
+{
+	Symbol *sym;
+	int paren;
+
+	disexpand = 1;
+	next();
+	paren = accept('(');
+	if (yytoken != IDEN && yytoken != TYPEIDEN)
+		cpperror("operator 'defined' requires an identifier");
+	if (yytoken == TYPEIDEN || !(yylval.sym->flags & SDECLARED))
+		sym = zero;
+	else
+		sym = one;
+	disexpand = 0;
+	next();
+	if (paren)
+		expect(')');
+	return constnode(sym);
 }
 
 static void
--- a/src/cmd/cc/cc1/expr.c
+++ b/src/cmd/cc/cc1/expr.c
@@ -568,32 +568,6 @@
 }
 
 static Symbol *
-notdefined(Symbol *sym)
-{
-	int isdef;
-
-	if (namespace == NS_CPP && !strcmp(sym->name, "defined")) {
-		disexpand = 1;
-		next();
-		expect('(');
-		sym = yylval.sym;
-		expect(IDEN);
-		expect(')');
-
-		isdef = (sym->flags & SDECLARED) != 0;
-		sym = newsym(NS_IDEN, NULL);
-		sym->type = inttype;
-		sym->flags |= SCONSTANT;
-		sym->u.i = isdef;
-		disexpand = 0;
-		return sym;
-	}
-	errorp("'%s' undeclared", yytext);
-	sym->type = inttype;
-	return install(sym->ns, yylval.sym);
-}
-
-static Symbol *
 adjstrings(Symbol *sym)
 {
 	char *s, *t;
@@ -650,17 +624,23 @@
 	case CONSTANT:
 		np = constnode(sym);
 		break;
+	case DEFINED:
+		np = defined();
+		break;
 	case IDEN:
 		assert((sym->flags & SCONSTANT) == 0);
-		if ((sym->flags & SDECLARED) == 0) {
-			if (namespace == NS_CPP) {
-				np = constnode(zero);
-				break;
-			}
-			sym = notdefined(sym);
+		if ((sym->flags & SDECLARED) != 0) {
+			sym->flags |= SUSED;
+			np = varnode(sym);
+		} else if (namespace == NS_CPP) {
+			np = constnode(zero);
+		} else {
+			errorp("'%s' undeclared", yytext);
+			sym->type = inttype;
+			sym = install(sym->ns, yylval.sym);
+			sym->flags |= SUSED;
+			np = varnode(sym);
 		}
-		sym->flags |= SUSED;
-		np = varnode(sym);
 		break;
 	default:
 		unexpected();
@@ -807,28 +787,6 @@
 	}
 }
 
-static Node *
-defined(void)
-{
-	Symbol *sym;
-	int paren;
-
-	disexpand = 1;
-	next();
-	paren = accept('(');
-	if (yytoken != IDEN && yytoken != TYPEIDEN)
-		cpperror("operator 'defined' requires an identifier");
-	if (yytoken == TYPEIDEN || !(yylval.sym->flags & SDECLARED))
-		sym = zero;
-	else
-		sym = one;
-	disexpand = 0;
-	next();
-	if (paren)
-		expect(')');
-	return constnode(sym);
-}
-
 static Node *cast(int);
 
 static Node *
@@ -857,10 +815,8 @@
 		next();
 		np = incdec(unary(1), op);
 		goto chk_decay;
-	case IDEN:
-	case TYPEIDEN:
-		if (lexmode == CPPMODE && !strcmp(yylval.sym->name, "defined"))
-			return defined();
+	case DEFINED:
+		return defined();
 	default:
 		np = postfix(primary());
 		goto chk_decay;
--- a/src/cmd/cc/cc1/symbol.c
+++ b/src/cmd/cc/cc1/symbol.c
@@ -344,6 +344,10 @@
 void
 isyms(void)
 {
+	static struct keyword cppoper[] = {
+		{"defined", DEFINED, DEFINED},
+		{NULL, 0, 0}
+	};
 	static struct keyword cppkeys[] = {
 		{"define", DEFINE, DEFINE},
 		{"include", INCLUDE, INCLUDE},
@@ -401,6 +405,7 @@
 
 	keywords(lexkeys, NS_KEYWORD);
 	keywords(cppkeys, NS_CPPCLAUSES);
+	keywords(cppoper, NS_CPP);
 	ibuilts();
 
 	/*