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
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
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/>. */
24 #include "coretypes.h"
28 #include "diagnostic-core.h"
33 #include "langhooks.h"
37 /* Table of the tables of attributes (common, language, format, machine)
39 static const struct attribute_spec
*attribute_tables
[4];
41 /* Substring representation. */
49 DEF_VEC_O (attribute_spec
);
50 DEF_VEC_ALLOC_O (attribute_spec
, heap
);
52 /* Scoped attribute name representation. */
54 struct scoped_attributes
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
*,
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. */
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] == '_')
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. */
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. */
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. */
130 register_scoped_attributes (const struct attribute_spec
* attributes
,
133 scoped_attributes
*result
= NULL
;
135 /* See if we already have attributes in the namespace NS. */
136 result
= find_attribute_namespace (ns
);
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
));
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
);
166 /* Return the namespace which name is NS, NULL if none exist. */
168 static scoped_attributes
*
169 find_attribute_namespace (const char* ns
)
172 scoped_attributes
*iter
;
174 FOR_EACH_VEC_ELT (scoped_attributes
, attributes_table
, ix
, iter
)
178 && !strcmp (iter
->ns
, ns
)))
183 /* Initialize attribute tables, and make some sanity checks
184 if --enable-checking. */
187 init_attributes (void)
191 if (attributes_initialized
)
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
++)
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
++)
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
++)
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
));
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. */
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. */
280 register_scoped_attribute (const struct attribute_spec
*attr
,
281 scoped_attributes
*name_space
)
283 struct substring str
;
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
),
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
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
);
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. */
330 const struct attribute_spec
*
331 lookup_attribute_spec (const_tree name
)
333 return lookup_scoped_attribute_spec (get_identifier ("gnu"), 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);