Support slim switch for cfg graph dump
[official-gcc.git] / gcc / attribs.c
blob08ebfe10a8f2edaa2f0512c564ba10f842be0f45
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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 "hashtab.h"
33 #include "plugin.h"
35 /* Table of the tables of attributes (common, language, format, machine)
36 searched. */
37 static const struct attribute_spec *attribute_tables[4];
39 /* Substring representation. */
41 struct substring
43 const char *str;
44 int length;
47 /* Scoped attribute name representation. */
49 struct scoped_attributes
51 const char *ns;
52 vec<attribute_spec> attributes;
53 htab_t attribute_hash;
56 /* The table of scope attributes. */
57 static vec<scoped_attributes> attributes_table;
59 static scoped_attributes* find_attribute_namespace (const char*);
60 static void register_scoped_attribute (const struct attribute_spec *,
61 scoped_attributes *);
63 static bool attributes_initialized = false;
65 /* Default empty table of attributes. */
67 static const struct attribute_spec empty_attribute_table[] =
69 { NULL, 0, 0, false, false, false, NULL, false }
72 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
73 To avoid need for copying, we simply return length of the string. */
75 static void
76 extract_attribute_substring (struct substring *str)
78 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
79 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
81 str->length -= 4;
82 str->str += 2;
86 /* Simple hash function to avoid need to scan whole string. */
88 static inline hashval_t
89 substring_hash (const char *str, int l)
91 return str[0] + str[l - 1] * 256 + l * 65536;
94 /* Used for attribute_hash. */
96 static hashval_t
97 hash_attr (const void *p)
99 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
100 const int l = strlen (spec->name);
102 return substring_hash (spec->name, l);
105 /* Used for attribute_hash. */
107 static int
108 eq_attr (const void *p, const void *q)
110 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
111 const struct substring *const str = (const struct substring *) q;
113 return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
116 /* Insert an array of attributes ATTRIBUTES into a namespace. This
117 array must be NULL terminated. NS is the name of attribute
118 namespace. The function returns the namespace into which the
119 attributes have been registered. */
121 scoped_attributes*
122 register_scoped_attributes (const struct attribute_spec * attributes,
123 const char* ns)
125 scoped_attributes *result = NULL;
127 /* See if we already have attributes in the namespace NS. */
128 result = find_attribute_namespace (ns);
130 if (result == NULL)
132 /* We don't have any namespace NS yet. Create one. */
133 scoped_attributes sa;
135 if (!attributes_table.is_empty ())
136 attributes_table.create (64);
138 memset (&sa, 0, sizeof (sa));
139 sa.ns = ns;
140 sa.attributes.create (64);
141 result = attributes_table.safe_push (sa);
142 result->attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
145 /* Really add the attributes to their namespace now. */
146 for (unsigned i = 0; attributes[i].name != NULL; ++i)
148 result->attributes.safe_push (attributes[i]);
149 register_scoped_attribute (&attributes[i], result);
152 gcc_assert (result != NULL);
154 return result;
157 /* Return the namespace which name is NS, NULL if none exist. */
159 static scoped_attributes*
160 find_attribute_namespace (const char* ns)
162 unsigned ix;
163 scoped_attributes *iter;
165 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
166 if (ns == iter->ns
167 || (iter->ns != NULL
168 && ns != NULL
169 && !strcmp (iter->ns, ns)))
170 return iter;
171 return NULL;
174 /* Initialize attribute tables, and make some sanity checks
175 if --enable-checking. */
177 void
178 init_attributes (void)
180 size_t i;
182 if (attributes_initialized)
183 return;
185 attribute_tables[0] = lang_hooks.common_attribute_table;
186 attribute_tables[1] = lang_hooks.attribute_table;
187 attribute_tables[2] = lang_hooks.format_attribute_table;
188 attribute_tables[3] = targetm.attribute_table;
190 /* Translate NULL pointers to pointers to the empty table. */
191 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
192 if (attribute_tables[i] == NULL)
193 attribute_tables[i] = empty_attribute_table;
195 #ifdef ENABLE_CHECKING
196 /* Make some sanity checks on the attribute tables. */
197 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
199 int j;
201 for (j = 0; attribute_tables[i][j].name != NULL; j++)
203 /* The name must not begin and end with __. */
204 const char *name = attribute_tables[i][j].name;
205 int len = strlen (name);
207 gcc_assert (!(name[0] == '_' && name[1] == '_'
208 && name[len - 1] == '_' && name[len - 2] == '_'));
210 /* The minimum and maximum lengths must be consistent. */
211 gcc_assert (attribute_tables[i][j].min_length >= 0);
213 gcc_assert (attribute_tables[i][j].max_length == -1
214 || (attribute_tables[i][j].max_length
215 >= attribute_tables[i][j].min_length));
217 /* An attribute cannot require both a DECL and a TYPE. */
218 gcc_assert (!attribute_tables[i][j].decl_required
219 || !attribute_tables[i][j].type_required);
221 /* If an attribute requires a function type, in particular
222 it requires a type. */
223 gcc_assert (!attribute_tables[i][j].function_type_required
224 || attribute_tables[i][j].type_required);
228 /* Check that each name occurs just once in each table. */
229 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
231 int j, k;
232 for (j = 0; attribute_tables[i][j].name != NULL; j++)
233 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
234 gcc_assert (strcmp (attribute_tables[i][j].name,
235 attribute_tables[i][k].name));
237 /* Check that no name occurs in more than one table. Names that
238 begin with '*' are exempt, and may be overridden. */
239 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
241 size_t j, k, l;
243 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
244 for (k = 0; attribute_tables[i][k].name != NULL; k++)
245 for (l = 0; attribute_tables[j][l].name != NULL; l++)
246 gcc_assert (attribute_tables[i][k].name[0] == '*'
247 || strcmp (attribute_tables[i][k].name,
248 attribute_tables[j][l].name));
250 #endif
252 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
253 /* Put all the GNU attributes into the "gnu" namespace. */
254 register_scoped_attributes (attribute_tables[i], "gnu");
256 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
257 attributes_initialized = true;
260 /* Insert a single ATTR into the attribute table. */
262 void
263 register_attribute (const struct attribute_spec *attr)
265 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
268 /* Insert a single attribute ATTR into a namespace of attributes. */
270 static void
271 register_scoped_attribute (const struct attribute_spec *attr,
272 scoped_attributes *name_space)
274 struct substring str;
275 void **slot;
277 gcc_assert (attr != NULL && name_space != NULL);
279 gcc_assert (name_space->attribute_hash != NULL);
281 str.str = attr->name;
282 str.length = strlen (str.str);
284 /* Attribute names in the table must be in the form 'text' and not
285 in the form '__text__'. */
286 gcc_assert (str.length > 0 && str.str[0] != '_');
288 slot = htab_find_slot_with_hash (name_space->attribute_hash, &str,
289 substring_hash (str.str, str.length),
290 INSERT);
291 gcc_assert (!*slot || attr->name[0] == '*');
292 *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
295 /* Return the spec for the scoped attribute with namespace NS and
296 name NAME. */
298 const struct attribute_spec *
299 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
301 struct substring attr;
302 scoped_attributes *attrs;
304 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
306 attrs = find_attribute_namespace (ns_str);
308 if (attrs == NULL)
309 return NULL;
311 attr.str = IDENTIFIER_POINTER (name);
312 attr.length = IDENTIFIER_LENGTH (name);
313 extract_attribute_substring (&attr);
314 return (const struct attribute_spec *)
315 htab_find_with_hash (attrs->attribute_hash, &attr,
316 substring_hash (attr.str, attr.length));
319 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
320 it also specifies the attribute namespace. */
322 const struct attribute_spec *
323 lookup_attribute_spec (const_tree name)
325 tree ns;
326 if (TREE_CODE (name) == TREE_LIST)
328 ns = TREE_PURPOSE (name);
329 name = TREE_VALUE (name);
331 else
332 ns = get_identifier ("gnu");
333 return lookup_scoped_attribute_spec (ns, name);
337 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
338 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
339 it should be modified in place; if a TYPE, a copy should be created
340 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
341 information, in the form of a bitwise OR of flags in enum attribute_flags
342 from tree.h. Depending on these flags, some attributes may be
343 returned to be applied at a later stage (for example, to apply
344 a decl attribute to the declaration rather than to its type). */
346 tree
347 decl_attributes (tree *node, tree attributes, int flags)
349 tree a;
350 tree returned_attrs = NULL_TREE;
352 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
353 return NULL_TREE;
355 if (!attributes_initialized)
356 init_attributes ();
358 /* If this is a function and the user used #pragma GCC optimize, add the
359 options to the attribute((optimize(...))) list. */
360 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
362 tree cur_attr = lookup_attribute ("optimize", attributes);
363 tree opts = copy_list (current_optimize_pragma);
365 if (! cur_attr)
366 attributes
367 = tree_cons (get_identifier ("optimize"), opts, attributes);
368 else
369 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
372 if (TREE_CODE (*node) == FUNCTION_DECL
373 && optimization_current_node != optimization_default_node
374 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
375 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
377 /* If this is a function and the user used #pragma GCC target, add the
378 options to the attribute((target(...))) list. */
379 if (TREE_CODE (*node) == FUNCTION_DECL
380 && current_target_pragma
381 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
382 current_target_pragma, 0))
384 tree cur_attr = lookup_attribute ("target", attributes);
385 tree opts = copy_list (current_target_pragma);
387 if (! cur_attr)
388 attributes = tree_cons (get_identifier ("target"), opts, attributes);
389 else
390 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
393 /* A "naked" function attribute implies "noinline" and "noclone" for
394 those targets that support it. */
395 if (TREE_CODE (*node) == FUNCTION_DECL
396 && attributes
397 && lookup_attribute_spec (get_identifier ("naked"))
398 && lookup_attribute ("naked", attributes) != NULL)
400 if (lookup_attribute ("noinline", attributes) == NULL)
401 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
403 if (lookup_attribute ("noclone", attributes) == NULL)
404 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
407 targetm.insert_attributes (*node, &attributes);
409 for (a = attributes; a; a = TREE_CHAIN (a))
411 tree ns = get_attribute_namespace (a);
412 tree name = get_attribute_name (a);
413 tree args = TREE_VALUE (a);
414 tree *anode = node;
415 const struct attribute_spec *spec =
416 lookup_scoped_attribute_spec (ns, name);
417 bool no_add_attrs = 0;
418 int fn_ptr_quals = 0;
419 tree fn_ptr_tmp = NULL_TREE;
421 if (spec == NULL)
423 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
425 if (ns == NULL_TREE || !cxx11_attribute_p (a))
426 warning (OPT_Wattributes, "%qE attribute directive ignored",
427 name);
428 else
429 warning (OPT_Wattributes,
430 "%<%E::%E%> scoped attribute directive ignored",
431 ns, name);
433 continue;
435 else if (list_length (args) < spec->min_length
436 || (spec->max_length >= 0
437 && list_length (args) > spec->max_length))
439 error ("wrong number of arguments specified for %qE attribute",
440 name);
441 continue;
443 gcc_assert (is_attribute_p (spec->name, name));
445 if (TYPE_P (*node)
446 && cxx11_attribute_p (a)
447 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
449 /* This is a c++11 attribute that appertains to a
450 type-specifier, outside of the definition of, a class
451 type. Ignore it. */
452 warning (OPT_Wattributes, "attribute ignored");
453 inform (input_location,
454 "an attribute that appertains to a type-specifier "
455 "is ignored");
456 continue;
459 if (spec->decl_required && !DECL_P (*anode))
461 if (flags & ((int) ATTR_FLAG_DECL_NEXT
462 | (int) ATTR_FLAG_FUNCTION_NEXT
463 | (int) ATTR_FLAG_ARRAY_NEXT))
465 /* Pass on this attribute to be tried again. */
466 returned_attrs = tree_cons (name, args, returned_attrs);
467 continue;
469 else
471 warning (OPT_Wattributes, "%qE attribute does not apply to types",
472 name);
473 continue;
477 /* If we require a type, but were passed a decl, set up to make a
478 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
479 would have applied if we'd been passed a type, but we cannot modify
480 the decl's type in place here. */
481 if (spec->type_required && DECL_P (*anode))
483 anode = &TREE_TYPE (*anode);
484 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
485 if (!(TREE_CODE (*anode) == TYPE_DECL
486 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
487 (TREE_TYPE (*anode)))))
488 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
491 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
492 && TREE_CODE (*anode) != METHOD_TYPE)
494 if (TREE_CODE (*anode) == POINTER_TYPE
495 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
496 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
498 /* OK, this is a bit convoluted. We can't just make a copy
499 of the pointer type and modify its TREE_TYPE, because if
500 we change the attributes of the target type the pointer
501 type needs to have a different TYPE_MAIN_VARIANT. So we
502 pull out the target type now, frob it as appropriate, and
503 rebuild the pointer type later.
505 This would all be simpler if attributes were part of the
506 declarator, grumble grumble. */
507 fn_ptr_tmp = TREE_TYPE (*anode);
508 fn_ptr_quals = TYPE_QUALS (*anode);
509 anode = &fn_ptr_tmp;
510 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
512 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
514 /* Pass on this attribute to be tried again. */
515 returned_attrs = tree_cons (name, args, returned_attrs);
516 continue;
519 if (TREE_CODE (*anode) != FUNCTION_TYPE
520 && TREE_CODE (*anode) != METHOD_TYPE)
522 warning (OPT_Wattributes,
523 "%qE attribute only applies to function types",
524 name);
525 continue;
529 if (TYPE_P (*anode)
530 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
531 && TYPE_SIZE (*anode) != NULL_TREE)
533 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
534 continue;
537 if (spec->handler != NULL)
539 int cxx11_flag =
540 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
542 returned_attrs = chainon ((*spec->handler) (anode, name, args,
543 flags|cxx11_flag,
544 &no_add_attrs),
545 returned_attrs);
548 /* Layout the decl in case anything changed. */
549 if (spec->type_required && DECL_P (*node)
550 && (TREE_CODE (*node) == VAR_DECL
551 || TREE_CODE (*node) == PARM_DECL
552 || TREE_CODE (*node) == RESULT_DECL))
553 relayout_decl (*node);
555 if (!no_add_attrs)
557 tree old_attrs;
558 tree a;
560 if (DECL_P (*anode))
561 old_attrs = DECL_ATTRIBUTES (*anode);
562 else
563 old_attrs = TYPE_ATTRIBUTES (*anode);
565 for (a = lookup_attribute (spec->name, old_attrs);
566 a != NULL_TREE;
567 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
569 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
570 break;
573 if (a == NULL_TREE)
575 /* This attribute isn't already in the list. */
576 if (DECL_P (*anode))
577 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
578 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
580 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
581 /* If this is the main variant, also push the attributes
582 out to the other variants. */
583 if (*anode == TYPE_MAIN_VARIANT (*anode))
585 tree variant;
586 for (variant = *anode; variant;
587 variant = TYPE_NEXT_VARIANT (variant))
589 if (TYPE_ATTRIBUTES (variant) == old_attrs)
590 TYPE_ATTRIBUTES (variant)
591 = TYPE_ATTRIBUTES (*anode);
592 else if (!lookup_attribute
593 (spec->name, TYPE_ATTRIBUTES (variant)))
594 TYPE_ATTRIBUTES (variant) = tree_cons
595 (name, args, TYPE_ATTRIBUTES (variant));
599 else
600 *anode = build_type_attribute_variant (*anode,
601 tree_cons (name, args,
602 old_attrs));
606 if (fn_ptr_tmp)
608 /* Rebuild the function pointer type and put it in the
609 appropriate place. */
610 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
611 if (fn_ptr_quals)
612 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
613 if (DECL_P (*node))
614 TREE_TYPE (*node) = fn_ptr_tmp;
615 else
617 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
618 *node = fn_ptr_tmp;
623 return returned_attrs;
626 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
627 attribute.
629 When G++ parses a C++11 attribute, it is represented as
630 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
631 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
632 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
633 use get_attribute_namespace and get_attribute_name to retrieve the
634 namespace and name of the attribute, as these accessors work with
635 GNU attributes as well. */
637 bool
638 cxx11_attribute_p (const_tree attr)
640 if (attr == NULL_TREE
641 || TREE_CODE (attr) != TREE_LIST)
642 return false;
644 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
647 /* Return the name of the attribute ATTR. This accessor works on GNU
648 and C++11 (scoped) attributes.
650 Please read the comments of cxx11_attribute_p to understand the
651 format of attributes. */
653 tree
654 get_attribute_name (const_tree attr)
656 if (cxx11_attribute_p (attr))
657 return TREE_VALUE (TREE_PURPOSE (attr));
658 return TREE_PURPOSE (attr);
661 /* Return the namespace of the attribute ATTR. This accessor works on
662 GNU and C++11 (scoped) attributes. On GNU attributes,
663 it returns an identifier tree for the string "gnu".
665 Please read the comments of cxx11_attribute_p to understand the
666 format of attributes. */
668 tree
669 get_attribute_namespace (const_tree attr)
671 if (cxx11_attribute_p (attr))
672 return TREE_PURPOSE (TREE_PURPOSE (attr));
673 return get_identifier ("gnu");
676 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
677 to the method FNDECL. */
679 void
680 apply_tm_attr (tree fndecl, tree attr)
682 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);