From f90fa9cc29d34f83b0a8d7080c8b67789a1bd501 Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 27 Oct 2012 19:50:49 +0000 Subject: [PATCH] Make the old "AllocNew" flag for symbols an enum and add an additional flag that allows to lookup a symbol without any flags added to it. git-svn-id: svn://svn.cc65.org/cc65/trunk@5884 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/symbol.c | 23 +++++++++++------------ src/ca65/symbol.h | 12 ++++++++---- src/ca65/symentry.h | 4 ---- src/ca65/symtab.c | 34 ++++++++++++++++++---------------- src/ca65/symtab.h | 26 +++++++++++++++++--------- 5 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/ca65/symbol.c b/src/ca65/symbol.c index 5f5abe560..581e8e54b 100644 --- a/src/ca65/symbol.c +++ b/src/ca65/symbol.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2011, Ullrich von Bassewitz */ +/* (C) 1998-2012, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -43,7 +43,6 @@ #include "nexttok.h" #include "scanner.h" #include "symbol.h" -#include "symtab.h" @@ -88,7 +87,7 @@ SymTable* ParseScopedIdent (StrBuf* Name, StrBuf* FullName) /* Pass the scope back to the caller */ SB_Append (FullName, Name); - + /* The scope must exist, so search for it starting with the current * scope. */ @@ -152,7 +151,7 @@ SymTable* ParseScopedIdent (StrBuf* Name, StrBuf* FullName) -SymEntry* ParseScopedSymName (int AllocNew) +SymEntry* ParseScopedSymName (SymFindAction Action) /* Parse a (possibly scoped) symbol name, search for it in the symbol table * and return the symbol table entry. */ @@ -178,17 +177,17 @@ SymEntry* ParseScopedSymName (int AllocNew) /* Search for the symbol and return it. If no scope was specified, * search also in the upper levels. */ - if (NoScope && !AllocNew) { + if (NoScope && (Action & SYM_ALLOC_NEW) == 0) { Sym = SymFindAny (Scope, &Ident); } else { - Sym = SymFind (Scope, &Ident, AllocNew); + Sym = SymFind (Scope, &Ident, Action); } } else { /* No scope ==> no symbol. To avoid errors in the calling routine that - * may not expect NULL to be returned if AllocNew is true, create a new - * symbol. + * may not expect NULL to be returned if Action contains SYM_ALLOC_NEW, + * create a new symbol. */ - if (AllocNew) { + if (Action & SYM_ALLOC_NEW) { Sym = NewSymEntry (&Ident, SF_NONE); } else { Sym = 0; @@ -245,7 +244,7 @@ SymTable* ParseScopedSymTable (void) -SymEntry* ParseAnySymName (int AllocNew) +SymEntry* ParseAnySymName (SymFindAction Action) /* Parse a cheap local symbol or a a (possibly scoped) symbol name, search * for it in the symbol table and return the symbol table entry. */ @@ -254,10 +253,10 @@ SymEntry* ParseAnySymName (int AllocNew) /* Distinguish cheap locals and other symbols */ if (CurTok.Tok == TOK_LOCAL_IDENT) { - Sym = SymFindLocal (SymLast, &CurTok.SVal, AllocNew); + Sym = SymFindLocal (SymLast, &CurTok.SVal, Action); NextTok (); } else { - Sym = ParseScopedSymName (AllocNew); + Sym = ParseScopedSymName (Action); } /* Return the symbol found */ diff --git a/src/ca65/symbol.h b/src/ca65/symbol.h index b790fcacb..fc1a84aa4 100644 --- a/src/ca65/symbol.h +++ b/src/ca65/symbol.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2010, Ullrich von Bassewitz */ +/* (C) 1998-2012, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -38,6 +38,11 @@ +/* cc65 */ +#include "symtab.h" + + + /*****************************************************************************/ /* Forwards */ /*****************************************************************************/ @@ -45,7 +50,6 @@ struct StrBuf; -struct SymTable; @@ -64,7 +68,7 @@ struct SymTable* ParseScopedIdent (struct StrBuf* Name, struct StrBuf* FullName) * by the caller for error messages or similar. */ -struct SymEntry* ParseScopedSymName (int AllowNew); +struct SymEntry* ParseScopedSymName (SymFindAction Action); /* Parse a (possibly scoped) symbol name, search for it in the symbol table * and return the symbol table entry. */ @@ -74,7 +78,7 @@ struct SymTable* ParseScopedSymTable (void); * symbol space and return the symbol table struct. */ -struct SymEntry* ParseAnySymName (int AllocNew); +struct SymEntry* ParseAnySymName (SymFindAction Action); /* Parse a cheap local symbol or a a (possibly scoped) symbol name, search * for it in the symbol table and return the symbol table entry. */ diff --git a/src/ca65/symentry.h b/src/ca65/symentry.h index c0039d00f..b55a20fca 100644 --- a/src/ca65/symentry.h +++ b/src/ca65/symentry.h @@ -78,10 +78,6 @@ struct HLLDbgSym; /* Combined values */ #define SF_REFIMP (SF_REFERENCED|SF_IMPORT) /* A ref'd import */ -/* Arguments for SymFind... */ -#define SYM_FIND_EXISTING 0 -#define SYM_ALLOC_NEW 1 - /* Structure of a symbol table entry */ typedef struct SymEntry SymEntry; struct SymEntry { diff --git a/src/ca65/symtab.c b/src/ca65/symtab.c index 776f005b1..02bc2af49 100644 --- a/src/ca65/symtab.c +++ b/src/ca65/symtab.c @@ -275,7 +275,7 @@ void SymLeaveLevel (void) -SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, int AllocNew) +SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, SymFindAction Action) /* Find a scope in the given enclosing scope */ { SymTable** T = &Parent->Childs; @@ -292,7 +292,7 @@ SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, int AllocNew) } /* Create a new scope if requested and we didn't find one */ - if (*T == 0 && AllocNew) { + if (*T == 0 && (Action & SYM_ALLOC_NEW) != 0) { *T = NewSymTable (Parent, Name); } @@ -323,11 +323,12 @@ SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name) -SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew) -/* Find a cheap local symbol. If AllocNew is given and the entry is not - * found, create a new one. Return the entry found, or the new entry created, - * or - in case AllocNew is zero - return 0. +SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, SymFindAction Action) +/* Find a cheap local symbol. If Action contains SYM_ALLOC_NEW and the entry is + * not found, create a new one. Return the entry found, or the new entry + * created, or - in case Action is SYM_FIND_EXISTING - return 0. */ + { SymEntry* S; int Cmp; @@ -336,7 +337,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew) if (!Parent) { /* No last global, so there's no local table */ Error ("No preceeding global symbol"); - if (AllocNew) { + if (Action & SYM_ALLOC_NEW) { return NewSymEntry (Name, SF_LOCAL); } else { return 0; @@ -351,7 +352,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew) return S; } - if (AllocNew) { + if (Action & SYM_ALLOC_NEW) { /* Otherwise create a new entry, insert and return it */ SymEntry* N = NewSymEntry (Name, SF_LOCAL); @@ -372,10 +373,11 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew) -SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew) -/* Find a new symbol table entry in the given table. If AllocNew is given and - * the entry is not found, create a new one. Return the entry found, or the - * new entry created, or - in case AllocNew is zero - return 0. +SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, SymFindAction Action) +/* Find a new symbol table entry in the given table. If Action contains + * SYM_ALLOC_NEW and the entry is not found, create a new one. Return the + * entry found, or the new entry created, or - in case Action is + * SYM_FIND_EXISTING - return 0. */ { SymEntry* S; @@ -388,13 +390,13 @@ SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew) /* If we found an entry, return it */ if (Cmp == 0) { - if (SymTabIsClosed (Scope)) { + if ((Action & SYM_CHECK_ONLY) == 0 && SymTabIsClosed (Scope)) { S->Flags |= SF_FIXED; } return S; } - if (AllocNew) { + if (Action & SYM_ALLOC_NEW) { /* Otherwise create a new entry, insert and return it. If the scope is * already closed, mark the symbol as fixed so it won't be resolved @@ -428,7 +430,7 @@ SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name) * never create a new symbol, since this can only be done in one specific * scope. */ -{ +{ /* Generate the name hash */ unsigned Hash = HashBuf (Name); @@ -478,7 +480,7 @@ static void SymCheckUndefined (SymEntry* S) if ((S->Flags & SF_FIXED) == 0) { SymTable* Tab = GetSymParentScope (S); while (Tab) { - Sym = SymFind (Tab, GetStrBuf (S->Name), SYM_FIND_EXISTING); + Sym = SymFind (Tab, GetStrBuf (S->Name), SYM_FIND_EXISTING | SYM_CHECK_ONLY); if (Sym && (Sym->Flags & (SF_DEFINED | SF_IMPORT)) != 0) { /* We've found a symbol in a higher level that is * either defined in the source, or an import. diff --git a/src/ca65/symtab.h b/src/ca65/symtab.h index 4ef2ea93c..0a0b2caa9 100644 --- a/src/ca65/symtab.h +++ b/src/ca65/symtab.h @@ -55,6 +55,13 @@ +/* Arguments for SymFind... */ +typedef enum { + SYM_FIND_EXISTING = 0x00, + SYM_ALLOC_NEW = 0x01, + SYM_CHECK_ONLY = 0x02, +} SymFindAction; + /* Symbol table flags */ #define ST_NONE 0x00 /* No flags */ #define ST_DEFINED 0x01 /* Scope has been defined */ @@ -100,7 +107,7 @@ void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, void SymLeaveLevel (void); /* Leave the current lexical level */ -SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, int AllocNew); +SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, SymFindAction Action); /* Find a scope in the given enclosing scope */ SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name); @@ -109,16 +116,17 @@ SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name); * scope. */ -SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* StrBuf, int AllocNew); -/* Find a cheap local symbol. If AllocNew is given and the entry is not - * found, create a new one. Return the entry found, or the new entry created, - * or - in case AllocNew is zero - return 0. +SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, SymFindAction Action); +/* Find a cheap local symbol. If Action contains SYM_ALLOC_NEW and the entry is + * not found, create a new one. Return the entry found, or the new entry + * created, or - in case Action is SYM_FIND_EXISTING - return 0. */ -SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew); -/* Find a new symbol table entry in the given table. If AllocNew is given and - * the entry is not found, create a new one. Return the entry found, or the - * new entry created, or - in case AllocNew is zero - return 0. +SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, SymFindAction Action); +/* Find a new symbol table entry in the given table. If Action contains + * SYM_ALLOC_NEW and the entry is not found, create a new one. Return the + * entry found, or the new entry created, or - in case Action is + * SYM_FIND_EXISTING - return 0. */ SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name); -- 2.39.5