tree-optimization/114485 - neg induction with partial vectors
[official-gcc.git] / gcc / attribs.cc
blobfc7459c3850ab22a631087034986a5de8e416dc6
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define INCLUDE_STRING
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "diagnostic-core.h"
28 #include "attribs.h"
29 #include "fold-const.h"
30 #include "ipa-strub.h"
31 #include "stor-layout.h"
32 #include "langhooks.h"
33 #include "plugin.h"
34 #include "selftest.h"
35 #include "hash-set.h"
36 #include "diagnostic.h"
37 #include "pretty-print.h"
38 #include "tree-pretty-print.h"
39 #include "intl.h"
41 /* Table of the tables of attributes (common, language, format, machine)
42 searched. */
43 static array_slice<const scoped_attribute_specs *const> attribute_tables[2];
45 /* Substring representation. */
47 struct substring
49 const char *str;
50 int length;
53 /* Simple hash function to avoid need to scan whole string. */
55 static inline hashval_t
56 substring_hash (const char *str, int l)
58 return str[0] + str[l - 1] * 256 + l * 65536;
61 /* Used for attribute_hash. */
63 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
65 typedef substring *compare_type;
66 static inline hashval_t hash (const attribute_spec *);
67 static inline bool equal (const attribute_spec *, const substring *);
70 inline hashval_t
71 attribute_hasher::hash (const attribute_spec *spec)
73 const int l = strlen (spec->name);
74 return substring_hash (spec->name, l);
77 inline bool
78 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
80 return (strncmp (spec->name, str->str, str->length) == 0
81 && !spec->name[str->length]);
84 /* Scoped attribute name representation. */
86 struct scoped_attributes
88 const char *ns;
89 vec<attribute_spec> attributes;
90 hash_table<attribute_hasher> *attribute_hash;
91 /* True if we should not warn about unknown attributes in this NS. */
92 bool ignored_p;
95 /* The table of scope attributes. */
96 static vec<scoped_attributes> attributes_table;
98 static scoped_attributes* find_attribute_namespace (const char*);
99 static void register_scoped_attribute (const struct attribute_spec *,
100 scoped_attributes *);
101 static const struct attribute_spec *lookup_scoped_attribute_spec (const_tree,
102 const_tree);
104 static bool attributes_initialized = false;
106 /* Do not use directly; go through get_gnu_namespace instead. */
107 static GTY(()) tree gnu_namespace_cache;
109 /* Return the IDENTIFIER_NODE for the gnu namespace. */
111 static tree
112 get_gnu_namespace ()
114 if (!gnu_namespace_cache)
115 gnu_namespace_cache = get_identifier ("gnu");
116 return gnu_namespace_cache;
119 /* Insert SPECS into its namespace. IGNORED_P is true iff all unknown
120 attributes in this namespace should be ignored for the purposes of
121 -Wattributes. The function returns the namespace into which the
122 attributes have been registered. */
124 scoped_attributes *
125 register_scoped_attributes (const scoped_attribute_specs &specs,
126 bool ignored_p /*=false*/)
128 scoped_attributes *result = NULL;
130 /* See if we already have attributes in the namespace NS. */
131 result = find_attribute_namespace (specs.ns);
133 if (result == NULL)
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));
142 sa.ns = specs.ns;
143 sa.attributes.create (64);
144 sa.ignored_p = ignored_p;
145 result = attributes_table.safe_push (sa);
146 result->attribute_hash = new hash_table<attribute_hasher> (200);
148 else
149 result->ignored_p |= ignored_p;
151 /* Really add the attributes to their namespace now. */
152 for (const attribute_spec &attribute : specs.attributes)
154 result->attributes.safe_push (attribute);
155 register_scoped_attribute (&attribute, result);
158 gcc_assert (result != NULL);
160 return result;
163 /* Return the namespace which name is NS, NULL if none exist. */
165 static scoped_attributes*
166 find_attribute_namespace (const char* ns)
168 for (scoped_attributes &iter : attributes_table)
169 if (ns == iter.ns
170 || (iter.ns != NULL
171 && ns != NULL
172 && !strcmp (iter.ns, ns)))
173 return &iter;
174 return NULL;
177 /* Make some sanity checks on the attribute tables. */
179 static void
180 check_attribute_tables (void)
182 hash_set<pair_hash<nofree_string_hash, nofree_string_hash>> names;
184 for (auto scoped_array : attribute_tables)
185 for (auto scoped_attributes : scoped_array)
186 for (const attribute_spec &attribute : scoped_attributes->attributes)
188 /* The name must not begin and end with __. */
189 const char *name = attribute.name;
190 int len = strlen (name);
192 gcc_assert (!(name[0] == '_' && name[1] == '_'
193 && name[len - 1] == '_' && name[len - 2] == '_'));
195 /* The minimum and maximum lengths must be consistent. */
196 gcc_assert (attribute.min_length >= 0);
198 gcc_assert (attribute.max_length == -1
199 || attribute.max_length >= attribute.min_length);
201 /* An attribute cannot require both a DECL and a TYPE. */
202 gcc_assert (!attribute.decl_required
203 || !attribute.type_required);
205 /* If an attribute requires a function type, in particular
206 it requires a type. */
207 gcc_assert (!attribute.function_type_required
208 || attribute.type_required);
210 /* Check that no name occurs more than once. Names that
211 begin with '*' are exempt, and may be overridden. */
212 const char *ns = scoped_attributes->ns;
213 if (name[0] != '*' && names.add ({ ns ? ns : "", name }))
214 gcc_unreachable ();
218 /* Used to stash pointers to allocated memory so that we can free them at
219 the end of parsing of all TUs. */
220 static vec<attribute_spec *> ignored_attributes_table;
222 /* Parse arguments V of -Wno-attributes=.
223 Currently we accept:
224 vendor::attr
225 vendor::
226 This functions also registers the parsed attributes so that we don't
227 warn that we don't recognize them. */
229 void
230 handle_ignored_attributes_option (vec<char *> *v)
232 if (v == nullptr)
233 return;
235 for (auto opt : v)
237 char *cln = strstr (opt, "::");
238 /* We don't accept '::attr'. */
239 if (cln == nullptr || cln == opt)
241 auto_diagnostic_group d;
242 error ("wrong argument to ignored attributes");
243 inform (input_location, "valid format is %<ns::attr%> or %<ns::%>");
244 continue;
246 const char *vendor_start = opt;
247 ptrdiff_t vendor_len = cln - opt;
248 const char *attr_start = cln + 2;
249 /* This could really use rawmemchr :(. */
250 ptrdiff_t attr_len = strchr (attr_start, '\0') - attr_start;
251 /* Verify that they look valid. */
252 auto valid_p = [](const char *const s, ptrdiff_t len) {
253 bool ok = false;
255 for (int i = 0; i < len; ++i)
256 if (ISALNUM (s[i]))
257 ok = true;
258 else if (s[i] != '_')
259 return false;
261 return ok;
263 if (!valid_p (vendor_start, vendor_len))
265 error ("wrong argument to ignored attributes");
266 continue;
268 canonicalize_attr_name (vendor_start, vendor_len);
269 /* We perform all this hijinks so that we don't have to copy OPT. */
270 tree vendor_id = get_identifier_with_length (vendor_start, vendor_len);
271 array_slice<const attribute_spec> attrs;
272 /* In the "vendor::" case, we should ignore *any* attribute coming
273 from this attribute namespace. */
274 if (attr_len > 0)
276 if (!valid_p (attr_start, attr_len))
278 error ("wrong argument to ignored attributes");
279 continue;
281 canonicalize_attr_name (attr_start, attr_len);
282 tree attr_id = get_identifier_with_length (attr_start, attr_len);
283 const char *attr = IDENTIFIER_POINTER (attr_id);
284 /* If we've already seen this vendor::attr, ignore it. Attempting to
285 register it twice would lead to a crash. */
286 if (lookup_scoped_attribute_spec (vendor_id, attr_id))
287 continue;
288 /* Create a table with extra attributes which we will register.
289 We can't free it here, so squirrel away the pointers. */
290 attribute_spec *table = new attribute_spec {
291 attr, 0, -2, false, false, false, false, nullptr, nullptr
293 ignored_attributes_table.safe_push (table);
294 attrs = { table, 1 };
296 const scoped_attribute_specs scoped_specs = {
297 IDENTIFIER_POINTER (vendor_id), { attrs }
299 register_scoped_attributes (scoped_specs, attrs.empty ());
303 /* Free data we might have allocated when adding extra attributes. */
305 void
306 free_attr_data ()
308 for (auto x : ignored_attributes_table)
309 delete x;
310 ignored_attributes_table.release ();
313 /* Initialize attribute tables, and make some sanity checks if checking is
314 enabled. */
316 void
317 init_attributes (void)
319 if (attributes_initialized)
320 return;
322 attribute_tables[0] = lang_hooks.attribute_table;
323 attribute_tables[1] = targetm.attribute_table;
325 if (flag_checking)
326 check_attribute_tables ();
328 for (auto scoped_array : attribute_tables)
329 for (auto scoped_attributes : scoped_array)
330 register_scoped_attributes (*scoped_attributes);
332 vec<char *> *ignored = (vec<char *> *) flag_ignored_attributes;
333 handle_ignored_attributes_option (ignored);
335 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
336 attributes_initialized = true;
339 /* Insert a single ATTR into the attribute table. */
341 void
342 register_attribute (const struct attribute_spec *attr)
344 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
347 /* Insert a single attribute ATTR into a namespace of attributes. */
349 static void
350 register_scoped_attribute (const struct attribute_spec *attr,
351 scoped_attributes *name_space)
353 struct substring str;
354 attribute_spec **slot;
356 gcc_assert (attr != NULL && name_space != NULL);
358 gcc_assert (name_space->attribute_hash);
360 str.str = attr->name;
361 str.length = strlen (str.str);
363 /* Attribute names in the table must be in the form 'text' and not
364 in the form '__text__'. */
365 gcc_checking_assert (!canonicalize_attr_name (str.str, str.length));
367 slot = name_space->attribute_hash
368 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
369 INSERT);
370 gcc_assert (!*slot || attr->name[0] == '*');
371 *slot = CONST_CAST (struct attribute_spec *, attr);
374 /* Return the spec for the scoped attribute with namespace NS and
375 name NAME. */
377 static const struct attribute_spec *
378 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
380 struct substring attr;
381 scoped_attributes *attrs;
383 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
385 attrs = find_attribute_namespace (ns_str);
387 if (attrs == NULL)
388 return NULL;
390 attr.str = IDENTIFIER_POINTER (name);
391 attr.length = IDENTIFIER_LENGTH (name);
392 return attrs->attribute_hash->find_with_hash (&attr,
393 substring_hash (attr.str,
394 attr.length));
397 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
398 it also specifies the attribute namespace. */
400 const struct attribute_spec *
401 lookup_attribute_spec (const_tree name)
403 tree ns;
404 if (TREE_CODE (name) == TREE_LIST)
406 ns = TREE_PURPOSE (name);
407 name = TREE_VALUE (name);
409 else
410 ns = get_gnu_namespace ();
411 return lookup_scoped_attribute_spec (ns, name);
415 /* Return the namespace of the attribute ATTR. This accessor works on
416 GNU and C++11 (scoped) attributes. On GNU attributes,
417 it returns an identifier tree for the string "gnu".
419 Please read the comments of cxx11_attribute_p to understand the
420 format of attributes. */
422 tree
423 get_attribute_namespace (const_tree attr)
425 if (cxx11_attribute_p (attr))
426 return TREE_PURPOSE (TREE_PURPOSE (attr));
427 return get_gnu_namespace ();
430 /* Check LAST_DECL and NODE of the same symbol for attributes that are
431 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
432 them, and return true if any have been found. NODE can be a DECL
433 or a TYPE. */
435 static bool
436 diag_attr_exclusions (tree last_decl, tree node, tree attrname,
437 const attribute_spec *spec)
439 const attribute_spec::exclusions *excl = spec->exclude;
441 tree_code code = TREE_CODE (node);
443 if ((code == FUNCTION_DECL && !excl->function
444 && (!excl->type || !spec->affects_type_identity))
445 || (code == VAR_DECL && !excl->variable
446 && (!excl->type || !spec->affects_type_identity))
447 || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
448 return false;
450 /* True if an attribute that's mutually exclusive with ATTRNAME
451 has been found. */
452 bool found = false;
454 if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
456 /* Check both the last DECL and its type for conflicts with
457 the attribute being added to the current decl or type. */
458 found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
459 tree decl_type = TREE_TYPE (last_decl);
460 found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
463 /* NODE is either the current DECL to which the attribute is being
464 applied or its TYPE. For the former, consider the attributes on
465 both the DECL and its type. */
466 tree attrs[2];
468 if (DECL_P (node))
470 attrs[0] = DECL_ATTRIBUTES (node);
471 attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
473 else
475 attrs[0] = TYPE_ATTRIBUTES (node);
476 attrs[1] = NULL_TREE;
479 /* Iterate over the mutually exclusive attribute names and verify
480 that the symbol doesn't contain it. */
481 for (unsigned i = 0; i != ARRAY_SIZE (attrs); ++i)
483 if (!attrs[i])
484 continue;
486 for ( ; excl->name; ++excl)
488 /* Avoid checking the attribute against itself. */
489 if (is_attribute_p (excl->name, attrname))
490 continue;
492 if (!lookup_attribute (excl->name, attrs[i]))
493 continue;
495 /* An exclusion may apply either to a function declaration,
496 type declaration, or a field/variable declaration, or
497 any subset of the three. */
498 if (TREE_CODE (node) == FUNCTION_DECL
499 && !excl->function)
500 continue;
502 if (TREE_CODE (node) == TYPE_DECL
503 && !excl->type)
504 continue;
506 if ((TREE_CODE (node) == FIELD_DECL
507 || VAR_P (node))
508 && !excl->variable)
509 continue;
511 found = true;
513 /* Print a note? */
514 bool note = last_decl != NULL_TREE;
515 auto_diagnostic_group d;
516 if (TREE_CODE (node) == FUNCTION_DECL
517 && fndecl_built_in_p (node))
518 note &= warning (OPT_Wattributes,
519 "ignoring attribute %qE in declaration of "
520 "a built-in function %qD because it conflicts "
521 "with attribute %qs",
522 attrname, node, excl->name);
523 else
524 note &= warning (OPT_Wattributes,
525 "ignoring attribute %qE because "
526 "it conflicts with attribute %qs",
527 attrname, excl->name);
529 if (note)
530 inform (DECL_SOURCE_LOCATION (last_decl),
531 "previous declaration here");
535 return found;
538 /* Return true iff we should not complain about unknown attributes
539 coming from the attribute namespace NS. This is the case for
540 the -Wno-attributes=ns:: command-line option. */
542 static bool
543 attr_namespace_ignored_p (tree ns)
545 if (ns == NULL_TREE)
546 return false;
547 scoped_attributes *r = find_attribute_namespace (IDENTIFIER_POINTER (ns));
548 return r && r->ignored_p;
551 /* Return true if the attribute ATTR should not be warned about. */
553 bool
554 attribute_ignored_p (tree attr)
556 if (!cxx11_attribute_p (attr))
557 return false;
558 if (tree ns = get_attribute_namespace (attr))
560 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
561 if (as == NULL && attr_namespace_ignored_p (ns))
562 return true;
563 if (as && as->max_length == -2)
564 return true;
566 return false;
569 /* Like above, but takes an attribute_spec AS, which must be nonnull. */
571 bool
572 attribute_ignored_p (const attribute_spec *const as)
574 return as->max_length == -2;
577 /* Return true if the ATTRS chain contains at least one attribute which
578 is not ignored. */
580 bool
581 any_nonignored_attribute_p (tree attrs)
583 for (tree attr = attrs; attr; attr = TREE_CHAIN (attr))
584 if (!attribute_ignored_p (attr))
585 return true;
587 return false;
590 /* See whether LIST contains at least one instance of attribute ATTR
591 (possibly with different arguments). Return the first such attribute
592 if so, otherwise return null. */
594 static tree
595 find_same_attribute (const_tree attr, tree list)
597 if (list == NULL_TREE)
598 return NULL_TREE;
599 tree ns = get_attribute_namespace (attr);
600 tree name = get_attribute_name (attr);
601 return private_lookup_attribute (ns ? IDENTIFIER_POINTER (ns) : nullptr,
602 IDENTIFIER_POINTER (name),
603 ns ? IDENTIFIER_LENGTH (ns) : 0,
604 IDENTIFIER_LENGTH (name), list);
607 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
608 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
609 it should be modified in place; if a TYPE, a copy should be created
610 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
611 information, in the form of a bitwise OR of flags in enum attribute_flags
612 from tree.h. Depending on these flags, some attributes may be
613 returned to be applied at a later stage (for example, to apply
614 a decl attribute to the declaration rather than to its type). */
616 tree
617 decl_attributes (tree *node, tree attributes, int flags,
618 tree last_decl /* = NULL_TREE */)
620 tree returned_attrs = NULL_TREE;
622 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
623 return NULL_TREE;
625 if (!attributes_initialized)
626 init_attributes ();
628 /* If this is a function and the user used #pragma GCC optimize, add the
629 options to the attribute((optimize(...))) list. */
630 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
632 tree cur_attr = lookup_attribute ("optimize", attributes);
633 tree opts = copy_list (current_optimize_pragma);
635 if (! cur_attr)
636 attributes
637 = tree_cons (get_identifier ("optimize"), opts, attributes);
638 else
639 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
642 if (TREE_CODE (*node) == FUNCTION_DECL
643 && (optimization_current_node != optimization_default_node
644 || target_option_current_node != target_option_default_node)
645 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
647 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
648 /* Don't set DECL_FUNCTION_SPECIFIC_TARGET for targets that don't
649 support #pragma GCC target or target attribute. */
650 if (target_option_default_node)
652 tree cur_tree
653 = build_target_option_node (&global_options, &global_options_set);
654 tree old_tree = DECL_FUNCTION_SPECIFIC_TARGET (*node);
655 if (!old_tree)
656 old_tree = target_option_default_node;
657 /* The changes on optimization options can cause the changes in
658 target options, update it accordingly if it's changed. */
659 if (old_tree != cur_tree)
660 DECL_FUNCTION_SPECIFIC_TARGET (*node) = cur_tree;
664 /* If this is a function and the user used #pragma GCC target, add the
665 options to the attribute((target(...))) list. */
666 if (TREE_CODE (*node) == FUNCTION_DECL
667 && current_target_pragma
668 && targetm.target_option.valid_attribute_p (*node,
669 get_identifier ("target"),
670 current_target_pragma, 0))
672 tree cur_attr = lookup_attribute ("target", attributes);
673 tree opts = copy_list (current_target_pragma);
675 if (! cur_attr)
676 attributes = tree_cons (get_identifier ("target"), opts, attributes);
677 else
678 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
681 /* A "naked" function attribute implies "noinline" and "noclone" for
682 those targets that support it. */
683 if (TREE_CODE (*node) == FUNCTION_DECL
684 && attributes
685 && lookup_attribute ("naked", attributes) != NULL
686 && lookup_attribute_spec (get_identifier ("naked"))
687 && lookup_attribute ("noipa", attributes) == NULL)
688 attributes = tree_cons (get_identifier ("noipa"), NULL, attributes);
690 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
691 for those targets that support it. */
692 if (TREE_CODE (*node) == FUNCTION_DECL
693 && attributes
694 && lookup_attribute ("noipa", attributes) != NULL
695 && lookup_attribute_spec (get_identifier ("noipa")))
697 if (lookup_attribute ("noinline", attributes) == NULL)
698 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
700 if (lookup_attribute ("noclone", attributes) == NULL)
701 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
703 if (lookup_attribute ("no_icf", attributes) == NULL)
704 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
707 targetm.insert_attributes (*node, &attributes);
709 /* Note that attributes on the same declaration are not necessarily
710 in the same order as in the source. */
711 for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
713 tree ns = get_attribute_namespace (attr);
714 tree name = get_attribute_name (attr);
715 tree args = TREE_VALUE (attr);
716 tree *anode = node;
717 const struct attribute_spec *spec
718 = lookup_scoped_attribute_spec (ns, name);
719 int fn_ptr_quals = 0;
720 tree fn_ptr_tmp = NULL_TREE;
721 const bool cxx11_attr_p = cxx11_attribute_p (attr);
723 if (spec == NULL)
725 if (!(flags & (int) ATTR_FLAG_BUILT_IN)
726 && !attr_namespace_ignored_p (ns))
728 if (ns == NULL_TREE || !cxx11_attr_p)
729 warning (OPT_Wattributes, "%qE attribute directive ignored",
730 name);
731 else if ((flag_openmp || flag_openmp_simd)
732 && is_attribute_p ("omp", ns)
733 && is_attribute_p ("directive", name)
734 && (VAR_P (*node)
735 || TREE_CODE (*node) == FUNCTION_DECL))
736 continue;
737 else
738 warning (OPT_Wattributes,
739 "%<%E::%E%> scoped attribute directive ignored",
740 ns, name);
742 continue;
744 else
746 int nargs = list_length (args);
747 if (nargs < spec->min_length
748 || (spec->max_length >= 0
749 && nargs > spec->max_length))
751 auto_diagnostic_group d;
752 error ("wrong number of arguments specified for %qE attribute",
753 name);
754 if (spec->max_length < 0)
755 inform (input_location, "expected %i or more, found %i",
756 spec->min_length, nargs);
757 else if (spec->min_length == spec->max_length)
758 inform (input_location, "expected %i, found %i",
759 spec->min_length, nargs);
760 else
761 inform (input_location, "expected between %i and %i, found %i",
762 spec->min_length, spec->max_length, nargs);
763 continue;
766 gcc_assert (is_attribute_p (spec->name, name));
768 if (spec->decl_required && !DECL_P (*anode))
770 if (flags & ((int) ATTR_FLAG_DECL_NEXT
771 | (int) ATTR_FLAG_FUNCTION_NEXT
772 | (int) ATTR_FLAG_ARRAY_NEXT))
774 /* Pass on this attribute to be tried again. */
775 tree attr = tree_cons (name, args, NULL_TREE);
776 returned_attrs = chainon (returned_attrs, attr);
777 continue;
779 else
781 warning (OPT_Wattributes, "%qE attribute does not apply to types",
782 name);
783 continue;
787 /* If we require a type, but were passed a decl, set up to make a
788 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
789 would have applied if we'd been passed a type, but we cannot modify
790 the decl's type in place here. */
791 if (spec->type_required && DECL_P (*anode))
793 anode = &TREE_TYPE (*anode);
794 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
797 if (spec->function_type_required
798 && !FUNC_OR_METHOD_TYPE_P (*anode))
800 if (TREE_CODE (*anode) == POINTER_TYPE
801 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
803 /* OK, this is a bit convoluted. We can't just make a copy
804 of the pointer type and modify its TREE_TYPE, because if
805 we change the attributes of the target type the pointer
806 type needs to have a different TYPE_MAIN_VARIANT. So we
807 pull out the target type now, frob it as appropriate, and
808 rebuild the pointer type later.
810 This would all be simpler if attributes were part of the
811 declarator, grumble grumble. */
812 fn_ptr_tmp = TREE_TYPE (*anode);
813 fn_ptr_quals = TYPE_QUALS (*anode);
814 anode = &fn_ptr_tmp;
815 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
817 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
819 /* Pass on this attribute to be tried again. */
820 tree attr = tree_cons (name, args, NULL_TREE);
821 returned_attrs = chainon (returned_attrs, attr);
822 continue;
825 if (TREE_CODE (*anode) != FUNCTION_TYPE
826 && TREE_CODE (*anode) != METHOD_TYPE)
828 warning (OPT_Wattributes,
829 "%qE attribute only applies to function types",
830 name);
831 continue;
835 if (TYPE_P (*anode)
836 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
837 && COMPLETE_TYPE_P (*anode))
839 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
840 continue;
843 bool no_add_attrs = false;
845 /* Check for exclusions with other attributes on the current
846 declation as well as the last declaration of the same
847 symbol already processed (if one exists). Detect and
848 reject incompatible attributes. */
849 bool built_in = flags & ATTR_FLAG_BUILT_IN;
850 if (spec->exclude
851 && (flag_checking || !built_in)
852 && !error_operand_p (last_decl))
854 /* Always check attributes on user-defined functions.
855 Check them on built-ins only when -fchecking is set.
856 Ignore __builtin_unreachable -- it's both const and
857 noreturn. */
859 if (!built_in
860 || !DECL_P (*anode)
861 || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
862 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
863 && DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE_TRAP
864 && (DECL_FUNCTION_CODE (*anode)
865 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
867 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
868 if (!no_add && anode != node)
869 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
870 no_add_attrs |= no_add;
874 if (no_add_attrs
875 /* Don't add attributes registered just for -Wno-attributes=foo::bar
876 purposes. */
877 || attribute_ignored_p (attr))
878 continue;
880 if (spec->handler != NULL)
882 int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0);
884 /* Pass in an array of the current declaration followed
885 by the last pushed/merged declaration if one exists.
886 For calls that modify the type attributes of a DECL
887 and for which *ANODE is *NODE's type, also pass in
888 the DECL as the third element to use in diagnostics.
889 If the handler changes CUR_AND_LAST_DECL[0] replace
890 *ANODE with its value. */
891 tree cur_and_last_decl[3] = { *anode, last_decl };
892 if (anode != node && DECL_P (*node))
893 cur_and_last_decl[2] = *node;
895 tree ret = (spec->handler) (cur_and_last_decl, name, args,
896 flags|cxx11_flag, &no_add_attrs);
898 /* Fix up typedefs clobbered by attribute handlers. */
899 if (TREE_CODE (*node) == TYPE_DECL
900 && anode == &TREE_TYPE (*node)
901 && DECL_ORIGINAL_TYPE (*node)
902 && TYPE_NAME (*anode) == *node
903 && TYPE_NAME (cur_and_last_decl[0]) != *node)
905 tree t = cur_and_last_decl[0];
906 DECL_ORIGINAL_TYPE (*node) = t;
907 tree tt = build_variant_type_copy (t);
908 cur_and_last_decl[0] = tt;
909 TREE_TYPE (*node) = tt;
910 TYPE_NAME (tt) = *node;
913 if (*anode != cur_and_last_decl[0])
915 /* Even if !spec->function_type_required, allow the attribute
916 handler to request the attribute to be applied to the function
917 type, rather than to the function pointer type, by setting
918 cur_and_last_decl[0] to the function type. */
919 if (!fn_ptr_tmp
920 && POINTER_TYPE_P (*anode)
921 && TREE_TYPE (*anode) == cur_and_last_decl[0]
922 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
924 fn_ptr_tmp = TREE_TYPE (*anode);
925 fn_ptr_quals = TYPE_QUALS (*anode);
926 anode = &fn_ptr_tmp;
928 *anode = cur_and_last_decl[0];
931 if (ret == error_mark_node)
933 warning (OPT_Wattributes, "%qE attribute ignored", name);
934 no_add_attrs = true;
936 else
937 returned_attrs = chainon (ret, returned_attrs);
940 /* Layout the decl in case anything changed. */
941 if (spec->type_required && DECL_P (*node)
942 && (VAR_P (*node)
943 || TREE_CODE (*node) == PARM_DECL
944 || TREE_CODE (*node) == RESULT_DECL))
945 relayout_decl (*node);
947 if (!no_add_attrs)
949 tree old_attrs;
950 tree a;
952 if (DECL_P (*anode))
953 old_attrs = DECL_ATTRIBUTES (*anode);
954 else
955 old_attrs = TYPE_ATTRIBUTES (*anode);
957 for (a = find_same_attribute (attr, old_attrs);
958 a != NULL_TREE;
959 a = find_same_attribute (attr, TREE_CHAIN (a)))
961 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
962 break;
965 if (a == NULL_TREE)
967 /* This attribute isn't already in the list. */
968 tree r;
969 /* Preserve the C++11 form. */
970 if (cxx11_attr_p)
971 r = tree_cons (build_tree_list (ns, name), args, old_attrs);
972 else
973 r = tree_cons (name, args, old_attrs);
975 if (DECL_P (*anode))
976 DECL_ATTRIBUTES (*anode) = r;
977 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
979 TYPE_ATTRIBUTES (*anode) = r;
980 /* If this is the main variant, also push the attributes
981 out to the other variants. */
982 if (*anode == TYPE_MAIN_VARIANT (*anode))
984 for (tree variant = *anode; variant;
985 variant = TYPE_NEXT_VARIANT (variant))
987 if (TYPE_ATTRIBUTES (variant) == old_attrs)
988 TYPE_ATTRIBUTES (variant)
989 = TYPE_ATTRIBUTES (*anode);
990 else if (!find_same_attribute
991 (attr, TYPE_ATTRIBUTES (variant)))
992 TYPE_ATTRIBUTES (variant) = tree_cons
993 (name, args, TYPE_ATTRIBUTES (variant));
997 else
998 *anode = build_type_attribute_variant (*anode, r);
1002 if (fn_ptr_tmp)
1004 /* Rebuild the function pointer type and put it in the
1005 appropriate place. */
1006 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
1007 if (fn_ptr_quals)
1008 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
1009 if (DECL_P (*node))
1010 TREE_TYPE (*node) = fn_ptr_tmp;
1011 else
1013 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
1014 *node = fn_ptr_tmp;
1019 return returned_attrs;
1022 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
1023 attribute.
1025 When G++ parses a C++11 attribute, it is represented as
1026 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
1027 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
1028 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
1029 use get_attribute_namespace and get_attribute_name to retrieve the
1030 namespace and name of the attribute, as these accessors work with
1031 GNU attributes as well. */
1033 bool
1034 cxx11_attribute_p (const_tree attr)
1036 if (attr == NULL_TREE
1037 || TREE_CODE (attr) != TREE_LIST)
1038 return false;
1040 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
1043 /* Return the name of the attribute ATTR. This accessor works on GNU
1044 and C++11 (scoped) attributes.
1046 Please read the comments of cxx11_attribute_p to understand the
1047 format of attributes. */
1049 tree
1050 get_attribute_name (const_tree attr)
1052 if (cxx11_attribute_p (attr))
1053 return TREE_VALUE (TREE_PURPOSE (attr));
1054 return TREE_PURPOSE (attr);
1057 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
1058 to the method FNDECL. */
1060 void
1061 apply_tm_attr (tree fndecl, tree attr)
1063 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
1066 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
1067 it to CHAIN. */
1069 tree
1070 make_attribute (const char *name, const char *arg_name, tree chain)
1072 tree attr_name;
1073 tree attr_arg_name;
1074 tree attr_args;
1075 tree attr;
1077 attr_name = get_identifier (name);
1078 attr_arg_name = build_string (strlen (arg_name), arg_name);
1079 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
1080 attr = tree_cons (attr_name, attr_args, chain);
1081 return attr;
1085 /* Common functions used for target clone support. */
1087 /* Comparator function to be used in qsort routine to sort attribute
1088 specification strings to "target". */
1090 static int
1091 attr_strcmp (const void *v1, const void *v2)
1093 const char *c1 = *(char *const*)v1;
1094 const char *c2 = *(char *const*)v2;
1095 return strcmp (c1, c2);
1098 /* ARGLIST is the argument to target attribute. This function tokenizes
1099 the comma separated arguments, sorts them and returns a string which
1100 is a unique identifier for the comma separated arguments. It also
1101 replaces non-identifier characters "=,-" with "_". */
1103 char *
1104 sorted_attr_string (tree arglist)
1106 tree arg;
1107 size_t str_len_sum = 0;
1108 char **args = NULL;
1109 char *attr_str, *ret_str;
1110 char *attr = NULL;
1111 unsigned int argnum = 1;
1112 unsigned int i;
1114 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1116 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1117 size_t len = strlen (str);
1118 str_len_sum += len + 1;
1119 if (arg != arglist)
1120 argnum++;
1121 for (i = 0; i < strlen (str); i++)
1122 if (str[i] == ',')
1123 argnum++;
1126 attr_str = XNEWVEC (char, str_len_sum);
1127 str_len_sum = 0;
1128 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1130 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1131 size_t len = strlen (str);
1132 memcpy (attr_str + str_len_sum, str, len);
1133 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
1134 str_len_sum += len + 1;
1137 /* Replace "=,-" with "_". */
1138 for (i = 0; i < strlen (attr_str); i++)
1139 if (attr_str[i] == '=' || attr_str[i]== '-')
1140 attr_str[i] = '_';
1142 if (argnum == 1)
1143 return attr_str;
1145 args = XNEWVEC (char *, argnum);
1147 i = 0;
1148 attr = strtok (attr_str, ",");
1149 while (attr != NULL)
1151 args[i] = attr;
1152 i++;
1153 attr = strtok (NULL, ",");
1156 qsort (args, argnum, sizeof (char *), attr_strcmp);
1158 ret_str = XNEWVEC (char, str_len_sum);
1159 str_len_sum = 0;
1160 for (i = 0; i < argnum; i++)
1162 size_t len = strlen (args[i]);
1163 memcpy (ret_str + str_len_sum, args[i], len);
1164 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
1165 str_len_sum += len + 1;
1168 XDELETEVEC (args);
1169 XDELETEVEC (attr_str);
1170 return ret_str;
1174 /* This function returns true if FN1 and FN2 are versions of the same function,
1175 that is, the target strings of the function decls are different. This assumes
1176 that FN1 and FN2 have the same signature. */
1178 bool
1179 common_function_versions (tree fn1, tree fn2)
1181 tree attr1, attr2;
1182 char *target1, *target2;
1183 bool result;
1185 if (TREE_CODE (fn1) != FUNCTION_DECL
1186 || TREE_CODE (fn2) != FUNCTION_DECL)
1187 return false;
1189 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
1190 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
1192 /* At least one function decl should have the target attribute specified. */
1193 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
1194 return false;
1196 /* Diagnose missing target attribute if one of the decls is already
1197 multi-versioned. */
1198 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
1200 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
1202 if (attr2 != NULL_TREE)
1204 std::swap (fn1, fn2);
1205 attr1 = attr2;
1207 auto_diagnostic_group d;
1208 error_at (DECL_SOURCE_LOCATION (fn2),
1209 "missing %<target%> attribute for multi-versioned %qD",
1210 fn2);
1211 inform (DECL_SOURCE_LOCATION (fn1),
1212 "previous declaration of %qD", fn1);
1213 /* Prevent diagnosing of the same error multiple times. */
1214 DECL_ATTRIBUTES (fn2)
1215 = tree_cons (get_identifier ("target"),
1216 copy_node (TREE_VALUE (attr1)),
1217 DECL_ATTRIBUTES (fn2));
1219 return false;
1222 target1 = sorted_attr_string (TREE_VALUE (attr1));
1223 target2 = sorted_attr_string (TREE_VALUE (attr2));
1225 /* The sorted target strings must be different for fn1 and fn2
1226 to be versions. */
1227 if (strcmp (target1, target2) == 0)
1228 result = false;
1229 else
1230 result = true;
1232 XDELETEVEC (target1);
1233 XDELETEVEC (target2);
1235 return result;
1238 /* Make a dispatcher declaration for the multi-versioned function DECL.
1239 Calls to DECL function will be replaced with calls to the dispatcher
1240 by the front-end. Return the decl created. */
1242 tree
1243 make_dispatcher_decl (const tree decl)
1245 tree func_decl;
1246 char *func_name;
1247 tree fn_type, func_type;
1249 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1251 fn_type = TREE_TYPE (decl);
1252 func_type = build_function_type (TREE_TYPE (fn_type),
1253 TYPE_ARG_TYPES (fn_type));
1255 func_decl = build_fn_decl (func_name, func_type);
1256 XDELETEVEC (func_name);
1257 TREE_USED (func_decl) = 1;
1258 DECL_CONTEXT (func_decl) = NULL_TREE;
1259 DECL_INITIAL (func_decl) = error_mark_node;
1260 DECL_ARTIFICIAL (func_decl) = 1;
1261 /* Mark this func as external, the resolver will flip it again if
1262 it gets generated. */
1263 DECL_EXTERNAL (func_decl) = 1;
1264 /* This will be of type IFUNCs have to be externally visible. */
1265 TREE_PUBLIC (func_decl) = 1;
1267 return func_decl;
1270 /* Returns true if DECL is multi-versioned using the target attribute, and this
1271 is the default version. This function can only be used for targets that do
1272 not support the "target_version" attribute. */
1274 bool
1275 is_function_default_version (const tree decl)
1277 if (TREE_CODE (decl) != FUNCTION_DECL
1278 || !DECL_FUNCTION_VERSIONED (decl))
1279 return false;
1280 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1281 gcc_assert (attr);
1282 attr = TREE_VALUE (TREE_VALUE (attr));
1283 return (TREE_CODE (attr) == STRING_CST
1284 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1287 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1288 is ATTRIBUTE. */
1290 tree
1291 build_decl_attribute_variant (tree ddecl, tree attribute)
1293 DECL_ATTRIBUTES (ddecl) = attribute;
1294 return ddecl;
1297 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1298 is ATTRIBUTE and its qualifiers are QUALS.
1300 Record such modified types already made so we don't make duplicates. */
1302 tree
1303 build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
1305 tree ttype = otype;
1306 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1308 tree ntype;
1310 /* Building a distinct copy of a tagged type is inappropriate; it
1311 causes breakage in code that expects there to be a one-to-one
1312 relationship between a struct and its fields.
1313 build_duplicate_type is another solution (as used in
1314 handle_transparent_union_attribute), but that doesn't play well
1315 with the stronger C++ type identity model. */
1316 if (RECORD_OR_UNION_TYPE_P (ttype)
1317 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1319 warning (OPT_Wattributes,
1320 "ignoring attributes applied to %qT after definition",
1321 TYPE_MAIN_VARIANT (ttype));
1322 return build_qualified_type (ttype, quals);
1325 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
1326 if (lang_hooks.types.copy_lang_qualifiers
1327 && otype != TYPE_MAIN_VARIANT (otype))
1328 ttype = (lang_hooks.types.copy_lang_qualifiers
1329 (ttype, TYPE_MAIN_VARIANT (otype)));
1331 tree dtype = ntype = build_distinct_type_copy (ttype);
1333 TYPE_ATTRIBUTES (ntype) = attribute;
1335 hashval_t hash = type_hash_canon_hash (ntype);
1336 ntype = type_hash_canon (hash, ntype);
1338 if (ntype != dtype)
1339 /* This variant was already in the hash table, don't mess with
1340 TYPE_CANONICAL. */;
1341 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1342 || !comp_type_attributes (ntype, ttype))
1343 /* If the target-dependent attributes make NTYPE different from
1344 its canonical type, we will need to use structural equality
1345 checks for this type.
1347 We shouldn't get here for stripping attributes from a type;
1348 the no-attribute type might not need structural comparison. But
1349 we can if was discarded from type_hash_table. */
1350 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
1351 else if (TYPE_CANONICAL (ntype) == ntype)
1352 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1354 ttype = build_qualified_type (ntype, quals);
1355 if (lang_hooks.types.copy_lang_qualifiers
1356 && otype != TYPE_MAIN_VARIANT (otype))
1357 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
1359 else if (TYPE_QUALS (ttype) != quals)
1360 ttype = build_qualified_type (ttype, quals);
1362 return ttype;
1365 /* Compare two identifier nodes representing attributes.
1366 Return true if they are the same, false otherwise. */
1368 static bool
1369 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1371 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1372 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1373 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1375 /* Identifiers can be compared directly for equality. */
1376 if (attr1 == attr2)
1377 return true;
1379 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1380 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1383 /* Compare two constructor-element-type constants. Return 1 if the lists
1384 are known to be equal; otherwise return 0. */
1386 bool
1387 simple_cst_list_equal (const_tree l1, const_tree l2)
1389 while (l1 != NULL_TREE && l2 != NULL_TREE)
1391 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1392 return false;
1394 l1 = TREE_CHAIN (l1);
1395 l2 = TREE_CHAIN (l2);
1398 return l1 == l2;
1401 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1402 the same. */
1404 static bool
1405 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1407 tree cl1, cl2;
1408 for (cl1 = clauses1, cl2 = clauses2;
1409 cl1 && cl2;
1410 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1412 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1413 return false;
1414 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1416 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1417 OMP_CLAUSE_DECL (cl2)) != 1)
1418 return false;
1420 switch (OMP_CLAUSE_CODE (cl1))
1422 case OMP_CLAUSE_ALIGNED:
1423 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1424 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1425 return false;
1426 break;
1427 case OMP_CLAUSE_LINEAR:
1428 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1429 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1430 return false;
1431 break;
1432 case OMP_CLAUSE_SIMDLEN:
1433 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1434 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1435 return false;
1436 default:
1437 break;
1440 return true;
1444 /* Compare two attributes for their value identity. Return true if the
1445 attribute values are known to be equal; otherwise return false. */
1447 bool
1448 attribute_value_equal (const_tree attr1, const_tree attr2)
1450 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1451 return true;
1453 if (TREE_VALUE (attr1) != NULL_TREE
1454 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1455 && TREE_VALUE (attr2) != NULL_TREE
1456 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1458 /* Handle attribute format. */
1459 if (is_attribute_p ("format", get_attribute_name (attr1)))
1461 attr1 = TREE_VALUE (attr1);
1462 attr2 = TREE_VALUE (attr2);
1463 /* Compare the archetypes (printf/scanf/strftime/...). */
1464 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1465 return false;
1466 /* Archetypes are the same. Compare the rest. */
1467 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1468 TREE_CHAIN (attr2)) == 1);
1470 return (simple_cst_list_equal (TREE_VALUE (attr1),
1471 TREE_VALUE (attr2)) == 1);
1474 if (TREE_VALUE (attr1)
1475 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
1476 && TREE_VALUE (attr2)
1477 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1478 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1479 TREE_VALUE (attr2));
1481 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1484 /* Return 0 if the attributes for two types are incompatible, 1 if they
1485 are compatible, and 2 if they are nearly compatible (which causes a
1486 warning to be generated). */
1488 comp_type_attributes (const_tree type1, const_tree type2)
1490 const_tree a1 = TYPE_ATTRIBUTES (type1);
1491 const_tree a2 = TYPE_ATTRIBUTES (type2);
1492 const_tree a;
1494 if (a1 == a2)
1495 return 1;
1496 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1498 const struct attribute_spec *as;
1499 const_tree attr;
1501 as = lookup_attribute_spec (TREE_PURPOSE (a));
1502 if (!as || as->affects_type_identity == false)
1503 continue;
1505 attr = find_same_attribute (a, CONST_CAST_TREE (a2));
1506 if (!attr || !attribute_value_equal (a, attr))
1507 break;
1509 if (!a)
1511 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1513 const struct attribute_spec *as;
1515 as = lookup_attribute_spec (TREE_PURPOSE (a));
1516 if (!as || as->affects_type_identity == false)
1517 continue;
1519 if (!find_same_attribute (a, CONST_CAST_TREE (a1)))
1520 break;
1521 /* We don't need to compare trees again, as we did this
1522 already in first loop. */
1524 /* All types - affecting identity - are equal, so
1525 there is no need to call target hook for comparison. */
1526 if (!a)
1527 return 1;
1529 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1530 return 0;
1531 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1532 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1533 return 0;
1534 int strub_ret = strub_comptypes (CONST_CAST_TREE (type1),
1535 CONST_CAST_TREE (type2));
1536 if (strub_ret == 0)
1537 return strub_ret;
1538 /* As some type combinations - like default calling-convention - might
1539 be compatible, we have to call the target hook to get the final result. */
1540 int target_ret = targetm.comp_type_attributes (type1, type2);
1541 if (target_ret == 0)
1542 return target_ret;
1543 if (strub_ret == 2 || target_ret == 2)
1544 return 2;
1545 if (strub_ret == 1 && target_ret == 1)
1546 return 1;
1547 gcc_unreachable ();
1550 /* PREDICATE acts as a function of type:
1552 (const_tree attr, const attribute_spec *as) -> bool
1554 where ATTR is an attribute and AS is its possibly-null specification.
1555 Return a list of every attribute in attribute list ATTRS for which
1556 PREDICATE is true. Return ATTRS itself if PREDICATE returns true
1557 for every attribute. */
1559 template<typename Predicate>
1560 tree
1561 remove_attributes_matching (tree attrs, Predicate predicate)
1563 tree new_attrs = NULL_TREE;
1564 tree *ptr = &new_attrs;
1565 const_tree start = attrs;
1566 for (const_tree attr = attrs; attr; attr = TREE_CHAIN (attr))
1568 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
1569 const_tree end;
1570 if (!predicate (attr, as))
1571 end = attr;
1572 else if (start == attrs)
1573 continue;
1574 else
1575 end = TREE_CHAIN (attr);
1577 for (; start != end; start = TREE_CHAIN (start))
1579 *ptr = tree_cons (TREE_PURPOSE (start),
1580 TREE_VALUE (start), NULL_TREE);
1581 TREE_CHAIN (*ptr) = NULL_TREE;
1582 ptr = &TREE_CHAIN (*ptr);
1584 start = TREE_CHAIN (attr);
1586 gcc_assert (!start || start == attrs);
1587 return start ? attrs : new_attrs;
1590 /* If VALUE is true, return the subset of ATTRS that affect type identity,
1591 otherwise return the subset of ATTRS that don't affect type identity. */
1593 tree
1594 affects_type_identity_attributes (tree attrs, bool value)
1596 auto predicate = [value](const_tree, const attribute_spec *as) -> bool
1598 return bool (as && as->affects_type_identity) == value;
1600 return remove_attributes_matching (attrs, predicate);
1603 /* Remove attributes that affect type identity from ATTRS unless the
1604 same attributes occur in OK_ATTRS. */
1606 tree
1607 restrict_type_identity_attributes_to (tree attrs, tree ok_attrs)
1609 auto predicate = [ok_attrs](const_tree attr,
1610 const attribute_spec *as) -> bool
1612 if (!as || !as->affects_type_identity)
1613 return true;
1615 for (tree ok_attr = lookup_attribute (as->name, ok_attrs);
1616 ok_attr;
1617 ok_attr = lookup_attribute (as->name, TREE_CHAIN (ok_attr)))
1618 if (simple_cst_equal (TREE_VALUE (ok_attr), TREE_VALUE (attr)) == 1)
1619 return true;
1621 return false;
1623 return remove_attributes_matching (attrs, predicate);
1626 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1627 is ATTRIBUTE.
1629 Record such modified types already made so we don't make duplicates. */
1631 tree
1632 build_type_attribute_variant (tree ttype, tree attribute)
1634 return build_type_attribute_qual_variant (ttype, attribute,
1635 TYPE_QUALS (ttype));
1638 /* A variant of lookup_attribute() that can be used with an identifier
1639 as the first argument, and where the identifier can be either
1640 'text' or '__text__'.
1642 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1643 return a pointer to the attribute's list element if the attribute
1644 is part of the list, or NULL_TREE if not found. If the attribute
1645 appears more than once, this only returns the first occurrence; the
1646 TREE_CHAIN of the return value should be passed back in if further
1647 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1648 can be in the form 'text' or '__text__'. */
1649 static tree
1650 lookup_ident_attribute (tree attr_identifier, tree list)
1652 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1654 while (list)
1656 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1657 == IDENTIFIER_NODE);
1659 if (cmp_attrib_identifiers (attr_identifier,
1660 get_attribute_name (list)))
1661 /* Found it. */
1662 break;
1663 list = TREE_CHAIN (list);
1666 return list;
1669 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1670 modified list. */
1672 tree
1673 remove_attribute (const char *attr_name, tree list)
1675 tree *p;
1676 gcc_checking_assert (attr_name[0] != '_');
1678 for (p = &list; *p;)
1680 tree l = *p;
1682 tree attr = get_attribute_name (l);
1683 if (is_attribute_p (attr_name, attr))
1684 *p = TREE_CHAIN (l);
1685 else
1686 p = &TREE_CHAIN (l);
1689 return list;
1692 /* Similarly but also match namespace on the removed attributes.
1693 ATTR_NS "" stands for NULL or "gnu" namespace. */
1695 tree
1696 remove_attribute (const char *attr_ns, const char *attr_name, tree list)
1698 tree *p;
1699 gcc_checking_assert (attr_name[0] != '_');
1700 gcc_checking_assert (attr_ns == NULL || attr_ns[0] != '_');
1702 for (p = &list; *p;)
1704 tree l = *p;
1706 tree attr = get_attribute_name (l);
1707 if (is_attribute_p (attr_name, attr)
1708 && is_attribute_namespace_p (attr_ns, l))
1710 *p = TREE_CHAIN (l);
1711 continue;
1713 p = &TREE_CHAIN (l);
1716 return list;
1719 /* Return an attribute list that is the union of a1 and a2. */
1721 tree
1722 merge_attributes (tree a1, tree a2)
1724 tree attributes;
1726 /* Either one unset? Take the set one. */
1728 if ((attributes = a1) == 0)
1729 attributes = a2;
1731 /* One that completely contains the other? Take it. */
1733 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1735 if (attribute_list_contained (a2, a1))
1736 attributes = a2;
1737 else
1739 /* Pick the longest list, and hang on the other list. */
1741 if (list_length (a1) < list_length (a2))
1742 attributes = a2, a2 = a1;
1744 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1746 tree a;
1747 for (a = lookup_ident_attribute (get_attribute_name (a2),
1748 attributes);
1749 a != NULL_TREE && !attribute_value_equal (a, a2);
1750 a = lookup_ident_attribute (get_attribute_name (a2),
1751 TREE_CHAIN (a)))
1753 if (a == NULL_TREE)
1755 a1 = copy_node (a2);
1756 TREE_CHAIN (a1) = attributes;
1757 attributes = a1;
1762 return attributes;
1765 /* Given types T1 and T2, merge their attributes and return
1766 the result. */
1768 tree
1769 merge_type_attributes (tree t1, tree t2)
1771 return merge_attributes (TYPE_ATTRIBUTES (t1),
1772 TYPE_ATTRIBUTES (t2));
1775 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1776 the result. */
1778 tree
1779 merge_decl_attributes (tree olddecl, tree newdecl)
1781 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1782 DECL_ATTRIBUTES (newdecl));
1785 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1786 they are missing there. */
1788 void
1789 duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1791 attr = lookup_attribute (name, attr);
1792 if (!attr)
1793 return;
1794 tree a = lookup_attribute (name, *attrs);
1795 while (attr)
1797 tree a2;
1798 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1799 if (attribute_value_equal (attr, a2))
1800 break;
1801 if (!a2)
1803 a2 = copy_node (attr);
1804 TREE_CHAIN (a2) = *attrs;
1805 *attrs = a2;
1807 attr = lookup_attribute (name, TREE_CHAIN (attr));
1811 /* Duplicate all attributes from user DECL to the corresponding
1812 builtin that should be propagated. */
1814 void
1815 copy_attributes_to_builtin (tree decl)
1817 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1818 if (b)
1819 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1820 DECL_ATTRIBUTES (decl), "omp declare simd");
1823 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1825 /* Specialization of merge_decl_attributes for various Windows targets.
1827 This handles the following situation:
1829 __declspec (dllimport) int foo;
1830 int foo;
1832 The second instance of `foo' nullifies the dllimport. */
1834 tree
1835 merge_dllimport_decl_attributes (tree old, tree new_tree)
1837 tree a;
1838 int delete_dllimport_p = 1;
1840 /* What we need to do here is remove from `old' dllimport if it doesn't
1841 appear in `new'. dllimport behaves like extern: if a declaration is
1842 marked dllimport and a definition appears later, then the object
1843 is not dllimport'd. We also remove a `new' dllimport if the old list
1844 contains dllexport: dllexport always overrides dllimport, regardless
1845 of the order of declaration. */
1846 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1847 delete_dllimport_p = 0;
1848 else if (DECL_DLLIMPORT_P (new_tree)
1849 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1851 DECL_DLLIMPORT_P (new_tree) = 0;
1852 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1853 "attribute: dllimport ignored", new_tree);
1855 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1857 /* Warn about overriding a symbol that has already been used, e.g.:
1858 extern int __attribute__ ((dllimport)) foo;
1859 int* bar () {return &foo;}
1860 int foo;
1862 if (TREE_USED (old))
1864 warning (0, "%q+D redeclared without dllimport attribute "
1865 "after being referenced with dll linkage", new_tree);
1866 /* If we have used a variable's address with dllimport linkage,
1867 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1868 decl may already have had TREE_CONSTANT computed.
1869 We still remove the attribute so that assembler code refers
1870 to '&foo rather than '_imp__foo'. */
1871 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1872 DECL_DLLIMPORT_P (new_tree) = 1;
1875 /* Let an inline definition silently override the external reference,
1876 but otherwise warn about attribute inconsistency. */
1877 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1878 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1879 "attribute: previous dllimport ignored", new_tree);
1881 else
1882 delete_dllimport_p = 0;
1884 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1886 if (delete_dllimport_p)
1887 a = remove_attribute ("dllimport", a);
1889 return a;
1892 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1893 struct attribute_spec.handler. */
1895 tree
1896 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1897 bool *no_add_attrs)
1899 tree node = *pnode;
1900 bool is_dllimport;
1902 /* These attributes may apply to structure and union types being created,
1903 but otherwise should pass to the declaration involved. */
1904 if (!DECL_P (node))
1906 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1907 | (int) ATTR_FLAG_ARRAY_NEXT))
1909 *no_add_attrs = true;
1910 return tree_cons (name, args, NULL_TREE);
1912 if (TREE_CODE (node) == RECORD_TYPE
1913 || TREE_CODE (node) == UNION_TYPE)
1915 node = TYPE_NAME (node);
1916 if (!node)
1917 return NULL_TREE;
1919 else
1921 warning (OPT_Wattributes, "%qE attribute ignored",
1922 name);
1923 *no_add_attrs = true;
1924 return NULL_TREE;
1928 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1930 *no_add_attrs = true;
1931 warning (OPT_Wattributes, "%qE attribute ignored",
1932 name);
1933 return NULL_TREE;
1936 if (TREE_CODE (node) == TYPE_DECL
1937 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1938 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1940 *no_add_attrs = true;
1941 warning (OPT_Wattributes, "%qE attribute ignored",
1942 name);
1943 return NULL_TREE;
1946 is_dllimport = is_attribute_p ("dllimport", name);
1948 /* Report error on dllimport ambiguities seen now before they cause
1949 any damage. */
1950 if (is_dllimport)
1952 /* Honor any target-specific overrides. */
1953 if (!targetm.valid_dllimport_attribute_p (node))
1954 *no_add_attrs = true;
1956 else if (TREE_CODE (node) == FUNCTION_DECL
1957 && DECL_DECLARED_INLINE_P (node))
1959 warning (OPT_Wattributes, "inline function %q+D declared as "
1960 "dllimport: attribute ignored", node);
1961 *no_add_attrs = true;
1963 /* Like MS, treat definition of dllimported variables and
1964 non-inlined functions on declaration as syntax errors. */
1965 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1967 error ("function %q+D definition is marked dllimport", node);
1968 *no_add_attrs = true;
1971 else if (VAR_P (node))
1973 if (DECL_INITIAL (node))
1975 error ("variable %q+D definition is marked dllimport",
1976 node);
1977 *no_add_attrs = true;
1980 /* `extern' needn't be specified with dllimport.
1981 Specify `extern' now and hope for the best. Sigh. */
1982 DECL_EXTERNAL (node) = 1;
1983 /* Also, implicitly give dllimport'd variables declared within
1984 a function global scope, unless declared static. */
1985 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1986 TREE_PUBLIC (node) = 1;
1987 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1988 it is a C++ static data member. */
1989 if (DECL_CONTEXT (node) == NULL_TREE
1990 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node)))
1991 TREE_STATIC (node) = 0;
1994 if (*no_add_attrs == false)
1995 DECL_DLLIMPORT_P (node) = 1;
1997 else if (TREE_CODE (node) == FUNCTION_DECL
1998 && DECL_DECLARED_INLINE_P (node)
1999 && flag_keep_inline_dllexport)
2000 /* An exported function, even if inline, must be emitted. */
2001 DECL_EXTERNAL (node) = 0;
2003 /* Report error if symbol is not accessible at global scope. */
2004 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
2006 error ("external linkage required for symbol %q+D because of "
2007 "%qE attribute", node, name);
2008 *no_add_attrs = true;
2011 /* A dllexport'd entity must have default visibility so that other
2012 program units (shared libraries or the main executable) can see
2013 it. A dllimport'd entity must have default visibility so that
2014 the linker knows that undefined references within this program
2015 unit can be resolved by the dynamic linker. */
2016 if (!*no_add_attrs)
2018 if (DECL_VISIBILITY_SPECIFIED (node)
2019 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
2020 error ("%qE implies default visibility, but %qD has already "
2021 "been declared with a different visibility",
2022 name, node);
2023 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
2024 DECL_VISIBILITY_SPECIFIED (node) = 1;
2027 return NULL_TREE;
2030 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2032 /* Given two lists of attributes, return true if list l2 is
2033 equivalent to l1. */
2036 attribute_list_equal (const_tree l1, const_tree l2)
2038 if (l1 == l2)
2039 return 1;
2041 return attribute_list_contained (l1, l2)
2042 && attribute_list_contained (l2, l1);
2045 /* Given two lists of attributes, return true if list L2 is
2046 completely contained within L1. */
2047 /* ??? This would be faster if attribute names were stored in a canonicalized
2048 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
2049 must be used to show these elements are equivalent (which they are). */
2050 /* ??? It's not clear that attributes with arguments will always be handled
2051 correctly. */
2054 attribute_list_contained (const_tree l1, const_tree l2)
2056 const_tree t1, t2;
2058 /* First check the obvious, maybe the lists are identical. */
2059 if (l1 == l2)
2060 return 1;
2062 /* Maybe the lists are similar. */
2063 for (t1 = l1, t2 = l2;
2064 t1 != 0 && t2 != 0
2065 && get_attribute_name (t1) == get_attribute_name (t2)
2066 && TREE_VALUE (t1) == TREE_VALUE (t2);
2067 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2070 /* Maybe the lists are equal. */
2071 if (t1 == 0 && t2 == 0)
2072 return 1;
2074 for (; t2 != 0; t2 = TREE_CHAIN (t2))
2076 const_tree attr;
2077 /* This CONST_CAST is okay because lookup_attribute does not
2078 modify its argument and the return value is assigned to a
2079 const_tree. */
2080 for (attr = lookup_ident_attribute (get_attribute_name (t2),
2081 CONST_CAST_TREE (l1));
2082 attr != NULL_TREE && !attribute_value_equal (t2, attr);
2083 attr = lookup_ident_attribute (get_attribute_name (t2),
2084 TREE_CHAIN (attr)))
2087 if (attr == NULL_TREE)
2088 return 0;
2091 return 1;
2094 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
2095 of ATTR_NAME, and LIST is not NULL_TREE.
2097 The function is called from lookup_attribute in order to optimize
2098 for size. */
2100 tree
2101 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
2103 while (list)
2105 tree attr = get_attribute_name (list);
2106 size_t ident_len = IDENTIFIER_LENGTH (attr);
2107 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2108 ident_len))
2109 break;
2110 list = TREE_CHAIN (list);
2113 return list;
2116 /* Similarly but with also attribute namespace. */
2118 tree
2119 private_lookup_attribute (const char *attr_ns, const char *attr_name,
2120 size_t attr_ns_len, size_t attr_len, tree list)
2122 while (list)
2124 tree attr = get_attribute_name (list);
2125 size_t ident_len = IDENTIFIER_LENGTH (attr);
2126 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2127 ident_len))
2129 tree ns = get_attribute_namespace (list);
2130 if (ns == NULL_TREE)
2132 if (attr_ns_len == 0)
2133 break;
2135 else if (attr_ns)
2137 ident_len = IDENTIFIER_LENGTH (ns);
2138 if (attr_ns_len == 0)
2140 if (cmp_attribs ("gnu", strlen ("gnu"),
2141 IDENTIFIER_POINTER (ns), ident_len))
2142 break;
2144 else if (cmp_attribs (attr_ns, attr_ns_len,
2145 IDENTIFIER_POINTER (ns), ident_len))
2146 break;
2149 list = TREE_CHAIN (list);
2152 return list;
2155 /* Return true if the function decl or type NODE has been declared
2156 with attribute ANAME among attributes ATTRS. */
2158 static bool
2159 has_attribute (tree node, tree attrs, const char *aname)
2161 if (!strcmp (aname, "const"))
2163 if (DECL_P (node) && TREE_READONLY (node))
2164 return true;
2166 else if (!strcmp (aname, "malloc"))
2168 if (DECL_P (node) && DECL_IS_MALLOC (node))
2169 return true;
2171 else if (!strcmp (aname, "noreturn"))
2173 if (DECL_P (node) && TREE_THIS_VOLATILE (node))
2174 return true;
2176 else if (!strcmp (aname, "nothrow"))
2178 if (TREE_NOTHROW (node))
2179 return true;
2181 else if (!strcmp (aname, "pure"))
2183 if (DECL_P (node) && DECL_PURE_P (node))
2184 return true;
2187 return lookup_attribute (aname, attrs);
2190 /* Return the number of mismatched function or type attributes between
2191 the "template" function declaration TMPL and DECL. The word "template"
2192 doesn't necessarily refer to a C++ template but rather a declaration
2193 whose attributes should be matched by those on DECL. For a non-zero
2194 return value set *ATTRSTR to a string representation of the list of
2195 mismatched attributes with quoted names.
2196 ATTRLIST is a list of additional attributes that SPEC should be
2197 taken to ultimately be declared with. */
2199 unsigned
2200 decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
2201 const char* const blacklist[],
2202 pretty_printer *attrstr)
2204 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2205 return 0;
2207 /* Avoid warning if either declaration or its type is deprecated. */
2208 if (TREE_DEPRECATED (tmpl)
2209 || TREE_DEPRECATED (decl))
2210 return 0;
2212 const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
2213 const tree decls[] = { decl, TREE_TYPE (decl) };
2215 if (TREE_DEPRECATED (tmpls[1])
2216 || TREE_DEPRECATED (decls[1])
2217 || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
2218 || TREE_DEPRECATED (TREE_TYPE (decls[1])))
2219 return 0;
2221 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
2222 tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
2224 if (!decl_attrs[0])
2225 decl_attrs[0] = attrlist;
2226 else if (!decl_attrs[1])
2227 decl_attrs[1] = attrlist;
2229 /* Avoid warning if the template has no attributes. */
2230 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2231 return 0;
2233 /* Avoid warning if either declaration contains an attribute on
2234 the white list below. */
2235 const char* const whitelist[] = {
2236 "error", "warning"
2239 for (unsigned i = 0; i != 2; ++i)
2240 for (unsigned j = 0; j != ARRAY_SIZE (whitelist); ++j)
2241 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2242 || lookup_attribute (whitelist[j], decl_attrs[i]))
2243 return 0;
2245 /* Put together a list of the black-listed attributes that the template
2246 is declared with and the declaration is not, in case it's not apparent
2247 from the most recent declaration of the template. */
2248 unsigned nattrs = 0;
2250 for (unsigned i = 0; blacklist[i]; ++i)
2252 /* Attribute leaf only applies to extern functions. Avoid mentioning
2253 it when it's missing from a static declaration. */
2254 if (!TREE_PUBLIC (decl)
2255 && !strcmp ("leaf", blacklist[i]))
2256 continue;
2258 for (unsigned j = 0; j != 2; ++j)
2260 if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
2261 continue;
2263 bool found = false;
2264 unsigned kmax = 1 + !!decl_attrs[1];
2265 for (unsigned k = 0; k != kmax; ++k)
2267 if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
2269 found = true;
2270 break;
2274 if (!found)
2276 if (nattrs)
2277 pp_string (attrstr, ", ");
2278 pp_begin_quote (attrstr, pp_show_color (global_dc->printer));
2279 pp_string (attrstr, blacklist[i]);
2280 pp_end_quote (attrstr, pp_show_color (global_dc->printer));
2281 ++nattrs;
2284 break;
2288 return nattrs;
2291 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
2292 specifies either attributes that are incompatible with those of
2293 TARGET, or attributes that are missing and that declaring ALIAS
2294 with would benefit. */
2296 void
2297 maybe_diag_alias_attributes (tree alias, tree target)
2299 /* Do not expect attributes to match between aliases and ifunc
2300 resolvers. There is no obvious correspondence between them. */
2301 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
2302 return;
2304 const char* const blacklist[] = {
2305 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
2306 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
2307 "returns_twice", NULL
2310 pretty_printer attrnames;
2311 if (warn_attribute_alias > 1)
2313 /* With -Wattribute-alias=2 detect alias declarations that are more
2314 restrictive than their targets first. Those indicate potential
2315 codegen bugs. */
2316 if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
2317 blacklist, &attrnames))
2319 auto_diagnostic_group d;
2320 if (warning_n (DECL_SOURCE_LOCATION (alias),
2321 OPT_Wattribute_alias_, n,
2322 "%qD specifies more restrictive attribute than "
2323 "its target %qD: %s",
2324 "%qD specifies more restrictive attributes than "
2325 "its target %qD: %s",
2326 alias, target, pp_formatted_text (&attrnames)))
2327 inform (DECL_SOURCE_LOCATION (target),
2328 "%qD target declared here", alias);
2329 return;
2333 /* Detect alias declarations that are less restrictive than their
2334 targets. Those suggest potential optimization opportunities
2335 (solved by adding the missing attribute(s) to the alias). */
2336 if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
2337 blacklist, &attrnames))
2339 auto_diagnostic_group d;
2340 if (warning_n (DECL_SOURCE_LOCATION (alias),
2341 OPT_Wmissing_attributes, n,
2342 "%qD specifies less restrictive attribute than "
2343 "its target %qD: %s",
2344 "%qD specifies less restrictive attributes than "
2345 "its target %qD: %s",
2346 alias, target, pp_formatted_text (&attrnames)))
2347 inform (DECL_SOURCE_LOCATION (target),
2348 "%qD target declared here", alias);
2352 /* Initialize a mapping RWM for a call to a function declared with
2353 attribute access in ATTRS. Each attribute positional operand
2354 inserts one entry into the mapping with the operand number as
2355 the key. */
2357 void
2358 init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
2360 if (!attrs)
2361 return;
2363 for (tree access = attrs;
2364 (access = lookup_attribute ("access", access));
2365 access = TREE_CHAIN (access))
2367 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2368 is the attribute argument's value. */
2369 tree mode = TREE_VALUE (access);
2370 if (!mode)
2371 return;
2373 /* The (optional) list of VLA bounds. */
2374 tree vblist = TREE_CHAIN (mode);
2375 mode = TREE_VALUE (mode);
2376 if (TREE_CODE (mode) != STRING_CST)
2377 continue;
2378 gcc_assert (TREE_CODE (mode) == STRING_CST);
2380 if (vblist)
2381 vblist = nreverse (copy_list (TREE_VALUE (vblist)));
2383 for (const char *m = TREE_STRING_POINTER (mode); *m; )
2385 attr_access acc = { };
2387 /* Skip the internal-only plus sign. */
2388 if (*m == '+')
2389 ++m;
2391 acc.str = m;
2392 acc.mode = acc.from_mode_char (*m);
2393 acc.sizarg = UINT_MAX;
2395 const char *end;
2396 acc.ptrarg = strtoul (++m, const_cast<char**>(&end), 10);
2397 m = end;
2399 if (*m == '[')
2401 /* Forms containing the square bracket are internal-only
2402 (not specified by an attribute declaration), and used
2403 for various forms of array and VLA parameters. */
2404 acc.internal_p = true;
2406 /* Search to the closing bracket and look at the preceding
2407 code: it determines the form of the most significant
2408 bound of the array. Others prior to it encode the form
2409 of interior VLA bounds. They're not of interest here. */
2410 end = strchr (m, ']');
2411 const char *p = end;
2412 gcc_assert (p);
2414 while (ISDIGIT (p[-1]))
2415 --p;
2417 if (ISDIGIT (*p))
2419 /* A digit denotes a constant bound (as in T[3]). */
2420 acc.static_p = p[-1] == 's';
2421 acc.minsize = strtoull (p, NULL, 10);
2423 else if (' ' == p[-1])
2425 /* A space denotes an ordinary array of unspecified bound
2426 (as in T[]). */
2427 acc.minsize = 0;
2429 else if ('*' == p[-1] || '$' == p[-1])
2431 /* An asterisk denotes a VLA. When the closing bracket
2432 is followed by a comma and a dollar sign its bound is
2433 on the list. Otherwise it's a VLA with an unspecified
2434 bound. */
2435 acc.static_p = p[-2] == 's';
2436 acc.minsize = HOST_WIDE_INT_M1U;
2439 m = end + 1;
2442 if (*m == ',')
2444 ++m;
2447 if (*m == '$')
2449 ++m;
2450 if (!acc.size && vblist)
2452 /* Extract the list of VLA bounds for the current
2453 parameter, store it in ACC.SIZE, and advance
2454 to the list of bounds for the next VLA parameter.
2456 acc.size = TREE_VALUE (vblist);
2457 vblist = TREE_CHAIN (vblist);
2461 if (ISDIGIT (*m))
2463 /* Extract the positional argument. It's absent
2464 for VLAs whose bound doesn't name a function
2465 parameter. */
2466 unsigned pos = strtoul (m, const_cast<char**>(&end), 10);
2467 if (acc.sizarg == UINT_MAX)
2468 acc.sizarg = pos;
2469 m = end;
2472 while (*m == '$');
2475 acc.end = m;
2477 bool existing;
2478 auto &ref = rwm->get_or_insert (acc.ptrarg, &existing);
2479 if (existing)
2481 /* Merge the new spec with the existing. */
2482 if (acc.minsize == HOST_WIDE_INT_M1U)
2483 ref.minsize = HOST_WIDE_INT_M1U;
2485 if (acc.sizarg != UINT_MAX)
2486 ref.sizarg = acc.sizarg;
2488 if (acc.mode)
2489 ref.mode = acc.mode;
2491 else
2492 ref = acc;
2494 /* Unconditionally add an entry for the required pointer
2495 operand of the attribute, and one for the optional size
2496 operand when it's specified. */
2497 if (acc.sizarg != UINT_MAX)
2498 rwm->put (acc.sizarg, acc);
2503 /* Return the access specification for a function parameter PARM
2504 or null if the current function has no such specification. */
2506 attr_access *
2507 get_parm_access (rdwr_map &rdwr_idx, tree parm,
2508 tree fndecl /* = current_function_decl */)
2510 tree fntype = TREE_TYPE (fndecl);
2511 init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
2513 if (rdwr_idx.is_empty ())
2514 return NULL;
2516 unsigned argpos = 0;
2517 tree fnargs = DECL_ARGUMENTS (fndecl);
2518 for (tree arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
2519 if (arg == parm)
2520 return rdwr_idx.get (argpos);
2522 return NULL;
2525 /* Return the internal representation as STRING_CST. Internal positional
2526 arguments are zero-based. */
2528 tree
2529 attr_access::to_internal_string () const
2531 return build_string (end - str, str);
2534 /* Return the human-readable representation of the external attribute
2535 specification (as it might appear in the source code) as STRING_CST.
2536 External positional arguments are one-based. */
2538 tree
2539 attr_access::to_external_string () const
2541 char buf[80];
2542 gcc_assert (mode != access_deferred);
2543 int len = snprintf (buf, sizeof buf, "access (%s, %u",
2544 mode_names[mode], ptrarg + 1);
2545 if (sizarg != UINT_MAX)
2546 len += snprintf (buf + len, sizeof buf - len, ", %u", sizarg + 1);
2547 strcpy (buf + len, ")");
2548 return build_string (len + 2, buf);
2551 /* Return the number of specified VLA bounds and set *nunspec to
2552 the number of unspecified ones (those designated by [*]). */
2554 unsigned
2555 attr_access::vla_bounds (unsigned *nunspec) const
2557 unsigned nbounds = 0;
2558 *nunspec = 0;
2559 /* STR points to the beginning of the specified string for the current
2560 argument that may be followed by the string for the next argument. */
2561 for (const char* p = strchr (str, ']'); p && *p != '['; --p)
2563 if (*p == '*')
2564 ++*nunspec;
2565 else if (*p == '$')
2566 ++nbounds;
2568 return nbounds;
2571 /* Reset front end-specific attribute access data from ATTRS.
2572 Called from the free_lang_data pass. */
2574 /* static */ void
2575 attr_access::free_lang_data (tree attrs)
2577 for (tree acs = attrs; (acs = lookup_attribute ("access", acs));
2578 acs = TREE_CHAIN (acs))
2580 tree vblist = TREE_VALUE (acs);
2581 vblist = TREE_CHAIN (vblist);
2582 if (!vblist)
2583 continue;
2585 for (vblist = TREE_VALUE (vblist); vblist; vblist = TREE_CHAIN (vblist))
2587 tree *pvbnd = &TREE_VALUE (vblist);
2588 if (!*pvbnd || DECL_P (*pvbnd))
2589 continue;
2591 /* VLA bounds that are expressions as opposed to DECLs are
2592 only used in the front end. Reset them to keep front end
2593 trees leaking into the middle end (see pr97172) and to
2594 free up memory. */
2595 *pvbnd = NULL_TREE;
2599 for (tree argspec = attrs; (argspec = lookup_attribute ("arg spec", argspec));
2600 argspec = TREE_CHAIN (argspec))
2602 /* Same as above. */
2603 tree *pvblist = &TREE_VALUE (argspec);
2604 *pvblist = NULL_TREE;
2608 /* Defined in attr_access. */
2609 constexpr char attr_access::mode_chars[];
2610 constexpr char attr_access::mode_names[][11];
2612 /* Format an array, including a VLA, pointed to by TYPE and used as
2613 a function parameter as a human-readable string. ACC describes
2614 an access to the parameter and is used to determine the outermost
2615 form of the array including its bound which is otherwise obviated
2616 by its decay to pointer. Return the formatted string. */
2618 std::string
2619 attr_access::array_as_string (tree type) const
2621 std::string typstr;
2623 if (type == error_mark_node)
2624 return std::string ();
2626 if (this->str)
2628 /* For array parameters (but not pointers) create a temporary array
2629 type that corresponds to the form of the parameter including its
2630 qualifiers even though they apply to the pointer, not the array
2631 type. */
2632 const bool vla_p = minsize == HOST_WIDE_INT_M1U;
2633 tree eltype = TREE_TYPE (type);
2634 tree index_type = NULL_TREE;
2636 if (minsize == HOST_WIDE_INT_M1U)
2638 /* Determine if this is a VLA (an array whose most significant
2639 bound is nonconstant and whose access string has "$]" in it)
2640 extract the bound expression from SIZE. */
2641 const char *p = end;
2642 for ( ; p != str && *p-- != ']'; );
2643 if (*p == '$')
2644 /* SIZE may have been cleared. Use it with care. */
2645 index_type = build_index_type (size ? TREE_VALUE (size) : size);
2647 else if (minsize)
2648 index_type = build_index_type (size_int (minsize - 1));
2650 tree arat = NULL_TREE;
2651 if (static_p || vla_p)
2653 tree flag = static_p ? integer_one_node : NULL_TREE;
2654 /* Hack: there's no language-independent way to encode
2655 the "static" specifier or the "*" notation in an array type.
2656 Add a "fake" attribute to have the pretty-printer add "static"
2657 or "*". The "[static N]" notation is only valid in the most
2658 significant bound but [*] can be used for any bound. Because
2659 [*] is represented the same as [0] this hack only works for
2660 the most significant bound like static and the others are
2661 rendered as [0]. */
2662 arat = build_tree_list (get_identifier ("array"), flag);
2665 const int quals = TYPE_QUALS (type);
2666 type = build_array_type (eltype, index_type);
2667 type = build_type_attribute_qual_variant (type, arat, quals);
2670 /* Format the type using the current pretty printer. The generic tree
2671 printer does a terrible job. */
2672 pretty_printer *pp = global_dc->printer->clone ();
2673 pp_printf (pp, "%qT", type);
2674 typstr = pp_formatted_text (pp);
2675 delete pp;
2677 return typstr;
2680 #if CHECKING_P
2682 namespace selftest
2685 /* Self-test to verify that each attribute exclusion is symmetric,
2686 meaning that if attribute A is encoded as incompatible with
2687 attribute B then the opposite relationship is also encoded.
2688 This test also detects most cases of misspelled attribute names
2689 in exclusions. */
2691 static void
2692 test_attribute_exclusions ()
2694 using excl_hash_traits = pair_hash<nofree_string_hash, nofree_string_hash>;
2696 /* Iterate over the array of attribute tables first (with TI0 as
2697 the index) and over the array of attribute_spec in each table
2698 (with SI0 as the index). */
2699 hash_set<excl_hash_traits> excl_set;
2701 for (auto scoped_array : attribute_tables)
2702 for (auto scoped_attributes : scoped_array)
2703 for (const attribute_spec &attribute : scoped_attributes->attributes)
2705 const attribute_spec::exclusions *excl = attribute.exclude;
2707 /* Skip each attribute that doesn't define exclusions. */
2708 if (!excl)
2709 continue;
2711 /* Skip standard (non-GNU) attributes, since currently the
2712 exclusions are implicitly for GNU attributes only.
2713 Also, C++ likely and unlikely get rewritten to gnu::hot
2714 and gnu::cold, so symmetry isn't necessary there. */
2715 if (!scoped_attributes->ns)
2716 continue;
2718 const char *attr_name = attribute.name;
2720 /* Iterate over the set of exclusions for every attribute
2721 (with EI0 as the index) adding the exclusions defined
2722 for each to the set. */
2723 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2725 const char *excl_name = excl[ei0].name;
2727 if (!strcmp (attr_name, excl_name))
2728 continue;
2730 excl_set.add ({ attr_name, excl_name });
2734 /* Traverse the set of mutually exclusive pairs of attributes
2735 and verify that they are symmetric. */
2736 for (auto excl_pair : excl_set)
2737 if (!excl_set.contains ({ excl_pair.second, excl_pair.first }))
2739 /* An exclusion for an attribute has been found that
2740 doesn't have a corresponding exclusion in the opposite
2741 direction. */
2742 char desc[120];
2743 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2744 excl_pair.first, excl_pair.second);
2745 fail (SELFTEST_LOCATION, desc);
2749 void
2750 attribs_cc_tests ()
2752 test_attribute_exclusions ();
2755 } /* namespace selftest */
2757 #endif /* CHECKING_P */
2759 #include "gt-attribs.h"