Daily bump.
[gcc.git] / gcc / attribs.c
1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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 "stor-layout.h"
31 #include "langhooks.h"
32 #include "plugin.h"
33 #include "selftest.h"
34 #include "hash-set.h"
35 #include "diagnostic.h"
36 #include "pretty-print.h"
37 #include "tree-pretty-print.h"
38 #include "intl.h"
39
40 /* Table of the tables of attributes (common, language, format, machine)
41 searched. */
42 static const struct attribute_spec *attribute_tables[4];
43
44 /* Substring representation. */
45
46 struct substring
47 {
48 const char *str;
49 int length;
50 };
51
52 /* Simple hash function to avoid need to scan whole string. */
53
54 static inline hashval_t
55 substring_hash (const char *str, int l)
56 {
57 return str[0] + str[l - 1] * 256 + l * 65536;
58 }
59
60 /* Used for attribute_hash. */
61
62 struct attribute_hasher : nofree_ptr_hash <attribute_spec>
63 {
64 typedef substring *compare_type;
65 static inline hashval_t hash (const attribute_spec *);
66 static inline bool equal (const attribute_spec *, const substring *);
67 };
68
69 inline hashval_t
70 attribute_hasher::hash (const attribute_spec *spec)
71 {
72 const int l = strlen (spec->name);
73 return substring_hash (spec->name, l);
74 }
75
76 inline bool
77 attribute_hasher::equal (const attribute_spec *spec, const substring *str)
78 {
79 return (strncmp (spec->name, str->str, str->length) == 0
80 && !spec->name[str->length]);
81 }
82
83 /* Scoped attribute name representation. */
84
85 struct scoped_attributes
86 {
87 const char *ns;
88 vec<attribute_spec> attributes;
89 hash_table<attribute_hasher> *attribute_hash;
90 };
91
92 /* The table of scope attributes. */
93 static vec<scoped_attributes> attributes_table;
94
95 static scoped_attributes* find_attribute_namespace (const char*);
96 static void register_scoped_attribute (const struct attribute_spec *,
97 scoped_attributes *);
98
99 static bool attributes_initialized = false;
100
101 /* Default empty table of attributes. */
102
103 static const struct attribute_spec empty_attribute_table[] =
104 {
105 { NULL, 0, 0, false, false, false, false, NULL, NULL }
106 };
107
108 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
109 To avoid need for copying, we simply return length of the string. */
110
111 static void
112 extract_attribute_substring (struct substring *str)
113 {
114 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
115 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
116 {
117 str->length -= 4;
118 str->str += 2;
119 }
120 }
121
122 /* Insert an array of attributes ATTRIBUTES into a namespace. This
123 array must be NULL terminated. NS is the name of attribute
124 namespace. The function returns the namespace into which the
125 attributes have been registered. */
126
127 scoped_attributes *
128 register_scoped_attributes (const struct attribute_spec *attributes,
129 const char *ns)
130 {
131 scoped_attributes *result = NULL;
132
133 /* See if we already have attributes in the namespace NS. */
134 result = find_attribute_namespace (ns);
135
136 if (result == NULL)
137 {
138 /* We don't have any namespace NS yet. Create one. */
139 scoped_attributes sa;
140
141 if (attributes_table.is_empty ())
142 attributes_table.create (64);
143
144 memset (&sa, 0, sizeof (sa));
145 sa.ns = ns;
146 sa.attributes.create (64);
147 result = attributes_table.safe_push (sa);
148 result->attribute_hash = new hash_table<attribute_hasher> (200);
149 }
150
151 /* Really add the attributes to their namespace now. */
152 for (unsigned i = 0; attributes[i].name != NULL; ++i)
153 {
154 result->attributes.safe_push (attributes[i]);
155 register_scoped_attribute (&attributes[i], result);
156 }
157
158 gcc_assert (result != NULL);
159
160 return result;
161 }
162
163 /* Return the namespace which name is NS, NULL if none exist. */
164
165 static scoped_attributes*
166 find_attribute_namespace (const char* ns)
167 {
168 unsigned ix;
169 scoped_attributes *iter;
170
171 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
172 if (ns == iter->ns
173 || (iter->ns != NULL
174 && ns != NULL
175 && !strcmp (iter->ns, ns)))
176 return iter;
177 return NULL;
178 }
179
180 /* Make some sanity checks on the attribute tables. */
181
182 static void
183 check_attribute_tables (void)
184 {
185 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
186 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
187 {
188 /* The name must not begin and end with __. */
189 const char *name = attribute_tables[i][j].name;
190 int len = strlen (name);
191
192 gcc_assert (!(name[0] == '_' && name[1] == '_'
193 && name[len - 1] == '_' && name[len - 2] == '_'));
194
195 /* The minimum and maximum lengths must be consistent. */
196 gcc_assert (attribute_tables[i][j].min_length >= 0);
197
198 gcc_assert (attribute_tables[i][j].max_length == -1
199 || (attribute_tables[i][j].max_length
200 >= attribute_tables[i][j].min_length));
201
202 /* An attribute cannot require both a DECL and a TYPE. */
203 gcc_assert (!attribute_tables[i][j].decl_required
204 || !attribute_tables[i][j].type_required);
205
206 /* If an attribute requires a function type, in particular
207 it requires a type. */
208 gcc_assert (!attribute_tables[i][j].function_type_required
209 || attribute_tables[i][j].type_required);
210 }
211
212 /* Check that each name occurs just once in each table. */
213 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
214 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
215 for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++)
216 gcc_assert (strcmp (attribute_tables[i][j].name,
217 attribute_tables[i][k].name));
218
219 /* Check that no name occurs in more than one table. Names that
220 begin with '*' are exempt, and may be overridden. */
221 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
222 for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
223 for (size_t k = 0; attribute_tables[i][k].name != NULL; k++)
224 for (size_t l = 0; attribute_tables[j][l].name != NULL; l++)
225 gcc_assert (attribute_tables[i][k].name[0] == '*'
226 || strcmp (attribute_tables[i][k].name,
227 attribute_tables[j][l].name));
228 }
229
230 /* Initialize attribute tables, and make some sanity checks if checking is
231 enabled. */
232
233 void
234 init_attributes (void)
235 {
236 size_t i;
237
238 if (attributes_initialized)
239 return;
240
241 attribute_tables[0] = lang_hooks.common_attribute_table;
242 attribute_tables[1] = lang_hooks.attribute_table;
243 attribute_tables[2] = lang_hooks.format_attribute_table;
244 attribute_tables[3] = targetm.attribute_table;
245
246 /* Translate NULL pointers to pointers to the empty table. */
247 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
248 if (attribute_tables[i] == NULL)
249 attribute_tables[i] = empty_attribute_table;
250
251 if (flag_checking)
252 check_attribute_tables ();
253
254 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
255 /* Put all the GNU attributes into the "gnu" namespace. */
256 register_scoped_attributes (attribute_tables[i], "gnu");
257
258 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
259 attributes_initialized = true;
260 }
261
262 /* Insert a single ATTR into the attribute table. */
263
264 void
265 register_attribute (const struct attribute_spec *attr)
266 {
267 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
268 }
269
270 /* Insert a single attribute ATTR into a namespace of attributes. */
271
272 static void
273 register_scoped_attribute (const struct attribute_spec *attr,
274 scoped_attributes *name_space)
275 {
276 struct substring str;
277 attribute_spec **slot;
278
279 gcc_assert (attr != NULL && name_space != NULL);
280
281 gcc_assert (name_space->attribute_hash);
282
283 str.str = attr->name;
284 str.length = strlen (str.str);
285
286 /* Attribute names in the table must be in the form 'text' and not
287 in the form '__text__'. */
288 gcc_assert (str.length > 0 && str.str[0] != '_');
289
290 slot = name_space->attribute_hash
291 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
292 INSERT);
293 gcc_assert (!*slot || attr->name[0] == '*');
294 *slot = CONST_CAST (struct attribute_spec *, attr);
295 }
296
297 /* Return the spec for the scoped attribute with namespace NS and
298 name NAME. */
299
300 static const struct attribute_spec *
301 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
302 {
303 struct substring attr;
304 scoped_attributes *attrs;
305
306 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
307
308 attrs = find_attribute_namespace (ns_str);
309
310 if (attrs == NULL)
311 return NULL;
312
313 attr.str = IDENTIFIER_POINTER (name);
314 attr.length = IDENTIFIER_LENGTH (name);
315 extract_attribute_substring (&attr);
316 return attrs->attribute_hash->find_with_hash (&attr,
317 substring_hash (attr.str,
318 attr.length));
319 }
320
321 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
322 it also specifies the attribute namespace. */
323
324 const struct attribute_spec *
325 lookup_attribute_spec (const_tree name)
326 {
327 tree ns;
328 if (TREE_CODE (name) == TREE_LIST)
329 {
330 ns = TREE_PURPOSE (name);
331 name = TREE_VALUE (name);
332 }
333 else
334 ns = get_identifier ("gnu");
335 return lookup_scoped_attribute_spec (ns, name);
336 }
337
338
339 /* Return the namespace of the attribute ATTR. This accessor works on
340 GNU and C++11 (scoped) attributes. On GNU attributes,
341 it returns an identifier tree for the string "gnu".
342
343 Please read the comments of cxx11_attribute_p to understand the
344 format of attributes. */
345
346 tree
347 get_attribute_namespace (const_tree attr)
348 {
349 if (cxx11_attribute_p (attr))
350 return TREE_PURPOSE (TREE_PURPOSE (attr));
351 return get_identifier ("gnu");
352 }
353
354 /* Check LAST_DECL and NODE of the same symbol for attributes that are
355 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
356 them, and return true if any have been found. NODE can be a DECL
357 or a TYPE. */
358
359 static bool
360 diag_attr_exclusions (tree last_decl, tree node, tree attrname,
361 const attribute_spec *spec)
362 {
363 const attribute_spec::exclusions *excl = spec->exclude;
364
365 tree_code code = TREE_CODE (node);
366
367 if ((code == FUNCTION_DECL && !excl->function
368 && (!excl->type || !spec->affects_type_identity))
369 || (code == VAR_DECL && !excl->variable
370 && (!excl->type || !spec->affects_type_identity))
371 || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
372 return false;
373
374 /* True if an attribute that's mutually exclusive with ATTRNAME
375 has been found. */
376 bool found = false;
377
378 if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
379 {
380 /* Check both the last DECL and its type for conflicts with
381 the attribute being added to the current decl or type. */
382 found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
383 tree decl_type = TREE_TYPE (last_decl);
384 found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
385 }
386
387 /* NODE is either the current DECL to which the attribute is being
388 applied or its TYPE. For the former, consider the attributes on
389 both the DECL and its type. */
390 tree attrs[2];
391
392 if (DECL_P (node))
393 {
394 attrs[0] = DECL_ATTRIBUTES (node);
395 attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
396 }
397 else
398 {
399 attrs[0] = TYPE_ATTRIBUTES (node);
400 attrs[1] = NULL_TREE;
401 }
402
403 /* Iterate over the mutually exclusive attribute names and verify
404 that the symbol doesn't contain it. */
405 for (unsigned i = 0; i != sizeof attrs / sizeof *attrs; ++i)
406 {
407 if (!attrs[i])
408 continue;
409
410 for ( ; excl->name; ++excl)
411 {
412 /* Avoid checking the attribute against itself. */
413 if (is_attribute_p (excl->name, attrname))
414 continue;
415
416 if (!lookup_attribute (excl->name, attrs[i]))
417 continue;
418
419 /* An exclusion may apply either to a function declaration,
420 type declaration, or a field/variable declaration, or
421 any subset of the three. */
422 if (TREE_CODE (node) == FUNCTION_DECL
423 && !excl->function)
424 continue;
425
426 if (TREE_CODE (node) == TYPE_DECL
427 && !excl->type)
428 continue;
429
430 if ((TREE_CODE (node) == FIELD_DECL
431 || TREE_CODE (node) == VAR_DECL)
432 && !excl->variable)
433 continue;
434
435 found = true;
436
437 /* Print a note? */
438 bool note = last_decl != NULL_TREE;
439 auto_diagnostic_group d;
440 if (TREE_CODE (node) == FUNCTION_DECL
441 && fndecl_built_in_p (node))
442 note &= warning (OPT_Wattributes,
443 "ignoring attribute %qE in declaration of "
444 "a built-in function %qD because it conflicts "
445 "with attribute %qs",
446 attrname, node, excl->name);
447 else
448 note &= warning (OPT_Wattributes,
449 "ignoring attribute %qE because "
450 "it conflicts with attribute %qs",
451 attrname, excl->name);
452
453 if (note)
454 inform (DECL_SOURCE_LOCATION (last_decl),
455 "previous declaration here");
456 }
457 }
458
459 return found;
460 }
461
462 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
463 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
464 it should be modified in place; if a TYPE, a copy should be created
465 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
466 information, in the form of a bitwise OR of flags in enum attribute_flags
467 from tree.h. Depending on these flags, some attributes may be
468 returned to be applied at a later stage (for example, to apply
469 a decl attribute to the declaration rather than to its type). */
470
471 tree
472 decl_attributes (tree *node, tree attributes, int flags,
473 tree last_decl /* = NULL_TREE */)
474 {
475 tree returned_attrs = NULL_TREE;
476
477 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
478 return NULL_TREE;
479
480 if (!attributes_initialized)
481 init_attributes ();
482
483 /* If this is a function and the user used #pragma GCC optimize, add the
484 options to the attribute((optimize(...))) list. */
485 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
486 {
487 tree cur_attr = lookup_attribute ("optimize", attributes);
488 tree opts = copy_list (current_optimize_pragma);
489
490 if (! cur_attr)
491 attributes
492 = tree_cons (get_identifier ("optimize"), opts, attributes);
493 else
494 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
495 }
496
497 if (TREE_CODE (*node) == FUNCTION_DECL
498 && optimization_current_node != optimization_default_node
499 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
500 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
501
502 /* If this is a function and the user used #pragma GCC target, add the
503 options to the attribute((target(...))) list. */
504 if (TREE_CODE (*node) == FUNCTION_DECL
505 && current_target_pragma
506 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
507 current_target_pragma, 0))
508 {
509 tree cur_attr = lookup_attribute ("target", attributes);
510 tree opts = copy_list (current_target_pragma);
511
512 if (! cur_attr)
513 attributes = tree_cons (get_identifier ("target"), opts, attributes);
514 else
515 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
516 }
517
518 /* A "naked" function attribute implies "noinline" and "noclone" for
519 those targets that support it. */
520 if (TREE_CODE (*node) == FUNCTION_DECL
521 && attributes
522 && lookup_attribute ("naked", attributes) != NULL
523 && lookup_attribute_spec (get_identifier ("naked")))
524 {
525 if (lookup_attribute ("noinline", attributes) == NULL)
526 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
527
528 if (lookup_attribute ("noclone", attributes) == NULL)
529 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
530 }
531
532 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
533 for those targets that support it. */
534 if (TREE_CODE (*node) == FUNCTION_DECL
535 && attributes
536 && lookup_attribute ("noipa", attributes) != NULL
537 && lookup_attribute_spec (get_identifier ("noipa")))
538 {
539 if (lookup_attribute ("noinline", attributes) == NULL)
540 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
541
542 if (lookup_attribute ("noclone", attributes) == NULL)
543 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
544
545 if (lookup_attribute ("no_icf", attributes) == NULL)
546 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
547 }
548
549 targetm.insert_attributes (*node, &attributes);
550
551 /* Note that attributes on the same declaration are not necessarily
552 in the same order as in the source. */
553 for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
554 {
555 tree ns = get_attribute_namespace (attr);
556 tree name = get_attribute_name (attr);
557 tree args = TREE_VALUE (attr);
558 tree *anode = node;
559 const struct attribute_spec *spec
560 = lookup_scoped_attribute_spec (ns, name);
561 int fn_ptr_quals = 0;
562 tree fn_ptr_tmp = NULL_TREE;
563 const bool cxx11_attr_p = cxx11_attribute_p (attr);
564
565 if (spec == NULL)
566 {
567 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
568 {
569 if (ns == NULL_TREE || !cxx11_attr_p)
570 warning (OPT_Wattributes, "%qE attribute directive ignored",
571 name);
572 else
573 warning (OPT_Wattributes,
574 "%<%E::%E%> scoped attribute directive ignored",
575 ns, name);
576 }
577 continue;
578 }
579 else
580 {
581 int nargs = list_length (args);
582 if (nargs < spec->min_length
583 || (spec->max_length >= 0
584 && nargs > spec->max_length))
585 {
586 error ("wrong number of arguments specified for %qE attribute",
587 name);
588 if (spec->max_length < 0)
589 inform (input_location, "expected %i or more, found %i",
590 spec->min_length, nargs);
591 else
592 inform (input_location, "expected between %i and %i, found %i",
593 spec->min_length, spec->max_length, nargs);
594 continue;
595 }
596 }
597 gcc_assert (is_attribute_p (spec->name, name));
598
599 if (spec->decl_required && !DECL_P (*anode))
600 {
601 if (flags & ((int) ATTR_FLAG_DECL_NEXT
602 | (int) ATTR_FLAG_FUNCTION_NEXT
603 | (int) ATTR_FLAG_ARRAY_NEXT))
604 {
605 /* Pass on this attribute to be tried again. */
606 tree attr = tree_cons (name, args, NULL_TREE);
607 returned_attrs = chainon (returned_attrs, attr);
608 continue;
609 }
610 else
611 {
612 warning (OPT_Wattributes, "%qE attribute does not apply to types",
613 name);
614 continue;
615 }
616 }
617
618 /* If we require a type, but were passed a decl, set up to make a
619 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
620 would have applied if we'd been passed a type, but we cannot modify
621 the decl's type in place here. */
622 if (spec->type_required && DECL_P (*anode))
623 {
624 anode = &TREE_TYPE (*anode);
625 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
626 }
627
628 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
629 && TREE_CODE (*anode) != METHOD_TYPE)
630 {
631 if (TREE_CODE (*anode) == POINTER_TYPE
632 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
633 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
634 {
635 /* OK, this is a bit convoluted. We can't just make a copy
636 of the pointer type and modify its TREE_TYPE, because if
637 we change the attributes of the target type the pointer
638 type needs to have a different TYPE_MAIN_VARIANT. So we
639 pull out the target type now, frob it as appropriate, and
640 rebuild the pointer type later.
641
642 This would all be simpler if attributes were part of the
643 declarator, grumble grumble. */
644 fn_ptr_tmp = TREE_TYPE (*anode);
645 fn_ptr_quals = TYPE_QUALS (*anode);
646 anode = &fn_ptr_tmp;
647 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
648 }
649 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
650 {
651 /* Pass on this attribute to be tried again. */
652 tree attr = tree_cons (name, args, NULL_TREE);
653 returned_attrs = chainon (returned_attrs, attr);
654 continue;
655 }
656
657 if (TREE_CODE (*anode) != FUNCTION_TYPE
658 && TREE_CODE (*anode) != METHOD_TYPE)
659 {
660 warning (OPT_Wattributes,
661 "%qE attribute only applies to function types",
662 name);
663 continue;
664 }
665 }
666
667 if (TYPE_P (*anode)
668 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
669 && TYPE_SIZE (*anode) != NULL_TREE)
670 {
671 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
672 continue;
673 }
674
675 bool no_add_attrs = false;
676
677 /* Check for exclusions with other attributes on the current
678 declation as well as the last declaration of the same
679 symbol already processed (if one exists). Detect and
680 reject incompatible attributes. */
681 bool built_in = flags & ATTR_FLAG_BUILT_IN;
682 if (spec->exclude
683 && (flag_checking || !built_in)
684 && !error_operand_p (last_decl))
685 {
686 /* Always check attributes on user-defined functions.
687 Check them on built-ins only when -fchecking is set.
688 Ignore __builtin_unreachable -- it's both const and
689 noreturn. */
690
691 if (!built_in
692 || !DECL_P (*anode)
693 || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
694 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
695 && (DECL_FUNCTION_CODE (*anode)
696 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
697 {
698 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
699 if (!no_add && anode != node)
700 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
701 no_add_attrs |= no_add;
702 }
703 }
704
705 if (no_add_attrs)
706 continue;
707
708 if (spec->handler != NULL)
709 {
710 int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0);
711
712 /* Pass in an array of the current declaration followed
713 by the last pushed/merged declaration if one exists.
714 For calls that modify the type attributes of a DECL
715 and for which *ANODE is *NODE's type, also pass in
716 the DECL as the third element to use in diagnostics.
717 If the handler changes CUR_AND_LAST_DECL[0] replace
718 *ANODE with its value. */
719 tree cur_and_last_decl[3] = { *anode, last_decl };
720 if (anode != node && DECL_P (*node))
721 cur_and_last_decl[2] = *node;
722
723 tree ret = (spec->handler) (cur_and_last_decl, name, args,
724 flags|cxx11_flag, &no_add_attrs);
725
726 *anode = cur_and_last_decl[0];
727 if (ret == error_mark_node)
728 {
729 warning (OPT_Wattributes, "%qE attribute ignored", name);
730 no_add_attrs = true;
731 }
732 else
733 returned_attrs = chainon (ret, returned_attrs);
734 }
735
736 /* Layout the decl in case anything changed. */
737 if (spec->type_required && DECL_P (*node)
738 && (VAR_P (*node)
739 || TREE_CODE (*node) == PARM_DECL
740 || TREE_CODE (*node) == RESULT_DECL))
741 relayout_decl (*node);
742
743 if (!no_add_attrs)
744 {
745 tree old_attrs;
746 tree a;
747
748 if (DECL_P (*anode))
749 old_attrs = DECL_ATTRIBUTES (*anode);
750 else
751 old_attrs = TYPE_ATTRIBUTES (*anode);
752
753 for (a = lookup_attribute (spec->name, old_attrs);
754 a != NULL_TREE;
755 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
756 {
757 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
758 break;
759 }
760
761 if (a == NULL_TREE)
762 {
763 /* This attribute isn't already in the list. */
764 tree r;
765 /* Preserve the C++11 form. */
766 if (cxx11_attr_p)
767 r = tree_cons (build_tree_list (ns, name), args, old_attrs);
768 else
769 r = tree_cons (name, args, old_attrs);
770
771 if (DECL_P (*anode))
772 DECL_ATTRIBUTES (*anode) = r;
773 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
774 {
775 TYPE_ATTRIBUTES (*anode) = r;
776 /* If this is the main variant, also push the attributes
777 out to the other variants. */
778 if (*anode == TYPE_MAIN_VARIANT (*anode))
779 {
780 for (tree variant = *anode; variant;
781 variant = TYPE_NEXT_VARIANT (variant))
782 {
783 if (TYPE_ATTRIBUTES (variant) == old_attrs)
784 TYPE_ATTRIBUTES (variant)
785 = TYPE_ATTRIBUTES (*anode);
786 else if (!lookup_attribute
787 (spec->name, TYPE_ATTRIBUTES (variant)))
788 TYPE_ATTRIBUTES (variant) = tree_cons
789 (name, args, TYPE_ATTRIBUTES (variant));
790 }
791 }
792 }
793 else
794 *anode = build_type_attribute_variant (*anode, r);
795 }
796 }
797
798 if (fn_ptr_tmp)
799 {
800 /* Rebuild the function pointer type and put it in the
801 appropriate place. */
802 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
803 if (fn_ptr_quals)
804 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
805 if (DECL_P (*node))
806 TREE_TYPE (*node) = fn_ptr_tmp;
807 else
808 {
809 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
810 *node = fn_ptr_tmp;
811 }
812 }
813 }
814
815 return returned_attrs;
816 }
817
818 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
819 attribute.
820
821 When G++ parses a C++11 attribute, it is represented as
822 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
823 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
824 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
825 use get_attribute_namespace and get_attribute_name to retrieve the
826 namespace and name of the attribute, as these accessors work with
827 GNU attributes as well. */
828
829 bool
830 cxx11_attribute_p (const_tree attr)
831 {
832 if (attr == NULL_TREE
833 || TREE_CODE (attr) != TREE_LIST)
834 return false;
835
836 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
837 }
838
839 /* Return the name of the attribute ATTR. This accessor works on GNU
840 and C++11 (scoped) attributes.
841
842 Please read the comments of cxx11_attribute_p to understand the
843 format of attributes. */
844
845 tree
846 get_attribute_name (const_tree attr)
847 {
848 if (cxx11_attribute_p (attr))
849 return TREE_VALUE (TREE_PURPOSE (attr));
850 return TREE_PURPOSE (attr);
851 }
852
853 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
854 to the method FNDECL. */
855
856 void
857 apply_tm_attr (tree fndecl, tree attr)
858 {
859 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
860 }
861
862 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
863 it to CHAIN. */
864
865 tree
866 make_attribute (const char *name, const char *arg_name, tree chain)
867 {
868 tree attr_name;
869 tree attr_arg_name;
870 tree attr_args;
871 tree attr;
872
873 attr_name = get_identifier (name);
874 attr_arg_name = build_string (strlen (arg_name), arg_name);
875 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
876 attr = tree_cons (attr_name, attr_args, chain);
877 return attr;
878 }
879
880 \f
881 /* Common functions used for target clone support. */
882
883 /* Comparator function to be used in qsort routine to sort attribute
884 specification strings to "target". */
885
886 static int
887 attr_strcmp (const void *v1, const void *v2)
888 {
889 const char *c1 = *(char *const*)v1;
890 const char *c2 = *(char *const*)v2;
891 return strcmp (c1, c2);
892 }
893
894 /* ARGLIST is the argument to target attribute. This function tokenizes
895 the comma separated arguments, sorts them and returns a string which
896 is a unique identifier for the comma separated arguments. It also
897 replaces non-identifier characters "=,-" with "_". */
898
899 char *
900 sorted_attr_string (tree arglist)
901 {
902 tree arg;
903 size_t str_len_sum = 0;
904 char **args = NULL;
905 char *attr_str, *ret_str;
906 char *attr = NULL;
907 unsigned int argnum = 1;
908 unsigned int i;
909
910 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
911 {
912 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
913 size_t len = strlen (str);
914 str_len_sum += len + 1;
915 if (arg != arglist)
916 argnum++;
917 for (i = 0; i < strlen (str); i++)
918 if (str[i] == ',')
919 argnum++;
920 }
921
922 attr_str = XNEWVEC (char, str_len_sum);
923 str_len_sum = 0;
924 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
925 {
926 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
927 size_t len = strlen (str);
928 memcpy (attr_str + str_len_sum, str, len);
929 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
930 str_len_sum += len + 1;
931 }
932
933 /* Replace "=,-" with "_". */
934 for (i = 0; i < strlen (attr_str); i++)
935 if (attr_str[i] == '=' || attr_str[i]== '-')
936 attr_str[i] = '_';
937
938 if (argnum == 1)
939 return attr_str;
940
941 args = XNEWVEC (char *, argnum);
942
943 i = 0;
944 attr = strtok (attr_str, ",");
945 while (attr != NULL)
946 {
947 args[i] = attr;
948 i++;
949 attr = strtok (NULL, ",");
950 }
951
952 qsort (args, argnum, sizeof (char *), attr_strcmp);
953
954 ret_str = XNEWVEC (char, str_len_sum);
955 str_len_sum = 0;
956 for (i = 0; i < argnum; i++)
957 {
958 size_t len = strlen (args[i]);
959 memcpy (ret_str + str_len_sum, args[i], len);
960 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
961 str_len_sum += len + 1;
962 }
963
964 XDELETEVEC (args);
965 XDELETEVEC (attr_str);
966 return ret_str;
967 }
968
969
970 /* This function returns true if FN1 and FN2 are versions of the same function,
971 that is, the target strings of the function decls are different. This assumes
972 that FN1 and FN2 have the same signature. */
973
974 bool
975 common_function_versions (tree fn1, tree fn2)
976 {
977 tree attr1, attr2;
978 char *target1, *target2;
979 bool result;
980
981 if (TREE_CODE (fn1) != FUNCTION_DECL
982 || TREE_CODE (fn2) != FUNCTION_DECL)
983 return false;
984
985 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
986 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
987
988 /* At least one function decl should have the target attribute specified. */
989 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
990 return false;
991
992 /* Diagnose missing target attribute if one of the decls is already
993 multi-versioned. */
994 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
995 {
996 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
997 {
998 if (attr2 != NULL_TREE)
999 {
1000 std::swap (fn1, fn2);
1001 attr1 = attr2;
1002 }
1003 error_at (DECL_SOURCE_LOCATION (fn2),
1004 "missing %<target%> attribute for multi-versioned %qD",
1005 fn2);
1006 inform (DECL_SOURCE_LOCATION (fn1),
1007 "previous declaration of %qD", fn1);
1008 /* Prevent diagnosing of the same error multiple times. */
1009 DECL_ATTRIBUTES (fn2)
1010 = tree_cons (get_identifier ("target"),
1011 copy_node (TREE_VALUE (attr1)),
1012 DECL_ATTRIBUTES (fn2));
1013 }
1014 return false;
1015 }
1016
1017 target1 = sorted_attr_string (TREE_VALUE (attr1));
1018 target2 = sorted_attr_string (TREE_VALUE (attr2));
1019
1020 /* The sorted target strings must be different for fn1 and fn2
1021 to be versions. */
1022 if (strcmp (target1, target2) == 0)
1023 result = false;
1024 else
1025 result = true;
1026
1027 XDELETEVEC (target1);
1028 XDELETEVEC (target2);
1029
1030 return result;
1031 }
1032
1033 /* Return a new name by appending SUFFIX to the DECL name. If make_unique
1034 is true, append the full path name of the source file. */
1035
1036 char *
1037 make_unique_name (tree decl, const char *suffix, bool make_unique)
1038 {
1039 char *global_var_name;
1040 int name_len;
1041 const char *name;
1042 const char *unique_name = NULL;
1043
1044 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1045
1046 /* Get a unique name that can be used globally without any chances
1047 of collision at link time. */
1048 if (make_unique)
1049 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
1050
1051 name_len = strlen (name) + strlen (suffix) + 2;
1052
1053 if (make_unique)
1054 name_len += strlen (unique_name) + 1;
1055 global_var_name = XNEWVEC (char, name_len);
1056
1057 /* Use '.' to concatenate names as it is demangler friendly. */
1058 if (make_unique)
1059 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
1060 suffix);
1061 else
1062 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
1063
1064 return global_var_name;
1065 }
1066
1067 /* Make a dispatcher declaration for the multi-versioned function DECL.
1068 Calls to DECL function will be replaced with calls to the dispatcher
1069 by the front-end. Return the decl created. */
1070
1071 tree
1072 make_dispatcher_decl (const tree decl)
1073 {
1074 tree func_decl;
1075 char *func_name;
1076 tree fn_type, func_type;
1077
1078 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1079
1080 fn_type = TREE_TYPE (decl);
1081 func_type = build_function_type (TREE_TYPE (fn_type),
1082 TYPE_ARG_TYPES (fn_type));
1083
1084 func_decl = build_fn_decl (func_name, func_type);
1085 XDELETEVEC (func_name);
1086 TREE_USED (func_decl) = 1;
1087 DECL_CONTEXT (func_decl) = NULL_TREE;
1088 DECL_INITIAL (func_decl) = error_mark_node;
1089 DECL_ARTIFICIAL (func_decl) = 1;
1090 /* Mark this func as external, the resolver will flip it again if
1091 it gets generated. */
1092 DECL_EXTERNAL (func_decl) = 1;
1093 /* This will be of type IFUNCs have to be externally visible. */
1094 TREE_PUBLIC (func_decl) = 1;
1095
1096 return func_decl;
1097 }
1098
1099 /* Returns true if decl is multi-versioned and DECL is the default function,
1100 that is it is not tagged with target specific optimization. */
1101
1102 bool
1103 is_function_default_version (const tree decl)
1104 {
1105 if (TREE_CODE (decl) != FUNCTION_DECL
1106 || !DECL_FUNCTION_VERSIONED (decl))
1107 return false;
1108 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1109 gcc_assert (attr);
1110 attr = TREE_VALUE (TREE_VALUE (attr));
1111 return (TREE_CODE (attr) == STRING_CST
1112 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1113 }
1114
1115 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1116 is ATTRIBUTE. */
1117
1118 tree
1119 build_decl_attribute_variant (tree ddecl, tree attribute)
1120 {
1121 DECL_ATTRIBUTES (ddecl) = attribute;
1122 return ddecl;
1123 }
1124
1125 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1126 is ATTRIBUTE and its qualifiers are QUALS.
1127
1128 Record such modified types already made so we don't make duplicates. */
1129
1130 tree
1131 build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
1132 {
1133 tree ttype = otype;
1134 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1135 {
1136 tree ntype;
1137
1138 /* Building a distinct copy of a tagged type is inappropriate; it
1139 causes breakage in code that expects there to be a one-to-one
1140 relationship between a struct and its fields.
1141 build_duplicate_type is another solution (as used in
1142 handle_transparent_union_attribute), but that doesn't play well
1143 with the stronger C++ type identity model. */
1144 if (TREE_CODE (ttype) == RECORD_TYPE
1145 || TREE_CODE (ttype) == UNION_TYPE
1146 || TREE_CODE (ttype) == QUAL_UNION_TYPE
1147 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1148 {
1149 warning (OPT_Wattributes,
1150 "ignoring attributes applied to %qT after definition",
1151 TYPE_MAIN_VARIANT (ttype));
1152 return build_qualified_type (ttype, quals);
1153 }
1154
1155 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
1156 if (lang_hooks.types.copy_lang_qualifiers
1157 && otype != TYPE_MAIN_VARIANT (otype))
1158 ttype = (lang_hooks.types.copy_lang_qualifiers
1159 (ttype, TYPE_MAIN_VARIANT (otype)));
1160
1161 tree dtype = ntype = build_distinct_type_copy (ttype);
1162
1163 TYPE_ATTRIBUTES (ntype) = attribute;
1164
1165 hashval_t hash = type_hash_canon_hash (ntype);
1166 ntype = type_hash_canon (hash, ntype);
1167
1168 if (ntype != dtype)
1169 /* This variant was already in the hash table, don't mess with
1170 TYPE_CANONICAL. */;
1171 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1172 || !comp_type_attributes (ntype, ttype))
1173 /* If the target-dependent attributes make NTYPE different from
1174 its canonical type, we will need to use structural equality
1175 checks for this type.
1176
1177 We shouldn't get here for stripping attributes from a type;
1178 the no-attribute type might not need structural comparison. But
1179 we can if was discarded from type_hash_table. */
1180 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
1181 else if (TYPE_CANONICAL (ntype) == ntype)
1182 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1183
1184 ttype = build_qualified_type (ntype, quals);
1185 if (lang_hooks.types.copy_lang_qualifiers
1186 && otype != TYPE_MAIN_VARIANT (otype))
1187 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
1188 }
1189 else if (TYPE_QUALS (ttype) != quals)
1190 ttype = build_qualified_type (ttype, quals);
1191
1192 return ttype;
1193 }
1194
1195 /* Compare two identifier nodes representing attributes.
1196 Return true if they are the same, false otherwise. */
1197
1198 static bool
1199 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1200 {
1201 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1202 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1203 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1204
1205 /* Identifiers can be compared directly for equality. */
1206 if (attr1 == attr2)
1207 return true;
1208
1209 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1210 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1211 }
1212
1213 /* Compare two constructor-element-type constants. Return 1 if the lists
1214 are known to be equal; otherwise return 0. */
1215
1216 static bool
1217 simple_cst_list_equal (const_tree l1, const_tree l2)
1218 {
1219 while (l1 != NULL_TREE && l2 != NULL_TREE)
1220 {
1221 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1222 return false;
1223
1224 l1 = TREE_CHAIN (l1);
1225 l2 = TREE_CHAIN (l2);
1226 }
1227
1228 return l1 == l2;
1229 }
1230
1231 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1232 the same. */
1233
1234 static bool
1235 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1236 {
1237 tree cl1, cl2;
1238 for (cl1 = clauses1, cl2 = clauses2;
1239 cl1 && cl2;
1240 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1241 {
1242 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1243 return false;
1244 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1245 {
1246 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1247 OMP_CLAUSE_DECL (cl2)) != 1)
1248 return false;
1249 }
1250 switch (OMP_CLAUSE_CODE (cl1))
1251 {
1252 case OMP_CLAUSE_ALIGNED:
1253 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1254 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1255 return false;
1256 break;
1257 case OMP_CLAUSE_LINEAR:
1258 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1259 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1260 return false;
1261 break;
1262 case OMP_CLAUSE_SIMDLEN:
1263 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1264 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1265 return false;
1266 default:
1267 break;
1268 }
1269 }
1270 return true;
1271 }
1272
1273
1274 /* Compare two attributes for their value identity. Return true if the
1275 attribute values are known to be equal; otherwise return false. */
1276
1277 bool
1278 attribute_value_equal (const_tree attr1, const_tree attr2)
1279 {
1280 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1281 return true;
1282
1283 if (TREE_VALUE (attr1) != NULL_TREE
1284 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1285 && TREE_VALUE (attr2) != NULL_TREE
1286 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1287 {
1288 /* Handle attribute format. */
1289 if (is_attribute_p ("format", get_attribute_name (attr1)))
1290 {
1291 attr1 = TREE_VALUE (attr1);
1292 attr2 = TREE_VALUE (attr2);
1293 /* Compare the archetypes (printf/scanf/strftime/...). */
1294 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1295 return false;
1296 /* Archetypes are the same. Compare the rest. */
1297 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1298 TREE_CHAIN (attr2)) == 1);
1299 }
1300 return (simple_cst_list_equal (TREE_VALUE (attr1),
1301 TREE_VALUE (attr2)) == 1);
1302 }
1303
1304 if (TREE_VALUE (attr1)
1305 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
1306 && TREE_VALUE (attr2)
1307 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1308 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1309 TREE_VALUE (attr2));
1310
1311 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1312 }
1313
1314 /* Return 0 if the attributes for two types are incompatible, 1 if they
1315 are compatible, and 2 if they are nearly compatible (which causes a
1316 warning to be generated). */
1317 int
1318 comp_type_attributes (const_tree type1, const_tree type2)
1319 {
1320 const_tree a1 = TYPE_ATTRIBUTES (type1);
1321 const_tree a2 = TYPE_ATTRIBUTES (type2);
1322 const_tree a;
1323
1324 if (a1 == a2)
1325 return 1;
1326 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1327 {
1328 const struct attribute_spec *as;
1329 const_tree attr;
1330
1331 as = lookup_attribute_spec (get_attribute_name (a));
1332 if (!as || as->affects_type_identity == false)
1333 continue;
1334
1335 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
1336 if (!attr || !attribute_value_equal (a, attr))
1337 break;
1338 }
1339 if (!a)
1340 {
1341 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1342 {
1343 const struct attribute_spec *as;
1344
1345 as = lookup_attribute_spec (get_attribute_name (a));
1346 if (!as || as->affects_type_identity == false)
1347 continue;
1348
1349 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
1350 break;
1351 /* We don't need to compare trees again, as we did this
1352 already in first loop. */
1353 }
1354 /* All types - affecting identity - are equal, so
1355 there is no need to call target hook for comparison. */
1356 if (!a)
1357 return 1;
1358 }
1359 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1360 return 0;
1361 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1362 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1363 return 0;
1364 /* As some type combinations - like default calling-convention - might
1365 be compatible, we have to call the target hook to get the final result. */
1366 return targetm.comp_type_attributes (type1, type2);
1367 }
1368
1369 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1370 is ATTRIBUTE.
1371
1372 Record such modified types already made so we don't make duplicates. */
1373
1374 tree
1375 build_type_attribute_variant (tree ttype, tree attribute)
1376 {
1377 return build_type_attribute_qual_variant (ttype, attribute,
1378 TYPE_QUALS (ttype));
1379 }
1380 \f
1381 /* A variant of lookup_attribute() that can be used with an identifier
1382 as the first argument, and where the identifier can be either
1383 'text' or '__text__'.
1384
1385 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1386 return a pointer to the attribute's list element if the attribute
1387 is part of the list, or NULL_TREE if not found. If the attribute
1388 appears more than once, this only returns the first occurrence; the
1389 TREE_CHAIN of the return value should be passed back in if further
1390 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1391 can be in the form 'text' or '__text__'. */
1392 static tree
1393 lookup_ident_attribute (tree attr_identifier, tree list)
1394 {
1395 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1396
1397 while (list)
1398 {
1399 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1400 == IDENTIFIER_NODE);
1401
1402 if (cmp_attrib_identifiers (attr_identifier,
1403 get_attribute_name (list)))
1404 /* Found it. */
1405 break;
1406 list = TREE_CHAIN (list);
1407 }
1408
1409 return list;
1410 }
1411
1412 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1413 modified list. */
1414
1415 tree
1416 remove_attribute (const char *attr_name, tree list)
1417 {
1418 tree *p;
1419 gcc_checking_assert (attr_name[0] != '_');
1420
1421 for (p = &list; *p;)
1422 {
1423 tree l = *p;
1424
1425 tree attr = get_attribute_name (l);
1426 if (is_attribute_p (attr_name, attr))
1427 *p = TREE_CHAIN (l);
1428 else
1429 p = &TREE_CHAIN (l);
1430 }
1431
1432 return list;
1433 }
1434
1435 /* Return an attribute list that is the union of a1 and a2. */
1436
1437 tree
1438 merge_attributes (tree a1, tree a2)
1439 {
1440 tree attributes;
1441
1442 /* Either one unset? Take the set one. */
1443
1444 if ((attributes = a1) == 0)
1445 attributes = a2;
1446
1447 /* One that completely contains the other? Take it. */
1448
1449 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1450 {
1451 if (attribute_list_contained (a2, a1))
1452 attributes = a2;
1453 else
1454 {
1455 /* Pick the longest list, and hang on the other list. */
1456
1457 if (list_length (a1) < list_length (a2))
1458 attributes = a2, a2 = a1;
1459
1460 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1461 {
1462 tree a;
1463 for (a = lookup_ident_attribute (get_attribute_name (a2),
1464 attributes);
1465 a != NULL_TREE && !attribute_value_equal (a, a2);
1466 a = lookup_ident_attribute (get_attribute_name (a2),
1467 TREE_CHAIN (a)))
1468 ;
1469 if (a == NULL_TREE)
1470 {
1471 a1 = copy_node (a2);
1472 TREE_CHAIN (a1) = attributes;
1473 attributes = a1;
1474 }
1475 }
1476 }
1477 }
1478 return attributes;
1479 }
1480
1481 /* Given types T1 and T2, merge their attributes and return
1482 the result. */
1483
1484 tree
1485 merge_type_attributes (tree t1, tree t2)
1486 {
1487 return merge_attributes (TYPE_ATTRIBUTES (t1),
1488 TYPE_ATTRIBUTES (t2));
1489 }
1490
1491 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1492 the result. */
1493
1494 tree
1495 merge_decl_attributes (tree olddecl, tree newdecl)
1496 {
1497 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1498 DECL_ATTRIBUTES (newdecl));
1499 }
1500
1501 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1502 they are missing there. */
1503
1504 void
1505 duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1506 {
1507 attr = lookup_attribute (name, attr);
1508 if (!attr)
1509 return;
1510 tree a = lookup_attribute (name, *attrs);
1511 while (attr)
1512 {
1513 tree a2;
1514 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1515 if (attribute_value_equal (attr, a2))
1516 break;
1517 if (!a2)
1518 {
1519 a2 = copy_node (attr);
1520 TREE_CHAIN (a2) = *attrs;
1521 *attrs = a2;
1522 }
1523 attr = lookup_attribute (name, TREE_CHAIN (attr));
1524 }
1525 }
1526
1527 /* Duplicate all attributes from user DECL to the corresponding
1528 builtin that should be propagated. */
1529
1530 void
1531 copy_attributes_to_builtin (tree decl)
1532 {
1533 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1534 if (b)
1535 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1536 DECL_ATTRIBUTES (decl), "omp declare simd");
1537 }
1538
1539 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1540
1541 /* Specialization of merge_decl_attributes for various Windows targets.
1542
1543 This handles the following situation:
1544
1545 __declspec (dllimport) int foo;
1546 int foo;
1547
1548 The second instance of `foo' nullifies the dllimport. */
1549
1550 tree
1551 merge_dllimport_decl_attributes (tree old, tree new_tree)
1552 {
1553 tree a;
1554 int delete_dllimport_p = 1;
1555
1556 /* What we need to do here is remove from `old' dllimport if it doesn't
1557 appear in `new'. dllimport behaves like extern: if a declaration is
1558 marked dllimport and a definition appears later, then the object
1559 is not dllimport'd. We also remove a `new' dllimport if the old list
1560 contains dllexport: dllexport always overrides dllimport, regardless
1561 of the order of declaration. */
1562 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1563 delete_dllimport_p = 0;
1564 else if (DECL_DLLIMPORT_P (new_tree)
1565 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1566 {
1567 DECL_DLLIMPORT_P (new_tree) = 0;
1568 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1569 "attribute: dllimport ignored", new_tree);
1570 }
1571 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1572 {
1573 /* Warn about overriding a symbol that has already been used, e.g.:
1574 extern int __attribute__ ((dllimport)) foo;
1575 int* bar () {return &foo;}
1576 int foo;
1577 */
1578 if (TREE_USED (old))
1579 {
1580 warning (0, "%q+D redeclared without dllimport attribute "
1581 "after being referenced with dll linkage", new_tree);
1582 /* If we have used a variable's address with dllimport linkage,
1583 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1584 decl may already have had TREE_CONSTANT computed.
1585 We still remove the attribute so that assembler code refers
1586 to '&foo rather than '_imp__foo'. */
1587 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1588 DECL_DLLIMPORT_P (new_tree) = 1;
1589 }
1590
1591 /* Let an inline definition silently override the external reference,
1592 but otherwise warn about attribute inconsistency. */
1593 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1594 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1595 "attribute: previous dllimport ignored", new_tree);
1596 }
1597 else
1598 delete_dllimport_p = 0;
1599
1600 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1601
1602 if (delete_dllimport_p)
1603 a = remove_attribute ("dllimport", a);
1604
1605 return a;
1606 }
1607
1608 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1609 struct attribute_spec.handler. */
1610
1611 tree
1612 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1613 bool *no_add_attrs)
1614 {
1615 tree node = *pnode;
1616 bool is_dllimport;
1617
1618 /* These attributes may apply to structure and union types being created,
1619 but otherwise should pass to the declaration involved. */
1620 if (!DECL_P (node))
1621 {
1622 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1623 | (int) ATTR_FLAG_ARRAY_NEXT))
1624 {
1625 *no_add_attrs = true;
1626 return tree_cons (name, args, NULL_TREE);
1627 }
1628 if (TREE_CODE (node) == RECORD_TYPE
1629 || TREE_CODE (node) == UNION_TYPE)
1630 {
1631 node = TYPE_NAME (node);
1632 if (!node)
1633 return NULL_TREE;
1634 }
1635 else
1636 {
1637 warning (OPT_Wattributes, "%qE attribute ignored",
1638 name);
1639 *no_add_attrs = true;
1640 return NULL_TREE;
1641 }
1642 }
1643
1644 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1645 {
1646 *no_add_attrs = true;
1647 warning (OPT_Wattributes, "%qE attribute ignored",
1648 name);
1649 return NULL_TREE;
1650 }
1651
1652 if (TREE_CODE (node) == TYPE_DECL
1653 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1654 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1655 {
1656 *no_add_attrs = true;
1657 warning (OPT_Wattributes, "%qE attribute ignored",
1658 name);
1659 return NULL_TREE;
1660 }
1661
1662 is_dllimport = is_attribute_p ("dllimport", name);
1663
1664 /* Report error on dllimport ambiguities seen now before they cause
1665 any damage. */
1666 if (is_dllimport)
1667 {
1668 /* Honor any target-specific overrides. */
1669 if (!targetm.valid_dllimport_attribute_p (node))
1670 *no_add_attrs = true;
1671
1672 else if (TREE_CODE (node) == FUNCTION_DECL
1673 && DECL_DECLARED_INLINE_P (node))
1674 {
1675 warning (OPT_Wattributes, "inline function %q+D declared as "
1676 "dllimport: attribute ignored", node);
1677 *no_add_attrs = true;
1678 }
1679 /* Like MS, treat definition of dllimported variables and
1680 non-inlined functions on declaration as syntax errors. */
1681 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1682 {
1683 error ("function %q+D definition is marked dllimport", node);
1684 *no_add_attrs = true;
1685 }
1686
1687 else if (VAR_P (node))
1688 {
1689 if (DECL_INITIAL (node))
1690 {
1691 error ("variable %q+D definition is marked dllimport",
1692 node);
1693 *no_add_attrs = true;
1694 }
1695
1696 /* `extern' needn't be specified with dllimport.
1697 Specify `extern' now and hope for the best. Sigh. */
1698 DECL_EXTERNAL (node) = 1;
1699 /* Also, implicitly give dllimport'd variables declared within
1700 a function global scope, unless declared static. */
1701 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1702 TREE_PUBLIC (node) = 1;
1703 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1704 it is a C++ static data member. */
1705 if (DECL_CONTEXT (node) == NULL_TREE
1706 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node)))
1707 TREE_STATIC (node) = 0;
1708 }
1709
1710 if (*no_add_attrs == false)
1711 DECL_DLLIMPORT_P (node) = 1;
1712 }
1713 else if (TREE_CODE (node) == FUNCTION_DECL
1714 && DECL_DECLARED_INLINE_P (node)
1715 && flag_keep_inline_dllexport)
1716 /* An exported function, even if inline, must be emitted. */
1717 DECL_EXTERNAL (node) = 0;
1718
1719 /* Report error if symbol is not accessible at global scope. */
1720 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
1721 {
1722 error ("external linkage required for symbol %q+D because of "
1723 "%qE attribute", node, name);
1724 *no_add_attrs = true;
1725 }
1726
1727 /* A dllexport'd entity must have default visibility so that other
1728 program units (shared libraries or the main executable) can see
1729 it. A dllimport'd entity must have default visibility so that
1730 the linker knows that undefined references within this program
1731 unit can be resolved by the dynamic linker. */
1732 if (!*no_add_attrs)
1733 {
1734 if (DECL_VISIBILITY_SPECIFIED (node)
1735 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
1736 error ("%qE implies default visibility, but %qD has already "
1737 "been declared with a different visibility",
1738 name, node);
1739 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
1740 DECL_VISIBILITY_SPECIFIED (node) = 1;
1741 }
1742
1743 return NULL_TREE;
1744 }
1745
1746 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1747
1748 /* Given two lists of attributes, return true if list l2 is
1749 equivalent to l1. */
1750
1751 int
1752 attribute_list_equal (const_tree l1, const_tree l2)
1753 {
1754 if (l1 == l2)
1755 return 1;
1756
1757 return attribute_list_contained (l1, l2)
1758 && attribute_list_contained (l2, l1);
1759 }
1760
1761 /* Given two lists of attributes, return true if list L2 is
1762 completely contained within L1. */
1763 /* ??? This would be faster if attribute names were stored in a canonicalized
1764 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1765 must be used to show these elements are equivalent (which they are). */
1766 /* ??? It's not clear that attributes with arguments will always be handled
1767 correctly. */
1768
1769 int
1770 attribute_list_contained (const_tree l1, const_tree l2)
1771 {
1772 const_tree t1, t2;
1773
1774 /* First check the obvious, maybe the lists are identical. */
1775 if (l1 == l2)
1776 return 1;
1777
1778 /* Maybe the lists are similar. */
1779 for (t1 = l1, t2 = l2;
1780 t1 != 0 && t2 != 0
1781 && get_attribute_name (t1) == get_attribute_name (t2)
1782 && TREE_VALUE (t1) == TREE_VALUE (t2);
1783 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1784 ;
1785
1786 /* Maybe the lists are equal. */
1787 if (t1 == 0 && t2 == 0)
1788 return 1;
1789
1790 for (; t2 != 0; t2 = TREE_CHAIN (t2))
1791 {
1792 const_tree attr;
1793 /* This CONST_CAST is okay because lookup_attribute does not
1794 modify its argument and the return value is assigned to a
1795 const_tree. */
1796 for (attr = lookup_ident_attribute (get_attribute_name (t2),
1797 CONST_CAST_TREE (l1));
1798 attr != NULL_TREE && !attribute_value_equal (t2, attr);
1799 attr = lookup_ident_attribute (get_attribute_name (t2),
1800 TREE_CHAIN (attr)))
1801 ;
1802
1803 if (attr == NULL_TREE)
1804 return 0;
1805 }
1806
1807 return 1;
1808 }
1809
1810 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
1811 of ATTR_NAME, and LIST is not NULL_TREE.
1812
1813 The function is called from lookup_attribute in order to optimize
1814 for size. */
1815
1816 tree
1817 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
1818 {
1819 while (list)
1820 {
1821 tree attr = get_attribute_name (list);
1822 size_t ident_len = IDENTIFIER_LENGTH (attr);
1823 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
1824 ident_len))
1825 break;
1826 list = TREE_CHAIN (list);
1827 }
1828
1829 return list;
1830 }
1831
1832 /* Return true if the function decl or type NODE has been declared
1833 with attribute ANAME among attributes ATTRS. */
1834
1835 static bool
1836 has_attribute (tree node, tree attrs, const char *aname)
1837 {
1838 if (!strcmp (aname, "const"))
1839 {
1840 if (DECL_P (node) && TREE_READONLY (node))
1841 return true;
1842 }
1843 else if (!strcmp (aname, "malloc"))
1844 {
1845 if (DECL_P (node) && DECL_IS_MALLOC (node))
1846 return true;
1847 }
1848 else if (!strcmp (aname, "noreturn"))
1849 {
1850 if (DECL_P (node) && TREE_THIS_VOLATILE (node))
1851 return true;
1852 }
1853 else if (!strcmp (aname, "nothrow"))
1854 {
1855 if (TREE_NOTHROW (node))
1856 return true;
1857 }
1858 else if (!strcmp (aname, "pure"))
1859 {
1860 if (DECL_P (node) && DECL_PURE_P (node))
1861 return true;
1862 }
1863
1864 return lookup_attribute (aname, attrs);
1865 }
1866
1867 /* Return the number of mismatched function or type attributes between
1868 the "template" function declaration TMPL and DECL. The word "template"
1869 doesn't necessarily refer to a C++ template but rather a declaration
1870 whose attributes should be matched by those on DECL. For a non-zero
1871 return value set *ATTRSTR to a string representation of the list of
1872 mismatched attributes with quoted names.
1873 ATTRLIST is a list of additional attributes that SPEC should be
1874 taken to ultimately be declared with. */
1875
1876 unsigned
1877 decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
1878 const char* const blacklist[],
1879 pretty_printer *attrstr)
1880 {
1881 if (TREE_CODE (tmpl) != FUNCTION_DECL)
1882 return 0;
1883
1884 /* Avoid warning if either declaration or its type is deprecated. */
1885 if (TREE_DEPRECATED (tmpl)
1886 || TREE_DEPRECATED (decl))
1887 return 0;
1888
1889 const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
1890 const tree decls[] = { decl, TREE_TYPE (decl) };
1891
1892 if (TREE_DEPRECATED (tmpls[1])
1893 || TREE_DEPRECATED (decls[1])
1894 || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
1895 || TREE_DEPRECATED (TREE_TYPE (decls[1])))
1896 return 0;
1897
1898 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
1899 tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
1900
1901 if (!decl_attrs[0])
1902 decl_attrs[0] = attrlist;
1903 else if (!decl_attrs[1])
1904 decl_attrs[1] = attrlist;
1905
1906 /* Avoid warning if the template has no attributes. */
1907 if (!tmpl_attrs[0] && !tmpl_attrs[1])
1908 return 0;
1909
1910 /* Avoid warning if either declaration contains an attribute on
1911 the white list below. */
1912 const char* const whitelist[] = {
1913 "error", "warning"
1914 };
1915
1916 for (unsigned i = 0; i != 2; ++i)
1917 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
1918 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
1919 || lookup_attribute (whitelist[j], decl_attrs[i]))
1920 return 0;
1921
1922 /* Put together a list of the black-listed attributes that the template
1923 is declared with and the declaration is not, in case it's not apparent
1924 from the most recent declaration of the template. */
1925 unsigned nattrs = 0;
1926
1927 for (unsigned i = 0; blacklist[i]; ++i)
1928 {
1929 /* Attribute leaf only applies to extern functions. Avoid mentioning
1930 it when it's missing from a static declaration. */
1931 if (!TREE_PUBLIC (decl)
1932 && !strcmp ("leaf", blacklist[i]))
1933 continue;
1934
1935 for (unsigned j = 0; j != 2; ++j)
1936 {
1937 if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
1938 continue;
1939
1940 bool found = false;
1941 unsigned kmax = 1 + !!decl_attrs[1];
1942 for (unsigned k = 0; k != kmax; ++k)
1943 {
1944 if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
1945 {
1946 found = true;
1947 break;
1948 }
1949 }
1950
1951 if (!found)
1952 {
1953 if (nattrs)
1954 pp_string (attrstr, ", ");
1955 pp_begin_quote (attrstr, pp_show_color (global_dc->printer));
1956 pp_string (attrstr, blacklist[i]);
1957 pp_end_quote (attrstr, pp_show_color (global_dc->printer));
1958 ++nattrs;
1959 }
1960
1961 break;
1962 }
1963 }
1964
1965 return nattrs;
1966 }
1967
1968 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
1969 specifies either attributes that are incompatible with those of
1970 TARGET, or attributes that are missing and that declaring ALIAS
1971 with would benefit. */
1972
1973 void
1974 maybe_diag_alias_attributes (tree alias, tree target)
1975 {
1976 /* Do not expect attributes to match between aliases and ifunc
1977 resolvers. There is no obvious correspondence between them. */
1978 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
1979 return;
1980
1981 const char* const blacklist[] = {
1982 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
1983 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
1984 "returns_twice", NULL
1985 };
1986
1987 pretty_printer attrnames;
1988 if (warn_attribute_alias > 1)
1989 {
1990 /* With -Wattribute-alias=2 detect alias declarations that are more
1991 restrictive than their targets first. Those indicate potential
1992 codegen bugs. */
1993 if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
1994 blacklist, &attrnames))
1995 {
1996 auto_diagnostic_group d;
1997 if (warning_n (DECL_SOURCE_LOCATION (alias),
1998 OPT_Wattribute_alias_, n,
1999 "%qD specifies more restrictive attribute than "
2000 "its target %qD: %s",
2001 "%qD specifies more restrictive attributes than "
2002 "its target %qD: %s",
2003 alias, target, pp_formatted_text (&attrnames)))
2004 inform (DECL_SOURCE_LOCATION (target),
2005 "%qD target declared here", alias);
2006 return;
2007 }
2008 }
2009
2010 /* Detect alias declarations that are less restrictive than their
2011 targets. Those suggest potential optimization opportunities
2012 (solved by adding the missing attribute(s) to the alias). */
2013 if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
2014 blacklist, &attrnames))
2015 {
2016 auto_diagnostic_group d;
2017 if (warning_n (DECL_SOURCE_LOCATION (alias),
2018 OPT_Wmissing_attributes, n,
2019 "%qD specifies less restrictive attribute than "
2020 "its target %qD: %s",
2021 "%qD specifies less restrictive attributes than "
2022 "its target %qD: %s",
2023 alias, target, pp_formatted_text (&attrnames)))
2024 inform (DECL_SOURCE_LOCATION (target),
2025 "%qD target declared here", alias);
2026 }
2027 }
2028
2029 /* Initialize a mapping RWM for a call to a function declared with
2030 attribute access in ATTRS. Each attribute positional operand
2031 inserts one entry into the mapping with the operand number as
2032 the key. */
2033
2034 void
2035 init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
2036 {
2037 if (!attrs)
2038 return;
2039
2040 for (tree access = attrs;
2041 (access = lookup_attribute ("access", access));
2042 access = TREE_CHAIN (access))
2043 {
2044 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2045 is the attribute argument's value. */
2046 tree mode = TREE_VALUE (access);
2047 if (!mode)
2048 return;
2049
2050 /* The (optional) list of VLA bounds. */
2051 tree vblist = TREE_CHAIN (mode);
2052 if (vblist)
2053 vblist = TREE_VALUE (vblist);
2054
2055 mode = TREE_VALUE (mode);
2056 if (TREE_CODE (mode) != STRING_CST)
2057 continue;
2058 gcc_assert (TREE_CODE (mode) == STRING_CST);
2059
2060 for (const char *m = TREE_STRING_POINTER (mode); *m; )
2061 {
2062 attr_access acc = { };
2063
2064 /* Skip the internal-only plus sign. */
2065 if (*m == '+')
2066 ++m;
2067
2068 acc.str = m;
2069 acc.mode = acc.from_mode_char (*m);
2070 acc.sizarg = UINT_MAX;
2071
2072 const char *end;
2073 acc.ptrarg = strtoul (++m, const_cast<char**>(&end), 10);
2074 m = end;
2075
2076 if (*m == '[')
2077 {
2078 /* Forms containing the square bracket are internal-only
2079 (not specified by an attribute declaration), and used
2080 for various forms of array and VLA parameters. */
2081 acc.internal_p = true;
2082
2083 /* Search to the closing bracket and look at the preceding
2084 code: it determines the form of the most significant
2085 bound of the array. Others prior to it encode the form
2086 of interior VLA bounds. They're not of interest here. */
2087 end = strchr (m, ']');
2088 const char *p = end;
2089 gcc_assert (p);
2090
2091 while (ISDIGIT (p[-1]))
2092 --p;
2093
2094 if (ISDIGIT (*p))
2095 {
2096 /* A digit denotes a constant bound (as in T[3]). */
2097 acc.static_p = p[-1] == 's';
2098 acc.minsize = strtoull (p, NULL, 10);
2099 }
2100 else if (' ' == p[-1])
2101 {
2102 /* A space denotes an ordinary array of unspecified bound
2103 (as in T[]). */
2104 acc.minsize = 0;
2105 }
2106 else if ('*' == p[-1] || '$' == p[-1])
2107 {
2108 /* An asterisk denotes a VLA. When the closing bracket
2109 is followed by a comma and a dollar sign its bound is
2110 on the list. Otherwise it's a VLA with an unspecified
2111 bound. */
2112 acc.static_p = p[-2] == 's';
2113 acc.minsize = HOST_WIDE_INT_M1U;
2114 }
2115
2116 m = end + 1;
2117 }
2118
2119 if (*m == ',')
2120 {
2121 ++m;
2122 do
2123 {
2124 if (*m == '$')
2125 {
2126 ++m;
2127 if (!acc.size)
2128 {
2129 /* Extract the list of VLA bounds for the current
2130 parameter, store it in ACC.SIZE, and advance
2131 to the list of bounds for the next VLA parameter.
2132 */
2133 acc.size = TREE_VALUE (vblist);
2134 vblist = TREE_CHAIN (vblist);
2135 }
2136 }
2137
2138 if (ISDIGIT (*m))
2139 {
2140 /* Extract the positional argument. It's absent
2141 for VLAs whose bound doesn't name a function
2142 parameter. */
2143 unsigned pos = strtoul (m, const_cast<char**>(&end), 10);
2144 if (acc.sizarg == UINT_MAX)
2145 acc.sizarg = pos;
2146 m = end;
2147 }
2148 }
2149 while (*m == '$');
2150 }
2151
2152 acc.end = m;
2153
2154 bool existing;
2155 auto &ref = rwm->get_or_insert (acc.ptrarg, &existing);
2156 if (existing)
2157 {
2158 /* Merge the new spec with the existing. */
2159 if (acc.minsize == HOST_WIDE_INT_M1U)
2160 ref.minsize = HOST_WIDE_INT_M1U;
2161
2162 if (acc.sizarg != UINT_MAX)
2163 ref.sizarg = acc.sizarg;
2164
2165 if (acc.mode)
2166 ref.mode = acc.mode;
2167 }
2168 else
2169 ref = acc;
2170
2171 /* Unconditionally add an entry for the required pointer
2172 operand of the attribute, and one for the optional size
2173 operand when it's specified. */
2174 if (acc.sizarg != UINT_MAX)
2175 rwm->put (acc.sizarg, acc);
2176 }
2177 }
2178 }
2179
2180 /* Return the access specification for a function parameter PARM
2181 or null if the current function has no such specification. */
2182
2183 attr_access *
2184 get_parm_access (rdwr_map &rdwr_idx, tree parm,
2185 tree fndecl /* = current_function_decl */)
2186 {
2187 tree fntype = TREE_TYPE (fndecl);
2188 init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
2189
2190 if (rdwr_idx.is_empty ())
2191 return NULL;
2192
2193 unsigned argpos = 0;
2194 tree fnargs = DECL_ARGUMENTS (fndecl);
2195 for (tree arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
2196 if (arg == parm)
2197 return rdwr_idx.get (argpos);
2198
2199 return NULL;
2200 }
2201
2202 /* Return the internal representation as STRING_CST. Internal positional
2203 arguments are zero-based. */
2204
2205 tree
2206 attr_access::to_internal_string () const
2207 {
2208 return build_string (end - str, str);
2209 }
2210
2211 /* Return the human-readable representation of the external attribute
2212 specification (as it might appear in the source code) as STRING_CST.
2213 External positional arguments are one-based. */
2214
2215 tree
2216 attr_access::to_external_string () const
2217 {
2218 char buf[80];
2219 gcc_assert (mode != access_deferred);
2220 int len = snprintf (buf, sizeof buf, "access (%s, %u",
2221 mode_names[mode], ptrarg + 1);
2222 if (sizarg != UINT_MAX)
2223 len += snprintf (buf + len, sizeof buf - len, ", %u", sizarg + 1);
2224 strcpy (buf + len, ")");
2225 return build_string (len + 2, buf);
2226 }
2227
2228 /* Return the number of specified VLA bounds and set *nunspec to
2229 the number of unspecified ones (those designated by [*]). */
2230
2231 unsigned
2232 attr_access::vla_bounds (unsigned *nunspec) const
2233 {
2234 *nunspec = 0;
2235 for (const char* p = strrchr (str, ']'); p && *p != '['; --p)
2236 if (*p == '*')
2237 ++*nunspec;
2238 return list_length (size);
2239 }
2240
2241
2242 /* Defined in attr_access. */
2243 constexpr char attr_access::mode_chars[];
2244 constexpr char attr_access::mode_names[][11];
2245
2246 /* Format an array, including a VLA, pointed to by TYPE and used as
2247 a function parameter as a human-readable string. ACC describes
2248 an access to the parameter and is used to determine the outermost
2249 form of the array including its bound which is otherwise obviated
2250 by its decay to pointer. Return the formatted string. */
2251
2252 std::string
2253 attr_access::array_as_string (tree type) const
2254 {
2255 std::string typstr;
2256
2257 if (type == error_mark_node)
2258 return std::string ();
2259
2260 if (this->str)
2261 {
2262 /* For array parameters (but not pointers) create a temporary array
2263 type that corresponds to the form of the parameter including its
2264 qualifiers even though they apply to the pointer, not the array
2265 type. */
2266 const bool vla_p = minsize == HOST_WIDE_INT_M1U;
2267 tree eltype = TREE_TYPE (type);
2268 tree index_type = NULL_TREE;
2269
2270 if (minsize == HOST_WIDE_INT_M1U)
2271 {
2272 /* Determine if this is a VLA (an array whose most significant
2273 bound is nonconstant and whose access string has "$]" in it)
2274 extract the bound expression from SIZE. */
2275 const char *p = end;
2276 for ( ; p != str && *p-- != ']'; );
2277 if (*p == '$')
2278 index_type = build_index_type (TREE_VALUE (size));
2279 }
2280 else if (minsize)
2281 index_type = build_index_type (size_int (minsize - 1));
2282
2283 tree arat = NULL_TREE;
2284 if (static_p || vla_p)
2285 {
2286 tree flag = static_p ? integer_one_node : NULL_TREE;
2287 /* Hack: there's no language-independent way to encode
2288 the "static" specifier or the "*" notation in an array type.
2289 Add a "fake" attribute to have the pretty-printer add "static"
2290 or "*". The "[static N]" notation is only valid in the most
2291 significant bound but [*] can be used for any bound. Because
2292 [*] is represented the same as [0] this hack only works for
2293 the most significant bound like static and the others are
2294 rendered as [0]. */
2295 arat = build_tree_list (get_identifier ("array"), flag);
2296 }
2297
2298 const int quals = TYPE_QUALS (type);
2299 type = build_array_type (eltype, index_type);
2300 type = build_type_attribute_qual_variant (type, arat, quals);
2301 }
2302
2303 /* Format the type using the current pretty printer. The generic tree
2304 printer does a terrible job. */
2305 pretty_printer *pp = global_dc->printer->clone ();
2306 pp_printf (pp, "%qT", type);
2307 typstr = pp_formatted_text (pp);
2308 delete pp;
2309
2310 return typstr;
2311 }
2312
2313 #if CHECKING_P
2314
2315 namespace selftest
2316 {
2317
2318 /* Helper types to verify the consistency attribute exclusions. */
2319
2320 typedef std::pair<const char *, const char *> excl_pair;
2321
2322 struct excl_hash_traits: typed_noop_remove<excl_pair>
2323 {
2324 typedef excl_pair value_type;
2325 typedef value_type compare_type;
2326
2327 static hashval_t hash (const value_type &x)
2328 {
2329 hashval_t h1 = htab_hash_string (x.first);
2330 hashval_t h2 = htab_hash_string (x.second);
2331 return h1 ^ h2;
2332 }
2333
2334 static bool equal (const value_type &x, const value_type &y)
2335 {
2336 return !strcmp (x.first, y.first) && !strcmp (x.second, y.second);
2337 }
2338
2339 static void mark_deleted (value_type &x)
2340 {
2341 x = value_type (NULL, NULL);
2342 }
2343
2344 static const bool empty_zero_p = false;
2345
2346 static void mark_empty (value_type &x)
2347 {
2348 x = value_type ("", "");
2349 }
2350
2351 static bool is_deleted (const value_type &x)
2352 {
2353 return !x.first && !x.second;
2354 }
2355
2356 static bool is_empty (const value_type &x)
2357 {
2358 return !*x.first && !*x.second;
2359 }
2360 };
2361
2362
2363 /* Self-test to verify that each attribute exclusion is symmetric,
2364 meaning that if attribute A is encoded as incompatible with
2365 attribute B then the opposite relationship is also encoded.
2366 This test also detects most cases of misspelled attribute names
2367 in exclusions. */
2368
2369 static void
2370 test_attribute_exclusions ()
2371 {
2372 /* Iterate over the array of attribute tables first (with TI0 as
2373 the index) and over the array of attribute_spec in each table
2374 (with SI0 as the index). */
2375 const size_t ntables = ARRAY_SIZE (attribute_tables);
2376
2377 /* Set of pairs of mutually exclusive attributes. */
2378 typedef hash_set<excl_pair, false, excl_hash_traits> exclusion_set;
2379 exclusion_set excl_set;
2380
2381 for (size_t ti0 = 0; ti0 != ntables; ++ti0)
2382 for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0)
2383 {
2384 const attribute_spec::exclusions *excl
2385 = attribute_tables[ti0][s0].exclude;
2386
2387 /* Skip each attribute that doesn't define exclusions. */
2388 if (!excl)
2389 continue;
2390
2391 const char *attr_name = attribute_tables[ti0][s0].name;
2392
2393 /* Iterate over the set of exclusions for every attribute
2394 (with EI0 as the index) adding the exclusions defined
2395 for each to the set. */
2396 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2397 {
2398 const char *excl_name = excl[ei0].name;
2399
2400 if (!strcmp (attr_name, excl_name))
2401 continue;
2402
2403 excl_set.add (excl_pair (attr_name, excl_name));
2404 }
2405 }
2406
2407 /* Traverse the set of mutually exclusive pairs of attributes
2408 and verify that they are symmetric. */
2409 for (exclusion_set::iterator it = excl_set.begin ();
2410 it != excl_set.end ();
2411 ++it)
2412 {
2413 if (!excl_set.contains (excl_pair ((*it).second, (*it).first)))
2414 {
2415 /* An exclusion for an attribute has been found that
2416 doesn't have a corresponding exclusion in the opposite
2417 direction. */
2418 char desc[120];
2419 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2420 (*it).first, (*it).second);
2421 fail (SELFTEST_LOCATION, desc);
2422 }
2423 }
2424 }
2425
2426 void
2427 attribute_c_tests ()
2428 {
2429 test_attribute_exclusions ();
2430 }
2431
2432 } /* namespace selftest */
2433
2434 #endif /* CHECKING_P */