1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2019 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"
25 #include "stringpool.h"
26 #include "diagnostic-core.h"
28 #include "stor-layout.h"
29 #include "langhooks.h"
33 #include "diagnostic.h"
34 #include "pretty-print.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 /* Simple hash function to avoid need to scan whole string. */
51 static inline hashval_t
52 substring_hash (const char *str
, int l
)
54 return str
[0] + str
[l
- 1] * 256 + l
* 65536;
57 /* Used for attribute_hash. */
59 struct attribute_hasher
: nofree_ptr_hash
<attribute_spec
>
61 typedef substring
*compare_type
;
62 static inline hashval_t
hash (const attribute_spec
*);
63 static inline bool equal (const attribute_spec
*, const substring
*);
67 attribute_hasher::hash (const attribute_spec
*spec
)
69 const int l
= strlen (spec
->name
);
70 return substring_hash (spec
->name
, l
);
74 attribute_hasher::equal (const attribute_spec
*spec
, const substring
*str
)
76 return (strncmp (spec
->name
, str
->str
, str
->length
) == 0
77 && !spec
->name
[str
->length
]);
80 /* Scoped attribute name representation. */
82 struct scoped_attributes
85 vec
<attribute_spec
> attributes
;
86 hash_table
<attribute_hasher
> *attribute_hash
;
89 /* The table of scope attributes. */
90 static vec
<scoped_attributes
> attributes_table
;
92 static scoped_attributes
* find_attribute_namespace (const char*);
93 static void register_scoped_attribute (const struct attribute_spec
*,
96 static bool attributes_initialized
= false;
98 /* Default empty table of attributes. */
100 static const struct attribute_spec empty_attribute_table
[] =
102 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
105 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
106 To avoid need for copying, we simply return length of the string. */
109 extract_attribute_substring (struct substring
*str
)
111 if (str
->length
> 4 && str
->str
[0] == '_' && str
->str
[1] == '_'
112 && str
->str
[str
->length
- 1] == '_' && str
->str
[str
->length
- 2] == '_')
119 /* Insert an array of attributes ATTRIBUTES into a namespace. This
120 array must be NULL terminated. NS is the name of attribute
121 namespace. The function returns the namespace into which the
122 attributes have been registered. */
125 register_scoped_attributes (const struct attribute_spec
*attributes
,
128 scoped_attributes
*result
= NULL
;
130 /* See if we already have attributes in the namespace NS. */
131 result
= find_attribute_namespace (ns
);
135 /* We don't have any namespace NS yet. Create one. */
136 scoped_attributes sa
;
138 if (attributes_table
.is_empty ())
139 attributes_table
.create (64);
141 memset (&sa
, 0, sizeof (sa
));
143 sa
.attributes
.create (64);
144 result
= attributes_table
.safe_push (sa
);
145 result
->attribute_hash
= new hash_table
<attribute_hasher
> (200);
148 /* Really add the attributes to their namespace now. */
149 for (unsigned i
= 0; attributes
[i
].name
!= NULL
; ++i
)
151 result
->attributes
.safe_push (attributes
[i
]);
152 register_scoped_attribute (&attributes
[i
], result
);
155 gcc_assert (result
!= NULL
);
160 /* Return the namespace which name is NS, NULL if none exist. */
162 static scoped_attributes
*
163 find_attribute_namespace (const char* ns
)
166 scoped_attributes
*iter
;
168 FOR_EACH_VEC_ELT (attributes_table
, ix
, iter
)
172 && !strcmp (iter
->ns
, ns
)))
177 /* Make some sanity checks on the attribute tables. */
180 check_attribute_tables (void)
182 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
183 for (size_t j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
185 /* The name must not begin and end with __. */
186 const char *name
= attribute_tables
[i
][j
].name
;
187 int len
= strlen (name
);
189 gcc_assert (!(name
[0] == '_' && name
[1] == '_'
190 && name
[len
- 1] == '_' && name
[len
- 2] == '_'));
192 /* The minimum and maximum lengths must be consistent. */
193 gcc_assert (attribute_tables
[i
][j
].min_length
>= 0);
195 gcc_assert (attribute_tables
[i
][j
].max_length
== -1
196 || (attribute_tables
[i
][j
].max_length
197 >= attribute_tables
[i
][j
].min_length
));
199 /* An attribute cannot require both a DECL and a TYPE. */
200 gcc_assert (!attribute_tables
[i
][j
].decl_required
201 || !attribute_tables
[i
][j
].type_required
);
203 /* If an attribute requires a function type, in particular
204 it requires a type. */
205 gcc_assert (!attribute_tables
[i
][j
].function_type_required
206 || attribute_tables
[i
][j
].type_required
);
209 /* Check that each name occurs just once in each table. */
210 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
211 for (size_t j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
212 for (size_t k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
213 gcc_assert (strcmp (attribute_tables
[i
][j
].name
,
214 attribute_tables
[i
][k
].name
));
216 /* Check that no name occurs in more than one table. Names that
217 begin with '*' are exempt, and may be overridden. */
218 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
219 for (size_t j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
220 for (size_t k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
221 for (size_t l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
222 gcc_assert (attribute_tables
[i
][k
].name
[0] == '*'
223 || strcmp (attribute_tables
[i
][k
].name
,
224 attribute_tables
[j
][l
].name
));
227 /* Initialize attribute tables, and make some sanity checks if checking is
231 init_attributes (void)
235 if (attributes_initialized
)
238 attribute_tables
[0] = lang_hooks
.common_attribute_table
;
239 attribute_tables
[1] = lang_hooks
.attribute_table
;
240 attribute_tables
[2] = lang_hooks
.format_attribute_table
;
241 attribute_tables
[3] = targetm
.attribute_table
;
243 /* Translate NULL pointers to pointers to the empty table. */
244 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
245 if (attribute_tables
[i
] == NULL
)
246 attribute_tables
[i
] = empty_attribute_table
;
249 check_attribute_tables ();
251 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); ++i
)
252 /* Put all the GNU attributes into the "gnu" namespace. */
253 register_scoped_attributes (attribute_tables
[i
], "gnu");
255 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES
, NULL
);
256 attributes_initialized
= true;
259 /* Insert a single ATTR into the attribute table. */
262 register_attribute (const struct attribute_spec
*attr
)
264 register_scoped_attribute (attr
, find_attribute_namespace ("gnu"));
267 /* Insert a single attribute ATTR into a namespace of attributes. */
270 register_scoped_attribute (const struct attribute_spec
*attr
,
271 scoped_attributes
*name_space
)
273 struct substring str
;
274 attribute_spec
**slot
;
276 gcc_assert (attr
!= NULL
&& name_space
!= NULL
);
278 gcc_assert (name_space
->attribute_hash
);
280 str
.str
= attr
->name
;
281 str
.length
= strlen (str
.str
);
283 /* Attribute names in the table must be in the form 'text' and not
284 in the form '__text__'. */
285 gcc_assert (str
.length
> 0 && str
.str
[0] != '_');
287 slot
= name_space
->attribute_hash
288 ->find_slot_with_hash (&str
, substring_hash (str
.str
, str
.length
),
290 gcc_assert (!*slot
|| attr
->name
[0] == '*');
291 *slot
= CONST_CAST (struct attribute_spec
*, attr
);
294 /* Return the spec for the scoped attribute with namespace NS and
297 static const struct attribute_spec
*
298 lookup_scoped_attribute_spec (const_tree ns
, const_tree name
)
300 struct substring attr
;
301 scoped_attributes
*attrs
;
303 const char *ns_str
= (ns
!= NULL_TREE
) ? IDENTIFIER_POINTER (ns
): NULL
;
305 attrs
= find_attribute_namespace (ns_str
);
310 attr
.str
= IDENTIFIER_POINTER (name
);
311 attr
.length
= IDENTIFIER_LENGTH (name
);
312 extract_attribute_substring (&attr
);
313 return attrs
->attribute_hash
->find_with_hash (&attr
,
314 substring_hash (attr
.str
,
318 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
319 it also specifies the attribute namespace. */
321 const struct attribute_spec
*
322 lookup_attribute_spec (const_tree name
)
325 if (TREE_CODE (name
) == TREE_LIST
)
327 ns
= TREE_PURPOSE (name
);
328 name
= TREE_VALUE (name
);
331 ns
= get_identifier ("gnu");
332 return lookup_scoped_attribute_spec (ns
, name
);
336 /* Return the namespace of the attribute ATTR. This accessor works on
337 GNU and C++11 (scoped) attributes. On GNU attributes,
338 it returns an identifier tree for the string "gnu".
340 Please read the comments of cxx11_attribute_p to understand the
341 format of attributes. */
344 get_attribute_namespace (const_tree attr
)
346 if (cxx11_attribute_p (attr
))
347 return TREE_PURPOSE (TREE_PURPOSE (attr
));
348 return get_identifier ("gnu");
351 /* Check LAST_DECL and NODE of the same symbol for attributes that are
352 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
353 them, and return true if any have been found. NODE can be a DECL
357 diag_attr_exclusions (tree last_decl
, tree node
, tree attrname
,
358 const attribute_spec
*spec
)
360 const attribute_spec::exclusions
*excl
= spec
->exclude
;
362 tree_code code
= TREE_CODE (node
);
364 if ((code
== FUNCTION_DECL
&& !excl
->function
365 && (!excl
->type
|| !spec
->affects_type_identity
))
366 || (code
== VAR_DECL
&& !excl
->variable
367 && (!excl
->type
|| !spec
->affects_type_identity
))
368 || (((code
== TYPE_DECL
|| RECORD_OR_UNION_TYPE_P (node
)) && !excl
->type
)))
371 /* True if an attribute that's mutually exclusive with ATTRNAME
375 if (last_decl
&& last_decl
!= node
&& TREE_TYPE (last_decl
) != node
)
377 /* Check both the last DECL and its type for conflicts with
378 the attribute being added to the current decl or type. */
379 found
|= diag_attr_exclusions (last_decl
, last_decl
, attrname
, spec
);
380 tree decl_type
= TREE_TYPE (last_decl
);
381 found
|= diag_attr_exclusions (last_decl
, decl_type
, attrname
, spec
);
384 /* NODE is either the current DECL to which the attribute is being
385 applied or its TYPE. For the former, consider the attributes on
386 both the DECL and its type. */
391 attrs
[0] = DECL_ATTRIBUTES (node
);
392 attrs
[1] = TYPE_ATTRIBUTES (TREE_TYPE (node
));
396 attrs
[0] = TYPE_ATTRIBUTES (node
);
397 attrs
[1] = NULL_TREE
;
400 /* Iterate over the mutually exclusive attribute names and verify
401 that the symbol doesn't contain it. */
402 for (unsigned i
= 0; i
!= sizeof attrs
/ sizeof *attrs
; ++i
)
407 for ( ; excl
->name
; ++excl
)
409 /* Avoid checking the attribute against itself. */
410 if (is_attribute_p (excl
->name
, attrname
))
413 if (!lookup_attribute (excl
->name
, attrs
[i
]))
416 /* An exclusion may apply either to a function declaration,
417 type declaration, or a field/variable declaration, or
418 any subset of the three. */
419 if (TREE_CODE (node
) == FUNCTION_DECL
423 if (TREE_CODE (node
) == TYPE_DECL
427 if ((TREE_CODE (node
) == FIELD_DECL
428 || TREE_CODE (node
) == VAR_DECL
)
435 bool note
= last_decl
!= NULL_TREE
;
436 auto_diagnostic_group d
;
437 if (TREE_CODE (node
) == FUNCTION_DECL
438 && fndecl_built_in_p (node
))
439 note
&= warning (OPT_Wattributes
,
440 "ignoring attribute %qE in declaration of "
441 "a built-in function %qD because it conflicts "
442 "with attribute %qs",
443 attrname
, node
, excl
->name
);
445 note
&= warning (OPT_Wattributes
,
446 "ignoring attribute %qE because "
447 "it conflicts with attribute %qs",
448 attrname
, excl
->name
);
451 inform (DECL_SOURCE_LOCATION (last_decl
),
452 "previous declaration here");
459 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
460 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
461 it should be modified in place; if a TYPE, a copy should be created
462 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
463 information, in the form of a bitwise OR of flags in enum attribute_flags
464 from tree.h. Depending on these flags, some attributes may be
465 returned to be applied at a later stage (for example, to apply
466 a decl attribute to the declaration rather than to its type). */
469 decl_attributes (tree
*node
, tree attributes
, int flags
,
470 tree last_decl
/* = NULL_TREE */)
472 tree returned_attrs
= NULL_TREE
;
474 if (TREE_TYPE (*node
) == error_mark_node
|| attributes
== error_mark_node
)
477 if (!attributes_initialized
)
480 /* If this is a function and the user used #pragma GCC optimize, add the
481 options to the attribute((optimize(...))) list. */
482 if (TREE_CODE (*node
) == FUNCTION_DECL
&& current_optimize_pragma
)
484 tree cur_attr
= lookup_attribute ("optimize", attributes
);
485 tree opts
= copy_list (current_optimize_pragma
);
489 = tree_cons (get_identifier ("optimize"), opts
, attributes
);
491 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
494 if (TREE_CODE (*node
) == FUNCTION_DECL
495 && optimization_current_node
!= optimization_default_node
496 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
))
497 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
) = optimization_current_node
;
499 /* If this is a function and the user used #pragma GCC target, add the
500 options to the attribute((target(...))) list. */
501 if (TREE_CODE (*node
) == FUNCTION_DECL
502 && current_target_pragma
503 && targetm
.target_option
.valid_attribute_p (*node
, NULL_TREE
,
504 current_target_pragma
, 0))
506 tree cur_attr
= lookup_attribute ("target", attributes
);
507 tree opts
= copy_list (current_target_pragma
);
510 attributes
= tree_cons (get_identifier ("target"), opts
, attributes
);
512 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
515 /* A "naked" function attribute implies "noinline" and "noclone" for
516 those targets that support it. */
517 if (TREE_CODE (*node
) == FUNCTION_DECL
519 && lookup_attribute ("naked", attributes
) != NULL
520 && lookup_attribute_spec (get_identifier ("naked")))
522 if (lookup_attribute ("noinline", attributes
) == NULL
)
523 attributes
= tree_cons (get_identifier ("noinline"), NULL
, attributes
);
525 if (lookup_attribute ("noclone", attributes
) == NULL
)
526 attributes
= tree_cons (get_identifier ("noclone"), NULL
, attributes
);
529 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
530 for those targets that support it. */
531 if (TREE_CODE (*node
) == FUNCTION_DECL
533 && lookup_attribute ("noipa", attributes
) != NULL
534 && lookup_attribute_spec (get_identifier ("noipa")))
536 if (lookup_attribute ("noinline", attributes
) == NULL
)
537 attributes
= tree_cons (get_identifier ("noinline"), NULL
, attributes
);
539 if (lookup_attribute ("noclone", attributes
) == NULL
)
540 attributes
= tree_cons (get_identifier ("noclone"), NULL
, attributes
);
542 if (lookup_attribute ("no_icf", attributes
) == NULL
)
543 attributes
= tree_cons (get_identifier ("no_icf"), NULL
, attributes
);
546 targetm
.insert_attributes (*node
, &attributes
);
548 /* Note that attributes on the same declaration are not necessarily
549 in the same order as in the source. */
550 for (tree attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
552 tree ns
= get_attribute_namespace (attr
);
553 tree name
= get_attribute_name (attr
);
554 tree args
= TREE_VALUE (attr
);
556 const struct attribute_spec
*spec
557 = lookup_scoped_attribute_spec (ns
, name
);
558 int fn_ptr_quals
= 0;
559 tree fn_ptr_tmp
= NULL_TREE
;
560 const bool cxx11_attr_p
= cxx11_attribute_p (attr
);
564 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
566 if (ns
== NULL_TREE
|| !cxx11_attr_p
)
567 warning (OPT_Wattributes
, "%qE attribute directive ignored",
570 warning (OPT_Wattributes
,
571 "%<%E::%E%> scoped attribute directive ignored",
578 int nargs
= list_length (args
);
579 if (nargs
< spec
->min_length
580 || (spec
->max_length
>= 0
581 && nargs
> spec
->max_length
))
583 error ("wrong number of arguments specified for %qE attribute",
585 if (spec
->max_length
< 0)
586 inform (input_location
, "expected %i or more, found %i",
587 spec
->min_length
, nargs
);
589 inform (input_location
, "expected between %i and %i, found %i",
590 spec
->min_length
, spec
->max_length
, nargs
);
594 gcc_assert (is_attribute_p (spec
->name
, name
));
596 if (spec
->decl_required
&& !DECL_P (*anode
))
598 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
599 | (int) ATTR_FLAG_FUNCTION_NEXT
600 | (int) ATTR_FLAG_ARRAY_NEXT
))
602 /* Pass on this attribute to be tried again. */
603 tree attr
= tree_cons (name
, args
, NULL_TREE
);
604 returned_attrs
= chainon (returned_attrs
, attr
);
609 warning (OPT_Wattributes
, "%qE attribute does not apply to types",
615 /* If we require a type, but were passed a decl, set up to make a
616 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
617 would have applied if we'd been passed a type, but we cannot modify
618 the decl's type in place here. */
619 if (spec
->type_required
&& DECL_P (*anode
))
621 anode
= &TREE_TYPE (*anode
);
622 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
625 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
626 && TREE_CODE (*anode
) != METHOD_TYPE
)
628 if (TREE_CODE (*anode
) == POINTER_TYPE
629 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
630 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
632 /* OK, this is a bit convoluted. We can't just make a copy
633 of the pointer type and modify its TREE_TYPE, because if
634 we change the attributes of the target type the pointer
635 type needs to have a different TYPE_MAIN_VARIANT. So we
636 pull out the target type now, frob it as appropriate, and
637 rebuild the pointer type later.
639 This would all be simpler if attributes were part of the
640 declarator, grumble grumble. */
641 fn_ptr_tmp
= TREE_TYPE (*anode
);
642 fn_ptr_quals
= TYPE_QUALS (*anode
);
644 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
646 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
648 /* Pass on this attribute to be tried again. */
649 tree attr
= tree_cons (name
, args
, NULL_TREE
);
650 returned_attrs
= chainon (returned_attrs
, attr
);
654 if (TREE_CODE (*anode
) != FUNCTION_TYPE
655 && TREE_CODE (*anode
) != METHOD_TYPE
)
657 warning (OPT_Wattributes
,
658 "%qE attribute only applies to function types",
665 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
666 && TYPE_SIZE (*anode
) != NULL_TREE
)
668 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
672 bool no_add_attrs
= false;
674 /* Check for exclusions with other attributes on the current
675 declation as well as the last declaration of the same
676 symbol already processed (if one exists). Detect and
677 reject incompatible attributes. */
678 bool built_in
= flags
& ATTR_FLAG_BUILT_IN
;
680 && (flag_checking
|| !built_in
))
682 /* Always check attributes on user-defined functions.
683 Check them on built-ins only when -fchecking is set.
684 Ignore __builtin_unreachable -- it's both const and
689 || DECL_BUILT_IN_CLASS (*anode
) != BUILT_IN_NORMAL
690 || (DECL_FUNCTION_CODE (*anode
) != BUILT_IN_UNREACHABLE
691 && (DECL_FUNCTION_CODE (*anode
)
692 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE
)))
694 bool no_add
= diag_attr_exclusions (last_decl
, *anode
, name
, spec
);
695 if (!no_add
&& anode
!= node
)
696 no_add
= diag_attr_exclusions (last_decl
, *node
, name
, spec
);
697 no_add_attrs
|= no_add
;
704 if (spec
->handler
!= NULL
)
706 int cxx11_flag
= (cxx11_attr_p
? ATTR_FLAG_CXX11
: 0);
708 /* Pass in an array of the current declaration followed
709 by the last pushed/merged declaration if one exists.
710 If the handler changes CUR_AND_LAST_DECL[0] replace
711 *ANODE with its value. */
712 tree cur_and_last_decl
[] = { *anode
, last_decl
};
713 tree ret
= (spec
->handler
) (cur_and_last_decl
, name
, args
,
714 flags
|cxx11_flag
, &no_add_attrs
);
716 *anode
= cur_and_last_decl
[0];
717 if (ret
== error_mark_node
)
719 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
723 returned_attrs
= chainon (ret
, returned_attrs
);
726 /* Layout the decl in case anything changed. */
727 if (spec
->type_required
&& DECL_P (*node
)
729 || TREE_CODE (*node
) == PARM_DECL
730 || TREE_CODE (*node
) == RESULT_DECL
))
731 relayout_decl (*node
);
739 old_attrs
= DECL_ATTRIBUTES (*anode
);
741 old_attrs
= TYPE_ATTRIBUTES (*anode
);
743 for (a
= lookup_attribute (spec
->name
, old_attrs
);
745 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
747 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
753 /* This attribute isn't already in the list. */
755 /* Preserve the C++11 form. */
757 r
= tree_cons (build_tree_list (ns
, name
), args
, old_attrs
);
759 r
= tree_cons (name
, args
, old_attrs
);
762 DECL_ATTRIBUTES (*anode
) = r
;
763 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
765 TYPE_ATTRIBUTES (*anode
) = r
;
766 /* If this is the main variant, also push the attributes
767 out to the other variants. */
768 if (*anode
== TYPE_MAIN_VARIANT (*anode
))
770 for (tree variant
= *anode
; variant
;
771 variant
= TYPE_NEXT_VARIANT (variant
))
773 if (TYPE_ATTRIBUTES (variant
) == old_attrs
)
774 TYPE_ATTRIBUTES (variant
)
775 = TYPE_ATTRIBUTES (*anode
);
776 else if (!lookup_attribute
777 (spec
->name
, TYPE_ATTRIBUTES (variant
)))
778 TYPE_ATTRIBUTES (variant
) = tree_cons
779 (name
, args
, TYPE_ATTRIBUTES (variant
));
784 *anode
= build_type_attribute_variant (*anode
, r
);
790 /* Rebuild the function pointer type and put it in the
791 appropriate place. */
792 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
794 fn_ptr_tmp
= build_qualified_type (fn_ptr_tmp
, fn_ptr_quals
);
796 TREE_TYPE (*node
) = fn_ptr_tmp
;
799 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
805 return returned_attrs
;
808 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
811 When G++ parses a C++11 attribute, it is represented as
812 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
813 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
814 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
815 use get_attribute_namespace and get_attribute_name to retrieve the
816 namespace and name of the attribute, as these accessors work with
817 GNU attributes as well. */
820 cxx11_attribute_p (const_tree attr
)
822 if (attr
== NULL_TREE
823 || TREE_CODE (attr
) != TREE_LIST
)
826 return (TREE_CODE (TREE_PURPOSE (attr
)) == TREE_LIST
);
829 /* Return the name of the attribute ATTR. This accessor works on GNU
830 and C++11 (scoped) attributes.
832 Please read the comments of cxx11_attribute_p to understand the
833 format of attributes. */
836 get_attribute_name (const_tree attr
)
838 if (cxx11_attribute_p (attr
))
839 return TREE_VALUE (TREE_PURPOSE (attr
));
840 return TREE_PURPOSE (attr
);
843 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
844 to the method FNDECL. */
847 apply_tm_attr (tree fndecl
, tree attr
)
849 decl_attributes (&TREE_TYPE (fndecl
), tree_cons (attr
, NULL
, NULL
), 0);
852 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
856 make_attribute (const char *name
, const char *arg_name
, tree chain
)
863 attr_name
= get_identifier (name
);
864 attr_arg_name
= build_string (strlen (arg_name
), arg_name
);
865 attr_args
= tree_cons (NULL_TREE
, attr_arg_name
, NULL_TREE
);
866 attr
= tree_cons (attr_name
, attr_args
, chain
);
871 /* Common functions used for target clone support. */
873 /* Comparator function to be used in qsort routine to sort attribute
874 specification strings to "target". */
877 attr_strcmp (const void *v1
, const void *v2
)
879 const char *c1
= *(char *const*)v1
;
880 const char *c2
= *(char *const*)v2
;
881 return strcmp (c1
, c2
);
884 /* ARGLIST is the argument to target attribute. This function tokenizes
885 the comma separated arguments, sorts them and returns a string which
886 is a unique identifier for the comma separated arguments. It also
887 replaces non-identifier characters "=,-" with "_". */
890 sorted_attr_string (tree arglist
)
893 size_t str_len_sum
= 0;
895 char *attr_str
, *ret_str
;
897 unsigned int argnum
= 1;
900 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
902 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
903 size_t len
= strlen (str
);
904 str_len_sum
+= len
+ 1;
907 for (i
= 0; i
< strlen (str
); i
++)
912 attr_str
= XNEWVEC (char, str_len_sum
);
914 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
916 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
917 size_t len
= strlen (str
);
918 memcpy (attr_str
+ str_len_sum
, str
, len
);
919 attr_str
[str_len_sum
+ len
] = TREE_CHAIN (arg
) ? ',' : '\0';
920 str_len_sum
+= len
+ 1;
923 /* Replace "=,-" with "_". */
924 for (i
= 0; i
< strlen (attr_str
); i
++)
925 if (attr_str
[i
] == '=' || attr_str
[i
]== '-')
931 args
= XNEWVEC (char *, argnum
);
934 attr
= strtok (attr_str
, ",");
939 attr
= strtok (NULL
, ",");
942 qsort (args
, argnum
, sizeof (char *), attr_strcmp
);
944 ret_str
= XNEWVEC (char, str_len_sum
);
946 for (i
= 0; i
< argnum
; i
++)
948 size_t len
= strlen (args
[i
]);
949 memcpy (ret_str
+ str_len_sum
, args
[i
], len
);
950 ret_str
[str_len_sum
+ len
] = i
< argnum
- 1 ? '_' : '\0';
951 str_len_sum
+= len
+ 1;
955 XDELETEVEC (attr_str
);
960 /* This function returns true if FN1 and FN2 are versions of the same function,
961 that is, the target strings of the function decls are different. This assumes
962 that FN1 and FN2 have the same signature. */
965 common_function_versions (tree fn1
, tree fn2
)
968 char *target1
, *target2
;
971 if (TREE_CODE (fn1
) != FUNCTION_DECL
972 || TREE_CODE (fn2
) != FUNCTION_DECL
)
975 attr1
= lookup_attribute ("target", DECL_ATTRIBUTES (fn1
));
976 attr2
= lookup_attribute ("target", DECL_ATTRIBUTES (fn2
));
978 /* At least one function decl should have the target attribute specified. */
979 if (attr1
== NULL_TREE
&& attr2
== NULL_TREE
)
982 /* Diagnose missing target attribute if one of the decls is already
984 if (attr1
== NULL_TREE
|| attr2
== NULL_TREE
)
986 if (DECL_FUNCTION_VERSIONED (fn1
) || DECL_FUNCTION_VERSIONED (fn2
))
988 if (attr2
!= NULL_TREE
)
990 std::swap (fn1
, fn2
);
993 error_at (DECL_SOURCE_LOCATION (fn2
),
994 "missing %<target%> attribute for multi-versioned %qD",
996 inform (DECL_SOURCE_LOCATION (fn1
),
997 "previous declaration of %qD", fn1
);
998 /* Prevent diagnosing of the same error multiple times. */
999 DECL_ATTRIBUTES (fn2
)
1000 = tree_cons (get_identifier ("target"),
1001 copy_node (TREE_VALUE (attr1
)),
1002 DECL_ATTRIBUTES (fn2
));
1007 target1
= sorted_attr_string (TREE_VALUE (attr1
));
1008 target2
= sorted_attr_string (TREE_VALUE (attr2
));
1010 /* The sorted target strings must be different for fn1 and fn2
1012 if (strcmp (target1
, target2
) == 0)
1017 XDELETEVEC (target1
);
1018 XDELETEVEC (target2
);
1023 /* Return a new name by appending SUFFIX to the DECL name. If make_unique
1024 is true, append the full path name of the source file. */
1027 make_unique_name (tree decl
, const char *suffix
, bool make_unique
)
1029 char *global_var_name
;
1032 const char *unique_name
= NULL
;
1034 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
1036 /* Get a unique name that can be used globally without any chances
1037 of collision at link time. */
1039 unique_name
= IDENTIFIER_POINTER (get_file_function_name ("\0"));
1041 name_len
= strlen (name
) + strlen (suffix
) + 2;
1044 name_len
+= strlen (unique_name
) + 1;
1045 global_var_name
= XNEWVEC (char, name_len
);
1047 /* Use '.' to concatenate names as it is demangler friendly. */
1049 snprintf (global_var_name
, name_len
, "%s.%s.%s", name
, unique_name
,
1052 snprintf (global_var_name
, name_len
, "%s.%s", name
, suffix
);
1054 return global_var_name
;
1057 /* Make a dispatcher declaration for the multi-versioned function DECL.
1058 Calls to DECL function will be replaced with calls to the dispatcher
1059 by the front-end. Return the decl created. */
1062 make_dispatcher_decl (const tree decl
)
1066 tree fn_type
, func_type
;
1068 func_name
= xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
1070 fn_type
= TREE_TYPE (decl
);
1071 func_type
= build_function_type (TREE_TYPE (fn_type
),
1072 TYPE_ARG_TYPES (fn_type
));
1074 func_decl
= build_fn_decl (func_name
, func_type
);
1075 XDELETEVEC (func_name
);
1076 TREE_USED (func_decl
) = 1;
1077 DECL_CONTEXT (func_decl
) = NULL_TREE
;
1078 DECL_INITIAL (func_decl
) = error_mark_node
;
1079 DECL_ARTIFICIAL (func_decl
) = 1;
1080 /* Mark this func as external, the resolver will flip it again if
1081 it gets generated. */
1082 DECL_EXTERNAL (func_decl
) = 1;
1083 /* This will be of type IFUNCs have to be externally visible. */
1084 TREE_PUBLIC (func_decl
) = 1;
1089 /* Returns true if decl is multi-versioned and DECL is the default function,
1090 that is it is not tagged with target specific optimization. */
1093 is_function_default_version (const tree decl
)
1095 if (TREE_CODE (decl
) != FUNCTION_DECL
1096 || !DECL_FUNCTION_VERSIONED (decl
))
1098 tree attr
= lookup_attribute ("target", DECL_ATTRIBUTES (decl
));
1100 attr
= TREE_VALUE (TREE_VALUE (attr
));
1101 return (TREE_CODE (attr
) == STRING_CST
1102 && strcmp (TREE_STRING_POINTER (attr
), "default") == 0);
1105 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1109 build_decl_attribute_variant (tree ddecl
, tree attribute
)
1111 DECL_ATTRIBUTES (ddecl
) = attribute
;
1115 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1116 is ATTRIBUTE and its qualifiers are QUALS.
1118 Record such modified types already made so we don't make duplicates. */
1121 build_type_attribute_qual_variant (tree otype
, tree attribute
, int quals
)
1124 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype
), attribute
))
1128 /* Building a distinct copy of a tagged type is inappropriate; it
1129 causes breakage in code that expects there to be a one-to-one
1130 relationship between a struct and its fields.
1131 build_duplicate_type is another solution (as used in
1132 handle_transparent_union_attribute), but that doesn't play well
1133 with the stronger C++ type identity model. */
1134 if (TREE_CODE (ttype
) == RECORD_TYPE
1135 || TREE_CODE (ttype
) == UNION_TYPE
1136 || TREE_CODE (ttype
) == QUAL_UNION_TYPE
1137 || TREE_CODE (ttype
) == ENUMERAL_TYPE
)
1139 warning (OPT_Wattributes
,
1140 "ignoring attributes applied to %qT after definition",
1141 TYPE_MAIN_VARIANT (ttype
));
1142 return build_qualified_type (ttype
, quals
);
1145 ttype
= build_qualified_type (ttype
, TYPE_UNQUALIFIED
);
1146 if (lang_hooks
.types
.copy_lang_qualifiers
1147 && otype
!= TYPE_MAIN_VARIANT (otype
))
1148 ttype
= (lang_hooks
.types
.copy_lang_qualifiers
1149 (ttype
, TYPE_MAIN_VARIANT (otype
)));
1151 tree dtype
= ntype
= build_distinct_type_copy (ttype
);
1153 TYPE_ATTRIBUTES (ntype
) = attribute
;
1155 hashval_t hash
= type_hash_canon_hash (ntype
);
1156 ntype
= type_hash_canon (hash
, ntype
);
1159 /* This variant was already in the hash table, don't mess with
1161 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype
)
1162 || !comp_type_attributes (ntype
, ttype
))
1163 /* If the target-dependent attributes make NTYPE different from
1164 its canonical type, we will need to use structural equality
1165 checks for this type.
1167 We shouldn't get here for stripping attributes from a type;
1168 the no-attribute type might not need structural comparison. But
1169 we can if was discarded from type_hash_table. */
1170 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
1171 else if (TYPE_CANONICAL (ntype
) == ntype
)
1172 TYPE_CANONICAL (ntype
) = TYPE_CANONICAL (ttype
);
1174 ttype
= build_qualified_type (ntype
, quals
);
1175 if (lang_hooks
.types
.copy_lang_qualifiers
1176 && otype
!= TYPE_MAIN_VARIANT (otype
))
1177 ttype
= lang_hooks
.types
.copy_lang_qualifiers (ttype
, otype
);
1179 else if (TYPE_QUALS (ttype
) != quals
)
1180 ttype
= build_qualified_type (ttype
, quals
);
1185 /* Compare two identifier nodes representing attributes.
1186 Return true if they are the same, false otherwise. */
1189 cmp_attrib_identifiers (const_tree attr1
, const_tree attr2
)
1191 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1192 gcc_checking_assert (TREE_CODE (attr1
) == IDENTIFIER_NODE
1193 && TREE_CODE (attr2
) == IDENTIFIER_NODE
);
1195 /* Identifiers can be compared directly for equality. */
1199 return cmp_attribs (IDENTIFIER_POINTER (attr1
), IDENTIFIER_LENGTH (attr1
),
1200 IDENTIFIER_POINTER (attr2
), IDENTIFIER_LENGTH (attr2
));
1203 /* Compare two constructor-element-type constants. Return 1 if the lists
1204 are known to be equal; otherwise return 0. */
1207 simple_cst_list_equal (const_tree l1
, const_tree l2
)
1209 while (l1
!= NULL_TREE
&& l2
!= NULL_TREE
)
1211 if (simple_cst_equal (TREE_VALUE (l1
), TREE_VALUE (l2
)) != 1)
1214 l1
= TREE_CHAIN (l1
);
1215 l2
= TREE_CHAIN (l2
);
1221 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1225 omp_declare_simd_clauses_equal (tree clauses1
, tree clauses2
)
1228 for (cl1
= clauses1
, cl2
= clauses2
;
1230 cl1
= OMP_CLAUSE_CHAIN (cl1
), cl2
= OMP_CLAUSE_CHAIN (cl2
))
1232 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_CODE (cl2
))
1234 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_SIMDLEN
)
1236 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1
),
1237 OMP_CLAUSE_DECL (cl2
)) != 1)
1240 switch (OMP_CLAUSE_CODE (cl1
))
1242 case OMP_CLAUSE_ALIGNED
:
1243 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1
),
1244 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2
)) != 1)
1247 case OMP_CLAUSE_LINEAR
:
1248 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1
),
1249 OMP_CLAUSE_LINEAR_STEP (cl2
)) != 1)
1252 case OMP_CLAUSE_SIMDLEN
:
1253 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1
),
1254 OMP_CLAUSE_SIMDLEN_EXPR (cl2
)) != 1)
1264 /* Compare two attributes for their value identity. Return true if the
1265 attribute values are known to be equal; otherwise return false. */
1268 attribute_value_equal (const_tree attr1
, const_tree attr2
)
1270 if (TREE_VALUE (attr1
) == TREE_VALUE (attr2
))
1273 if (TREE_VALUE (attr1
) != NULL_TREE
1274 && TREE_CODE (TREE_VALUE (attr1
)) == TREE_LIST
1275 && TREE_VALUE (attr2
) != NULL_TREE
1276 && TREE_CODE (TREE_VALUE (attr2
)) == TREE_LIST
)
1278 /* Handle attribute format. */
1279 if (is_attribute_p ("format", get_attribute_name (attr1
)))
1281 attr1
= TREE_VALUE (attr1
);
1282 attr2
= TREE_VALUE (attr2
);
1283 /* Compare the archetypes (printf/scanf/strftime/...). */
1284 if (!cmp_attrib_identifiers (TREE_VALUE (attr1
), TREE_VALUE (attr2
)))
1286 /* Archetypes are the same. Compare the rest. */
1287 return (simple_cst_list_equal (TREE_CHAIN (attr1
),
1288 TREE_CHAIN (attr2
)) == 1);
1290 return (simple_cst_list_equal (TREE_VALUE (attr1
),
1291 TREE_VALUE (attr2
)) == 1);
1294 if (TREE_VALUE (attr1
)
1295 && TREE_CODE (TREE_VALUE (attr1
)) == OMP_CLAUSE
1296 && TREE_VALUE (attr2
)
1297 && TREE_CODE (TREE_VALUE (attr2
)) == OMP_CLAUSE
)
1298 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1
),
1299 TREE_VALUE (attr2
));
1301 return (simple_cst_equal (TREE_VALUE (attr1
), TREE_VALUE (attr2
)) == 1);
1304 /* Return 0 if the attributes for two types are incompatible, 1 if they
1305 are compatible, and 2 if they are nearly compatible (which causes a
1306 warning to be generated). */
1308 comp_type_attributes (const_tree type1
, const_tree type2
)
1310 const_tree a1
= TYPE_ATTRIBUTES (type1
);
1311 const_tree a2
= TYPE_ATTRIBUTES (type2
);
1316 for (a
= a1
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1318 const struct attribute_spec
*as
;
1321 as
= lookup_attribute_spec (get_attribute_name (a
));
1322 if (!as
|| as
->affects_type_identity
== false)
1325 attr
= lookup_attribute (as
->name
, CONST_CAST_TREE (a2
));
1326 if (!attr
|| !attribute_value_equal (a
, attr
))
1331 for (a
= a2
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1333 const struct attribute_spec
*as
;
1335 as
= lookup_attribute_spec (get_attribute_name (a
));
1336 if (!as
|| as
->affects_type_identity
== false)
1339 if (!lookup_attribute (as
->name
, CONST_CAST_TREE (a1
)))
1341 /* We don't need to compare trees again, as we did this
1342 already in first loop. */
1344 /* All types - affecting identity - are equal, so
1345 there is no need to call target hook for comparison. */
1349 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a
)))
1351 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1
)) != NULL
)
1352 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2
)) != NULL
))
1354 /* As some type combinations - like default calling-convention - might
1355 be compatible, we have to call the target hook to get the final result. */
1356 return targetm
.comp_type_attributes (type1
, type2
);
1359 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1362 Record such modified types already made so we don't make duplicates. */
1365 build_type_attribute_variant (tree ttype
, tree attribute
)
1367 return build_type_attribute_qual_variant (ttype
, attribute
,
1368 TYPE_QUALS (ttype
));
1371 /* A variant of lookup_attribute() that can be used with an identifier
1372 as the first argument, and where the identifier can be either
1373 'text' or '__text__'.
1375 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1376 return a pointer to the attribute's list element if the attribute
1377 is part of the list, or NULL_TREE if not found. If the attribute
1378 appears more than once, this only returns the first occurrence; the
1379 TREE_CHAIN of the return value should be passed back in if further
1380 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1381 can be in the form 'text' or '__text__'. */
1383 lookup_ident_attribute (tree attr_identifier
, tree list
)
1385 gcc_checking_assert (TREE_CODE (attr_identifier
) == IDENTIFIER_NODE
);
1389 gcc_checking_assert (TREE_CODE (get_attribute_name (list
))
1390 == IDENTIFIER_NODE
);
1392 if (cmp_attrib_identifiers (attr_identifier
,
1393 get_attribute_name (list
)))
1396 list
= TREE_CHAIN (list
);
1402 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1406 remove_attribute (const char *attr_name
, tree list
)
1409 gcc_checking_assert (attr_name
[0] != '_');
1411 for (p
= &list
; *p
;)
1415 tree attr
= get_attribute_name (l
);
1416 if (is_attribute_p (attr_name
, attr
))
1417 *p
= TREE_CHAIN (l
);
1419 p
= &TREE_CHAIN (l
);
1425 /* Return an attribute list that is the union of a1 and a2. */
1428 merge_attributes (tree a1
, tree a2
)
1432 /* Either one unset? Take the set one. */
1434 if ((attributes
= a1
) == 0)
1437 /* One that completely contains the other? Take it. */
1439 else if (a2
!= 0 && ! attribute_list_contained (a1
, a2
))
1441 if (attribute_list_contained (a2
, a1
))
1445 /* Pick the longest list, and hang on the other list. */
1447 if (list_length (a1
) < list_length (a2
))
1448 attributes
= a2
, a2
= a1
;
1450 for (; a2
!= 0; a2
= TREE_CHAIN (a2
))
1453 for (a
= lookup_ident_attribute (get_attribute_name (a2
),
1455 a
!= NULL_TREE
&& !attribute_value_equal (a
, a2
);
1456 a
= lookup_ident_attribute (get_attribute_name (a2
),
1461 a1
= copy_node (a2
);
1462 TREE_CHAIN (a1
) = attributes
;
1471 /* Given types T1 and T2, merge their attributes and return
1475 merge_type_attributes (tree t1
, tree t2
)
1477 return merge_attributes (TYPE_ATTRIBUTES (t1
),
1478 TYPE_ATTRIBUTES (t2
));
1481 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1485 merge_decl_attributes (tree olddecl
, tree newdecl
)
1487 return merge_attributes (DECL_ATTRIBUTES (olddecl
),
1488 DECL_ATTRIBUTES (newdecl
));
1491 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1492 they are missing there. */
1495 duplicate_one_attribute (tree
*attrs
, tree attr
, const char *name
)
1497 attr
= lookup_attribute (name
, attr
);
1500 tree a
= lookup_attribute (name
, *attrs
);
1504 for (a2
= a
; a2
; a2
= lookup_attribute (name
, TREE_CHAIN (a2
)))
1505 if (attribute_value_equal (attr
, a2
))
1509 a2
= copy_node (attr
);
1510 TREE_CHAIN (a2
) = *attrs
;
1513 attr
= lookup_attribute (name
, TREE_CHAIN (attr
));
1517 /* Duplicate all attributes from user DECL to the corresponding
1518 builtin that should be propagated. */
1521 copy_attributes_to_builtin (tree decl
)
1523 tree b
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
1525 duplicate_one_attribute (&DECL_ATTRIBUTES (b
),
1526 DECL_ATTRIBUTES (decl
), "omp declare simd");
1529 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1531 /* Specialization of merge_decl_attributes for various Windows targets.
1533 This handles the following situation:
1535 __declspec (dllimport) int foo;
1538 The second instance of `foo' nullifies the dllimport. */
1541 merge_dllimport_decl_attributes (tree old
, tree new_tree
)
1544 int delete_dllimport_p
= 1;
1546 /* What we need to do here is remove from `old' dllimport if it doesn't
1547 appear in `new'. dllimport behaves like extern: if a declaration is
1548 marked dllimport and a definition appears later, then the object
1549 is not dllimport'd. We also remove a `new' dllimport if the old list
1550 contains dllexport: dllexport always overrides dllimport, regardless
1551 of the order of declaration. */
1552 if (!VAR_OR_FUNCTION_DECL_P (new_tree
))
1553 delete_dllimport_p
= 0;
1554 else if (DECL_DLLIMPORT_P (new_tree
)
1555 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old
)))
1557 DECL_DLLIMPORT_P (new_tree
) = 0;
1558 warning (OPT_Wattributes
, "%q+D already declared with dllexport "
1559 "attribute: dllimport ignored", new_tree
);
1561 else if (DECL_DLLIMPORT_P (old
) && !DECL_DLLIMPORT_P (new_tree
))
1563 /* Warn about overriding a symbol that has already been used, e.g.:
1564 extern int __attribute__ ((dllimport)) foo;
1565 int* bar () {return &foo;}
1568 if (TREE_USED (old
))
1570 warning (0, "%q+D redeclared without dllimport attribute "
1571 "after being referenced with dll linkage", new_tree
);
1572 /* If we have used a variable's address with dllimport linkage,
1573 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1574 decl may already have had TREE_CONSTANT computed.
1575 We still remove the attribute so that assembler code refers
1576 to '&foo rather than '_imp__foo'. */
1577 if (VAR_P (old
) && TREE_ADDRESSABLE (old
))
1578 DECL_DLLIMPORT_P (new_tree
) = 1;
1581 /* Let an inline definition silently override the external reference,
1582 but otherwise warn about attribute inconsistency. */
1583 else if (VAR_P (new_tree
) || !DECL_DECLARED_INLINE_P (new_tree
))
1584 warning (OPT_Wattributes
, "%q+D redeclared without dllimport "
1585 "attribute: previous dllimport ignored", new_tree
);
1588 delete_dllimport_p
= 0;
1590 a
= merge_attributes (DECL_ATTRIBUTES (old
), DECL_ATTRIBUTES (new_tree
));
1592 if (delete_dllimport_p
)
1593 a
= remove_attribute ("dllimport", a
);
1598 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1599 struct attribute_spec.handler. */
1602 handle_dll_attribute (tree
* pnode
, tree name
, tree args
, int flags
,
1608 /* These attributes may apply to structure and union types being created,
1609 but otherwise should pass to the declaration involved. */
1612 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
| (int) ATTR_FLAG_FUNCTION_NEXT
1613 | (int) ATTR_FLAG_ARRAY_NEXT
))
1615 *no_add_attrs
= true;
1616 return tree_cons (name
, args
, NULL_TREE
);
1618 if (TREE_CODE (node
) == RECORD_TYPE
1619 || TREE_CODE (node
) == UNION_TYPE
)
1621 node
= TYPE_NAME (node
);
1627 warning (OPT_Wattributes
, "%qE attribute ignored",
1629 *no_add_attrs
= true;
1634 if (!VAR_OR_FUNCTION_DECL_P (node
) && TREE_CODE (node
) != TYPE_DECL
)
1636 *no_add_attrs
= true;
1637 warning (OPT_Wattributes
, "%qE attribute ignored",
1642 if (TREE_CODE (node
) == TYPE_DECL
1643 && TREE_CODE (TREE_TYPE (node
)) != RECORD_TYPE
1644 && TREE_CODE (TREE_TYPE (node
)) != UNION_TYPE
)
1646 *no_add_attrs
= true;
1647 warning (OPT_Wattributes
, "%qE attribute ignored",
1652 is_dllimport
= is_attribute_p ("dllimport", name
);
1654 /* Report error on dllimport ambiguities seen now before they cause
1658 /* Honor any target-specific overrides. */
1659 if (!targetm
.valid_dllimport_attribute_p (node
))
1660 *no_add_attrs
= true;
1662 else if (TREE_CODE (node
) == FUNCTION_DECL
1663 && DECL_DECLARED_INLINE_P (node
))
1665 warning (OPT_Wattributes
, "inline function %q+D declared as "
1666 "dllimport: attribute ignored", node
);
1667 *no_add_attrs
= true;
1669 /* Like MS, treat definition of dllimported variables and
1670 non-inlined functions on declaration as syntax errors. */
1671 else if (TREE_CODE (node
) == FUNCTION_DECL
&& DECL_INITIAL (node
))
1673 error ("function %q+D definition is marked dllimport", node
);
1674 *no_add_attrs
= true;
1677 else if (VAR_P (node
))
1679 if (DECL_INITIAL (node
))
1681 error ("variable %q+D definition is marked dllimport",
1683 *no_add_attrs
= true;
1686 /* `extern' needn't be specified with dllimport.
1687 Specify `extern' now and hope for the best. Sigh. */
1688 DECL_EXTERNAL (node
) = 1;
1689 /* Also, implicitly give dllimport'd variables declared within
1690 a function global scope, unless declared static. */
1691 if (current_function_decl
!= NULL_TREE
&& !TREE_STATIC (node
))
1692 TREE_PUBLIC (node
) = 1;
1693 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1694 it is a C++ static data member. */
1695 if (DECL_CONTEXT (node
) == NULL_TREE
1696 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node
)))
1697 TREE_STATIC (node
) = 0;
1700 if (*no_add_attrs
== false)
1701 DECL_DLLIMPORT_P (node
) = 1;
1703 else if (TREE_CODE (node
) == FUNCTION_DECL
1704 && DECL_DECLARED_INLINE_P (node
)
1705 && flag_keep_inline_dllexport
)
1706 /* An exported function, even if inline, must be emitted. */
1707 DECL_EXTERNAL (node
) = 0;
1709 /* Report error if symbol is not accessible at global scope. */
1710 if (!TREE_PUBLIC (node
) && VAR_OR_FUNCTION_DECL_P (node
))
1712 error ("external linkage required for symbol %q+D because of "
1713 "%qE attribute", node
, name
);
1714 *no_add_attrs
= true;
1717 /* A dllexport'd entity must have default visibility so that other
1718 program units (shared libraries or the main executable) can see
1719 it. A dllimport'd entity must have default visibility so that
1720 the linker knows that undefined references within this program
1721 unit can be resolved by the dynamic linker. */
1724 if (DECL_VISIBILITY_SPECIFIED (node
)
1725 && DECL_VISIBILITY (node
) != VISIBILITY_DEFAULT
)
1726 error ("%qE implies default visibility, but %qD has already "
1727 "been declared with a different visibility",
1729 DECL_VISIBILITY (node
) = VISIBILITY_DEFAULT
;
1730 DECL_VISIBILITY_SPECIFIED (node
) = 1;
1736 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1738 /* Given two lists of attributes, return true if list l2 is
1739 equivalent to l1. */
1742 attribute_list_equal (const_tree l1
, const_tree l2
)
1747 return attribute_list_contained (l1
, l2
)
1748 && attribute_list_contained (l2
, l1
);
1751 /* Given two lists of attributes, return true if list L2 is
1752 completely contained within L1. */
1753 /* ??? This would be faster if attribute names were stored in a canonicalized
1754 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1755 must be used to show these elements are equivalent (which they are). */
1756 /* ??? It's not clear that attributes with arguments will always be handled
1760 attribute_list_contained (const_tree l1
, const_tree l2
)
1764 /* First check the obvious, maybe the lists are identical. */
1768 /* Maybe the lists are similar. */
1769 for (t1
= l1
, t2
= l2
;
1771 && get_attribute_name (t1
) == get_attribute_name (t2
)
1772 && TREE_VALUE (t1
) == TREE_VALUE (t2
);
1773 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1776 /* Maybe the lists are equal. */
1777 if (t1
== 0 && t2
== 0)
1780 for (; t2
!= 0; t2
= TREE_CHAIN (t2
))
1783 /* This CONST_CAST is okay because lookup_attribute does not
1784 modify its argument and the return value is assigned to a
1786 for (attr
= lookup_ident_attribute (get_attribute_name (t2
),
1787 CONST_CAST_TREE (l1
));
1788 attr
!= NULL_TREE
&& !attribute_value_equal (t2
, attr
);
1789 attr
= lookup_ident_attribute (get_attribute_name (t2
),
1793 if (attr
== NULL_TREE
)
1800 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
1801 of ATTR_NAME, and LIST is not NULL_TREE.
1803 The function is called from lookup_attribute in order to optimize
1807 private_lookup_attribute (const char *attr_name
, size_t attr_len
, tree list
)
1811 tree attr
= get_attribute_name (list
);
1812 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
1813 if (cmp_attribs (attr_name
, attr_len
, IDENTIFIER_POINTER (attr
),
1816 list
= TREE_CHAIN (list
);
1822 /* Return true if the function decl or type NODE has been declared
1823 with attribute ANAME among attributes ATTRS. */
1826 has_attribute (tree node
, tree attrs
, const char *aname
)
1828 if (!strcmp (aname
, "const"))
1830 if (DECL_P (node
) && TREE_READONLY (node
))
1833 else if (!strcmp (aname
, "malloc"))
1835 if (DECL_P (node
) && DECL_IS_MALLOC (node
))
1838 else if (!strcmp (aname
, "noreturn"))
1840 if (DECL_P (node
) && TREE_THIS_VOLATILE (node
))
1843 else if (!strcmp (aname
, "nothrow"))
1845 if (TREE_NOTHROW (node
))
1848 else if (!strcmp (aname
, "pure"))
1850 if (DECL_P (node
) && DECL_PURE_P (node
))
1854 return lookup_attribute (aname
, attrs
);
1857 /* Return the number of mismatched function or type attributes between
1858 the "template" function declaration TMPL and DECL. The word "template"
1859 doesn't necessarily refer to a C++ template but rather a declaration
1860 whose attributes should be matched by those on DECL. For a non-zero
1861 return value set *ATTRSTR to a string representation of the list of
1862 mismatched attributes with quoted names.
1863 ATTRLIST is a list of additional attributes that SPEC should be
1864 taken to ultimately be declared with. */
1867 decls_mismatched_attributes (tree tmpl
, tree decl
, tree attrlist
,
1868 const char* const blacklist
[],
1869 pretty_printer
*attrstr
)
1871 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
1874 /* Avoid warning if either declaration or its type is deprecated. */
1875 if (TREE_DEPRECATED (tmpl
)
1876 || TREE_DEPRECATED (decl
))
1879 const tree tmpls
[] = { tmpl
, TREE_TYPE (tmpl
) };
1880 const tree decls
[] = { decl
, TREE_TYPE (decl
) };
1882 if (TREE_DEPRECATED (tmpls
[1])
1883 || TREE_DEPRECATED (decls
[1])
1884 || TREE_DEPRECATED (TREE_TYPE (tmpls
[1]))
1885 || TREE_DEPRECATED (TREE_TYPE (decls
[1])))
1888 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpls
[1]) };
1889 tree decl_attrs
[] = { DECL_ATTRIBUTES (decl
), TYPE_ATTRIBUTES (decls
[1]) };
1892 decl_attrs
[0] = attrlist
;
1893 else if (!decl_attrs
[1])
1894 decl_attrs
[1] = attrlist
;
1896 /* Avoid warning if the template has no attributes. */
1897 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
1900 /* Avoid warning if either declaration contains an attribute on
1901 the white list below. */
1902 const char* const whitelist
[] = {
1906 for (unsigned i
= 0; i
!= 2; ++i
)
1907 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
1908 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
1909 || lookup_attribute (whitelist
[j
], decl_attrs
[i
]))
1912 /* Put together a list of the black-listed attributes that the template
1913 is declared with and the declaration is not, in case it's not apparent
1914 from the most recent declaration of the template. */
1915 unsigned nattrs
= 0;
1917 for (unsigned i
= 0; blacklist
[i
]; ++i
)
1919 /* Attribute leaf only applies to extern functions. Avoid mentioning
1920 it when it's missing from a static declaration. */
1921 if (!TREE_PUBLIC (decl
)
1922 && !strcmp ("leaf", blacklist
[i
]))
1925 for (unsigned j
= 0; j
!= 2; ++j
)
1927 if (!has_attribute (tmpls
[j
], tmpl_attrs
[j
], blacklist
[i
]))
1931 unsigned kmax
= 1 + !!decl_attrs
[1];
1932 for (unsigned k
= 0; k
!= kmax
; ++k
)
1934 if (has_attribute (decls
[k
], decl_attrs
[k
], blacklist
[i
]))
1944 pp_string (attrstr
, ", ");
1945 pp_begin_quote (attrstr
, pp_show_color (global_dc
->printer
));
1946 pp_string (attrstr
, blacklist
[i
]);
1947 pp_end_quote (attrstr
, pp_show_color (global_dc
->printer
));
1958 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
1959 specifies either attributes that are incompatible with those of
1960 TARGET, or attributes that are missing and that declaring ALIAS
1961 with would benefit. */
1964 maybe_diag_alias_attributes (tree alias
, tree target
)
1966 /* Do not expect attributes to match between aliases and ifunc
1967 resolvers. There is no obvious correspondence between them. */
1968 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias
)))
1971 const char* const blacklist
[] = {
1972 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
1973 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
1974 "returns_twice", NULL
1977 pretty_printer attrnames
;
1978 if (warn_attribute_alias
> 1)
1980 /* With -Wattribute-alias=2 detect alias declarations that are more
1981 restrictive than their targets first. Those indicate potential
1983 if (unsigned n
= decls_mismatched_attributes (alias
, target
, NULL_TREE
,
1984 blacklist
, &attrnames
))
1986 auto_diagnostic_group d
;
1987 if (warning_n (DECL_SOURCE_LOCATION (alias
),
1988 OPT_Wattribute_alias_
, n
,
1989 "%qD specifies more restrictive attribute than "
1990 "its target %qD: %s",
1991 "%qD specifies more restrictive attributes than "
1992 "its target %qD: %s",
1993 alias
, target
, pp_formatted_text (&attrnames
)))
1994 inform (DECL_SOURCE_LOCATION (target
),
1995 "%qD target declared here", alias
);
2000 /* Detect alias declarations that are less restrictive than their
2001 targets. Those suggest potential optimization opportunities
2002 (solved by adding the missing attribute(s) to the alias). */
2003 if (unsigned n
= decls_mismatched_attributes (target
, alias
, NULL_TREE
,
2004 blacklist
, &attrnames
))
2006 auto_diagnostic_group d
;
2007 if (warning_n (DECL_SOURCE_LOCATION (alias
),
2008 OPT_Wmissing_attributes
, n
,
2009 "%qD specifies less restrictive attribute than "
2010 "its target %qD: %s",
2011 "%qD specifies less restrictive attributes than "
2012 "its target %qD: %s",
2013 alias
, target
, pp_formatted_text (&attrnames
)))
2014 inform (DECL_SOURCE_LOCATION (target
),
2015 "%qD target declared here", alias
);
2025 /* Helper types to verify the consistency attribute exclusions. */
2027 typedef std::pair
<const char *, const char *> excl_pair
;
2029 struct excl_hash_traits
: typed_noop_remove
<excl_pair
>
2031 typedef excl_pair value_type
;
2032 typedef value_type compare_type
;
2034 static hashval_t
hash (const value_type
&x
)
2036 hashval_t h1
= htab_hash_string (x
.first
);
2037 hashval_t h2
= htab_hash_string (x
.second
);
2041 static bool equal (const value_type
&x
, const value_type
&y
)
2043 return !strcmp (x
.first
, y
.first
) && !strcmp (x
.second
, y
.second
);
2046 static void mark_deleted (value_type
&x
)
2048 x
= value_type (NULL
, NULL
);
2051 static void mark_empty (value_type
&x
)
2053 x
= value_type ("", "");
2056 static bool is_deleted (const value_type
&x
)
2058 return !x
.first
&& !x
.second
;
2061 static bool is_empty (const value_type
&x
)
2063 return !*x
.first
&& !*x
.second
;
2068 /* Self-test to verify that each attribute exclusion is symmetric,
2069 meaning that if attribute A is encoded as incompatible with
2070 attribute B then the opposite relationship is also encoded.
2071 This test also detects most cases of misspelled attribute names
2075 test_attribute_exclusions ()
2077 /* Iterate over the array of attribute tables first (with TI0 as
2078 the index) and over the array of attribute_spec in each table
2079 (with SI0 as the index). */
2080 const size_t ntables
= ARRAY_SIZE (attribute_tables
);
2082 /* Set of pairs of mutually exclusive attributes. */
2083 typedef hash_set
<excl_pair
, false, excl_hash_traits
> exclusion_set
;
2084 exclusion_set excl_set
;
2086 for (size_t ti0
= 0; ti0
!= ntables
; ++ti0
)
2087 for (size_t s0
= 0; attribute_tables
[ti0
][s0
].name
; ++s0
)
2089 const attribute_spec::exclusions
*excl
2090 = attribute_tables
[ti0
][s0
].exclude
;
2092 /* Skip each attribute that doesn't define exclusions. */
2096 const char *attr_name
= attribute_tables
[ti0
][s0
].name
;
2098 /* Iterate over the set of exclusions for every attribute
2099 (with EI0 as the index) adding the exclusions defined
2100 for each to the set. */
2101 for (size_t ei0
= 0; excl
[ei0
].name
; ++ei0
)
2103 const char *excl_name
= excl
[ei0
].name
;
2105 if (!strcmp (attr_name
, excl_name
))
2108 excl_set
.add (excl_pair (attr_name
, excl_name
));
2112 /* Traverse the set of mutually exclusive pairs of attributes
2113 and verify that they are symmetric. */
2114 for (exclusion_set::iterator it
= excl_set
.begin ();
2115 it
!= excl_set
.end ();
2118 if (!excl_set
.contains (excl_pair ((*it
).second
, (*it
).first
)))
2120 /* An exclusion for an attribute has been found that
2121 doesn't have a corresponding exclusion in the opposite
2124 sprintf (desc
, "'%s' attribute exclusion '%s' must be symmetric",
2125 (*it
).first
, (*it
).second
);
2126 fail (SELFTEST_LOCATION
, desc
);
2132 attribute_c_tests ()
2134 test_attribute_exclusions ();
2137 } /* namespace selftest */
2139 #endif /* CHECKING_P */