]>
Commit | Line | Data |
---|---|---|
bb9f8221 | 1 | /* Functions dealing with attribute handling, used by most front ends. |
2cc2d4bb | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
9dcd6f09 | 3 | 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. |
bb9f8221 RK |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
bb9f8221 RK |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
bb9f8221 RK |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
4977bab6 ZW |
23 | #include "coretypes.h" |
24 | #include "tm.h" | |
bb9f8221 RK |
25 | #include "tree.h" |
26 | #include "flags.h" | |
27 | #include "toplev.h" | |
28 | #include "output.h" | |
29 | #include "rtl.h" | |
30 | #include "ggc.h" | |
bb9f8221 | 31 | #include "tm_p.h" |
bb9f8221 RK |
32 | #include "cpplib.h" |
33 | #include "target.h" | |
7ffb4fd2 | 34 | #include "langhooks.h" |
bb9f8221 | 35 | |
4682ae04 | 36 | static void init_attributes (void); |
bb9f8221 | 37 | |
349ae713 | 38 | /* Table of the tables of attributes (common, language, format, machine) |
bb9f8221 RK |
39 | searched. */ |
40 | static const struct attribute_spec *attribute_tables[4]; | |
41 | ||
42 | static bool attributes_initialized = false; | |
43 | ||
bb9f8221 RK |
44 | /* Default empty table of attributes. */ |
45 | static const struct attribute_spec empty_attribute_table[] = | |
46 | { | |
47 | { NULL, 0, 0, false, false, false, NULL } | |
48 | }; | |
49 | ||
bb9f8221 RK |
50 | /* Initialize attribute tables, and make some sanity checks |
51 | if --enable-checking. */ | |
52 | ||
53 | static void | |
4682ae04 | 54 | init_attributes (void) |
bb9f8221 | 55 | { |
ca7558fc | 56 | size_t i; |
bb9f8221 | 57 | |
349ae713 NB |
58 | attribute_tables[0] = lang_hooks.common_attribute_table; |
59 | attribute_tables[1] = lang_hooks.attribute_table; | |
60 | attribute_tables[2] = lang_hooks.format_attribute_table; | |
bb9f8221 RK |
61 | attribute_tables[3] = targetm.attribute_table; |
62 | ||
349ae713 NB |
63 | /* Translate NULL pointers to pointers to the empty table. */ |
64 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
65 | if (attribute_tables[i] == NULL) | |
66 | attribute_tables[i] = empty_attribute_table; | |
67 | ||
bb9f8221 RK |
68 | #ifdef ENABLE_CHECKING |
69 | /* Make some sanity checks on the attribute tables. */ | |
ca7558fc | 70 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
bb9f8221 RK |
71 | { |
72 | int j; | |
73 | ||
74 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
75 | { | |
76 | /* The name must not begin and end with __. */ | |
77 | const char *name = attribute_tables[i][j].name; | |
78 | int len = strlen (name); | |
c22cacf3 | 79 | |
298e6adc NS |
80 | gcc_assert (!(name[0] == '_' && name[1] == '_' |
81 | && name[len - 1] == '_' && name[len - 2] == '_')); | |
c22cacf3 | 82 | |
bb9f8221 | 83 | /* The minimum and maximum lengths must be consistent. */ |
298e6adc | 84 | gcc_assert (attribute_tables[i][j].min_length >= 0); |
c22cacf3 | 85 | |
298e6adc NS |
86 | gcc_assert (attribute_tables[i][j].max_length == -1 |
87 | || (attribute_tables[i][j].max_length | |
88 | >= attribute_tables[i][j].min_length)); | |
c22cacf3 | 89 | |
bb9f8221 | 90 | /* An attribute cannot require both a DECL and a TYPE. */ |
298e6adc NS |
91 | gcc_assert (!attribute_tables[i][j].decl_required |
92 | || !attribute_tables[i][j].type_required); | |
c22cacf3 | 93 | |
bb9f8221 RK |
94 | /* If an attribute requires a function type, in particular |
95 | it requires a type. */ | |
298e6adc NS |
96 | gcc_assert (!attribute_tables[i][j].function_type_required |
97 | || attribute_tables[i][j].type_required); | |
bb9f8221 RK |
98 | } |
99 | } | |
100 | ||
101 | /* Check that each name occurs just once in each table. */ | |
ca7558fc | 102 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
bb9f8221 RK |
103 | { |
104 | int j, k; | |
105 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
106 | for (k = j + 1; attribute_tables[i][k].name != NULL; k++) | |
298e6adc NS |
107 | gcc_assert (strcmp (attribute_tables[i][j].name, |
108 | attribute_tables[i][k].name)); | |
bb9f8221 RK |
109 | } |
110 | /* Check that no name occurs in more than one table. */ | |
ca7558fc | 111 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
bb9f8221 | 112 | { |
ca7558fc | 113 | size_t j, k, l; |
bb9f8221 | 114 | |
ca7558fc | 115 | for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++) |
bb9f8221 RK |
116 | for (k = 0; attribute_tables[i][k].name != NULL; k++) |
117 | for (l = 0; attribute_tables[j][l].name != NULL; l++) | |
298e6adc NS |
118 | gcc_assert (strcmp (attribute_tables[i][k].name, |
119 | attribute_tables[j][l].name)); | |
bb9f8221 RK |
120 | } |
121 | #endif | |
122 | ||
123 | attributes_initialized = true; | |
124 | } | |
125 | \f | |
126 | /* Process the attributes listed in ATTRIBUTES and install them in *NODE, | |
127 | which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, | |
128 | it should be modified in place; if a TYPE, a copy should be created | |
129 | unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further | |
130 | information, in the form of a bitwise OR of flags in enum attribute_flags | |
131 | from tree.h. Depending on these flags, some attributes may be | |
132 | returned to be applied at a later stage (for example, to apply | |
03aa99d4 | 133 | a decl attribute to the declaration rather than to its type). */ |
bb9f8221 RK |
134 | |
135 | tree | |
4682ae04 | 136 | decl_attributes (tree *node, tree attributes, int flags) |
bb9f8221 RK |
137 | { |
138 | tree a; | |
139 | tree returned_attrs = NULL_TREE; | |
140 | ||
141 | if (!attributes_initialized) | |
142 | init_attributes (); | |
143 | ||
5fd9b178 | 144 | targetm.insert_attributes (*node, &attributes); |
bb9f8221 RK |
145 | |
146 | for (a = attributes; a; a = TREE_CHAIN (a)) | |
147 | { | |
148 | tree name = TREE_PURPOSE (a); | |
149 | tree args = TREE_VALUE (a); | |
150 | tree *anode = node; | |
151 | const struct attribute_spec *spec = NULL; | |
152 | bool no_add_attrs = 0; | |
3acef2ae | 153 | tree fn_ptr_tmp = NULL_TREE; |
2b03d201 | 154 | size_t i; |
bb9f8221 | 155 | |
ca7558fc | 156 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
bb9f8221 RK |
157 | { |
158 | int j; | |
159 | ||
160 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
161 | { | |
162 | if (is_attribute_p (attribute_tables[i][j].name, name)) | |
163 | { | |
164 | spec = &attribute_tables[i][j]; | |
165 | break; | |
166 | } | |
167 | } | |
168 | if (spec != NULL) | |
169 | break; | |
170 | } | |
171 | ||
172 | if (spec == NULL) | |
173 | { | |
5c498b10 | 174 | warning (OPT_Wattributes, "%qs attribute directive ignored", |
bb9f8221 RK |
175 | IDENTIFIER_POINTER (name)); |
176 | continue; | |
177 | } | |
178 | else if (list_length (args) < spec->min_length | |
179 | || (spec->max_length >= 0 | |
180 | && list_length (args) > spec->max_length)) | |
181 | { | |
971801ff | 182 | error ("wrong number of arguments specified for %qs attribute", |
bb9f8221 RK |
183 | IDENTIFIER_POINTER (name)); |
184 | continue; | |
185 | } | |
186 | ||
187 | if (spec->decl_required && !DECL_P (*anode)) | |
188 | { | |
189 | if (flags & ((int) ATTR_FLAG_DECL_NEXT | |
190 | | (int) ATTR_FLAG_FUNCTION_NEXT | |
191 | | (int) ATTR_FLAG_ARRAY_NEXT)) | |
192 | { | |
193 | /* Pass on this attribute to be tried again. */ | |
194 | returned_attrs = tree_cons (name, args, returned_attrs); | |
195 | continue; | |
196 | } | |
197 | else | |
198 | { | |
5c498b10 | 199 | warning (OPT_Wattributes, "%qs attribute does not apply to types", |
bb9f8221 RK |
200 | IDENTIFIER_POINTER (name)); |
201 | continue; | |
202 | } | |
203 | } | |
204 | ||
dd4dc3cd RK |
205 | /* If we require a type, but were passed a decl, set up to make a |
206 | new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE | |
207 | would have applied if we'd been passed a type, but we cannot modify | |
208 | the decl's type in place here. */ | |
bb9f8221 | 209 | if (spec->type_required && DECL_P (*anode)) |
dd4dc3cd RK |
210 | { |
211 | anode = &TREE_TYPE (*anode); | |
212 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; | |
213 | } | |
bb9f8221 RK |
214 | |
215 | if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE | |
216 | && TREE_CODE (*anode) != METHOD_TYPE) | |
217 | { | |
218 | if (TREE_CODE (*anode) == POINTER_TYPE | |
219 | && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE | |
220 | || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE)) | |
221 | { | |
3acef2ae JM |
222 | /* OK, this is a bit convoluted. We can't just make a copy |
223 | of the pointer type and modify its TREE_TYPE, because if | |
224 | we change the attributes of the target type the pointer | |
225 | type needs to have a different TYPE_MAIN_VARIANT. So we | |
226 | pull out the target type now, frob it as appropriate, and | |
227 | rebuild the pointer type later. | |
228 | ||
c22cacf3 MS |
229 | This would all be simpler if attributes were part of the |
230 | declarator, grumble grumble. */ | |
3acef2ae JM |
231 | fn_ptr_tmp = TREE_TYPE (*anode); |
232 | anode = &fn_ptr_tmp; | |
233 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; | |
bb9f8221 RK |
234 | } |
235 | else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
236 | { | |
237 | /* Pass on this attribute to be tried again. */ | |
238 | returned_attrs = tree_cons (name, args, returned_attrs); | |
239 | continue; | |
240 | } | |
241 | ||
242 | if (TREE_CODE (*anode) != FUNCTION_TYPE | |
243 | && TREE_CODE (*anode) != METHOD_TYPE) | |
244 | { | |
5c498b10 DD |
245 | warning (OPT_Wattributes, |
246 | "%qs attribute only applies to function types", | |
bb9f8221 RK |
247 | IDENTIFIER_POINTER (name)); |
248 | continue; | |
249 | } | |
250 | } | |
251 | ||
b9e75696 JM |
252 | if (TYPE_P (*anode) |
253 | && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) | |
254 | && TYPE_SIZE (*anode) != NULL_TREE) | |
255 | { | |
256 | warning (OPT_Wattributes, "type attributes ignored after type is already defined"); | |
257 | continue; | |
258 | } | |
259 | ||
bb9f8221 RK |
260 | if (spec->handler != NULL) |
261 | returned_attrs = chainon ((*spec->handler) (anode, name, args, | |
262 | flags, &no_add_attrs), | |
263 | returned_attrs); | |
1b9191d2 AH |
264 | |
265 | /* Layout the decl in case anything changed. */ | |
266 | if (spec->type_required && DECL_P (*node) | |
67282790 JM |
267 | && (TREE_CODE (*node) == VAR_DECL |
268 | || TREE_CODE (*node) == PARM_DECL | |
269 | || TREE_CODE (*node) == RESULT_DECL)) | |
d1838621 | 270 | relayout_decl (*node); |
1b9191d2 | 271 | |
bb9f8221 RK |
272 | if (!no_add_attrs) |
273 | { | |
274 | tree old_attrs; | |
275 | tree a; | |
276 | ||
277 | if (DECL_P (*anode)) | |
278 | old_attrs = DECL_ATTRIBUTES (*anode); | |
279 | else | |
280 | old_attrs = TYPE_ATTRIBUTES (*anode); | |
281 | ||
282 | for (a = lookup_attribute (spec->name, old_attrs); | |
283 | a != NULL_TREE; | |
284 | a = lookup_attribute (spec->name, TREE_CHAIN (a))) | |
285 | { | |
286 | if (simple_cst_equal (TREE_VALUE (a), args) == 1) | |
287 | break; | |
288 | } | |
289 | ||
290 | if (a == NULL_TREE) | |
291 | { | |
292 | /* This attribute isn't already in the list. */ | |
293 | if (DECL_P (*anode)) | |
294 | DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); | |
295 | else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) | |
67214984 JM |
296 | { |
297 | TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); | |
298 | /* If this is the main variant, also push the attributes | |
299 | out to the other variants. */ | |
300 | if (*anode == TYPE_MAIN_VARIANT (*anode)) | |
301 | { | |
302 | tree variant; | |
303 | for (variant = *anode; variant; | |
304 | variant = TYPE_NEXT_VARIANT (variant)) | |
305 | { | |
306 | if (TYPE_ATTRIBUTES (variant) == old_attrs) | |
307 | TYPE_ATTRIBUTES (variant) | |
308 | = TYPE_ATTRIBUTES (*anode); | |
309 | else if (!lookup_attribute | |
310 | (spec->name, TYPE_ATTRIBUTES (variant))) | |
311 | TYPE_ATTRIBUTES (variant) = tree_cons | |
312 | (name, args, TYPE_ATTRIBUTES (variant)); | |
313 | } | |
314 | } | |
315 | } | |
bb9f8221 RK |
316 | else |
317 | *anode = build_type_attribute_variant (*anode, | |
318 | tree_cons (name, args, | |
319 | old_attrs)); | |
320 | } | |
321 | } | |
3acef2ae JM |
322 | |
323 | if (fn_ptr_tmp) | |
324 | { | |
325 | /* Rebuild the function pointer type and put it in the | |
326 | appropriate place. */ | |
327 | fn_ptr_tmp = build_pointer_type (fn_ptr_tmp); | |
328 | if (DECL_P (*node)) | |
329 | TREE_TYPE (*node) = fn_ptr_tmp; | |
3acef2ae | 330 | else |
298e6adc NS |
331 | { |
332 | gcc_assert (TREE_CODE (*node) == POINTER_TYPE); | |
333 | *node = fn_ptr_tmp; | |
334 | } | |
3acef2ae | 335 | } |
bb9f8221 RK |
336 | } |
337 | ||
338 | return returned_attrs; | |
339 | } |