]> git.sur5r.net Git - cc65/blobdiff - src/cc65/symtab.c
Bugfix
[cc65] / src / cc65 / symtab.c
index 11a4252f49a43a3fa7ecbe57f203dd00e7817ee4..6f01b109496cb37d69d1ee37733b6ffbd33b692b 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 2000-2001 Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -165,9 +165,9 @@ static void CheckSymTable (SymTable* Tab)
            if (((Flags & SC_AUTO) || (Flags & SC_STATIC)) && (Flags & SC_EXTERN) == 0) {
                if ((Flags & SC_DEF) && !(Flags & SC_REF)) {
                    if (Flags & SC_PARAM) {
-                       Warning (WARN_UNUSED_PARM, Entry->Name);
+                       Warning ("Parameter `%s' is never used", Entry->Name);
                    } else {
-                       Warning (WARN_UNUSED_ITEM, Entry->Name);
+                       Warning ("`%s' is defined but never used", Entry->Name);
                    }
                }
            }
@@ -176,10 +176,10 @@ static void CheckSymTable (SymTable* Tab)
            if (Flags & SC_LABEL) {
                if ((Flags & SC_DEF) == 0) {
                    /* Undefined label */
-                   Error (ERR_UNDEFINED_LABEL, Entry->Name);
+                   Error ("Undefined label: `%s'", Entry->Name);
                } else if ((Flags & SC_REF) == 0) {
                    /* Defined but not used */
-                   Warning (WARN_UNUSED_ITEM, Entry->Name);
+                   Warning ("`%s' is defined but never used", Entry->Name);
                }
            }
 
@@ -224,13 +224,13 @@ void LeaveGlobalLevel (void)
 
     /* Dump the tables if requested */
     if (Debug) {
-       PrintSymTable (SymTab0, stdout, "Global symbol table");
-       PrintSymTable (TagTab0, stdout, "Global tag table");
+       PrintSymTable (SymTab0, stdout, "Global symbol table");
+       PrintSymTable (TagTab0, stdout, "Global tag table");
     }
 
     /* Don't delete the symbol and struct tables! */
-    SymTab0 = SymTab = 0;
-    TagTab0 = TagTab = 0;
+    SymTab = 0;
+    TagTab = 0;
 }
 
 
@@ -441,6 +441,14 @@ SymEntry* FindSym (const char* Name)
 
 
 
