PR c++/54859
[official-gcc.git] / gcc / attribs.c
blob0425de9f5b127f65c2bed05f1deb3c55d983c924
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "diagnostic-core.h"
29 #include "ggc.h"
30 #include "tm_p.h"
31 #include "cpplib.h"
32 #include "target.h"
33 #include "langhooks.h"
34 #include "hashtab.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 DEF_VEC_O (attribute_spec);
50 DEF_VEC_ALLOC_O (attribute_spec, heap);
52 /* Scoped attribute name representation. */
54 struct scoped_attributes
56 const char *ns;
57 VEC (attribute_spec, heap) *attributes;
58 htab_t attribute_hash;
61 DEF_VEC_O (scoped_attributes);
62 DEF_VEC_ALLOC_O (scoped_attributes, heap);
64 /* The table of scope attributes. */
65 static VEC(scoped_attributes, heap) *attributes_table;
67 static scoped_attributes* find_attribute_namespace (const char*);
68 static void register_scoped_attribute (const struct attribute_spec *,
69 scoped_attributes *);
71 static bool attributes_initialized = false;
73 /* Default empty table of attributes. */
75 static const struct attribute_spec empty_attribute_table[] =
77 { NULL, 0, 0, false, false, false, NULL, false }
80 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
81 To avoid need for copying, we simply return length of the string. */
83 static void
84 extract_attribute_substring (struct substring *str)
86 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
87 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
89 str->length -= 4;
90 str->str += 2;
94 /* Simple hash function to avoid need to scan whole string. */
96 static inline hashval_t
97 substring_hash (const char *str, int l)
99 return str[0] + str[l - 1] * 256 + l * 65536;
102 /* Used for attribute_hash. */
104 static hashval_t
105 hash_attr (const void *p)
107 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
108 const int l = strlen (spec->name);
110 return substring_hash (spec->name, l);
113 /* Used for attribute_hash. */
115 static int
116 eq_attr (const void *p, const void *q)
118 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
119 const struct substring *const str = (const struct substring *) q;
121 return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
124 /* Insert an array of attributes ATTRIBUTES into a namespace. This
125 array must be NULL terminated. NS is the name of attribute
126 namespace. The function returns the namespace into which the
127 attributes have been registered. */
129 scoped_attributes*
130 register_scoped_attributes (const struct attribute_spec * attributes,
131 const char* ns)
133 scoped_attributes *result = NULL;
135 /* See if we already have attributes in the namespace NS. */
136 result = find_attribute_namespace (ns);
138 if (result == NULL)
140 /* We don't have any namespace NS yet. Create one. */
141 scoped_attributes sa;
143 if (attributes_table == NULL)
144 attributes_table = VEC_alloc (scoped_attributes, heap, 64);
146 memset (&sa, 0, sizeof (sa));
147 sa.ns = ns;
148 sa.attributes = VEC_alloc (attribute_spec, heap, 64);
149 result = VEC_safe_push (scoped_attributes, heap, attributes_table, sa);
150 result->attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
153 /* Really add the attributes to their namespace now. */
154 for (unsigned i = 0; attributes[i].name != NULL; ++i)
156 VEC_safe_push (attribute_spec, heap,
157 result->attributes, attributes[i]);
158 register_scoped_attribute (&attributes[i], result);
161 gcc_assert (result != NULL);
163 return result;
166 /* Return the namespace which name is NS, NULL if none exist. */
168 static scoped_attributes*
169 find_attribute_namespace (const char* ns)
171 unsigned ix;
172 scoped_attributes *iter;
174 FOR_EACH_VEC_ELT (scoped_attributes, attributes_table, ix, iter)
175 if (ns == iter->ns
176 || (iter->ns != NULL
177 && ns != NULL
178 && !strcmp (iter->ns, ns)))
179 return iter;
180 return NULL;
183 /* Initialize attribute tables, and make some sanity checks
184 if --enable-checking. */
186 void
187 init_attributes (void)
189 size_t i;
191 if (attributes_initialized)
192 return;
194 attribute_tables[0] = lang_hooks.common_attribute_table;
195 attribute_tables[1] = lang_hooks.attribute_table;
196 attribute_tables[2] = lang_hooks.format_attribute_table;
197 attribute_tables[3] = targetm.attribute_table;
199 /* Translate NULL pointers to pointers to the empty table. */
200 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
201 if (attribute_tables[i] == NULL)
202 attribute_tables[i] = empty_attribute_table;
204 #ifdef ENABLE_CHECKING
205 /* Make some sanity checks on the attribute tables. */
206 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
208 int j;
210 for (j = 0; attribute_tables[i][j].name != NULL; j++)
212 /* The name must not begin and end with __. */
213 const char *name = attribute_tables[i][j].name;
214 int len = strlen (name);
216 gcc_assert (!(name[0] == '_' && name[1] == '_'
217 && name[len - 1] == '_' && name[len - 2] == '_'));
219 /* The minimum and maximum lengths must be consistent. */
220 gcc_assert (attribute_tables[i][j].min_length >= 0);
222 gcc_assert (attribute_tables[i][j].max_length == -1
223 || (attribute_tables[i][j].max_length
224 >= attribute_tables[i][j].min_length));
226 /* An attribute cannot require both a DECL and a TYPE. */
227 gcc_assert (!attribute_tables[i][j].decl_required
228 || !attribute_tables[i][j].type_required);
230 /* If an attribute requires a function type, in particular
231 it requires a type. */
232 gcc_assert (!attribute_tables[i][j].function_type_required
233 || attribute_tables[i][j].type_required);
237 /* Check that each name occurs just once in each table. */
238 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
240 int j, k;
241 for (j = 0; attribute_tables[i][j].name != NULL; j++)
242 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
243 gcc_assert (strcmp (attribute_tables[i][j].name,
244 attribute_tables[i][k].name));
246 /* Check that no name occurs in more than one table. Names that
247 begin with '*' are exempt, and may be overridden. */
248 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
250 size_t j, k, l;
252 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
253 for (k = 0; attribute_tables[i][k].name != NULL; k++)
254 for (l = 0; attribute_tables[j][l].name != NULL; l++)
255 gcc_assert (attribute_tables[i][k].name[0] == '*'
256 || strcmp (attribute_tables[i][k].name,
257 attribute_tables[j][l].name));
259 #endif
261 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
262 /* Put all the GNU attributes into the "gnu" namespace. */
263 register_scoped_attributes (attribute_tables[i], "gnu");
265 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
266 attributes_initialized = true;
269 /* Insert a single ATTR into the attribute table. */
271 void
272 register_attribute (const struct attribute_spec *attr)
274 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
277 /* Insert a single attribute ATTR into a namespace of attributes. */
279 static void
280 register_scoped_attribute (const struct attribute_spec *attr,
281 scoped_attributes *name_space)
283 struct substring str;
284 void **slot;
286 gcc_assert (attr != NULL && name_space != NULL);
288 gcc_assert (name_space->attribute_hash != NULL);
290 str.str = attr->name;
291 str.length = strlen (str.str);
293 /* Attribute names in the table must be in the form 'text' and not
294 in the form '__text__'. */
295 gcc_assert (str.length > 0 && str.str[0] != '_');
297 slot = htab_find_slot_with_hash (name_space->attribute_hash, &str,
298 substring_hash (str.str, str.length),
299 INSERT);
300 gcc_assert (!*slot || attr->name[0] == '*');
301 *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
304 /* Return the spec for the scoped attribute with namespace NS and
305 name NAME. */
307 const struct attribute_spec *
308 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
310 struct substring attr;
311 scoped_attributes *attrs;
313 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
315 attrs = find_attribute_namespace (ns_str);
317 if (attrs == NULL)
318 return NULL;
320 attr.str = IDENTIFIER_POINTER (name);
321 attr.length = IDENTIFIER_LENGTH (name);
322 extract_attribute_substring (&attr);
323 return (const struct attribute_spec *)
324 htab_find_with_hash (attrs->attribute_hash, &attr,
325 substring_hash (attr.str, attr.length));
328 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
329 it also specifies the attribute namespace. */
331 const struct attribute_spec *
332 lookup_attribute_spec (const_tree name)
334 tree ns;
335 if (TREE_CODE (name) == TREE_LIST)
337 ns = TREE_PURPOSE (name);
338 name = TREE_VALUE (name);
340 else
341 ns = get_identifier ("gnu");
342 return lookup_scoped_attribute_spec (ns, name);
346 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
347 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
348 it should be modified in place; if a TYPE, a copy should be created
349 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
350 information, in the form of a bitwise OR of flags in enum attribute_flags
351 from tree.h. Depending on these flags, some attributes may be
352 returned to be applied at a later stage (for example, to apply
353 a decl attribute to the declaration rather than to its type). */
355 tree
356 decl_attributes (tree *node, tree attributes, int flags)
358 tree a;
359 tree returned_attrs = NULL_TREE;
361 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
362 return NULL_TREE;
364 if (!attributes_initialized)
365 init_attributes ();
367 /* If this is a function and the user used #pragma GCC optimize, add the
368 options to the attribute((optimize(...))) list. */
369 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
371 tree cur_attr = lookup_attribute ("optimize", attributes);
372 tree opts = copy_list (current_optimize_pragma);
374 if (! cur_attr)
375 attributes
376 = tree_cons (get_identifier ("optimize"), opts, attributes);
377 else
378 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
381 if (TREE_CODE (*node) == FUNCTION_DECL
382 && optimization_current_node != optimization_default_node
383 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
384 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
386 /* If this is a function and the user used #pragma GCC target, add the
387 options to the attribute((target(...))) list. */
388 if (TREE_CODE (*node) == FUNCTION_DECL
389 && current_target_pragma
390 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
391 current_target_pragma, 0))
393 tree cur_attr = lookup_attribute ("target", attributes);
394 tree opts = copy_list (current_target_pragma);
396 if (! cur_attr)
397 attributes = tree_cons (get_identifier ("target"), opts, attributes);
398 else
399 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
402 /* A "naked" function attribute implies "noinline" and "noclone" for
403 those targets that support it. */
404 if (TREE_CODE (*node) == FUNCTION_DECL
405 && attributes
406 && lookup_attribute_spec (get_identifier ("naked"))
407 && lookup_attribute ("naked", attributes) != NULL)
409 if (lookup_attribute ("noinline", attributes) == NULL)
410 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
412 if (lookup_attribute ("noclone", attributes) == NULL)
413 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
416 targetm.insert_attributes (*node, &attributes);
418 for (a = attributes; a; a = TREE_CHAIN (a))
420 tree ns = get_attribute_namespace (a);
421 tree name = get_attribute_name (a);
422 tree args = TREE_VALUE (a);
423 tree *anode = node;
424 const struct attribute_spec *spec =
425 lookup_scoped_attribute_spec (ns, name);
426 bool no_add_attrs = 0;
427 int fn_ptr_quals = 0;
428 tree fn_ptr_tmp = NULL_TREE;
430 if (spec == NULL)
432 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
434 if (ns == NULL_TREE || !cxx11_attribute_p (a))
435 warning (OPT_Wattributes, "%qE attribute directive ignored",
436 name);
437 else
438 warning (OPT_Wattributes,
439 "%<%E::%E%> scoped attribute directive ignored",
440 ns, name);
442 continue;
444 else if (list_length (args) < spec->min_length
445 || (spec->max_length >= 0
446 && list_length (args) > spec->max_length))
448 error ("wrong number of arguments specified for %qE attribute",
449 name);
450 continue;
452 gcc_assert (is_attribute_p (spec->name, name));
454 if (TYPE_P (*node)
455 && cxx11_attribute_p (a)
456 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
458 /* This is a c++11 attribute that appertains to a
459 type-specifier, outside of the definition of, a class
460 type. Ignore it. */
461 warning (OPT_Wattributes, "attribute ignored");
462 inform (input_location,
463 "an attribute that appertains to a type-specifier "
464 "is ignored");
465 continue;
468 if (spec->decl_required && !DECL_P (*anode))
470 if (flags & ((int) ATTR_FLAG_DECL_NEXT
471 | (int) ATTR_FLAG_FUNCTION_NEXT
472 | (int) ATTR_FLAG_ARRAY_NEXT))
474 /* Pass on this attribute to be tried again. */
475 returned_attrs = tree_cons (name, args, returned_attrs);
476 continue;
478 else
480 warning (OPT_Wattributes, "%qE attribute does not apply to types",
481 name);
482 continue;
486 /* If we require a type, but were passed a decl, set up to make a
487 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
488 would have applied if we'd been passed a type, but we cannot modify
489 the decl's type in place here. */
490 if (spec->type_required && DECL_P (*anode))
492 anode = &TREE_TYPE (*anode);
493 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
494 if (!(TREE_CODE (*anode) == TYPE_DECL
495 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
496 (TREE_TYPE (*anode)))))
497 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
500 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
501 && TREE_CODE (*anode) != METHOD_TYPE)
503 if (TREE_CODE (*anode) == POINTER_TYPE
504 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
505 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
507 /* OK, this is a bit convoluted. We can't just make a copy
508 of the pointer type and modify its TREE_TYPE, because if
509 we change the attributes of the target type the pointer
510 type needs to have a different TYPE_MAIN_VARIANT. So we
511 pull out the target type now, frob it as appropriate, and
512 rebuild the pointer type later.
514 This would all be simpler if attributes were part of the
515 declarator, grumble grumble. */
516 fn_ptr_tmp = TREE_TYPE (*anode);
517 fn_ptr_quals = TYPE_QUALS (*anode);
518 anode = &fn_ptr_tmp;
519 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
521 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
523 /* Pass on this attribute to be tried again. */
524 returned_attrs = tree_cons (name, args, returned_attrs);
525 continue;
528 if (TREE_CODE (*anode) != FUNCTION_TYPE
529 && TREE_CODE (*anode) != METHOD_TYPE)
531 warning (OPT_Wattributes,
532 "%qE attribute only applies to function types",
533 name);
534 continue;
538 if (TYPE_P (*anode)
539 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
540 && TYPE_SIZE (*anode) != NULL_TREE)
542 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
543 continue;
546 if (spec->handler != NULL)
548 int cxx11_flag =
549 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
551 returned_attrs = chainon ((*spec->handler) (anode, name, args,
552 flags|cxx11_flag,
553 &no_add_attrs),
554 returned_attrs);
557 /* Layout the decl in case anything changed. */
558 if (spec->type_required && DECL_P (*node)
559 && (TREE_CODE (*node) == VAR_DECL
560 || TREE_CODE (*node) == PARM_DECL
561 || TREE_CODE (*node) == RESULT_DECL))
562 relayout_decl (*node);
564 if (!no_add_attrs)
566 tree old_attrs;
567 tree a;
569 if (DECL_P (*anode))
570 old_attrs = DECL_ATTRIBUTES (*anode);
571 else
572 old_attrs = TYPE_ATTRIBUTES (*anode);
574 for (a = lookup_attribute (spec->name, old_attrs);
575 a != NULL_TREE;
576 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
578 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
579 break;
582 if (a == NULL_TREE)
584 /* This attribute isn't already in the list. */
585 if (DECL_P (*anode))
586 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
587 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
589 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
590 /* If this is the main variant, also push the attributes
591 out to the other variants. */
592 if (*anode == TYPE_MAIN_VARIANT (*anode))
594 tree variant;
595 for (variant = *anode; variant;
596 variant = TYPE_NEXT_VARIANT (variant))
598 if (TYPE_ATTRIBUTES (variant) == old_attrs)
599 TYPE_ATTRIBUTES (variant)
600 = TYPE_ATTRIBUTES (*anode);
601 else if (!lookup_attribute
602 (spec->name, TYPE_ATTRIBUTES (variant)))
603 TYPE_ATTRIBUTES (variant) = tree_cons
604 (name, args, TYPE_ATTRIBUTES (variant));
608 else
609 *anode = build_type_attribute_variant (*anode,
610 tree_cons (name, args,
611 old_attrs));
615 if (fn_ptr_tmp)
617 /* Rebuild the function pointer type and put it in the
618 appropriate place. */
619 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
620 if (fn_ptr_quals)
621 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
622 if (DECL_P (*node))
623 TREE_TYPE (*node) = fn_ptr_tmp;
624 else
626 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
627 *node = fn_ptr_tmp;
632 return returned_attrs;
635 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
636 attribute.
638 When G++ parses a C++11 attribute, it is represented as
639 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
640 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
641 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
642 use get_attribute_namespace and get_attribute_name to retrieve the
643 namespace and name of the attribute, as these accessors work with
644 GNU attributes as well. */
646 bool
647 cxx11_attribute_p (const_tree attr)
649 if (attr == NULL_TREE
650 || TREE_CODE (attr) != TREE_LIST)
651 return false;
653 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
656 /* Return the name of the attribute ATTR. This accessor works on GNU
657 and C++11 (scoped) attributes.
659 Please read the comments of cxx11_attribute_p to understand the
660 format of attributes. */
662 tree
663 get_attribute_name (const_tree attr)
665 if (cxx11_attribute_p (attr))
666 return TREE_VALUE (TREE_PURPOSE (attr));
667 return TREE_PURPOSE (attr);
670 /* Return the namespace of the attribute ATTR. This accessor works on
671 GNU and C++11 (scoped) attributes. On GNU attributes,
672 it returns an identifier tree for the string "gnu".
674 Please read the comments of cxx11_attribute_p to understand the
675 format of attributes. */
677 tree
678 get_attribute_namespace (const_tree attr)
680 if (cxx11_attribute_p (attr))
681 return TREE_PURPOSE (TREE_PURPOSE (attr));
682 return get_identifier ("gnu");
685 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
686 to the method FNDECL. */
688 void
689 apply_tm_attr (tree fndecl, tree attr)
691 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);