]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
re PR c++/66093 (g++ produces incorrect output on code with constexpr function initia...
[thirdparty/gcc.git] / gcc / attribs.c
CommitLineData
bb9f8221 1/* Functions dealing with attribute handling, used by most front ends.
cbe34bb5 2 Copyright (C) 1992-2017 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
RK
19
20#include "config.h"
21#include "system.h"
4977bab6 22#include "coretypes.h"
957060b5 23#include "target.h"
bb9f8221 24#include "tree.h"
d8a2d370 25#include "stringpool.h"
957060b5 26#include "diagnostic-core.h"
d8a2d370
DN
27#include "attribs.h"
28#include "stor-layout.h"
7ffb4fd2 29#include "langhooks.h"
d1c8e08a 30#include "plugin.h"
bb9f8221 31
349ae713 32/* Table of the tables of attributes (common, language, format, machine)
bb9f8221
RK
33 searched. */
34static const struct attribute_spec *attribute_tables[4];
35
23b43207
JH
36/* Substring representation. */
37
38struct substring
39{
40 const char *str;
41 int length;
42};
43
4a8fb1a1
LC
44/* Simple hash function to avoid need to scan whole string. */
45
46static inline hashval_t
47substring_hash (const char *str, int l)
48{
49 return str[0] + str[l - 1] * 256 + l * 65536;
50}
51
52/* Used for attribute_hash. */
53
8d67ee55 54struct attribute_hasher : nofree_ptr_hash <attribute_spec>
4a8fb1a1 55{
67f58944
TS
56 typedef substring *compare_type;
57 static inline hashval_t hash (const attribute_spec *);
58 static inline bool equal (const attribute_spec *, const substring *);
4a8fb1a1
LC
59};
60
61inline hashval_t
67f58944 62attribute_hasher::hash (const attribute_spec *spec)
4a8fb1a1
LC
63{
64 const int l = strlen (spec->name);
65 return substring_hash (spec->name, l);
66}
67
68inline bool
67f58944 69attribute_hasher::equal (const attribute_spec *spec, const substring *str)
4a8fb1a1
LC
70{
71 return (strncmp (spec->name, str->str, str->length) == 0
72 && !spec->name[str->length]);
73}
74
e28d52cf
DS
75/* Scoped attribute name representation. */
76
77struct scoped_attributes
78{
79 const char *ns;
9771b263 80 vec<attribute_spec> attributes;
c203e8a7 81 hash_table<attribute_hasher> *attribute_hash;
e28d52cf
DS
82};
83
e28d52cf 84/* The table of scope attributes. */
9771b263 85static vec<scoped_attributes> attributes_table;
e28d52cf
DS
86
87static scoped_attributes* find_attribute_namespace (const char*);
88static void register_scoped_attribute (const struct attribute_spec *,
89 scoped_attributes *);
90
bb9f8221
RK
91static bool attributes_initialized = false;
92
bb9f8221 93/* Default empty table of attributes. */
23b43207 94
bb9f8221
RK
95static const struct attribute_spec empty_attribute_table[] =
96{
62d784f7 97 { NULL, 0, 0, false, false, false, NULL, false }
bb9f8221
RK
98};
99
23b43207
JH
100/* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
101 To avoid need for copying, we simply return length of the string. */
102
103static void
104extract_attribute_substring (struct substring *str)
105{
106 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
107 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
108 {
109 str->length -= 4;
110 str->str += 2;
111 }
112}
113
e28d52cf
DS
114/* Insert an array of attributes ATTRIBUTES into a namespace. This
115 array must be NULL terminated. NS is the name of attribute
116 namespace. The function returns the namespace into which the
117 attributes have been registered. */
118
119scoped_attributes*
120register_scoped_attributes (const struct attribute_spec * attributes,
121 const char* ns)
122{
123 scoped_attributes *result = NULL;
124
125 /* See if we already have attributes in the namespace NS. */
126 result = find_attribute_namespace (ns);
127
128 if (result == NULL)
129 {
130 /* We don't have any namespace NS yet. Create one. */
131 scoped_attributes sa;
132
d067e05f 133 if (attributes_table.is_empty ())
9771b263 134 attributes_table.create (64);
e28d52cf
DS
135
136 memset (&sa, 0, sizeof (sa));
137 sa.ns = ns;
9771b263
DN
138 sa.attributes.create (64);
139 result = attributes_table.safe_push (sa);
c203e8a7 140 result->attribute_hash = new hash_table<attribute_hasher> (200);
e28d52cf
DS
141 }
142
143 /* Really add the attributes to their namespace now. */
144 for (unsigned i = 0; attributes[i].name != NULL; ++i)
145 {
9771b263 146 result->attributes.safe_push (attributes[i]);
e28d52cf
DS
147 register_scoped_attribute (&attributes[i], result);
148 }
149
150 gcc_assert (result != NULL);
151
152 return result;
153}
154
155/* Return the namespace which name is NS, NULL if none exist. */
156
157static scoped_attributes*
158find_attribute_namespace (const char* ns)
159{
160 unsigned ix;
161 scoped_attributes *iter;
162
9771b263 163 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
e28d52cf
DS
164 if (ns == iter->ns
165 || (iter->ns != NULL
166 && ns != NULL
167 && !strcmp (iter->ns, ns)))
168 return iter;
169 return NULL;
170}
171
b2b29377
MM
172/* Make some sanity checks on the attribute tables. */
173
174static void
175check_attribute_tables (void)
176{
177 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
178 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
179 {
180 /* The name must not begin and end with __. */
181 const char *name = attribute_tables[i][j].name;
182 int len = strlen (name);
183
184 gcc_assert (!(name[0] == '_' && name[1] == '_'
185 && name[len - 1] == '_' && name[len - 2] == '_'));
186
187 /* The minimum and maximum lengths must be consistent. */
188 gcc_assert (attribute_tables[i][j].min_length >= 0);
189
190 gcc_assert (attribute_tables[i][j].max_length == -1
191 || (attribute_tables[i][j].max_length
192 >= attribute_tables[i][j].min_length));
193
194 /* An attribute cannot require both a DECL and a TYPE. */
195 gcc_assert (!attribute_tables[i][j].decl_required
196 || !attribute_tables[i][j].type_required);
197
198 /* If an attribute requires a function type, in particular
199 it requires a type. */
200 gcc_assert (!attribute_tables[i][j].function_type_required
201 || attribute_tables[i][j].type_required);
202 }
203
204 /* Check that each name occurs just once in each table. */
205 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
206 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
207 for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++)
208 gcc_assert (strcmp (attribute_tables[i][j].name,
209 attribute_tables[i][k].name));
210
211 /* Check that no name occurs in more than one table. Names that
212 begin with '*' are exempt, and may be overridden. */
213 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
214 for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
215 for (size_t k = 0; attribute_tables[i][k].name != NULL; k++)
216 for (size_t l = 0; attribute_tables[j][l].name != NULL; l++)
217 gcc_assert (attribute_tables[i][k].name[0] == '*'
218 || strcmp (attribute_tables[i][k].name,
219 attribute_tables[j][l].name));
220}
221
222/* Initialize attribute tables, and make some sanity checks if checking is
223 enabled. */
bb9f8221 224
8dd00781 225void
4682ae04 226init_attributes (void)
bb9f8221 227{
ca7558fc 228 size_t i;
bb9f8221 229
8dd00781
JJ
230 if (attributes_initialized)
231 return;
232
349ae713
NB
233 attribute_tables[0] = lang_hooks.common_attribute_table;
234 attribute_tables[1] = lang_hooks.attribute_table;
235 attribute_tables[2] = lang_hooks.format_attribute_table;
bb9f8221
RK
236 attribute_tables[3] = targetm.attribute_table;
237
349ae713
NB
238 /* Translate NULL pointers to pointers to the empty table. */
239 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
240 if (attribute_tables[i] == NULL)
241 attribute_tables[i] = empty_attribute_table;
242
b2b29377
MM
243 if (flag_checking)
244 check_attribute_tables ();
bb9f8221 245
e28d52cf
DS
246 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
247 /* Put all the GNU attributes into the "gnu" namespace. */
248 register_scoped_attributes (attribute_tables[i], "gnu");
249
d1c8e08a
TG
250 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
251 attributes_initialized = true;
252}
253
254/* Insert a single ATTR into the attribute table. */
255
256void
b8698a0f 257register_attribute (const struct attribute_spec *attr)
e28d52cf
DS
258{
259 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
260}
261
262/* Insert a single attribute ATTR into a namespace of attributes. */
263
264static void
265register_scoped_attribute (const struct attribute_spec *attr,
266 scoped_attributes *name_space)
d1c8e08a 267{
16f7ad42 268 struct substring str;
4a8fb1a1 269 attribute_spec **slot;
16f7ad42 270
e28d52cf
DS
271 gcc_assert (attr != NULL && name_space != NULL);
272
c203e8a7 273 gcc_assert (name_space->attribute_hash);
e28d52cf 274
16f7ad42
TG
275 str.str = attr->name;
276 str.length = strlen (str.str);
70e41a6a
NP
277
278 /* Attribute names in the table must be in the form 'text' and not
279 in the form '__text__'. */
280 gcc_assert (str.length > 0 && str.str[0] != '_');
281
4a8fb1a1 282 slot = name_space->attribute_hash
c203e8a7
TS
283 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
284 INSERT);
0a35513e 285 gcc_assert (!*slot || attr->name[0] == '*');
4a8fb1a1 286 *slot = CONST_CAST (struct attribute_spec *, attr);
bb9f8221 287}
a7f6bc8c 288
e28d52cf
DS
289/* Return the spec for the scoped attribute with namespace NS and
290 name NAME. */
a7f6bc8c 291
862d0b35 292static const struct attribute_spec *
e28d52cf 293lookup_scoped_attribute_spec (const_tree ns, const_tree name)
a7f6bc8c
JM
294{
295 struct substring attr;
e28d52cf
DS
296 scoped_attributes *attrs;
297
298 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
299
300 attrs = find_attribute_namespace (ns_str);
301
302 if (attrs == NULL)
303 return NULL;
a7f6bc8c
JM
304
305 attr.str = IDENTIFIER_POINTER (name);
306 attr.length = IDENTIFIER_LENGTH (name);
307 extract_attribute_substring (&attr);
c203e8a7
TS
308 return attrs->attribute_hash->find_with_hash (&attr,
309 substring_hash (attr.str,
310 attr.length));
a7f6bc8c 311}
e28d52cf 312
7dbb85a7
JM
313/* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
314 it also specifies the attribute namespace. */
e28d52cf
DS
315
316const struct attribute_spec *
317lookup_attribute_spec (const_tree name)
318{
7dbb85a7
JM
319 tree ns;
320 if (TREE_CODE (name) == TREE_LIST)
321 {
322 ns = TREE_PURPOSE (name);
323 name = TREE_VALUE (name);
324 }
325 else
326 ns = get_identifier ("gnu");
327 return lookup_scoped_attribute_spec (ns, name);
e28d52cf
DS
328}
329
862d0b35
DN
330
331/* Return the namespace of the attribute ATTR. This accessor works on
332 GNU and C++11 (scoped) attributes. On GNU attributes,
333 it returns an identifier tree for the string "gnu".
334
335 Please read the comments of cxx11_attribute_p to understand the
336 format of attributes. */
337
338static tree
339get_attribute_namespace (const_tree attr)
340{
341 if (cxx11_attribute_p (attr))
342 return TREE_PURPOSE (TREE_PURPOSE (attr));
343 return get_identifier ("gnu");
344}
345
346
bb9f8221
RK
347/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
348 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
349 it should be modified in place; if a TYPE, a copy should be created
350 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
351 information, in the form of a bitwise OR of flags in enum attribute_flags
352 from tree.h. Depending on these flags, some attributes may be
353 returned to be applied at a later stage (for example, to apply
03aa99d4 354 a decl attribute to the declaration rather than to its type). */
bb9f8221
RK
355
356tree
4682ae04 357decl_attributes (tree *node, tree attributes, int flags)
bb9f8221
RK
358{
359 tree a;
360 tree returned_attrs = NULL_TREE;
361
e28d52cf 362 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
21516d64
VR
363 return NULL_TREE;
364
bb9f8221
RK
365 if (!attributes_initialized)
366 init_attributes ();
367
ab442df7
MM
368 /* If this is a function and the user used #pragma GCC optimize, add the
369 options to the attribute((optimize(...))) list. */
370 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
371 {
372 tree cur_attr = lookup_attribute ("optimize", attributes);
373 tree opts = copy_list (current_optimize_pragma);
374
375 if (! cur_attr)
376 attributes
377 = tree_cons (get_identifier ("optimize"), opts, attributes);
378 else
379 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
380 }
381
382 if (TREE_CODE (*node) == FUNCTION_DECL
383 && optimization_current_node != optimization_default_node
384 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
385 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
386
5779e713
MM
387 /* If this is a function and the user used #pragma GCC target, add the
388 options to the attribute((target(...))) list. */
ab442df7 389 if (TREE_CODE (*node) == FUNCTION_DECL
5779e713 390 && current_target_pragma
ab442df7 391 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
5779e713 392 current_target_pragma, 0))
ab442df7 393 {
5779e713
MM
394 tree cur_attr = lookup_attribute ("target", attributes);
395 tree opts = copy_list (current_target_pragma);
ab442df7
MM
396
397 if (! cur_attr)
5779e713 398 attributes = tree_cons (get_identifier ("target"), opts, attributes);
ab442df7
MM
399 else
400 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
401 }
402
61044492
JZ
403 /* A "naked" function attribute implies "noinline" and "noclone" for
404 those targets that support it. */
405 if (TREE_CODE (*node) == FUNCTION_DECL
70e41a6a 406 && attributes
61044492
JZ
407 && lookup_attribute_spec (get_identifier ("naked"))
408 && lookup_attribute ("naked", attributes) != NULL)
409 {
410 if (lookup_attribute ("noinline", attributes) == NULL)
411 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
412
413 if (lookup_attribute ("noclone", attributes) == NULL)
414 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
415 }
416
5fd9b178 417 targetm.insert_attributes (*node, &attributes);
bb9f8221
RK
418
419 for (a = attributes; a; a = TREE_CHAIN (a))
420 {
e28d52cf
DS
421 tree ns = get_attribute_namespace (a);
422 tree name = get_attribute_name (a);
bb9f8221
RK
423 tree args = TREE_VALUE (a);
424 tree *anode = node;
e28d52cf
DS
425 const struct attribute_spec *spec =
426 lookup_scoped_attribute_spec (ns, name);
bb9f8221 427 bool no_add_attrs = 0;
f9ceed32 428 int fn_ptr_quals = 0;
3acef2ae 429 tree fn_ptr_tmp = NULL_TREE;
bb9f8221
RK
430
431 if (spec == NULL)
432 {
e384e6b5 433 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
e28d52cf
DS
434 {
435 if (ns == NULL_TREE || !cxx11_attribute_p (a))
436 warning (OPT_Wattributes, "%qE attribute directive ignored",
437 name);
438 else
439 warning (OPT_Wattributes,
440 "%<%E::%E%> scoped attribute directive ignored",
441 ns, name);
442 }
bb9f8221
RK
443 continue;
444 }
445 else if (list_length (args) < spec->min_length
446 || (spec->max_length >= 0
447 && list_length (args) > spec->max_length))
448 {
4f1e4960
JM
449 error ("wrong number of arguments specified for %qE attribute",
450 name);
bb9f8221
RK
451 continue;
452 }
23b43207 453 gcc_assert (is_attribute_p (spec->name, name));
bb9f8221 454
e28d52cf
DS
455 if (TYPE_P (*node)
456 && cxx11_attribute_p (a)
457 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
458 {
459 /* This is a c++11 attribute that appertains to a
460 type-specifier, outside of the definition of, a class
461 type. Ignore it. */
fe6f27c7
PC
462 if (warning (OPT_Wattributes, "attribute ignored"))
463 inform (input_location,
464 "an attribute that appertains to a type-specifier "
465 "is ignored");
e28d52cf
DS
466 continue;
467 }
468
bb9f8221
RK
469 if (spec->decl_required && !DECL_P (*anode))
470 {
471 if (flags & ((int) ATTR_FLAG_DECL_NEXT
472 | (int) ATTR_FLAG_FUNCTION_NEXT
473 | (int) ATTR_FLAG_ARRAY_NEXT))
474 {
475 /* Pass on this attribute to be tried again. */
476 returned_attrs = tree_cons (name, args, returned_attrs);
477 continue;
478 }
479 else
480 {
4f1e4960
JM
481 warning (OPT_Wattributes, "%qE attribute does not apply to types",
482 name);
bb9f8221
RK
483 continue;
484 }
485 }
486
dd4dc3cd
RK
487 /* If we require a type, but were passed a decl, set up to make a
488 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
489 would have applied if we'd been passed a type, but we cannot modify
490 the decl's type in place here. */
bb9f8221 491 if (spec->type_required && DECL_P (*anode))
dd4dc3cd
RK
492 {
493 anode = &TREE_TYPE (*anode);
26c87b1a 494 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
dd4dc3cd 495 }
bb9f8221
RK
496
497 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
498 && TREE_CODE (*anode) != METHOD_TYPE)
499 {
500 if (TREE_CODE (*anode) == POINTER_TYPE
501 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
502 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
503 {
3acef2ae
JM
504 /* OK, this is a bit convoluted. We can't just make a copy
505 of the pointer type and modify its TREE_TYPE, because if
506 we change the attributes of the target type the pointer
507 type needs to have a different TYPE_MAIN_VARIANT. So we
508 pull out the target type now, frob it as appropriate, and
509 rebuild the pointer type later.
510
c22cacf3
MS
511 This would all be simpler if attributes were part of the
512 declarator, grumble grumble. */
3acef2ae 513 fn_ptr_tmp = TREE_TYPE (*anode);
f9ceed32 514 fn_ptr_quals = TYPE_QUALS (*anode);
3acef2ae
JM
515 anode = &fn_ptr_tmp;
516 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
bb9f8221
RK
517 }
518 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
519 {
520 /* Pass on this attribute to be tried again. */
521 returned_attrs = tree_cons (name, args, returned_attrs);
522 continue;
523 }
524
525 if (TREE_CODE (*anode) != FUNCTION_TYPE
526 && TREE_CODE (*anode) != METHOD_TYPE)
527 {
5c498b10 528 warning (OPT_Wattributes,
4f1e4960
JM
529 "%qE attribute only applies to function types",
530 name);
bb9f8221
RK
531 continue;
532 }
533 }
534
b9e75696
JM
535 if (TYPE_P (*anode)
536 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
537 && TYPE_SIZE (*anode) != NULL_TREE)
538 {
539 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
540 continue;
541 }
542
bb9f8221 543 if (spec->handler != NULL)
e28d52cf
DS
544 {
545 int cxx11_flag =
546 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
547
548 returned_attrs = chainon ((*spec->handler) (anode, name, args,
549 flags|cxx11_flag,
550 &no_add_attrs),
551 returned_attrs);
552 }
1b9191d2
AH
553
554 /* Layout the decl in case anything changed. */
555 if (spec->type_required && DECL_P (*node)
8813a647 556 && (VAR_P (*node)
67282790
JM
557 || TREE_CODE (*node) == PARM_DECL
558 || TREE_CODE (*node) == RESULT_DECL))
d1838621 559 relayout_decl (*node);
1b9191d2 560
bb9f8221
RK
561 if (!no_add_attrs)
562 {
563 tree old_attrs;
564 tree a;
565
566 if (DECL_P (*anode))
567 old_attrs = DECL_ATTRIBUTES (*anode);
568 else
569 old_attrs = TYPE_ATTRIBUTES (*anode);
570
571 for (a = lookup_attribute (spec->name, old_attrs);
572 a != NULL_TREE;
573 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
574 {
575 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
576 break;
577 }
578
579 if (a == NULL_TREE)
580 {
581 /* This attribute isn't already in the list. */
582 if (DECL_P (*anode))
583 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
584 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
67214984
JM
585 {
586 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
587 /* If this is the main variant, also push the attributes
588 out to the other variants. */
589 if (*anode == TYPE_MAIN_VARIANT (*anode))
590 {
591 tree variant;
592 for (variant = *anode; variant;
593 variant = TYPE_NEXT_VARIANT (variant))
594 {
595 if (TYPE_ATTRIBUTES (variant) == old_attrs)
596 TYPE_ATTRIBUTES (variant)
597 = TYPE_ATTRIBUTES (*anode);
598 else if (!lookup_attribute
599 (spec->name, TYPE_ATTRIBUTES (variant)))
600 TYPE_ATTRIBUTES (variant) = tree_cons
601 (name, args, TYPE_ATTRIBUTES (variant));
602 }
603 }
604 }
bb9f8221
RK
605 else
606 *anode = build_type_attribute_variant (*anode,
607 tree_cons (name, args,
608 old_attrs));
609 }
610 }
3acef2ae
JM
611
612 if (fn_ptr_tmp)
613 {
614 /* Rebuild the function pointer type and put it in the
615 appropriate place. */
616 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
f9ceed32
MM
617 if (fn_ptr_quals)
618 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
3acef2ae
JM
619 if (DECL_P (*node))
620 TREE_TYPE (*node) = fn_ptr_tmp;
3acef2ae 621 else
298e6adc
NS
622 {
623 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
624 *node = fn_ptr_tmp;
625 }
3acef2ae 626 }
bb9f8221
RK
627 }
628
629 return returned_attrs;
630}
0a35513e 631
e28d52cf
DS
632/* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
633 attribute.
634
635 When G++ parses a C++11 attribute, it is represented as
636 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
637 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
638 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
639 use get_attribute_namespace and get_attribute_name to retrieve the
640 namespace and name of the attribute, as these accessors work with
641 GNU attributes as well. */
642
643bool
644cxx11_attribute_p (const_tree attr)
645{
646 if (attr == NULL_TREE
647 || TREE_CODE (attr) != TREE_LIST)
648 return false;
649
650 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
651}
652
653/* Return the name of the attribute ATTR. This accessor works on GNU
654 and C++11 (scoped) attributes.
655
656 Please read the comments of cxx11_attribute_p to understand the
657 format of attributes. */
658
659tree
660get_attribute_name (const_tree attr)
661{
662 if (cxx11_attribute_p (attr))
663 return TREE_VALUE (TREE_PURPOSE (attr));
664 return TREE_PURPOSE (attr);
665}
666
0a35513e
AH
667/* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
668 to the method FNDECL. */
669
670void
671apply_tm_attr (tree fndecl, tree attr)
672{
673 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
674}
3b1661a9
ES
675
676/* Makes a function attribute of the form NAME(ARG_NAME) and chains
677 it to CHAIN. */
678
679tree
680make_attribute (const char *name, const char *arg_name, tree chain)
681{
682 tree attr_name;
683 tree attr_arg_name;
684 tree attr_args;
685 tree attr;
686
687 attr_name = get_identifier (name);
688 attr_arg_name = build_string (strlen (arg_name), arg_name);
689 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
690 attr = tree_cons (attr_name, attr_args, chain);
691 return attr;
692}
1b062c1a
MM
693
694\f
695/* Common functions used for target clone support. */
696
697/* Comparator function to be used in qsort routine to sort attribute
698 specification strings to "target". */
699
700static int
701attr_strcmp (const void *v1, const void *v2)
702{
703 const char *c1 = *(char *const*)v1;
704 const char *c2 = *(char *const*)v2;
705 return strcmp (c1, c2);
706}
707
708/* ARGLIST is the argument to target attribute. This function tokenizes
709 the comma separated arguments, sorts them and returns a string which
710 is a unique identifier for the comma separated arguments. It also
711 replaces non-identifier characters "=,-" with "_". */
712
713char *
714sorted_attr_string (tree arglist)
715{
716 tree arg;
717 size_t str_len_sum = 0;
718 char **args = NULL;
719 char *attr_str, *ret_str;
720 char *attr = NULL;
721 unsigned int argnum = 1;
722 unsigned int i;
723
724 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
725 {
726 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
727 size_t len = strlen (str);
728 str_len_sum += len + 1;
729 if (arg != arglist)
730 argnum++;
731 for (i = 0; i < strlen (str); i++)
732 if (str[i] == ',')
733 argnum++;
734 }
735
736 attr_str = XNEWVEC (char, str_len_sum);
737 str_len_sum = 0;
738 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
739 {
740 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
741 size_t len = strlen (str);
742 memcpy (attr_str + str_len_sum, str, len);
743 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
744 str_len_sum += len + 1;
745 }
746
747 /* Replace "=,-" with "_". */
748 for (i = 0; i < strlen (attr_str); i++)
749 if (attr_str[i] == '=' || attr_str[i]== '-')
750 attr_str[i] = '_';
751
752 if (argnum == 1)
753 return attr_str;
754
755 args = XNEWVEC (char *, argnum);
756
757 i = 0;
758 attr = strtok (attr_str, ",");
759 while (attr != NULL)
760 {
761 args[i] = attr;
762 i++;
763 attr = strtok (NULL, ",");
764 }
765
766 qsort (args, argnum, sizeof (char *), attr_strcmp);
767
768 ret_str = XNEWVEC (char, str_len_sum);
769 str_len_sum = 0;
770 for (i = 0; i < argnum; i++)
771 {
772 size_t len = strlen (args[i]);
773 memcpy (ret_str + str_len_sum, args[i], len);
774 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
775 str_len_sum += len + 1;
776 }
777
778 XDELETEVEC (args);
779 XDELETEVEC (attr_str);
780 return ret_str;
781}
782
783
784/* This function returns true if FN1 and FN2 are versions of the same function,
785 that is, the target strings of the function decls are different. This assumes
786 that FN1 and FN2 have the same signature. */
787
788bool
789common_function_versions (tree fn1, tree fn2)
790{
791 tree attr1, attr2;
792 char *target1, *target2;
793 bool result;
794
795 if (TREE_CODE (fn1) != FUNCTION_DECL
796 || TREE_CODE (fn2) != FUNCTION_DECL)
797 return false;
798
799 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
800 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
801
802 /* At least one function decl should have the target attribute specified. */
803 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
804 return false;
805
806 /* Diagnose missing target attribute if one of the decls is already
807 multi-versioned. */
808 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
809 {
810 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
811 {
812 if (attr2 != NULL_TREE)
813 {
814 std::swap (fn1, fn2);
815 attr1 = attr2;
816 }
817 error_at (DECL_SOURCE_LOCATION (fn2),
818 "missing %<target%> attribute for multi-versioned %qD",
819 fn2);
820 inform (DECL_SOURCE_LOCATION (fn1),
821 "previous declaration of %qD", fn1);
822 /* Prevent diagnosing of the same error multiple times. */
823 DECL_ATTRIBUTES (fn2)
824 = tree_cons (get_identifier ("target"),
825 copy_node (TREE_VALUE (attr1)),
826 DECL_ATTRIBUTES (fn2));
827 }
828 return false;
829 }
830
831 target1 = sorted_attr_string (TREE_VALUE (attr1));
832 target2 = sorted_attr_string (TREE_VALUE (attr2));
833
834 /* The sorted target strings must be different for fn1 and fn2
835 to be versions. */
836 if (strcmp (target1, target2) == 0)
837 result = false;
838 else
839 result = true;
840
841 XDELETEVEC (target1);
842 XDELETEVEC (target2);
843
844 return result;
845}
846
847/* Return a new name by appending SUFFIX to the DECL name. If make_unique
848 is true, append the full path name of the source file. */
849
850char *
851make_unique_name (tree decl, const char *suffix, bool make_unique)
852{
853 char *global_var_name;
854 int name_len;
855 const char *name;
856 const char *unique_name = NULL;
857
858 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
859
860 /* Get a unique name that can be used globally without any chances
861 of collision at link time. */
862 if (make_unique)
863 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
864
865 name_len = strlen (name) + strlen (suffix) + 2;
866
867 if (make_unique)
868 name_len += strlen (unique_name) + 1;
869 global_var_name = XNEWVEC (char, name_len);
870
871 /* Use '.' to concatenate names as it is demangler friendly. */
872 if (make_unique)
873 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
874 suffix);
875 else
876 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
877
878 return global_var_name;
879}
880
881/* Make a dispatcher declaration for the multi-versioned function DECL.
882 Calls to DECL function will be replaced with calls to the dispatcher
883 by the front-end. Return the decl created. */
884
885tree
886make_dispatcher_decl (const tree decl)
887{
888 tree func_decl;
889 char *func_name;
890 tree fn_type, func_type;
891 bool is_uniq = false;
892
893 if (TREE_PUBLIC (decl) == 0)
894 is_uniq = true;
895
896 func_name = make_unique_name (decl, "ifunc", is_uniq);
897
898 fn_type = TREE_TYPE (decl);
899 func_type = build_function_type (TREE_TYPE (fn_type),
900 TYPE_ARG_TYPES (fn_type));
901
902 func_decl = build_fn_decl (func_name, func_type);
903 XDELETEVEC (func_name);
904 TREE_USED (func_decl) = 1;
905 DECL_CONTEXT (func_decl) = NULL_TREE;
906 DECL_INITIAL (func_decl) = error_mark_node;
907 DECL_ARTIFICIAL (func_decl) = 1;
908 /* Mark this func as external, the resolver will flip it again if
909 it gets generated. */
910 DECL_EXTERNAL (func_decl) = 1;
911 /* This will be of type IFUNCs have to be externally visible. */
912 TREE_PUBLIC (func_decl) = 1;
913
914 return func_decl;
915}
916
917/* Returns true if decl is multi-versioned and DECL is the default function,
918 that is it is not tagged with target specific optimization. */
919
920bool
921is_function_default_version (const tree decl)
922{
923 if (TREE_CODE (decl) != FUNCTION_DECL
924 || !DECL_FUNCTION_VERSIONED (decl))
925 return false;
926 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
927 gcc_assert (attr);
928 attr = TREE_VALUE (TREE_VALUE (attr));
929 return (TREE_CODE (attr) == STRING_CST
930 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
931}