Revert:PR c++/50800
[official-gcc.git] / gcc / attribs.c
blob7b7e2a9af1bc675c4ca72960f12e1af52dbf1ddf
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 "hash-set.h"
25 #include "vec.h"
26 #include "symtab.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "double-int.h"
30 #include "machmode.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "ggc.h"
39 #include "tm_p.h"
40 #include "cpplib.h"
41 #include "target.h"
42 #include "langhooks.h"
43 #include "hash-table.h"
44 #include "plugin.h"
46 /* Table of the tables of attributes (common, language, format, machine)
47 searched. */
48 static const struct attribute_spec *attribute_tables[4];
50 /* Substring representation. */
52 struct substring
54 const char *str;
55 int length;
58 /* Simple hash function to avoid need to scan whole string. */
60 static inline hashval_t
61 substring_hash (const char *str, int l)
63 return str[0] + str[l - 1] * 256 + l * 65536;
66 /* Used for attribute_hash. */
68 struct attribute_hasher : typed_noop_remove <attribute_spec>
70 typedef attribute_spec *value_type;
71 typedef substring *compare_type;
72 static inline hashval_t hash (const attribute_spec *);
73 static inline bool equal (const attribute_spec *, const substring *);
76 inline hashval_t
77 attribute_hasher::hash (const attribute_spec *spec)
79 const int l = strlen (spec->name);
80 return substring_hash (spec->name, l);
83 inline bool
84 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
86 return (strncmp (spec->name, str->str, str->length) == 0
87 && !spec->name[str->length]);
90 /* Scoped attribute name representation. */
92 struct scoped_attributes
94 const char *ns;
95 vec<attribute_spec> attributes;
96 hash_table<attribute_hasher> *attribute_hash;
99 /* The table of scope attributes. */
100 static vec<scoped_attributes> attributes_table;
102 static scoped_attributes* find_attribute_namespace (const char*);
103 static void register_scoped_attribute (const struct attribute_spec *,
104 scoped_attributes *);
106 static bool attributes_initialized = false;
108 /* Default empty table of attributes. */
110 static const struct attribute_spec empty_attribute_table[] =
112 { NULL, 0, 0, false, false, false, NULL, false }
115 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
116 To avoid need for copying, we simply return length of the string. */
118 static void
119 extract_attribute_substring (struct substring *str)
121 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
122 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
124 str->length -= 4;
125 str->str += 2;
129 /* Insert an array of attributes ATTRIBUTES into a namespace. This
130 array must be NULL terminated. NS is the name of attribute
131 namespace. The function returns the namespace into which the
132 attributes have been registered. */
134 scoped_attributes*
135 register_scoped_attributes (const struct attribute_spec * attributes,
136 const char* ns)
138 scoped_attributes *result = NULL;
140 /* See if we already have attributes in the namespace NS. */
141 result = find_attribute_namespace (ns);
143 if (result == NULL)
145 /* We don't have any namespace NS yet. Create one. */
146 scoped_attributes sa;
148 if (!attributes_table.is_empty ())
149 attributes_table.create (64);
151 memset (&sa, 0, sizeof (sa));
152 sa.ns = ns;
153 sa.attributes.create (64);
154 result = attributes_table.safe_push (sa);
155 result->attribute_hash = new hash_table<attribute_hasher> (200);
158 /* Really add the attributes to their namespace now. */
159 for (unsigned i = 0; attributes[i].name != NULL; ++i)
161 result->attributes.safe_push (attributes[i]);
162 register_scoped_attribute (&attributes[i], result);
165 gcc_assert (result != NULL);
167 return result;
170 /* Return the namespace which name is NS, NULL if none exist. */
172 static scoped_attributes*
173 find_attribute_namespace (const char* ns)
175 unsigned ix;
176 scoped_attributes *iter;
178 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
179 if (ns == iter->ns
180 || (iter->ns != NULL
181 && ns != NULL
182 && !strcmp (iter->ns, ns)))
183 return iter;
184 return NULL;
187 /* Initialize attribute tables, and make some sanity checks
188 if --enable-checking. */
190 void
191 init_attributes (void)
193 size_t i;
195 if (attributes_initialized)
196 return;
198 attribute_tables[0] = lang_hooks.common_attribute_table;
199 attribute_tables[1] = lang_hooks.attribute_table;
200 attribute_tables[2] = lang_hooks.format_attribute_table;
201 attribute_tables[3] = targetm.attribute_table;
203 /* Translate NULL pointers to pointers to the empty table. */
204 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
205 if (attribute_tables[i] == NULL)
206 attribute_tables[i] = empty_attribute_table;
208 #ifdef ENABLE_CHECKING
209 /* Make some sanity checks on the attribute tables. */
210 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
212 int j;
214 for (j = 0; attribute_tables[i][j].name != NULL; j++)
216 /* The name must not begin and end with __. */
217 const char *name = attribute_tables[i][j].name;
218 int len = strlen (name);
220 gcc_assert (!(name[0] == '_' && name[1] == '_'
221 && name[len - 1] == '_' && name[len - 2] == '_'));
223 /* The minimum and maximum lengths must be consistent. */
224 gcc_assert (attribute_tables[i][j].min_length >= 0);
226 gcc_assert (attribute_tables[i][j].max_length == -1
227 || (attribute_tables[i][j].max_length
228 >= attribute_tables[i][j].min_length));
230 /* An attribute cannot require both a DECL and a TYPE. */
231 gcc_assert (!attribute_tables[i][j].decl_required
232 || !attribute_tables[i][j].type_required);
234 /* If an attribute requires a function type, in particular
235 it requires a type. */
236 gcc_assert (!attribute_tables[i][j].function_type_required
237 || attribute_tables[i][j].type_required);
241 /* Check that each name occurs just once in each table. */
242 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
244 int j, k;
245 for (j = 0; attribute_tables[i][j].name != NULL; j++)
246 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
247 gcc_assert (strcmp (attribute_tables[i][j].name,
248 attribute_tables[i][k].name));
250 /* Check that no name occurs in more than one table. Names that
251 begin with '*' are exempt, and may be overridden. */
252 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
254 size_t j, k, l;
256 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
257 for (k = 0; attribute_tables[i][k].name != NULL; k++)
258 for (l = 0; attribute_tables[j][l].name != NULL; l++)
259 gcc_assert (attribute_tables[i][k].name[0] == '*'
260 || strcmp (attribute_tables[i][k].name,
261 attribute_tables[j][l].name));
263 #endif
265 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
266 /* Put all the GNU attributes into the "gnu" namespace. */
267 register_scoped_attributes (attribute_tables[i], "gnu");
269 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
270 attributes_initialized = true;
273 /* Insert a single ATTR into the attribute table. */
275 void
276 register_attribute (const struct attribute_spec *attr)
278 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
281 /* Insert a single attribute ATTR into a namespace of attributes. */
283 static void
284 register_scoped_attribute (const struct attribute_spec *attr,
285 scoped_attributes *name_space)
287 struct substring str;
288 attribute_spec **slot;
290 gcc_assert (attr != NULL && name_space != NULL);
292 gcc_assert (name_space->attribute_hash);
294 str.str = attr->name;
295 str.length = strlen (str.str);
297 /* Attribute names in the table must be in the form 'text' and not
298 in the form '__text__'. */
299 gcc_assert (str.length > 0 && str.str[0] != '_');
301 slot = name_space->attribute_hash
302 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
303 INSERT);
304 gcc_assert (!*slot || attr->name[0] == '*');
305 *slot = CONST_CAST (struct attribute_spec *, attr);
308 /* Return the spec for the scoped attribute with namespace NS and
309 name NAME. */
311 static const struct attribute_spec *
312 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
314 struct substring attr;
315 scoped_attributes *attrs;
317 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
319 attrs = find_attribute_namespace (ns_str);
321 if (attrs == NULL)
322 return NULL;
324 attr.str = IDENTIFIER_POINTER (name);
325 attr.length = IDENTIFIER_LENGTH (name);
326 extract_attribute_substring (&attr);
327 return attrs->attribute_hash->find_with_hash (&attr,
328 substring_hash (attr.str,
329 attr.length));
332 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
333 it also specifies the attribute namespace. */
335 const struct attribute_spec *
336 lookup_attribute_spec (const_tree name)
338 tree ns;
339 if (TREE_CODE (name) == TREE_LIST)
341 ns = TREE_PURPOSE (name);
342 name = TREE_VALUE (name);
344 else
345 ns = get_identifier ("gnu");
346 return lookup_scoped_attribute_spec (ns, name);
350 /* Return the namespace of the attribute ATTR. This accessor works on
351 GNU and C++11 (scoped) attributes. On GNU attributes,
352 it returns an identifier tree for the string "gnu".
354 Please read the comments of cxx11_attribute_p to understand the
355 format of attributes. */
357 static tree
358 get_attribute_namespace (const_tree attr)
360 if (cxx11_attribute_p (attr))
361 return TREE_PURPOSE (TREE_PURPOSE (attr));
362 return get_identifier ("gnu");
366 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
367 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
368 it should be modified in place; if a TYPE, a copy should be created
369 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
370 information, in the form of a bitwise OR of flags in enum attribute_flags
371 from tree.h. Depending on these flags, some attributes may be
372 returned to be applied at a later stage (for example, to apply
373 a decl attribute to the declaration rather than to its type). */
375 tree
376 decl_attributes (tree *node, tree attributes, int flags)
378 tree a;
379 tree returned_attrs = NULL_TREE;
381 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
382 return NULL_TREE;
384 if (!attributes_initialized)
385 init_attributes ();
387 /* If this is a function and the user used #pragma GCC optimize, add the
388 options to the attribute((optimize(...))) list. */
389 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
391 tree cur_attr = lookup_attribute ("optimize", attributes);
392 tree opts = copy_list (current_optimize_pragma);
394 if (! cur_attr)
395 attributes
396 = tree_cons (get_identifier ("optimize"), opts, attributes);
397 else
398 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
401 if (TREE_CODE (*node) == FUNCTION_DECL
402 && optimization_current_node != optimization_default_node
403 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
406 /* If this is a function and the user used #pragma GCC target, add the
407 options to the attribute((target(...))) list. */
408 if (TREE_CODE (*node) == FUNCTION_DECL
409 && current_target_pragma
410 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
411 current_target_pragma, 0))
413 tree cur_attr = lookup_attribute ("target", attributes);
414 tree opts = copy_list (current_target_pragma);
416 if (! cur_attr)
417 attributes = tree_cons (get_identifier ("target"), opts, attributes);
418 else
419 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
422 /* A "naked" function attribute implies "noinline" and "noclone" for
423 those targets that support it. */
424 if (TREE_CODE (*node) == FUNCTION_DECL
425 && attributes
426 && lookup_attribute_spec (get_identifier ("naked"))
427 && lookup_attribute ("naked", attributes) != NULL)
429 if (lookup_attribute ("noinline", attributes) == NULL)
430 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
432 if (lookup_attribute ("noclone", attributes) == NULL)
433 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
436 targetm.insert_attributes (*node, &attributes);
438 for (a = attributes; a; a = TREE_CHAIN (a))
440 tree ns = get_attribute_namespace (a);
441 tree name = get_attribute_name (a);
442 tree args = TREE_VALUE (a);
443 tree *anode = node;
444 const struct attribute_spec *spec =
445 lookup_scoped_attribute_spec (ns, name);
446 bool no_add_attrs = 0;
447 int fn_ptr_quals = 0;
448 tree fn_ptr_tmp = NULL_TREE;
450 if (spec == NULL)
452 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
454 if (ns == NULL_TREE || !cxx11_attribute_p (a))
455 warning (OPT_Wattributes, "%qE attribute directive ignored",
456 name);
457 else
458 warning (OPT_Wattributes,
459 "%<%E::%E%> scoped attribute directive ignored",
460 ns, name);
462 continue;
464 else if (list_length (args) < spec->min_length
465 || (spec->max_length >= 0
466 && list_length (args) > spec->max_length))
468 error ("wrong number of arguments specified for %qE attribute",
469 name);
470 continue;
472 gcc_assert (is_attribute_p (spec->name, name));
474 if (TYPE_P (*node)
475 && cxx11_attribute_p (a)
476 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
478 /* This is a c++11 attribute that appertains to a
479 type-specifier, outside of the definition of, a class
480 type. Ignore it. */
481 warning (OPT_Wattributes, "attribute ignored");
482 inform (input_location,
483 "an attribute that appertains to a type-specifier "
484 "is ignored");
485 continue;
488 if (spec->decl_required && !DECL_P (*anode))
490 if (flags & ((int) ATTR_FLAG_DECL_NEXT
491 | (int) ATTR_FLAG_FUNCTION_NEXT
492 | (int) ATTR_FLAG_ARRAY_NEXT))
494 /* Pass on this attribute to be tried again. */
495 returned_attrs = tree_cons (name, args, returned_attrs);
496 continue;
498 else
500 warning (OPT_Wattributes, "%qE attribute does not apply to types",
501 name);
502 continue;
506 /* If we require a type, but were passed a decl, set up to make a
507 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
508 would have applied if we'd been passed a type, but we cannot modify
509 the decl's type in place here. */
510 if (spec->type_required && DECL_P (*anode))
512 anode = &TREE_TYPE (*anode);
513 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
516 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
517 && TREE_CODE (*anode) != METHOD_TYPE)
519 if (TREE_CODE (*anode) == POINTER_TYPE
520 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
521 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
523 /* OK, this is a bit convoluted. We can't just make a copy
524 of the pointer type and modify its TREE_TYPE, because if
525 we change the attributes of the target type the pointer
526 type needs to have a different TYPE_MAIN_VARIANT. So we
527 pull out the target type now, frob it as appropriate, and
528 rebuild the pointer type later.
530 This would all be simpler if attributes were part of the
531 declarator, grumble grumble. */
532 fn_ptr_tmp = TREE_TYPE (*anode);
533 fn_ptr_quals = TYPE_QUALS (*anode);
534 anode = &fn_ptr_tmp;
535 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
537 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
539 /* Pass on this attribute to be tried again. */
540 returned_attrs = tree_cons (name, args, returned_attrs);
541 continue;
544 if (TREE_CODE (*anode) != FUNCTION_TYPE
545 && TREE_CODE (*anode) != METHOD_TYPE)
547 warning (OPT_Wattributes,
548 "%qE attribute only applies to function types",
549 name);
550 continue;
554 if (TYPE_P (*anode)
555 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
556 && TYPE_SIZE (*anode) != NULL_TREE)
558 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
559 continue;
562 if (spec->handler != NULL)
564 int cxx11_flag =
565 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
567 returned_attrs = chainon ((*spec->handler) (anode, name, args,
568 flags|cxx11_flag,
569 &no_add_attrs),
570 returned_attrs);
573 /* Layout the decl in case anything changed. */
574 if (spec->type_required && DECL_P (*node)
575 && (TREE_CODE (*node) == VAR_DECL
576 || TREE_CODE (*node) == PARM_DECL
577 || TREE_CODE (*node) == RESULT_DECL))
578 relayout_decl (*node);
580 if (!no_add_attrs)
582 tree old_attrs;
583 tree a;
585 if (DECL_P (*anode))
586 old_attrs = DECL_ATTRIBUTES (*anode);
587 else
588 old_attrs = TYPE_ATTRIBUTES (*anode);
590 for (a = lookup_attribute (spec->name, old_attrs);
591 a != NULL_TREE;
592 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
594 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
595 break;
598 if (a == NULL_TREE)
600 /* This attribute isn't already in the list. */
601 if (DECL_P (*anode))
602 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
603 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
605 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
606 /* If this is the main variant, also push the attributes
607 out to the other variants. */
608 if (*anode == TYPE_MAIN_VARIANT (*anode))
610 tree variant;
611 for (variant = *anode; variant;
612 variant = TYPE_NEXT_VARIANT (variant))
614 if (TYPE_ATTRIBUTES (variant) == old_attrs)
615 TYPE_ATTRIBUTES (variant)
616 = TYPE_ATTRIBUTES (*anode);
617 else if (!lookup_attribute
618 (spec->name, TYPE_ATTRIBUTES (variant)))
619 TYPE_ATTRIBUTES (variant) = tree_cons
620 (name, args, TYPE_ATTRIBUTES (variant));
624 else
625 *anode = build_type_attribute_variant (*anode,
626 tree_cons (name, args,
627 old_attrs));
631 if (fn_ptr_tmp)
633 /* Rebuild the function pointer type and put it in the
634 appropriate place. */
635 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
636 if (fn_ptr_quals)
637 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
638 if (DECL_P (*node))
639 TREE_TYPE (*node) = fn_ptr_tmp;
640 else
642 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
643 *node = fn_ptr_tmp;
648 return returned_attrs;
651 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
652 attribute.
654 When G++ parses a C++11 attribute, it is represented as
655 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
656 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
657 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
658 use get_attribute_namespace and get_attribute_name to retrieve the
659 namespace and name of the attribute, as these accessors work with
660 GNU attributes as well. */
662 bool
663 cxx11_attribute_p (const_tree attr)
665 if (attr == NULL_TREE
666 || TREE_CODE (attr) != TREE_LIST)
667 return false;
669 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
672 /* Return the name of the attribute ATTR. This accessor works on GNU
673 and C++11 (scoped) attributes.
675 Please read the comments of cxx11_attribute_p to understand the
676 format of attributes. */
678 tree
679 get_attribute_name (const_tree attr)
681 if (cxx11_attribute_p (attr))
682 return TREE_VALUE (TREE_PURPOSE (attr));
683 return TREE_PURPOSE (attr);
686 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
687 to the method FNDECL. */
689 void
690 apply_tm_attr (tree fndecl, tree attr)
692 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);