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