Fixes for scoping and better .sizeof support
git-svn-id: svn://svn.cc65.org/cc65/trunk@2706 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
@@ -386,21 +386,26 @@ static ExprNode* FuncReferenced (void)
|
|||||||
static ExprNode* FuncSizeOf (void)
|
static ExprNode* FuncSizeOf (void)
|
||||||
/* Handle the .SIZEOF function */
|
/* Handle the .SIZEOF function */
|
||||||
{
|
{
|
||||||
StrBuf FullName = AUTO_STRBUF_INITIALIZER;
|
StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
|
||||||
char Name[sizeof (SVal)];
|
char Name[sizeof (SVal)];
|
||||||
SymTable* Scope;
|
SymTable* Scope;
|
||||||
SymEntry* Sym;
|
SymEntry* Sym;
|
||||||
SymEntry* SizeSym;
|
SymEntry* SizeSym;
|
||||||
long Size;
|
long Size;
|
||||||
|
int NoScope;
|
||||||
|
|
||||||
|
|
||||||
|
/* Assume an error */
|
||||||
|
SizeSym = 0;
|
||||||
|
|
||||||
|
/* Check for a cheap local which needs special handling */
|
||||||
if (Tok == TOK_LOCAL_IDENT) {
|
if (Tok == TOK_LOCAL_IDENT) {
|
||||||
|
|
||||||
/* Cheap local symbol, special handling */
|
/* Cheap local symbol */
|
||||||
Sym = SymFindLocal (SymLast, SVal, SYM_FIND_EXISTING);
|
Sym = SymFindLocal (SymLast, SVal, SYM_FIND_EXISTING);
|
||||||
if (Sym == 0) {
|
if (Sym == 0) {
|
||||||
Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
|
Error ("Unknown symbol or scope: `%s%s'",
|
||||||
return GenLiteralExpr (0);
|
SB_GetConstBuf (&ScopeName), Name);
|
||||||
} else {
|
} else {
|
||||||
SizeSym = GetSizeOfSymbol (Sym);
|
SizeSym = GetSizeOfSymbol (Sym);
|
||||||
}
|
}
|
||||||
@@ -408,33 +413,59 @@ static ExprNode* FuncSizeOf (void)
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* Parse the scope and the name */
|
/* Parse the scope and the name */
|
||||||
SymTable* ParentScope = ParseScopedIdent (Name, &FullName);
|
SymTable* ParentScope = ParseScopedIdent (Name, &ScopeName);
|
||||||
|
|
||||||
/* Check if the parent scope is valid */
|
/* Check if the parent scope is valid */
|
||||||
if (ParentScope == 0) {
|
if (ParentScope == 0) {
|
||||||
/* No such scope */
|
/* No such scope */
|
||||||
DoneStrBuf (&FullName);
|
DoneStrBuf (&ScopeName);
|
||||||
return GenLiteralExpr (0);
|
return GenLiteralExpr (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The scope is valid, search first for a child scope, then for a symbol */
|
/* If ScopeName is empty, no explicit scope was specified. We have to
|
||||||
if ((Scope = SymFindScope (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
|
* search upper scope levels in this case.
|
||||||
|
*/
|
||||||
|
NoScope = SB_IsEmpty (&ScopeName);
|
||||||
|
|
||||||
|
/* First search for a scope with the given name */
|
||||||
|
if (NoScope) {
|
||||||
|
Scope = SymFindAnyScope (ParentScope, Name);
|
||||||
|
} else {
|
||||||
|
Scope = SymFindScope (ParentScope, Name, SYM_FIND_EXISTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we did find a scope with the name, read the symbol defining the
|
||||||
|
* size, otherwise search for a symbol entry with the name and scope.
|
||||||
|
*/
|
||||||
|
if (Scope) {
|
||||||
/* Yep, it's a scope */
|
/* Yep, it's a scope */
|
||||||
SizeSym = GetSizeOfScope (Scope);
|
SizeSym = GetSizeOfScope (Scope);
|
||||||
} else if ((Sym = SymFind (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
|
|
||||||
SizeSym = GetSizeOfSymbol (Sym);
|
|
||||||
} else {
|
} else {
|
||||||
Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
|
if (NoScope) {
|
||||||
return GenLiteralExpr (0);
|
Sym = SymFindAny (ParentScope, Name);
|
||||||
|
} else {
|
||||||
|
Sym = SymFind (ParentScope, Name, SYM_FIND_EXISTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we found the symbol retrieve the size, otherwise complain */
|
||||||
|
if (Sym) {
|
||||||
|
SizeSym = GetSizeOfSymbol (Sym);
|
||||||
|
} else {
|
||||||
|
Error ("Unknown symbol or scope: `%s%s'",
|
||||||
|
SB_GetConstBuf (&ScopeName), Name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if we have a size */
|
/* Check if we have a size */
|
||||||
if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
|
if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
|
||||||
Error ("Size of `%s' is unknown", SB_GetConstBuf (&FullName));
|
Error ("Size of `%s%s' is unknown", SB_GetConstBuf (&ScopeName), Name);
|
||||||
return GenLiteralExpr (0);
|
Size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free the scope name */
|
||||||
|
DoneStrBuf (&ScopeName);
|
||||||
|
|
||||||
/* Return the size */
|
/* Return the size */
|
||||||
return GenLiteralExpr (Size);
|
return GenLiteralExpr (Size);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,14 +53,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
|
SymTable* ParseScopedIdent (char* Name, StrBuf* ScopeName)
|
||||||
/* Parse a (possibly scoped) identifer. Name must point to a buffer big enough
|
/* Parse a (possibly scoped) identifer. Name must point to a buffer big enough
|
||||||
* to hold such an identifier. The scope of the name must exist and is returned
|
* to hold such an identifier. The scope of the name must exist and is returned
|
||||||
* as function result, while the last part (the identifier) which may be either
|
* as function result, while the last part (the identifier) which may be either
|
||||||
* a symbol or a scope depending on the context is returned in Name. FullName
|
* a symbol or a scope depending on the context is returned in Name. ScopeName
|
||||||
* is a string buffer that is used to store the full name of the identifier
|
* is a string buffer that is used to store the name of the scope, the
|
||||||
* including the scope. It is used internally and may be used by the caller
|
* identifier lives in. It does contain anything but the identifier itself, so
|
||||||
* for error messages or similar.
|
* if ScopeName is empty on return, no explicit scope was specified. The full
|
||||||
|
* name of the identifier (including the scope) is ScopeName+Name.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
/* Get the starting table */
|
/* Get the starting table */
|
||||||
@@ -73,23 +74,26 @@ SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
|
|||||||
} else if (Tok == TOK_IDENT) {
|
} else if (Tok == TOK_IDENT) {
|
||||||
|
|
||||||
/* Remember the name and skip it */
|
/* Remember the name and skip it */
|
||||||
SB_AppendStr (FullName, strcpy (Name, SVal));
|
strcpy (Name, SVal);
|
||||||
NextTok ();
|
NextTok ();
|
||||||
|
|
||||||
/* If no namespace symbol follows, we're already done */
|
/* If no namespace symbol follows, we're already done */
|
||||||
if (Tok != TOK_NAMESPACE) {
|
if (Tok != TOK_NAMESPACE) {
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
return CurrentScope;
|
return CurrentScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Pass the scope back to the caller */
|
||||||
|
SB_AppendStr (ScopeName, Name);
|
||||||
|
|
||||||
/* The scope must exist, so search for it starting with the current
|
/* The scope must exist, so search for it starting with the current
|
||||||
* scope.
|
* scope.
|
||||||
*/
|
*/
|
||||||
Scope = SymFindAnyScope (CurrentScope, Name);
|
Scope = SymFindAnyScope (CurrentScope, Name);
|
||||||
if (Scope == 0) {
|
if (Scope == 0) {
|
||||||
/* Scope not found */
|
/* Scope not found */
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
|
Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,14 +101,14 @@ SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
|
|||||||
|
|
||||||
/* Invalid token */
|
/* Invalid token */
|
||||||
Error ("Identifier expected");
|
Error ("Identifier expected");
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
Name[0] = '\0';
|
Name[0] = '\0';
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Skip the namespace token that follows */
|
/* Skip the namespace token that follows */
|
||||||
SB_AppendStr (FullName, "::");
|
SB_AppendStr (ScopeName, "::");
|
||||||
NextTok ();
|
NextTok ();
|
||||||
|
|
||||||
/* Resolve scopes. */
|
/* Resolve scopes. */
|
||||||
@@ -113,13 +117,13 @@ SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
|
|||||||
/* Next token must be an identifier. */
|
/* Next token must be an identifier. */
|
||||||
if (Tok != TOK_IDENT) {
|
if (Tok != TOK_IDENT) {
|
||||||
Error ("Identifier expected");
|
Error ("Identifier expected");
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
Name[0] = '\0';
|
Name[0] = '\0';
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remember and skip the identifier */
|
/* Remember and skip the identifier */
|
||||||
SB_AppendStr (FullName, strcpy (Name, SVal));
|
strcpy (Name, SVal);
|
||||||
NextTok ();
|
NextTok ();
|
||||||
|
|
||||||
/* If a namespace token follows, we search for another scope, otherwise
|
/* If a namespace token follows, we search for another scope, otherwise
|
||||||
@@ -127,25 +131,28 @@ SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
|
|||||||
*/
|
*/
|
||||||
if (Tok != TOK_NAMESPACE) {
|
if (Tok != TOK_NAMESPACE) {
|
||||||
/* Symbol */
|
/* Symbol */
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
return Scope;
|
return Scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Pass the scope back to the caller */
|
||||||
|
SB_AppendStr (ScopeName, Name);
|
||||||
|
|
||||||
/* Search for the child scope */
|
/* Search for the child scope */
|
||||||
Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
|
Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
|
||||||
if (Scope == 0) {
|
if (Scope == 0) {
|
||||||
/* Scope not found */
|
/* Scope not found */
|
||||||
SB_Terminate (FullName);
|
SB_Terminate (ScopeName);
|
||||||
Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
|
Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Skip the namespace token that follows */
|
/* Skip the namespace token that follows */
|
||||||
SB_AppendStr (FullName, "::");
|
SB_AppendStr (ScopeName, "::");
|
||||||
NextTok ();
|
NextTok ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SymEntry* ParseScopedSymName (int AllocNew)
|
SymEntry* ParseScopedSymName (int AllocNew)
|
||||||
@@ -153,32 +160,49 @@ SymEntry* ParseScopedSymName (int AllocNew)
|
|||||||
* and return the symbol table entry.
|
* and return the symbol table entry.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
StrBuf FullName = AUTO_STRBUF_INITIALIZER;
|
StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
|
||||||
char Ident[sizeof (SVal)];
|
char Ident[sizeof (SVal)];
|
||||||
|
int NoScope;
|
||||||
|
SymEntry* Sym;
|
||||||
|
|
||||||
/* Parse the scoped symbol name */
|
/* Parse the scoped symbol name */
|
||||||
SymTable* Scope = ParseScopedIdent (Ident, &FullName);
|
SymTable* Scope = ParseScopedIdent (Ident, &ScopeName);
|
||||||
|
|
||||||
/* We don't need FullName any longer */
|
/* If ScopeName is empty, no scope was specified */
|
||||||
DoneStrBuf (&FullName);
|
NoScope = SB_IsEmpty (&ScopeName);
|
||||||
|
|
||||||
|
/* We don't need ScopeName any longer */
|
||||||
|
DoneStrBuf (&ScopeName);
|
||||||
|
|
||||||
/* Check if the scope is valid. Errors have already been diagnosed by
|
/* Check if the scope is valid. Errors have already been diagnosed by
|
||||||
* the routine, so just exit.
|
* the routine, so just exit.
|
||||||
*/
|
*/
|
||||||
if (Scope) {
|
if (Scope) {
|
||||||
/* Search for the symbol and return it */
|
/* Search for the symbol and return it. If no scope was specified,
|
||||||
return SymFind (Scope, Ident, AllocNew);
|
* search also in the upper levels.
|
||||||
|
*/
|
||||||
|
if (NoScope) {
|
||||||
|
Sym = SymFindAny (Scope, Ident);
|
||||||
|
if (Sym == 0 && AllocNew) {
|
||||||
|
Sym = SymFind (Scope, Ident, AllocNew);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Sym = SymFind (Scope, Ident, AllocNew);
|
||||||
|
}
|
||||||
} 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 AllocNew is true, create a new
|
||||||
* symbol.
|
* symbol.
|
||||||
*/
|
*/
|
||||||
if (AllocNew) {
|
if (AllocNew) {
|
||||||
return NewSymEntry (Ident, SF_NONE);
|
Sym = NewSymEntry (Ident, SF_NONE);
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
Sym = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return the symbol found */
|
||||||
|
return Sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -188,21 +212,31 @@ SymTable* ParseScopedSymTable (void)
|
|||||||
* symbol space and return the symbol table struct.
|
* symbol space and return the symbol table struct.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
StrBuf FullName = AUTO_STRBUF_INITIALIZER;
|
StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
|
||||||
char Ident[sizeof (SVal)];
|
char Name[sizeof (SVal)];
|
||||||
|
int NoScope;
|
||||||
|
|
||||||
|
|
||||||
/* Parse the scoped symbol name */
|
/* Parse the scoped symbol name */
|
||||||
SymTable* Scope = ParseScopedIdent (Ident, &FullName);
|
SymTable* Scope = ParseScopedIdent (Name, &ScopeName);
|
||||||
|
|
||||||
|
/* If ScopeName is empty, no scope was specified */
|
||||||
|
NoScope = SB_IsEmpty (&ScopeName);
|
||||||
|
|
||||||
/* We don't need FullName any longer */
|
/* We don't need FullName any longer */
|
||||||
DoneStrBuf (&FullName);
|
DoneStrBuf (&ScopeName);
|
||||||
|
|
||||||
/* Check if the scope is valid. Errors have already been diagnosed by
|
/* If we got no error, search for the child scope withint the enclosing one.
|
||||||
* the routine, so just exit.
|
* Beware: If no explicit parent scope was specified, search in all upper
|
||||||
|
* levels.
|
||||||
*/
|
*/
|
||||||
if (Scope) {
|
if (Scope) {
|
||||||
/* Search for the last scope */
|
/* Search for the last scope */
|
||||||
Scope = SymFindScope (Scope, Ident, SYM_FIND_EXISTING);
|
if (NoScope) {
|
||||||
|
Scope = SymFindAnyScope (Scope, Name);
|
||||||
|
} else {
|
||||||
|
Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Scope;
|
return Scope;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static SymEntry* SymFindAny (SymTable* Scope, const char* Name)
|
SymEntry* SymFindAny (SymTable* Scope, const char* Name)
|
||||||
/* Find a symbol in the given or any of its parent scopes. The function will
|
/* Find a symbol in the given or any of its parent scopes. The function will
|
||||||
* never create a new symbol, since this can only be done in one specific
|
* never create a new symbol, since this can only be done in one specific
|
||||||
* scope.
|
* scope.
|
||||||
|
|||||||
@@ -123,6 +123,12 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew);
|
|||||||
* new entry created, or - in case AllocNew is zero - return 0.
|
* new entry created, or - in case AllocNew is zero - return 0.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
SymEntry* SymFindAny (SymTable* Scope, const char* Name);
|
||||||
|
/* Find a symbol in the given or any of its parent scopes. The function will
|
||||||
|
* never create a new symbol, since this can only be done in one specific
|
||||||
|
* scope.
|
||||||
|
*/
|
||||||
|
|
||||||
int SymIsZP (SymEntry* Sym);
|
int SymIsZP (SymEntry* Sym);
|
||||||
/* Return true if the symbol is explicitly marked as zeropage symbol */
|
/* Return true if the symbol is explicitly marked as zeropage symbol */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user