[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / attribs.c
blob6cbe011883dd61e1646ce67c15f049d55c866234
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2015 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 "alias.h"
26 #include "stringpool.h"
27 #include "attribs.h"
28 #include "stor-layout.h"
29 #include "flags.h"
30 #include "diagnostic-core.h"
31 #include "tm_p.h"
32 #include "cpplib.h"
33 #include "target.h"
34 #include "langhooks.h"
35 #include "plugin.h"
37 /* Table of the tables of attributes (common, language, format, machine)
38 searched. */
39 static const struct attribute_spec *attribute_tables[4];
41 /* Substring representation. */
43 struct substring
45 const char *str;
46 int length;
49 /* Simple hash function to avoid need to scan whole string. */
51 static inline hashval_t
52 substring_hash (const char *str, int l)
54 return str[0] + str[l - 1] * 256 + l * 65536;
57 /* Used for attribute_hash. */
59 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
61 typedef substring *compare_type;
62 static inline hashval_t hash (const attribute_spec *);
63 static inline bool equal (const attribute_spec *, const substring *);
66 inline hashval_t
67 attribute_hasher::hash (const attribute_spec *spec)
69 const int l = strlen (spec->name);
70 return substring_hash (spec->name, l);
73 inline bool
74 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
76 return (strncmp (spec->name, str->str, str->length) == 0
77 && !spec->name[str->length]);
80 /* Scoped attribute name representation. */
82 struct scoped_attributes
84 const char *ns;
85 vec<attribute_spec> attributes;
86 hash_table<attribute_hasher> *attribute_hash;
89 /* The table of scope attributes. */
90 static vec<scoped_attributes> attributes_table;
92 static scoped_attributes* find_attribute_namespace (const char*);
93 static void register_scoped_attribute (const struct attribute_spec *,
94 scoped_attributes *);
96 static bool attributes_initialized = false;
98 /* Default empty table of attributes. */
100 static const struct attribute_spec empty_attribute_table[] =
102 { NULL, 0, 0, false, false, false, NULL, false }
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. */
108 static void
109 extract_attribute_substring (struct substring *str)
111 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
112 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
114 str->length -= 4;
115 str->str += 2;
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. */
124 scoped_attributes*
125 register_scoped_attributes (const struct attribute_spec * attributes,
126 const char* ns)
128 scoped_attributes *result = NULL;
130 /* See if we already have attributes in the namespace NS. */
131 result = find_attribute_namespace (ns);
133 if (result == NULL)
135 /* We don't have any namespace NS yet. Create one. */
136 scoped_attributes sa;
138 if (!attributes_table.is_empty ())
139 attributes_table.create (64);
141 memset (&sa, 0, sizeof (sa));
142 sa.ns = ns;
143 sa.attributes.create (64);
144 result = attributes_table.safe_push (sa);
145 result->attribute_hash = new hash_table<attribute_hasher> (200);
148 /* Really add the attributes to their namespace now. */
149 for (unsigned i = 0; attributes[i].name != NULL; ++i)
151 result->attributes.safe_push (attributes[i]);
152 register_scoped_attribute (&attributes[i], result);
155 gcc_assert (result != NULL);
157 return result;
160 /* Return the namespace which name is NS, NULL if none exist. */
162 static scoped_attributes*
163 find_attribute_namespace (const char* ns)
165 unsigned ix;
166 scoped_attributes *iter;
168 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
169 if (ns == iter->ns
170 || (iter->ns != NULL
171 && ns != NULL
172 && !strcmp (iter->ns, ns)))
173 return iter;
174 return NULL;
177 /* Initialize attribute tables, and make some sanity checks
178 if --enable-checking. */
180 void
181 init_attributes (void)
183 size_t i;
185 if (attributes_initialized)
186 return;
188 attribute_tables[0] = lang_hooks.common_attribute_table;
189 attribute_tables[1] = lang_hooks.attribute_table;
190 attribute_tables[2] = lang_hooks.format_attribute_table;
191 attribute_tables[3] = targetm.attribute_table;
193 /* Translate NULL pointers to pointers to the empty table. */
194 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
195 if (attribute_tables[i] == NULL)
196 attribute_tables[i] = empty_attribute_table;
198 #ifdef ENABLE_CHECKING
199 /* Make some sanity checks on the attribute tables. */
200 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
202 int j;
204 for (j = 0; attribute_tables[i][j].name != NULL; j++)
206 /* The name must not begin and end with __. */
207 const char *name = attribute_tables[i][j].name;
208 int len = strlen (name);
210 gcc_assert (!(name[0] == '_' && name[1] == '_'
211 && name[len - 1] == '_' && name[len - 2] == '_'));
213 /* The minimum and maximum lengths must be consistent. */
214 gcc_assert (attribute_tables[i][j].min_length >= 0);
216 gcc_assert (attribute_tables[i][j].max_length == -1
217 || (attribute_tables[i][j].max_length
218 >= attribute_tables[i][j].min_length));
220 /* An attribute cannot require both a DECL and a TYPE. */
221 gcc_assert (!attribute_tables[i][j].decl_required
222 || !attribute_tables[i][j].type_required);
224 /* If an attribute requires a function type, in particular
225 it requires a type. */
226 gcc_assert (!attribute_tables[i][j].function_type_required
227 || attribute_tables[i][j].type_required);
231 /* Check that each name occurs just once in each table. */
232 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
234 int j, k;
235 for (j = 0; attribute_tables[i][j].name != NULL; j++)
236 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
237 gcc_assert (strcmp (attribute_tables[i][j].name,
238 attribute_tables[i][k].name));
240 /* Check that no name occurs in more than one table. Names that
241 begin with '*' are exempt, and may be overridden. */
242 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
244 size_t j, k, l;
246 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
247 for (k = 0; attribute_tables[i][k].name != NULL; k++)
248 for (l = 0; attribute_tables[j][l].name != NULL; l++)
249 gcc_assert (attribute_tables[i][k].name[0] == '*'
250 || strcmp (attribute_tables[i][k].name,
251 attribute_tables[j][l].name));
253 #endif
255 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
256 /* Put all the GNU attributes into the "gnu" namespace. */
257 register_scoped_attributes (attribute_tables[i], "gnu");
259 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
260 attributes_initialized = true;
263 /* Insert a single ATTR into the attribute table. */
265 void
266 register_attribute (const struct attribute_spec *attr)
268 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
271 /* Insert a single attribute ATTR into a namespace of attributes. */
273 static void
274 register_scoped_attribute (const struct attribute_spec *attr,
275 scoped_attributes *name_space)
277 struct substring str;
278 attribute_spec **slot;
280 gcc_assert (attr != NULL && name_space != NULL);
282 gcc_assert (name_space->attribute_hash);
284 str.str = attr->name;
285 str.length = strlen (str.str);
287 /* Attribute names in the table must be in the form 'text' and not
288 in the form '__text__'. */
289 gcc_assert (str.length > 0 && str.str[0] != '_');
291 slot = name_space->attribute_hash
292 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
293 INSERT);
294 gcc_assert (!*slot || attr->name[0] == '*');
295 *slot = CONST_CAST (struct attribute_spec *, attr);
298 /* Return the spec for the scoped attribute with namespace NS and
299 name NAME. */
301 static const struct attribute_spec *
302 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
304 struct substring attr;
305 scoped_attributes *attrs;
307 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
309 attrs = find_attribute_namespace (ns_str);
311 if (attrs == NULL)
312 return NULL;
314 attr.str = IDENTIFIER_POINTER (name);
315 attr.length = IDENTIFIER_LENGTH (name);
316 extract_attribute_substring (&attr);
317 return attrs->attribute_hash->find_with_hash (&attr,
318 substring_hash (attr.str,
319 attr.length));
322 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
323 it also specifies the attribute namespace. */
325 const struct attribute_spec *
326 lookup_attribute_spec (const_tree name)
328 tree ns;
329 if (TREE_CODE (name) == TREE_LIST)
331 ns = TREE_PURPOSE (name);
332 name = TREE_VALUE (name);
334 else
335 ns = get_identifier ("gnu");
336 return lookup_scoped_attribute_spec (ns, name);
340 /* Return the namespace of the attribute ATTR. This accessor works on
341 GNU and C++11 (scoped) attributes. On GNU attributes,
342 it returns an identifier tree for the string "gnu".
344 Please read the comments of cxx11_attribute_p to understand the
345 format of attributes. */
347 static tree
348 get_attribute_namespace (const_tree attr)
350 if (cxx11_attribute_p (attr))
351 return TREE_PURPOSE (TREE_PURPOSE (attr));
352 return get_identifier ("gnu");
356 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
357 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
358 it should be modified in place; if a TYPE, a copy should be created
359 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
360 information, in the form of a bitwise OR of flags in enum attribute_flags
361 from tree.h. Depending on these flags, some attributes may be
362 returned to be applied at a later stage (for example, to apply
363 a decl attribute to the declaration rather than to its type). */
365 tree
366 decl_attributes (tree *node, tree attributes, int flags)
368 tree a;
369 tree returned_attrs = NULL_TREE;
371 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
372 return NULL_TREE;
374 if (!attributes_initialized)
375 init_attributes ();
377 /* If this is a function and the user used #pragma GCC optimize, add the
378 options to the attribute((optimize(...))) list. */
379 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
381 tree cur_attr = lookup_attribute ("optimize", attributes);
382 tree opts = copy_list (current_optimize_pragma);
384 if (! cur_attr)
385 attributes
386 = tree_cons (get_identifier ("optimize"), opts, attributes);
387 else
388 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
391 if (TREE_CODE (*node) == FUNCTION_DECL
392 && optimization_current_node != optimization_default_node
393 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
394 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
396 /* If this is a function and the user used #pragma GCC target, add the
397 options to the attribute((target(...))) list. */
398 if (TREE_CODE (*node) == FUNCTION_DECL
399 && current_target_pragma
400 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
401 current_target_pragma, 0))
403 tree cur_attr = lookup_attribute ("target", attributes);
404 tree opts = copy_list (current_target_pragma);
406 if (! cur_attr)
407 attributes = tree_cons (get_identifier ("target"), opts, attributes);
408 else
409 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
412 /* A "naked" function attribute implies "noinline" and "noclone" for
413 those targets that support it. */
414 if (TREE_CODE (*node) == FUNCTION_DECL
415 && attributes
416 && lookup_attribute_spec (get_identifier ("naked"))
417 && lookup_attribute ("naked", attributes) != NULL)
419 if (lookup_attribute ("noinline", attributes) == NULL)
420 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
422 if (lookup_attribute ("noclone", attributes) == NULL)
423 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
426 targetm.insert_attributes (*node, &attributes);
428 for (a = attributes; a; a = TREE_CHAIN (a))
430 tree ns = get_attribute_namespace (a);
431 tree name = get_attribute_name (a);
432 tree args = TREE_VALUE (a);
433 tree *anode = node;
434 const struct attribute_spec *spec =
435 lookup_scoped_attribute_spec (ns, name);
436 bool no_add_attrs = 0;
437 int fn_ptr_quals = 0;
438 tree fn_ptr_tmp = NULL_TREE;
440 if (spec == NULL)
442 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
444 if (ns == NULL_TREE || !cxx11_attribute_p (a))
445 warning (OPT_Wattributes, "%qE attribute directive ignored",
446 name);
447 else
448 warning (OPT_Wattributes,
449 "%<%E::%E%> scoped attribute directive ignored",
450 ns, name);
452 continue;
454 else if (list_length (args) < spec->min_length
455 || (spec->max_length >= 0
456 && list_length (args) > spec->max_length))
458 error ("wrong number of arguments specified for %qE attribute",
459 name);
460 continue;
462 gcc_assert (is_attribute_p (spec->name, name));
464 if (TYPE_P (*node)
465 && cxx11_attribute_p (a)
466 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
468 /* This is a c++11 attribute that appertains to a
469 type-specifier, outside of the definition of, a class
470 type. Ignore it. */
471 if (warning (OPT_Wattributes, "attribute ignored"))
472 inform (input_location,
473 "an attribute that appertains to a type-specifier "
474 "is ignored");
475 continue;
478 if (spec->decl_required && !DECL_P (*anode))
480 if (flags & ((int) ATTR_FLAG_DECL_NEXT
481 | (int) ATTR_FLAG_FUNCTION_NEXT
482 | (int) ATTR_FLAG_ARRAY_NEXT))
484 /* Pass on this attribute to be tried again. */
485 returned_attrs = tree_cons (name, args, returned_attrs);
486 continue;
488 else
490 warning (OPT_Wattributes, "%qE attribute does not apply to types",
491 name);
492 continue;
496 /* If we require a type, but were passed a decl, set up to make a
497 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
498 would have applied if we'd been passed a type, but we cannot modify
499 the decl's type in place here. */
500 if (spec->type_required && DECL_P (*anode))
502 anode = &TREE_TYPE (*anode);
503 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
506 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
507 && TREE_CODE (*anode) != METHOD_TYPE)
509 if (TREE_CODE (*anode) == POINTER_TYPE
510 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
511 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
513 /* OK, this is a bit convoluted. We can't just make a copy
514 of the pointer type and modify its TREE_TYPE, because if
515 we change the attributes of the target type the pointer
516 type needs to have a different TYPE_MAIN_VARIANT. So we
517 pull out the target type now, frob it as appropriate, and
518 rebuild the pointer type later.
520 This would all be simpler if attributes were part of the
521 declarator, grumble grumble. */
522 fn_ptr_tmp = TREE_TYPE (*anode);
523 fn_ptr_quals = TYPE_QUALS (*anode);
524 anode = &fn_ptr_tmp;
525 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
527 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
529 /* Pass on this attribute to be tried again. */
530 returned_attrs = tree_cons (name, args, returned_attrs);
531 continue;
534 if (TREE_CODE (*anode) != FUNCTION_TYPE
535 && TREE_CODE (*anode) != METHOD_TYPE)
537 warning (OPT_Wattributes,
538 "%qE attribute only applies to function types",
539 name);
540 continue;
544 if (TYPE_P (*anode)
545 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
546 && TYPE_SIZE (*anode) != NULL_TREE)
548 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
549 continue;
552 if (spec->handler != NULL)
554 int cxx11_flag =
555 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
557 returned_attrs = chainon ((*spec->handler) (anode, name, args,
558 flags|cxx11_flag,
559 &no_add_attrs),
560 returned_attrs);
563 /* Layout the decl in case anything changed. */
564 if (spec->type_required && DECL_P (*node)
565 && (TREE_CODE (*node) == VAR_DECL
566 || TREE_CODE (*node) == PARM_DECL
567 || TREE_CODE (*node) == RESULT_DECL))
568 relayout_decl (*node);
570 if (!no_add_attrs)
572 tree old_attrs;
573 tree a;
575 if (DECL_P (*anode))
576 old_attrs = DECL_ATTRIBUTES (*anode);
577 else
578 old_attrs = TYPE_ATTRIBUTES (*anode);
580 for (a = lookup_attribute (spec->name, old_attrs);
581 a != NULL_TREE;
582 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
584 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
585 break;
588 if (a == NULL_TREE)
590 /* This attribute isn't already in the list. */
591 if (DECL_P (*anode))
592 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
593 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
595 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
596 /* If this is the main variant, also push the attributes
597 out to the other variants. */
598 if (*anode == TYPE_MAIN_VARIANT (*anode))
600 tree variant;
601 for (variant = *anode; variant;
602 variant = TYPE_NEXT_VARIANT (variant))
604 if (TYPE_ATTRIBUTES (variant) == old_attrs)
605 TYPE_ATTRIBUTES (variant)
606 = TYPE_ATTRIBUTES (*anode);
607 else if (!lookup_attribute
608 (spec->name, TYPE_ATTRIBUTES (variant)))
609 TYPE_ATTRIBUTES (variant) = tree_cons
610 (name, args, TYPE_ATTRIBUTES (variant));
614 else
615 *anode = build_type_attribute_variant (*anode,
616 tree_cons (name, args,
617 old_attrs));
621 if (fn_ptr_tmp)
623 /* Rebuild the function pointer type and put it in the
624 appropriate place. */
625 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
626 if (fn_ptr_quals)
627 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
628 if (DECL_P (*node))
629 TREE_TYPE (*node) = fn_ptr_tmp;
630 else
632 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
633 *node = fn_ptr_tmp;
638 return returned_attrs;
641 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
642 attribute.
644 When G++ parses a C++11 attribute, it is represented as
645 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
646 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
647 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
648 use get_attribute_namespace and get_attribute_name to retrieve the
649 namespace and name of the attribute, as these accessors work with
650 GNU attributes as well. */
652 bool
653 cxx11_attribute_p (const_tree attr)
655 if (attr == NULL_TREE
656 || TREE_CODE (attr) != TREE_LIST)
657 return false;
659 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
662 /* Return the name of the attribute ATTR. This accessor works on GNU
663 and C++11 (scoped) attributes.
665 Please read the comments of cxx11_attribute_p to understand the
666 format of attributes. */
668 tree
669 get_attribute_name (const_tree attr)
671 if (cxx11_attribute_p (attr))
672 return TREE_VALUE (TREE_PURPOSE (attr));
673 return TREE_PURPOSE (attr);
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);