gcc/
[official-gcc.git] / gcc / attribs.c
blob265009b47a2e945bd3178533212b235eabd48429
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 "symtab.h"
25 #include "alias.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "attribs.h"
29 #include "stor-layout.h"
30 #include "flags.h"
31 #include "diagnostic-core.h"
32 #include "tm_p.h"
33 #include "cpplib.h"
34 #include "target.h"
35 #include "langhooks.h"
36 #include "plugin.h"
38 /* Table of the tables of attributes (common, language, format, machine)
39 searched. */
40 static const struct attribute_spec *attribute_tables[4];
42 /* Substring representation. */
44 struct substring
46 const char *str;
47 int length;
50 /* Simple hash function to avoid need to scan whole string. */
52 static inline hashval_t
53 substring_hash (const char *str, int l)
55 return str[0] + str[l - 1] * 256 + l * 65536;
58 /* Used for attribute_hash. */
60 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
62 typedef substring *compare_type;
63 static inline hashval_t hash (const attribute_spec *);
64 static inline bool equal (const attribute_spec *, const substring *);
67 inline hashval_t
68 attribute_hasher::hash (const attribute_spec *spec)
70 const int l = strlen (spec->name);
71 return substring_hash (spec->name, l);
74 inline bool
75 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
77 return (strncmp (spec->name, str->str, str->length) == 0
78 && !spec->name[str->length]);
81 /* Scoped attribute name representation. */
83 struct scoped_attributes
85 const char *ns;
86 vec<attribute_spec> attributes;
87 hash_table<attribute_hasher> *attribute_hash;
90 /* The table of scope attributes. */
91 static vec<scoped_attributes> attributes_table;
93 static scoped_attributes* find_attribute_namespace (const char*);
94 static void register_scoped_attribute (const struct attribute_spec *,
95 scoped_attributes *);
97 static bool attributes_initialized = false;
99 /* Default empty table of attributes. */
101 static const struct attribute_spec empty_attribute_table[] =
103 { NULL, 0, 0, false, false, false, NULL, false }
106 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
107 To avoid need for copying, we simply return length of the string. */
109 static void
110 extract_attribute_substring (struct substring *str)
112 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
113 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
115 str->length -= 4;
116 str->str += 2;
120 /* Insert an array of attributes ATTRIBUTES into a namespace. This
121 array must be NULL terminated. NS is the name of attribute
122 namespace. The function returns the namespace into which the
123 attributes have been registered. */
125 scoped_attributes*
126 register_scoped_attributes (const struct attribute_spec * attributes,
127 const char* ns)
129 scoped_attributes *result = NULL;
131 /* See if we already have attributes in the namespace NS. */
132 result = find_attribute_namespace (ns);
134 if (result == NULL)
136 /* We don't have any namespace NS yet. Create one. */
137 scoped_attributes sa;
139 if (!attributes_table.is_empty ())
140 attributes_table.create (64);
142 memset (&sa, 0, sizeof (sa));
143 sa.ns = ns;
144 sa.attributes.create (64);
145 result = attributes_table.safe_push (sa);
146 result->attribute_hash = new hash_table<attribute_hasher> (200);
149 /* Really add the attributes to their namespace now. */
150 for (unsigned i = 0; attributes[i].name != NULL; ++i)
152 result->attributes.safe_push (attributes[i]);
153 register_scoped_attribute (&attributes[i], result);
156 gcc_assert (result != NULL);
158 return result;
161 /* Return the namespace which name is NS, NULL if none exist. */
163 static scoped_attributes*
164 find_attribute_namespace (const char* ns)
166 unsigned ix;
167 scoped_attributes *iter;
169 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
170 if (ns == iter->ns
171 || (iter->ns != NULL
172 && ns != NULL
173 && !strcmp (iter->ns, ns)))
174 return iter;
175 return NULL;
178 /* Initialize attribute tables, and make some sanity checks
179 if --enable-checking. */
181 void
182 init_attributes (void)
184 size_t i;
186 if (attributes_initialized)
187 return;
189 attribute_tables[0] = lang_hooks.common_attribute_table;
190 attribute_tables[1] = lang_hooks.attribute_table;
191 attribute_tables[2] = lang_hooks.format_attribute_table;
192 attribute_tables[3] = targetm.attribute_table;
194 /* Translate NULL pointers to pointers to the empty table. */
195 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
196 if (attribute_tables[i] == NULL)
197 attribute_tables[i] = empty_attribute_table;
199 #ifdef ENABLE_CHECKING
200 /* Make some sanity checks on the attribute tables. */
201 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
203 int j;
205 for (j = 0; attribute_tables[i][j].name != NULL; j++)
207 /* The name must not begin and end with __. */
208 const char *name = attribute_tables[i][j].name;
209 int len = strlen (name);
211 gcc_assert (!(name[0] == '_' && name[1] == '_'
212 && name[len - 1] == '_' && name[len - 2] == '_'));
214 /* The minimum and maximum lengths must be consistent. */
215 gcc_assert (attribute_tables[i][j].min_length >= 0);
217 gcc_assert (attribute_tables[i][j].max_length == -1
218 || (attribute_tables[i][j].max_length
219 >= attribute_tables[i][j].min_length));
221 /* An attribute cannot require both a DECL and a TYPE. */
222 gcc_assert (!attribute_tables[i][j].decl_required
223 || !attribute_tables[i][j].type_required);
225 /* If an attribute requires a function type, in particular
226 it requires a type. */
227 gcc_assert (!attribute_tables[i][j].function_type_required
228 || attribute_tables[i][j].type_required);
232 /* Check that each name occurs just once in each table. */
233 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
235 int j, k;
236 for (j = 0; attribute_tables[i][j].name != NULL; j++)
237 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
238 gcc_assert (strcmp (attribute_tables[i][j].name,
239 attribute_tables[i][k].name));
241 /* Check that no name occurs in more than one table. Names that
242 begin with '*' are exempt, and may be overridden. */
243 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
245 size_t j, k, l;
247 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
248 for (k = 0; attribute_tables[i][k].name != NULL; k++)
249 for (l = 0; attribute_tables[j][l].name != NULL; l++)
250 gcc_assert (attribute_tables[i][k].name[0] == '*'
251 || strcmp (attribute_tables[i][k].name,
252 attribute_tables[j][l].name));
254 #endif
256 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
257 /* Put all the GNU attributes into the "gnu" namespace. */
258 register_scoped_attributes (attribute_tables[i], "gnu");
260 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
261 attributes_initialized = true;
264 /* Insert a single ATTR into the attribute table. */
266 void
267 register_attribute (const struct attribute_spec *attr)
269 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
272 /* Insert a single attribute ATTR into a namespace of attributes. */
274 static void
275 register_scoped_attribute (const struct attribute_spec *attr,
276 scoped_attributes *name_space)
278 struct substring str;
279 attribute_spec **slot;
281 gcc_assert (attr != NULL && name_space != NULL);
283 gcc_assert (name_space->attribute_hash);
285 str.str = attr->name;
286 str.length = strlen (str.str);
288 /* Attribute names in the table must be in the form 'text' and not
289 in the form '__text__'. */
290 gcc_assert (str.length > 0 && str.str[0] != '_');
292 slot = name_space->attribute_hash
293 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
294 INSERT);
295 gcc_assert (!*slot || attr->name[0] == '*');
296 *slot = CONST_CAST (struct attribute_spec *, attr);
299 /* Return the spec for the scoped attribute with namespace NS and
300 name NAME. */
302 static const struct attribute_spec *
303 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
305 struct substring attr;
306 scoped_attributes *attrs;
308 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
310 attrs = find_attribute_namespace (ns_str);
312 if (attrs == NULL)
313 return NULL;
315 attr.str = IDENTIFIER_POINTER (name);
316 attr.length = IDENTIFIER_LENGTH (name);
317 extract_attribute_substring (&attr);
318 return attrs->attribute_hash->find_with_hash (&attr,
319 substring_hash (attr.str,
320 attr.length));
323 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
324 it also specifies the attribute namespace. */
326 const struct attribute_spec *
327 lookup_attribute_spec (const_tree name)
329 tree ns;
330 if (TREE_CODE (name) == TREE_LIST)
332 ns = TREE_PURPOSE (name);
333 name = TREE_VALUE (name);
335 else
336 ns = get_identifier ("gnu");
337 return lookup_scoped_attribute_spec (ns, name);
341 /* Return the namespace of the attribute ATTR. This accessor works on
342 GNU and C++11 (scoped) attributes. On GNU attributes,
343 it returns an identifier tree for the string "gnu".
345 Please read the comments of cxx11_attribute_p to understand the
346 format of attributes. */
348 static tree
349 get_attribute_namespace (const_tree attr)
351 if (cxx11_attribute_p (attr))
352 return TREE_PURPOSE (TREE_PURPOSE (attr));
353 return get_identifier ("gnu");
357 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
358 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
359 it should be modified in place; if a TYPE, a copy should be created
360 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
361 information, in the form of a bitwise OR of flags in enum attribute_flags
362 from tree.h. Depending on these flags, some attributes may be
363 returned to be applied at a later stage (for example, to apply
364 a decl attribute to the declaration rather than to its type). */
366 tree
367 decl_attributes (tree *node, tree attributes, int flags)
369 tree a;
370 tree returned_attrs = NULL_TREE;
372 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
373 return NULL_TREE;
375 if (!attributes_initialized)
376 init_attributes ();
378 /* If this is a function and the user used #pragma GCC optimize, add the
379 options to the attribute((optimize(...))) list. */
380 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
382 tree cur_attr = lookup_attribute ("optimize", attributes);
383 tree opts = copy_list (current_optimize_pragma);
385 if (! cur_attr)
386 attributes
387 = tree_cons (get_identifier ("optimize"), opts, attributes);
388 else
389 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
392 if (TREE_CODE (*node) == FUNCTION_DECL
393 && optimization_current_node != optimization_default_node
394 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
395 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
397 /* If this is a function and the user used #pragma GCC target, add the
398 options to the attribute((target(...))) list. */
399 if (TREE_CODE (*node) == FUNCTION_DECL
400 && current_target_pragma
401 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
402 current_target_pragma, 0))
404 tree cur_attr = lookup_attribute ("target", attributes);
405 tree opts = copy_list (current_target_pragma);
407 if (! cur_attr)
408 attributes = tree_cons (get_identifier ("target"), opts, attributes);
409 else
410 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
413 /* A "naked" function attribute implies "noinline" and "noclone" for
414 those targets that support it. */
415 if (TREE_CODE (*node) == FUNCTION_DECL
416 && attributes
417 && lookup_attribute_spec (get_identifier ("naked"))
418 && lookup_attribute ("naked", attributes) != NULL)
420 if (lookup_attribute ("noinline", attributes) == NULL)
421 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
423 if (lookup_attribute ("noclone", attributes) == NULL)
424 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
427 targetm.insert_attributes (*node, &attributes);
429 for (a = attributes; a; a = TREE_CHAIN (a))
431 tree ns = get_attribute_namespace (a);
432 tree name = get_attribute_name (a);
433 tree args = TREE_VALUE (a);
434 tree *anode = node;
435 const struct attribute_spec *spec =
436 lookup_scoped_attribute_spec (ns, name);
437 bool no_add_attrs = 0;
438 int fn_ptr_quals = 0;
439 tree fn_ptr_tmp = NULL_TREE;
441 if (spec == NULL)
443 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
445 if (ns == NULL_TREE || !cxx11_attribute_p (a))
446 warning (OPT_Wattributes, "%qE attribute directive ignored",
447 name);
448 else
449 warning (OPT_Wattributes,
450 "%<%E::%E%> scoped attribute directive ignored",
451 ns, name);
453 continue;
455 else if (list_length (args) < spec->min_length
456 || (spec->max_length >= 0
457 && list_length (args) > spec->max_length))
459 error ("wrong number of arguments specified for %qE attribute",
460 name);
461 continue;
463 gcc_assert (is_attribute_p (spec->name, name));
465 if (TYPE_P (*node)
466 && cxx11_attribute_p (a)
467 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
469 /* This is a c++11 attribute that appertains to a
470 type-specifier, outside of the definition of, a class
471 type. Ignore it. */
472 warning (OPT_Wattributes, "attribute ignored");
473 inform (input_location,
474 "an attribute that appertains to a type-specifier "
475 "is ignored");
476 continue;
479 if (spec->decl_required && !DECL_P (*anode))
481 if (flags & ((int) ATTR_FLAG_DECL_NEXT
482 | (int) ATTR_FLAG_FUNCTION_NEXT
483 | (int) ATTR_FLAG_ARRAY_NEXT))
485 /* Pass on this attribute to be tried again. */
486 returned_attrs = tree_cons (name, args, returned_attrs);
487 continue;
489 else
491 warning (OPT_Wattributes, "%qE attribute does not apply to types",
492 name);
493 continue;
497 /* If we require a type, but were passed a decl, set up to make a
498 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
499 would have applied if we'd been passed a type, but we cannot modify
500 the decl's type in place here. */
501 if (spec->type_required && DECL_P (*anode))
503 anode = &TREE_TYPE (*anode);
504 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
507 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
508 && TREE_CODE (*anode) != METHOD_TYPE)
510 if (TREE_CODE (*anode) == POINTER_TYPE
511 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
512 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
514 /* OK, this is a bit convoluted. We can't just make a copy
515 of the pointer type and modify its TREE_TYPE, because if
516 we change the attributes of the target type the pointer
517 type needs to have a different TYPE_MAIN_VARIANT. So we
518 pull out the target type now, frob it as appropriate, and
519 rebuild the pointer type later.
521 This would all be simpler if attributes were part of the
522 declarator, grumble grumble. */
523 fn_ptr_tmp = TREE_TYPE (*anode);
524 fn_ptr_quals = TYPE_QUALS (*anode);
525 anode = &fn_ptr_tmp;
526 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
528 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
530 /* Pass on this attribute to be tried again. */
531 returned_attrs = tree_cons (name, args, returned_attrs);
532 continue;
535 if (TREE_CODE (*anode) != FUNCTION_TYPE
536 && TREE_CODE (*anode) != METHOD_TYPE)
538 warning (OPT_Wattributes,
539 "%qE attribute only applies to function types",
540 name);
541 continue;
545 if (TYPE_P (*anode)
546 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
547 && TYPE_SIZE (*anode) != NULL_TREE)
549 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
550 continue;
553 if (spec->handler != NULL)
555 int cxx11_flag =
556 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
558 returned_attrs = chainon ((*spec->handler) (anode, name, args,
559 flags|cxx11_flag,
560 &no_add_attrs),
561 returned_attrs);
564 /* Layout the decl in case anything changed. */
565 if (spec->type_required && DECL_P (*node)
566 && (TREE_CODE (*node) == VAR_DECL
567 || TREE_CODE (*node) == PARM_DECL
568 || TREE_CODE (*node) == RESULT_DECL))
569 relayout_decl (*node);
571 if (!no_add_attrs)
573 tree old_attrs;
574 tree a;
576 if (DECL_P (*anode))
577 old_attrs = DECL_ATTRIBUTES (*anode);
578 else
579 old_attrs = TYPE_ATTRIBUTES (*anode);
581 for (a = lookup_attribute (spec->name, old_attrs);
582 a != NULL_TREE;
583 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
585 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
586 break;
589 if (a == NULL_TREE)
591 /* This attribute isn't already in the list. */
592 if (DECL_P (*anode))
593 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
594 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
596 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
597 /* If this is the main variant, also push the attributes
598 out to the other variants. */
599 if (*anode == TYPE_MAIN_VARIANT (*anode))
601 tree variant;
602 for (variant = *anode; variant;
603 variant = TYPE_NEXT_VARIANT (variant))
605 if (TYPE_ATTRIBUTES (variant) == old_attrs)
606 TYPE_ATTRIBUTES (variant)
607 = TYPE_ATTRIBUTES (*anode);
608 else if (!lookup_attribute
609 (spec->name, TYPE_ATTRIBUTES (variant)))
610 TYPE_ATTRIBUTES (variant) = tree_cons
611 (name, args, TYPE_ATTRIBUTES (variant));
615 else
616 *anode = build_type_attribute_variant (*anode,
617 tree_cons (name, args,
618 old_attrs));
622 if (fn_ptr_tmp)
624 /* Rebuild the function pointer type and put it in the
625 appropriate place. */
626 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
627 if (fn_ptr_quals)
628 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
629 if (DECL_P (*node))
630 TREE_TYPE (*node) = fn_ptr_tmp;
631 else
633 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
634 *node = fn_ptr_tmp;
639 return returned_attrs;
642 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
643 attribute.
645 When G++ parses a C++11 attribute, it is represented as
646 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
647 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
648 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
649 use get_attribute_namespace and get_attribute_name to retrieve the
650 namespace and name of the attribute, as these accessors work with
651 GNU attributes as well. */
653 bool
654 cxx11_attribute_p (const_tree attr)
656 if (attr == NULL_TREE
657 || TREE_CODE (attr) != TREE_LIST)
658 return false;
660 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
663 /* Return the name of the attribute ATTR. This accessor works on GNU
664 and C++11 (scoped) attributes.
666 Please read the comments of cxx11_attribute_p to understand the
667 format of attributes. */
669 tree
670 get_attribute_name (const_tree attr)
672 if (cxx11_attribute_p (attr))
673 return TREE_VALUE (TREE_PURPOSE (attr));
674 return TREE_PURPOSE (attr);
677 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
678 to the method FNDECL. */
680 void
681 apply_tm_attr (tree fndecl, tree attr)
683 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);