]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/d-attribs.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / d / d-attribs.cc
1 /* d-attribs.c -- D attributes handling.
2 Copyright (C) 2015-2021 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/mtype.h"
28
29 #include "tree.h"
30 #include "diagnostic.h"
31 #include "tm.h"
32 #include "cgraph.h"
33 #include "toplev.h"
34 #include "target.h"
35 #include "common/common-target.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "varasm.h"
39
40 #include "d-tree.h"
41
42
43 /* Internal attribute handlers for built-in functions. */
44 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
45 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_always_inline_attribute (tree *, tree, tree, int, bool *);
57
58 /* D attribute handlers for user defined attributes. */
59 static tree d_handle_noinline_attribute (tree *, tree, tree, int, bool *);
60 static tree d_handle_forceinline_attribute (tree *, tree, tree, int, bool *);
61 static tree d_handle_flatten_attribute (tree *, tree, tree, int, bool *);
62 static tree d_handle_target_attribute (tree *, tree, tree, int, bool *);
63 static tree d_handle_noclone_attribute (tree *, tree, tree, int, bool *);
64 static tree d_handle_section_attribute (tree *, tree, tree, int, bool *);
65 static tree d_handle_alias_attribute (tree *, tree, tree, int, bool *);
66 static tree d_handle_weak_attribute (tree *, tree, tree, int, bool *) ;
67
68 /* Helper to define attribute exclusions. */
69 #define ATTR_EXCL(name, function, type, variable) \
70 { name, function, type, variable }
71
72 /* Define attributes that are mutually exclusive with one another. */
73 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
74 {
75 ATTR_EXCL ("const", true, true, true),
76 ATTR_EXCL ("malloc", true, true, true),
77 ATTR_EXCL ("pure", true, true, true),
78 ATTR_EXCL ("returns_twice", true, true, true),
79 ATTR_EXCL (NULL, false, false, false),
80 };
81
82 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
83 {
84 ATTR_EXCL ("noreturn", true, true, true),
85 ATTR_EXCL (NULL, false, false, false),
86 };
87
88 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
89 {
90 ATTR_EXCL ("const", true, true, true),
91 ATTR_EXCL ("noreturn", true, true, true),
92 ATTR_EXCL ("pure", true, true, true),
93 ATTR_EXCL (NULL, false, false, false)
94 };
95
96 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
97 {
98 ATTR_EXCL ("noinline", true, true, true),
99 ATTR_EXCL (NULL, false, false, false),
100 };
101
102 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
103 {
104 ATTR_EXCL ("forceinline", true, true, true),
105 ATTR_EXCL (NULL, false, false, false),
106 };
107
108 /* Helper to define an attribute. */
109 #define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req, \
110 affects_type_identity, handler, exclude) \
111 { name, min_len, max_len, decl_req, type_req, fn_type_req, \
112 affects_type_identity, handler, exclude }
113
114 /* Table of machine-independent attributes.
115 For internal use (marking of built-ins) only. */
116 const attribute_spec d_langhook_common_attribute_table[] =
117 {
118 ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
119 handle_noreturn_attribute, attr_noreturn_exclusions),
120 ATTR_SPEC ("leaf", 0, 0, true, false, false, false,
121 handle_leaf_attribute, NULL),
122 ATTR_SPEC ("const", 0, 0, true, false, false, false,
123 handle_const_attribute, attr_const_pure_exclusions),
124 ATTR_SPEC ("malloc", 0, 0, true, false, false, false,
125 handle_malloc_attribute, NULL),
126 ATTR_SPEC ("returns_twice", 0, 0, true, false, false, false,
127 handle_returns_twice_attribute, attr_returns_twice_exclusions),
128 ATTR_SPEC ("pure", 0, 0, true, false, false, false,
129 handle_pure_attribute, attr_const_pure_exclusions),
130 ATTR_SPEC ("nonnull", 0, -1, false, true, true, false,
131 handle_nonnull_attribute, NULL),
132 ATTR_SPEC ("nothrow", 0, 0, true, false, false, false,
133 handle_nothrow_attribute, NULL),
134 ATTR_SPEC ("transaction_pure", 0, 0, false, true, true, false,
135 handle_transaction_pure_attribute, NULL),
136 ATTR_SPEC ("no vops", 0, 0, true, false, false, false,
137 handle_novops_attribute, NULL),
138 ATTR_SPEC ("type generic", 0, 0, false, true, true, false,
139 handle_type_generic_attribute, NULL),
140 ATTR_SPEC ("fn spec", 1, 1, false, true, true, false,
141 handle_fnspec_attribute, NULL),
142 ATTR_SPEC ("always_inline", 0, 0, true, false, false, false,
143 handle_always_inline_attribute, NULL),
144 ATTR_SPEC (NULL, 0, 0, false, false, false, false, NULL, NULL),
145 };
146
147 /* Table of D language attributes exposed by `gcc.attribute' UDAs. */
148 const attribute_spec d_langhook_attribute_table[] =
149 {
150 ATTR_SPEC ("noinline", 0, 0, true, false, false, false,
151 d_handle_noinline_attribute, attr_noinline_exclusions),
152 ATTR_SPEC ("forceinline", 0, 0, true, false, false, false,
153 d_handle_forceinline_attribute, attr_inline_exclusions),
154 ATTR_SPEC ("flatten", 0, 0, true, false, false, false,
155 d_handle_flatten_attribute, NULL),
156 ATTR_SPEC ("target", 1, -1, true, false, false, false,
157 d_handle_target_attribute, NULL),
158 ATTR_SPEC ("noclone", 0, 0, true, false, false, false,
159 d_handle_noclone_attribute, NULL),
160 ATTR_SPEC ("section", 1, 1, true, false, false, false,
161 d_handle_section_attribute, NULL),
162 ATTR_SPEC ("alias", 1, 1, true, false, false, false,
163 d_handle_alias_attribute, NULL),
164 ATTR_SPEC ("weak", 0, 0, true, false, false, false,
165 d_handle_weak_attribute, NULL),
166 ATTR_SPEC (NULL, 0, 0, false, false, false, false, NULL, NULL),
167 };
168
169
170 /* Insert the type attribute ATTRNAME with value VALUE into TYPE.
171 Returns a new variant of the original type declaration. */
172
173 tree
174 insert_type_attribute (tree type, const char *attrname, tree value)
175 {
176 tree ident = get_identifier (attrname);
177
178 if (value)
179 value = tree_cons (NULL_TREE, value, NULL_TREE);
180
181 tree attribs = merge_attributes (TYPE_ATTRIBUTES (type),
182 tree_cons (ident, value, NULL_TREE));
183
184 return build_type_attribute_variant (type, attribs);
185 }
186
187 /* Insert the decl attribute ATTRNAME with value VALUE into DECL. */
188
189 tree
190 insert_decl_attribute (tree decl, const char *attrname, tree value)
191 {
192 tree ident = get_identifier (attrname);
193
194 if (value)
195 value = tree_cons (NULL_TREE, value, NULL_TREE);
196
197 tree attribs = merge_attributes (DECL_ATTRIBUTES (decl),
198 tree_cons (ident, value, NULL_TREE));
199
200 return build_decl_attribute_variant (decl, attribs);
201 }
202
203 /* Returns TRUE if NAME is an attribute recognized as being handled by
204 the `gcc.attribute' module. */
205
206 static bool
207 uda_attribute_p (const char *name)
208 {
209 tree ident = get_identifier (name);
210
211 /* Search both our language, and target attribute tables.
212 Common and format attributes are kept internal. */
213 for (const attribute_spec *p = d_langhook_attribute_table; p->name; p++)
214 {
215 if (get_identifier (p->name) == ident)
216 return true;
217 }
218
219 if (targetm.attribute_table)
220 {
221 for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
222 {
223 if (get_identifier (p->name) == ident)
224 return true;
225 }
226 }
227
228 return false;
229 }
230
231 /* [attribute/uda]
232
233 User Defined Attributes (UDA) are compile time expressions that can be
234 attached to a declaration. These attributes can then be queried, extracted,
235 and manipulated at compile-time. There is no run-time component to them.
236
237 Expand and merge all UDAs found in the EATTRS list that are of type
238 `gcc.attribute.Attribute'. This symbol is internally recognized by the
239 compiler and maps them to their equivalent GCC attribute. */
240
241 static tree
242 build_attributes (Expressions *eattrs)
243 {
244 if (!eattrs)
245 return NULL_TREE;
246
247 expandTuples (eattrs);
248
249 tree attribs = NULL_TREE;
250
251 for (size_t i = 0; i < eattrs->length; i++)
252 {
253 Expression *attr = (*eattrs)[i];
254 Dsymbol *sym = attr->type->toDsymbol (0);
255
256 if (!sym)
257 continue;
258
259 /* Attribute symbol must come from the `gcc.attribute' module. */
260 Dsymbol *mod = (Dsymbol *) sym->getModule ();
261 if (!(strcmp (mod->toChars (), "attribute") == 0
262 && mod->parent != NULL
263 && strcmp (mod->parent->toChars (), "gcc") == 0
264 && !mod->parent->parent))
265 continue;
266
267 /* Get the result of the attribute if it hasn't already been folded. */
268 if (attr->op == TOKcall)
269 attr = attr->ctfeInterpret ();
270
271 /* Should now have a struct `Attribute("attrib", "value", ...)'
272 initializer list. */
273 gcc_assert (attr->op == TOKstructliteral);
274 Expressions *elems = attr->isStructLiteralExp ()->elements;
275 Expression *e0 = (*elems)[0];
276
277 if (e0->op != TOKstring)
278 {
279 error ("expected string attribute, not %qs", e0->toChars ());
280 return error_mark_node;
281 }
282
283 StringExp *se = e0->toStringExp ();
284 gcc_assert (se->sz == 1);
285
286 /* Empty string attribute, just ignore it. */
287 if (se->len == 0)
288 continue;
289
290 /* Check if the attribute is recognized and handled.
291 Done here to report the diagnostic at the right location. */
292 const char *name = (const char *)(se->len ? se->string : "");
293 if (!uda_attribute_p (name))
294 {
295 warning_at (make_location_t (e0->loc), OPT_Wattributes,
296 "unknown attribute %qs", name);
297 return error_mark_node;
298 }
299
300 /* Chain all attribute arguments together. */
301 tree args = NULL_TREE;
302
303 for (size_t j = 1; j < elems->length; j++)
304 {
305 Expression *e = (*elems)[j];
306 StringExp *s = e->isStringExp ();
307 tree t;
308 if (s != NULL && s->sz == 1)
309 {
310 const char *string = (const char *)(s->len ? s->string : "");
311 t = build_string (s->len, string);
312 }
313 else
314 t = build_expr (e);
315
316 args = chainon (args, build_tree_list (0, t));
317 }
318
319 tree list = build_tree_list (get_identifier (name), args);
320 attribs = chainon (attribs, list);
321 }
322
323 return attribs;
324 }
325
326 /* If any GCC attributes are found in the declaration SYM, apply them to the
327 type or decl NODE. */
328
329 void
330 apply_user_attributes (Dsymbol *sym, tree node)
331 {
332 if (!sym->userAttribDecl)
333 {
334 if (DECL_P (node) && DECL_ATTRIBUTES (node) != NULL)
335 decl_attributes (&node, DECL_ATTRIBUTES (node), 0);
336
337 return;
338 }
339
340 location_t saved_location = input_location;
341 input_location = make_location_t (sym->loc);
342
343 Expressions *attrs = sym->userAttribDecl->getAttributes ();
344 decl_attributes (&node, build_attributes (attrs),
345 TYPE_P (node) ? ATTR_FLAG_TYPE_IN_PLACE : 0);
346
347 input_location = saved_location;
348 }
349
350 /* Built-in attribute handlers.
351 These functions take the arguments:
352 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
353
354 /* Handle a "noreturn" attribute; arguments as in
355 struct attribute_spec.handler. */
356
357 static tree
358 handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
359 {
360 tree type = TREE_TYPE (*node);
361
362 if (TREE_CODE (*node) == FUNCTION_DECL)
363 TREE_THIS_VOLATILE (*node) = 1;
364 else if (TREE_CODE (type) == POINTER_TYPE
365 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
366 TREE_TYPE (*node)
367 = build_pointer_type
368 (build_type_variant (TREE_TYPE (type),
369 TYPE_READONLY (TREE_TYPE (type)), 1));
370 else
371 gcc_unreachable ();
372
373 return NULL_TREE;
374 }
375
376 /* Handle a "leaf" attribute; arguments as in
377 struct attribute_spec.handler. */
378
379 static tree
380 handle_leaf_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
381 {
382 if (TREE_CODE (*node) != FUNCTION_DECL)
383 {
384 warning (OPT_Wattributes, "%qE attribute ignored", name);
385 *no_add_attrs = true;
386 }
387 if (!TREE_PUBLIC (*node))
388 {
389 warning (OPT_Wattributes, "%qE attribute has no effect", name);
390 *no_add_attrs = true;
391 }
392
393 return NULL_TREE;
394 }
395
396 /* Handle a "const" attribute; arguments as in
397 struct attribute_spec.handler. */
398
399 static tree
400 handle_const_attribute (tree *node, tree, tree, int, bool *)
401 {
402 tree type = TREE_TYPE (*node);
403
404 if (TREE_CODE (*node) == FUNCTION_DECL)
405 TREE_READONLY (*node) = 1;
406 else if (TREE_CODE (type) == POINTER_TYPE
407 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
408 TREE_TYPE (*node)
409 = build_pointer_type
410 (build_type_variant (TREE_TYPE (type), 1,
411 TREE_THIS_VOLATILE (TREE_TYPE (type))));
412 else
413 gcc_unreachable ();
414
415 return NULL_TREE;
416 }
417
418 /* Handle a "malloc" attribute; arguments as in
419 struct attribute_spec.handler. */
420
421 tree
422 handle_malloc_attribute (tree *node, tree, tree, int, bool *)
423 {
424 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
425 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))));
426 DECL_IS_MALLOC (*node) = 1;
427 return NULL_TREE;
428 }
429
430 /* Handle a "pure" attribute; arguments as in
431 struct attribute_spec.handler. */
432
433 static tree
434 handle_pure_attribute (tree *node, tree, tree, int, bool *)
435 {
436 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
437 DECL_PURE_P (*node) = 1;
438 return NULL_TREE;
439 }
440
441 /* Handle a "no vops" attribute; arguments as in
442 struct attribute_spec.handler. */
443
444 static tree
445 handle_novops_attribute (tree *node, tree, tree, int, bool *)
446 {
447 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
448 DECL_IS_NOVOPS (*node) = 1;
449 return NULL_TREE;
450 }
451
452 /* Helper for nonnull attribute handling; fetch the operand number
453 from the attribute argument list. */
454
455 static bool
456 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
457 {
458 /* Verify the arg number is a constant. */
459 if (!tree_fits_uhwi_p (arg_num_expr))
460 return false;
461
462 *valp = TREE_INT_CST_LOW (arg_num_expr);
463 return true;
464 }
465
466 /* Handle the "nonnull" attribute. */
467
468 static tree
469 handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
470 {
471 tree type = *node;
472
473 /* If no arguments are specified, all pointer arguments should be
474 non-null. Verify a full prototype is given so that the arguments
475 will have the correct types when we actually check them later.
476 Avoid diagnosing type-generic built-ins since those have no
477 prototype. */
478 if (!args)
479 {
480 gcc_assert (prototype_p (type)
481 || !TYPE_ATTRIBUTES (type)
482 || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
483
484 return NULL_TREE;
485 }
486
487 /* Argument list specified. Verify that each argument number references
488 a pointer argument. */
489 for (; args; args = TREE_CHAIN (args))
490 {
491 tree argument;
492 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
493
494 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
495 gcc_unreachable ();
496
497 argument = TYPE_ARG_TYPES (type);
498 if (argument)
499 {
500 for (ck_num = 1; ; ck_num++)
501 {
502 if (!argument || ck_num == arg_num)
503 break;
504 argument = TREE_CHAIN (argument);
505 }
506
507 gcc_assert (argument
508 && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
509 }
510 }
511
512 return NULL_TREE;
513 }
514
515 /* Handle a "nothrow" attribute; arguments as in
516 struct attribute_spec.handler. */
517
518 static tree
519 handle_nothrow_attribute (tree *node, tree, tree, int, bool *)
520 {
521 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
522 TREE_NOTHROW (*node) = 1;
523 return NULL_TREE;
524 }
525
526 /* Handle a "type_generic" attribute. */
527
528 static tree
529 handle_type_generic_attribute (tree *node, tree, tree, int, bool *)
530 {
531 /* Ensure we have a function type. */
532 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
533
534 /* Ensure we have a variadic function. */
535 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
536
537 return NULL_TREE;
538 }
539
540 /* Handle a "transaction_pure" attribute. */
541
542 static tree
543 handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
544 {
545 /* Ensure we have a function type. */
546 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
547
548 return NULL_TREE;
549 }
550
551 /* Handle a "returns_twice" attribute. */
552
553 static tree
554 handle_returns_twice_attribute (tree *node, tree, tree, int, bool *)
555 {
556 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
557
558 DECL_IS_RETURNS_TWICE (*node) = 1;
559
560 return NULL_TREE;
561 }
562
563 /* Handle a "fn spec" attribute; arguments as in
564 struct attribute_spec.handler. */
565
566 tree
567 handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
568 {
569 gcc_assert (args
570 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
571 && !TREE_CHAIN (args));
572 return NULL_TREE;
573 }
574
575 /* Handle a "always_inline" attribute; arguments as in
576 struct attribute_spec.handler. */
577
578 static tree
579 handle_always_inline_attribute (tree *node, tree, tree, int, bool *)
580 {
581 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
582
583 return NULL_TREE;
584 }
585
586 /* Language specific attribute handlers.
587 These functions take the arguments:
588 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
589
590 /* Handle a "noinline" attribute. */
591
592 static tree
593 d_handle_noinline_attribute (tree *node, tree name, tree, int,
594 bool *no_add_attrs)
595 {
596 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
597
598 if (t->ty == Tfunction)
599 DECL_UNINLINABLE (*node) = 1;
600 else
601 {
602 warning (OPT_Wattributes, "%qE attribute ignored", name);
603 *no_add_attrs = true;
604 }
605
606 return NULL_TREE;
607 }
608
609 /* Handle a "forceinline" attribute. */
610
611 static tree
612 d_handle_forceinline_attribute (tree *node, tree name, tree, int,
613 bool *no_add_attrs)
614 {
615 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
616
617 if (t->ty == Tfunction)
618 {
619 tree attributes = DECL_ATTRIBUTES (*node);
620
621 /* Push attribute always_inline. */
622 if (!lookup_attribute ("always_inline", attributes))
623 DECL_ATTRIBUTES (*node) = tree_cons (get_identifier ("always_inline"),
624 NULL_TREE, attributes);
625
626 DECL_DECLARED_INLINE_P (*node) = 1;
627 DECL_NO_INLINE_WARNING_P (*node) = 1;
628 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
629 }
630 else
631 {
632 warning (OPT_Wattributes, "%qE attribute ignored", name);
633 *no_add_attrs = true;
634 }
635
636 return NULL_TREE;
637 }
638
639 /* Handle a "flatten" attribute. */
640
641 static tree
642 d_handle_flatten_attribute (tree *node, tree name, tree, int,
643 bool *no_add_attrs)
644 {
645 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
646
647 if (t->ty != Tfunction)
648 {
649 warning (OPT_Wattributes, "%qE attribute ignored", name);
650 *no_add_attrs = true;
651 }
652
653 return NULL_TREE;
654 }
655
656 /* Handle a "target" attribute. */
657
658 static tree
659 d_handle_target_attribute (tree *node, tree name, tree args, int flags,
660 bool *no_add_attrs)
661 {
662 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
663
664 /* Ensure we have a function type. */
665 if (t->ty != Tfunction)
666 {
667 warning (OPT_Wattributes, "%qE attribute ignored", name);
668 *no_add_attrs = true;
669 }
670 else if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
671 *no_add_attrs = true;
672
673 return NULL_TREE;
674 }
675
676 /* Handle a "noclone" attribute. */
677
678 static tree
679 d_handle_noclone_attribute (tree *node, tree name, tree, int,
680 bool *no_add_attrs)
681 {
682 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
683
684 if (t->ty == Tfunction)
685 {
686 tree attributes = DECL_ATTRIBUTES (*node);
687
688 /* Push attribute noclone. */
689 if (!lookup_attribute ("noclone", attributes))
690 DECL_ATTRIBUTES (*node) = tree_cons (get_identifier ("noclone"),
691 NULL_TREE, attributes);
692 }
693 else
694 {
695 warning (OPT_Wattributes, "%qE attribute ignored", name);
696 *no_add_attrs = true;
697 }
698
699 return NULL_TREE;
700 }
701
702 /* Handle a "section" attribute; arguments as in
703 struct attribute_spec.handler. */
704
705 static tree
706 d_handle_section_attribute (tree *node, tree, tree args, int,
707 bool *no_add_attrs)
708 {
709 tree decl = *node;
710
711 if (targetm_common.have_named_sections)
712 {
713 if (VAR_OR_FUNCTION_DECL_P (decl)
714 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
715 {
716 if (VAR_P (decl)
717 && current_function_decl != NULL_TREE
718 && !TREE_STATIC (decl))
719 {
720 error_at (DECL_SOURCE_LOCATION (decl),
721 "section attribute cannot be specified for "
722 "local variables");
723 *no_add_attrs = true;
724 }
725
726 /* The decl may have already been given a section attribute
727 from a previous declaration. Ensure they match. */
728 else if (DECL_SECTION_NAME (decl) != NULL
729 && strcmp (DECL_SECTION_NAME (decl),
730 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
731 {
732 error ("section of %q+D conflicts with previous declaration",
733 *node);
734 *no_add_attrs = true;
735 }
736 else if (VAR_P (decl)
737 && !targetm.have_tls && targetm.emutls.tmpl_section
738 && DECL_THREAD_LOCAL_P (decl))
739 {
740 error ("section of %q+D cannot be overridden", *node);
741 *no_add_attrs = true;
742 }
743 else
744 set_decl_section_name (decl,
745 TREE_STRING_POINTER (TREE_VALUE (args)));
746 }
747 else
748 {
749 error ("section attribute not allowed for %q+D", *node);
750 *no_add_attrs = true;
751 }
752 }
753 else
754 {
755 error_at (DECL_SOURCE_LOCATION (*node),
756 "section attributes are not supported for this target");
757 *no_add_attrs = true;
758 }
759
760 return NULL_TREE;
761 }
762
763 /* Handle an "alias" attribute; arguments as in
764 struct attribute_spec.handler. */
765
766 static tree
767 d_handle_alias_attribute (tree *node, tree name, tree args, int,
768 bool *no_add_attrs)
769 {
770 tree decl = *node;
771
772 if (TREE_CODE (decl) != FUNCTION_DECL
773 && TREE_CODE (decl) != VAR_DECL)
774 {
775 warning (OPT_Wattributes, "%qE attribute ignored", name);
776 *no_add_attrs = true;
777 return NULL_TREE;
778 }
779 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
780 || (TREE_CODE (decl) != FUNCTION_DECL
781 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
782 /* A static variable declaration is always a tentative definition,
783 but the alias is a non-tentative definition which overrides. */
784 || (TREE_CODE (decl) != FUNCTION_DECL
785 && !TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
786 {
787 error ("%q+D defined both normally and as %qE attribute", decl, name);
788 *no_add_attrs = true;
789 return NULL_TREE;
790 }
791 else if (decl_function_context (decl))
792 {
793 error ("%q+D alias functions must be global", name);
794 *no_add_attrs = true;
795 return NULL_TREE;
796 }
797 else
798 {
799 tree id;
800
801 id = TREE_VALUE (args);
802 if (TREE_CODE (id) != STRING_CST)
803 {
804 error ("attribute %qE argument not a string", name);
805 *no_add_attrs = true;
806 return NULL_TREE;
807 }
808 id = get_identifier (TREE_STRING_POINTER (id));
809 /* This counts as a use of the object pointed to. */
810 TREE_USED (id) = 1;
811
812 if (TREE_CODE (decl) == FUNCTION_DECL)
813 DECL_INITIAL (decl) = error_mark_node;
814 else
815 TREE_STATIC (decl) = 1;
816
817 return NULL_TREE;
818 }
819 }
820
821 /* Handle a "weak" attribute; arguments as in
822 struct attribute_spec.handler. */
823
824 static tree
825 d_handle_weak_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
826 {
827 if (TREE_CODE (*node) == FUNCTION_DECL
828 && DECL_DECLARED_INLINE_P (*node))
829 {
830 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
831 *no_add_attrs = true;
832 }
833 else if (VAR_OR_FUNCTION_DECL_P (*node))
834 {
835 struct symtab_node *n = symtab_node::get (*node);
836 if (n && n->refuse_visibility_changes)
837 error ("%q+D declared weak after being used", *node);
838 declare_weak (*node);
839 }
840 else
841 warning (OPT_Wattributes, "%qE attribute ignored", name);
842
843 return NULL_TREE;
844 }
845