]>
Commit | Line | Data |
---|---|---|
e3f6ce11 | 1 | /* Functions dealing with attribute handling, used by most front ends. |
80fabb90 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
3 | 2002 Free Software Foundation, Inc. | |
e3f6ce11 | 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 | |
9 | Software Foundation; either version 2, or (at your option) any later | |
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 | |
18 | along with GCC; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "tree.h" | |
25 | #include "flags.h" | |
26 | #include "toplev.h" | |
27 | #include "output.h" | |
28 | #include "rtl.h" | |
29 | #include "ggc.h" | |
30 | #include "expr.h" | |
31 | #include "tm_p.h" | |
32 | #include "obstack.h" | |
33 | #include "cpplib.h" | |
34 | #include "target.h" | |
26ca6c20 | 35 | #include "langhooks.h" |
e3f6ce11 | 36 | |
37 | static void init_attributes PARAMS ((void)); | |
38 | ||
f8e93a2e | 39 | /* Table of the tables of attributes (common, language, format, machine) |
e3f6ce11 | 40 | searched. */ |
41 | static const struct attribute_spec *attribute_tables[4]; | |
42 | ||
43 | static bool attributes_initialized = false; | |
44 | ||
e3f6ce11 | 45 | /* Default empty table of attributes. */ |
46 | static const struct attribute_spec empty_attribute_table[] = | |
47 | { | |
48 | { NULL, 0, 0, false, false, false, NULL } | |
49 | }; | |
50 | ||
e3f6ce11 | 51 | /* Initialize attribute tables, and make some sanity checks |
52 | if --enable-checking. */ | |
53 | ||
54 | static void | |
55 | init_attributes () | |
56 | { | |
3585dac7 | 57 | size_t i; |
e3f6ce11 | 58 | |
f8e93a2e | 59 | attribute_tables[0] = lang_hooks.common_attribute_table; |
60 | attribute_tables[1] = lang_hooks.attribute_table; | |
61 | attribute_tables[2] = lang_hooks.format_attribute_table; | |
e3f6ce11 | 62 | attribute_tables[3] = targetm.attribute_table; |
63 | ||
f8e93a2e | 64 | /* Translate NULL pointers to pointers to the empty table. */ |
65 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
66 | if (attribute_tables[i] == NULL) | |
67 | attribute_tables[i] = empty_attribute_table; | |
68 | ||
e3f6ce11 | 69 | #ifdef ENABLE_CHECKING |
70 | /* Make some sanity checks on the attribute tables. */ | |
3585dac7 | 71 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
e3f6ce11 | 72 | { |
73 | int j; | |
74 | ||
75 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
76 | { | |
77 | /* The name must not begin and end with __. */ | |
78 | const char *name = attribute_tables[i][j].name; | |
79 | int len = strlen (name); | |
80 | if (name[0] == '_' && name[1] == '_' | |
81 | && name[len - 1] == '_' && name[len - 2] == '_') | |
82 | abort (); | |
83 | /* The minimum and maximum lengths must be consistent. */ | |
84 | if (attribute_tables[i][j].min_length < 0) | |
85 | abort (); | |
86 | if (attribute_tables[i][j].max_length != -1 | |
87 | && (attribute_tables[i][j].max_length | |
88 | < attribute_tables[i][j].min_length)) | |
89 | abort (); | |
90 | /* An attribute cannot require both a DECL and a TYPE. */ | |
91 | if (attribute_tables[i][j].decl_required | |
92 | && attribute_tables[i][j].type_required) | |
93 | abort (); | |
94 | /* If an attribute requires a function type, in particular | |
95 | it requires a type. */ | |
96 | if (attribute_tables[i][j].function_type_required | |
97 | && !attribute_tables[i][j].type_required) | |
98 | abort (); | |
99 | } | |
100 | } | |
101 | ||
102 | /* Check that each name occurs just once in each table. */ | |
3585dac7 | 103 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
e3f6ce11 | 104 | { |
105 | int j, k; | |
106 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
107 | for (k = j + 1; attribute_tables[i][k].name != NULL; k++) | |
108 | if (!strcmp (attribute_tables[i][j].name, | |
109 | attribute_tables[i][k].name)) | |
110 | abort (); | |
111 | } | |
112 | /* Check that no name occurs in more than one table. */ | |
3585dac7 | 113 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
e3f6ce11 | 114 | { |
3585dac7 | 115 | size_t j, k, l; |
e3f6ce11 | 116 | |
3585dac7 | 117 | for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++) |
e3f6ce11 | 118 | for (k = 0; attribute_tables[i][k].name != NULL; k++) |
119 | for (l = 0; attribute_tables[j][l].name != NULL; l++) | |
120 | if (!strcmp (attribute_tables[i][k].name, | |
121 | attribute_tables[j][l].name)) | |
122 | abort (); | |
123 | } | |
124 | #endif | |
125 | ||
126 | attributes_initialized = true; | |
127 | } | |
128 | \f | |
129 | /* Process the attributes listed in ATTRIBUTES and install them in *NODE, | |
130 | which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, | |
131 | it should be modified in place; if a TYPE, a copy should be created | |
132 | unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further | |
133 | information, in the form of a bitwise OR of flags in enum attribute_flags | |
134 | from tree.h. Depending on these flags, some attributes may be | |
135 | returned to be applied at a later stage (for example, to apply | |
7d3b509a | 136 | a decl attribute to the declaration rather than to its type). If |
137 | ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider | |
138 | whether there might be some default attributes to apply to this DECL; | |
35a3065a | 139 | if so, decl_attributes will be called recursively with those attributes |
7d3b509a | 140 | and ATTR_FLAG_BUILT_IN set. */ |
e3f6ce11 | 141 | |
142 | tree | |
143 | decl_attributes (node, attributes, flags) | |
144 | tree *node, attributes; | |
145 | int flags; | |
146 | { | |
147 | tree a; | |
148 | tree returned_attrs = NULL_TREE; | |
149 | ||
150 | if (!attributes_initialized) | |
151 | init_attributes (); | |
152 | ||
153 | (*targetm.insert_attributes) (*node, &attributes); | |
154 | ||
7d3b509a | 155 | if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL |
156 | && !(flags & (int) ATTR_FLAG_BUILT_IN)) | |
26ca6c20 | 157 | (*lang_hooks.insert_default_attributes) (*node); |
7d3b509a | 158 | |
e3f6ce11 | 159 | for (a = attributes; a; a = TREE_CHAIN (a)) |
160 | { | |
161 | tree name = TREE_PURPOSE (a); | |
162 | tree args = TREE_VALUE (a); | |
163 | tree *anode = node; | |
164 | const struct attribute_spec *spec = NULL; | |
165 | bool no_add_attrs = 0; | |
40265b77 | 166 | size_t i; |
e3f6ce11 | 167 | |
3585dac7 | 168 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) |
e3f6ce11 | 169 | { |
170 | int j; | |
171 | ||
172 | for (j = 0; attribute_tables[i][j].name != NULL; j++) | |
173 | { | |
174 | if (is_attribute_p (attribute_tables[i][j].name, name)) | |
175 | { | |
176 | spec = &attribute_tables[i][j]; | |
177 | break; | |
178 | } | |
179 | } | |
180 | if (spec != NULL) | |
181 | break; | |
182 | } | |
183 | ||
184 | if (spec == NULL) | |
185 | { | |
186 | warning ("`%s' attribute directive ignored", | |
187 | IDENTIFIER_POINTER (name)); | |
188 | continue; | |
189 | } | |
190 | else if (list_length (args) < spec->min_length | |
191 | || (spec->max_length >= 0 | |
192 | && list_length (args) > spec->max_length)) | |
193 | { | |
194 | error ("wrong number of arguments specified for `%s' attribute", | |
195 | IDENTIFIER_POINTER (name)); | |
196 | continue; | |
197 | } | |
198 | ||
199 | if (spec->decl_required && !DECL_P (*anode)) | |
200 | { | |
201 | if (flags & ((int) ATTR_FLAG_DECL_NEXT | |
202 | | (int) ATTR_FLAG_FUNCTION_NEXT | |
203 | | (int) ATTR_FLAG_ARRAY_NEXT)) | |
204 | { | |
205 | /* Pass on this attribute to be tried again. */ | |
206 | returned_attrs = tree_cons (name, args, returned_attrs); | |
207 | continue; | |
208 | } | |
209 | else | |
210 | { | |
211 | warning ("`%s' attribute does not apply to types", | |
212 | IDENTIFIER_POINTER (name)); | |
213 | continue; | |
214 | } | |
215 | } | |
216 | ||
aa9c60c1 | 217 | /* If we require a type, but were passed a decl, set up to make a |
218 | new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE | |
219 | would have applied if we'd been passed a type, but we cannot modify | |
220 | the decl's type in place here. */ | |
e3f6ce11 | 221 | if (spec->type_required && DECL_P (*anode)) |
aa9c60c1 | 222 | { |
223 | anode = &TREE_TYPE (*anode); | |
224 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; | |
225 | } | |
e3f6ce11 | 226 | |
227 | if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE | |
228 | && TREE_CODE (*anode) != METHOD_TYPE) | |
229 | { | |
230 | if (TREE_CODE (*anode) == POINTER_TYPE | |
231 | && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE | |
232 | || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE)) | |
233 | { | |
234 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
235 | *anode = build_type_copy (*anode); | |
236 | anode = &TREE_TYPE (*anode); | |
237 | } | |
238 | else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
239 | { | |
240 | /* Pass on this attribute to be tried again. */ | |
241 | returned_attrs = tree_cons (name, args, returned_attrs); | |
242 | continue; | |
243 | } | |
244 | ||
245 | if (TREE_CODE (*anode) != FUNCTION_TYPE | |
246 | && TREE_CODE (*anode) != METHOD_TYPE) | |
247 | { | |
248 | warning ("`%s' attribute only applies to function types", | |
249 | IDENTIFIER_POINTER (name)); | |
250 | continue; | |
251 | } | |
252 | } | |
253 | ||
254 | if (spec->handler != NULL) | |
255 | returned_attrs = chainon ((*spec->handler) (anode, name, args, | |
256 | flags, &no_add_attrs), | |
257 | returned_attrs); | |
ae4718db | 258 | |
259 | /* Layout the decl in case anything changed. */ | |
260 | if (spec->type_required && DECL_P (*node) | |
e56de52f | 261 | && (TREE_CODE (*node) == VAR_DECL |
262 | || TREE_CODE (*node) == PARM_DECL | |
263 | || TREE_CODE (*node) == RESULT_DECL)) | |
ae4718db | 264 | { |
265 | /* Force a recalculation of mode and size. */ | |
266 | DECL_MODE (*node) = VOIDmode; | |
267 | DECL_SIZE (*node) = 0; | |
268 | ||
269 | layout_decl (*node, 0); | |
270 | } | |
271 | ||
e3f6ce11 | 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) | |
296 | TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); | |
297 | else | |
298 | *anode = build_type_attribute_variant (*anode, | |
299 | tree_cons (name, args, | |
300 | old_attrs)); | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
305 | return returned_attrs; | |
306 | } | |
307 | ||
e3f6ce11 | 308 | /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two |
309 | lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE). | |
310 | ||
311 | The head of the declspec list is stored in DECLSPECS. | |
312 | The head of the attribute list is stored in PREFIX_ATTRIBUTES. | |
313 | ||
314 | Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of | |
315 | the list elements. We drop the containing TREE_LIST nodes and link the | |
316 | resulting attributes together the way decl_attributes expects them. */ | |
317 | ||
318 | void | |
319 | split_specs_attrs (specs_attrs, declspecs, prefix_attributes) | |
320 | tree specs_attrs; | |
321 | tree *declspecs, *prefix_attributes; | |
322 | { | |
323 | tree t, s, a, next, specs, attrs; | |
324 | ||
325 | /* This can happen after an __extension__ in pedantic mode. */ | |
326 | if (specs_attrs != NULL_TREE | |
327 | && TREE_CODE (specs_attrs) == INTEGER_CST) | |
328 | { | |
329 | *declspecs = NULL_TREE; | |
330 | *prefix_attributes = NULL_TREE; | |
331 | return; | |
332 | } | |
333 | ||
334 | /* This can happen in c++ (eg: decl: typespec initdecls ';'). */ | |
335 | if (specs_attrs != NULL_TREE | |
336 | && TREE_CODE (specs_attrs) != TREE_LIST) | |
337 | { | |
338 | *declspecs = specs_attrs; | |
339 | *prefix_attributes = NULL_TREE; | |
340 | return; | |
341 | } | |
342 | ||
343 | /* Remember to keep the lists in the same order, element-wise. */ | |
344 | ||
345 | specs = s = NULL_TREE; | |
346 | attrs = a = NULL_TREE; | |
347 | for (t = specs_attrs; t; t = next) | |
348 | { | |
349 | next = TREE_CHAIN (t); | |
350 | /* Declspecs have a non-NULL TREE_VALUE. */ | |
351 | if (TREE_VALUE (t) != NULL_TREE) | |
352 | { | |
353 | if (specs == NULL_TREE) | |
354 | specs = s = t; | |
355 | else | |
356 | { | |
357 | TREE_CHAIN (s) = t; | |
358 | s = t; | |
359 | } | |
360 | } | |
361 | /* The TREE_PURPOSE may also be empty in the case of | |
362 | __attribute__(()). */ | |
363 | else if (TREE_PURPOSE (t) != NULL_TREE) | |
364 | { | |
365 | if (attrs == NULL_TREE) | |
366 | attrs = a = TREE_PURPOSE (t); | |
367 | else | |
368 | { | |
369 | TREE_CHAIN (a) = TREE_PURPOSE (t); | |
370 | a = TREE_PURPOSE (t); | |
371 | } | |
372 | /* More attrs can be linked here, move A to the end. */ | |
373 | while (TREE_CHAIN (a) != NULL_TREE) | |
374 | a = TREE_CHAIN (a); | |
375 | } | |
376 | } | |
377 | ||
378 | /* Terminate the lists. */ | |
379 | if (s != NULL_TREE) | |
380 | TREE_CHAIN (s) = NULL_TREE; | |
381 | if (a != NULL_TREE) | |
382 | TREE_CHAIN (a) = NULL_TREE; | |
383 | ||
384 | /* All done. */ | |
385 | *declspecs = specs; | |
386 | *prefix_attributes = attrs; | |
387 | } | |
388 | ||
389 | /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes. | |
390 | This function is used by the parser when a rule will accept attributes | |
391 | in a particular position, but we don't want to support that just yet. | |
392 | ||
393 | A warning is issued for every ignored attribute. */ | |
394 | ||
395 | tree | |
396 | strip_attrs (specs_attrs) | |
397 | tree specs_attrs; | |
398 | { | |
399 | tree specs, attrs; | |
400 | ||
401 | split_specs_attrs (specs_attrs, &specs, &attrs); | |
402 | ||
403 | while (attrs) | |
404 | { | |
405 | warning ("`%s' attribute ignored", | |
406 | IDENTIFIER_POINTER (TREE_PURPOSE (attrs))); | |
407 | attrs = TREE_CHAIN (attrs); | |
408 | } | |
409 | ||
410 | return specs; | |
411 | } | |
af87ad83 | 412 |