ref: f51c3127ed2a277489722c40aaa947b7fab7040f
parent: db2ea6a2cd2633a66d50471fc91181daff7db230
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Jul 5 15:26:21 EDT 2013
Add namespace function These function returns to the user the symbol in the desired namespace. Like the first lookup is always done in the lexical part, in that moment we don't have the information for selecting the correct namespace, so it selects the last defined symbol, and not always it is the symbol we are looking for. This function check if the symbol is in the correct namespace, and if it is necessary it installs it.
--- a/decl.c
+++ b/decl.c
@@ -13,23 +13,28 @@
static struct symbol *cursym;
static void declarator(struct ctype *tp);
-static void
-newiden(struct ctype *tp)
+static struct symbol *
+namespace(register unsigned char ns, unsigned char alloc)
{
- register unsigned char yyns, ns;
+ register struct symbol *sym = yyval.sym;
+ unsigned char yyns = sym->ns;
- ns = tp->c_typedef ? NS_TYPEDEF : NS_IDEN;
- yyns = yyval.sym->ns;
-
- if (yyns == NS_ANY) { /* First appearence of the symbol */
- yyval.sym->ns = ns;
- cursym = yyval.sym;
- return;
- } else if (ns == yyns) { /* Duplicated symbol */
- if (yyval.sym->ctx == curctx)
+ if (!alloc) {
+ if (yyns == NS_ANY)
+ return NULL;
+ else if (yyns == ns)
+ return sym;
+ else
+ return find(yytext, ns);
+ } else {
+ if (yyns == NS_ANY) {
+ sym->ns = ns;
+ return sym;
+ } else if (yyns == ns && sym->ctx == curctx) {
error("redeclaration of '%s'", yytext);
+ }
+ return lookup(yytext, ns);
}
- cursym = lookup(yytext, ns);
}
static void
@@ -39,7 +44,7 @@
declarator(tp);
expect(')');
} else if (yytoken == IDEN) {
- newiden(tp);
+ cursym = namespace(tp->c_typedef ? NS_TYPEDEF : NS_IDEN, 1);
next();
} else {
error("expected '(' or identifier before of '%s'", yytext);
@@ -97,8 +102,7 @@
struct symbol *sym;
unsigned char tok = ahead();
- sym = (yyval.sym->ns == NS_TYPEDEF) ?
- yyval.sym : find(yytext, NS_TYPEDEF);
+ sym = namespace(NS_TYPEDEF, 0);
if (sym && tok != ';' && tok != ',') {
if (!tp)
tp = newctype();