#include "exprdefs.h"
#include "print.h"
#include "shift.h"
+#include "strbuf.h"
#include "tgttrans.h"
#include "version.h"
#include "xmalloc.h"
static ExprNode* FuncSizeOf (void)
/* Handle the .SIZEOF function */
{
- /* Get the struct for the scoped struct name */
- SymTable* Struct = ParseScopedSymTable (SYM_FIND_EXISTING);
+ StrBuf FullName = AUTO_STRBUF_INITIALIZER;
+ char Name[sizeof (SVal)];
+ SymTable* Scope;
+ SymEntry* Sym;
+ SymEntry* SizeSym;
+ long Size;
- /* Check if the given symbol is really a struct */
- if (GetSymTabType (Struct) != ST_STRUCT) {
- Error ("Argument to .SIZEOF is not a struct");
+
+ /* Parse the scope and the name */
+ SymTable* ParentScope = ParseScopedIdent (Name, &FullName);
+
+ /* Check if the parent scope is valid */
+ if (ParentScope == 0) {
+ /* No such scope */
+ DoneStrBuf (&FullName);
return GenLiteralExpr (0);
+ }
+
+ /* The scope is valid, search first for a child scope, then for a symbol */
+ if ((Scope = SymFindScope (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
+ /* Yep, it's a scope */
+ SizeSym = GetSizeOfScope (Scope);
+ } else if ((Sym = SymFind (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
+ SizeSym = GetSizeOfSymbol (Sym);
} else {
- return Symbol (GetSizeOfScope (Struct));
+ Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
+ return GenLiteralExpr (0);
}
+
+ /* Check if we have a size */
+ if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
+ Error ("Size of `%s' is unknown", SB_GetConstBuf (&FullName));
+ return GenLiteralExpr (0);
+ }
+
+ /* Return the size */
+ return GenLiteralExpr (Size);
}
#include <string.h>
+/* common */
+#include "strbuf.h"
+
/* ca65 */
#include "error.h"
#include "nexttok.h"
/*****************************************************************************/
-/* Data */
+/* Code */
/*****************************************************************************/
-/*****************************************************************************/
-/* Code */
-/*****************************************************************************/
-
-
-
-SymEntry* ParseScopedSymName (int AllocNew)
-/* Parse a (possibly scoped) symbol name, search for it in the symbol table
- * and return the symbol table entry.
+SymTable* ParseScopedIdent (char* Name, StrBuf* FullName)
+/* 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
+ * 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
+ * is a string buffer that is used to store the full name of the identifier
+ * including the scope. It is used internally and may be used by the caller
+ * for error messages or similar.
*/
{
/* Get the starting table */
SymTable* Scope;
if (Tok == TOK_NAMESPACE) {
+
+ /* Start from the root scope */
Scope = RootScope;
+
+ } else if (Tok == TOK_IDENT) {
+
+ /* Remember the name and skip it */
+ SB_AppendStr (FullName, strcpy (Name, SVal));
NextTok ();
+
+ /* If no namespace symbol follows, we're already done */
+ if (Tok != TOK_NAMESPACE) {
+ SB_Terminate (FullName);
+ return CurrentScope;
+ }
+
+ /* The scope must exist, so search for it starting with the current
+ * scope.
+ */
+ Scope = SymFindAnyScope (CurrentScope, Name);
+ if (Scope == 0) {
+ /* Scope not found */
+ SB_Terminate (FullName);
+ Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
+ return 0;
+ }
+
} else {
- Scope = CurrentScope;
- /* ### Need to walk up the tree */
+
+ /* Invalid token */
+ Error ("Identifier expected");
+ SB_Terminate (FullName);
+ Name[0] = '\0';
+ return 0;
+
}
- /* Resolve scopes */
+ /* Skip the namespace token that follows */
+ SB_AppendStr (FullName, "::");
+ NextTok ();
+
+ /* Resolve scopes. */
while (1) {
- /* An identifier must follow. Remember and skip it. */
- char Name[sizeof (SVal)];
+ /* Next token must be an identifier. */
if (Tok != TOK_IDENT) {
Error ("Identifier expected");
+ SB_Terminate (FullName);
+ Name[0] = '\0';
return 0;
}
- strcpy (Name, SVal);
+
+ /* Remember and skip the identifier */
+ SB_AppendStr (FullName, strcpy (Name, SVal));
NextTok ();
- /* If the next token is a namespace token, handle the name as the
- * name of a scope, otherwise it's the name of a symbol in that
- * scope.
+ /* If a namespace token follows, we search for another scope, otherwise
+ * the name is a symbol and we're done.
*/
+ if (Tok != TOK_NAMESPACE) {
+ /* Symbol */
+ SB_Terminate (FullName);
+ return Scope;
+ }
- if (Tok == TOK_NAMESPACE) {
+ /* Search for the child scope */
+ Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
+ if (Scope == 0) {
+ /* Scope not found */
+ SB_Terminate (FullName);
+ Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
+ return 0;
+ }
- /* Search for the child scope */
- Scope = SymFindScope (Scope, Name, AllocNew);
+ /* Skip the namespace token that follows */
+ SB_AppendStr (FullName, "::");
+ NextTok ();
+ }
+}
- /* Skip the namespace token */
- NextTok ();
- /* If we didn't find the scope, bail out */
- if (Scope == 0) {
- return 0;
- }
- } else {
+SymEntry* ParseScopedSymName (int AllocNew)
+/* Parse a (possibly scoped) symbol name, search for it in the symbol table
+ * and return the symbol table entry.
+ */
+{
+ StrBuf FullName = AUTO_STRBUF_INITIALIZER;
+ char Ident[sizeof (SVal)];
- /* Search for the symbol and return it */
- return SymFind (Scope, Name, AllocNew);
+ /* Parse the scoped symbol name */
+ SymTable* Scope = ParseScopedIdent (Ident, &FullName);
+ /* We don't need FullName any longer */
+ DoneStrBuf (&FullName);
+
+ /* Check if the scope is valid. Errors have already been diagnosed by
+ * the routine, so just exit.
+ */
+ if (Scope) {
+ /* Search for the symbol and return it */
+ return SymFind (Scope, Ident, AllocNew);
+ } 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.
+ */
+ if (AllocNew) {
+ return NewSymEntry (Ident);
+ } else {
+ return 0;
}
}
}
-SymTable* ParseScopedSymTable (int AllocNew)
+SymTable* ParseScopedSymTable (void)
/* Parse a (possibly scoped) symbol table (scope) name, search for it in the
* symbol space and return the symbol table struct.
*/
{
- /* Get the starting table */
- SymTable* Scope;
- if (Tok == TOK_NAMESPACE) {
- Scope = RootScope;
- NextTok ();
- } else {
- Scope = CurrentScope;
- if (Tok != TOK_IDENT) {
- Error ("Identifier expected");
- return Scope;
- }
-
- /* If no new scope should be allocated, the scope may specify any
- * scope in any of the parent scopes, so search for it.
- */
- if (!AllocNew) {
- Scope = SymFindAnyScope (Scope, SVal);
- NextTok ();
- if (Tok != TOK_NAMESPACE) {
- return Scope;
- }
- NextTok ();
- }
- }
-
- /* Resolve scopes. */
- while (Tok == TOK_IDENT) {
+ StrBuf FullName = AUTO_STRBUF_INITIALIZER;
+ char Ident[sizeof (SVal)];
- /* Search for the child scope if we have a valid parent */
- if (Scope) {
- Scope = SymFindScope (Scope, SVal, AllocNew);
- }
+ /* Parse the scoped symbol name */
+ SymTable* Scope = ParseScopedIdent (Ident, &FullName);
- /* Skip the name token */
- NextTok ();
+ /* We don't need FullName any longer */
+ DoneStrBuf (&FullName);
- /* If a namespace token follows, read on, otherwise bail out */
- if (Tok == TOK_NAMESPACE) {
- NextTok ();
- if (Tok != TOK_IDENT) {
- Error ("Identifier expected");
- }
- } else {
- break;
- }
+ /* Check if the scope is valid. Errors have already been diagnosed by
+ * the routine, so just exit.
+ */
+ if (Scope) {
+ /* Search for the last scope */
+ Scope = SymFindScope (Scope, Ident, SYM_FIND_EXISTING);
}
-
- /* Return the scope we found or created */
return Scope;
}