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