Debian Codegraph @

gcc/d/d-attribs.cc

raw
1 /* d-attribs.c -- D attributes handling.
2 Copyright (C) 2015-2025 Free Software Foundation, Inc.
3
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 /* Implementation of attribute handlers for user defined attributes and
19 internal built-in functions. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24
25 #include "dmd/attrib.h"
26 #include "dmd/declaration.h"
27 #include "dmd/expression.h"
28 #include "dmd/module.h"
29 #include "dmd/mtype.h"
30 #include "dmd/template.h"
31
32 #include "tree.h"
33 #include "diagnostic.h"
34 #include "tm.h"
35 #include "cgraph.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "common/common-target.h"
39 #include "stringpool.h"
40 #include "attribs.h"
41 #include "varasm.h"
42 #include "fold-const.h"
43 #include "opts.h"
44
45 #include "d-tree.h"
46
47
48 /* Internal attribute handlers for built-in functions. */
49 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
57 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
58 static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
59 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
60 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
61 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *);
62
63 /* D attribute handlers for user defined attributes. */
64 static tree d_handle_noinline_attribute (tree *, tree, tree, int, bool *);
65 static tree d_handle_always_inline_attribute (tree *, tree, tree, int, bool *);
66 static tree d_handle_flatten_attribute (tree *, tree, tree, int, bool *);
67 static tree d_handle_target_attribute (tree *, tree, tree, int, bool *);
68 static tree d_handle_target_clones_attribute (tree *, tree, tree, int, bool *);
69 static tree d_handle_optimize_attribute (tree *, tree, tree, int, bool *);
70 static tree d_handle_noclone_attribute (tree *, tree, tree, int, bool *);
71 static tree d_handle_noicf_attribute (tree *, tree, tree, int, bool *);
72 static tree d_handle_noipa_attribute (tree *, tree, tree, int, bool *);
73 static tree d_handle_section_attribute (tree *, tree, tree, int, bool *);
74 static tree d_handle_symver_attribute (tree *, tree, tree, int, bool *);
75 static tree d_handle_weak_attribute (tree *, tree, tree, int, bool *) ;
76 static tree d_handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
77 static tree d_handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
78 static tree d_handle_cold_attribute (tree *, tree, tree, int, bool *);
79 static tree d_handle_register_attribute (tree *, tree, tree, int, bool *);
80 static tree d_handle_restrict_attribute (tree *, tree, tree, int, bool *);
81 static tree d_handle_used_attribute (tree *, tree, tree, int, bool *);
82 static tree d_handle_visibility_attribute (tree *, tree, tree, int, bool *);
83 static tree d_handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
84 static tree d_handle_simd_attribute (tree *, tree, tree, int, bool *);
85
86 /* Helper to define attribute exclusions. */
87 #define ATTR_EXCL(name, function, type, variable) \
88 { name, function, type, variable }
89
90 /* Define attributes that are mutually exclusive with one another. */
91 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
92 {
93 ATTR_EXCL ("alloc_size", true, true, true),
94 ATTR_EXCL ("const", true, true, true),
95 ATTR_EXCL ("malloc", true, true, true),
96 ATTR_EXCL ("pure", true, true, true),
97 ATTR_EXCL ("returns_twice", true, true, true),
98 ATTR_EXCL (NULL, false, false, false),
99 };
100
101 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
102 {
103 ATTR_EXCL ("noreturn", true, true, true),
104 ATTR_EXCL (NULL, false, false, false),
105 };
106
107 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
108 {
109 ATTR_EXCL ("alloc_size", true, true, true),
110 ATTR_EXCL ("const", true, true, true),
111 ATTR_EXCL ("noreturn", true, true, true),
112 ATTR_EXCL ("pure", true, true, true),
113 ATTR_EXCL (NULL, false, false, false)
114 };
115
116 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
117 {
118 ATTR_EXCL ("noinline", true, true, true),
119 ATTR_EXCL ("target_clones", true, true, true),
120 ATTR_EXCL (NULL, false, false, false),
121 };
122
123 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
124 {
125 ATTR_EXCL ("always_inline", true, true, true),
126 ATTR_EXCL (NULL, false, false, false),
127 };
128
129 static const struct attribute_spec::exclusions attr_target_exclusions[] =
130 {
131 ATTR_EXCL ("target_clones", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
132 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE),
133 ATTR_EXCL (NULL, false, false, false),
134 };
135
136 static const struct attribute_spec::exclusions attr_target_clones_exclusions[] =
137 {
138 ATTR_EXCL ("always_inline", true, true, true),
139 ATTR_EXCL ("target", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
140 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE),
141 ATTR_EXCL (NULL, false, false, false),
142 };
143
144 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
145 {
146 ATTR_EXCL ("const", true, true, true),
147 ATTR_EXCL ("noreturn", true, true, true),
148 ATTR_EXCL ("pure", true, true, true),
149 ATTR_EXCL (NULL, false, false, false),
150 };
151
152 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
153 {
154 ATTR_EXCL ("cold", true, true, true),
155 ATTR_EXCL ("hot", true, true, true),
156 ATTR_EXCL (NULL, false, false, false)
157 };
158
159 /* Helper to define an attribute. */
160 #define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req, \
161 affects_type_identity, handler, exclude) \
162 { name, min_len, max_len, decl_req, type_req, fn_type_req, \
163 affects_type_identity, handler, exclude }
164
165 /* Table of machine-independent attributes.
166 For internal use (marking of built-ins) only. */
167 static const attribute_spec d_langhook_common_attributes[] =
168 {
169 ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
170 handle_noreturn_attribute, attr_noreturn_exclusions),
171 ATTR_SPEC ("leaf", 0, 0, true, false, false, false,
172 handle_leaf_attribute, NULL),
173 ATTR_SPEC ("const", 0, 0, true, false, false, false,
174 handle_const_attribute, attr_const_pure_exclusions),
175 ATTR_SPEC ("malloc", 0, 0, true, false, false, false,
176 handle_malloc_attribute, NULL),
177 ATTR_SPEC ("returns_twice", 0, 0, true, false, false, false,
178 handle_returns_twice_attribute, attr_returns_twice_exclusions),
179 ATTR_SPEC ("pure", 0, 0, true, false, false, false,
180 handle_pure_attribute, attr_const_pure_exclusions),
181 ATTR_SPEC ("nonnull", 0, -1, false, true, true, false,
182 handle_nonnull_attribute, NULL),
183 ATTR_SPEC ("nothrow", 0, 0, true, false, false, false,
184 handle_nothrow_attribute, NULL),
185 ATTR_SPEC ("transaction_pure", 0, 0, false, true, true, false,
186 handle_transaction_pure_attribute, NULL),
187 ATTR_SPEC ("no vops", 0, 0, true, false, false, false,
188 handle_novops_attribute, NULL),
189 ATTR_SPEC ("type generic", 0, 0, false, true, true, false,
190 handle_type_generic_attribute, NULL),
191 ATTR_SPEC ("fn spec", 1, 1, false, true, true, false,
192 handle_fnspec_attribute, NULL),
193 ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
194 handle_omp_declare_simd_attribute, NULL),
195 };
196
197 const scoped_attribute_specs d_langhook_common_attribute_table =
198 {
199 "gnu", { d_langhook_common_attributes }
200 };
201
202 /* Table of D language attributes exposed by `gcc.attribute' UDAs. */
203 static const attribute_spec d_langhook_gnu_attributes[] =
204 {
205 ATTR_SPEC ("noinline", 0, 0, true, false, false, false,
206 d_handle_noinline_attribute, attr_noinline_exclusions),
207 ATTR_SPEC ("always_inline", 0, 0, true, false, false, false,
208 d_handle_always_inline_attribute, attr_inline_exclusions),
209 ATTR_SPEC ("flatten", 0, 0, true, false, false, false,
210 d_handle_flatten_attribute, NULL),
211 ATTR_SPEC ("target", 1, -1, true, false, false, false,
212 d_handle_target_attribute, attr_target_exclusions),
213 ATTR_SPEC ("target_clones", 1, -1, true, false, false, false,
214 d_handle_target_clones_attribute, attr_target_clones_exclusions),
215 ATTR_SPEC ("optimize", 1, -1, true, false, false, false,
216 d_handle_optimize_attribute, NULL),
217 ATTR_SPEC ("noclone", 0, 0, true, false, false, false,
218 d_handle_noclone_attribute, NULL),
219 ATTR_SPEC ("no_icf", 0, 0, true, false, false, false,
220 d_handle_noicf_attribute, NULL),
221 ATTR_SPEC ("noipa", 0, 0, true, false, false, false,
222 d_handle_noipa_attribute, NULL),
223 ATTR_SPEC ("section", 1, 1, true, false, false, false,
224 d_handle_section_attribute, NULL),
225 ATTR_SPEC ("symver", 1, -1, true, false, false, false,
226 d_handle_symver_attribute, NULL),
227 ATTR_SPEC ("weak", 0, 0, true, false, false, false,
228 d_handle_weak_attribute, NULL),
229 ATTR_SPEC ("noplt", 0, 0, true, false, false, false,
230 d_handle_noplt_attribute, NULL),
231 ATTR_SPEC ("alloc_size", 1, 3, false, true, true, false,
232 d_handle_alloc_size_attribute, attr_alloc_exclusions),
233 ATTR_SPEC ("cold", 0, 0, true, false, false, false,
234 d_handle_cold_attribute, attr_cold_hot_exclusions),
235 ATTR_SPEC ("no_sanitize", 1, -1, true, false, false, false,
236 d_handle_no_sanitize_attribute, NULL),
237 ATTR_SPEC ("register", 1, 1, true, false, false, false,
238 d_handle_register_attribute, NULL),
239 ATTR_SPEC ("restrict", 0, 0, true, false, false, false,
240 d_handle_restrict_attribute, NULL),
241 ATTR_SPEC ("simd", 0, 1, true, false, false, false,
242 d_handle_simd_attribute, NULL),
243 ATTR_SPEC ("used", 0, 0, true, false, false, false,
244 d_handle_used_attribute, NULL),
245 ATTR_SPEC ("visibility", 1, 1, false, false, false, false,
246 d_handle_visibility_attribute, NULL),
247 };
248
249 const scoped_attribute_specs d_langhook_gnu_attribute_table =
250 {
251 "gnu", { d_langhook_gnu_attributes }
252 };
253
254 /* Insert the type attribute ATTRNAME with value VALUE into TYPE.
255 Returns a new variant of the original type declaration. */
256
257 tree
258 insert_type_attribute (tree type, const char *attrname, tree value)
259 {
260 tree ident = get_identifier (attrname);
261
262 if (value)
263 value = tree_cons (NULL_TREE, value, NULL_TREE);
264
265 decl_attributes (&type, build_tree_list (ident, value),
266 ATTR_FLAG_TYPE_IN_PLACE);
267 return type;
268 }
269
270 /* Insert the decl attribute ATTRNAME with value VALUE into DECL. */
271
272 tree
273 insert_decl_attribute (tree decl, const char *attrname, tree value)
274 {
275 tree ident = get_identifier (attrname);
276
277 if (value)
278 value = tree_cons (NULL_TREE, value, NULL_TREE);
279
280 decl_attributes (&decl, build_tree_list (ident, value), 0);
281
282 return decl;
283 }
284
285 /* Returns TRUE if NAME is an attribute recognized as being handled by
286 the `gcc.attribute' module. */
287
288 static bool
289 uda_attribute_p (const char *name)
290 {
291 tree ident = get_identifier (name);
292
293 /* Search both our language, and target attribute tables.
294 Common and format attributes are kept internal. */
295 for (const attribute_spec &p : d_langhook_gnu_attributes)
296 if (get_identifier (p.name) == ident)
297 return true;
298
299 for (auto scoped_attributes : targetm.attribute_table)
300 for (const attribute_spec &p : scoped_attributes->attributes)
301 if (get_identifier (p.name) == ident)
302 return true;
303
304 return false;
305 }
306
307 /* [attribute/uda]
308
309 User Defined Attributes (UDA) are compile time expressions that can be
310 attached to a declaration. These attributes can then be queried, extracted,
311 and manipulated at compile-time. There is no run-time component to them.
312
313 Expand and merge all UDAs found in the EATTRS list that are of type
314 `gcc.attribute.Attribute'. This symbol is internally recognized by the
315 compiler and maps them to their equivalent GCC attribute. */
316
317 static tree
318 build_attributes (Expressions *eattrs)
319 {
320 if (!eattrs)
321 return NULL_TREE;
322
323 dmd::expandTuples (eattrs);
324
325 tree attribs = NULL_TREE;
326
327 for (size_t i = 0; i < eattrs->length; i++)
328 {
329 Expression *attr = (*eattrs)[i];
330 Dsymbol *sym = dmd::toDsymbol (attr->type, NULL);
331
332 if (!sym)
333 {
334 /* If attribute is a template symbol, perhaps arguments were not
335 supplied, so warn about attribute having no effect. */
336 if (TemplateExp *te = attr->isTemplateExp ())
337 {
338 if (!te->td || !te->td->onemember)
339 continue;
340
341 sym = te->td->onemember;
342 }
343 else
344 continue;
345 }
346
347 /* Attribute symbol must come from the `gcc.attribute' module. */
348 Dsymbol *mod = sym->getModule ();
349 if (!(strcmp (mod->toChars (), "attributes") == 0
350 && mod->parent != NULL
351 && strcmp (mod->parent->toChars (), "gcc") == 0
352 && !mod->parent->parent))
353 continue;
354
355 /* Get the result of the attribute if it hasn't already been folded. */
356 if (attr->op == EXP::call)
357 attr = dmd::ctfeInterpret (attr);
358
359 if (attr->op != EXP::structLiteral)
360 {
361 warning_at (make_location_t (attr->loc), OPT_Wattributes,
362 "%qE attribute has no effect",
363 get_identifier (sym->toChars ()));
364 continue;
365 }
366
367 /* Should now have a struct `Attribute("attrib", "value", ...)'
368 initializer list. */
369 Expressions *elems = attr->isStructLiteralExp ()->elements;
370 Expression *e0 = (*elems)[0];
371
372 if (e0->op != EXP::string_)
373 {
374 warning_at (make_location_t (attr->loc), OPT_Wattributes,
375 "unknown attribute %qs", e0->toChars());
376 continue;
377 }
378
379 StringExp *se = e0->toStringExp ();
380 gcc_assert (se->sz == 1);
381
382 /* Empty string attribute, just ignore it. */
383 if (se->len == 0)
384 continue;
385
386 /* Check if the attribute is recognized and handled.
387 Done here to report the diagnostic at the right location. */
388 const char *name = (const char *)(se->len ? se->string : "");
389 if (!uda_attribute_p (name))
390 {
391 warning_at (make_location_t (attr->loc), OPT_Wattributes,
392 "unknown attribute %qs", name);
393 continue;
394 }
395
396 /* Chain all attribute arguments together. */
397 tree args = NULL_TREE;
398
399 for (size_t j = 1; j < elems->length; j++)
400 {
401 Expression *e = (*elems)[j];
402 /* Stop after the first `void' argument. */
403 if (e == NULL)
404 break;
405
406 StringExp *s = e->isStringExp ();
407 tree t;
408 if (s != NULL && s->sz == 1)
409 {
410 const char *string = (const char *)(s->len ? s->string : "");
411 t = build_string (s->len, string);
412 }
413 else
414 t = build_expr (e);
415
416 args = chainon (args, build_tree_list (0, t));
417 }
418
419 tree list = build_tree_list (get_identifier (name), args);
420 attribs = chainon (attribs, list);
421 }
422
423 return attribs;
424 }
425
426 /* If any GCC attributes are found in the declaration SYM, apply them to the
427 type or decl NODE. */
428
429 void
430 apply_user_attributes (Dsymbol *sym, tree node)
431 {
432 UserAttributeDeclaration *uda = sym->userAttribDecl ();
433 if (uda == NULL)
434 return;
435
436 location_t saved_location = input_location;
437 input_location = make_location_t (sym->loc);
438
439 int attr_flags = 0;
440 if (TYPE_P (node) && !COMPLETE_TYPE_P (node))
441 attr_flags |= ATTR_FLAG_TYPE_IN_PLACE;
442
443 Expressions *attrs = dmd::getAttributes (uda);
444 decl_attributes (&node, build_attributes (attrs), attr_flags);
445
446 input_location = saved_location;
447 }
448
449 /* Built-in attribute handlers.
450 These functions take the arguments:
451 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
452
453 /* Handle a "noreturn" attribute; arguments as in
454 struct attribute_spec.handler. */
455
456 static tree
457 handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
458 {
459 tree type = TREE_TYPE (*node);
460
461 if (TREE_CODE (*node) == FUNCTION_DECL)
462 TREE_THIS_VOLATILE (*node) = 1;
463 else if (TREE_CODE (type) == POINTER_TYPE
464 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
465 TREE_TYPE (*node)
466 = build_pointer_type
467 (build_type_variant (TREE_TYPE (type),
468 TYPE_READONLY (TREE_TYPE (type)), 1));
469 else
470 gcc_unreachable ();
471
472 return NULL_TREE;
473 }
474
475 /* Handle a "leaf" attribute; arguments as in
476 struct attribute_spec.handler. */
477
478 static tree
479 handle_leaf_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
480 {
481 if (TREE_CODE (*node) != FUNCTION_DECL)
482 {
483 warning (OPT_Wattributes, "%qE attribute ignored", name);
484 *no_add_attrs = true;
485 }
486 if (!TREE_PUBLIC (*node))
487 {
488 warning (OPT_Wattributes, "%qE attribute has no effect", name);
489 *no_add_attrs = true;
490 }
491
492 return NULL_TREE;
493 }
494
495 /* Handle a "const" attribute; arguments as in
496 struct attribute_spec.handler. */
497
498 static tree
499 handle_const_attribute (tree *node, tree, tree, int, bool *)
500 {
501 tree type = TREE_TYPE (*node);
502
503 if (TREE_CODE (*node) == FUNCTION_DECL)
504 TREE_READONLY (*node) = 1;
505 else if (TREE_CODE (type) == POINTER_TYPE
506 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
507 TREE_TYPE (*node)
508 = build_pointer_type
509 (build_type_variant (TREE_TYPE (type), 1,
510 TREE_THIS_VOLATILE (TREE_TYPE (type))));
511 else
512 gcc_unreachable ();
513
514 return NULL_TREE;
515 }
516
517 /* Handle a "malloc" attribute; arguments as in
518 struct attribute_spec.handler. */
519
520 tree
521 handle_malloc_attribute (tree *node, tree, tree, int, bool *)
522 {
523 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
524 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))));
525 DECL_IS_MALLOC (*node) = 1;
526 return NULL_TREE;
527 }
528
529 /* Handle a "pure" attribute; arguments as in
530 struct attribute_spec.handler. */
531
532 static tree
533 handle_pure_attribute (tree *node, tree, tree, int, bool *)
534 {
535 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
536 DECL_PURE_P (*node) = 1;
537 return NULL_TREE;
538 }
539
540 /* Handle a "no vops" attribute; arguments as in
541 struct attribute_spec.handler. */
542
543 static tree
544 handle_novops_attribute (tree *node, tree, tree, int, bool *)
545 {
546 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
547 DECL_IS_NOVOPS (*node) = 1;
548 return NULL_TREE;
549 }
550
551 /* Helper for nonnull attribute handling; fetch the operand number
552 from the attribute argument list. */
553
554 static bool
555 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
556 {
557 /* Verify the arg number is a constant. */
558 if (!tree_fits_uhwi_p (arg_num_expr))
559 return false;
560
561 *valp = TREE_INT_CST_LOW (arg_num_expr);
562 return true;
563 }
564
565 /* Handle the "nonnull" attribute. */
566
567 static tree
568 handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
569 {
570 tree type = *node;
571
572 /* If no arguments are specified, all pointer arguments should be
573 non-null. Verify a full prototype is given so that the arguments
574 will have the correct types when we actually check them later.
575 Avoid diagnosing type-generic built-ins since those have no
576 prototype. */
577 if (!args)
578 {
579 gcc_assert (prototype_p (type)
580 || !TYPE_ATTRIBUTES (type)
581 || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
582
583 return NULL_TREE;
584 }
585
586 /* Argument list specified. Verify that each argument number references
587 a pointer argument. */
588 for (; args; args = TREE_CHAIN (args))
589 {
590 tree argument;
591 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
592
593 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
594 gcc_unreachable ();
595
596 argument = TYPE_ARG_TYPES (type);
597 if (argument)
598 {
599 for (ck_num = 1; ; ck_num++)
600 {
601 if (!argument || ck_num == arg_num)
602 break;
603 argument = TREE_CHAIN (argument);
604 }
605
606 gcc_assert (argument
607 && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
608 }
609 }
610
611 return NULL_TREE;
612 }
613
614 /* Handle a "nothrow" attribute; arguments as in
615 struct attribute_spec.handler. */
616
617 static tree
618 handle_nothrow_attribute (tree *node, tree, tree, int, bool *)
619 {
620 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
621 TREE_NOTHROW (*node) = 1;
622 return NULL_TREE;
623 }
624
625 /* Handle a "type generic" attribute; arguments as in
626 struct attribute_spec.handler. */
627
628 static tree
629 handle_type_generic_attribute (tree *node, tree, tree, int, bool *)
630 {
631 /* Ensure we have a function type. */
632 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
633
634 /* Ensure we have a variadic function. */
635 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
636
637 return NULL_TREE;
638 }
639
640 /* Handle a "transaction_pure" attribute; arguments as in
641 struct attribute_spec.handler. */
642
643 static tree
644 handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
645 {
646 /* Ensure we have a function type. */
647 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
648
649 return NULL_TREE;
650 }
651
652 /* Handle a "returns_twice" attribute; arguments as in
653 struct attribute_spec.handler. */
654
655 static tree
656 handle_returns_twice_attribute (tree *node, tree, tree, int, bool *)
657 {
658 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
659
660 DECL_IS_RETURNS_TWICE (*node) = 1;
661
662 return NULL_TREE;
663 }
664
665 /* Handle a "fn spec" attribute; arguments as in
666 struct attribute_spec.handler. */
667
668 tree
669 handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
670 {
671 gcc_assert (args
672 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
673 && !TREE_CHAIN (args));
674 return NULL_TREE;
675 }
676
677 /* Handle an "omp declare simd" attribute; arguments as in
678 struct attribute_spec.handler. */
679
680 tree
681 handle_omp_declare_simd_attribute (tree *node, tree, tree, int, bool *)
682 {
683 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
684 return NULL_TREE;
685 }
686
687 /* Language specific attribute handlers.
688 These functions take the arguments:
689 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
690
691 /* Handle a "noinline" attribute; arguments as in
692 struct attribute_spec.handler. */
693
694 static tree
695 d_handle_noinline_attribute (tree *node, tree name, tree, int,
696 bool *no_add_attrs)
697 {
698 if (TREE_CODE (*node) == FUNCTION_DECL)
699 DECL_UNINLINABLE (*node) = 1;
700 else
701 {
702 warning (OPT_Wattributes, "%qE attribute ignored", name);
703 *no_add_attrs = true;
704 }
705
706 return NULL_TREE;
707 }
708
709 /* Handle a "always_inline" attribute; arguments as in
710 struct attribute_spec.handler. */
711
712 static tree
713 d_handle_always_inline_attribute (tree *node, tree name, tree, int,
714 bool *no_add_attrs)
715 {
716 if (TREE_CODE (*node) == FUNCTION_DECL)
717 {
718 DECL_DECLARED_INLINE_P (*node) = 1;
719 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
720 }
721 else
722 {
723 warning (OPT_Wattributes, "%qE attribute ignored", name);
724 *no_add_attrs = true;
725 }
726
727 return NULL_TREE;
728 }
729
730 /* Handle a "flatten" attribute; arguments as in
731 struct attribute_spec.handler. */
732
733 static tree
734 d_handle_flatten_attribute (tree *node, tree name, tree, int,
735 bool *no_add_attrs)
736 {
737 if (TREE_CODE (*node) != FUNCTION_DECL)
738 {
739 warning (OPT_Wattributes, "%qE attribute ignored", name);
740 *no_add_attrs = true;
741 }
742
743 return NULL_TREE;
744 }
745
746 /* Handle a "target" attribute; arguments as in
747 struct attribute_spec.handler. */
748
749 static tree
750 d_handle_target_attribute (tree *node, tree name, tree args, int flags,
751 bool *no_add_attrs)
752 {
753 /* Ensure we have a function type. */
754 if (TREE_CODE (*node) != FUNCTION_DECL)
755 {
756 warning (OPT_Wattributes, "%qE attribute ignored", name);
757 *no_add_attrs = true;
758 }
759 else if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
760 *no_add_attrs = true;
761
762 /* Check that there's no empty string in values of the attribute. */
763 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
764 {
765 tree value = TREE_VALUE (t);
766 if (TREE_CODE (value) != STRING_CST
767 || (TREE_STRING_LENGTH (value) != 0
768 && TREE_STRING_POINTER (value)[0] != '\0'))
769 continue;
770
771 warning (OPT_Wattributes, "empty string in attribute %<target%>");
772 *no_add_attrs = true;
773 }
774
775 return NULL_TREE;
776 }
777
778 /* Handle a "target_clones" attribute; arguments as in
779 struct attribute_spec.handler. */
780
781 static tree
782 d_handle_target_clones_attribute (tree *node, tree name, tree, int,
783 bool *no_add_attrs)
784 {
785 /* Ensure we have a function type. */
786 if (TREE_CODE (*node) != FUNCTION_DECL)
787 {
788 warning (OPT_Wattributes, "%qE attribute ignored", name);
789 *no_add_attrs = true;
790 }
791 else
792 {
793 /* Do not inline functions with multiple clone targets. */
794 DECL_UNINLINABLE (*node) = 1;
795 }
796
797 return NULL_TREE;
798 }
799
800 /* Arguments being collected for optimization. */
801 static GTY(()) vec <const char *, va_gc> *optimize_args;
802
803 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
804 options in ARGS. */
805
806 static bool
807 parse_optimize_options (tree args)
808 {
809 bool ret = true;
810
811 /* Build up argv vector. Just in case the string is stored away, use garbage
812 collected strings. */
813 vec_safe_truncate (optimize_args, 0);
814 vec_safe_push (optimize_args, (const char *) NULL);
815
816 for (tree ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
817 {
818 tree value = TREE_VALUE (ap);
819
820 if (TREE_CODE (value) == INTEGER_CST)
821 {
822 char buffer[20];
823 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
824 vec_safe_push (optimize_args, ggc_strdup (buffer));
825 }
826 else if (TREE_CODE (value) == STRING_CST)
827 {
828 size_t len = TREE_STRING_LENGTH (value);
829 const char *p = TREE_STRING_POINTER (value);
830
831 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
832 options. */
833 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
834 {
835 ret = false;
836 warning (OPT_Wattributes,
837 "bad option %qs to attribute %<optimize%>", p);
838 continue;
839 }
840
841 /* Can't use GC memory here. */
842 char *q = XOBNEWVEC (&opts_obstack, char, len + 3);
843 char *r = q;
844
845 if (*p != '-')
846 {
847 *r++ = '-';
848
849 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
850 itself is -Os, and any other switch begins with a -f. */
851 if ((*p >= '0' && *p <= '9') || (p[0] == 's' && p[1] == '\0'))
852 *r++ = 'O';
853 else if (*p != 'O')
854 *r++ = 'f';
855 }
856
857 memcpy (r, p, len);
858 r[len] = '\0';
859 vec_safe_push (optimize_args, (const char *) q);
860 }
861 }
862
863 unsigned opt_argc = optimize_args->length ();
864 const char **opt_argv
865 = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
866
867 for (unsigned i = 1; i < opt_argc; i++)
868 opt_argv[i] = (*optimize_args)[i];
869
870 /* Now parse the options. */
871 struct cl_decoded_option *decoded_options;
872 unsigned int decoded_options_count;
873
874 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
875 &decoded_options,
876 &decoded_options_count);
877 /* Drop non-Optimization options. */
878 unsigned j = 1;
879 for (unsigned i = 1; i < decoded_options_count; ++i)
880 {
881 unsigned opt_index = decoded_options[i].opt_index;
882 if (opt_index >= cl_options_count
883 || ! (cl_options[opt_index].flags & CL_OPTIMIZATION))
884 {
885 ret = false;
886 warning (OPT_Wattributes,
887 "bad option %qs to attribute %<optimize%>",
888 decoded_options[i].orig_option_with_args_text);
889 continue;
890 }
891 if (i != j)
892 decoded_options[j] = decoded_options[i];
893 j++;
894 }
895 decoded_options_count = j;
896 /* And apply them. */
897 decode_options (&global_options, &global_options_set,
898 decoded_options, decoded_options_count,
899 input_location, global_dc, NULL);
900
901 targetm.override_options_after_change();
902
903 optimize_args->truncate (0);
904 return ret;
905 }
906
907 /* Handle a "optimize" attribute; arguments as in
908 struct attribute_spec.handler. */
909
910 static tree
911 d_handle_optimize_attribute (tree *node, tree name, tree args, int,
912 bool *no_add_attrs)
913 {
914 /* Ensure we have a function type. */
915 if (TREE_CODE (*node) != FUNCTION_DECL)
916 {
917 warning (OPT_Wattributes, "%qE attribute ignored", name);
918 *no_add_attrs = true;
919 }
920 else
921 {
922 struct cl_optimization cur_opts;
923 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
924
925 /* Save current options. */
926 cl_optimization_save (&cur_opts, &global_options, &global_options_set);
927 tree prev_target_node = build_target_option_node (&global_options,
928 &global_options_set);
929
930 /* If we previously had some optimization options, use them as the
931 default. */
932 gcc_options *saved_global_options = NULL;
933 if (flag_checking)
934 {
935 saved_global_options = XNEW (gcc_options);
936 *saved_global_options = global_options;
937 }
938
939 if (old_opts)
940 cl_optimization_restore (&global_options, &global_options_set,
941 TREE_OPTIMIZATION (old_opts));
942
943 /* Parse options, and update the vector. */
944 parse_optimize_options (args);
945 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
946 = build_optimization_node (&global_options, &global_options_set);
947 tree target_node = build_target_option_node (&global_options,
948 &global_options_set);
949 if (prev_target_node != target_node)
950 DECL_FUNCTION_SPECIFIC_TARGET (*node) = target_node;
951
952 /* Restore current options. */
953 cl_optimization_restore (&global_options, &global_options_set,
954 &cur_opts);
955 cl_target_option_restore (&global_options, &global_options_set,
956 TREE_TARGET_OPTION (prev_target_node));
957 if (saved_global_options != NULL)
958 {
959 cl_optimization_compare (saved_global_options, &global_options);
960 free (saved_global_options);
961 }
962 }
963
964 return NULL_TREE;
965 }
966
967 /* Handle a "noclone" attribute; arguments as in
968 struct attribute_spec.handler. */
969
970 static tree
971 d_handle_noclone_attribute (tree *node, tree name, tree, int,
972 bool *no_add_attrs)
973 {
974 if (TREE_CODE (*node) != FUNCTION_DECL)
975 {
976 warning (OPT_Wattributes, "%qE attribute ignored", name);
977 *no_add_attrs = true;
978 }
979
980 return NULL_TREE;
981 }
982
983 /* Handle a "no_icf" attribute; arguments as in
984 struct attribute_spec.handler. */
985
986 static tree
987 d_handle_noicf_attribute (tree *node, tree name, tree, int,
988 bool *no_add_attrs)
989 {
990 if (TREE_CODE (*node) != FUNCTION_DECL)
991 {
992 warning (OPT_Wattributes, "%qE attribute ignored", name);
993 *no_add_attrs = true;
994 }
995
996 return NULL_TREE;
997 }
998
999 /* Handle a "noipa" attribute; arguments as in
1000 struct attribute_spec.handler. */
1001
1002 static tree
1003 d_handle_noipa_attribute (tree *node, tree name, tree, int,
1004 bool *no_add_attrs)
1005 {
1006 if (TREE_CODE (*node) != FUNCTION_DECL)
1007 {
1008 warning (OPT_Wattributes, "%qE attribute ignored", name);
1009 *no_add_attrs = true;
1010 }
1011
1012 return NULL_TREE;
1013 }
1014
1015 /* Handle a "section" attribute; arguments as in
1016 struct attribute_spec.handler. */
1017
1018 static tree
1019 d_handle_section_attribute (tree *node, tree name, tree args, int flags,
1020 bool *no_add_attrs)
1021 {
1022 if (!targetm_common.have_named_sections)
1023 {
1024 error ("section attributes are not supported for this target");
1025 *no_add_attrs = true;
1026 return NULL_TREE;
1027 }
1028
1029 if (!VAR_OR_FUNCTION_DECL_P (*node))
1030 {
1031 error ("section attribute not allowed for %q+D", *node);
1032 *no_add_attrs = true;
1033 return NULL_TREE;
1034 }
1035
1036 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1037 {
1038 error ("%qE attribute argument not a string constant", name);
1039 *no_add_attrs = true;
1040 return NULL_TREE;
1041 }
1042
1043 if (VAR_P (*node)
1044 && current_function_decl != NULL_TREE
1045 && !TREE_STATIC (*node))
1046 {
1047 error ("section attribute cannot be specified for local variables");
1048 *no_add_attrs = true;
1049 return NULL_TREE;
1050 }
1051
1052 /* The decl may have already been given a section attribute
1053 from a previous declaration. Ensure they match. */
1054 if (DECL_SECTION_NAME (*node) != NULL
1055 && strcmp (DECL_SECTION_NAME (*node),
1056 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1057 {
1058 error ("section of %q+D conflicts with previous declaration", *node);
1059 *no_add_attrs = true;
1060 return NULL_TREE;
1061 }
1062
1063 if (VAR_P (*node)
1064 && !targetm.have_tls && targetm.emutls.tmpl_section
1065 && DECL_THREAD_LOCAL_P (*node))
1066 {
1067 error ("section of %q+D cannot be overridden", *node);
1068 *no_add_attrs = true;
1069 return NULL_TREE;
1070 }
1071
1072 tree res = targetm.handle_generic_attribute (node, name, args, flags,
1073 no_add_attrs);
1074
1075 /* If the back end confirms the attribute can be added then continue onto
1076 final processing. */
1077 if (*no_add_attrs)
1078 return NULL_TREE;
1079
1080 set_decl_section_name (*node, TREE_STRING_POINTER (TREE_VALUE (args)));
1081 return res;
1082 }
1083
1084 /* Handle a "symver" and attribute; arguments as in
1085 struct attribute_spec.handler. */
1086
1087 static tree
1088 d_handle_symver_attribute (tree *node, tree name, tree args, int,
1089 bool *no_add_attrs)
1090 {
1091 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1092 {
1093 warning (OPT_Wattributes,
1094 "%<symver%> attribute only applies to functions and variables");
1095 *no_add_attrs = true;
1096 return NULL_TREE;
1097 }
1098
1099 if (!decl_in_symtab_p (*node))
1100 {
1101 warning (OPT_Wattributes,
1102 "%<symver%> attribute is only applicable to symbols");
1103 *no_add_attrs = true;
1104 return NULL_TREE;
1105 }
1106
1107 for (; args; args = TREE_CHAIN (args))
1108 {
1109 tree symver = TREE_VALUE (args);
1110 if (TREE_CODE (symver) != STRING_CST)
1111 {
1112 error ("%qE attribute argument not a string constant", name);
1113 *no_add_attrs = true;
1114 return NULL_TREE;
1115 }
1116
1117 const char *symver_str = TREE_STRING_POINTER (symver);
1118
1119 int ats = 0;
1120 for (int n = 0; (int)n < TREE_STRING_LENGTH (symver); n++)
1121 if (symver_str[n] == '@')
1122 ats++;
1123
1124 if (ats != 1 && ats != 2)
1125 {
1126 error ("symver attribute argument must have format %<name@nodename%>");
1127 error ("%<symver%> attribute argument %qs must contain one or two "
1128 "%<@%>", symver_str);
1129 *no_add_attrs = true;
1130 return NULL_TREE;
1131 }
1132 }
1133
1134 return NULL_TREE;
1135 }
1136
1137 /* Handle a "weak" attribute; arguments as in
1138 struct attribute_spec.handler. */
1139
1140 static tree
1141 d_handle_weak_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1142 {
1143 if (TREE_CODE (*node) == FUNCTION_DECL
1144 && DECL_DECLARED_INLINE_P (*node))
1145 {
1146 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1147 *no_add_attrs = true;
1148 }
1149 else if (VAR_OR_FUNCTION_DECL_P (*node))
1150 {
1151 struct symtab_node *n = symtab_node::get (*node);
1152 if (n && n->refuse_visibility_changes)
1153 error ("%q+D declared weak after being used", *node);
1154 declare_weak (*node);
1155 }
1156 else
1157 warning (OPT_Wattributes, "%qE attribute ignored", name);
1158
1159 return NULL_TREE;
1160 }
1161
1162 /* Handle a "noplt" attribute; arguments as in
1163 struct attribute_spec.handler. */
1164
1165 static tree
1166 d_handle_noplt_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1167 {
1168 if (TREE_CODE (*node) != FUNCTION_DECL)
1169 {
1170 warning (OPT_Wattributes, "%qE attribute ignored", name);
1171 *no_add_attrs = true;
1172 }
1173
1174 return NULL_TREE;
1175 }
1176
1177 /* Verify that argument value POS at position ARGNO to attribute ATNAME applied
1178 to function FNTYPE refers to a function parameter at position POS and is a
1179 valid integer type. When ZERO_BASED is true, POS is adjusted to be 1-based.
1180 If successful, POS is returned. Otherwise, issue appropriate warnings and
1181 return null. A non-zero 1-based ARGNO should be passed in by callers only
1182 for attributes with more than one argument. */
1183
1184 static tree
1185 positional_argument (const_tree fntype, const_tree atname, tree pos,
1186 int argno, bool zero_based)
1187 {
1188 tree postype = TREE_TYPE (pos);
1189
1190 if (pos == error_mark_node || !postype)
1191 {
1192 /* Only mention the positional argument number when it's non-zero. */
1193 if (argno < 1)
1194 warning (OPT_Wattributes,
1195 "%qE attribute argument is invalid", atname);
1196 else
1197 warning (OPT_Wattributes,
1198 "%qE attribute argument %i is invalid", atname, argno);
1199
1200 return NULL_TREE;
1201 }
1202
1203 if (!INTEGRAL_TYPE_P (postype))
1204 {
1205 /* Handle this case specially to avoid mentioning the value
1206 of pointer constants in diagnostics. Only mention
1207 the positional argument number when it's non-zero. */
1208 if (argno < 1)
1209 warning (OPT_Wattributes,
1210 "%qE attribute argument has type %qT",
1211 atname, postype);
1212 else
1213 warning (OPT_Wattributes,
1214 "%qE attribute argument %i has type %qT",
1215 atname, argno, postype);
1216
1217 return NULL_TREE;
1218 }
1219
1220 if (TREE_CODE (pos) != INTEGER_CST)
1221 {
1222 /* Only mention the argument number when it's non-zero. */
1223 if (argno < 1)
1224 warning (OPT_Wattributes,
1225 "%qE attribute argument value %qE is not an integer "
1226 "constant",
1227 atname, pos);
1228 else
1229 warning (OPT_Wattributes,
1230 "%qE attribute argument %i value %qE is not an integer "
1231 "constant",
1232 atname, argno, pos);
1233
1234 return NULL_TREE;
1235 }
1236
1237 /* Validate the value of the position argument. If 0-based, then it should
1238 not be negative. If 1-based, it should be greater than zero. */
1239 if ((zero_based && tree_int_cst_sgn (pos) < 0)
1240 || (!zero_based && tree_int_cst_sgn (pos) < 1))
1241 {
1242 if (argno < 1)
1243 warning (OPT_Wattributes,
1244 "%qE attribute argument value %qE does not refer to "
1245 "a function parameter",
1246 atname, pos);
1247 else
1248 warning (OPT_Wattributes,
1249 "%qE attribute argument %i value %qE does not refer to "
1250 "a function parameter",
1251 atname, argno, pos);
1252
1253 return NULL_TREE;
1254 }
1255
1256 /* Adjust the value of pos to be 1-based. */
1257 tree adjusted_pos = (zero_based)
1258 ? int_const_binop (PLUS_EXPR, pos, integer_one_node) : pos;
1259
1260 if (!prototype_p (fntype))
1261 return adjusted_pos;
1262
1263 /* Verify that the argument position does not exceed the number
1264 of formal arguments to the function. */
1265 unsigned nargs = type_num_arguments (fntype);
1266 if (!nargs
1267 || !tree_fits_uhwi_p (adjusted_pos)
1268 || !IN_RANGE (tree_to_uhwi (adjusted_pos), 1, nargs))
1269 {
1270 if (argno < 1)
1271 warning (OPT_Wattributes,
1272 "%qE attribute argument value %qE exceeds the number "
1273 "of function parameters %u",
1274 atname, pos, nargs);
1275 else
1276 warning (OPT_Wattributes,
1277 "%qE attribute argument %i value %qE exceeds the number "
1278 "of function parameters %u",
1279 atname, argno, pos, nargs);
1280
1281 return NULL_TREE;
1282 }
1283
1284 /* Verify that the type of the referenced formal argument matches
1285 the expected type. */
1286 unsigned HOST_WIDE_INT ipos = tree_to_uhwi (adjusted_pos);
1287
1288 /* Zero was handled above. */
1289 gcc_assert (ipos != 0);
1290
1291 if (tree argtype = type_argument_type (fntype, ipos))
1292 {
1293 /* Accept types that match INTEGRAL_TYPE_P except for bool. */
1294 if (!INTEGRAL_TYPE_P (argtype) || TREE_CODE (argtype) == BOOLEAN_TYPE)
1295 {
1296 if (argno < 1)
1297 warning (OPT_Wattributes,
1298 "%qE attribute argument value %qE refers to "
1299 "parameter type %qT",
1300 atname, pos, argtype);
1301 else
1302 warning (OPT_Wattributes,
1303 "%qE attribute argument %i value %qE refers to "
1304 "parameter type %qT",
1305 atname, argno, pos, argtype);
1306
1307 return NULL_TREE;
1308 }
1309
1310 return adjusted_pos;
1311 }
1312
1313 /* Argument position exceeding number of parameters was handled above. */
1314 gcc_unreachable ();
1315 }
1316
1317 /* Handle a "alloc_size" attribute; arguments as in
1318 struct attribute_spec.handler. */
1319
1320 static tree
1321 d_handle_alloc_size_attribute (tree *node, tree name, tree args, int,
1322 bool *no_add_attrs)
1323 {
1324 tree fntype = *node;
1325 tree rettype = TREE_TYPE (fntype);
1326 if (!POINTER_TYPE_P (rettype))
1327 {
1328 warning (OPT_Wattributes,
1329 "%qE attribute ignored on a function returning %qT",
1330 name, rettype);
1331 *no_add_attrs = true;
1332 return NULL_TREE;
1333 }
1334
1335 /* The first argument SIZE_ARG is never null. */
1336 tree size_arg = TREE_VALUE (args);
1337 tree next = TREE_CHAIN (args);
1338
1339 /* NUM_ARG is null when the attribute includes just one argument, or is
1340 explictly set to null if it has been left uninitialized by the caller. */
1341 tree num_arg = NULL_TREE;
1342 if (next != NULL_TREE)
1343 {
1344 if (TREE_VALUE (next) != TYPE_MIN_VALUE (d_int_type))
1345 num_arg = TREE_VALUE (next);
1346
1347 next = TREE_CHAIN (next);
1348 }
1349
1350 /* If ZERO_ARG is set and true, arguments positions are treated as 0-based.
1351 Otherwise the default is 1-based. */
1352 bool zero_based = false;
1353 if (next != NULL_TREE)
1354 zero_based = integer_truep (TREE_VALUE (next));
1355
1356 /* Update the argument values with the real argument position. */
1357 if (tree val = positional_argument (fntype, name, size_arg, num_arg ? 1 : 0,
1358 zero_based))
1359 TREE_VALUE (args) = val;
1360 else
1361 *no_add_attrs = true;
1362
1363 if (num_arg != NULL_TREE)
1364 {
1365 args = TREE_CHAIN (args);
1366 if (tree val = positional_argument (fntype, name, num_arg, 2, zero_based))
1367 TREE_VALUE (args) = val;
1368 else
1369 *no_add_attrs = true;
1370 }
1371
1372 /* Terminate the original TREE_CHAIN in `args' to remove any remaining
1373 D-specific `alloc_size` arguments. */
1374 TREE_CHAIN (args) = NULL_TREE;
1375
1376 return NULL_TREE;
1377 }
1378
1379 /* Handle a "cold" and attribute; arguments as in
1380 struct attribute_spec.handler. */
1381
1382 static tree
1383 d_handle_cold_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1384 {
1385 if (TREE_CODE (*node) != FUNCTION_DECL)
1386 {
1387 warning (OPT_Wattributes, "%qE attribute ignored", name);
1388 *no_add_attrs = true;
1389 }
1390
1391 return NULL_TREE;
1392 }
1393
1394 /* Handle a "no_sanitize" attribute; arguments as in
1395 struct attribute_spec.handler. */
1396
1397 static tree
1398 d_handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
1399 bool *no_add_attrs)
1400 {
1401 *no_add_attrs = true;
1402
1403 if (TREE_CODE (*node) != FUNCTION_DECL)
1404 {
1405 warning (OPT_Wattributes, "%qE attribute ignored", name);
1406 return NULL_TREE;
1407 }
1408
1409 unsigned int flags = 0;
1410 for (; args; args = TREE_CHAIN (args))
1411 {
1412 tree id = TREE_VALUE (args);
1413 if (TREE_CODE (id) != STRING_CST)
1414 {
1415 error ("%qE attribute argument not a string constant", name);
1416 return NULL_TREE;
1417 }
1418
1419 char *string = ASTRDUP (TREE_STRING_POINTER (id));
1420 flags |= parse_no_sanitize_attribute (string);
1421 }
1422
1423 /* Store the flags argument back into no_sanitize attribute as an integer,
1424 merge existing flags if no_sanitize was previously handled. */
1425 if (tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (*node)))
1426 {
1427 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
1428 flags |= old_value;
1429
1430 if (flags != old_value)
1431 TREE_VALUE (attr) = build_int_cst (d_uint_type, flags);
1432 }
1433 else
1434 {
1435 DECL_ATTRIBUTES (*node) = tree_cons (get_identifier ("no_sanitize"),
1436 build_int_cst (d_uint_type, flags),
1437 DECL_ATTRIBUTES (*node));
1438 }
1439
1440 return NULL_TREE;
1441 }
1442
1443 /* Handle a "register" attribute; arguments as in
1444 struct attribute_spec.handler. */
1445
1446 static tree
1447 d_handle_register_attribute (tree *node, tree name, tree args, int,
1448 bool *no_add_attrs)
1449 {
1450 if (!VAR_P (*node))
1451 {
1452 warning (OPT_Wattributes, "%qE attribute ignored", name);
1453 *no_add_attrs = true;
1454 }
1455 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1456 {
1457 error ("%qE attribute argument not a string constant", name);
1458 *no_add_attrs = true;
1459 }
1460 else if (TREE_STRING_LENGTH (TREE_VALUE (args)) == 0
1461 || TREE_STRING_POINTER (TREE_VALUE (args))[0] == '\0')
1462 {
1463 error ("register name not specified for %q+D", *node);
1464 *no_add_attrs = true;
1465 }
1466 else
1467 {
1468 DECL_REGISTER (*node) = 1;
1469 set_user_assembler_name (*node, TREE_STRING_POINTER (TREE_VALUE (args)));
1470 DECL_HARD_REGISTER (*node) = 1;
1471 }
1472
1473 return NULL_TREE;
1474 }
1475
1476 /* Handle a "restrict" attribute; arguments as in
1477 struct attribute_spec.handler. */
1478
1479 static tree
1480 d_handle_restrict_attribute (tree *node, tree name, tree, int,
1481 bool *no_add_attrs)
1482 {
1483 if (TREE_CODE (*node) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (*node)))
1484 {
1485 TREE_TYPE (*node) = build_qualified_type (TREE_TYPE (*node),
1486 TYPE_QUAL_RESTRICT);
1487 }
1488 else
1489 {
1490 warning (OPT_Wattributes, "%qE attribute ignored", name);
1491 *no_add_attrs = true;
1492 }
1493
1494 return NULL_TREE;
1495 }
1496
1497 /* Handle a "simd" attribute; arguments as in
1498 struct attribute_spec.handler. */
1499
1500 static tree
1501 d_handle_simd_attribute (tree *node, tree name, tree args, int,
1502 bool *no_add_attrs)
1503 {
1504 if (TREE_CODE (*node) != FUNCTION_DECL)
1505 {
1506 warning (OPT_Wattributes, "%qE attribute ignored", name);
1507 *no_add_attrs = true;
1508 return NULL_TREE;
1509 }
1510
1511 tree omp_attr = get_identifier ("omp declare simd");
1512 tree omp_flags = NULL_TREE;
1513 if (args)
1514 {
1515 tree id = TREE_VALUE (args);
1516
1517 if (TREE_CODE (id) != STRING_CST)
1518 {
1519 error ("%qE attribute argument not a string constant", name);
1520 *no_add_attrs = true;
1521 return NULL_TREE;
1522 }
1523
1524 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
1525 omp_flags = build_omp_clause (DECL_SOURCE_LOCATION (*node),
1526 OMP_CLAUSE_NOTINBRANCH);
1527 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
1528 omp_flags = build_omp_clause (DECL_SOURCE_LOCATION (*node),
1529 OMP_CLAUSE_INBRANCH);
1530 else
1531 {
1532 error ("only %<inbranch%> and %<notinbranch%> flags are "
1533 "allowed for %<simd%> attribute");
1534 *no_add_attrs = true;
1535 return NULL_TREE;
1536 }
1537 }
1538
1539 DECL_ATTRIBUTES (*node) =
1540 tree_cons (omp_attr, build_tree_list (NULL_TREE, omp_flags),
1541 DECL_ATTRIBUTES (*node));
1542
1543 return NULL_TREE;
1544 }
1545
1546 /* Handle a "used" attribute; arguments as in
1547 struct attribute_spec.handler. */
1548
1549 static tree
1550 d_handle_used_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1551 {
1552 if (TREE_CODE (*node) == FUNCTION_DECL
1553 || (VAR_P (*node) && TREE_STATIC (*node))
1554 || (TREE_CODE (*node) == TYPE_DECL))
1555 {
1556 TREE_USED (*node) = 1;
1557 DECL_PRESERVE_P (*node) = 1;
1558 if (VAR_P (*node))
1559 DECL_READ_P (*node) = 1;
1560 }
1561 else
1562 {
1563 warning (OPT_Wattributes, "%qE attribute ignored", name);
1564 *no_add_attrs = true;
1565 }
1566
1567 return NULL_TREE;
1568 }
1569
1570 /* Handle an "visibility" attribute; arguments as in
1571 struct attribute_spec.handler. */
1572
1573 static tree
1574 d_handle_visibility_attribute (tree *node, tree name, tree args,
1575 int, bool *)
1576 {
1577 /* If this is a type, set the visibility on the type decl. */
1578 tree decl = *node;
1579 if (TYPE_P (decl))
1580 {
1581 decl = TYPE_NAME (decl);
1582 if (decl == NULL_TREE || TREE_CODE (decl) != TYPE_DECL)
1583 {
1584 warning (OPT_Wattributes, "%qE attribute ignored on types", name);
1585 return NULL_TREE;
1586 }
1587 }
1588
1589 if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
1590 {
1591 warning (OPT_Wattributes, "%qE attribute ignored", name);
1592 return NULL_TREE;
1593 }
1594
1595 tree id = TREE_VALUE (args);
1596 if (TREE_CODE (id) != STRING_CST)
1597 {
1598 error ("%qE attribute argument not a string constant", name);
1599 return NULL_TREE;
1600 }
1601
1602 enum symbol_visibility vis;
1603 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
1604 vis = VISIBILITY_DEFAULT;
1605 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
1606 vis = VISIBILITY_INTERNAL;
1607 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
1608 vis = VISIBILITY_HIDDEN;
1609 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
1610 vis = VISIBILITY_PROTECTED;
1611 else
1612 {
1613 error ("attribute %qE argument must be one of %qs, %qs, %qs, or %qs",
1614 name, "default", "hidden", "protected", "internal");
1615 vis = VISIBILITY_DEFAULT;
1616 }
1617
1618 if (DECL_VISIBILITY_SPECIFIED (decl) && vis != DECL_VISIBILITY (decl))
1619 {
1620 tree attributes = (TYPE_P (*node)
1621 ? TYPE_ATTRIBUTES (*node)
1622 : DECL_ATTRIBUTES (decl));
1623 if (lookup_attribute ("visibility", attributes))
1624 error ("%qD redeclared with different visibility", decl);
1625 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1626 && lookup_attribute ("dllimport", attributes))
1627 error ("%qD was declared %qs which implies default visibility",
1628 decl, "export");
1629 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1630 && lookup_attribute ("dllexport", attributes))
1631 error ("%qD was declared %qs which implies default visibility",
1632 decl, "export");
1633 }
1634
1635 DECL_VISIBILITY (decl) = vis;
1636 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1637
1638 /* Go ahead and attach the attribute to the node as well. This is needed
1639 so we can determine whether we have VISIBILITY_DEFAULT because the
1640 visibility was not specified, or because it was explicitly overridden
1641 from the containing scope. */
1642
1643 return NULL_TREE;
1644 }