+SymEntry* FindGlobalSym (const char* Name)
+/* Find the symbol with the given name in the global symbol table only */
+{
+    return FindSymInTable (SymTab0, Name, HashStr (Name));
+}
+
+
+
 SymEntry* FindLocalSym (const char* Name)
 /* Find the symbol with the given name in the current symbol table only */
 {
@@ -536,10 +544,10 @@ SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
        /* We do have an entry. This may be a forward, so check it. */
        if ((Entry->Flags & SC_STRUCT) == 0) {
            /* Existing symbol is not a struct */
-           Error (ERR_SYMBOL_KIND);
+           Error ("Symbol `%s' is already different kind", Name);
        } else if (Size > 0 && Entry->V.S.Size > 0) {
            /* Both structs are definitions. */
-           Error (ERR_MULTIPLE_DEFINITION, Name);
+           Error ("Multiple definition for `%s'", Name);
        } else {
            /* Define the struct size if it is given */
            if (Size > 0) {
@@ -567,28 +575,28 @@ SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
 
 
 
-SymEntry* AddEnumSym (const char* Name, int Val)
-/* Add an enum symbol to the symbol table and return it */
+SymEntry* AddConstSym (const char* Name, const type* Type, unsigned Flags, long Val)
+/* Add an constant symbol to the symbol table and return it */
 {
     /* Do we have an entry with this name already? */
     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
     if (Entry) {
-       if (Entry->Flags != SC_ENUM) {
-           Error (ERR_SYMBOL_KIND);
+       if ((Entry->Flags & SC_CONST) != SC_CONST) {
+           Error ("Symbol `%s' is already different kind", Name);
        } else {
-           Error (ERR_MULTIPLE_DEFINITION, Name);
+           Error ("Multiple definition for `%s'", Name);
        }
        return Entry;
     }
 
     /* Create a new entry */
-    Entry = NewSymEntry (Name, SC_ENUM);
+    Entry = NewSymEntry (Name, Flags);
 
     /* Enum values are ints */
-    Entry->Type        = TypeDup (type_int);
+    Entry->Type        = TypeDup (Type);
 
     /* Set the enum data */
-    Entry->V.EnumVal = Val;
+    Entry->V.ConstVal = Val;
 
     /* Add the entry to the symbol table */
     AddSymEntry (SymTab, Entry);
@@ -608,7 +616,7 @@ SymEntry* AddLabelSym (const char* Name, unsigned Flags)
 
        if ((Entry->Flags & SC_DEF) != 0 && (Flags & SC_DEF) != 0) {
            /* Trying to define the label more than once */
-                   Error (ERR_MULTIPLE_DEFINITION, Name);
+                   Error ("Label `%s' is defined more than once", Name);
        }
        Entry->Flags |= Flags;
 
@@ -618,7 +626,7 @@ SymEntry* AddLabelSym (const char* Name, unsigned Flags)
        Entry = NewSymEntry (Name, SC_LABEL | Flags);
 
        /* Set a new label number */
-       Entry->V.Label = GetLabel ();
+       Entry->V.Label = GetLocalLabel ();
 
        /* Add the entry to the label table */
        AddSymEntry (LabelTab, Entry);
@@ -631,7 +639,7 @@ SymEntry* AddLabelSym (const char* Name, unsigned Flags)
 
 
 
-SymEntry* AddLocalSym (const char* Name, type* Type, unsigned Flags, int Offs)
+SymEntry* AddLocalSym (const char* Name, const type* Type, unsigned Flags, int Offs)
 /* Add a local symbol and return the symbol entry */
 {
     /* Do we have an entry with this name already? */
@@ -639,7 +647,7 @@ SymEntry* AddLocalSym (const char* Name, type* Type, unsigned Flags, int Offs)
     if (Entry) {
 
        /* We have a symbol with this name already */
-       Error (ERR_MULTIPLE_DEFINITION, Name);
+       Error ("Multiple definition for `%s'", Name);
 
     } else {
 
@@ -661,11 +669,14 @@ SymEntry* AddLocalSym (const char* Name, type* Type, unsigned Flags, int Offs)
 
 
 
-SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
+SymEntry* AddGlobalSym (const char* Name, const type* Type, unsigned Flags)
 /* Add an external or global symbol to the symbol table and return the entry */
 {
+    /* There is some special handling for functions, so check if it is one */
+    int IsFunc = IsTypeFunc (Type);
+
     /* Functions must be inserted in the global symbol table */
-    SymTable* Tab = IsTypeFunc (Type)? SymTab0 : SymTab;
+    SymTable* Tab = IsFunc? SymTab0 : SymTab;
 
     /* Do we have an entry with this name already? */
     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
@@ -675,7 +686,7 @@ SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
 
        /* We have a symbol with this name already */
        if (Entry->Flags & SC_TYPE) {
-           Error (ERR_MULTIPLE_DEFINITION, Name);
+           Error ("Multiple definition for `%s'", Name);
            return Entry;
        }
 
@@ -695,7 +706,8 @@ SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
            if ((Size != 0 && ESize != 0) ||
                TypeCmp (Type+DECODE_SIZE+1, EType+DECODE_SIZE+1) < TC_EQUAL) {
                /* Types not identical: Conflicting types */
-               Error (ERR_CONFLICTING_TYPES, Name);
+               Error ("Conflicting types for `%s'", Name);
+               return Entry;
            } else {
                /* Check if we have a size in the existing definition */
                if (ESize == 0) {
@@ -707,15 +719,20 @@ SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
                } else {
            /* New type must be identical */
            if (TypeCmp (EType, Type) < TC_EQUAL) {
-               Error (ERR_CONFLICTING_TYPES, Name);
+               Error ("Conflicting types for `%s'", Name);
+               return Entry;
            }
 
            /* In case of a function, use the new type descriptor, since it
             * contains pointers to the new symbol tables that are needed if
             * an actual function definition follows.
             */
-           if (IsTypeFunc (Type)) {
-               CopyEncode (Type+1, EType+1);
+           if (IsFunc) {
+               /* Get the function descriptor from the new type */
+               FuncDesc* F = GetFuncDesc (Type);
+               /* Use this new function descriptor */
+               Entry->V.F.Func = F;
+               EncodePtr (EType+1, F);
            }
        }
 
@@ -730,6 +747,14 @@ SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
        /* Set the symbol attributes */
        Entry->Type = TypeDup (Type);
 
+       /* If this is a function, set the function descriptor and clear
+        * additional fields.
+        */
+       if (IsFunc) {
+           Entry->V.F.Func = GetFuncDesc (Entry->Type);
+           Entry->V.F.Seg  = 0;
+       }
+
        /* Add the entry to the symbol table */
        AddSymEntry (Tab, Entry);
     }
@@ -754,6 +779,14 @@ SymTable* GetSymTab (void)
 
 
 
+SymTable* GetGlobalSymTab (void)
+/* Return the global symbol table */
+{
+    return SymTab0;
+}
+
+
+
 int SymIsLocal (SymEntry* Sym)
 /* Return true if the symbol is defined in the highest lexical level */
 {
@@ -772,7 +805,7 @@ void MakeZPSym (const char* Name)
     if (Entry) {
        Entry->Flags |= SC_ZEROPAGE;
     } else {
-       Error (ERR_UNDEFINED_SYMBOL, Name);
+       Error ("Undefined symbol: `%s'", Name);
     }
 }
 
@@ -818,8 +851,6 @@ void EmitExternals (void)
 {
     SymEntry* Entry;
 
-    AddEmptyLine ();
-
     Entry = SymTab->SymHead;
     while (Entry) {
        unsigned Flags = Entry->Flags;