1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "stringpool.h"
26 #include "diagnostic-core.h"
28 #include "stor-layout.h"
29 #include "langhooks.h"
32 /* Table of the tables of attributes (common, language, format, machine)
34 static const struct attribute_spec *attribute_tables[4];
36 /* Substring representation. */
44 /* Simple hash function to avoid need to scan whole string. */
46 static inline hashval_t
47 substring_hash (const char *str, int l)
49 return str[0] + str[l - 1] * 256 + l * 65536;
52 /* Used for attribute_hash. */
54 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
56 typedef substring *compare_type;
57 static inline hashval_t hash (const attribute_spec *);
58 static inline bool equal (const attribute_spec *, const substring *);
62 attribute_hasher::hash (const attribute_spec *spec)
64 const int l = strlen (spec->name);
65 return substring_hash (spec->name, l);
69 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
71 return (strncmp (spec->name, str->str, str->length) == 0
72 && !spec->name[str->length]);
75 /* Scoped attribute name representation. */
77 struct scoped_attributes
80 vec<attribute_spec> attributes;
81 hash_table<attribute_hasher> *attribute_hash;
84 /* The table of scope attributes. */
85 static vec<scoped_attributes> attributes_table;
87 static scoped_attributes* find_attribute_namespace (const char*);
88 static void register_scoped_attribute (const struct attribute_spec *,
91 static bool attributes_initialized = false;
93 /* Default empty table of attributes. */
95 static const struct attribute_spec empty_attribute_table[] =
97 { NULL, 0, 0, false, false, false, NULL, false }
100 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
101 To avoid need for copying, we simply return length of the string. */
104 extract_attribute_substring (struct substring *str)
106 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
107 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
114 /* Insert an array of attributes ATTRIBUTES into a namespace. This
115 array must be NULL terminated. NS is the name of attribute
116 namespace. The function returns the namespace into which the
117 attributes have been registered. */
120 register_scoped_attributes (const struct attribute_spec * attributes,
123 scoped_attributes *result = NULL;
125 /* See if we already have attributes in the namespace NS. */
126 result = find_attribute_namespace (ns);
130 /* We don't have any namespace NS yet. Create one. */
131 scoped_attributes sa;
133 if (attributes_table.is_empty ())
134 attributes_table.create (64);
136 memset (&sa, 0, sizeof (sa));
138 sa.attributes.create (64);
139 result = attributes_table.safe_push (sa);
140 result->attribute_hash = new hash_table<attribute_hasher> (200);
143 /* Really add the attributes to their namespace now. */
144 for (unsigned i = 0; attributes[i].name != NULL; ++i)
146 result->attributes.safe_push (attributes[i]);
147 register_scoped_attribute (&attributes[i], result);
150 gcc_assert (result != NULL);
155 /* Return the namespace which name is NS, NULL if none exist. */
157 static scoped_attributes*
158 find_attribute_namespace (const char* ns)
161 scoped_attributes *iter;
163 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
167 && !strcmp (iter->ns, ns)))
172 /* Make some sanity checks on the attribute tables. */
175 check_attribute_tables (void)
177 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
178 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
180 /* The name must not begin and end with __. */
181 const char *name = attribute_tables[i][j].name;
182 int len = strlen (name);
184 gcc_assert (!(name[0] == '_' && name[1] == '_'
185 && name[len - 1] == '_' && name[len - 2] == '_'));
187 /* The minimum and maximum lengths must be consistent. */
188 gcc_assert (attribute_tables[i][j].min_length >= 0);
190 gcc_assert (attribute_tables[i][j].max_length == -1
191 || (attribute_tables[i][j].max_length
192 >= attribute_tables[i][j].min_length));
194 /* An attribute cannot require both a DECL and a TYPE. */
195 gcc_assert (!attribute_tables[i][j].decl_required
196 || !attribute_tables[i][j].type_required);
198 /* If an attribute requires a function type, in particular
199 it requires a type. */
200 gcc_assert (!attribute_tables[i][j].function_type_required
201 || attribute_tables[i][j].type_required);
204 /* Check that each name occurs just once in each table. */
205 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
206 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
207 for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++)
208 gcc_assert (strcmp (attribute_tables[i][j].name,
209 attribute_tables[i][k].name));
211 /* Check that no name occurs in more than one table. Names that
212 begin with '*' are exempt, and may be overridden. */
213 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
214 for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
215 for (size_t k = 0; attribute_tables[i][k].name != NULL; k++)
216 for (size_t l = 0; attribute_tables[j][l].name != NULL; l++)
217 gcc_assert (attribute_tables[i][k].name[0] == '*'
218 || strcmp (attribute_tables[i][k].name,
219 attribute_tables[j][l].name));
222 /* Initialize attribute tables, and make some sanity checks if checking is
226 init_attributes (void)
230 if (attributes_initialized)
233 attribute_tables[0] = lang_hooks.common_attribute_table;
234 attribute_tables[1] = lang_hooks.attribute_table;
235 attribute_tables[2] = lang_hooks.format_attribute_table;
236 attribute_tables[3] = targetm.attribute_table;
238 /* Translate NULL pointers to pointers to the empty table. */
239 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
240 if (attribute_tables[i] == NULL)
241 attribute_tables[i] = empty_attribute_table;
244 check_attribute_tables ();
246 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
247 /* Put all the GNU attributes into the "gnu" namespace. */
248 register_scoped_attributes (attribute_tables[i], "gnu");
250 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
251 attributes_initialized = true;
254 /* Insert a single ATTR into the attribute table. */
257 register_attribute (const struct attribute_spec *attr)
259 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
262 /* Insert a single attribute ATTR into a namespace of attributes. */
265 register_scoped_attribute (const struct attribute_spec *attr,
266 scoped_attributes *name_space)
268 struct substring str;
269 attribute_spec **slot;
271 gcc_assert (attr != NULL && name_space != NULL);
273 gcc_assert (name_space->attribute_hash);
275 str.str = attr->name;
276 str.length = strlen (str.str);
278 /* Attribute names in the table must be in the form 'text' and not
279 in the form '__text__'. */
280 gcc_assert (str.length > 0 && str.str[0] != '_');
282 slot = name_space->attribute_hash
283 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
285 gcc_assert (!*slot || attr->name[0] == '*');
286 *slot = CONST_CAST (struct attribute_spec *, attr);
289 /* Return the spec for the scoped attribute with namespace NS and
292 static const struct attribute_spec *
293 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
295 struct substring attr;
296 scoped_attributes *attrs;
298 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
300 attrs = find_attribute_namespace (ns_str);
305 attr.str = IDENTIFIER_POINTER (name);
306 attr.length = IDENTIFIER_LENGTH (name);
307 extract_attribute_substring (&attr);
308 return attrs->attribute_hash->find_with_hash (&attr,
309 substring_hash (attr.str,
313 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
314 it also specifies the attribute namespace. */
316 const struct attribute_spec *
317 lookup_attribute_spec (const_tree name)
320 if (TREE_CODE (name) == TREE_LIST)
322 ns = TREE_PURPOSE (name);
323 name = TREE_VALUE (name);
326 ns = get_identifier ("gnu");
327 return lookup_scoped_attribute_spec (ns, name);
331 /* Return the namespace of the attribute ATTR. This accessor works on
332 GNU and C++11 (scoped) attributes. On GNU attributes,
333 it returns an identifier tree for the string "gnu".
335 Please read the comments of cxx11_attribute_p to understand the
336 format of attributes. */
339 get_attribute_namespace (const_tree attr)
341 if (cxx11_attribute_p (attr))
342 return TREE_PURPOSE (TREE_PURPOSE (attr));
343 return get_identifier ("gnu");
347 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
348 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
349 it should be modified in place; if a TYPE, a copy should be created
350 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
351 information, in the form of a bitwise OR of flags in enum attribute_flags
352 from tree.h. Depending on these flags, some attributes may be
353 returned to be applied at a later stage (for example, to apply
354 a decl attribute to the declaration rather than to its type). */
357 decl_attributes (tree *node, tree attributes, int flags)
360 tree returned_attrs = NULL_TREE;
362 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
365 if (!attributes_initialized)
368 /* If this is a function and the user used #pragma GCC optimize, add the
369 options to the attribute((optimize(...))) list. */
370 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
372 tree cur_attr = lookup_attribute ("optimize", attributes);
373 tree opts = copy_list (current_optimize_pragma);
377 = tree_cons (get_identifier ("optimize"), opts, attributes);
379 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
382 if (TREE_CODE (*node) == FUNCTION_DECL
383 && optimization_current_node != optimization_default_node
384 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
385 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
387 /* If this is a function and the user used #pragma GCC target, add the
388 options to the attribute((target(...))) list. */
389 if (TREE_CODE (*node) == FUNCTION_DECL
390 && current_target_pragma
391 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
392 current_target_pragma, 0))
394 tree cur_attr = lookup_attribute ("target", attributes);
395 tree opts = copy_list (current_target_pragma);
398 attributes = tree_cons (get_identifier ("target"), opts, attributes);
400 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
403 /* A "naked" function attribute implies "noinline" and "noclone" for
404 those targets that support it. */
405 if (TREE_CODE (*node) == FUNCTION_DECL
407 && lookup_attribute ("naked", attributes) != NULL
408 && lookup_attribute_spec (get_identifier ("naked")))
410 if (lookup_attribute ("noinline", attributes) == NULL)
411 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
413 if (lookup_attribute ("noclone", attributes) == NULL)
414 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
417 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
418 for those targets that support it. */
419 if (TREE_CODE (*node) == FUNCTION_DECL
421 && lookup_attribute ("noipa", attributes) != NULL
422 && lookup_attribute_spec (get_identifier ("noipa")))
424 if (lookup_attribute ("noinline", attributes) == NULL)
425 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
427 if (lookup_attribute ("noclone", attributes) == NULL)
428 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
430 if (lookup_attribute ("no_icf", attributes) == NULL)
431 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
434 targetm.insert_attributes (*node, &attributes);
436 for (a = attributes; a; a = TREE_CHAIN (a))
438 tree ns = get_attribute_namespace (a);
439 tree name = get_attribute_name (a);
440 tree args = TREE_VALUE (a);
442 const struct attribute_spec *spec =
443 lookup_scoped_attribute_spec (ns, name);
444 bool no_add_attrs = 0;
445 int fn_ptr_quals = 0;
446 tree fn_ptr_tmp = NULL_TREE;
450 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
452 if (ns == NULL_TREE || !cxx11_attribute_p (a))
453 warning (OPT_Wattributes, "%qE attribute directive ignored",
456 warning (OPT_Wattributes,
457 "%<%E::%E%> scoped attribute directive ignored",
462 else if (list_length (args) < spec->min_length
463 || (spec->max_length >= 0
464 && list_length (args) > spec->max_length))
466 error ("wrong number of arguments specified for %qE attribute",
470 gcc_assert (is_attribute_p (spec->name, name));
473 && cxx11_attribute_p (a)
474 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
476 /* This is a c++11 attribute that appertains to a
477 type-specifier, outside of the definition of, a class
479 if (warning (OPT_Wattributes, "attribute ignored"))
480 inform (input_location,
481 "an attribute that appertains to a type-specifier "
486 if (spec->decl_required && !DECL_P (*anode))
488 if (flags & ((int) ATTR_FLAG_DECL_NEXT
489 | (int) ATTR_FLAG_FUNCTION_NEXT
490 | (int) ATTR_FLAG_ARRAY_NEXT))
492 /* Pass on this attribute to be tried again. */
493 returned_attrs = tree_cons (name, args, returned_attrs);
498 warning (OPT_Wattributes, "%qE attribute does not apply to types",
504 /* If we require a type, but were passed a decl, set up to make a
505 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
506 would have applied if we'd been passed a type, but we cannot modify
507 the decl's type in place here. */
508 if (spec->type_required && DECL_P (*anode))
510 anode = &TREE_TYPE (*anode);
511 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
514 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
515 && TREE_CODE (*anode) != METHOD_TYPE)
517 if (TREE_CODE (*anode) == POINTER_TYPE
518 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
519 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
521 /* OK, this is a bit convoluted. We can't just make a copy
522 of the pointer type and modify its TREE_TYPE, because if
523 we change the attributes of the target type the pointer
524 type needs to have a different TYPE_MAIN_VARIANT. So we
525 pull out the target type now, frob it as appropriate, and
526 rebuild the pointer type later.
528 This would all be simpler if attributes were part of the
529 declarator, grumble grumble. */
530 fn_ptr_tmp = TREE_TYPE (*anode);
531 fn_ptr_quals = TYPE_QUALS (*anode);
533 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
535 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
537 /* Pass on this attribute to be tried again. */
538 returned_attrs = tree_cons (name, args, returned_attrs);
542 if (TREE_CODE (*anode) != FUNCTION_TYPE
543 && TREE_CODE (*anode) != METHOD_TYPE)
545 warning (OPT_Wattributes,
546 "%qE attribute only applies to function types",
553 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
554 && TYPE_SIZE (*anode) != NULL_TREE)
556 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
560 if (spec->handler != NULL)
563 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
565 returned_attrs = chainon ((*spec->handler) (anode, name, args,
571 /* Layout the decl in case anything changed. */
572 if (spec->type_required && DECL_P (*node)
574 || TREE_CODE (*node) == PARM_DECL
575 || TREE_CODE (*node) == RESULT_DECL))
576 relayout_decl (*node);
584 old_attrs = DECL_ATTRIBUTES (*anode);
586 old_attrs = TYPE_ATTRIBUTES (*anode);
588 for (a = lookup_attribute (spec->name, old_attrs);
590 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
592 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
598 /* This attribute isn't already in the list. */
600 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
601 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
603 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
604 /* If this is the main variant, also push the attributes
605 out to the other variants. */
606 if (*anode == TYPE_MAIN_VARIANT (*anode))
609 for (variant = *anode; variant;
610 variant = TYPE_NEXT_VARIANT (variant))
612 if (TYPE_ATTRIBUTES (variant) == old_attrs)
613 TYPE_ATTRIBUTES (variant)
614 = TYPE_ATTRIBUTES (*anode);
615 else if (!lookup_attribute
616 (spec->name, TYPE_ATTRIBUTES (variant)))
617 TYPE_ATTRIBUTES (variant) = tree_cons
618 (name, args, TYPE_ATTRIBUTES (variant));
623 *anode = build_type_attribute_variant (*anode,
624 tree_cons (name, args,
631 /* Rebuild the function pointer type and put it in the
632 appropriate place. */
633 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
635 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
637 TREE_TYPE (*node) = fn_ptr_tmp;
640 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
646 return returned_attrs;
649 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
652 When G++ parses a C++11 attribute, it is represented as
653 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
654 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
655 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
656 use get_attribute_namespace and get_attribute_name to retrieve the
657 namespace and name of the attribute, as these accessors work with
658 GNU attributes as well. */
661 cxx11_attribute_p (const_tree attr)
663 if (attr == NULL_TREE
664 || TREE_CODE (attr) != TREE_LIST)
667 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
670 /* Return the name of the attribute ATTR. This accessor works on GNU
671 and C++11 (scoped) attributes.
673 Please read the comments of cxx11_attribute_p to understand the
674 format of attributes. */
677 get_attribute_name (const_tree attr)
679 if (cxx11_attribute_p (attr))
680 return TREE_VALUE (TREE_PURPOSE (attr));
681 return TREE_PURPOSE (attr);
684 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
685 to the method FNDECL. */
688 apply_tm_attr (tree fndecl, tree attr)
690 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
693 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
697 make_attribute (const char *name, const char *arg_name, tree chain)
704 attr_name = get_identifier (name);
705 attr_arg_name = build_string (strlen (arg_name), arg_name);
706 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
707 attr = tree_cons (attr_name, attr_args, chain);
712 /* Common functions used for target clone support. */
714 /* Comparator function to be used in qsort routine to sort attribute
715 specification strings to "target". */
718 attr_strcmp (const void *v1, const void *v2)
720 const char *c1 = *(char *const*)v1;
721 const char *c2 = *(char *const*)v2;
722 return strcmp (c1, c2);
725 /* ARGLIST is the argument to target attribute. This function tokenizes
726 the comma separated arguments, sorts them and returns a string which
727 is a unique identifier for the comma separated arguments. It also
728 replaces non-identifier characters "=,-" with "_". */
731 sorted_attr_string (tree arglist)
734 size_t str_len_sum = 0;
736 char *attr_str, *ret_str;
738 unsigned int argnum = 1;
741 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
743 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
744 size_t len = strlen (str);
745 str_len_sum += len + 1;
748 for (i = 0; i < strlen (str); i++)
753 attr_str = XNEWVEC (char, str_len_sum);
755 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
757 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
758 size_t len = strlen (str);
759 memcpy (attr_str + str_len_sum, str, len);
760 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
761 str_len_sum += len + 1;
764 /* Replace "=,-" with "_". */
765 for (i = 0; i < strlen (attr_str); i++)
766 if (attr_str[i] == '=' || attr_str[i]== '-')
772 args = XNEWVEC (char *, argnum);
775 attr = strtok (attr_str, ",");
780 attr = strtok (NULL, ",");
783 qsort (args, argnum, sizeof (char *), attr_strcmp);
785 ret_str = XNEWVEC (char, str_len_sum);
787 for (i = 0; i < argnum; i++)
789 size_t len = strlen (args[i]);
790 memcpy (ret_str + str_len_sum, args[i], len);
791 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
792 str_len_sum += len + 1;
796 XDELETEVEC (attr_str);
801 /* This function returns true if FN1 and FN2 are versions of the same function,
802 that is, the target strings of the function decls are different. This assumes
803 that FN1 and FN2 have the same signature. */
806 common_function_versions (tree fn1, tree fn2)
809 char *target1, *target2;
812 if (TREE_CODE (fn1) != FUNCTION_DECL
813 || TREE_CODE (fn2) != FUNCTION_DECL)
816 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
817 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
819 /* At least one function decl should have the target attribute specified. */
820 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
823 /* Diagnose missing target attribute if one of the decls is already
825 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
827 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
829 if (attr2 != NULL_TREE)
831 std::swap (fn1, fn2);
834 error_at (DECL_SOURCE_LOCATION (fn2),
835 "missing %<target%> attribute for multi-versioned %qD",
837 inform (DECL_SOURCE_LOCATION (fn1),
838 "previous declaration of %qD", fn1);
839 /* Prevent diagnosing of the same error multiple times. */
840 DECL_ATTRIBUTES (fn2)
841 = tree_cons (get_identifier ("target"),
842 copy_node (TREE_VALUE (attr1)),
843 DECL_ATTRIBUTES (fn2));
848 target1 = sorted_attr_string (TREE_VALUE (attr1));
849 target2 = sorted_attr_string (TREE_VALUE (attr2));
851 /* The sorted target strings must be different for fn1 and fn2
853 if (strcmp (target1, target2) == 0)
858 XDELETEVEC (target1);
859 XDELETEVEC (target2);
864 /* Return a new name by appending SUFFIX to the DECL name. If make_unique
865 is true, append the full path name of the source file. */
868 make_unique_name (tree decl, const char *suffix, bool make_unique)
870 char *global_var_name;
873 const char *unique_name = NULL;
875 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
877 /* Get a unique name that can be used globally without any chances
878 of collision at link time. */
880 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
882 name_len = strlen (name) + strlen (suffix) + 2;
885 name_len += strlen (unique_name) + 1;
886 global_var_name = XNEWVEC (char, name_len);
888 /* Use '.' to concatenate names as it is demangler friendly. */
890 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
893 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
895 return global_var_name;
898 /* Make a dispatcher declaration for the multi-versioned function DECL.
899 Calls to DECL function will be replaced with calls to the dispatcher
900 by the front-end. Return the decl created. */
903 make_dispatcher_decl (const tree decl)
907 tree fn_type, func_type;
909 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
911 fn_type = TREE_TYPE (decl);
912 func_type = build_function_type (TREE_TYPE (fn_type),
913 TYPE_ARG_TYPES (fn_type));
915 func_decl = build_fn_decl (func_name, func_type);
916 XDELETEVEC (func_name);
917 TREE_USED (func_decl) = 1;
918 DECL_CONTEXT (func_decl) = NULL_TREE;
919 DECL_INITIAL (func_decl) = error_mark_node;
920 DECL_ARTIFICIAL (func_decl) = 1;
921 /* Mark this func as external, the resolver will flip it again if
922 it gets generated. */
923 DECL_EXTERNAL (func_decl) = 1;
924 /* This will be of type IFUNCs have to be externally visible. */
925 TREE_PUBLIC (func_decl) = 1;
930 /* Returns true if decl is multi-versioned and DECL is the default function,
931 that is it is not tagged with target specific optimization. */
934 is_function_default_version (const tree decl)
936 if (TREE_CODE (decl) != FUNCTION_DECL
937 || !DECL_FUNCTION_VERSIONED (decl))
939 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
941 attr = TREE_VALUE (TREE_VALUE (attr));
942 return (TREE_CODE (attr) == STRING_CST
943 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);