1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2013 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
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
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/>. */
22 #include "coretypes.h"
26 #include "diagnostic-core.h"
31 #include "langhooks.h"
35 /* Table of the tables of attributes (common, language, format, machine)
37 static const struct attribute_spec
*attribute_tables
[4];
39 /* Substring representation. */
47 /* Scoped attribute name representation. */
49 struct scoped_attributes
52 vec
<attribute_spec
> attributes
;
53 htab_t attribute_hash
;
56 /* The table of scope attributes. */
57 static vec
<scoped_attributes
> attributes_table
;
59 static scoped_attributes
* find_attribute_namespace (const char*);
60 static void register_scoped_attribute (const struct attribute_spec
*,
63 static bool attributes_initialized
= false;
65 /* Default empty table of attributes. */
67 static const struct attribute_spec empty_attribute_table
[] =
69 { NULL
, 0, 0, false, false, false, NULL
, false }
72 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
73 To avoid need for copying, we simply return length of the string. */
76 extract_attribute_substring (struct substring
*str
)
78 if (str
->length
> 4 && str
->str
[0] == '_' && str
->str
[1] == '_'
79 && str
->str
[str
->length
- 1] == '_' && str
->str
[str
->length
- 2] == '_')
86 /* Simple hash function to avoid need to scan whole string. */
88 static inline hashval_t
89 substring_hash (const char *str
, int l
)
91 return str
[0] + str
[l
- 1] * 256 + l
* 65536;
94 /* Used for attribute_hash. */
97 hash_attr (const void *p
)
99 const struct attribute_spec
*const spec
= (const struct attribute_spec
*) p
;
100 const int l
= strlen (spec
->name
);
102 return substring_hash (spec
->name
, l
);
105 /* Used for attribute_hash. */
108 eq_attr (const void *p
, const void *q
)
110 const struct attribute_spec
*const spec
= (const struct attribute_spec
*) p
;
111 const struct substring
*const str
= (const struct substring
*) q
;
113 return (!strncmp (spec
->name
, str
->str
, str
->length
) && !spec
->name
[str
->length
]);
116 /* Insert an array of attributes ATTRIBUTES into a namespace. This
117 array must be NULL terminated. NS is the name of attribute
118 namespace. The function returns the namespace into which the
119 attributes have been registered. */
122 register_scoped_attributes (const struct attribute_spec
* attributes
,
125 scoped_attributes
*result
= NULL
;
127 /* See if we already have attributes in the namespace NS. */
128 result
= find_attribute_namespace (ns
);
132 /* We don't have any namespace NS yet. Create one. */
133 scoped_attributes sa
;
135 if (!attributes_table
.is_empty ())
136 attributes_table
.create (64);
138 memset (&sa
, 0, sizeof (sa
));
140 sa
.attributes
.create (64);
141 result
= attributes_table
.safe_push (sa
);
142 result
->attribute_hash
= htab_create (200, hash_attr
, eq_attr
, NULL
);
145 /* Really add the attributes to their namespace now. */
146 for (unsigned i
= 0; attributes
[i
].name
!= NULL
; ++i
)
148 result
->attributes
.safe_push (attributes
[i
]);
149 register_scoped_attribute (&attributes
[i
], result
);
152 gcc_assert (result
!= NULL
);
157 /* Return the namespace which name is NS, NULL if none exist. */
159 static scoped_attributes
*
160 find_attribute_namespace (const char* ns
)
163 scoped_attributes
*iter
;
165 FOR_EACH_VEC_ELT (attributes_table
, ix
, iter
)
169 && !strcmp (iter
->ns
, ns
)))
174 /* Initialize attribute tables, and make some sanity checks
175 if --enable-checking. */
178 init_attributes (void)
182 if (attributes_initialized
)
185 attribute_tables
[0] = lang_hooks
.common_attribute_table
;
186 attribute_tables
[1] = lang_hooks
.attribute_table
;
187 attribute_tables
[2] = lang_hooks
.format_attribute_table
;
188 attribute_tables
[3] = targetm
.attribute_table
;
190 /* Translate NULL pointers to pointers to the empty table. */
191 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
192 if (attribute_tables
[i
] == NULL
)
193 attribute_tables
[i
] = empty_attribute_table
;
195 #ifdef ENABLE_CHECKING
196 /* Make some sanity checks on the attribute tables. */
197 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
201 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
203 /* The name must not begin and end with __. */
204 const char *name
= attribute_tables
[i
][j
].name
;
205 int len
= strlen (name
);
207 gcc_assert (!(name
[0] == '_' && name
[1] == '_'
208 && name
[len
- 1] == '_' && name
[len
- 2] == '_'));
210 /* The minimum and maximum lengths must be consistent. */
211 gcc_assert (attribute_tables
[i
][j
].min_length
>= 0);
213 gcc_assert (attribute_tables
[i
][j
].max_length
== -1
214 || (attribute_tables
[i
][j
].max_length
215 >= attribute_tables
[i
][j
].min_length
));
217 /* An attribute cannot require both a DECL and a TYPE. */
218 gcc_assert (!attribute_tables
[i
][j
].decl_required
219 || !attribute_tables
[i
][j
].type_required
);
221 /* If an attribute requires a function type, in particular
222 it requires a type. */
223 gcc_assert (!attribute_tables
[i
][j
].function_type_required
224 || attribute_tables
[i
][j
].type_required
);
228 /* Check that each name occurs just once in each table. */
229 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
232 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
233 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
234 gcc_assert (strcmp (attribute_tables
[i
][j
].name
,
235 attribute_tables
[i
][k
].name
));
237 /* Check that no name occurs in more than one table. Names that
238 begin with '*' are exempt, and may be overridden. */
239 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
243 for (j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
244 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
245 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
246 gcc_assert (attribute_tables
[i
][k
].name
[0] == '*'
247 || strcmp (attribute_tables
[i
][k
].name
,
248 attribute_tables
[j
][l
].name
));
252 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); ++i
)
253 /* Put all the GNU attributes into the "gnu" namespace. */
254 register_scoped_attributes (attribute_tables
[i
], "gnu");
256 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES
, NULL
);
257 attributes_initialized
= true;
260 /* Insert a single ATTR into the attribute table. */
263 register_attribute (const struct attribute_spec
*attr
)
265 register_scoped_attribute (attr
, find_attribute_namespace ("gnu"));
268 /* Insert a single attribute ATTR into a namespace of attributes. */
271 register_scoped_attribute (const struct attribute_spec
*attr
,
272 scoped_attributes
*name_space
)
274 struct substring str
;
277 gcc_assert (attr
!= NULL
&& name_space
!= NULL
);
279 gcc_assert (name_space
->attribute_hash
!= NULL
);
281 str
.str
= attr
->name
;
282 str
.length
= strlen (str
.str
);
284 /* Attribute names in the table must be in the form 'text' and not
285 in the form '__text__'. */
286 gcc_assert (str
.length
> 0 && str
.str
[0] != '_');
288 slot
= htab_find_slot_with_hash (name_space
->attribute_hash
, &str
,
289 substring_hash (str
.str
, str
.length
),
291 gcc_assert (!*slot
|| attr
->name
[0] == '*');
292 *slot
= (void *) CONST_CAST (struct attribute_spec
*, attr
);
295 /* Return the spec for the scoped attribute with namespace NS and
298 const struct attribute_spec
*
299 lookup_scoped_attribute_spec (const_tree ns
, const_tree name
)
301 struct substring attr
;
302 scoped_attributes
*attrs
;
304 const char *ns_str
= (ns
!= NULL_TREE
) ? IDENTIFIER_POINTER (ns
): NULL
;
306 attrs
= find_attribute_namespace (ns_str
);
311 attr
.str
= IDENTIFIER_POINTER (name
);
312 attr
.length
= IDENTIFIER_LENGTH (name
);
313 extract_attribute_substring (&attr
);
314 return (const struct attribute_spec
*)
315 htab_find_with_hash (attrs
->attribute_hash
, &attr
,
316 substring_hash (attr
.str
, attr
.length
));
319 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
320 it also specifies the attribute namespace. */
322 const struct attribute_spec
*
323 lookup_attribute_spec (const_tree name
)
326 if (TREE_CODE (name
) == TREE_LIST
)
328 ns
= TREE_PURPOSE (name
);
329 name
= TREE_VALUE (name
);
332 ns
= get_identifier ("gnu");
333 return lookup_scoped_attribute_spec (ns
, name
);
337 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
338 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
339 it should be modified in place; if a TYPE, a copy should be created
340 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
341 information, in the form of a bitwise OR of flags in enum attribute_flags
342 from tree.h. Depending on these flags, some attributes may be
343 returned to be applied at a later stage (for example, to apply
344 a decl attribute to the declaration rather than to its type). */
347 decl_attributes (tree
*node
, tree attributes
, int flags
)
350 tree returned_attrs
= NULL_TREE
;
352 if (TREE_TYPE (*node
) == error_mark_node
|| attributes
== error_mark_node
)
355 if (!attributes_initialized
)
358 /* If this is a function and the user used #pragma GCC optimize, add the
359 options to the attribute((optimize(...))) list. */
360 if (TREE_CODE (*node
) == FUNCTION_DECL
&& current_optimize_pragma
)
362 tree cur_attr
= lookup_attribute ("optimize", attributes
);
363 tree opts
= copy_list (current_optimize_pragma
);
367 = tree_cons (get_identifier ("optimize"), opts
, attributes
);
369 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
372 if (TREE_CODE (*node
) == FUNCTION_DECL
373 && optimization_current_node
!= optimization_default_node
374 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
))
375 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
) = optimization_current_node
;
377 /* If this is a function and the user used #pragma GCC target, add the
378 options to the attribute((target(...))) list. */
379 if (TREE_CODE (*node
) == FUNCTION_DECL
380 && current_target_pragma
381 && targetm
.target_option
.valid_attribute_p (*node
, NULL_TREE
,
382 current_target_pragma
, 0))
384 tree cur_attr
= lookup_attribute ("target", attributes
);
385 tree opts
= copy_list (current_target_pragma
);
388 attributes
= tree_cons (get_identifier ("target"), opts
, attributes
);
390 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
393 /* A "naked" function attribute implies "noinline" and "noclone" for
394 those targets that support it. */
395 if (TREE_CODE (*node
) == FUNCTION_DECL
397 && lookup_attribute_spec (get_identifier ("naked"))
398 && lookup_attribute ("naked", attributes
) != NULL
)
400 if (lookup_attribute ("noinline", attributes
) == NULL
)
401 attributes
= tree_cons (get_identifier ("noinline"), NULL
, attributes
);
403 if (lookup_attribute ("noclone", attributes
) == NULL
)
404 attributes
= tree_cons (get_identifier ("noclone"), NULL
, attributes
);
407 targetm
.insert_attributes (*node
, &attributes
);
409 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
411 tree ns
= get_attribute_namespace (a
);
412 tree name
= get_attribute_name (a
);
413 tree args
= TREE_VALUE (a
);
415 const struct attribute_spec
*spec
=
416 lookup_scoped_attribute_spec (ns
, name
);
417 bool no_add_attrs
= 0;
418 int fn_ptr_quals
= 0;
419 tree fn_ptr_tmp
= NULL_TREE
;
423 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
425 if (ns
== NULL_TREE
|| !cxx11_attribute_p (a
))
426 warning (OPT_Wattributes
, "%qE attribute directive ignored",
429 warning (OPT_Wattributes
,
430 "%<%E::%E%> scoped attribute directive ignored",
435 else if (list_length (args
) < spec
->min_length
436 || (spec
->max_length
>= 0
437 && list_length (args
) > spec
->max_length
))
439 error ("wrong number of arguments specified for %qE attribute",
443 gcc_assert (is_attribute_p (spec
->name
, name
));
446 && cxx11_attribute_p (a
)
447 && !(flags
& ATTR_FLAG_TYPE_IN_PLACE
))
449 /* This is a c++11 attribute that appertains to a
450 type-specifier, outside of the definition of, a class
452 warning (OPT_Wattributes
, "attribute ignored");
453 inform (input_location
,
454 "an attribute that appertains to a type-specifier "
459 if (spec
->decl_required
&& !DECL_P (*anode
))
461 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
462 | (int) ATTR_FLAG_FUNCTION_NEXT
463 | (int) ATTR_FLAG_ARRAY_NEXT
))
465 /* Pass on this attribute to be tried again. */
466 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
471 warning (OPT_Wattributes
, "%qE attribute does not apply to types",
477 /* If we require a type, but were passed a decl, set up to make a
478 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
479 would have applied if we'd been passed a type, but we cannot modify
480 the decl's type in place here. */
481 if (spec
->type_required
&& DECL_P (*anode
))
483 anode
= &TREE_TYPE (*anode
);
484 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
485 if (!(TREE_CODE (*anode
) == TYPE_DECL
486 && *anode
== TYPE_NAME (TYPE_MAIN_VARIANT
487 (TREE_TYPE (*anode
)))))
488 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
491 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
492 && TREE_CODE (*anode
) != METHOD_TYPE
)
494 if (TREE_CODE (*anode
) == POINTER_TYPE
495 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
496 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
498 /* OK, this is a bit convoluted. We can't just make a copy
499 of the pointer type and modify its TREE_TYPE, because if
500 we change the attributes of the target type the pointer
501 type needs to have a different TYPE_MAIN_VARIANT. So we
502 pull out the target type now, frob it as appropriate, and
503 rebuild the pointer type later.
505 This would all be simpler if attributes were part of the
506 declarator, grumble grumble. */
507 fn_ptr_tmp
= TREE_TYPE (*anode
);
508 fn_ptr_quals
= TYPE_QUALS (*anode
);
510 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
512 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
514 /* Pass on this attribute to be tried again. */
515 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
519 if (TREE_CODE (*anode
) != FUNCTION_TYPE
520 && TREE_CODE (*anode
) != METHOD_TYPE
)
522 warning (OPT_Wattributes
,
523 "%qE attribute only applies to function types",
530 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
531 && TYPE_SIZE (*anode
) != NULL_TREE
)
533 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
537 if (spec
->handler
!= NULL
)
540 cxx11_attribute_p (a
) ? ATTR_FLAG_CXX11
: 0;
542 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
548 /* Layout the decl in case anything changed. */
549 if (spec
->type_required
&& DECL_P (*node
)
550 && (TREE_CODE (*node
) == VAR_DECL
551 || TREE_CODE (*node
) == PARM_DECL
552 || TREE_CODE (*node
) == RESULT_DECL
))
553 relayout_decl (*node
);
561 old_attrs
= DECL_ATTRIBUTES (*anode
);
563 old_attrs
= TYPE_ATTRIBUTES (*anode
);
565 for (a
= lookup_attribute (spec
->name
, old_attrs
);
567 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
569 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
575 /* This attribute isn't already in the list. */
577 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
578 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
580 TYPE_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
581 /* If this is the main variant, also push the attributes
582 out to the other variants. */
583 if (*anode
== TYPE_MAIN_VARIANT (*anode
))
586 for (variant
= *anode
; variant
;
587 variant
= TYPE_NEXT_VARIANT (variant
))
589 if (TYPE_ATTRIBUTES (variant
) == old_attrs
)
590 TYPE_ATTRIBUTES (variant
)
591 = TYPE_ATTRIBUTES (*anode
);
592 else if (!lookup_attribute
593 (spec
->name
, TYPE_ATTRIBUTES (variant
)))
594 TYPE_ATTRIBUTES (variant
) = tree_cons
595 (name
, args
, TYPE_ATTRIBUTES (variant
));
600 *anode
= build_type_attribute_variant (*anode
,
601 tree_cons (name
, args
,
608 /* Rebuild the function pointer type and put it in the
609 appropriate place. */
610 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
612 fn_ptr_tmp
= build_qualified_type (fn_ptr_tmp
, fn_ptr_quals
);
614 TREE_TYPE (*node
) = fn_ptr_tmp
;
617 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
623 return returned_attrs
;
626 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
629 When G++ parses a C++11 attribute, it is represented as
630 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
631 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
632 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
633 use get_attribute_namespace and get_attribute_name to retrieve the
634 namespace and name of the attribute, as these accessors work with
635 GNU attributes as well. */
638 cxx11_attribute_p (const_tree attr
)
640 if (attr
== NULL_TREE
641 || TREE_CODE (attr
) != TREE_LIST
)
644 return (TREE_CODE (TREE_PURPOSE (attr
)) == TREE_LIST
);
647 /* Return the name of the attribute ATTR. This accessor works on GNU
648 and C++11 (scoped) attributes.
650 Please read the comments of cxx11_attribute_p to understand the
651 format of attributes. */
654 get_attribute_name (const_tree attr
)
656 if (cxx11_attribute_p (attr
))
657 return TREE_VALUE (TREE_PURPOSE (attr
));
658 return TREE_PURPOSE (attr
);
661 /* Return the namespace of the attribute ATTR. This accessor works on
662 GNU and C++11 (scoped) attributes. On GNU attributes,
663 it returns an identifier tree for the string "gnu".
665 Please read the comments of cxx11_attribute_p to understand the
666 format of attributes. */
669 get_attribute_namespace (const_tree attr
)
671 if (cxx11_attribute_p (attr
))
672 return TREE_PURPOSE (TREE_PURPOSE (attr
));
673 return get_identifier ("gnu");
676 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
677 to the method FNDECL. */
680 apply_tm_attr (tree fndecl
, tree attr
)
682 decl_attributes (&TREE_TYPE (fndecl
), tree_cons (attr
, NULL
, NULL
), 0);