]> git.sur5r.net Git - cc65/blobdiff - src/cc65/declattr.c
Fixed several type conversion issues
[cc65] / src / cc65 / declattr.c
index 9450356b781e22193a9df39a3117d8c0f67fb926..3ac3e147974fb15987bb56fd0da81be94d694819 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                               declattr.c                                 */
+/*                               declattr.c                                 */
 /*                                                                           */
 /*                         Declaration attributes                           */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 2000-2002 Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -33,6 +33,8 @@
 
 
 
+#include <string.h>
+
 /* cc65 */
 #include "error.h"
 #include "scanner.h"
 
 
 
-/* Attribute names */
-static const char* const AttrNames [atCount] = {
-    "alias",
+/* Forwards for attribute handlers */
+static void AliasAttr (const Declaration* D, DeclAttr* A);
+static void UnusedAttr (const Declaration* D, DeclAttr* A);
+static void ZeroPageAttr (const Declaration* D, DeclAttr* A);
+
+
+
+/* Attribute table */
+typedef struct AttrDesc AttrDesc;
+struct AttrDesc {
+    const char Name[12];
+    void       (*Handler) (const Declaration*, DeclAttr*);
+};
+static const AttrDesc AttrTable [atCount] = {
+    { "alias",         AliasAttr       },
+    { "unused",                UnusedAttr      },
+    { "zeropage",              ZeroPageAttr    },
 };
 
 
@@ -61,28 +77,28 @@ static const char* const AttrNames [atCount] = {
 
 
 
-static attrib_t FindAttribute (const char* Attr)
-/* Search the attribute and return the corresponding attribute constant.
- * Return atNone if the attribute name is not known.
+static const AttrDesc* FindAttribute (const char* Attr)
+/* Search the attribute and return the corresponding attribute descriptor.
+ * Return NULL if the attribute name is not known.
  */
 {
-    attrib_t A;
+    unsigned A;
 
     /* For now do a linear search */
     for (A = 0; A < atCount; ++A) {
-       if (strcmp (Attr, AttrNames[A]) == 0) {
+               if (strcmp (Attr, AttrTable[A].Name) == 0) {
            /* Found */
-                   return A;
+                   return AttrTable + A;
        }
     }
 
     /* Not found */
-    return atNone;
+    return 0;
 }
 
 
 
-static void AliasAttrib (const Declaration* D, DeclAttr* A)
+static void AliasAttr (const Declaration* D, DeclAttr* A)
 /* Handle the "alias" attribute */
 {
     SymEntry* Sym;
@@ -92,14 +108,14 @@ static void AliasAttrib (const Declaration* D, DeclAttr* A)
 
     /* The next identifier is the name of the alias symbol */
     if (CurTok.Tok != TOK_IDENT) {
-               Error (ERR_IDENT_EXPECTED);
+               Error ("Identifier expected");
        return;
     }
 
     /* Lookup the symbol for this name, it must exist */
     Sym = FindSym (CurTok.Ident);
     if (Sym == 0) {
-       Error (ERR_UNKNOWN_IDENT, CurTok.Ident);
+       Error ("Unknown identifier: `%s'", CurTok.Ident);
        NextToken ();
        return;
     }
@@ -110,7 +126,7 @@ static void AliasAttrib (const Declaration* D, DeclAttr* A)
     /* Check if the types of the symbols are identical */
     if (TypeCmp (D->Type, Sym->Type) < TC_EQUAL) {
        /* Types are not identical */
-       Error (ERR_INCOMPATIBLE_TYPES);
+       Error ("Incompatible types");
        return;
     }
 
@@ -121,10 +137,29 @@ static void AliasAttrib (const Declaration* D, DeclAttr* A)
 
 
 
+static void UnusedAttr (const Declaration* D attribute ((unused)), DeclAttr* A)
+/* Handle the "unused" attribute */
+{
+    /* No parameters */
+    A->AttrType = atUnused;
+}
+
+
+
+static void ZeroPageAttr (const Declaration* D attribute ((unused)), DeclAttr* A)
+/* Handle the "zeropage" attribute */
+{
+    /* No parameters */
+    A->AttrType = atZeroPage;
+}
+
+
+
 void ParseAttribute (const Declaration* D, DeclAttr* A)
 /* Parse an additional __attribute__ modifier */
 {
-    attrib_t AttrType;
+    ident          AttrName;
+    const AttrDesc* Attr;
 
     /* Initialize the attribute description with "no attribute" */
     A->AttrType = atNone;
@@ -144,31 +179,43 @@ void ParseAttribute (const Declaration* D, DeclAttr* A)
 
     /* Identifier follows */
     if (CurTok.Tok != TOK_IDENT) {
-               Error (ERR_IDENT_EXPECTED);
+               Error ("Identifier expected");
        /* We should *really* try to recover here, but for now: */
        return;
     }
 
     /* Map the attribute name to its id, then skip the identifier */
-    AttrType = FindAttribute (CurTok.Ident);
+    strcpy (AttrName, CurTok.Ident);
+    Attr = FindAttribute (AttrName);
     NextToken ();
 
-    /* Handle possible attributes */
-    switch (AttrType) {
+    /* Did we find a valid attribute? */
+    if (Attr) {
 
-       case atAlias:
-           AliasAttrib (D, A);
-           break;
+       /* Call the handler */
+       Attr->Handler (D, A);
 
-       default:
-           /* Attribute not known, maybe typo */
-           Error (ERR_ILLEGAL_ATTRIBUTE);
-           break;
-    }
+       /* Read the two closing braces */
+       ConsumeRParen ();
+       ConsumeRParen ();
 
-    /* Read the two closing braces */
-    ConsumeRParen ();
-    ConsumeRParen ();
+    } else {
+       /* List of tokens to skip */
+       static const token_t SkipList[] = { TOK_LPAREN, TOK_SEMI };
+
+       /* Attribute not known, maybe typo */
+       Error ("Illegal attribute: `%s'", AttrName);
+
+       /* Skip until closing brace or semicolon */
+       SkipTokens (SkipList, sizeof (SkipList) / sizeof (SkipList[0]));
+
+       /* If we have a closing brace, read it, otherwise bail out */
+       if (CurTok.Tok == TOK_LPAREN) {
+           /* Read the two closing braces */
+           ConsumeRParen ();
+           ConsumeRParen ();
+       }
+    }
 }