]> git.sur5r.net Git - cc65/blob - src/cc65/declattr.c
More optimizations
[cc65] / src / cc65 / declattr.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                declattr.c                                 */
4 /*                                                                           */
5 /*                          Declaration attributes                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* cc65 */
39 #include "error.h"
40 #include "scanner.h"
41 #include "symtab.h"
42 #include "typecmp.h"
43 #include "declattr.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /* Attribute names */
54 static const char* const AttrNames [atCount] = {
55     "alias",
56 };
57
58
59
60 /*****************************************************************************/
61 /*                                   Code                                    */
62 /*****************************************************************************/
63
64
65
66 static attrib_t FindAttribute (const char* Attr)
67 /* Search the attribute and return the corresponding attribute constant.
68  * Return atNone if the attribute name is not known.
69  */
70 {
71     int A;
72
73     /* For now do a linear search */
74     for (A = 0; A < atCount; ++A) {
75         if (strcmp (Attr, AttrNames[A]) == 0) {
76             /* Found */
77             return (attrib_t) A;
78         }
79     }
80
81     /* Not found */
82     return atNone;
83 }
84
85
86
87 static void AliasAttrib (const Declaration* D, DeclAttr* A)
88 /* Handle the "alias" attribute */
89 {
90     SymEntry* Sym;
91
92     /* Comma expected */
93     ConsumeComma ();
94
95     /* The next identifier is the name of the alias symbol */
96     if (CurTok.Tok != TOK_IDENT) {
97         Error ("Identifier expected");
98         return;
99     }
100
101     /* Lookup the symbol for this name, it must exist */
102     Sym = FindSym (CurTok.Ident);
103     if (Sym == 0) {
104         Error ("Unknown identifier: `%s'", CurTok.Ident);
105         NextToken ();
106         return;
107     }
108
109     /* Since we have the symbol entry now, skip the name */
110     NextToken ();
111
112     /* Check if the types of the symbols are identical */
113     if (TypeCmp (D->Type, Sym->Type) < TC_EQUAL) {
114         /* Types are not identical */
115         Error ("Incompatible types");
116         return;
117     }
118
119     /* Attribute is verified, set the stuff in the attribute description */
120     A->AttrType = atAlias;
121     A->V.Sym    = Sym;
122 }
123
124
125
126 void ParseAttribute (const Declaration* D, DeclAttr* A)
127 /* Parse an additional __attribute__ modifier */
128 {
129     ident    AttrName;
130     attrib_t AttrType;
131
132     /* Initialize the attribute description with "no attribute" */
133     A->AttrType = atNone;
134
135     /* Do we have an attribute? */
136     if (CurTok.Tok != TOK_ATTRIBUTE) {
137         /* No attribute, bail out */
138         return;
139     }
140
141     /* Skip the attribute token */
142     NextToken ();
143
144     /* Expect two(!) open braces */
145     ConsumeLParen ();
146     ConsumeLParen ();
147
148     /* Identifier follows */
149     if (CurTok.Tok != TOK_IDENT) {
150         Error ("Identifier expected");
151         /* We should *really* try to recover here, but for now: */
152         return;
153     }
154
155     /* Map the attribute name to its id, then skip the identifier */
156     strcpy (AttrName, CurTok.Ident);
157     AttrType = FindAttribute (AttrName);
158     NextToken ();
159
160     /* Handle possible attributes */
161     switch (AttrType) {
162
163         case atAlias:
164             AliasAttrib (D, A);
165             break;
166
167         default:
168             /* Attribute not known, maybe typo */
169             Error ("Illegal attribute: `%s'", AttrName);
170             break;
171     }
172
173     /* Read the two closing braces */
174     ConsumeRParen ();
175     ConsumeRParen ();
176 }
177
178
179