]>
Commit | Line | Data |
---|---|---|
bb9f8221 | 1 | /* Functions dealing with attribute handling, used by most front ends. |
5624e564 | 2 | Copyright (C) 1992-2015 Free Software Foundation, Inc. |
bb9f8221 RK |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
bb9f8221 RK |
9 | version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
bb9f8221 RK |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
4977bab6 ZW |
22 | #include "coretypes.h" |
23 | #include "tm.h" | |
bb9f8221 | 24 | #include "tree.h" |
c7131fb2 | 25 | #include "alias.h" |
d8a2d370 DN |
26 | #include "stringpool.h" |
27 | #include "attribs.h" | |
28 | #include "stor-layout.h" | |
bb9f8221 | 29 | #include "flags.h" |
718f9c0f | 30 | #include "diagnostic-core.h" |
bb9f8221 | 31 | #include "tm_p.h" |
bb9f8221 RK |
32 | #include "cpplib.h" |
33 | #include "target.h" | |
7ffb4fd2 | 34 | #include "langhooks.h" |
d1c8e08a | 35 | #include "plugin.h" |
bb9f8221 | 36 | |
349ae713 | 37 | /* Table of the tables of attributes (common, language, format, machine) |
bb9f8221 RK |
38 | searched. */ |
39 | static const struct attribute_spec *attribute_tables[4]; | |
40 | ||
23b43207 JH |
41 | /* Substring representation. */ |
42 | ||
43 | struct substring | |
44 | { | |
45 | const char *str; | |
46 | int length; | |
47 | }; | |
48 | ||
4a8fb1a1 LC |
49 | /* Simple hash function to avoid need to scan whole string. */ |
50 | ||
51 | static inline hashval_t | |
52 | substring_hash (const char *str, int l) | |
53 | { | |
54 | return str[0] + str[l - 1] * 256 + l * 65536; | |
55 | } | |
56 | ||
57 | /* Used for attribute_hash. */ | |
58 | ||
8d67ee55 | 59 | struct attribute_hasher : nofree_ptr_hash <attribute_spec> |
4a8fb1a1 | 60 | { |
67f58944 TS |
61 | typedef substring *compare_type; |
62 | static inline hashval_t hash (const attribute_spec *); | |
63 | static inline bool equal (const attribute_spec *, const substring *); | |
4a8fb1a1 LC |
64 | }; |
65 | ||
66 | inline hashval_t | |
67f58944 | 67 | attribute_hasher::hash (const attribute_spec *spec) |
4a8fb1a1 LC |
68 | { |
69 | const int l = strlen (spec->name); | |
70 | return substring_hash (spec->name, l); | |
71 | } | |
72 | ||
73 | inline bool | |
67f58944 | 74 | attribute_hasher::equal (const attribute_spec *spec, const substring *str) |
4a8fb1a1 LC |
75 | { |
76 | return (strncmp (spec->name, str->str, str->length) == 0 | |
77 | && !spec->name[str->length]); | |
78 | } | |
79 | ||
e28d52cf DS |
80 | /* Scoped attribute name representation. */ |
81 | ||
82 | struct scoped_attributes | |
83 | { | |
84 | const char *ns; | |
9771b263 | 85 | vec<attribute_spec> attributes; |
c203e8a7 | 86 | hash_table<attribute_hasher> *attribute_hash; |
e28d52cf DS |
87 | }; |
88 | ||
e28d52cf | 89 | /* The table of scope attributes. */ |
9771b263 | 90 | static vec<scoped_attributes> attributes_table; |
e28d52cf DS |
91 | |
92 | static scoped_attributes* find_attribute_namespace (const char*); | |
93 | static void register_scoped_attribute (const struct attribute_spec *, | |
94 | scoped_attributes *); | |
95 | ||
bb9f8221 RK |
96 | static bool attributes_initialized = false; |
97 | ||
bb9f8221 | 98 | /* Default empty table of attributes. */ |
23b43207 | 99 | |
bb9f8221 RK |
100 | static const struct attribute_spec empty_attribute_table[] = |
101 | { | |
62d784f7 | 102 | { NULL, 0, 0, false, false, false, NULL, false } |
bb9f8221 RK |
103 | }; |
104 | ||
23b43207 JH |
105 | /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'. |
106 | To avoid need for copying, we simply return length of the string. */ | |
107 | ||
108 | static void | |
109 | extract_attribute_substring (struct substring *str) | |
110 | { | |
111 | if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_' | |
112 | && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_') | |
113 | { | |
114 | str->length -= 4; | |
115 | str->str += 2; | |
116 | } | |
117 | } | |
118 | ||
e28d52cf DS |
119 | /* Insert an array of attributes ATTRIBUTES into a namespace. This |
120 | array must be NULL terminated. NS is the name of attribute | |
121 | namespace. The function returns the namespace into which the | |
122 | attributes have been registered. */ | |
123 | ||
124 | scoped_attributes* | |
125 | register_scoped_attributes (const struct attribute_spec * attributes, | |
126 | const char* ns) | |
127 | { | |
128 | scoped_attributes *result = NULL; | |
129 | ||
130 | /* See if we already have attributes in the namespace NS. */ | |
131 | result = find_attribute_namespace (ns); | |
132 | ||
133 | if (result == NULL) | |
134 | { | |
135 | /* We don't have any namespace NS yet. Create one. */ | |
136 | scoped_attributes sa; | |
137 | ||
9771b263 DN |
138 | if (!attributes_table.is_empty ()) |
139 | attributes_table.create (64); | |
e28d52cf DS |
140 | |
141 | memset (&sa, 0, sizeof (sa)); | |
142 | sa.ns = ns; | |
9771b263 DN |
143 | sa.attributes.create (64); |
144 | result = attributes_table.safe_push (sa); | |
c203e8a7 | 145 | result->attribute_hash = new hash_table<attribute_hasher> (200); |
e28d52cf DS |
146 | } |
147 | ||
148 | /* Really add the attributes to their namespace now. */ | |
149 | for (unsigned i = 0; attributes[i].name != NULL; ++i) | |
150 | { | |
9771b263 | 151 | result->attributes.safe_push (attributes[i]); |
e28d52cf DS |
152 | register_scoped_attribute (&attributes[i], result); |
153 | } | |
154 | ||
155 | gcc_assert (result != NULL); | |
156 | ||
157 | return result; | |
158 | } | |
159 | ||
160 | /* Return the namespace which name is NS, NULL if none exist. */ | |
161 | ||
162 | static scoped_attributes* | |
163 | find_attribute_namespace (const char* ns) | |
164 | { | |
165 | unsigned ix; | |
166 | scoped_attributes *iter; | |
167 | ||
9771b263 | 168 | FOR_EACH_VEC_ELT (attributes_table, ix, iter) |
e28d52cf DS |
169 | if (ns == iter->ns |
170 | || (iter->ns != NULL | |
171 | && ns != NULL | |
172 | && !strcmp (iter->ns, ns))) | |
173 | return iter; | |
174 | return NULL; | |
175 | } | |
176 | ||
b2b29377 MM |
177 | /* Make some sanity checks on the attribute tables. */ |
178 | ||
179 | static void | |
180 | check_attribute_tables (void) | |
181 | { | |
182 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
183 | for (size_t j = 0; attribute_tables[i][j].name != NULL; j++) | |
184 | { | |
185 | /* The name must not begin and end with __. */ | |
186 | const char *name = attribute_tables[i][j].name; | |
187 | int len = strlen (name); | |
188 | ||
189 | gcc_assert (!(name[0] == '_' && name[1] == '_' | |
190 | && name[len - 1] == '_' && name[len - 2] == '_')); | |
191 | ||
192 | /* The minimum and maximum lengths must be consistent. */ | |
193 | gcc_assert (attribute_tables[i][j].min_length >= 0); | |
194 | ||
195 | gcc_assert (attribute_tables[i][j].max_length == -1 | |
196 | || (attribute_tables[i][j].max_length | |
197 | >= attribute_tables[i][j].min_length)); | |
198 | ||
199 | /* An attribute cannot require both a DECL and a TYPE. */ | |
200 | gcc_assert (!attribute_tables[i][j].decl_required | |
201 | || !attribute_tables[i][j].type_required); | |
202 | ||
203 | /* If an attribute requires a function type, in particular | |
204 | it requires a type. */ | |
205 | gcc_assert (!attribute_tables[i][j].function_type_required | |
206 | || attribute_tables[i][j].type_required); | |
207 | } | |
208 | ||
209 | /* Check that each name occurs just once in each table. */ | |
210 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
211 | for (size_t j = 0; attribute_tables[i][j].name != NULL; j++) | |
212 | for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++) | |
213 | gcc_assert (strcmp (attribute_tables[i][j].name, | |
214 | attribute_tables[i][k].name)); | |
215 | ||
216 | /* Check that no name occurs in more than one table. Names that | |
217 | begin with '*' are exempt, and may be overridden. */ | |
218 | for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
219 | for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++) | |
220 | for (size_t k = 0; attribute_tables[i][k].name != NULL; k++) | |
221 | for (size_t l = 0; attribute_tables[j][l].name != NULL; l++) | |
222 | gcc_assert (attribute_tables[i][k].name[0] == '*' | |
223 | || strcmp (attribute_tables[i][k].name, | |
224 | attribute_tables[j][l].name)); | |
225 | } | |
226 | ||
227 | /* Initialize attribute tables, and make some sanity checks if checking is | |
228 | enabled. */ | |
bb9f8221 | 229 | |
8dd00781 | 230 | void |
4682ae04 | 231 | init_attributes (void) |
bb9f8221 | 232 | { |
ca7558fc | 233 | size_t i; |
bb9f8221 | 234 | |
8dd00781 JJ |
235 | if (attributes_initialized) |
236 | return; | |
237 | ||
349ae713 NB |
238 | attribute_tables[0] = lang_hooks.common_attribute_table; |
239 | attribute_tables[1] = lang_hooks.attribute_table; | |
240 | attribute_tables[2] = lang_hooks.format_attribute_table; | |
bb9f8221 RK |
241 | attribute_tables[3] = targetm.attribute_table; |
242 | ||
349ae713 NB |
243 | /* Translate NULL pointers to pointers to the empty table. */ |
244 | for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) | |
245 | if (attribute_tables[i] == NULL) | |
246 | attribute_tables[i] = empty_attribute_table; | |
247 | ||
b2b29377 MM |
248 | if (flag_checking) |
249 | check_attribute_tables (); | |
bb9f8221 | 250 | |
e28d52cf DS |
251 | for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i) |
252 | /* Put all the GNU attributes into the "gnu" namespace. */ | |
253 | register_scoped_attributes (attribute_tables[i], "gnu"); | |
254 | ||
d1c8e08a TG |
255 | invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL); |
256 | attributes_initialized = true; | |
257 | } | |
258 | ||
259 | /* Insert a single ATTR into the attribute table. */ | |
260 | ||
261 | void | |
b8698a0f | 262 | register_attribute (const struct attribute_spec *attr) |
e28d52cf DS |
263 | { |
264 | register_scoped_attribute (attr, find_attribute_namespace ("gnu")); | |
265 | } | |
266 | ||
267 | /* Insert a single attribute ATTR into a namespace of attributes. */ | |
268 | ||
269 | static void | |
270 | register_scoped_attribute (const struct attribute_spec *attr, | |
271 | scoped_attributes *name_space) | |
d1c8e08a | 272 | { |
16f7ad42 | 273 | struct substring str; |
4a8fb1a1 | 274 | attribute_spec **slot; |
16f7ad42 | 275 | |
e28d52cf DS |
276 | gcc_assert (attr != NULL && name_space != NULL); |
277 | ||
c203e8a7 | 278 | gcc_assert (name_space->attribute_hash); |
e28d52cf | 279 | |
16f7ad42 TG |
280 | str.str = attr->name; |
281 | str.length = strlen (str.str); | |
70e41a6a NP |
282 | |
283 | /* Attribute names in the table must be in the form 'text' and not | |
284 | in the form '__text__'. */ | |
285 | gcc_assert (str.length > 0 && str.str[0] != '_'); | |
286 | ||
4a8fb1a1 | 287 | slot = name_space->attribute_hash |
c203e8a7 TS |
288 | ->find_slot_with_hash (&str, substring_hash (str.str, str.length), |
289 | INSERT); | |
0a35513e | 290 | gcc_assert (!*slot || attr->name[0] == '*'); |
4a8fb1a1 | 291 | *slot = CONST_CAST (struct attribute_spec *, attr); |
bb9f8221 | 292 | } |
a7f6bc8c | 293 | |
e28d52cf DS |
294 | /* Return the spec for the scoped attribute with namespace NS and |
295 | name NAME. */ | |
a7f6bc8c | 296 | |
862d0b35 | 297 | static const struct attribute_spec * |
e28d52cf | 298 | lookup_scoped_attribute_spec (const_tree ns, const_tree name) |
a7f6bc8c JM |
299 | { |
300 | struct substring attr; | |
e28d52cf DS |
301 | scoped_attributes *attrs; |
302 | ||
303 | const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL; | |
304 | ||
305 | attrs = find_attribute_namespace (ns_str); | |
306 | ||
307 | if (attrs == NULL) | |
308 | return NULL; | |
a7f6bc8c JM |
309 | |
310 | attr.str = IDENTIFIER_POINTER (name); | |
311 | attr.length = IDENTIFIER_LENGTH (name); | |
312 | extract_attribute_substring (&attr); | |
c203e8a7 TS |
313 | return attrs->attribute_hash->find_with_hash (&attr, |
314 | substring_hash (attr.str, | |
315 | attr.length)); | |
a7f6bc8c | 316 | } |
e28d52cf | 317 | |
7dbb85a7 JM |
318 | /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST, |
319 | it also specifies the attribute namespace. */ | |
e28d52cf DS |
320 | |
321 | const struct attribute_spec * | |
322 | lookup_attribute_spec (const_tree name) | |
323 | { | |
7dbb85a7 JM |
324 | tree ns; |
325 | if (TREE_CODE (name) == TREE_LIST) | |
326 | { | |
327 | ns = TREE_PURPOSE (name); | |
328 | name = TREE_VALUE (name); | |
329 | } | |
330 | else | |
331 | ns = get_identifier ("gnu"); | |
332 | return lookup_scoped_attribute_spec (ns, name); | |
e28d52cf DS |
333 | } |
334 | ||
862d0b35 DN |
335 | |
336 | /* Return the namespace of the attribute ATTR. This accessor works on | |
337 | GNU and C++11 (scoped) attributes. On GNU attributes, | |
338 | it returns an identifier tree for the string "gnu". | |
339 | ||
340 | Please read the comments of cxx11_attribute_p to understand the | |
341 | format of attributes. */ | |
342 | ||
343 | static tree | |
344 | get_attribute_namespace (const_tree attr) | |
345 | { | |
346 | if (cxx11_attribute_p (attr)) | |
347 | return TREE_PURPOSE (TREE_PURPOSE (attr)); | |
348 | return get_identifier ("gnu"); | |
349 | } | |
350 | ||
351 | ||
bb9f8221 RK |
352 | /* Process the attributes listed in ATTRIBUTES and install them in *NODE, |
353 | which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, | |
354 | it should be modified in place; if a TYPE, a copy should be created | |
355 | unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further | |
356 | information, in the form of a bitwise OR of flags in enum attribute_flags | |
357 | from tree.h. Depending on these flags, some attributes may be | |
358 | returned to be applied at a later stage (for example, to apply | |
03aa99d4 | 359 | a decl attribute to the declaration rather than to its type). */ |
bb9f8221 RK |
360 | |
361 | tree | |
4682ae04 | 362 | decl_attributes (tree *node, tree attributes, int flags) |
bb9f8221 RK |
363 | { |
364 | tree a; | |
365 | tree returned_attrs = NULL_TREE; | |
366 | ||
e28d52cf | 367 | if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node) |
21516d64 VR |
368 | return NULL_TREE; |
369 | ||
bb9f8221 RK |
370 | if (!attributes_initialized) |
371 | init_attributes (); | |
372 | ||
ab442df7 MM |
373 | /* If this is a function and the user used #pragma GCC optimize, add the |
374 | options to the attribute((optimize(...))) list. */ | |
375 | if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma) | |
376 | { | |
377 | tree cur_attr = lookup_attribute ("optimize", attributes); | |
378 | tree opts = copy_list (current_optimize_pragma); | |
379 | ||
380 | if (! cur_attr) | |
381 | attributes | |
382 | = tree_cons (get_identifier ("optimize"), opts, attributes); | |
383 | else | |
384 | TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr)); | |
385 | } | |
386 | ||
387 | if (TREE_CODE (*node) == FUNCTION_DECL | |
388 | && optimization_current_node != optimization_default_node | |
389 | && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)) | |
390 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node; | |
391 | ||
5779e713 MM |
392 | /* If this is a function and the user used #pragma GCC target, add the |
393 | options to the attribute((target(...))) list. */ | |
ab442df7 | 394 | if (TREE_CODE (*node) == FUNCTION_DECL |
5779e713 | 395 | && current_target_pragma |
ab442df7 | 396 | && targetm.target_option.valid_attribute_p (*node, NULL_TREE, |
5779e713 | 397 | current_target_pragma, 0)) |
ab442df7 | 398 | { |
5779e713 MM |
399 | tree cur_attr = lookup_attribute ("target", attributes); |
400 | tree opts = copy_list (current_target_pragma); | |
ab442df7 MM |
401 | |
402 | if (! cur_attr) | |
5779e713 | 403 | attributes = tree_cons (get_identifier ("target"), opts, attributes); |
ab442df7 MM |
404 | else |
405 | TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr)); | |
406 | } | |
407 | ||
61044492 JZ |
408 | /* A "naked" function attribute implies "noinline" and "noclone" for |
409 | those targets that support it. */ | |
410 | if (TREE_CODE (*node) == FUNCTION_DECL | |
70e41a6a | 411 | && attributes |
61044492 JZ |
412 | && lookup_attribute_spec (get_identifier ("naked")) |
413 | && lookup_attribute ("naked", attributes) != NULL) | |
414 | { | |
415 | if (lookup_attribute ("noinline", attributes) == NULL) | |
416 | attributes = tree_cons (get_identifier ("noinline"), NULL, attributes); | |
417 | ||
418 | if (lookup_attribute ("noclone", attributes) == NULL) | |
419 | attributes = tree_cons (get_identifier ("noclone"), NULL, attributes); | |
420 | } | |
421 | ||
5fd9b178 | 422 | targetm.insert_attributes (*node, &attributes); |
bb9f8221 RK |
423 | |
424 | for (a = attributes; a; a = TREE_CHAIN (a)) | |
425 | { | |
e28d52cf DS |
426 | tree ns = get_attribute_namespace (a); |
427 | tree name = get_attribute_name (a); | |
bb9f8221 RK |
428 | tree args = TREE_VALUE (a); |
429 | tree *anode = node; | |
e28d52cf DS |
430 | const struct attribute_spec *spec = |
431 | lookup_scoped_attribute_spec (ns, name); | |
bb9f8221 | 432 | bool no_add_attrs = 0; |
f9ceed32 | 433 | int fn_ptr_quals = 0; |
3acef2ae | 434 | tree fn_ptr_tmp = NULL_TREE; |
bb9f8221 RK |
435 | |
436 | if (spec == NULL) | |
437 | { | |
e384e6b5 | 438 | if (!(flags & (int) ATTR_FLAG_BUILT_IN)) |
e28d52cf DS |
439 | { |
440 | if (ns == NULL_TREE || !cxx11_attribute_p (a)) | |
441 | warning (OPT_Wattributes, "%qE attribute directive ignored", | |
442 | name); | |
443 | else | |
444 | warning (OPT_Wattributes, | |
445 | "%<%E::%E%> scoped attribute directive ignored", | |
446 | ns, name); | |
447 | } | |
bb9f8221 RK |
448 | continue; |
449 | } | |
450 | else if (list_length (args) < spec->min_length | |
451 | || (spec->max_length >= 0 | |
452 | && list_length (args) > spec->max_length)) | |
453 | { | |
4f1e4960 JM |
454 | error ("wrong number of arguments specified for %qE attribute", |
455 | name); | |
bb9f8221 RK |
456 | continue; |
457 | } | |
23b43207 | 458 | gcc_assert (is_attribute_p (spec->name, name)); |
bb9f8221 | 459 | |
e28d52cf DS |
460 | if (TYPE_P (*node) |
461 | && cxx11_attribute_p (a) | |
462 | && !(flags & ATTR_FLAG_TYPE_IN_PLACE)) | |
463 | { | |
464 | /* This is a c++11 attribute that appertains to a | |
465 | type-specifier, outside of the definition of, a class | |
466 | type. Ignore it. */ | |
fe6f27c7 PC |
467 | if (warning (OPT_Wattributes, "attribute ignored")) |
468 | inform (input_location, | |
469 | "an attribute that appertains to a type-specifier " | |
470 | "is ignored"); | |
e28d52cf DS |
471 | continue; |
472 | } | |
473 | ||
bb9f8221 RK |
474 | if (spec->decl_required && !DECL_P (*anode)) |
475 | { | |
476 | if (flags & ((int) ATTR_FLAG_DECL_NEXT | |
477 | | (int) ATTR_FLAG_FUNCTION_NEXT | |
478 | | (int) ATTR_FLAG_ARRAY_NEXT)) | |
479 | { | |
480 | /* Pass on this attribute to be tried again. */ | |
481 | returned_attrs = tree_cons (name, args, returned_attrs); | |
482 | continue; | |
483 | } | |
484 | else | |
485 | { | |
4f1e4960 JM |
486 | warning (OPT_Wattributes, "%qE attribute does not apply to types", |
487 | name); | |
bb9f8221 RK |
488 | continue; |
489 | } | |
490 | } | |
491 | ||
dd4dc3cd RK |
492 | /* If we require a type, but were passed a decl, set up to make a |
493 | new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE | |
494 | would have applied if we'd been passed a type, but we cannot modify | |
495 | the decl's type in place here. */ | |
bb9f8221 | 496 | if (spec->type_required && DECL_P (*anode)) |
dd4dc3cd RK |
497 | { |
498 | anode = &TREE_TYPE (*anode); | |
26c87b1a | 499 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; |
dd4dc3cd | 500 | } |
bb9f8221 RK |
501 | |
502 | if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE | |
503 | && TREE_CODE (*anode) != METHOD_TYPE) | |
504 | { | |
505 | if (TREE_CODE (*anode) == POINTER_TYPE | |
506 | && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE | |
507 | || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE)) | |
508 | { | |
3acef2ae JM |
509 | /* OK, this is a bit convoluted. We can't just make a copy |
510 | of the pointer type and modify its TREE_TYPE, because if | |
511 | we change the attributes of the target type the pointer | |
512 | type needs to have a different TYPE_MAIN_VARIANT. So we | |
513 | pull out the target type now, frob it as appropriate, and | |
514 | rebuild the pointer type later. | |
515 | ||
c22cacf3 MS |
516 | This would all be simpler if attributes were part of the |
517 | declarator, grumble grumble. */ | |
3acef2ae | 518 | fn_ptr_tmp = TREE_TYPE (*anode); |
f9ceed32 | 519 | fn_ptr_quals = TYPE_QUALS (*anode); |
3acef2ae JM |
520 | anode = &fn_ptr_tmp; |
521 | flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; | |
bb9f8221 RK |
522 | } |
523 | else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
524 | { | |
525 | /* Pass on this attribute to be tried again. */ | |
526 | returned_attrs = tree_cons (name, args, returned_attrs); | |
527 | continue; | |
528 | } | |
529 | ||
530 | if (TREE_CODE (*anode) != FUNCTION_TYPE | |
531 | && TREE_CODE (*anode) != METHOD_TYPE) | |
532 | { | |
5c498b10 | 533 | warning (OPT_Wattributes, |
4f1e4960 JM |
534 | "%qE attribute only applies to function types", |
535 | name); | |
bb9f8221 RK |
536 | continue; |
537 | } | |
538 | } | |
539 | ||
b9e75696 JM |
540 | if (TYPE_P (*anode) |
541 | && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) | |
542 | && TYPE_SIZE (*anode) != NULL_TREE) | |
543 | { | |
544 | warning (OPT_Wattributes, "type attributes ignored after type is already defined"); | |
545 | continue; | |
546 | } | |
547 | ||
bb9f8221 | 548 | if (spec->handler != NULL) |
e28d52cf DS |
549 | { |
550 | int cxx11_flag = | |
551 | cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0; | |
552 | ||
553 | returned_attrs = chainon ((*spec->handler) (anode, name, args, | |
554 | flags|cxx11_flag, | |
555 | &no_add_attrs), | |
556 | returned_attrs); | |
557 | } | |
1b9191d2 AH |
558 | |
559 | /* Layout the decl in case anything changed. */ | |
560 | if (spec->type_required && DECL_P (*node) | |
67282790 JM |
561 | && (TREE_CODE (*node) == VAR_DECL |
562 | || TREE_CODE (*node) == PARM_DECL | |
563 | || TREE_CODE (*node) == RESULT_DECL)) | |
d1838621 | 564 | relayout_decl (*node); |
1b9191d2 | 565 | |
bb9f8221 RK |
566 | if (!no_add_attrs) |
567 | { | |
568 | tree old_attrs; | |
569 | tree a; | |
570 | ||
571 | if (DECL_P (*anode)) | |
572 | old_attrs = DECL_ATTRIBUTES (*anode); | |
573 | else | |
574 | old_attrs = TYPE_ATTRIBUTES (*anode); | |
575 | ||
576 | for (a = lookup_attribute (spec->name, old_attrs); | |
577 | a != NULL_TREE; | |
578 | a = lookup_attribute (spec->name, TREE_CHAIN (a))) | |
579 | { | |
580 | if (simple_cst_equal (TREE_VALUE (a), args) == 1) | |
581 | break; | |
582 | } | |
583 | ||
584 | if (a == NULL_TREE) | |
585 | { | |
586 | /* This attribute isn't already in the list. */ | |
587 | if (DECL_P (*anode)) | |
588 | DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); | |
589 | else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) | |
67214984 JM |
590 | { |
591 | TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); | |
592 | /* If this is the main variant, also push the attributes | |
593 | out to the other variants. */ | |
594 | if (*anode == TYPE_MAIN_VARIANT (*anode)) | |
595 | { | |
596 | tree variant; | |
597 | for (variant = *anode; variant; | |
598 | variant = TYPE_NEXT_VARIANT (variant)) | |
599 | { | |
600 | if (TYPE_ATTRIBUTES (variant) == old_attrs) | |
601 | TYPE_ATTRIBUTES (variant) | |
602 | = TYPE_ATTRIBUTES (*anode); | |
603 | else if (!lookup_attribute | |
604 | (spec->name, TYPE_ATTRIBUTES (variant))) | |
605 | TYPE_ATTRIBUTES (variant) = tree_cons | |
606 | (name, args, TYPE_ATTRIBUTES (variant)); | |
607 | } | |
608 | } | |
609 | } | |
bb9f8221 RK |
610 | else |
611 | *anode = build_type_attribute_variant (*anode, | |
612 | tree_cons (name, args, | |
613 | old_attrs)); | |
614 | } | |
615 | } | |
3acef2ae JM |
616 | |
617 | if (fn_ptr_tmp) | |
618 | { | |
619 | /* Rebuild the function pointer type and put it in the | |
620 | appropriate place. */ | |
621 | fn_ptr_tmp = build_pointer_type (fn_ptr_tmp); | |
f9ceed32 MM |
622 | if (fn_ptr_quals) |
623 | fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals); | |
3acef2ae JM |
624 | if (DECL_P (*node)) |
625 | TREE_TYPE (*node) = fn_ptr_tmp; | |
3acef2ae | 626 | else |
298e6adc NS |
627 | { |
628 | gcc_assert (TREE_CODE (*node) == POINTER_TYPE); | |
629 | *node = fn_ptr_tmp; | |
630 | } | |
3acef2ae | 631 | } |
bb9f8221 RK |
632 | } |
633 | ||
634 | return returned_attrs; | |
635 | } | |
0a35513e | 636 | |
e28d52cf DS |
637 | /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11 |
638 | attribute. | |
639 | ||
640 | When G++ parses a C++11 attribute, it is represented as | |
641 | a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE | |
642 | (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the | |
643 | TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please | |
644 | use get_attribute_namespace and get_attribute_name to retrieve the | |
645 | namespace and name of the attribute, as these accessors work with | |
646 | GNU attributes as well. */ | |
647 | ||
648 | bool | |
649 | cxx11_attribute_p (const_tree attr) | |
650 | { | |
651 | if (attr == NULL_TREE | |
652 | || TREE_CODE (attr) != TREE_LIST) | |
653 | return false; | |
654 | ||
655 | return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST); | |
656 | } | |
657 | ||
658 | /* Return the name of the attribute ATTR. This accessor works on GNU | |
659 | and C++11 (scoped) attributes. | |
660 | ||
661 | Please read the comments of cxx11_attribute_p to understand the | |
662 | format of attributes. */ | |
663 | ||
664 | tree | |
665 | get_attribute_name (const_tree attr) | |
666 | { | |
667 | if (cxx11_attribute_p (attr)) | |
668 | return TREE_VALUE (TREE_PURPOSE (attr)); | |
669 | return TREE_PURPOSE (attr); | |
670 | } | |
671 | ||
0a35513e AH |
672 | /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR |
673 | to the method FNDECL. */ | |
674 | ||
675 | void | |
676 | apply_tm_attr (tree fndecl, tree attr) | |
677 | { | |
678 | decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0); | |
679 | } |