1
0
mirror of https://github.com/cc65/cc65.git synced 2025-02-08 11:31:34 +00:00

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
This commit is contained in:
uz 2012-10-27 19:50:49 +00:00
parent a3ae2e9125
commit f90fa9cc29
5 changed files with 54 additions and 45 deletions

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2011, Ullrich von Bassewitz */ /* (C) 1998-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -43,7 +43,6 @@
#include "nexttok.h" #include "nexttok.h"
#include "scanner.h" #include "scanner.h"
#include "symbol.h" #include "symbol.h"
#include "symtab.h"
@ -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 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
* and return the symbol table entry. * 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 for the symbol and return it. If no scope was specified,
* search also in the upper levels. * search also in the upper levels.
*/ */
if (NoScope && !AllocNew) { if (NoScope && (Action & SYM_ALLOC_NEW) == 0) {
Sym = SymFindAny (Scope, &Ident); Sym = SymFindAny (Scope, &Ident);
} else { } else {
Sym = SymFind (Scope, &Ident, AllocNew); Sym = SymFind (Scope, &Ident, Action);
} }
} else { } else {
/* No scope ==> no symbol. To avoid errors in the calling routine that /* 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 * may not expect NULL to be returned if Action contains SYM_ALLOC_NEW,
* symbol. * create a new symbol.
*/ */
if (AllocNew) { if (Action & SYM_ALLOC_NEW) {
Sym = NewSymEntry (&Ident, SF_NONE); Sym = NewSymEntry (&Ident, SF_NONE);
} else { } else {
Sym = 0; 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 /* 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. * 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 */ /* Distinguish cheap locals and other symbols */
if (CurTok.Tok == TOK_LOCAL_IDENT) { if (CurTok.Tok == TOK_LOCAL_IDENT) {
Sym = SymFindLocal (SymLast, &CurTok.SVal, AllocNew); Sym = SymFindLocal (SymLast, &CurTok.SVal, Action);
NextTok (); NextTok ();
} else { } else {
Sym = ParseScopedSymName (AllocNew); Sym = ParseScopedSymName (Action);
} }
/* Return the symbol found */ /* Return the symbol found */

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2010, Ullrich von Bassewitz */ /* (C) 1998-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -38,6 +38,11 @@
/* cc65 */
#include "symtab.h"
/*****************************************************************************/ /*****************************************************************************/
/* Forwards */ /* Forwards */
/*****************************************************************************/ /*****************************************************************************/
@ -45,7 +50,6 @@
struct StrBuf; 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. * 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 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
* and return the symbol table entry. * and return the symbol table entry.
*/ */
@ -74,7 +78,7 @@ struct SymTable* ParseScopedSymTable (void);
* symbol space and return the symbol table struct. * 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 /* 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. * for it in the symbol table and return the symbol table entry.
*/ */

View File

@ -78,10 +78,6 @@ struct HLLDbgSym;
/* Combined values */ /* Combined values */
#define SF_REFIMP (SF_REFERENCED|SF_IMPORT) /* A ref'd import */ #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 */ /* Structure of a symbol table entry */
typedef struct SymEntry SymEntry; typedef struct SymEntry SymEntry;
struct SymEntry { struct SymEntry {

View File

@ -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 */ /* Find a scope in the given enclosing scope */
{ {
SymTable** T = &Parent->Childs; 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 */ /* 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); *T = NewSymTable (Parent, Name);
} }
@ -323,11 +323,12 @@ SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name)
SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew) SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, SymFindAction Action)
/* Find a cheap local symbol. If AllocNew is given and the entry is not /* Find a cheap local symbol. If Action contains SYM_ALLOC_NEW and the entry is
* found, create a new one. Return the entry found, or the new entry created, * not found, create a new one. Return the entry found, or the new entry
* or - in case AllocNew is zero - return 0. * created, or - in case Action is SYM_FIND_EXISTING - return 0.
*/ */
{ {
SymEntry* S; SymEntry* S;
int Cmp; int Cmp;
@ -336,7 +337,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew)
if (!Parent) { if (!Parent) {
/* No last global, so there's no local table */ /* No last global, so there's no local table */
Error ("No preceeding global symbol"); Error ("No preceeding global symbol");
if (AllocNew) { if (Action & SYM_ALLOC_NEW) {
return NewSymEntry (Name, SF_LOCAL); return NewSymEntry (Name, SF_LOCAL);
} else { } else {
return 0; return 0;
@ -351,7 +352,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, int AllocNew)
return S; return S;
} }
if (AllocNew) { if (Action & SYM_ALLOC_NEW) {
/* Otherwise create a new entry, insert and return it */ /* Otherwise create a new entry, insert and return it */
SymEntry* N = NewSymEntry (Name, SF_LOCAL); 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) SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, SymFindAction Action)
/* Find a new symbol table entry in the given table. If AllocNew is given and /* Find a new symbol table entry in the given table. If Action contains
* the entry is not found, create a new one. Return the entry found, or the * SYM_ALLOC_NEW and the entry is not found, create a new one. Return the
* new entry created, or - in case AllocNew is zero - return 0. * entry found, or the new entry created, or - in case Action is
* SYM_FIND_EXISTING - return 0.
*/ */
{ {
SymEntry* S; SymEntry* S;
@ -388,13 +390,13 @@ SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew)
/* If we found an entry, return it */ /* If we found an entry, return it */
if (Cmp == 0) { if (Cmp == 0) {
if (SymTabIsClosed (Scope)) { if ((Action & SYM_CHECK_ONLY) == 0 && SymTabIsClosed (Scope)) {
S->Flags |= SF_FIXED; S->Flags |= SF_FIXED;
} }
return S; return S;
} }
if (AllocNew) { if (Action & SYM_ALLOC_NEW) {
/* Otherwise create a new entry, insert and return it. If the scope is /* 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 * already closed, mark the symbol as fixed so it won't be resolved
@ -478,7 +480,7 @@ static void SymCheckUndefined (SymEntry* S)
if ((S->Flags & SF_FIXED) == 0) { if ((S->Flags & SF_FIXED) == 0) {
SymTable* Tab = GetSymParentScope (S); SymTable* Tab = GetSymParentScope (S);
while (Tab) { 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) { if (Sym && (Sym->Flags & (SF_DEFINED | SF_IMPORT)) != 0) {
/* We've found a symbol in a higher level that is /* We've found a symbol in a higher level that is
* either defined in the source, or an import. * either defined in the source, or an import.

View File

@ -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 */ /* Symbol table flags */
#define ST_NONE 0x00 /* No flags */ #define ST_NONE 0x00 /* No flags */
#define ST_DEFINED 0x01 /* Scope has been defined */ #define ST_DEFINED 0x01 /* Scope has been defined */
@ -100,7 +107,7 @@ void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type,
void SymLeaveLevel (void); void SymLeaveLevel (void);
/* Leave the current lexical level */ /* 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 */ /* Find a scope in the given enclosing scope */
SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name); SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name);
@ -109,16 +116,17 @@ SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name);
* scope. * scope.
*/ */
SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* StrBuf, int AllocNew); SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* Name, SymFindAction Action);
/* Find a cheap local symbol. If AllocNew is given and the entry is not /* Find a cheap local symbol. If Action contains SYM_ALLOC_NEW and the entry is
* found, create a new one. Return the entry found, or the new entry created, * not found, create a new one. Return the entry found, or the new entry
* or - in case AllocNew is zero - return 0. * created, or - in case Action is SYM_FIND_EXISTING - return 0.
*/ */
SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew); SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, SymFindAction Action);
/* Find a new symbol table entry in the given table. If AllocNew is given and /* Find a new symbol table entry in the given table. If Action contains
* the entry is not found, create a new one. Return the entry found, or the * SYM_ALLOC_NEW and the entry is not found, create a new one. Return the
* new entry created, or - in case AllocNew is zero - return 0. * entry found, or the new entry created, or - in case Action is
* SYM_FIND_EXISTING - return 0.
*/ */
SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name); SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name);