scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit f51c3127ed2a277489722c40aaa947b7fab7040f
parent db2ea6a2cd2633a66d50471fc91181daff7db230
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  5 Jul 2013 19:26:21 +0200

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.

Diffstat:
Mdecl.c | 38+++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/decl.c b/decl.c @@ -13,23 +13,28 @@ char parser_out_home; 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; - - 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) + register struct symbol *sym = yyval.sym; + unsigned char yyns = sym->ns; + + 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 @@ dirdcl(register struct ctype *tp) 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 @@ spec(void) 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();