Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / attribs.c
blobb50c73392c3ad1f52d37eb6bff1226e706c454c3
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 "input.h"
26 #include "alias.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "attribs.h"
30 #include "stor-layout.h"
31 #include "flags.h"
32 #include "diagnostic-core.h"
33 #include "tm_p.h"
34 #include "cpplib.h"
35 #include "target.h"
36 #include "langhooks.h"
37 #include "plugin.h"
39 /* Table of the tables of attributes (common, language, format, machine)
40 searched. */
41 static const struct attribute_spec *attribute_tables[4];
43 /* Substring representation. */
45 struct substring
47 const char *str;
48 int length;
51 /* Simple hash function to avoid need to scan whole string. */
53 static inline hashval_t
54 substring_hash (const char *str, int l)
56 return str[0] + str[l - 1] * 256 + l * 65536;
59 /* Used for attribute_hash. */
61 struct attribute_hasher : typed_noop_remove <attribute_spec>
63 typedef attribute_spec *value_type;
64 typedef substring *compare_type;
65 static inline hashval_t hash (const attribute_spec *);
66 static inline bool equal (const attribute_spec *, const substring *);
69 inline hashval_t
70 attribute_hasher::hash (const attribute_spec *spec)
72 const int l = strlen (spec->name);
73 return substring_hash (spec->name, l);
76 inline bool
77 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
79 return (strncmp (spec->name, str->str, str->length) == 0
80 && !spec->name[str->length]);
83 /* Scoped attribute name representation. */
85 struct scoped_attributes
87 const char *ns;
88 vec<attribute_spec> attributes;
89 hash_table<attribute_hasher> *attribute_hash;
92 /* The table of scope attributes. */
93 static vec<scoped_attributes> attributes_table;
95 static scoped_attributes* find_attribute_namespace (const char*);
96 static void register_scoped_attribute (const struct attribute_spec *,
97 scoped_attributes *);
99 static bool attributes_initialized = false;
101 /* Default empty table of attributes. */
103 static const struct attribute_spec empty_attribute_table[] =
105 { NULL, 0, 0, false, false, false, NULL, false }
108 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
109 To avoid need for copying, we simply return length of the string. */
111 static void
112 extract_attribute_substring (struct substring *str)
114 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
115 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
117 str->length -= 4;
118 str->str += 2;
122 /* Insert an array of attributes ATTRIBUTES into a namespace. This
123 array must be NULL terminated. NS is the name of attribute
124 namespace. The function returns the namespace into which the
125 attributes have been registered. */
127 scoped_attributes*
128 register_scoped_attributes (const struct attribute_spec * attributes,
129 const char* ns)
131 scoped_attributes *result = NULL;
133 /* See if we already have attributes in the namespace NS. */
134 result = find_attribute_namespace (ns);
136 if (result == NULL)
138 /* We don't have any namespace NS yet. Create one. */
139 scoped_attributes sa;
141 if (!attributes_table.is_empty ())
142 attributes_table.create (64);
144 memset (&sa, 0, sizeof (sa));
145 sa.ns = ns;
146 sa.attributes.create (64);
147 result = attributes_table.safe_push (sa);
148 result->attribute_hash = new hash_table<attribute_hasher> (200);
151 /* Really add the attributes to their namespace now. */
152 for (unsigned i = 0; attributes[i].name != NULL; ++i)
154 result->attributes.safe_push (attributes[i]);
155 register_scoped_attribute (&attributes[i], result);
158 gcc_assert (result != NULL);
160 return result;
163 /* Return the namespace which name is NS, NULL if none exist. */
165 static scoped_attributes*
166 find_attribute_namespace (const char* ns)
168 unsigned ix;
169 scoped_attributes *iter;
171 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
172 if (ns == iter->ns
173 || (iter->ns != NULL
174 && ns != NULL
175 && !strcmp (iter->ns, ns)))
176 return iter;
177 return NULL;
180 /* Initialize attribute tables, and make some sanity checks
181 if --enable-checking. */
183 void
184 init_attributes (void)
186 size_t i;
188 if (attributes_initialized)
189 return;
191 attribute_tables[0] = lang_hooks.common_attribute_table;
192 attribute_tables[1] = lang_hooks.attribute_table;
193 attribute_tables[2] = lang_hooks.format_attribute_table;
194 attribute_tables[3] = targetm.attribute_table;
196 /* Translate NULL pointers to pointers to the empty table. */
197 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
198 if (attribute_tables[i] == NULL)
199 attribute_tables[i] = empty_attribute_table;
201 #ifdef ENABLE_CHECKING
202 /* Make some sanity checks on the attribute tables. */
203 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
205 int j;
207 for (j = 0; attribute_tables[i][j].name != NULL; j++)
209 /* The name must not begin and end with __. */
210 const char *name = attribute_tables[i][j].name;
211 int len = strlen (name);
213 gcc_assert (!(name[0] == '_' && name[1] == '_'
214 && name[len - 1] == '_' && name[len - 2] == '_'));
216 /* The minimum and maximum lengths must be consistent. */
217 gcc_assert (attribute_tables[i][j].min_length >= 0);
219 gcc_assert (attribute_tables[i][j].max_length == -1
220 || (attribute_tables[i][j].max_length
221 >= attribute_tables[i][j].min_length));
223 /* An attribute cannot require both a DECL and a TYPE. */
224 gcc_assert (!attribute_tables[i][j].decl_required
225 || !attribute_tables[i][j].type_required);
227 /* If an attribute requires a function type, in particular
228 it requires a type. */
229 gcc_assert (!attribute_tables[i][j].function_type_required
230 || attribute_tables[i][j].type_required);
234 /* Check that each name occurs just once in each table. */
235 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
237 int j, k;
238 for (j = 0; attribute_tables[i][j].name != NULL; j++)
239 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
240 gcc_assert (strcmp (attribute_tables[i][j].name,
241 attribute_tables[i][k].name));
243 /* Check that no name occurs in more than one table. Names that
244 begin with '*' are exempt, and may be overridden. */
245 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
247 size_t j, k, l;
249 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
250 for (k = 0; attribute_tables[i][k].name != NULL; k++)
251 for (l = 0; attribute_tables[j][l].name != NULL; l++)
252 gcc_assert (attribute_tables[i][k].name[0] == '*'
253 || strcmp (attribute_tables[i][k].name,
254 attribute_tables[j][l].name));
256 #endif
258 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
259 /* Put all the GNU attributes into the "gnu" namespace. */
260 register_scoped_attributes (attribute_tables[i], "gnu");
262 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
263 attributes_initialized = true;
266 /* Insert a single ATTR into the attribute table. */
268 void
269 register_attribute (const struct attribute_spec *attr)
271 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
274 /* Insert a single attribute ATTR into a namespace of attributes. */
276 static void
277 register_scoped_attribute (const struct attribute_spec *attr,
278 scoped_attributes *name_space)
280 struct substring str;
281 attribute_spec **slot;
283 gcc_assert (attr != NULL && name_space != NULL);
285 gcc_assert (name_space->attribute_hash);
287 str.str = attr->name;
288 str.length = strlen (str.str);
290 /* Attribute names in the table must be in the form 'text' and not
291 in the form '__text__'. */
292 gcc_assert (str.length > 0 && str.str[0] != '_');
294 slot = name_space->attribute_hash
295 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
296 INSERT);
297 gcc_assert (!*slot || attr->name[0] == '*');
298 *slot = CONST_CAST (struct attribute_spec *, attr);
301 /* Return the spec for the scoped attribute with namespace NS and
302 name NAME. */
304 static const struct attribute_spec *
305 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
307 struct substring attr;
308 scoped_attributes *attrs;
310 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
312 attrs = find_attribute_namespace (ns_str);
314 if (attrs == NULL)
315 return NULL;
317 attr.str = IDENTIFIER_POINTER (name);
318 attr.length = IDENTIFIER_LENGTH (name);
319 extract_attribute_substring (&attr);
320 return attrs->attribute_hash->find_with_hash (&attr,
321 substring_hash (attr.str,
322 attr.length));
325 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
326 it also specifies the attribute namespace. */
328 const struct attribute_spec *
329 lookup_attribute_spec (const_tree name)
331 tree ns;
332 if (TREE_CODE (name) == TREE_LIST)
334 ns = TREE_PURPOSE (name);
335 name = TREE_VALUE (name);
337 else
338 ns = get_identifier ("gnu");
339 return lookup_scoped_attribute_spec (ns, name);
343 /* Return the namespace of the attribute ATTR. This accessor works on
344 GNU and C++11 (scoped) attributes. On GNU attributes,
345 it returns an identifier tree for the string "gnu".
347 Please read the comments of cxx11_attribute_p to understand the
348 format of attributes. */
350 static tree
351 get_attribute_namespace (const_tree attr)
353 if (cxx11_attribute_p (attr))
354 return TREE_PURPOSE (TREE_PURPOSE (attr));
355 return get_identifier ("gnu");
359 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
360 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
361 it should be modified in place; if a TYPE, a copy should be created
362 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
363 information, in the form of a bitwise OR of flags in enum attribute_flags
364 from tree.h. Depending on these flags, some attributes may be
365 returned to be applied at a later stage (for example, to apply
366 a decl attribute to the declaration rather than to its type). */
368 tree
369 decl_attributes (tree *node, tree attributes, int flags)
371 tree a;
372 tree returned_attrs = NULL_TREE;
374 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
375 return NULL_TREE;
377 if (!attributes_initialized)
378 init_attributes ();
380 /* If this is a function and the user used #pragma GCC optimize, add the
381 options to the attribute((optimize(...))) list. */
382 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
384 tree cur_attr = lookup_attribute ("optimize", attributes);
385 tree opts = copy_list (current_optimize_pragma);
387 if (! cur_attr)
388 attributes
389 = tree_cons (get_identifier ("optimize"), opts, attributes);
390 else
391 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
394 if (TREE_CODE (*node) == FUNCTION_DECL
395 && optimization_current_node != optimization_default_node
396 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
397 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
399 /* If this is a function and the user used #pragma GCC target, add the
400 options to the attribute((target(...))) list. */
401 if (TREE_CODE (*node) == FUNCTION_DECL
402 && current_target_pragma
403 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
404 current_target_pragma, 0))
406 tree cur_attr = lookup_attribute ("target", attributes);
407 tree opts = copy_list (current_target_pragma);
409 if (! cur_attr)
410 attributes = tree_cons (get_identifier ("target"), opts, attributes);
411 else
412 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
415 /* A "naked" function attribute implies "noinline" and "noclone" for
416 those targets that support it. */
417 if (TREE_CODE (*node) == FUNCTION_DECL
418 && attributes
419 && lookup_attribute_spec (get_identifier ("naked"))
420 && lookup_attribute ("naked", attributes) != NULL)
422 if (lookup_attribute ("noinline", attributes) == NULL)
423 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
425 if (lookup_attribute ("noclone", attributes) == NULL)
426 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
429 targetm.insert_attributes (*node, &attributes);
431 for (a = attributes; a; a = TREE_CHAIN (a))
433 tree ns = get_attribute_namespace (a);
434 tree name = get_attribute_name (a);
435 tree args = TREE_VALUE (a);
436 tree *anode = node;
437 const struct attribute_spec *spec =
438 lookup_scoped_attribute_spec (ns, name);
439 bool no_add_attrs = 0;
440 int fn_ptr_quals = 0;
441 tree fn_ptr_tmp = NULL_TREE;
443 if (spec == NULL)
445 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
447 if (ns == NULL_TREE || !cxx11_attribute_p (a))
448 warning (OPT_Wattributes, "%qE attribute directive ignored",
449 name);
450 else
451 warning (OPT_Wattributes,
452 "%<%E::%E%> scoped attribute directive ignored",
453 ns, name);
455 continue;
457 else if (list_length (args) < spec->min_length
458 || (spec->max_length >= 0
459 && list_length (args) > spec->max_length))
461 error ("wrong number of arguments specified for %qE attribute",
462 name);
463 continue;
465 gcc_assert (is_attribute_p (spec->name, name));
467 if (TYPE_P (*node)
468 && cxx11_attribute_p (a)
469 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
471 /* This is a c++11 attribute that appertains to a
472 type-specifier, outside of the definition of, a class
473 type. Ignore it. */
474 warning (OPT_Wattributes, "attribute ignored");
475 inform (input_location,
476 "an attribute that appertains to a type-specifier "
477 "is ignored");
478 continue;
481 if (spec->decl_required && !DECL_P (*anode))
483 if (flags & ((int) ATTR_FLAG_DECL_NEXT
484 | (int) ATTR_FLAG_FUNCTION_NEXT
485 | (int) ATTR_FLAG_ARRAY_NEXT))
487 /* Pass on this attribute to be tried again. */
488 returned_attrs = tree_cons (name, args, returned_attrs);
489 continue;
491 else
493 warning (OPT_Wattributes, "%qE attribute does not apply to types",
494 name);
495 continue;
499 /* If we require a type, but were passed a decl, set up to make a
500 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
501 would have applied if we'd been passed a type, but we cannot modify
502 the decl's type in place here. */
503 if (spec->type_required && DECL_P (*anode))
505 anode = &TREE_TYPE (*anode);
506 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
509 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
510 && TREE_CODE (*anode) != METHOD_TYPE)
512 if (TREE_CODE (*anode) == POINTER_TYPE
513 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
514 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
516 /* OK, this is a bit convoluted. We can't just make a copy
517 of the pointer type and modify its TREE_TYPE, because if
518 we change the attributes of the target type the pointer
519 type needs to have a different TYPE_MAIN_VARIANT. So we
520 pull out the target type now, frob it as appropriate, and
521 rebuild the pointer type later.
523 This would all be simpler if attributes were part of the
524 declarator, grumble grumble. */
525 fn_ptr_tmp = TREE_TYPE (*anode);
526 fn_ptr_quals = TYPE_QUALS (*anode);
527 anode = &fn_ptr_tmp;
528 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
530 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
532 /* Pass on this attribute to be tried again. */
533 returned_attrs = tree_cons (name, args, returned_attrs);
534 continue;
537 if (TREE_CODE (*anode) != FUNCTION_TYPE
538 && TREE_CODE (*anode) != METHOD_TYPE)
540 warning (OPT_Wattributes,
541 "%qE attribute only applies to function types",
542 name);
543 continue;
547 if (TYPE_P (*anode)
548 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
549 && TYPE_SIZE (*anode) != NULL_TREE)
551 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
552 continue;
555 if (spec->handler != NULL)
557 int cxx11_flag =
558 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
560 returned_attrs = chainon ((*spec->handler) (anode, name, args,
561 flags|cxx11_flag,
562 &no_add_attrs),
563 returned_attrs);
566 /* Layout the decl in case anything changed. */
567 if (spec->type_required && DECL_P (*node)
568 && (TREE_CODE (*node) == VAR_DECL
569 || TREE_CODE (*node) == PARM_DECL
570 || TREE_CODE (*node) == RESULT_DECL))
571 relayout_decl (*node);
573 if (!no_add_attrs)
575 tree old_attrs;
576 tree a;
578 if (DECL_P (*anode))
579 old_attrs = DECL_ATTRIBUTES (*anode);
580 else
581 old_attrs = TYPE_ATTRIBUTES (*anode);
583 for (a = lookup_attribute (spec->name, old_attrs);
584 a != NULL_TREE;
585 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
587 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
588 break;
591 if (a == NULL_TREE)
593 /* This attribute isn't already in the list. */
594 if (DECL_P (*anode))
595 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
596 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
598 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
599 /* If this is the main variant, also push the attributes
600 out to the other variants. */
601 if (*anode == TYPE_MAIN_VARIANT (*anode))
603 tree variant;
604 for (variant = *anode; variant;
605 variant = TYPE_NEXT_VARIANT (variant))
607 if (TYPE_ATTRIBUTES (variant) == old_attrs)
608 TYPE_ATTRIBUTES (variant)
609 = TYPE_ATTRIBUTES (*anode);
610 else if (!lookup_attribute
611 (spec->name, TYPE_ATTRIBUTES (variant)))
612 TYPE_ATTRIBUTES (variant) = tree_cons
613 (name, args, TYPE_ATTRIBUTES (variant));
617 else
618 *anode = build_type_attribute_variant (*anode,
619 tree_cons (name, args,
620 old_attrs));
624 if (fn_ptr_tmp)
626 /* Rebuild the function pointer type and put it in the
627 appropriate place. */
628 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
629 if (fn_ptr_quals)
630 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
631 if (DECL_P (*node))
632 TREE_TYPE (*node) = fn_ptr_tmp;
633 else
635 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
636 *node = fn_ptr_tmp;
641 return returned_attrs;
644 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
645 attribute.
647 When G++ parses a C++11 attribute, it is represented as
648 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
649 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
650 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
651 use get_attribute_namespace and get_attribute_name to retrieve the
652 namespace and name of the attribute, as these accessors work with
653 GNU attributes as well. */
655 bool
656 cxx11_attribute_p (const_tree attr)
658 if (attr == NULL_TREE
659 || TREE_CODE (attr) != TREE_LIST)
660 return false;
662 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
665 /* Return the name of the attribute ATTR. This accessor works on GNU
666 and C++11 (scoped) attributes.
668 Please read the comments of cxx11_attribute_p to understand the
669 format of attributes. */
671 tree
672 get_attribute_name (const_tree attr)
674 if (cxx11_attribute_p (attr))
675 return TREE_VALUE (TREE_PURPOSE (attr));
676 return TREE_PURPOSE (attr);
679 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
680 to the method FNDECL. */
682 void
683 apply_tm_attr (tree fndecl, tree attr)
685 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);