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