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