Fortran: Fix PR 47485.
[official-gcc.git] / gcc / attribs.cc
blob56dd18c2fa8ea2b3ed8da50837a53775790427e8
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2025 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 "pretty-print-markup.h"
39 #include "tree-pretty-print.h"
40 #include "intl.h"
41 #include "gcc-urlifier.h"
43 /* Table of the tables of attributes (common, language, format, machine)
44 searched. */
45 static array_slice<const scoped_attribute_specs *const> attribute_tables[2];
47 /* Substring representation. */
49 struct substring
51 const char *str;
52 int length;
55 /* Simple hash function to avoid need to scan whole string. */
57 static inline hashval_t
58 substring_hash (const char *str, int l)
60 return str[0] + str[l - 1] * 256 + l * 65536;
63 /* Used for attribute_hash. */
65 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
67 typedef substring *compare_type;
68 static inline hashval_t hash (const attribute_spec *);
69 static inline bool equal (const attribute_spec *, const substring *);
72 inline hashval_t
73 attribute_hasher::hash (const attribute_spec *spec)
75 const int l = strlen (spec->name);
76 return substring_hash (spec->name, l);
79 inline bool
80 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
82 return (strncmp (spec->name, str->str, str->length) == 0
83 && !spec->name[str->length]);
86 /* Scoped attribute name representation. */
88 struct scoped_attributes
90 const char *ns;
91 vec<attribute_spec> attributes;
92 hash_table<attribute_hasher> *attribute_hash;
93 /* True if we should not warn about unknown attributes in this NS. */
94 bool ignored_p;
97 /* The table of scope attributes. */
98 static vec<scoped_attributes> attributes_table;
100 static scoped_attributes* find_attribute_namespace (const char*);
101 static void register_scoped_attribute (const struct attribute_spec *,
102 scoped_attributes *);
103 static const struct attribute_spec *lookup_scoped_attribute_spec (const_tree,
104 const_tree);
106 static bool attributes_initialized = false;
108 /* Do not use directly; go through get_gnu_namespace instead. */
109 static GTY(()) tree gnu_namespace_cache;
111 /* Return the IDENTIFIER_NODE for the gnu namespace. */
113 static tree
114 get_gnu_namespace ()
116 if (!gnu_namespace_cache)
117 gnu_namespace_cache = get_identifier ("gnu");
118 return gnu_namespace_cache;
121 /* Insert SPECS into its namespace. IGNORED_P is true iff all unknown
122 attributes in this namespace should be ignored for the purposes of
123 -Wattributes. The function returns the namespace into which the
124 attributes have been registered. */
126 scoped_attributes *
127 register_scoped_attributes (const scoped_attribute_specs &specs,
128 bool ignored_p /*=false*/)
130 scoped_attributes *result = NULL;
132 /* See if we already have attributes in the namespace NS. */
133 result = find_attribute_namespace (specs.ns);
135 if (result == NULL)
137 /* We don't have any namespace NS yet. Create one. */
138 scoped_attributes sa;
140 if (attributes_table.is_empty ())
141 attributes_table.create (64);
143 memset (&sa, 0, sizeof (sa));
144 sa.ns = specs.ns;
145 sa.attributes.create (64);
146 sa.ignored_p = ignored_p;
147 result = attributes_table.safe_push (sa);
148 result->attribute_hash = new hash_table<attribute_hasher> (200);
150 else
151 result->ignored_p |= ignored_p;
153 /* Really add the attributes to their namespace now. */
154 for (const attribute_spec &attribute : specs.attributes)
156 result->attributes.safe_push (attribute);
157 register_scoped_attribute (&attribute, result);
160 gcc_assert (result != NULL);
162 return result;
165 /* Return the namespace which name is NS, NULL if none exist. */
167 static scoped_attributes*
168 find_attribute_namespace (const char* ns)
170 for (scoped_attributes &iter : attributes_table)
171 if (ns == iter.ns
172 || (iter.ns != NULL
173 && ns != NULL
174 && !strcmp (iter.ns, ns)))
175 return &iter;
176 return NULL;
179 /* Make some sanity checks on the attribute tables. */
181 static void
182 check_attribute_tables (void)
184 hash_set<pair_hash<nofree_string_hash, nofree_string_hash>> names;
186 for (auto scoped_array : attribute_tables)
187 for (auto scoped_attributes : scoped_array)
188 for (const attribute_spec &attribute : scoped_attributes->attributes)
190 /* The name must not begin and end with __. */
191 const char *name = attribute.name;
192 int len = strlen (name);
194 gcc_assert (!(name[0] == '_' && name[1] == '_'
195 && name[len - 1] == '_' && name[len - 2] == '_'));
197 /* The minimum and maximum lengths must be consistent. */
198 gcc_assert (attribute.min_length >= 0);
200 gcc_assert (attribute.max_length == -1
201 || attribute.max_length >= attribute.min_length);
203 /* An attribute cannot require both a DECL and a TYPE. */
204 gcc_assert (!attribute.decl_required
205 || !attribute.type_required);
207 /* If an attribute requires a function type, in particular
208 it requires a type. */
209 gcc_assert (!attribute.function_type_required
210 || attribute.type_required);
212 /* Check that no name occurs more than once. Names that
213 begin with '*' are exempt, and may be overridden. */
214 const char *ns = scoped_attributes->ns;
215 if (name[0] != '*' && names.add ({ ns ? ns : "", name }))
216 gcc_unreachable ();
220 /* Used to stash pointers to allocated memory so that we can free them at
221 the end of parsing of all TUs. */
222 static vec<attribute_spec *> ignored_attributes_table;
224 /* Parse arguments V of -Wno-attributes=.
225 Currently we accept:
226 vendor::attr
227 vendor::
228 This functions also registers the parsed attributes so that we don't
229 warn that we don't recognize them. */
231 void
232 handle_ignored_attributes_option (vec<char *> *v)
234 if (v == nullptr)
235 return;
237 for (auto opt : v)
239 char *cln = strstr (opt, "::");
240 /* We don't accept '::attr'. */
241 if (cln == nullptr || cln == opt)
243 auto_diagnostic_group d;
244 error ("wrong argument to ignored attributes");
245 inform (input_location, "valid format is %<ns::attr%> or %<ns::%>");
246 continue;
248 const char *vendor_start = opt;
249 ptrdiff_t vendor_len = cln - opt;
250 const char *attr_start = cln + 2;
251 /* This could really use rawmemchr :(. */
252 ptrdiff_t attr_len = strchr (attr_start, '\0') - attr_start;
253 /* Verify that they look valid. */
254 auto valid_p = [](const char *const s, ptrdiff_t len) {
255 bool ok = false;
257 for (int i = 0; i < len; ++i)
258 if (ISALNUM (s[i]))
259 ok = true;
260 else if (s[i] != '_')
261 return false;
263 return ok;
265 if (!valid_p (vendor_start, vendor_len))
267 error ("wrong argument to ignored attributes");
268 continue;
270 canonicalize_attr_name (vendor_start, vendor_len);
271 /* We perform all this hijinks so that we don't have to copy OPT. */
272 tree vendor_id = get_identifier_with_length (vendor_start, vendor_len);
273 array_slice<const attribute_spec> attrs;
274 /* In the "vendor::" case, we should ignore *any* attribute coming
275 from this attribute namespace. */
276 if (attr_len > 0)
278 if (!valid_p (attr_start, attr_len))
280 error ("wrong argument to ignored attributes");
281 continue;
283 canonicalize_attr_name (attr_start, attr_len);
284 tree attr_id = get_identifier_with_length (attr_start, attr_len);
285 const char *attr = IDENTIFIER_POINTER (attr_id);
286 /* If we've already seen this vendor::attr, ignore it. Attempting to
287 register it twice would lead to a crash. */
288 if (lookup_scoped_attribute_spec (vendor_id, attr_id))
289 continue;
290 /* Create a table with extra attributes which we will register.
291 We can't free it here, so squirrel away the pointers. */
292 attribute_spec *table = new attribute_spec {
293 attr, 0, -2, false, false, false, false, nullptr, nullptr
295 ignored_attributes_table.safe_push (table);
296 attrs = { table, 1 };
298 const scoped_attribute_specs scoped_specs = {
299 IDENTIFIER_POINTER (vendor_id), { attrs }
301 register_scoped_attributes (scoped_specs, attrs.empty ());
305 /* Free data we might have allocated when adding extra attributes. */
307 void
308 free_attr_data ()
310 for (auto x : ignored_attributes_table)
311 delete x;
312 ignored_attributes_table.release ();
315 /* Initialize attribute tables, and make some sanity checks if checking is
316 enabled. */
318 void
319 init_attributes (void)
321 if (attributes_initialized)
322 return;
324 attribute_tables[0] = lang_hooks.attribute_table;
325 attribute_tables[1] = targetm.attribute_table;
327 if (flag_checking)
328 check_attribute_tables ();
330 for (auto scoped_array : attribute_tables)
331 for (auto scoped_attributes : scoped_array)
332 register_scoped_attributes (*scoped_attributes);
334 vec<char *> *ignored = (vec<char *> *) flag_ignored_attributes;
335 handle_ignored_attributes_option (ignored);
337 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
338 attributes_initialized = true;
341 /* Insert a single ATTR into the attribute table. */
343 void
344 register_attribute (const struct attribute_spec *attr)
346 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
349 /* Insert a single attribute ATTR into a namespace of attributes. */
351 static void
352 register_scoped_attribute (const struct attribute_spec *attr,
353 scoped_attributes *name_space)
355 struct substring str;
356 attribute_spec **slot;
358 gcc_assert (attr != NULL && name_space != NULL);
360 gcc_assert (name_space->attribute_hash);
362 str.str = attr->name;
363 str.length = strlen (str.str);
365 /* Attribute names in the table must be in the form 'text' and not
366 in the form '__text__'. */
367 gcc_checking_assert (!canonicalize_attr_name (str.str, str.length));
369 slot = name_space->attribute_hash
370 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
371 INSERT);
372 gcc_assert (!*slot || attr->name[0] == '*');
373 *slot = CONST_CAST (struct attribute_spec *, attr);
376 /* Return the spec for the scoped attribute with namespace NS and
377 name NAME. */
379 static const struct attribute_spec *
380 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
382 struct substring attr;
383 scoped_attributes *attrs;
385 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns) : NULL;
387 attrs = find_attribute_namespace (ns_str);
389 if (attrs == NULL)
390 return NULL;
392 attr.str = IDENTIFIER_POINTER (name);
393 attr.length = IDENTIFIER_LENGTH (name);
394 return attrs->attribute_hash->find_with_hash (&attr,
395 substring_hash (attr.str,
396 attr.length));
399 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
400 it also specifies the attribute namespace. */
402 const struct attribute_spec *
403 lookup_attribute_spec (const_tree name)
405 tree ns;
406 if (TREE_CODE (name) == TREE_LIST)
408 ns = TREE_PURPOSE (name);
409 name = TREE_VALUE (name);
411 else
412 ns = get_gnu_namespace ();
413 return lookup_scoped_attribute_spec (ns, name);
417 /* Return the namespace of the attribute ATTR. This accessor works on
418 GNU and C++11 (scoped) attributes. On GNU attributes,
419 it returns an identifier tree for the string "gnu".
421 Please read the comments of cxx11_attribute_p to understand the
422 format of attributes. */
424 tree
425 get_attribute_namespace (const_tree attr)
427 if (cxx11_attribute_p (attr))
428 return TREE_PURPOSE (TREE_PURPOSE (attr));
429 return get_gnu_namespace ();
432 /* Check LAST_DECL and NODE of the same symbol for attributes that are
433 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
434 them, and return true if any have been found. NODE can be a DECL
435 or a TYPE. */
437 static bool
438 diag_attr_exclusions (tree last_decl, tree node, tree attrname,
439 const attribute_spec *spec)
441 const attribute_spec::exclusions *excl = spec->exclude;
443 tree_code code = TREE_CODE (node);
445 if ((code == FUNCTION_DECL && !excl->function
446 && (!excl->type || !spec->affects_type_identity))
447 || (code == VAR_DECL && !excl->variable
448 && (!excl->type || !spec->affects_type_identity))
449 || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
450 return false;
452 /* True if an attribute that's mutually exclusive with ATTRNAME
453 has been found. */
454 bool found = false;
456 if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
458 /* Check both the last DECL and its type for conflicts with
459 the attribute being added to the current decl or type. */
460 found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
461 tree decl_type = TREE_TYPE (last_decl);
462 found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
465 /* NODE is either the current DECL to which the attribute is being
466 applied or its TYPE. For the former, consider the attributes on
467 both the DECL and its type. */
468 tree attrs[2];
470 if (DECL_P (node))
472 attrs[0] = DECL_ATTRIBUTES (node);
473 if (TREE_TYPE (node))
474 attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
475 else
476 /* TREE_TYPE can be NULL e.g. while processing attributes on
477 enumerators. */
478 attrs[1] = NULL_TREE;
480 else
482 attrs[0] = TYPE_ATTRIBUTES (node);
483 attrs[1] = NULL_TREE;
486 /* Iterate over the mutually exclusive attribute names and verify
487 that the symbol doesn't contain it. */
488 for (unsigned i = 0; i != ARRAY_SIZE (attrs); ++i)
490 if (!attrs[i])
491 continue;
493 for ( ; excl->name; ++excl)
495 /* Avoid checking the attribute against itself. */
496 if (is_attribute_p (excl->name, attrname))
497 continue;
499 if (!lookup_attribute (excl->name, attrs[i]))
500 continue;
502 /* An exclusion may apply either to a function declaration,
503 type declaration, or a field/variable declaration, or
504 any subset of the three. */
505 if (TREE_CODE (node) == FUNCTION_DECL
506 && !excl->function)
507 continue;
509 if (TREE_CODE (node) == TYPE_DECL
510 && !excl->type)
511 continue;
513 if ((TREE_CODE (node) == FIELD_DECL
514 || VAR_P (node))
515 && !excl->variable)
516 continue;
518 found = true;
520 /* Print a note? */
521 bool note = last_decl != NULL_TREE;
522 auto_diagnostic_group d;
523 if (TREE_CODE (node) == FUNCTION_DECL
524 && fndecl_built_in_p (node))
525 note &= warning (OPT_Wattributes,
526 "ignoring attribute %qE in declaration of "
527 "a built-in function %qD because it conflicts "
528 "with attribute %qs",
529 attrname, node, excl->name);
530 else
531 note &= warning (OPT_Wattributes,
532 "ignoring attribute %qE because "
533 "it conflicts with attribute %qs",
534 attrname, excl->name);
536 if (note)
537 inform (DECL_SOURCE_LOCATION (last_decl),
538 "previous declaration here");
542 return found;
545 /* Return true iff we should not complain about unknown attributes
546 coming from the attribute namespace NS. This is the case for
547 the -Wno-attributes=ns:: command-line option. */
549 static bool
550 attr_namespace_ignored_p (tree ns)
552 if (ns == NULL_TREE)
553 return false;
554 scoped_attributes *r = find_attribute_namespace (IDENTIFIER_POINTER (ns));
555 return r && r->ignored_p;
558 /* Return true if the attribute ATTR should not be warned about. */
560 bool
561 attribute_ignored_p (tree attr)
563 if (!cxx11_attribute_p (attr))
564 return false;
565 if (tree ns = get_attribute_namespace (attr))
567 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
568 if (as == NULL && attr_namespace_ignored_p (ns))
569 return true;
570 if (as && as->max_length == -2)
571 return true;
573 return false;
576 /* Like above, but takes an attribute_spec AS, which must be nonnull. */
578 bool
579 attribute_ignored_p (const attribute_spec *const as)
581 return as->max_length == -2;
584 /* Return true if the ATTRS chain contains at least one attribute which
585 is not ignored. */
587 bool
588 any_nonignored_attribute_p (tree attrs)
590 for (tree attr = attrs; attr; attr = TREE_CHAIN (attr))
591 if (!attribute_ignored_p (attr))
592 return true;
594 return false;
597 /* See whether LIST contains at least one instance of attribute ATTR
598 (possibly with different arguments). Return the first such attribute
599 if so, otherwise return null. */
601 static tree
602 find_same_attribute (const_tree attr, tree list)
604 if (list == NULL_TREE)
605 return NULL_TREE;
606 tree ns = get_attribute_namespace (attr);
607 tree name = get_attribute_name (attr);
608 return private_lookup_attribute (ns ? IDENTIFIER_POINTER (ns) : nullptr,
609 IDENTIFIER_POINTER (name),
610 ns ? IDENTIFIER_LENGTH (ns) : 0,
611 IDENTIFIER_LENGTH (name), list);
614 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
615 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
616 it should be modified in place; if a TYPE, a copy should be created
617 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
618 information, in the form of a bitwise OR of flags in enum attribute_flags
619 from tree.h. Depending on these flags, some attributes may be
620 returned to be applied at a later stage (for example, to apply
621 a decl attribute to the declaration rather than to its type). */
623 tree
624 decl_attributes (tree *node, tree attributes, int flags,
625 tree last_decl /* = NULL_TREE */)
627 tree returned_attrs = NULL_TREE;
629 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
630 return NULL_TREE;
632 if (!attributes_initialized)
633 init_attributes ();
635 auto_urlify_attributes sentinel;
637 /* If this is a function and the user used #pragma GCC optimize, add the
638 options to the attribute((optimize(...))) list. */
639 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
641 tree cur_attr = lookup_attribute ("optimize", attributes);
642 tree opts = copy_list (current_optimize_pragma);
644 if (! cur_attr)
645 attributes
646 = tree_cons (get_identifier ("optimize"), opts, attributes);
647 else
648 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
651 if (TREE_CODE (*node) == FUNCTION_DECL
652 && (optimization_current_node != optimization_default_node
653 || target_option_current_node != target_option_default_node)
654 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
656 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
657 /* Don't set DECL_FUNCTION_SPECIFIC_TARGET for targets that don't
658 support #pragma GCC target or target attribute. */
659 if (target_option_default_node)
661 tree cur_tree
662 = build_target_option_node (&global_options, &global_options_set);
663 tree old_tree = DECL_FUNCTION_SPECIFIC_TARGET (*node);
664 if (!old_tree)
665 old_tree = target_option_default_node;
666 /* The changes on optimization options can cause the changes in
667 target options, update it accordingly if it's changed. */
668 if (old_tree != cur_tree)
669 DECL_FUNCTION_SPECIFIC_TARGET (*node) = cur_tree;
673 /* If this is a function and the user used #pragma GCC target, add the
674 options to the attribute((target(...))) list. */
675 if (TREE_CODE (*node) == FUNCTION_DECL
676 && current_target_pragma
677 && targetm.target_option.valid_attribute_p (*node,
678 get_identifier ("target"),
679 current_target_pragma, 0))
681 tree cur_attr = lookup_attribute ("target", attributes);
682 tree opts = copy_list (current_target_pragma);
684 if (! cur_attr)
685 attributes = tree_cons (get_identifier ("target"), opts, attributes);
686 else
687 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
690 /* A "naked" function attribute implies "noinline" and "noclone" for
691 those targets that support it. */
692 if (TREE_CODE (*node) == FUNCTION_DECL
693 && attributes
694 && lookup_attribute ("naked", attributes) != NULL
695 && lookup_attribute_spec (get_identifier ("naked"))
696 && lookup_attribute ("noipa", attributes) == NULL)
697 attributes = tree_cons (get_identifier ("noipa"), NULL, attributes);
699 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
700 for those targets that support it. */
701 if (TREE_CODE (*node) == FUNCTION_DECL
702 && attributes
703 && lookup_attribute ("noipa", attributes) != NULL
704 && lookup_attribute_spec (get_identifier ("noipa")))
706 if (lookup_attribute ("noinline", attributes) == NULL)
707 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
709 if (lookup_attribute ("noclone", attributes) == NULL)
710 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
712 if (lookup_attribute ("no_icf", attributes) == NULL)
713 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
716 targetm.insert_attributes (*node, &attributes);
718 /* Note that attributes on the same declaration are not necessarily
719 in the same order as in the source. */
720 for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
722 tree ns = get_attribute_namespace (attr);
723 tree name = get_attribute_name (attr);
724 tree args = TREE_VALUE (attr);
725 tree *anode = node;
726 const struct attribute_spec *spec
727 = lookup_scoped_attribute_spec (ns, name);
728 int fn_ptr_quals = 0;
729 tree fn_ptr_tmp = NULL_TREE;
730 const bool cxx11_attr_p = cxx11_attribute_p (attr);
732 if (spec == NULL)
734 if (!(flags & (int) ATTR_FLAG_BUILT_IN)
735 && !attr_namespace_ignored_p (ns))
737 if (ns == NULL_TREE || !cxx11_attr_p)
738 warning (OPT_Wattributes, "%qE attribute directive ignored",
739 name);
740 else if ((flag_openmp || flag_openmp_simd)
741 && is_attribute_p ("omp", ns)
742 && is_attribute_p ("directive", name)
743 && (VAR_P (*node)
744 || TREE_CODE (*node) == FUNCTION_DECL))
745 continue;
746 else
747 warning (OPT_Wattributes,
748 "%<%E::%E%> scoped attribute directive ignored",
749 ns, name);
751 continue;
753 else
755 int nargs = list_length (args);
756 if (nargs < spec->min_length
757 || (spec->max_length >= 0
758 && nargs > spec->max_length))
760 auto_diagnostic_group d;
761 error ("wrong number of arguments specified for %qE attribute",
762 name);
763 if (spec->max_length < 0)
764 inform (input_location, "expected %i or more, found %i",
765 spec->min_length, nargs);
766 else if (spec->min_length == spec->max_length)
767 inform (input_location, "expected %i, found %i",
768 spec->min_length, nargs);
769 else
770 inform (input_location, "expected between %i and %i, found %i",
771 spec->min_length, spec->max_length, nargs);
772 continue;
775 gcc_assert (is_attribute_p (spec->name, name));
777 if (spec->decl_required && !DECL_P (*anode))
779 if (flags & ((int) ATTR_FLAG_DECL_NEXT
780 | (int) ATTR_FLAG_FUNCTION_NEXT
781 | (int) ATTR_FLAG_ARRAY_NEXT))
783 /* Pass on this attribute to be tried again. */
784 tree attr = tree_cons (name, args, NULL_TREE);
785 returned_attrs = chainon (returned_attrs, attr);
786 continue;
788 else
790 warning (OPT_Wattributes, "%qE attribute does not apply to types",
791 name);
792 continue;
796 /* If we require a type, but were passed a decl, set up to make a
797 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
798 would have applied if we'd been passed a type, but we cannot modify
799 the decl's type in place here. */
800 if (spec->type_required && DECL_P (*anode))
802 anode = &TREE_TYPE (*anode);
803 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
806 if (spec->function_type_required
807 && !FUNC_OR_METHOD_TYPE_P (*anode))
809 if (TREE_CODE (*anode) == POINTER_TYPE
810 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
812 /* OK, this is a bit convoluted. We can't just make a copy
813 of the pointer type and modify its TREE_TYPE, because if
814 we change the attributes of the target type the pointer
815 type needs to have a different TYPE_MAIN_VARIANT. So we
816 pull out the target type now, frob it as appropriate, and
817 rebuild the pointer type later.
819 This would all be simpler if attributes were part of the
820 declarator, grumble grumble. */
821 fn_ptr_tmp = TREE_TYPE (*anode);
822 fn_ptr_quals = TYPE_QUALS (*anode);
823 anode = &fn_ptr_tmp;
824 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
826 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
828 /* Pass on this attribute to be tried again. */
829 tree attr = tree_cons (name, args, NULL_TREE);
830 returned_attrs = chainon (returned_attrs, attr);
831 continue;
834 if (TREE_CODE (*anode) != FUNCTION_TYPE
835 && TREE_CODE (*anode) != METHOD_TYPE)
837 warning (OPT_Wattributes,
838 "%qE attribute only applies to function types",
839 name);
840 continue;
844 if (TYPE_P (*anode)
845 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
846 && COMPLETE_TYPE_P (*anode))
848 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
849 continue;
852 bool no_add_attrs = false;
854 /* Check for exclusions with other attributes on the current
855 declation as well as the last declaration of the same
856 symbol already processed (if one exists). Detect and
857 reject incompatible attributes. */
858 bool built_in = flags & ATTR_FLAG_BUILT_IN;
859 if (spec->exclude
860 && (flag_checking || !built_in)
861 && !error_operand_p (last_decl))
863 /* Always check attributes on user-defined functions.
864 Check them on built-ins only when -fchecking is set.
865 Ignore __builtin_unreachable -- it's both const and
866 noreturn. */
868 if (!built_in
869 || !DECL_P (*anode)
870 || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
871 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
872 && DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE_TRAP
873 && (DECL_FUNCTION_CODE (*anode)
874 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
876 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
877 if (!no_add && anode != node)
878 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
879 no_add_attrs |= no_add;
883 if (no_add_attrs
884 /* Don't add attributes registered just for -Wno-attributes=foo::bar
885 purposes. */
886 || attribute_ignored_p (attr))
887 continue;
889 if (spec->handler != NULL)
891 int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0);
893 /* Pass in an array of the current declaration followed
894 by the last pushed/merged declaration if one exists.
895 For calls that modify the type attributes of a DECL
896 and for which *ANODE is *NODE's type, also pass in
897 the DECL as the third element to use in diagnostics.
898 If the handler changes CUR_AND_LAST_DECL[0] replace
899 *ANODE with its value. */
900 tree cur_and_last_decl[3] = { *anode, last_decl };
901 if (anode != node && DECL_P (*node))
902 cur_and_last_decl[2] = *node;
904 tree ret = (spec->handler) (cur_and_last_decl, name, args,
905 flags|cxx11_flag, &no_add_attrs);
907 /* Fix up typedefs clobbered by attribute handlers. */
908 if (TREE_CODE (*node) == TYPE_DECL
909 && anode == &TREE_TYPE (*node)
910 && DECL_ORIGINAL_TYPE (*node)
911 && TYPE_NAME (*anode) == *node
912 && TYPE_NAME (cur_and_last_decl[0]) != *node)
914 tree t = cur_and_last_decl[0];
915 DECL_ORIGINAL_TYPE (*node) = t;
916 tree tt = build_variant_type_copy (t);
917 cur_and_last_decl[0] = tt;
918 TREE_TYPE (*node) = tt;
919 TYPE_NAME (tt) = *node;
922 if (*anode != cur_and_last_decl[0])
924 /* Even if !spec->function_type_required, allow the attribute
925 handler to request the attribute to be applied to the function
926 type, rather than to the function pointer type, by setting
927 cur_and_last_decl[0] to the function type. */
928 if (!fn_ptr_tmp
929 && POINTER_TYPE_P (*anode)
930 && TREE_TYPE (*anode) == cur_and_last_decl[0]
931 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
933 fn_ptr_tmp = TREE_TYPE (*anode);
934 fn_ptr_quals = TYPE_QUALS (*anode);
935 anode = &fn_ptr_tmp;
937 *anode = cur_and_last_decl[0];
940 if (ret == error_mark_node)
942 warning (OPT_Wattributes, "%qE attribute ignored", name);
943 no_add_attrs = true;
945 else
946 returned_attrs = chainon (ret, returned_attrs);
949 /* Layout the decl in case anything changed. */
950 if (spec->type_required && DECL_P (*node)
951 && (VAR_P (*node)
952 || TREE_CODE (*node) == PARM_DECL
953 || TREE_CODE (*node) == RESULT_DECL))
954 relayout_decl (*node);
956 if (!no_add_attrs)
958 tree old_attrs;
959 tree a;
961 if (DECL_P (*anode))
962 old_attrs = DECL_ATTRIBUTES (*anode);
963 else
964 old_attrs = TYPE_ATTRIBUTES (*anode);
966 for (a = find_same_attribute (attr, old_attrs);
967 a != NULL_TREE;
968 a = find_same_attribute (attr, TREE_CHAIN (a)))
970 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
971 break;
974 if (a == NULL_TREE)
976 /* This attribute isn't already in the list. */
977 tree r;
978 /* Preserve the C++11 form. */
979 if (cxx11_attr_p)
980 r = tree_cons (build_tree_list (ns, name), args, old_attrs);
981 else
982 r = tree_cons (name, args, old_attrs);
984 if (DECL_P (*anode))
985 DECL_ATTRIBUTES (*anode) = r;
986 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
988 TYPE_ATTRIBUTES (*anode) = r;
989 /* If this is the main variant, also push the attributes
990 out to the other variants. */
991 if (*anode == TYPE_MAIN_VARIANT (*anode))
993 for (tree variant = *anode; variant;
994 variant = TYPE_NEXT_VARIANT (variant))
996 if (TYPE_ATTRIBUTES (variant) == old_attrs)
997 TYPE_ATTRIBUTES (variant)
998 = TYPE_ATTRIBUTES (*anode);
999 else if (!find_same_attribute
1000 (attr, TYPE_ATTRIBUTES (variant)))
1001 TYPE_ATTRIBUTES (variant) = tree_cons
1002 (name, args, TYPE_ATTRIBUTES (variant));
1006 else
1007 *anode = build_type_attribute_variant (*anode, r);
1011 if (fn_ptr_tmp)
1013 /* Rebuild the function pointer type and put it in the
1014 appropriate place. */
1015 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
1016 if (fn_ptr_quals)
1017 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
1018 if (DECL_P (*node))
1019 TREE_TYPE (*node) = fn_ptr_tmp;
1020 else
1022 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
1023 *node = fn_ptr_tmp;
1028 return returned_attrs;
1031 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
1032 attribute.
1034 When G++ parses a C++11 attribute, it is represented as
1035 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
1036 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
1037 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
1038 use get_attribute_namespace and get_attribute_name to retrieve the
1039 namespace and name of the attribute, as these accessors work with
1040 GNU attributes as well. */
1042 bool
1043 cxx11_attribute_p (const_tree attr)
1045 if (attr == NULL_TREE
1046 || TREE_CODE (attr) != TREE_LIST)
1047 return false;
1049 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
1052 /* Return the name of the attribute ATTR. This accessor works on GNU
1053 and C++11 (scoped) attributes.
1055 Please read the comments of cxx11_attribute_p to understand the
1056 format of attributes. */
1058 tree
1059 get_attribute_name (const_tree attr)
1061 if (cxx11_attribute_p (attr))
1062 return TREE_VALUE (TREE_PURPOSE (attr));
1063 return TREE_PURPOSE (attr);
1066 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
1067 to the method FNDECL. */
1069 void
1070 apply_tm_attr (tree fndecl, tree attr)
1072 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
1075 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
1076 it to CHAIN. */
1078 tree
1079 make_attribute (const char *name, const char *arg_name, tree chain)
1081 tree attr_name;
1082 tree attr_arg_name;
1083 tree attr_args;
1084 tree attr;
1086 attr_name = get_identifier (name);
1087 attr_arg_name = build_string (strlen (arg_name), arg_name);
1088 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
1089 attr = tree_cons (attr_name, attr_args, chain);
1090 return attr;
1094 /* Common functions used for target clone support. */
1096 /* Comparator function to be used in qsort routine to sort attribute
1097 specification strings to "target". */
1099 static int
1100 attr_strcmp (const void *v1, const void *v2)
1102 const char *c1 = *(char *const*)v1;
1103 const char *c2 = *(char *const*)v2;
1104 return strcmp (c1, c2);
1107 /* ARGLIST is the argument to target attribute. This function tokenizes
1108 the TARGET_CLONES_ATTR_SEPARATOR separated arguments, sorts them and
1109 returns a string which is a unique identifier for the
1110 TARGET_CLONES_ATTR_SEPARATOR separated arguments. It also replaces
1111 non-identifier characters "=,-" with "_". */
1113 char *
1114 sorted_attr_string (tree arglist)
1116 tree arg;
1117 size_t str_len_sum = 0;
1118 char **args = NULL;
1119 char *attr_str, *ret_str;
1120 char *attr = NULL;
1121 unsigned int argnum = 1;
1122 unsigned int i;
1123 static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 };
1125 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1127 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1128 size_t len = strlen (str);
1129 str_len_sum += len + 1;
1130 if (arg != arglist)
1131 argnum++;
1132 for (i = 0; i < strlen (str); i++)
1133 if (str[i] == TARGET_CLONES_ATTR_SEPARATOR)
1134 argnum++;
1137 attr_str = XNEWVEC (char, str_len_sum);
1138 str_len_sum = 0;
1139 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1141 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1142 size_t len = strlen (str);
1143 memcpy (attr_str + str_len_sum, str, len);
1144 attr_str[str_len_sum + len]
1145 = TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0';
1146 str_len_sum += len + 1;
1149 /* Replace "=,-" with "_". */
1150 for (i = 0; i < strlen (attr_str); i++)
1151 if (attr_str[i] == '=' || attr_str[i]== '-')
1152 attr_str[i] = '_';
1154 if (argnum == 1)
1155 return attr_str;
1157 args = XNEWVEC (char *, argnum);
1159 i = 0;
1160 attr = strtok (attr_str, separator_str);
1161 while (attr != NULL)
1163 args[i] = attr;
1164 i++;
1165 attr = strtok (NULL, separator_str);
1168 qsort (args, argnum, sizeof (char *), attr_strcmp);
1170 ret_str = XNEWVEC (char, str_len_sum);
1171 str_len_sum = 0;
1172 for (i = 0; i < argnum; i++)
1174 size_t len = strlen (args[i]);
1175 memcpy (ret_str + str_len_sum, args[i], len);
1176 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
1177 str_len_sum += len + 1;
1180 XDELETEVEC (args);
1181 XDELETEVEC (attr_str);
1182 return ret_str;
1186 /* This function returns true if FN1 and FN2 are versions of the same function,
1187 that is, the target strings of the function decls are different. This assumes
1188 that FN1 and FN2 have the same signature. */
1190 bool
1191 common_function_versions (tree fn1, tree fn2)
1193 tree attr1, attr2;
1194 char *target1, *target2;
1195 bool result;
1197 if (TREE_CODE (fn1) != FUNCTION_DECL
1198 || TREE_CODE (fn2) != FUNCTION_DECL)
1199 return false;
1201 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
1202 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
1204 /* At least one function decl should have the target attribute specified. */
1205 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
1206 return false;
1208 /* Diagnose missing target attribute if one of the decls is already
1209 multi-versioned. */
1210 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
1212 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
1214 if (attr2 != NULL_TREE)
1216 std::swap (fn1, fn2);
1217 attr1 = attr2;
1219 auto_diagnostic_group d;
1220 error_at (DECL_SOURCE_LOCATION (fn2),
1221 "missing %<target%> attribute for multi-versioned %qD",
1222 fn2);
1223 inform (DECL_SOURCE_LOCATION (fn1),
1224 "previous declaration of %qD", fn1);
1225 /* Prevent diagnosing of the same error multiple times. */
1226 DECL_ATTRIBUTES (fn2)
1227 = tree_cons (get_identifier ("target"),
1228 copy_node (TREE_VALUE (attr1)),
1229 DECL_ATTRIBUTES (fn2));
1231 return false;
1234 target1 = sorted_attr_string (TREE_VALUE (attr1));
1235 target2 = sorted_attr_string (TREE_VALUE (attr2));
1237 /* The sorted target strings must be different for fn1 and fn2
1238 to be versions. */
1239 if (strcmp (target1, target2) == 0)
1240 result = false;
1241 else
1242 result = true;
1244 XDELETEVEC (target1);
1245 XDELETEVEC (target2);
1247 return result;
1250 /* Make a dispatcher declaration for the multi-versioned function DECL.
1251 Calls to DECL function will be replaced with calls to the dispatcher
1252 by the front-end. Return the decl created. */
1254 tree
1255 make_dispatcher_decl (const tree decl)
1257 tree func_decl;
1258 char *func_name;
1259 tree fn_type, func_type;
1261 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1263 fn_type = TREE_TYPE (decl);
1264 func_type = build_function_type (TREE_TYPE (fn_type),
1265 TYPE_ARG_TYPES (fn_type));
1267 func_decl = build_fn_decl (func_name, func_type);
1268 XDELETEVEC (func_name);
1269 TREE_USED (func_decl) = 1;
1270 DECL_CONTEXT (func_decl) = NULL_TREE;
1271 DECL_INITIAL (func_decl) = error_mark_node;
1272 DECL_ARTIFICIAL (func_decl) = 1;
1273 /* Mark this func as external, the resolver will flip it again if
1274 it gets generated. */
1275 DECL_EXTERNAL (func_decl) = 1;
1276 /* This will be of type IFUNCs have to be externally visible. */
1277 TREE_PUBLIC (func_decl) = 1;
1279 return func_decl;
1282 /* Returns true if DECL is multi-versioned using the target attribute, and this
1283 is the default version. This function can only be used for targets that do
1284 not support the "target_version" attribute. */
1286 bool
1287 is_function_default_version (const tree decl)
1289 if (TREE_CODE (decl) != FUNCTION_DECL
1290 || !DECL_FUNCTION_VERSIONED (decl))
1291 return false;
1292 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1293 gcc_assert (attr);
1294 attr = TREE_VALUE (TREE_VALUE (attr));
1295 return (TREE_CODE (attr) == STRING_CST
1296 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1299 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1300 is ATTRIBUTE. */
1302 tree
1303 build_decl_attribute_variant (tree ddecl, tree attribute)
1305 DECL_ATTRIBUTES (ddecl) = attribute;
1306 return ddecl;
1309 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1310 is ATTRIBUTE and its qualifiers are QUALS.
1312 Record such modified types already made so we don't make duplicates. */
1314 tree
1315 build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
1317 tree ttype = otype;
1318 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1320 tree ntype;
1322 /* Building a distinct copy of a tagged type is inappropriate; it
1323 causes breakage in code that expects there to be a one-to-one
1324 relationship between a struct and its fields.
1325 build_duplicate_type is another solution (as used in
1326 handle_transparent_union_attribute), but that doesn't play well
1327 with the stronger C++ type identity model. */
1328 if (RECORD_OR_UNION_TYPE_P (ttype)
1329 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1331 warning (OPT_Wattributes,
1332 "ignoring attributes applied to %qT after definition",
1333 TYPE_MAIN_VARIANT (ttype));
1334 return build_qualified_type (ttype, quals);
1337 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
1338 if (lang_hooks.types.copy_lang_qualifiers
1339 && otype != TYPE_MAIN_VARIANT (otype))
1340 ttype = (lang_hooks.types.copy_lang_qualifiers
1341 (ttype, TYPE_MAIN_VARIANT (otype)));
1343 tree dtype = ntype = build_distinct_type_copy (ttype);
1345 TYPE_ATTRIBUTES (ntype) = attribute;
1346 /* If the target-dependent attributes make NTYPE different from
1347 its canonical type, we will need to use structural equality
1348 checks for this type.
1350 We shouldn't get here for stripping attributes from a type;
1351 the no-attribute type might not need structural comparison. But
1352 we can if was discarded from type_hash_table. */
1353 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1354 || !comp_type_attributes (ntype, ttype))
1355 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
1357 hashval_t hash = type_hash_canon_hash (ntype);
1358 ntype = type_hash_canon (hash, ntype);
1360 if (ntype != dtype)
1361 /* This variant was already in the hash table, don't mess with
1362 TYPE_CANONICAL. */;
1363 else if (TYPE_CANONICAL (ntype) == ntype)
1364 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1366 ttype = build_qualified_type (ntype, quals);
1367 if (lang_hooks.types.copy_lang_qualifiers
1368 && otype != TYPE_MAIN_VARIANT (otype))
1369 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
1371 else if (TYPE_QUALS (ttype) != quals)
1372 ttype = build_qualified_type (ttype, quals);
1374 return ttype;
1377 /* Compare two identifier nodes representing attributes.
1378 Return true if they are the same, false otherwise. */
1380 static bool
1381 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1383 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1384 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1385 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1387 /* Identifiers can be compared directly for equality. */
1388 if (attr1 == attr2)
1389 return true;
1391 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1392 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1395 /* Compare two constructor-element-type constants. Return 1 if the lists
1396 are known to be equal; otherwise return 0. */
1398 bool
1399 simple_cst_list_equal (const_tree l1, const_tree l2)
1401 while (l1 != NULL_TREE && l2 != NULL_TREE)
1403 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1404 return false;
1406 l1 = TREE_CHAIN (l1);
1407 l2 = TREE_CHAIN (l2);
1410 return l1 == l2;
1413 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1414 the same. */
1416 static bool
1417 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1419 tree cl1, cl2;
1420 for (cl1 = clauses1, cl2 = clauses2;
1421 cl1 && cl2;
1422 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1424 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1425 return false;
1426 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1428 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1429 OMP_CLAUSE_DECL (cl2)) != 1)
1430 return false;
1432 switch (OMP_CLAUSE_CODE (cl1))
1434 case OMP_CLAUSE_ALIGNED:
1435 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1436 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1437 return false;
1438 break;
1439 case OMP_CLAUSE_LINEAR:
1440 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1441 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1442 return false;
1443 break;
1444 case OMP_CLAUSE_SIMDLEN:
1445 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1446 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1447 return false;
1448 default:
1449 break;
1452 return true;
1456 /* Compare two attributes for their value identity. Return true if the
1457 attribute values are known to be equal; otherwise return false. */
1459 bool
1460 attribute_value_equal (const_tree attr1, const_tree attr2)
1462 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1463 return true;
1465 if (TREE_VALUE (attr1) != NULL_TREE
1466 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1467 && TREE_VALUE (attr2) != NULL_TREE
1468 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1470 /* Handle attribute format. */
1471 if (is_attribute_p ("format", get_attribute_name (attr1)))
1473 attr1 = TREE_VALUE (attr1);
1474 attr2 = TREE_VALUE (attr2);
1475 /* Compare the archetypes (printf/scanf/strftime/...). */
1476 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1477 return false;
1478 /* Archetypes are the same. Compare the rest. */
1479 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1480 TREE_CHAIN (attr2)) == 1);
1482 return (simple_cst_list_equal (TREE_VALUE (attr1),
1483 TREE_VALUE (attr2)) == 1);
1486 if (TREE_VALUE (attr1)
1487 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
1488 && TREE_VALUE (attr2)
1489 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1490 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1491 TREE_VALUE (attr2));
1493 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1496 /* Return 0 if the attributes for two types are incompatible, 1 if they
1497 are compatible, and 2 if they are nearly compatible (which causes a
1498 warning to be generated). */
1500 comp_type_attributes (const_tree type1, const_tree type2)
1502 const_tree a1 = TYPE_ATTRIBUTES (type1);
1503 const_tree a2 = TYPE_ATTRIBUTES (type2);
1504 const_tree a;
1506 if (a1 == a2)
1507 return 1;
1508 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1510 const struct attribute_spec *as;
1511 const_tree attr;
1513 as = lookup_attribute_spec (TREE_PURPOSE (a));
1514 if (!as || as->affects_type_identity == false)
1515 continue;
1517 attr = find_same_attribute (a, CONST_CAST_TREE (a2));
1518 if (!attr || !attribute_value_equal (a, attr))
1519 break;
1521 if (!a)
1523 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1525 const struct attribute_spec *as;
1527 as = lookup_attribute_spec (TREE_PURPOSE (a));
1528 if (!as || as->affects_type_identity == false)
1529 continue;
1531 if (!find_same_attribute (a, CONST_CAST_TREE (a1)))
1532 break;
1533 /* We don't need to compare trees again, as we did this
1534 already in first loop. */
1536 /* All types - affecting identity - are equal, so
1537 there is no need to call target hook for comparison. */
1538 if (!a)
1539 return 1;
1541 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1542 return 0;
1543 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1544 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1545 return 0;
1546 int strub_ret = strub_comptypes (CONST_CAST_TREE (type1),
1547 CONST_CAST_TREE (type2));
1548 if (strub_ret == 0)
1549 return strub_ret;
1550 /* As some type combinations - like default calling-convention - might
1551 be compatible, we have to call the target hook to get the final result. */
1552 int target_ret = targetm.comp_type_attributes (type1, type2);
1553 if (target_ret == 0)
1554 return target_ret;
1555 if (strub_ret == 2 || target_ret == 2)
1556 return 2;
1557 if (strub_ret == 1 && target_ret == 1)
1558 return 1;
1559 gcc_unreachable ();
1562 /* PREDICATE acts as a function of type:
1564 (const_tree attr, const attribute_spec *as) -> bool
1566 where ATTR is an attribute and AS is its possibly-null specification.
1567 Return a list of every attribute in attribute list ATTRS for which
1568 PREDICATE is true. Return ATTRS itself if PREDICATE returns true
1569 for every attribute. */
1571 template<typename Predicate>
1572 tree
1573 remove_attributes_matching (tree attrs, Predicate predicate)
1575 tree new_attrs = NULL_TREE;
1576 tree *ptr = &new_attrs;
1577 const_tree start = attrs;
1578 for (const_tree attr = attrs; attr; attr = TREE_CHAIN (attr))
1580 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
1581 const_tree end;
1582 if (!predicate (attr, as))
1583 end = attr;
1584 else if (start == attrs)
1585 continue;
1586 else
1587 end = TREE_CHAIN (attr);
1589 for (; start != end; start = TREE_CHAIN (start))
1591 *ptr = tree_cons (TREE_PURPOSE (start),
1592 TREE_VALUE (start), NULL_TREE);
1593 TREE_CHAIN (*ptr) = NULL_TREE;
1594 ptr = &TREE_CHAIN (*ptr);
1596 start = TREE_CHAIN (attr);
1598 gcc_assert (!start || start == attrs);
1599 return start ? attrs : new_attrs;
1602 /* If VALUE is true, return the subset of ATTRS that affect type identity,
1603 otherwise return the subset of ATTRS that don't affect type identity. */
1605 tree
1606 affects_type_identity_attributes (tree attrs, bool value)
1608 auto predicate = [value](const_tree, const attribute_spec *as) -> bool
1610 return bool (as && as->affects_type_identity) == value;
1612 return remove_attributes_matching (attrs, predicate);
1615 /* Remove attributes that affect type identity from ATTRS unless the
1616 same attributes occur in OK_ATTRS. */
1618 tree
1619 restrict_type_identity_attributes_to (tree attrs, tree ok_attrs)
1621 auto predicate = [ok_attrs](const_tree attr,
1622 const attribute_spec *as) -> bool
1624 if (!as || !as->affects_type_identity)
1625 return true;
1627 for (tree ok_attr = lookup_attribute (as->name, ok_attrs);
1628 ok_attr;
1629 ok_attr = lookup_attribute (as->name, TREE_CHAIN (ok_attr)))
1630 if (simple_cst_equal (TREE_VALUE (ok_attr), TREE_VALUE (attr)) == 1)
1631 return true;
1633 return false;
1635 return remove_attributes_matching (attrs, predicate);
1638 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1639 is ATTRIBUTE.
1641 Record such modified types already made so we don't make duplicates. */
1643 tree
1644 build_type_attribute_variant (tree ttype, tree attribute)
1646 return build_type_attribute_qual_variant (ttype, attribute,
1647 TYPE_QUALS (ttype));
1650 /* A variant of lookup_attribute() that can be used with an identifier
1651 as the first argument, and where the identifier can be either
1652 'text' or '__text__'.
1654 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1655 return a pointer to the attribute's list element if the attribute
1656 is part of the list, or NULL_TREE if not found. If the attribute
1657 appears more than once, this only returns the first occurrence; the
1658 TREE_CHAIN of the return value should be passed back in if further
1659 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1660 can be in the form 'text' or '__text__'. */
1661 static tree
1662 lookup_ident_attribute (tree attr_identifier, tree list)
1664 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1666 while (list)
1668 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1669 == IDENTIFIER_NODE);
1671 if (cmp_attrib_identifiers (attr_identifier,
1672 get_attribute_name (list)))
1673 /* Found it. */
1674 break;
1675 list = TREE_CHAIN (list);
1678 return list;
1681 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1682 modified list. */
1684 tree
1685 remove_attribute (const char *attr_name, tree list)
1687 tree *p;
1688 gcc_checking_assert (attr_name[0] != '_');
1690 for (p = &list; *p;)
1692 tree l = *p;
1694 tree attr = get_attribute_name (l);
1695 if (is_attribute_p (attr_name, attr))
1696 *p = TREE_CHAIN (l);
1697 else
1698 p = &TREE_CHAIN (l);
1701 return list;
1704 /* Similarly but also match namespace on the removed attributes.
1705 ATTR_NS "" stands for NULL or "gnu" namespace. */
1707 tree
1708 remove_attribute (const char *attr_ns, const char *attr_name, tree list)
1710 tree *p;
1711 gcc_checking_assert (attr_name[0] != '_');
1712 gcc_checking_assert (attr_ns == NULL || attr_ns[0] != '_');
1714 for (p = &list; *p;)
1716 tree l = *p;
1718 tree attr = get_attribute_name (l);
1719 if (is_attribute_p (attr_name, attr)
1720 && is_attribute_namespace_p (attr_ns, l))
1722 *p = TREE_CHAIN (l);
1723 continue;
1725 p = &TREE_CHAIN (l);
1728 return list;
1731 /* Return an attribute list that is the union of a1 and a2. */
1733 tree
1734 merge_attributes (tree a1, tree a2)
1736 tree attributes;
1738 /* Either one unset? Take the set one. */
1740 if ((attributes = a1) == 0)
1741 attributes = a2;
1743 /* One that completely contains the other? Take it. */
1745 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1747 if (attribute_list_contained (a2, a1))
1748 attributes = a2;
1749 else
1751 /* Pick the longest list, and hang on the other list. */
1753 if (list_length (a1) < list_length (a2))
1754 attributes = a2, a2 = a1;
1756 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1758 tree a;
1759 for (a = lookup_ident_attribute (get_attribute_name (a2),
1760 attributes);
1761 a != NULL_TREE && !attribute_value_equal (a, a2);
1762 a = lookup_ident_attribute (get_attribute_name (a2),
1763 TREE_CHAIN (a)))
1765 if (a == NULL_TREE)
1767 a1 = copy_node (a2);
1768 TREE_CHAIN (a1) = attributes;
1769 attributes = a1;
1774 return attributes;
1777 /* Given types T1 and T2, merge their attributes and return
1778 the result. */
1780 tree
1781 merge_type_attributes (tree t1, tree t2)
1783 return merge_attributes (TYPE_ATTRIBUTES (t1),
1784 TYPE_ATTRIBUTES (t2));
1787 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1788 the result. */
1790 tree
1791 merge_decl_attributes (tree olddecl, tree newdecl)
1793 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1794 DECL_ATTRIBUTES (newdecl));
1797 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1798 they are missing there. */
1800 void
1801 duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1803 attr = lookup_attribute (name, attr);
1804 if (!attr)
1805 return;
1806 tree a = lookup_attribute (name, *attrs);
1807 while (attr)
1809 tree a2;
1810 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1811 if (attribute_value_equal (attr, a2))
1812 break;
1813 if (!a2)
1815 a2 = copy_node (attr);
1816 TREE_CHAIN (a2) = *attrs;
1817 *attrs = a2;
1819 attr = lookup_attribute (name, TREE_CHAIN (attr));
1823 /* Duplicate all attributes from user DECL to the corresponding
1824 builtin that should be propagated. */
1826 void
1827 copy_attributes_to_builtin (tree decl)
1829 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1830 if (b)
1831 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1832 DECL_ATTRIBUTES (decl), "omp declare simd");
1835 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1837 /* Specialization of merge_decl_attributes for various Windows targets.
1839 This handles the following situation:
1841 __declspec (dllimport) int foo;
1842 int foo;
1844 The second instance of `foo' nullifies the dllimport. */
1846 tree
1847 merge_dllimport_decl_attributes (tree old, tree new_tree)
1849 tree a;
1850 int delete_dllimport_p = 1;
1852 /* What we need to do here is remove from `old' dllimport if it doesn't
1853 appear in `new'. dllimport behaves like extern: if a declaration is
1854 marked dllimport and a definition appears later, then the object
1855 is not dllimport'd. We also remove a `new' dllimport if the old list
1856 contains dllexport: dllexport always overrides dllimport, regardless
1857 of the order of declaration. */
1858 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1859 delete_dllimport_p = 0;
1860 else if (DECL_DLLIMPORT_P (new_tree)
1861 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1863 DECL_DLLIMPORT_P (new_tree) = 0;
1864 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1865 "attribute: dllimport ignored", new_tree);
1867 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1869 /* Warn about overriding a symbol that has already been used, e.g.:
1870 extern int __attribute__ ((dllimport)) foo;
1871 int* bar () {return &foo;}
1872 int foo;
1874 if (TREE_USED (old))
1876 warning (0, "%q+D redeclared without dllimport attribute "
1877 "after being referenced with dll linkage", new_tree);
1878 /* If we have used a variable's address with dllimport linkage,
1879 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1880 decl may already have had TREE_CONSTANT computed.
1881 We still remove the attribute so that assembler code refers
1882 to '&foo rather than '_imp__foo'. */
1883 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1884 DECL_DLLIMPORT_P (new_tree) = 1;
1887 /* Let an inline definition silently override the external reference,
1888 but otherwise warn about attribute inconsistency. */
1889 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1890 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1891 "attribute: previous dllimport ignored", new_tree);
1893 else
1894 delete_dllimport_p = 0;
1896 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1898 if (delete_dllimport_p)
1899 a = remove_attribute ("dllimport", a);
1901 return a;
1904 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1905 struct attribute_spec.handler. */
1907 tree
1908 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1909 bool *no_add_attrs)
1911 tree node = *pnode;
1912 bool is_dllimport;
1914 /* These attributes may apply to structure and union types being created,
1915 but otherwise should pass to the declaration involved. */
1916 if (!DECL_P (node))
1918 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1919 | (int) ATTR_FLAG_ARRAY_NEXT))
1921 *no_add_attrs = true;
1922 return tree_cons (name, args, NULL_TREE);
1924 if (TREE_CODE (node) == RECORD_TYPE
1925 || TREE_CODE (node) == UNION_TYPE)
1927 node = TYPE_NAME (node);
1928 if (!node)
1929 return NULL_TREE;
1931 else
1933 warning (OPT_Wattributes, "%qE attribute ignored",
1934 name);
1935 *no_add_attrs = true;
1936 return NULL_TREE;
1940 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1942 *no_add_attrs = true;
1943 warning (OPT_Wattributes, "%qE attribute ignored",
1944 name);
1945 return NULL_TREE;
1948 if (TREE_CODE (node) == TYPE_DECL
1949 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1950 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1952 *no_add_attrs = true;
1953 warning (OPT_Wattributes, "%qE attribute ignored",
1954 name);
1955 return NULL_TREE;
1958 is_dllimport = is_attribute_p ("dllimport", name);
1960 /* Report error on dllimport ambiguities seen now before they cause
1961 any damage. */
1962 if (is_dllimport)
1964 /* Honor any target-specific overrides. */
1965 if (!targetm.valid_dllimport_attribute_p (node))
1966 *no_add_attrs = true;
1968 else if (TREE_CODE (node) == FUNCTION_DECL
1969 && DECL_DECLARED_INLINE_P (node))
1971 warning (OPT_Wattributes, "inline function %q+D declared as "
1972 "dllimport: attribute ignored", node);
1973 *no_add_attrs = true;
1975 /* Like MS, treat definition of dllimported variables and
1976 non-inlined functions on declaration as syntax errors. */
1977 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1979 error ("function %q+D definition is marked dllimport", node);
1980 *no_add_attrs = true;
1983 else if (VAR_P (node))
1985 if (DECL_INITIAL (node))
1987 error ("variable %q+D definition is marked dllimport",
1988 node);
1989 *no_add_attrs = true;
1992 /* `extern' needn't be specified with dllimport.
1993 Specify `extern' now and hope for the best. Sigh. */
1994 DECL_EXTERNAL (node) = 1;
1995 /* Also, implicitly give dllimport'd variables declared within
1996 a function global scope, unless declared static. */
1997 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1998 TREE_PUBLIC (node) = 1;
1999 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
2000 it is a C++ static data member. */
2001 if (DECL_CONTEXT (node) == NULL_TREE
2002 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node)))
2003 TREE_STATIC (node) = 0;
2006 if (*no_add_attrs == false)
2007 DECL_DLLIMPORT_P (node) = 1;
2009 else if (TREE_CODE (node) == FUNCTION_DECL
2010 && DECL_DECLARED_INLINE_P (node)
2011 && flag_keep_inline_dllexport)
2012 /* An exported function, even if inline, must be emitted. */
2013 DECL_EXTERNAL (node) = 0;
2015 /* Report error if symbol is not accessible at global scope. */
2016 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
2018 error ("external linkage required for symbol %q+D because of "
2019 "%qE attribute", node, name);
2020 *no_add_attrs = true;
2023 /* A dllexport'd entity must have default visibility so that other
2024 program units (shared libraries or the main executable) can see
2025 it. A dllimport'd entity must have default visibility so that
2026 the linker knows that undefined references within this program
2027 unit can be resolved by the dynamic linker. */
2028 if (!*no_add_attrs)
2030 if (DECL_VISIBILITY_SPECIFIED (node)
2031 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
2032 error ("%qE implies default visibility, but %qD has already "
2033 "been declared with a different visibility",
2034 name, node);
2035 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
2036 DECL_VISIBILITY_SPECIFIED (node) = 1;
2039 return NULL_TREE;
2042 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2044 /* Given two lists of attributes, return true if list l2 is
2045 equivalent to l1. */
2048 attribute_list_equal (const_tree l1, const_tree l2)
2050 if (l1 == l2)
2051 return 1;
2053 return attribute_list_contained (l1, l2)
2054 && attribute_list_contained (l2, l1);
2057 /* Given two lists of attributes, return true if list L2 is
2058 completely contained within L1. */
2059 /* ??? This would be faster if attribute names were stored in a canonicalized
2060 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
2061 must be used to show these elements are equivalent (which they are). */
2062 /* ??? It's not clear that attributes with arguments will always be handled
2063 correctly. */
2066 attribute_list_contained (const_tree l1, const_tree l2)
2068 const_tree t1, t2;
2070 /* First check the obvious, maybe the lists are identical. */
2071 if (l1 == l2)
2072 return 1;
2074 /* Maybe the lists are similar. */
2075 for (t1 = l1, t2 = l2;
2076 t1 != 0 && t2 != 0
2077 && get_attribute_name (t1) == get_attribute_name (t2)
2078 && TREE_VALUE (t1) == TREE_VALUE (t2);
2079 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2082 /* Maybe the lists are equal. */
2083 if (t1 == 0 && t2 == 0)
2084 return 1;
2086 for (; t2 != 0; t2 = TREE_CHAIN (t2))
2088 const_tree attr;
2089 /* This CONST_CAST is okay because lookup_attribute does not
2090 modify its argument and the return value is assigned to a
2091 const_tree. */
2092 for (attr = lookup_ident_attribute (get_attribute_name (t2),
2093 CONST_CAST_TREE (l1));
2094 attr != NULL_TREE && !attribute_value_equal (t2, attr);
2095 attr = lookup_ident_attribute (get_attribute_name (t2),
2096 TREE_CHAIN (attr)))
2099 if (attr == NULL_TREE)
2100 return 0;
2103 return 1;
2106 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
2107 of ATTR_NAME, and LIST is not NULL_TREE.
2109 The function is called from lookup_attribute in order to optimize
2110 for size. */
2112 tree
2113 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
2115 while (list)
2117 tree attr = get_attribute_name (list);
2118 size_t ident_len = IDENTIFIER_LENGTH (attr);
2119 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2120 ident_len))
2121 break;
2122 list = TREE_CHAIN (list);
2125 return list;
2128 /* Similarly but with also attribute namespace. */
2130 tree
2131 private_lookup_attribute (const char *attr_ns, const char *attr_name,
2132 size_t attr_ns_len, size_t attr_len, tree list)
2134 while (list)
2136 tree attr = get_attribute_name (list);
2137 size_t ident_len = IDENTIFIER_LENGTH (attr);
2138 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2139 ident_len))
2141 tree ns = get_attribute_namespace (list);
2142 if (ns == NULL_TREE)
2144 if (attr_ns_len == 0)
2145 break;
2147 else if (attr_ns)
2149 ident_len = IDENTIFIER_LENGTH (ns);
2150 if (attr_ns_len == 0)
2152 if (cmp_attribs ("gnu", strlen ("gnu"),
2153 IDENTIFIER_POINTER (ns), ident_len))
2154 break;
2156 else if (cmp_attribs (attr_ns, attr_ns_len,
2157 IDENTIFIER_POINTER (ns), ident_len))
2158 break;
2161 list = TREE_CHAIN (list);
2164 return list;
2167 /* Return true if the function decl or type NODE has been declared
2168 with attribute ANAME among attributes ATTRS. */
2170 static bool
2171 has_attribute (tree node, tree attrs, const char *aname)
2173 if (!strcmp (aname, "const"))
2175 if (DECL_P (node) && TREE_READONLY (node))
2176 return true;
2178 else if (!strcmp (aname, "malloc"))
2180 if (DECL_P (node) && DECL_IS_MALLOC (node))
2181 return true;
2183 else if (!strcmp (aname, "noreturn"))
2185 if (DECL_P (node) && TREE_THIS_VOLATILE (node))
2186 return true;
2188 else if (!strcmp (aname, "nothrow"))
2190 if (TREE_NOTHROW (node))
2191 return true;
2193 else if (!strcmp (aname, "pure"))
2195 if (DECL_P (node) && DECL_PURE_P (node))
2196 return true;
2199 return lookup_attribute (aname, attrs);
2202 /* Return the number of mismatched function or type attributes between
2203 the "template" function declaration TMPL and DECL. The word "template"
2204 doesn't necessarily refer to a C++ template but rather a declaration
2205 whose attributes should be matched by those on DECL. For a non-zero
2206 return value append the names of the mismatcheed attributes to OUTATTRS.
2207 ATTRLIST is a list of additional attributes that SPEC should be
2208 taken to ultimately be declared with. */
2210 unsigned
2211 decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
2212 const char* const blacklist[],
2213 auto_vec<const char *> &outattrs)
2215 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2216 return 0;
2218 /* Avoid warning if either declaration or its type is deprecated. */
2219 if (TREE_DEPRECATED (tmpl)
2220 || TREE_DEPRECATED (decl))
2221 return 0;
2223 const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
2224 const tree decls[] = { decl, TREE_TYPE (decl) };
2226 if (TREE_DEPRECATED (tmpls[1])
2227 || TREE_DEPRECATED (decls[1])
2228 || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
2229 || TREE_DEPRECATED (TREE_TYPE (decls[1])))
2230 return 0;
2232 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
2233 tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
2235 if (!decl_attrs[0])
2236 decl_attrs[0] = attrlist;
2237 else if (!decl_attrs[1])
2238 decl_attrs[1] = attrlist;
2240 /* Avoid warning if the template has no attributes. */
2241 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2242 return 0;
2244 /* Avoid warning if either declaration contains an attribute on
2245 the white list below. */
2246 const char* const whitelist[] = {
2247 "error", "warning"
2250 for (unsigned i = 0; i != 2; ++i)
2251 for (unsigned j = 0; j != ARRAY_SIZE (whitelist); ++j)
2252 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2253 || lookup_attribute (whitelist[j], decl_attrs[i]))
2254 return 0;
2256 /* Put together a list of the black-listed attributes that the template
2257 is declared with and the declaration is not, in case it's not apparent
2258 from the most recent declaration of the template. */
2259 unsigned nattrs = 0;
2261 for (unsigned i = 0; blacklist[i]; ++i)
2263 /* Attribute leaf only applies to extern functions. Avoid mentioning
2264 it when it's missing from a static declaration. */
2265 if (!TREE_PUBLIC (decl)
2266 && !strcmp ("leaf", blacklist[i]))
2267 continue;
2269 for (unsigned j = 0; j != 2; ++j)
2271 if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
2272 continue;
2274 bool found = false;
2275 unsigned kmax = 1 + !!decl_attrs[1];
2276 for (unsigned k = 0; k != kmax; ++k)
2278 if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
2280 found = true;
2281 break;
2285 if (!found)
2287 outattrs.safe_push (blacklist[i]);
2288 ++nattrs;
2291 break;
2295 return nattrs;
2298 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
2299 specifies either attributes that are incompatible with those of
2300 TARGET, or attributes that are missing and that declaring ALIAS
2301 with would benefit. */
2303 void
2304 maybe_diag_alias_attributes (tree alias, tree target)
2306 /* Do not expect attributes to match between aliases and ifunc
2307 resolvers. There is no obvious correspondence between them. */
2308 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
2309 return;
2311 const char* const blacklist[] = {
2312 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
2313 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
2314 "returns_twice", NULL
2317 if (warn_attribute_alias > 1)
2319 /* With -Wattribute-alias=2 detect alias declarations that are more
2320 restrictive than their targets first. Those indicate potential
2321 codegen bugs. */
2322 auto_vec<const char *> mismatches;
2323 if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
2324 blacklist, mismatches))
2326 auto_diagnostic_group d;
2327 pp_markup::comma_separated_quoted_strings e (mismatches);
2328 if (warning_n (DECL_SOURCE_LOCATION (alias),
2329 OPT_Wattribute_alias_, n,
2330 "%qD specifies more restrictive attribute than "
2331 "its target %qD: %e",
2332 "%qD specifies more restrictive attributes than "
2333 "its target %qD: %e",
2334 alias, target, &e))
2335 inform (DECL_SOURCE_LOCATION (target),
2336 "%qD target declared here", alias);
2337 return;
2341 /* Detect alias declarations that are less restrictive than their
2342 targets. Those suggest potential optimization opportunities
2343 (solved by adding the missing attribute(s) to the alias). */
2344 auto_vec<const char *> mismatches;
2345 if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
2346 blacklist, mismatches))
2348 auto_diagnostic_group d;
2349 pp_markup::comma_separated_quoted_strings e (mismatches);
2350 if (warning_n (DECL_SOURCE_LOCATION (alias),
2351 OPT_Wmissing_attributes, n,
2352 "%qD specifies less restrictive attribute than "
2353 "its target %qD: %e",
2354 "%qD specifies less restrictive attributes than "
2355 "its target %qD: %e",
2356 alias, target, &e))
2357 inform (DECL_SOURCE_LOCATION (target),
2358 "%qD target declared here", alias);
2362 /* Initialize a mapping RWM for a call to a function declared with
2363 attribute access in ATTRS. Each attribute positional operand
2364 inserts one entry into the mapping with the operand number as
2365 the key. */
2367 void
2368 init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
2370 if (!attrs)
2371 return;
2373 for (tree access = attrs;
2374 (access = lookup_attribute ("access", access));
2375 access = TREE_CHAIN (access))
2377 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2378 is the attribute argument's value. */
2379 tree mode = TREE_VALUE (access);
2380 if (!mode)
2381 return;
2383 /* The (optional) list of VLA bounds. */
2384 tree vblist = TREE_CHAIN (mode);
2385 mode = TREE_VALUE (mode);
2386 if (TREE_CODE (mode) != STRING_CST)
2387 continue;
2388 gcc_assert (TREE_CODE (mode) == STRING_CST);
2390 if (vblist)
2391 vblist = nreverse (copy_list (TREE_VALUE (vblist)));
2393 for (const char *m = TREE_STRING_POINTER (mode); *m; )
2395 attr_access acc = { };
2397 /* Skip the internal-only plus sign. */
2398 if (*m == '+')
2399 ++m;
2401 acc.str = m;
2402 acc.mode = acc.from_mode_char (*m);
2403 acc.sizarg = UINT_MAX;
2405 const char *end;
2406 acc.ptrarg = strtoul (++m, const_cast<char**>(&end), 10);
2407 m = end;
2409 if (*m == '[')
2411 /* Forms containing the square bracket are internal-only
2412 (not specified by an attribute declaration), and used
2413 for various forms of array and VLA parameters. */
2414 acc.internal_p = true;
2416 /* Search to the closing bracket and look at the preceding
2417 code: it determines the form of the most significant
2418 bound of the array. Others prior to it encode the form
2419 of interior VLA bounds. They're not of interest here. */
2420 end = strchr (m, ']');
2421 const char *p = end;
2422 gcc_assert (p);
2424 while (ISDIGIT (p[-1]))
2425 --p;
2427 if (ISDIGIT (*p))
2429 /* A digit denotes a constant bound (as in T[3]). */
2430 acc.static_p = p[-1] == 's';
2431 acc.minsize = strtoull (p, NULL, 10);
2433 else if (' ' == p[-1])
2435 /* A space denotes an ordinary array of unspecified bound
2436 (as in T[]). */
2437 acc.minsize = 0;
2439 else if ('*' == p[-1] || '$' == p[-1])
2441 /* An asterisk denotes a VLA. When the closing bracket
2442 is followed by a comma and a dollar sign its bound is
2443 on the list. Otherwise it's a VLA with an unspecified
2444 bound. */
2445 acc.static_p = p[-2] == 's';
2446 acc.minsize = HOST_WIDE_INT_M1U;
2449 m = end + 1;
2452 if (*m == ',')
2454 ++m;
2457 if (*m == '$')
2459 ++m;
2460 if (!acc.size && vblist)
2462 /* Extract the list of VLA bounds for the current
2463 parameter, store it in ACC.SIZE, and advance
2464 to the list of bounds for the next VLA parameter.
2466 acc.size = TREE_VALUE (vblist);
2467 vblist = TREE_CHAIN (vblist);
2471 if (ISDIGIT (*m))
2473 /* Extract the positional argument. It's absent
2474 for VLAs whose bound doesn't name a function
2475 parameter. */
2476 unsigned pos = strtoul (m, const_cast<char**>(&end), 10);
2477 if (acc.sizarg == UINT_MAX)
2478 acc.sizarg = pos;
2479 m = end;
2482 while (*m == '$');
2485 acc.end = m;
2487 bool existing;
2488 auto &ref = rwm->get_or_insert (acc.ptrarg, &existing);
2489 if (existing)
2491 /* Merge the new spec with the existing. */
2492 if (acc.minsize == HOST_WIDE_INT_M1U)
2493 ref.minsize = HOST_WIDE_INT_M1U;
2495 if (acc.sizarg != UINT_MAX)
2496 ref.sizarg = acc.sizarg;
2498 if (acc.mode)
2499 ref.mode = acc.mode;
2501 else
2502 ref = acc;
2504 /* Unconditionally add an entry for the required pointer
2505 operand of the attribute, and one for the optional size
2506 operand when it's specified. */
2507 if (acc.sizarg != UINT_MAX)
2508 rwm->put (acc.sizarg, acc);
2513 /* Return the access specification for a function parameter PARM
2514 or null if the current function has no such specification. */
2516 attr_access *
2517 get_parm_access (rdwr_map &rdwr_idx, tree parm,
2518 tree fndecl /* = current_function_decl */)
2520 tree fntype = TREE_TYPE (fndecl);
2521 init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
2523 if (rdwr_idx.is_empty ())
2524 return NULL;
2526 unsigned argpos = 0;
2527 tree fnargs = DECL_ARGUMENTS (fndecl);
2528 for (tree arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
2529 if (arg == parm)
2530 return rdwr_idx.get (argpos);
2532 return NULL;
2535 /* Return the internal representation as STRING_CST. Internal positional
2536 arguments are zero-based. */
2538 tree
2539 attr_access::to_internal_string () const
2541 return build_string (end - str, str);
2544 /* Return the human-readable representation of the external attribute
2545 specification (as it might appear in the source code) as STRING_CST.
2546 External positional arguments are one-based. */
2548 tree
2549 attr_access::to_external_string () const
2551 char buf[80];
2552 gcc_assert (mode != access_deferred);
2553 int len = snprintf (buf, sizeof buf, "access (%s, %u",
2554 mode_names[mode], ptrarg + 1);
2555 if (sizarg != UINT_MAX)
2556 len += snprintf (buf + len, sizeof buf - len, ", %u", sizarg + 1);
2557 strcpy (buf + len, ")");
2558 return build_string (len + 2, buf);
2561 /* Return the number of specified VLA bounds and set *nunspec to
2562 the number of unspecified ones (those designated by [*]). */
2564 unsigned
2565 attr_access::vla_bounds (unsigned *nunspec) const
2567 unsigned nbounds = 0;
2568 *nunspec = 0;
2569 /* STR points to the beginning of the specified string for the current
2570 argument that may be followed by the string for the next argument. */
2571 for (const char* p = strchr (str, ']'); p && *p != '['; --p)
2573 if (*p == '*')
2574 ++*nunspec;
2575 else if (*p == '$')
2576 ++nbounds;
2578 return nbounds;
2581 /* Reset front end-specific attribute access data from ATTRS.
2582 Called from the free_lang_data pass. */
2584 /* static */ void
2585 attr_access::free_lang_data (tree attrs)
2587 for (tree acs = attrs; (acs = lookup_attribute ("access", acs));
2588 acs = TREE_CHAIN (acs))
2590 tree vblist = TREE_VALUE (acs);
2591 vblist = TREE_CHAIN (vblist);
2592 if (!vblist)
2593 continue;
2595 for (vblist = TREE_VALUE (vblist); vblist; vblist = TREE_CHAIN (vblist))
2597 tree *pvbnd = &TREE_VALUE (vblist);
2598 if (!*pvbnd || DECL_P (*pvbnd))
2599 continue;
2601 /* VLA bounds that are expressions as opposed to DECLs are
2602 only used in the front end. Reset them to keep front end
2603 trees leaking into the middle end (see pr97172) and to
2604 free up memory. */
2605 *pvbnd = NULL_TREE;
2609 for (tree argspec = attrs; (argspec = lookup_attribute ("arg spec", argspec));
2610 argspec = TREE_CHAIN (argspec))
2612 /* Same as above. */
2613 tree *pvblist = &TREE_VALUE (argspec);
2614 *pvblist = NULL_TREE;
2618 /* Defined in attr_access. */
2619 constexpr char attr_access::mode_chars[];
2620 constexpr char attr_access::mode_names[][11];
2622 /* Format an array, including a VLA, pointed to by TYPE and used as
2623 a function parameter as a human-readable string. ACC describes
2624 an access to the parameter and is used to determine the outermost
2625 form of the array including its bound which is otherwise obviated
2626 by its decay to pointer. Return the formatted string. */
2628 std::string
2629 attr_access::array_as_string (tree type) const
2631 std::string typstr;
2633 if (type == error_mark_node)
2634 return std::string ();
2636 if (this->str)
2638 /* For array parameters (but not pointers) create a temporary array
2639 type that corresponds to the form of the parameter including its
2640 qualifiers even though they apply to the pointer, not the array
2641 type. */
2642 const bool vla_p = minsize == HOST_WIDE_INT_M1U;
2643 tree eltype = TREE_TYPE (type);
2644 tree index_type = NULL_TREE;
2646 if (minsize == HOST_WIDE_INT_M1U)
2648 /* Determine if this is a VLA (an array whose most significant
2649 bound is nonconstant and whose access string has "$]" in it)
2650 extract the bound expression from SIZE. */
2651 const char *p = end;
2652 for ( ; p != str && *p-- != ']'; );
2653 if (*p == '$')
2654 /* SIZE may have been cleared. Use it with care. */
2655 index_type = build_index_type (size ? TREE_VALUE (size) : size);
2657 else if (minsize)
2658 index_type = build_index_type (size_int (minsize - 1));
2660 tree arat = NULL_TREE;
2661 if (static_p || vla_p)
2663 tree flag = static_p ? integer_one_node : NULL_TREE;
2664 /* Hack: there's no language-independent way to encode
2665 the "static" specifier or the "*" notation in an array type.
2666 Add a "fake" attribute to have the pretty-printer add "static"
2667 or "*". The "[static N]" notation is only valid in the most
2668 significant bound but [*] can be used for any bound. Because
2669 [*] is represented the same as [0] this hack only works for
2670 the most significant bound like static and the others are
2671 rendered as [0]. */
2672 arat = build_tree_list (get_identifier ("array"), flag);
2675 const int quals = TYPE_QUALS (type);
2676 type = build_array_type (eltype, index_type);
2677 type = build_type_attribute_qual_variant (type, arat, quals);
2680 /* Format the type using the current pretty printer. The generic tree
2681 printer does a terrible job. */
2682 std::unique_ptr<pretty_printer> pp (global_dc->clone_printer ());
2683 pp_printf (pp.get (), "%qT", type);
2684 typstr = pp_formatted_text (pp.get ());
2686 return typstr;
2689 #if CHECKING_P
2691 namespace selftest
2694 /* Self-test to verify that each attribute exclusion is symmetric,
2695 meaning that if attribute A is encoded as incompatible with
2696 attribute B then the opposite relationship is also encoded.
2697 This test also detects most cases of misspelled attribute names
2698 in exclusions. */
2700 static void
2701 test_attribute_exclusions ()
2703 using excl_hash_traits = pair_hash<nofree_string_hash, nofree_string_hash>;
2705 /* Iterate over the array of attribute tables first (with TI0 as
2706 the index) and over the array of attribute_spec in each table
2707 (with SI0 as the index). */
2708 hash_set<excl_hash_traits> excl_set;
2710 for (auto scoped_array : attribute_tables)
2711 for (auto scoped_attributes : scoped_array)
2712 for (const attribute_spec &attribute : scoped_attributes->attributes)
2714 const attribute_spec::exclusions *excl = attribute.exclude;
2716 /* Skip each attribute that doesn't define exclusions. */
2717 if (!excl)
2718 continue;
2720 /* Skip standard (non-GNU) attributes, since currently the
2721 exclusions are implicitly for GNU attributes only.
2722 Also, C++ likely and unlikely get rewritten to gnu::hot
2723 and gnu::cold, so symmetry isn't necessary there. */
2724 if (!scoped_attributes->ns)
2725 continue;
2727 const char *attr_name = attribute.name;
2729 /* Iterate over the set of exclusions for every attribute
2730 (with EI0 as the index) adding the exclusions defined
2731 for each to the set. */
2732 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2734 const char *excl_name = excl[ei0].name;
2736 if (!strcmp (attr_name, excl_name))
2737 continue;
2739 excl_set.add ({ attr_name, excl_name });
2743 /* Traverse the set of mutually exclusive pairs of attributes
2744 and verify that they are symmetric. */
2745 for (auto excl_pair : excl_set)
2746 if (!excl_set.contains ({ excl_pair.second, excl_pair.first }))
2748 /* An exclusion for an attribute has been found that
2749 doesn't have a corresponding exclusion in the opposite
2750 direction. */
2751 char desc[120];
2752 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2753 excl_pair.first, excl_pair.second);
2754 fail (SELFTEST_LOCATION, desc);
2758 void
2759 attribs_cc_tests ()
2761 test_attribute_exclusions ();
2764 } /* namespace selftest */
2766 #endif /* CHECKING_P */
2768 #include "gt-attribs.h"