1/* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "target.h"
24#include "tree.h"
25#include "stringpool.h"
26#include "diagnostic-core.h"
27#include "attribs.h"
28#include "stor-layout.h"
29#include "langhooks.h"
30#include "plugin.h"
31#include "selftest.h"
32#include "hash-set.h"
33
34/* Table of the tables of attributes (common, language, format, machine)
35 searched. */
36static const struct attribute_spec *attribute_tables[4];
37
38/* Substring representation. */
39
40struct substring
41{
42 const char *str;
43 int length;
44};
45
46/* Simple hash function to avoid need to scan whole string. */
47
48static inline hashval_t
49substring_hash (const char *str, int l)
50{
51 return str[0] + str[l - 1] * 256 + l * 65536;
52}
53
54/* Used for attribute_hash. */
55
56struct attribute_hasher : nofree_ptr_hash <attribute_spec>
57{
58 typedef substring *compare_type;
59 static inline hashval_t hash (const attribute_spec *);
60 static inline bool equal (const attribute_spec *, const substring *);
61};
62
63inline hashval_t
64attribute_hasher::hash (const attribute_spec *spec)
65{
66 const int l = strlen (spec->name);
67 return substring_hash (spec->name, l);
68}
69
70inline bool
71attribute_hasher::equal (const attribute_spec *spec, const substring *str)
72{
73 return (strncmp (spec->name, str->str, str->length) == 0
74 && !spec->name[str->length]);
75}
76
77/* Scoped attribute name representation. */
78
79struct scoped_attributes
80{
81 const char *ns;
82 vec<attribute_spec> attributes;
83 hash_table<attribute_hasher> *attribute_hash;
84};
85
86/* The table of scope attributes. */
87static vec<scoped_attributes> attributes_table;
88
89static scoped_attributes* find_attribute_namespace (const char*);
90static void register_scoped_attribute (const struct attribute_spec *,
91 scoped_attributes *);
92
93static bool attributes_initialized = false;
94
95/* Default empty table of attributes. */
96
97static const struct attribute_spec empty_attribute_table[] =
98{
99 { NULL, 0, 0, false, false, false, NULL, false, NULL }
100};
101
102/* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
103 To avoid need for copying, we simply return length of the string. */
104
105static void
106extract_attribute_substring (struct substring *str)
107{
108 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
109 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
110 {
111 str->length -= 4;
112 str->str += 2;
113 }
114}
115
116/* Insert an array of attributes ATTRIBUTES into a namespace. This
117 array must be NULL terminated. NS is the name of attribute
118 namespace. The function returns the namespace into which the
119 attributes have been registered. */
120
121scoped_attributes*
122register_scoped_attributes (const struct attribute_spec * attributes,
123 const char* ns)
124{
125 scoped_attributes *result = NULL;
126
127 /* See if we already have attributes in the namespace NS. */
128 result = find_attribute_namespace (ns);
129
130 if (result == NULL)
131 {
132 /* We don't have any namespace NS yet. Create one. */
133 scoped_attributes sa;
134
135 if (attributes_table.is_empty ())
136 attributes_table.create (64);
137
138 memset (&sa, 0, sizeof (sa));
139 sa.ns = ns;
140 sa.attributes.create (64);
141 result = attributes_table.safe_push (sa);
142 result->attribute_hash = new hash_table<attribute_hasher> (200);
143 }
144
145 /* Really add the attributes to their namespace now. */
146 for (unsigned i = 0; attributes[i].name != NULL; ++i)
147 {
148 result->attributes.safe_push (attributes[i]);
149 register_scoped_attribute (&attributes[i], result);
150 }
151
152 gcc_assert (result != NULL);
153
154 return result;
155}
156
157/* Return the namespace which name is NS, NULL if none exist. */
158
159static scoped_attributes*
160find_attribute_namespace (const char* ns)
161{
162 unsigned ix;
163 scoped_attributes *iter;
164
165 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
166 if (ns == iter->ns
167 || (iter->ns != NULL
168 && ns != NULL
169 && !strcmp (iter->ns, ns)))
170 return iter;
171 return NULL;
172}
173
174/* Make some sanity checks on the attribute tables. */
175
176static void
177check_attribute_tables (void)
178{
179 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
180 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
181 {
182 /* The name must not begin and end with __. */
183 const char *name = attribute_tables[i][j].name;
184 int len = strlen (name);
185
186 gcc_assert (!(name[0] == '_' && name[1] == '_'
187 && name[len - 1] == '_' && name[len - 2] == '_'));
188
189 /* The minimum and maximum lengths must be consistent. */
190 gcc_assert (attribute_tables[i][j].min_length >= 0);
191
192 gcc_assert (attribute_tables[i][j].max_length == -1
193 || (attribute_tables[i][j].max_length
194 >= attribute_tables[i][j].min_length));
195
196 /* An attribute cannot require both a DECL and a TYPE. */
197 gcc_assert (!attribute_tables[i][j].decl_required
198 || !attribute_tables[i][j].type_required);
199
200 /* If an attribute requires a function type, in particular
201 it requires a type. */
202 gcc_assert (!attribute_tables[i][j].function_type_required
203 || attribute_tables[i][j].type_required);
204 }
205
206 /* Check that each name occurs just once in each table. */
207 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
208 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
209 for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++)
210 gcc_assert (strcmp (attribute_tables[i][j].name,
211 attribute_tables[i][k].name));
212
213 /* Check that no name occurs in more than one table. Names that
214 begin with '*' are exempt, and may be overridden. */
215 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
216 for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
217 for (size_t k = 0; attribute_tables[i][k].name != NULL; k++)
218 for (size_t l = 0; attribute_tables[j][l].name != NULL; l++)
219 gcc_assert (attribute_tables[i][k].name[0] == '*'
220 || strcmp (attribute_tables[i][k].name,
221 attribute_tables[j][l].name));
222}
223
224/* Initialize attribute tables, and make some sanity checks if checking is
225 enabled. */
226
227void
228init_attributes (void)
229{
230 size_t i;
231
232 if (attributes_initialized)
233 return;
234
235 attribute_tables[0] = lang_hooks.common_attribute_table;
236 attribute_tables[1] = lang_hooks.attribute_table;
237 attribute_tables[2] = lang_hooks.format_attribute_table;
238 attribute_tables[3] = targetm.attribute_table;
239
240 /* Translate NULL pointers to pointers to the empty table. */
241 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
242 if (attribute_tables[i] == NULL)
243 attribute_tables[i] = empty_attribute_table;
244
245 if (flag_checking)
246 check_attribute_tables ();
247
248 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
249 /* Put all the GNU attributes into the "gnu" namespace. */
250 register_scoped_attributes (attribute_tables[i], "gnu");
251
252 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
253 attributes_initialized = true;
254}
255
256/* Insert a single ATTR into the attribute table. */
257
258void
259register_attribute (const struct attribute_spec *attr)
260{
261 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
262}
263
264/* Insert a single attribute ATTR into a namespace of attributes. */
265
266static void
267register_scoped_attribute (const struct attribute_spec *attr,
268 scoped_attributes *name_space)
269{
270 struct substring str;
271 attribute_spec **slot;
272
273 gcc_assert (attr != NULL && name_space != NULL);
274
275 gcc_assert (name_space->attribute_hash);
276
277 str.str = attr->name;
278 str.length = strlen (str.str);
279
280 /* Attribute names in the table must be in the form 'text' and not
281 in the form '__text__'. */
282 gcc_assert (str.length > 0 && str.str[0] != '_');
283
284 slot = name_space->attribute_hash
285 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
286 INSERT);
287 gcc_assert (!*slot || attr->name[0] == '*');
288 *slot = CONST_CAST (struct attribute_spec *, attr);
289}
290
291/* Return the spec for the scoped attribute with namespace NS and
292 name NAME. */
293
294static const struct attribute_spec *
295lookup_scoped_attribute_spec (const_tree ns, const_tree name)
296{
297 struct substring attr;
298 scoped_attributes *attrs;
299
300 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
301
302 attrs = find_attribute_namespace (ns_str);
303
304 if (attrs == NULL)
305 return NULL;
306
307 attr.str = IDENTIFIER_POINTER (name);
308 attr.length = IDENTIFIER_LENGTH (name);
309 extract_attribute_substring (&attr);
310 return attrs->attribute_hash->find_with_hash (&attr,
311 substring_hash (attr.str,
312 attr.length));
313}
314
315/* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
316 it also specifies the attribute namespace. */
317
318const struct attribute_spec *
319lookup_attribute_spec (const_tree name)
320{
321 tree ns;
322 if (TREE_CODE (name) == TREE_LIST)
323 {
324 ns = TREE_PURPOSE (name);
325 name = TREE_VALUE (name);
326 }
327 else
328 ns = get_identifier ("gnu");
329 return lookup_scoped_attribute_spec (ns, name);
330}
331
332
333/* Return the namespace of the attribute ATTR. This accessor works on
334 GNU and C++11 (scoped) attributes. On GNU attributes,
335 it returns an identifier tree for the string "gnu".
336
337 Please read the comments of cxx11_attribute_p to understand the
338 format of attributes. */
339
340static tree
341get_attribute_namespace (const_tree attr)
342{
343 if (cxx11_attribute_p (attr))
344 return TREE_PURPOSE (TREE_PURPOSE (attr));
345 return get_identifier ("gnu");
346}
347
348/* Check LAST_DECL and NODE of the same symbol for attributes that are
349 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
350 them, and return true if any have been found. NODE can be a DECL
351 or a TYPE. */
352
353static bool
354diag_attr_exclusions (tree last_decl, tree node, tree attrname,
355 const attribute_spec *spec)
356{
357 const attribute_spec::exclusions *excl = spec->exclude;
358
359 tree_code code = TREE_CODE (node);
360
361 if ((code == FUNCTION_DECL && !excl->function
362 && (!excl->type || !spec->affects_type_identity))
363 || (code == VAR_DECL && !excl->variable
364 && (!excl->type || !spec->affects_type_identity))
365 || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
366 return false;
367
368 /* True if an attribute that's mutually exclusive with ATTRNAME
369 has been found. */
370 bool found = false;
371
372 if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
373 {
374 /* Check both the last DECL and its type for conflicts with
375 the attribute being added to the current decl or type. */
376 found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
377 tree decl_type = TREE_TYPE (last_decl);
378 found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
379 }
380
381 /* NODE is either the current DECL to which the attribute is being
382 applied or its TYPE. For the former, consider the attributes on
383 both the DECL and its type. */
384 tree attrs[2];
385
386 if (DECL_P (node))
387 {
388 attrs[0] = DECL_ATTRIBUTES (node);
389 attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
390 }
391 else
392 {
393 attrs[0] = TYPE_ATTRIBUTES (node);
394 attrs[1] = NULL_TREE;
395 }
396
397 /* Iterate over the mutually exclusive attribute names and verify
398 that the symbol doesn't contain it. */
399 for (unsigned i = 0; i != sizeof attrs / sizeof *attrs; ++i)
400 {
401 if (!attrs[i])
402 continue;
403
404 for ( ; excl->name; ++excl)
405 {
406 /* Avoid checking the attribute against itself. */
407 if (is_attribute_p (excl->name, attrname))
408 continue;
409
410 if (!lookup_attribute (excl->name, attrs[i]))
411 continue;
412
413 found = true;
414
415 /* Print a note? */
416 bool note = last_decl != NULL_TREE;
417
418 if (TREE_CODE (node) == FUNCTION_DECL
419 && DECL_BUILT_IN (node))
420 note &= warning (OPT_Wattributes,
421 "ignoring attribute %qE in declaration of "
422 "a built-in function %qD because it conflicts "
423 "with attribute %qs",
424 attrname, node, excl->name);
425 else
426 note &= warning (OPT_Wattributes,
427 "ignoring attribute %qE because "
428 "it conflicts with attribute %qs",
429 attrname, excl->name);
430
431 if (note)
432 inform (DECL_SOURCE_LOCATION (last_decl),
433 "previous declaration here");
434 }
435 }
436
437 return found;
438}
439
440/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
441 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
442 it should be modified in place; if a TYPE, a copy should be created
443 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
444 information, in the form of a bitwise OR of flags in enum attribute_flags
445 from tree.h. Depending on these flags, some attributes may be
446 returned to be applied at a later stage (for example, to apply
447 a decl attribute to the declaration rather than to its type). */
448
449tree
450decl_attributes (tree *node, tree attributes, int flags,
451 tree last_decl /* = NULL_TREE */)
452{
453 tree a;
454 tree returned_attrs = NULL_TREE;
455
456 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
457 return NULL_TREE;
458
459 if (!attributes_initialized)
460 init_attributes ();
461
462 /* If this is a function and the user used #pragma GCC optimize, add the
463 options to the attribute((optimize(...))) list. */
464 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
465 {
466 tree cur_attr = lookup_attribute ("optimize", attributes);
467 tree opts = copy_list (current_optimize_pragma);
468
469 if (! cur_attr)
470 attributes
471 = tree_cons (get_identifier ("optimize"), opts, attributes);
472 else
473 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
474 }
475
476 if (TREE_CODE (*node) == FUNCTION_DECL
477 && optimization_current_node != optimization_default_node
478 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
479 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
480
481 /* If this is a function and the user used #pragma GCC target, add the
482 options to the attribute((target(...))) list. */
483 if (TREE_CODE (*node) == FUNCTION_DECL
484 && current_target_pragma
485 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
486 current_target_pragma, 0))
487 {
488 tree cur_attr = lookup_attribute ("target", attributes);
489 tree opts = copy_list (current_target_pragma);
490
491 if (! cur_attr)
492 attributes = tree_cons (get_identifier ("target"), opts, attributes);
493 else
494 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
495 }
496
497 /* A "naked" function attribute implies "noinline" and "noclone" for
498 those targets that support it. */
499 if (TREE_CODE (*node) == FUNCTION_DECL
500 && attributes
501 && lookup_attribute ("naked", attributes) != NULL
502 && lookup_attribute_spec (get_identifier ("naked")))
503 {
504 if (lookup_attribute ("noinline", attributes) == NULL)
505 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
506
507 if (lookup_attribute ("noclone", attributes) == NULL)
508 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
509 }
510
511 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
512 for those targets that support it. */
513 if (TREE_CODE (*node) == FUNCTION_DECL
514 && attributes
515 && lookup_attribute ("noipa", attributes) != NULL
516 && lookup_attribute_spec (get_identifier ("noipa")))
517 {
518 if (lookup_attribute ("noinline", attributes) == NULL)
519 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
520
521 if (lookup_attribute ("noclone", attributes) == NULL)
522 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
523
524 if (lookup_attribute ("no_icf", attributes) == NULL)
525 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
526 }
527
528 targetm.insert_attributes (*node, &attributes);
529
530 /* Note that attributes on the same declaration are not necessarily
531 in the same order as in the source. */
532 for (a = attributes; a; a = TREE_CHAIN (a))
533 {
534 tree ns = get_attribute_namespace (a);
535 tree name = get_attribute_name (a);
536 tree args = TREE_VALUE (a);
537 tree *anode = node;
538 const struct attribute_spec *spec =
539 lookup_scoped_attribute_spec (ns, name);
540 int fn_ptr_quals = 0;
541 tree fn_ptr_tmp = NULL_TREE;
542
543 if (spec == NULL)
544 {
545 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
546 {
547 if (ns == NULL_TREE || !cxx11_attribute_p (a))
548 warning (OPT_Wattributes, "%qE attribute directive ignored",
549 name);
550 else
551 warning (OPT_Wattributes,
552 "%<%E::%E%> scoped attribute directive ignored",
553 ns, name);
554 }
555 continue;
556 }
557 else if (list_length (args) < spec->min_length
558 || (spec->max_length >= 0
559 && list_length (args) > spec->max_length))
560 {
561 error ("wrong number of arguments specified for %qE attribute",
562 name);
563 continue;
564 }
565 gcc_assert (is_attribute_p (spec->name, name));
566
567 if (TYPE_P (*node)
568 && cxx11_attribute_p (a)
569 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
570 {
571 /* This is a c++11 attribute that appertains to a
572 type-specifier, outside of the definition of, a class
573 type. Ignore it. */
574 if (warning (OPT_Wattributes, "attribute ignored"))
575 inform (input_location,
576 "an attribute that appertains to a type-specifier "
577 "is ignored");
578 continue;
579 }
580
581 if (spec->decl_required && !DECL_P (*anode))
582 {
583 if (flags & ((int) ATTR_FLAG_DECL_NEXT
584 | (int) ATTR_FLAG_FUNCTION_NEXT
585 | (int) ATTR_FLAG_ARRAY_NEXT))
586 {
587 /* Pass on this attribute to be tried again. */
588 tree attr = tree_cons (name, args, NULL_TREE);
589 returned_attrs = chainon (returned_attrs, attr);
590 continue;
591 }
592 else
593 {
594 warning (OPT_Wattributes, "%qE attribute does not apply to types",
595 name);
596 continue;
597 }
598 }
599
600 /* If we require a type, but were passed a decl, set up to make a
601 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
602 would have applied if we'd been passed a type, but we cannot modify
603 the decl's type in place here. */
604 if (spec->type_required && DECL_P (*anode))
605 {
606 anode = &TREE_TYPE (*anode);
607 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
608 }
609
610 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
611 && TREE_CODE (*anode) != METHOD_TYPE)
612 {
613 if (TREE_CODE (*anode) == POINTER_TYPE
614 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
615 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
616 {
617 /* OK, this is a bit convoluted. We can't just make a copy
618 of the pointer type and modify its TREE_TYPE, because if
619 we change the attributes of the target type the pointer
620 type needs to have a different TYPE_MAIN_VARIANT. So we
621 pull out the target type now, frob it as appropriate, and
622 rebuild the pointer type later.
623
624 This would all be simpler if attributes were part of the
625 declarator, grumble grumble. */
626 fn_ptr_tmp = TREE_TYPE (*anode);
627 fn_ptr_quals = TYPE_QUALS (*anode);
628 anode = &fn_ptr_tmp;
629 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
630 }
631 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
632 {
633 /* Pass on this attribute to be tried again. */
634 tree attr = tree_cons (name, args, NULL_TREE);
635 returned_attrs = chainon (returned_attrs, attr);
636 continue;
637 }
638
639 if (TREE_CODE (*anode) != FUNCTION_TYPE
640 && TREE_CODE (*anode) != METHOD_TYPE)
641 {
642 warning (OPT_Wattributes,
643 "%qE attribute only applies to function types",
644 name);
645 continue;
646 }
647 }
648
649 if (TYPE_P (*anode)
650 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
651 && TYPE_SIZE (*anode) != NULL_TREE)
652 {
653 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
654 continue;
655 }
656
657 bool no_add_attrs = false;
658
659 if (spec->handler != NULL)
660 {
661 int cxx11_flag =
662 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
663
664 /* Pass in an array of the current declaration followed
665 by the last pushed/merged declaration if one exists.
666 If the handler changes CUR_AND_LAST_DECL[0] replace
667 *ANODE with its value. */
668 tree cur_and_last_decl[] = { *anode, last_decl };
669 tree ret = (spec->handler) (cur_and_last_decl, name, args,
670 flags|cxx11_flag, &no_add_attrs);
671
672 *anode = cur_and_last_decl[0];
673 if (ret == error_mark_node)
674 {
675 warning (OPT_Wattributes, "%qE attribute ignored", name);
676 no_add_attrs = true;
677 }
678 else
679 returned_attrs = chainon (ret, returned_attrs);
680 }
681
682 /* If the attribute was successfully handled on its own and is
683 about to be added check for exclusions with other attributes
684 on the current declation as well as the last declaration of
685 the same symbol already processed (if one exists). */
686 bool built_in = flags & ATTR_FLAG_BUILT_IN;
687 if (spec->exclude
688 && !no_add_attrs
689 && (flag_checking || !built_in))
690 {
691 /* Always check attributes on user-defined functions.
692 Check them on built-ins only when -fchecking is set.
693 Ignore __builtin_unreachable -- it's both const and
694 noreturn. */
695
696 if (!built_in
697 || !DECL_P (*anode)
698 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
699 && (DECL_FUNCTION_CODE (*anode)
700 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
701 {
702 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
703 if (!no_add && anode != node)
704 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
705 no_add_attrs |= no_add;
706 }
707 }
708
709 /* Layout the decl in case anything changed. */
710 if (spec->type_required && DECL_P (*node)
711 && (VAR_P (*node)
712 || TREE_CODE (*node) == PARM_DECL
713 || TREE_CODE (*node) == RESULT_DECL))
714 relayout_decl (*node);
715
716 if (!no_add_attrs)
717 {
718 tree old_attrs;
719 tree a;
720
721 if (DECL_P (*anode))
722 old_attrs = DECL_ATTRIBUTES (*anode);
723 else
724 old_attrs = TYPE_ATTRIBUTES (*anode);
725
726 for (a = lookup_attribute (spec->name, old_attrs);
727 a != NULL_TREE;
728 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
729 {
730 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
731 break;
732 }
733
734 if (a == NULL_TREE)
735 {
736 /* This attribute isn't already in the list. */
737 if (DECL_P (*anode))
738 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
739 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
740 {
741 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
742 /* If this is the main variant, also push the attributes
743 out to the other variants. */
744 if (*anode == TYPE_MAIN_VARIANT (*anode))
745 {
746 tree variant;
747 for (variant = *anode; variant;
748 variant = TYPE_NEXT_VARIANT (variant))
749 {
750 if (TYPE_ATTRIBUTES (variant) == old_attrs)
751 TYPE_ATTRIBUTES (variant)
752 = TYPE_ATTRIBUTES (*anode);
753 else if (!lookup_attribute
754 (spec->name, TYPE_ATTRIBUTES (variant)))
755 TYPE_ATTRIBUTES (variant) = tree_cons
756 (name, args, TYPE_ATTRIBUTES (variant));
757 }
758 }
759 }
760 else
761 *anode = build_type_attribute_variant (*anode,
762 tree_cons (name, args,
763 old_attrs));
764 }
765 }
766
767 if (fn_ptr_tmp)
768 {
769 /* Rebuild the function pointer type and put it in the
770 appropriate place. */
771 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
772 if (fn_ptr_quals)
773 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
774 if (DECL_P (*node))
775 TREE_TYPE (*node) = fn_ptr_tmp;
776 else
777 {
778 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
779 *node = fn_ptr_tmp;
780 }
781 }
782 }
783
784 return returned_attrs;
785}
786
787/* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
788 attribute.
789
790 When G++ parses a C++11 attribute, it is represented as
791 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
792 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
793 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
794 use get_attribute_namespace and get_attribute_name to retrieve the
795 namespace and name of the attribute, as these accessors work with
796 GNU attributes as well. */
797
798bool
799cxx11_attribute_p (const_tree attr)
800{
801 if (attr == NULL_TREE
802 || TREE_CODE (attr) != TREE_LIST)
803 return false;
804
805 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
806}
807
808/* Return the name of the attribute ATTR. This accessor works on GNU
809 and C++11 (scoped) attributes.
810
811 Please read the comments of cxx11_attribute_p to understand the
812 format of attributes. */
813
814tree
815get_attribute_name (const_tree attr)
816{
817 if (cxx11_attribute_p (attr))
818 return TREE_VALUE (TREE_PURPOSE (attr));
819 return TREE_PURPOSE (attr);
820}
821
822/* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
823 to the method FNDECL. */
824
825void
826apply_tm_attr (tree fndecl, tree attr)
827{
828 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
829}
830
831/* Makes a function attribute of the form NAME(ARG_NAME) and chains
832 it to CHAIN. */
833
834tree
835make_attribute (const char *name, const char *arg_name, tree chain)
836{
837 tree attr_name;
838 tree attr_arg_name;
839 tree attr_args;
840 tree attr;
841
842 attr_name = get_identifier (name);
843 attr_arg_name = build_string (strlen (arg_name), arg_name);
844 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
845 attr = tree_cons (attr_name, attr_args, chain);
846 return attr;
847}
848
849
850/* Common functions used for target clone support. */
851
852/* Comparator function to be used in qsort routine to sort attribute
853 specification strings to "target". */
854
855static int
856attr_strcmp (const void *v1, const void *v2)
857{
858 const char *c1 = *(char *const*)v1;
859 const char *c2 = *(char *const*)v2;
860 return strcmp (c1, c2);
861}
862
863/* ARGLIST is the argument to target attribute. This function tokenizes
864 the comma separated arguments, sorts them and returns a string which
865 is a unique identifier for the comma separated arguments. It also
866 replaces non-identifier characters "=,-" with "_". */
867
868char *
869sorted_attr_string (tree arglist)
870{
871 tree arg;
872 size_t str_len_sum = 0;
873 char **args = NULL;
874 char *attr_str, *ret_str;
875 char *attr = NULL;
876 unsigned int argnum = 1;
877 unsigned int i;
878
879 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
880 {
881 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
882 size_t len = strlen (str);
883 str_len_sum += len + 1;
884 if (arg != arglist)
885 argnum++;
886 for (i = 0; i < strlen (str); i++)
887 if (str[i] == ',')
888 argnum++;
889 }
890
891 attr_str = XNEWVEC (char, str_len_sum);
892 str_len_sum = 0;
893 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
894 {
895 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
896 size_t len = strlen (str);
897 memcpy (attr_str + str_len_sum, str, len);
898 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
899 str_len_sum += len + 1;
900 }
901
902 /* Replace "=,-" with "_". */
903 for (i = 0; i < strlen (attr_str); i++)
904 if (attr_str[i] == '=' || attr_str[i]== '-')
905 attr_str[i] = '_';
906
907 if (argnum == 1)
908 return attr_str;
909
910 args = XNEWVEC (char *, argnum);
911
912 i = 0;
913 attr = strtok (attr_str, ",");
914 while (attr != NULL)
915 {
916 args[i] = attr;
917 i++;
918 attr = strtok (NULL, ",");
919 }
920
921 qsort (args, argnum, sizeof (char *), attr_strcmp);
922
923 ret_str = XNEWVEC (char, str_len_sum);
924 str_len_sum = 0;
925 for (i = 0; i < argnum; i++)
926 {
927 size_t len = strlen (args[i]);
928 memcpy (ret_str + str_len_sum, args[i], len);
929 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
930 str_len_sum += len + 1;
931 }
932
933 XDELETEVEC (args);
934 XDELETEVEC (attr_str);
935 return ret_str;
936}
937
938
939/* This function returns true if FN1 and FN2 are versions of the same function,
940 that is, the target strings of the function decls are different. This assumes
941 that FN1 and FN2 have the same signature. */
942
943bool
944common_function_versions (tree fn1, tree fn2)
945{
946 tree attr1, attr2;
947 char *target1, *target2;
948 bool result;
949
950 if (TREE_CODE (fn1) != FUNCTION_DECL
951 || TREE_CODE (fn2) != FUNCTION_DECL)
952 return false;
953
954 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
955 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
956
957 /* At least one function decl should have the target attribute specified. */
958 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
959 return false;
960
961 /* Diagnose missing target attribute if one of the decls is already
962 multi-versioned. */
963 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
964 {
965 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
966 {
967 if (attr2 != NULL_TREE)
968 {
969 std::swap (fn1, fn2);
970 attr1 = attr2;
971 }
972 error_at (DECL_SOURCE_LOCATION (fn2),
973 "missing %<target%> attribute for multi-versioned %qD",
974 fn2);
975 inform (DECL_SOURCE_LOCATION (fn1),
976 "previous declaration of %qD", fn1);
977 /* Prevent diagnosing of the same error multiple times. */
978 DECL_ATTRIBUTES (fn2)
979 = tree_cons (get_identifier ("target"),
980 copy_node (TREE_VALUE (attr1)),
981 DECL_ATTRIBUTES (fn2));
982 }
983 return false;
984 }
985
986 target1 = sorted_attr_string (TREE_VALUE (attr1));
987 target2 = sorted_attr_string (TREE_VALUE (attr2));
988
989 /* The sorted target strings must be different for fn1 and fn2
990 to be versions. */
991 if (strcmp (target1, target2) == 0)
992 result = false;
993 else
994 result = true;
995
996 XDELETEVEC (target1);
997 XDELETEVEC (target2);
998
999 return result;
1000}
1001
1002/* Return a new name by appending SUFFIX to the DECL name. If make_unique
1003 is true, append the full path name of the source file. */
1004
1005char *
1006make_unique_name (tree decl, const char *suffix, bool make_unique)
1007{
1008 char *global_var_name;
1009 int name_len;
1010 const char *name;
1011 const char *unique_name = NULL;
1012
1013 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1014
1015 /* Get a unique name that can be used globally without any chances
1016 of collision at link time. */
1017 if (make_unique)
1018 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
1019
1020 name_len = strlen (name) + strlen (suffix) + 2;
1021
1022 if (make_unique)
1023 name_len += strlen (unique_name) + 1;
1024 global_var_name = XNEWVEC (char, name_len);
1025
1026 /* Use '.' to concatenate names as it is demangler friendly. */
1027 if (make_unique)
1028 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
1029 suffix);
1030 else
1031 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
1032
1033 return global_var_name;
1034}
1035
1036/* Make a dispatcher declaration for the multi-versioned function DECL.
1037 Calls to DECL function will be replaced with calls to the dispatcher
1038 by the front-end. Return the decl created. */
1039
1040tree
1041make_dispatcher_decl (const tree decl)
1042{
1043 tree func_decl;
1044 char *func_name;
1045 tree fn_type, func_type;
1046
1047 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1048
1049 fn_type = TREE_TYPE (decl);
1050 func_type = build_function_type (TREE_TYPE (fn_type),
1051 TYPE_ARG_TYPES (fn_type));
1052
1053 func_decl = build_fn_decl (func_name, func_type);
1054 XDELETEVEC (func_name);
1055 TREE_USED (func_decl) = 1;
1056 DECL_CONTEXT (func_decl) = NULL_TREE;
1057 DECL_INITIAL (func_decl) = error_mark_node;
1058 DECL_ARTIFICIAL (func_decl) = 1;
1059 /* Mark this func as external, the resolver will flip it again if
1060 it gets generated. */
1061 DECL_EXTERNAL (func_decl) = 1;
1062 /* This will be of type IFUNCs have to be externally visible. */
1063 TREE_PUBLIC (func_decl) = 1;
1064
1065 return func_decl;
1066}
1067
1068/* Returns true if decl is multi-versioned and DECL is the default function,
1069 that is it is not tagged with target specific optimization. */
1070
1071bool
1072is_function_default_version (const tree decl)
1073{
1074 if (TREE_CODE (decl) != FUNCTION_DECL
1075 || !DECL_FUNCTION_VERSIONED (decl))
1076 return false;
1077 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1078 gcc_assert (attr);
1079 attr = TREE_VALUE (TREE_VALUE (attr));
1080 return (TREE_CODE (attr) == STRING_CST
1081 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1082}
1083
1084/* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1085 is ATTRIBUTE. */
1086
1087tree
1088build_decl_attribute_variant (tree ddecl, tree attribute)
1089{
1090 DECL_ATTRIBUTES (ddecl) = attribute;
1091 return ddecl;
1092}
1093
1094/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1095 is ATTRIBUTE and its qualifiers are QUALS.
1096
1097 Record such modified types already made so we don't make duplicates. */
1098
1099tree
1100build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
1101{
1102 tree ttype = otype;
1103 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1104 {
1105 tree ntype;
1106
1107 /* Building a distinct copy of a tagged type is inappropriate; it
1108 causes breakage in code that expects there to be a one-to-one
1109 relationship between a struct and its fields.
1110 build_duplicate_type is another solution (as used in
1111 handle_transparent_union_attribute), but that doesn't play well
1112 with the stronger C++ type identity model. */
1113 if (TREE_CODE (ttype) == RECORD_TYPE
1114 || TREE_CODE (ttype) == UNION_TYPE
1115 || TREE_CODE (ttype) == QUAL_UNION_TYPE
1116 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1117 {
1118 warning (OPT_Wattributes,
1119 "ignoring attributes applied to %qT after definition",
1120 TYPE_MAIN_VARIANT (ttype));
1121 return build_qualified_type (ttype, quals);
1122 }
1123
1124 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
1125 if (lang_hooks.types.copy_lang_qualifiers
1126 && otype != TYPE_MAIN_VARIANT (otype))
1127 ttype = (lang_hooks.types.copy_lang_qualifiers
1128 (ttype, TYPE_MAIN_VARIANT (otype)));
1129
1130 ntype = build_distinct_type_copy (ttype);
1131
1132 TYPE_ATTRIBUTES (ntype) = attribute;
1133
1134 hashval_t hash = type_hash_canon_hash (ntype);
1135 ntype = type_hash_canon (hash, ntype);
1136
1137 /* If the target-dependent attributes make NTYPE different from
1138 its canonical type, we will need to use structural equality
1139 checks for this type. */
1140 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1141 || !comp_type_attributes (ntype, ttype))
1142 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
1143 else if (TYPE_CANONICAL (ntype) == ntype)
1144 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1145
1146 ttype = build_qualified_type (ntype, quals);
1147 if (lang_hooks.types.copy_lang_qualifiers
1148 && otype != TYPE_MAIN_VARIANT (otype))
1149 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
1150 }
1151 else if (TYPE_QUALS (ttype) != quals)
1152 ttype = build_qualified_type (ttype, quals);
1153
1154 return ttype;
1155}
1156
1157/* Compare two identifier nodes representing attributes.
1158 Return true if they are the same, false otherwise. */
1159
1160static bool
1161cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1162{
1163 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1164 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1165 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1166
1167 /* Identifiers can be compared directly for equality. */
1168 if (attr1 == attr2)
1169 return true;
1170
1171 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1172 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1173}
1174
1175/* Compare two constructor-element-type constants. Return 1 if the lists
1176 are known to be equal; otherwise return 0. */
1177
1178static bool
1179simple_cst_list_equal (const_tree l1, const_tree l2)
1180{
1181 while (l1 != NULL_TREE && l2 != NULL_TREE)
1182 {
1183 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1184 return false;
1185
1186 l1 = TREE_CHAIN (l1);
1187 l2 = TREE_CHAIN (l2);
1188 }
1189
1190 return l1 == l2;
1191}
1192
1193/* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1194 the same. */
1195
1196static bool
1197omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1198{
1199 tree cl1, cl2;
1200 for (cl1 = clauses1, cl2 = clauses2;
1201 cl1 && cl2;
1202 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1203 {
1204 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1205 return false;
1206 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1207 {
1208 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1209 OMP_CLAUSE_DECL (cl2)) != 1)
1210 return false;
1211 }
1212 switch (OMP_CLAUSE_CODE (cl1))
1213 {
1214 case OMP_CLAUSE_ALIGNED:
1215 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1216 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1217 return false;
1218 break;
1219 case OMP_CLAUSE_LINEAR:
1220 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1221 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1222 return false;
1223 break;
1224 case OMP_CLAUSE_SIMDLEN:
1225 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1226 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1227 return false;
1228 default:
1229 break;
1230 }
1231 }
1232 return true;
1233}
1234
1235
1236/* Compare two attributes for their value identity. Return true if the
1237 attribute values are known to be equal; otherwise return false. */
1238
1239bool
1240attribute_value_equal (const_tree attr1, const_tree attr2)
1241{
1242 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1243 return true;
1244
1245 if (TREE_VALUE (attr1) != NULL_TREE
1246 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1247 && TREE_VALUE (attr2) != NULL_TREE
1248 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1249 {
1250 /* Handle attribute format. */
1251 if (is_attribute_p ("format", get_attribute_name (attr1)))
1252 {
1253 attr1 = TREE_VALUE (attr1);
1254 attr2 = TREE_VALUE (attr2);
1255 /* Compare the archetypes (printf/scanf/strftime/...). */
1256 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1257 return false;
1258 /* Archetypes are the same. Compare the rest. */
1259 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1260 TREE_CHAIN (attr2)) == 1);
1261 }
1262 return (simple_cst_list_equal (TREE_VALUE (attr1),
1263 TREE_VALUE (attr2)) == 1);
1264 }
1265
1266 if (TREE_VALUE (attr1)
1267 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
1268 && TREE_VALUE (attr2)
1269 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1270 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1271 TREE_VALUE (attr2));
1272
1273 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1274}
1275
1276/* Return 0 if the attributes for two types are incompatible, 1 if they
1277 are compatible, and 2 if they are nearly compatible (which causes a
1278 warning to be generated). */
1279int
1280comp_type_attributes (const_tree type1, const_tree type2)
1281{
1282 const_tree a1 = TYPE_ATTRIBUTES (type1);
1283 const_tree a2 = TYPE_ATTRIBUTES (type2);
1284 const_tree a;
1285
1286 if (a1 == a2)
1287 return 1;
1288 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1289 {
1290 const struct attribute_spec *as;
1291 const_tree attr;
1292
1293 as = lookup_attribute_spec (get_attribute_name (a));
1294 if (!as || as->affects_type_identity == false)
1295 continue;
1296
1297 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
1298 if (!attr || !attribute_value_equal (a, attr))
1299 break;
1300 }
1301 if (!a)
1302 {
1303 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1304 {
1305 const struct attribute_spec *as;
1306
1307 as = lookup_attribute_spec (get_attribute_name (a));
1308 if (!as || as->affects_type_identity == false)
1309 continue;
1310
1311 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
1312 break;
1313 /* We don't need to compare trees again, as we did this
1314 already in first loop. */
1315 }
1316 /* All types - affecting identity - are equal, so
1317 there is no need to call target hook for comparison. */
1318 if (!a)
1319 return 1;
1320 }
1321 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1322 return 0;
1323 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1324 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1325 return 0;
1326 /* As some type combinations - like default calling-convention - might
1327 be compatible, we have to call the target hook to get the final result. */
1328 return targetm.comp_type_attributes (type1, type2);
1329}
1330
1331/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1332 is ATTRIBUTE.
1333
1334 Record such modified types already made so we don't make duplicates. */
1335
1336tree
1337build_type_attribute_variant (tree ttype, tree attribute)
1338{
1339 return build_type_attribute_qual_variant (ttype, attribute,
1340 TYPE_QUALS (ttype));
1341}
1342
1343/* A variant of lookup_attribute() that can be used with an identifier
1344 as the first argument, and where the identifier can be either
1345 'text' or '__text__'.
1346
1347 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1348 return a pointer to the attribute's list element if the attribute
1349 is part of the list, or NULL_TREE if not found. If the attribute
1350 appears more than once, this only returns the first occurrence; the
1351 TREE_CHAIN of the return value should be passed back in if further
1352 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1353 can be in the form 'text' or '__text__'. */
1354static tree
1355lookup_ident_attribute (tree attr_identifier, tree list)
1356{
1357 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1358
1359 while (list)
1360 {
1361 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1362 == IDENTIFIER_NODE);
1363
1364 if (cmp_attrib_identifiers (attr_identifier,
1365 get_attribute_name (list)))
1366 /* Found it. */
1367 break;
1368 list = TREE_CHAIN (list);
1369 }
1370
1371 return list;
1372}
1373
1374/* Remove any instances of attribute ATTR_NAME in LIST and return the
1375 modified list. */
1376
1377tree
1378remove_attribute (const char *attr_name, tree list)
1379{
1380 tree *p;
1381 gcc_checking_assert (attr_name[0] != '_');
1382
1383 for (p = &list; *p;)
1384 {
1385 tree l = *p;
1386
1387 tree attr = get_attribute_name (l);
1388 if (is_attribute_p (attr_name, attr))
1389 *p = TREE_CHAIN (l);
1390 else
1391 p = &TREE_CHAIN (l);
1392 }
1393
1394 return list;
1395}
1396
1397/* Return an attribute list that is the union of a1 and a2. */
1398
1399tree
1400merge_attributes (tree a1, tree a2)
1401{
1402 tree attributes;
1403
1404 /* Either one unset? Take the set one. */
1405
1406 if ((attributes = a1) == 0)
1407 attributes = a2;
1408
1409 /* One that completely contains the other? Take it. */
1410
1411 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1412 {
1413 if (attribute_list_contained (a2, a1))
1414 attributes = a2;
1415 else
1416 {
1417 /* Pick the longest list, and hang on the other list. */
1418
1419 if (list_length (a1) < list_length (a2))
1420 attributes = a2, a2 = a1;
1421
1422 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1423 {
1424 tree a;
1425 for (a = lookup_ident_attribute (get_attribute_name (a2),
1426 attributes);
1427 a != NULL_TREE && !attribute_value_equal (a, a2);
1428 a = lookup_ident_attribute (get_attribute_name (a2),
1429 TREE_CHAIN (a)))
1430 ;
1431 if (a == NULL_TREE)
1432 {
1433 a1 = copy_node (a2);
1434 TREE_CHAIN (a1) = attributes;
1435 attributes = a1;
1436 }
1437 }
1438 }
1439 }
1440 return attributes;
1441}
1442
1443/* Given types T1 and T2, merge their attributes and return
1444 the result. */
1445
1446tree
1447merge_type_attributes (tree t1, tree t2)
1448{
1449 return merge_attributes (TYPE_ATTRIBUTES (t1),
1450 TYPE_ATTRIBUTES (t2));
1451}
1452
1453/* Given decls OLDDECL and NEWDECL, merge their attributes and return
1454 the result. */
1455
1456tree
1457merge_decl_attributes (tree olddecl, tree newdecl)
1458{
1459 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1460 DECL_ATTRIBUTES (newdecl));
1461}
1462
1463/* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1464 they are missing there. */
1465
1466void
1467duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1468{
1469 attr = lookup_attribute (name, attr);
1470 if (!attr)
1471 return;
1472 tree a = lookup_attribute (name, *attrs);
1473 while (attr)
1474 {
1475 tree a2;
1476 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1477 if (attribute_value_equal (attr, a2))
1478 break;
1479 if (!a2)
1480 {
1481 a2 = copy_node (attr);
1482 TREE_CHAIN (a2) = *attrs;
1483 *attrs = a2;
1484 }
1485 attr = lookup_attribute (name, TREE_CHAIN (attr));
1486 }
1487}
1488
1489/* Duplicate all attributes from user DECL to the corresponding
1490 builtin that should be propagated. */
1491
1492void
1493copy_attributes_to_builtin (tree decl)
1494{
1495 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1496 if (b)
1497 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1498 DECL_ATTRIBUTES (decl), "omp declare simd");
1499}
1500
1501#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1502
1503/* Specialization of merge_decl_attributes for various Windows targets.
1504
1505 This handles the following situation:
1506
1507 __declspec (dllimport) int foo;
1508 int foo;
1509
1510 The second instance of `foo' nullifies the dllimport. */
1511
1512tree
1513merge_dllimport_decl_attributes (tree old, tree new_tree)
1514{
1515 tree a;
1516 int delete_dllimport_p = 1;
1517
1518 /* What we need to do here is remove from `old' dllimport if it doesn't
1519 appear in `new'. dllimport behaves like extern: if a declaration is
1520 marked dllimport and a definition appears later, then the object
1521 is not dllimport'd. We also remove a `new' dllimport if the old list
1522 contains dllexport: dllexport always overrides dllimport, regardless
1523 of the order of declaration. */
1524 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1525 delete_dllimport_p = 0;
1526 else if (DECL_DLLIMPORT_P (new_tree)
1527 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1528 {
1529 DECL_DLLIMPORT_P (new_tree) = 0;
1530 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1531 "attribute: dllimport ignored", new_tree);
1532 }
1533 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1534 {
1535 /* Warn about overriding a symbol that has already been used, e.g.:
1536 extern int __attribute__ ((dllimport)) foo;
1537 int* bar () {return &foo;}
1538 int foo;
1539 */
1540 if (TREE_USED (old))
1541 {
1542 warning (0, "%q+D redeclared without dllimport attribute "
1543 "after being referenced with dll linkage", new_tree);
1544 /* If we have used a variable's address with dllimport linkage,
1545 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1546 decl may already have had TREE_CONSTANT computed.
1547 We still remove the attribute so that assembler code refers
1548 to '&foo rather than '_imp__foo'. */
1549 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1550 DECL_DLLIMPORT_P (new_tree) = 1;
1551 }
1552
1553 /* Let an inline definition silently override the external reference,
1554 but otherwise warn about attribute inconsistency. */
1555 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1556 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1557 "attribute: previous dllimport ignored", new_tree);
1558 }
1559 else
1560 delete_dllimport_p = 0;
1561
1562 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1563
1564 if (delete_dllimport_p)
1565 a = remove_attribute ("dllimport", a);
1566
1567 return a;
1568}
1569
1570/* Handle a "dllimport" or "dllexport" attribute; arguments as in
1571 struct attribute_spec.handler. */
1572
1573tree
1574handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1575 bool *no_add_attrs)
1576{
1577 tree node = *pnode;
1578 bool is_dllimport;
1579
1580 /* These attributes may apply to structure and union types being created,
1581 but otherwise should pass to the declaration involved. */
1582 if (!DECL_P (node))
1583 {
1584 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1585 | (int) ATTR_FLAG_ARRAY_NEXT))
1586 {
1587 *no_add_attrs = true;
1588 return tree_cons (name, args, NULL_TREE);
1589 }
1590 if (TREE_CODE (node) == RECORD_TYPE
1591 || TREE_CODE (node) == UNION_TYPE)
1592 {
1593 node = TYPE_NAME (node);
1594 if (!node)
1595 return NULL_TREE;
1596 }
1597 else
1598 {
1599 warning (OPT_Wattributes, "%qE attribute ignored",
1600 name);
1601 *no_add_attrs = true;
1602 return NULL_TREE;
1603 }
1604 }
1605
1606 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1607 {
1608 *no_add_attrs = true;
1609 warning (OPT_Wattributes, "%qE attribute ignored",
1610 name);
1611 return NULL_TREE;
1612 }
1613
1614 if (TREE_CODE (node) == TYPE_DECL
1615 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1616 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1617 {
1618 *no_add_attrs = true;
1619 warning (OPT_Wattributes, "%qE attribute ignored",
1620 name);
1621 return NULL_TREE;
1622 }
1623
1624 is_dllimport = is_attribute_p ("dllimport", name);
1625
1626 /* Report error on dllimport ambiguities seen now before they cause
1627 any damage. */
1628 if (is_dllimport)
1629 {
1630 /* Honor any target-specific overrides. */
1631 if (!targetm.valid_dllimport_attribute_p (node))
1632 *no_add_attrs = true;
1633
1634 else if (TREE_CODE (node) == FUNCTION_DECL
1635 && DECL_DECLARED_INLINE_P (node))
1636 {
1637 warning (OPT_Wattributes, "inline function %q+D declared as "
1638 " dllimport: attribute ignored", node);
1639 *no_add_attrs = true;
1640 }
1641 /* Like MS, treat definition of dllimported variables and
1642 non-inlined functions on declaration as syntax errors. */
1643 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1644 {
1645 error ("function %q+D definition is marked dllimport", node);
1646 *no_add_attrs = true;
1647 }
1648
1649 else if (VAR_P (node))
1650 {
1651 if (DECL_INITIAL (node))
1652 {
1653 error ("variable %q+D definition is marked dllimport",
1654 node);
1655 *no_add_attrs = true;
1656 }
1657
1658 /* `extern' needn't be specified with dllimport.
1659 Specify `extern' now and hope for the best. Sigh. */
1660 DECL_EXTERNAL (node) = 1;
1661 /* Also, implicitly give dllimport'd variables declared within
1662 a function global scope, unless declared static. */
1663 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1664 TREE_PUBLIC (node) = 1;
1665 }
1666
1667 if (*no_add_attrs == false)
1668 DECL_DLLIMPORT_P (node) = 1;
1669 }
1670 else if (TREE_CODE (node) == FUNCTION_DECL
1671 && DECL_DECLARED_INLINE_P (node)
1672 && flag_keep_inline_dllexport)
1673 /* An exported function, even if inline, must be emitted. */
1674 DECL_EXTERNAL (node) = 0;
1675
1676 /* Report error if symbol is not accessible at global scope. */
1677 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
1678 {
1679 error ("external linkage required for symbol %q+D because of "
1680 "%qE attribute", node, name);
1681 *no_add_attrs = true;
1682 }
1683
1684 /* A dllexport'd entity must have default visibility so that other
1685 program units (shared libraries or the main executable) can see
1686 it. A dllimport'd entity must have default visibility so that
1687 the linker knows that undefined references within this program
1688 unit can be resolved by the dynamic linker. */
1689 if (!*no_add_attrs)
1690 {
1691 if (DECL_VISIBILITY_SPECIFIED (node)
1692 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
1693 error ("%qE implies default visibility, but %qD has already "
1694 "been declared with a different visibility",
1695 name, node);
1696 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
1697 DECL_VISIBILITY_SPECIFIED (node) = 1;
1698 }
1699
1700 return NULL_TREE;
1701}
1702
1703#endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1704
1705/* Given two lists of attributes, return true if list l2 is
1706 equivalent to l1. */
1707
1708int
1709attribute_list_equal (const_tree l1, const_tree l2)
1710{
1711 if (l1 == l2)
1712 return 1;
1713
1714 return attribute_list_contained (l1, l2)
1715 && attribute_list_contained (l2, l1);
1716}
1717
1718/* Given two lists of attributes, return true if list L2 is
1719 completely contained within L1. */
1720/* ??? This would be faster if attribute names were stored in a canonicalized
1721 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1722 must be used to show these elements are equivalent (which they are). */
1723/* ??? It's not clear that attributes with arguments will always be handled
1724 correctly. */
1725
1726int
1727attribute_list_contained (const_tree l1, const_tree l2)
1728{
1729 const_tree t1, t2;
1730
1731 /* First check the obvious, maybe the lists are identical. */
1732 if (l1 == l2)
1733 return 1;
1734
1735 /* Maybe the lists are similar. */
1736 for (t1 = l1, t2 = l2;
1737 t1 != 0 && t2 != 0
1738 && get_attribute_name (t1) == get_attribute_name (t2)
1739 && TREE_VALUE (t1) == TREE_VALUE (t2);
1740 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1741 ;
1742
1743 /* Maybe the lists are equal. */
1744 if (t1 == 0 && t2 == 0)
1745 return 1;
1746
1747 for (; t2 != 0; t2 = TREE_CHAIN (t2))
1748 {
1749 const_tree attr;
1750 /* This CONST_CAST is okay because lookup_attribute does not
1751 modify its argument and the return value is assigned to a
1752 const_tree. */
1753 for (attr = lookup_ident_attribute (get_attribute_name (t2),
1754 CONST_CAST_TREE (l1));
1755 attr != NULL_TREE && !attribute_value_equal (t2, attr);
1756 attr = lookup_ident_attribute (get_attribute_name (t2),
1757 TREE_CHAIN (attr)))
1758 ;
1759
1760 if (attr == NULL_TREE)
1761 return 0;
1762 }
1763
1764 return 1;
1765}
1766
1767/* The backbone of lookup_attribute(). ATTR_LEN is the string length
1768 of ATTR_NAME, and LIST is not NULL_TREE.
1769
1770 The function is called from lookup_attribute in order to optimize
1771 for size. */
1772
1773tree
1774private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
1775{
1776 while (list)
1777 {
1778 tree attr = get_attribute_name (list);
1779 size_t ident_len = IDENTIFIER_LENGTH (attr);
1780 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
1781 ident_len))
1782 break;
1783 list = TREE_CHAIN (list);
1784 }
1785
1786 return list;
1787}
1788
1789#if CHECKING_P
1790
1791namespace selftest
1792{
1793
1794/* Helper types to verify the consistency attribute exclusions. */
1795
1796typedef std::pair<const char *, const char *> excl_pair;
1797
1798struct excl_hash_traits: typed_noop_remove<excl_pair>
1799{
1800 typedef excl_pair value_type;
1801 typedef value_type compare_type;
1802
1803 static hashval_t hash (const value_type &x)
1804 {
1805 hashval_t h1 = htab_hash_string (x.first);
1806 hashval_t h2 = htab_hash_string (x.second);
1807 return h1 ^ h2;
1808 }
1809
1810 static bool equal (const value_type &x, const value_type &y)
1811 {
1812 return !strcmp (x.first, y.first) && !strcmp (x.second, y.second);
1813 }
1814
1815 static void mark_deleted (value_type &x)
1816 {
1817 x = value_type (NULL, NULL);
1818 }
1819
1820 static void mark_empty (value_type &x)
1821 {
1822 x = value_type ("", "");
1823 }
1824
1825 static bool is_deleted (const value_type &x)
1826 {
1827 return !x.first && !x.second;
1828 }
1829
1830 static bool is_empty (const value_type &x)
1831 {
1832 return !*x.first && !*x.second;
1833 }
1834};
1835
1836
1837/* Self-test to verify that each attribute exclusion is symmetric,
1838 meaning that if attribute A is encoded as incompatible with
1839 attribute B then the opposite relationship is also encoded.
1840 This test also detects most cases of misspelled attribute names
1841 in exclusions. */
1842
1843static void
1844test_attribute_exclusions ()
1845{
1846 /* Iterate over the array of attribute tables first (with TI0 as
1847 the index) and over the array of attribute_spec in each table
1848 (with SI0 as the index). */
1849 const size_t ntables = ARRAY_SIZE (attribute_tables);
1850
1851 /* Set of pairs of mutually exclusive attributes. */
1852 typedef hash_set<excl_pair, excl_hash_traits> exclusion_set;
1853 exclusion_set excl_set;
1854
1855 for (size_t ti0 = 0; ti0 != ntables; ++ti0)
1856 for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0)
1857 {
1858 const attribute_spec::exclusions *excl
1859 = attribute_tables[ti0][s0].exclude;
1860
1861 /* Skip each attribute that doesn't define exclusions. */
1862 if (!excl)
1863 continue;
1864
1865 const char *attr_name = attribute_tables[ti0][s0].name;
1866
1867 /* Iterate over the set of exclusions for every attribute
1868 (with EI0 as the index) adding the exclusions defined
1869 for each to the set. */
1870 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
1871 {
1872 const char *excl_name = excl[ei0].name;
1873
1874 if (!strcmp (attr_name, excl_name))
1875 continue;
1876
1877 excl_set.add (excl_pair (attr_name, excl_name));
1878 }
1879 }
1880
1881 /* Traverse the set of mutually exclusive pairs of attributes
1882 and verify that they are symmetric. */
1883 for (exclusion_set::iterator it = excl_set.begin ();
1884 it != excl_set.end ();
1885 ++it)
1886 {
1887 if (!excl_set.contains (excl_pair ((*it).second, (*it).first)))
1888 {
1889 /* An exclusion for an attribute has been found that
1890 doesn't have a corresponding exclusion in the opposite
1891 direction. */
1892 char desc[120];
1893 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
1894 (*it).first, (*it).second);
1895 fail (SELFTEST_LOCATION, desc);
1896 }
1897 }
1898}
1899
1900void
1901attribute_c_tests ()
1902{
1903 test_attribute_exclusions ();
1904}
1905
1906} /* namespace selftest */
1907
1908#endif /* CHECKING_P */
1909