1 /*****************************************************************************/
5 /* Declaration attributes */
9 /* (C) 2000-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
53 /* Forwards for attribute handlers */
54 static void AliasAttr (const Declaration* D, DeclAttr* A);
55 static void UnusedAttr (const Declaration* D, DeclAttr* A);
56 static void ZeroPageAttr (const Declaration* D, DeclAttr* A);
61 typedef struct AttrDesc AttrDesc;
64 void (*Handler) (const Declaration*, DeclAttr*);
66 static const AttrDesc AttrTable [atCount] = {
67 { "alias", AliasAttr },
68 { "unused", UnusedAttr },
69 { "zeropage", ZeroPageAttr },
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static const AttrDesc* FindAttribute (const char* Attr)
81 /* Search the attribute and return the corresponding attribute descriptor.
82 * Return NULL if the attribute name is not known.
87 /* For now do a linear search */
88 for (A = 0; A < atCount; ++A) {
89 if (strcmp (Attr, AttrTable[A].Name) == 0) {
101 static void AliasAttr (const Declaration* D, DeclAttr* A)
102 /* Handle the "alias" attribute */
109 /* The next identifier is the name of the alias symbol */
110 if (CurTok.Tok != TOK_IDENT) {
111 Error ("Identifier expected");
115 /* Lookup the symbol for this name, it must exist */
116 Sym = FindSym (CurTok.Ident);
118 Error ("Unknown identifier: `%s'", CurTok.Ident);
123 /* Since we have the symbol entry now, skip the name */
126 /* Check if the types of the symbols are identical */
127 if (TypeCmp (D->Type, Sym->Type) < TC_EQUAL) {
128 /* Types are not identical */
129 Error ("Incompatible types");
133 /* Attribute is verified, set the stuff in the attribute description */
134 A->AttrType = atAlias;
140 static void UnusedAttr (const Declaration* D attribute ((unused)), DeclAttr* A)
141 /* Handle the "unused" attribute */
144 A->AttrType = atUnused;
149 static void ZeroPageAttr (const Declaration* D attribute ((unused)), DeclAttr* A)
150 /* Handle the "zeropage" attribute */
153 A->AttrType = atZeroPage;
158 void ParseAttribute (const Declaration* D, DeclAttr* A)
159 /* Parse an additional __attribute__ modifier */
162 const AttrDesc* Attr;
164 /* Initialize the attribute description with "no attribute" */
165 A->AttrType = atNone;
167 /* Do we have an attribute? */
168 if (CurTok.Tok != TOK_ATTRIBUTE) {
169 /* No attribute, bail out */
173 /* Skip the attribute token */
176 /* Expect two(!) open braces */
180 /* Identifier follows */
181 if (CurTok.Tok != TOK_IDENT) {
182 Error ("Identifier expected");
183 /* We should *really* try to recover here, but for now: */
187 /* Map the attribute name to its id, then skip the identifier */
188 strcpy (AttrName, CurTok.Ident);
189 Attr = FindAttribute (AttrName);
192 /* Did we find a valid attribute? */
195 /* Call the handler */
196 Attr->Handler (D, A);
198 /* Read the two closing braces */
203 /* List of tokens to skip */
204 static const token_t SkipList[] = { TOK_LPAREN, TOK_SEMI };
206 /* Attribute not known, maybe typo */
207 Error ("Illegal attribute: `%s'", AttrName);
209 /* Skip until closing brace or semicolon */
210 SkipTokens (SkipList, sizeof (SkipList) / sizeof (SkipList[0]));
212 /* If we have a closing brace, read it, otherwise bail out */
213 if (CurTok.Tok == TOK_LPAREN) {
214 /* Read the two closing braces */