1
0
mirror of https://github.com/cc65/cc65.git synced 2024-09-06 02:54:33 +00:00

Attribute work

git-svn-id: svn://svn.cc65.org/cc65/trunk@1201 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2002-03-18 06:05:04 +00:00
parent fe1f354fe8
commit 5bdb19f63c
2 changed files with 81 additions and 36 deletions

View File

@ -1,15 +1,15 @@
/*****************************************************************************/ /*****************************************************************************/
/* */ /* */
/* declattr.c */ /* declattr.c */
/* */ /* */
/* Declaration attributes */ /* Declaration attributes */
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000 Ullrich von Bassewitz */ /* (C) 2000-2002 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -50,9 +50,23 @@
/* Attribute names */ /* Forwards for attribute handlers */
static const char* const AttrNames [atCount] = { static void AliasAttr (const Declaration* D, DeclAttr* A);
"alias", 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 },
}; };
@ -63,28 +77,28 @@ static const char* const AttrNames [atCount] = {
static attrib_t FindAttribute (const char* Attr) static const AttrDesc* FindAttribute (const char* Attr)
/* Search the attribute and return the corresponding attribute constant. /* Search the attribute and return the corresponding attribute descriptor.
* Return atNone if the attribute name is not known. * Return NULL if the attribute name is not known.
*/ */
{ {
int A; unsigned A;
/* For now do a linear search */ /* For now do a linear search */
for (A = 0; A < atCount; ++A) { for (A = 0; A < atCount; ++A) {
if (strcmp (Attr, AttrNames[A]) == 0) { if (strcmp (Attr, AttrTable[A].Name) == 0) {
/* Found */ /* Found */
return (attrib_t) A; return AttrTable + A;
} }
} }
/* Not found */ /* 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 */ /* Handle the "alias" attribute */
{ {
SymEntry* Sym; SymEntry* Sym;
@ -123,11 +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) void ParseAttribute (const Declaration* D, DeclAttr* A)
/* Parse an additional __attribute__ modifier */ /* Parse an additional __attribute__ modifier */
{ {
ident AttrName; ident AttrName;
attrib_t AttrType; const AttrDesc* Attr;
/* Initialize the attribute description with "no attribute" */ /* Initialize the attribute description with "no attribute" */
A->AttrType = atNone; A->AttrType = atNone;
@ -154,25 +186,36 @@ void ParseAttribute (const Declaration* D, DeclAttr* A)
/* Map the attribute name to its id, then skip the identifier */ /* Map the attribute name to its id, then skip the identifier */
strcpy (AttrName, CurTok.Ident); strcpy (AttrName, CurTok.Ident);
AttrType = FindAttribute (AttrName); Attr = FindAttribute (AttrName);
NextToken (); NextToken ();
/* Handle possible attributes */ /* Did we find a valid attribute? */
switch (AttrType) { if (Attr) {
case atAlias: /* Call the handler */
AliasAttrib (D, A); Attr->Handler (D, A);
break;
default: /* Read the two closing braces */
/* Attribute not known, maybe typo */ ConsumeRParen ();
Error ("Illegal attribute: `%s'", AttrName); ConsumeRParen ();
break;
} 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 ();
}
} }
/* Read the two closing braces */
ConsumeRParen ();
ConsumeRParen ();
} }

View File

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000 Ullrich von Bassewitz */ /* (C) 2000-2002 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -52,6 +52,8 @@
typedef enum { typedef enum {
atNone = -1, /* No attribute */ atNone = -1, /* No attribute */
atAlias, /* Alias declaration */ atAlias, /* Alias declaration */
atUnused, /* Variable is unused */
atZeroPage, /* Zero page symbol */
atCount /* Number of attributes */ atCount /* Number of attributes */
} attrib_t; } attrib_t;