testsuite: remove SPE tests.
[official-gcc.git] / gcc / d / d-attribs.cc
blobf4086c0f0ee090400a46192eb2f9c0b136cd4bb0
1 /* d-attribs.c -- D attributes handling.
2 Copyright (C) 2015-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
18 /* Implementation of attribute handlers for user defined attributes and
19 internal built-in functions. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
25 #include "dmd/attrib.h"
26 #include "dmd/declaration.h"
27 #include "dmd/mtype.h"
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"
40 #include "d-tree.h"
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 *);
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 *) ;
68 /* Helper to define attribute exclusions. */
69 #define ATTR_EXCL(name, function, type, variable) \
70 { name, function, type, variable }
72 /* Define attributes that are mutually exclusive with one another. */
73 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
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),
82 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
84 ATTR_EXCL ("noreturn", true, true, true),
85 ATTR_EXCL (NULL, false, false, false),
88 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
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)
96 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
98 ATTR_EXCL ("noinline", true, true, true),
99 ATTR_EXCL (NULL, false, false, false),
102 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
104 ATTR_EXCL ("forceinline", true, true, true),
105 ATTR_EXCL (NULL, false, false, false),
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 }
114 /* Table of machine-independent attributes.
115 For internal use (marking of built-ins) only. */
116 const attribute_spec d_langhook_common_attribute_table[] =
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),
147 /* Table of D language attributes exposed by `gcc.attribute' UDAs. */
148 const attribute_spec d_langhook_attribute_table[] =
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),
170 /* Insert the type attribute ATTRNAME with value VALUE into TYPE.
171 Returns a new variant of the original type declaration. */
173 tree
174 insert_type_attribute (tree type, const char *attrname, tree value)
176 tree ident = get_identifier (attrname);
178 if (value)
179 value = tree_cons (NULL_TREE, value, NULL_TREE);
181 tree attribs = merge_attributes (TYPE_ATTRIBUTES (type),
182 tree_cons (ident, value, NULL_TREE));
184 return build_type_attribute_variant (type, attribs);
187 /* Insert the decl attribute ATTRNAME with value VALUE into DECL. */
189 tree
190 insert_decl_attribute (tree decl, const char *attrname, tree value)
192 tree ident = get_identifier (attrname);
194 if (value)
195 value = tree_cons (NULL_TREE, value, NULL_TREE);
197 tree attribs = merge_attributes (DECL_ATTRIBUTES (decl),
198 tree_cons (ident, value, NULL_TREE));
200 return build_decl_attribute_variant (decl, attribs);
203 /* Returns TRUE if NAME is an attribute recognized as being handled by
204 the `gcc.attribute' module. */
206 static bool
207 uda_attribute_p (const char *name)
209 tree ident = get_identifier (name);
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++)
215 if (get_identifier (p->name) == ident)
216 return true;
219 if (targetm.attribute_table)
221 for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
223 if (get_identifier (p->name) == ident)
224 return true;
228 return false;
231 /* [attribute/uda]
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.
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. */
241 static tree
242 build_attributes (Expressions *eattrs)
244 if (!eattrs)
245 return NULL_TREE;
247 expandTuples (eattrs);
249 tree attribs = NULL_TREE;
251 for (size_t i = 0; i < eattrs->length; i++)
253 Expression *attr = (*eattrs)[i];
254 Dsymbol *sym = attr->type->toDsymbol (0);
256 if (!sym)
257 continue;
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;
267 /* Get the result of the attribute if it hasn't already been folded. */
268 if (attr->op == TOKcall)
269 attr = attr->ctfeInterpret ();
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];
277 if (e0->op != TOKstring)
279 error ("expected string attribute, not %qs", e0->toChars ());
280 return error_mark_node;
283 StringExp *se = e0->toStringExp ();
284 gcc_assert (se->sz == 1);
286 /* Empty string attribute, just ignore it. */
287 if (se->len == 0)
288 continue;
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))
295 warning_at (make_location_t (e0->loc), OPT_Wattributes,
296 "unknown attribute %qs", name);
297 return error_mark_node;
300 /* Chain all attribute arguments together. */
301 tree args = NULL_TREE;
303 for (size_t j = 1; j < elems->length; j++)
305 Expression *e = (*elems)[j];
306 StringExp *s = e->isStringExp ();
307 tree t;
308 if (s != NULL && s->sz == 1)
310 const char *string = (const char *)(s->len ? s->string : "");
311 t = build_string (s->len, string);
313 else
314 t = build_expr (e);
316 args = chainon (args, build_tree_list (0, t));
319 tree list = build_tree_list (get_identifier (name), args);
320 attribs = chainon (attribs, list);
323 return attribs;
326 /* If any GCC attributes are found in the declaration SYM, apply them to the
327 type or decl NODE. */
329 void
330 apply_user_attributes (Dsymbol *sym, tree node)
332 if (!sym->userAttribDecl)
334 if (DECL_P (node) && DECL_ATTRIBUTES (node) != NULL)
335 decl_attributes (&node, DECL_ATTRIBUTES (node), 0);
337 return;
340 location_t saved_location = input_location;
341 input_location = make_location_t (sym->loc);
343 Expressions *attrs = sym->userAttribDecl->getAttributes ();
344 decl_attributes (&node, build_attributes (attrs),
345 TYPE_P (node) ? ATTR_FLAG_TYPE_IN_PLACE : 0);
347 input_location = saved_location;
350 /* Built-in attribute handlers.
351 These functions take the arguments:
352 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
354 /* Handle a "noreturn" attribute; arguments as in
355 struct attribute_spec.handler. */
357 static tree
358 handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
360 tree type = TREE_TYPE (*node);
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 ();
373 return NULL_TREE;
376 /* Handle a "leaf" attribute; arguments as in
377 struct attribute_spec.handler. */
379 static tree
380 handle_leaf_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
382 if (TREE_CODE (*node) != FUNCTION_DECL)
384 warning (OPT_Wattributes, "%qE attribute ignored", name);
385 *no_add_attrs = true;
387 if (!TREE_PUBLIC (*node))
389 warning (OPT_Wattributes, "%qE attribute has no effect", name);
390 *no_add_attrs = true;
393 return NULL_TREE;
396 /* Handle a "const" attribute; arguments as in
397 struct attribute_spec.handler. */
399 static tree
400 handle_const_attribute (tree *node, tree, tree, int, bool *)
402 tree type = TREE_TYPE (*node);
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 ();
415 return NULL_TREE;
418 /* Handle a "malloc" attribute; arguments as in
419 struct attribute_spec.handler. */
421 tree
422 handle_malloc_attribute (tree *node, tree, tree, int, bool *)
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;
430 /* Handle a "pure" attribute; arguments as in
431 struct attribute_spec.handler. */
433 static tree
434 handle_pure_attribute (tree *node, tree, tree, int, bool *)
436 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
437 DECL_PURE_P (*node) = 1;
438 return NULL_TREE;
441 /* Handle a "no vops" attribute; arguments as in
442 struct attribute_spec.handler. */
444 static tree
445 handle_novops_attribute (tree *node, tree, tree, int, bool *)
447 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
448 DECL_IS_NOVOPS (*node) = 1;
449 return NULL_TREE;
452 /* Helper for nonnull attribute handling; fetch the operand number
453 from the attribute argument list. */
455 static bool
456 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
458 /* Verify the arg number is a constant. */
459 if (!tree_fits_uhwi_p (arg_num_expr))
460 return false;
462 *valp = TREE_INT_CST_LOW (arg_num_expr);
463 return true;
466 /* Handle the "nonnull" attribute. */
468 static tree
469 handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
471 tree type = *node;
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)
480 gcc_assert (prototype_p (type)
481 || !TYPE_ATTRIBUTES (type)
482 || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
484 return NULL_TREE;
487 /* Argument list specified. Verify that each argument number references
488 a pointer argument. */
489 for (; args; args = TREE_CHAIN (args))
491 tree argument;
492 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
494 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
495 gcc_unreachable ();
497 argument = TYPE_ARG_TYPES (type);
498 if (argument)
500 for (ck_num = 1; ; ck_num++)
502 if (!argument || ck_num == arg_num)
503 break;
504 argument = TREE_CHAIN (argument);
507 gcc_assert (argument
508 && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
512 return NULL_TREE;
515 /* Handle a "nothrow" attribute; arguments as in
516 struct attribute_spec.handler. */
518 static tree
519 handle_nothrow_attribute (tree *node, tree, tree, int, bool *)
521 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
522 TREE_NOTHROW (*node) = 1;
523 return NULL_TREE;
526 /* Handle a "type_generic" attribute. */
528 static tree
529 handle_type_generic_attribute (tree *node, tree, tree, int, bool *)
531 /* Ensure we have a function type. */
532 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
534 /* Ensure we have a variadic function. */
535 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
537 return NULL_TREE;
540 /* Handle a "transaction_pure" attribute. */
542 static tree
543 handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
545 /* Ensure we have a function type. */
546 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
548 return NULL_TREE;
551 /* Handle a "returns_twice" attribute. */
553 static tree
554 handle_returns_twice_attribute (tree *node, tree, tree, int, bool *)
556 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
558 DECL_IS_RETURNS_TWICE (*node) = 1;
560 return NULL_TREE;
563 /* Handle a "fn spec" attribute; arguments as in
564 struct attribute_spec.handler. */
566 tree
567 handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
569 gcc_assert (args
570 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
571 && !TREE_CHAIN (args));
572 return NULL_TREE;
575 /* Handle a "always_inline" attribute; arguments as in
576 struct attribute_spec.handler. */
578 static tree
579 handle_always_inline_attribute (tree *node, tree, tree, int, bool *)
581 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
583 return NULL_TREE;
586 /* Language specific attribute handlers.
587 These functions take the arguments:
588 (tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
590 /* Handle a "noinline" attribute. */
592 static tree
593 d_handle_noinline_attribute (tree *node, tree name, tree, int,
594 bool *no_add_attrs)
596 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
598 if (t->ty == Tfunction)
599 DECL_UNINLINABLE (*node) = 1;
600 else
602 warning (OPT_Wattributes, "%qE attribute ignored", name);
603 *no_add_attrs = true;
606 return NULL_TREE;
609 /* Handle a "forceinline" attribute. */
611 static tree
612 d_handle_forceinline_attribute (tree *node, tree name, tree, int,
613 bool *no_add_attrs)
615 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
617 if (t->ty == Tfunction)
619 tree attributes = DECL_ATTRIBUTES (*node);
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);
626 DECL_DECLARED_INLINE_P (*node) = 1;
627 DECL_NO_INLINE_WARNING_P (*node) = 1;
628 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
630 else
632 warning (OPT_Wattributes, "%qE attribute ignored", name);
633 *no_add_attrs = true;
636 return NULL_TREE;
639 /* Handle a "flatten" attribute. */
641 static tree
642 d_handle_flatten_attribute (tree *node, tree name, tree, int,
643 bool *no_add_attrs)
645 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
647 if (t->ty != Tfunction)
649 warning (OPT_Wattributes, "%qE attribute ignored", name);
650 *no_add_attrs = true;
653 return NULL_TREE;
656 /* Handle a "target" attribute. */
658 static tree
659 d_handle_target_attribute (tree *node, tree name, tree args, int flags,
660 bool *no_add_attrs)
662 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
664 /* Ensure we have a function type. */
665 if (t->ty != Tfunction)
667 warning (OPT_Wattributes, "%qE attribute ignored", name);
668 *no_add_attrs = true;
670 else if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
671 *no_add_attrs = true;
673 return NULL_TREE;
676 /* Handle a "noclone" attribute. */
678 static tree
679 d_handle_noclone_attribute (tree *node, tree name, tree, int,
680 bool *no_add_attrs)
682 Type *t = TYPE_LANG_FRONTEND (TREE_TYPE (*node));
684 if (t->ty == Tfunction)
686 tree attributes = DECL_ATTRIBUTES (*node);
688 /* Push attribute noclone. */
689 if (!lookup_attribute ("noclone", attributes))
690 DECL_ATTRIBUTES (*node) = tree_cons (get_identifier ("noclone"),
691 NULL_TREE, attributes);
693 else
695 warning (OPT_Wattributes, "%qE attribute ignored", name);
696 *no_add_attrs = true;
699 return NULL_TREE;
702 /* Handle a "section" attribute; arguments as in
703 struct attribute_spec.handler. */
705 static tree
706 d_handle_section_attribute (tree *node, tree, tree args, int,
707 bool *no_add_attrs)
709 tree decl = *node;
711 if (targetm_common.have_named_sections)
713 if (VAR_OR_FUNCTION_DECL_P (decl)
714 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
716 if (VAR_P (decl)
717 && current_function_decl != NULL_TREE
718 && !TREE_STATIC (decl))
720 error_at (DECL_SOURCE_LOCATION (decl),
721 "section attribute cannot be specified for "
722 "local variables");
723 *no_add_attrs = true;
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)
732 error ("section of %q+D conflicts with previous declaration",
733 *node);
734 *no_add_attrs = true;
736 else if (VAR_P (decl)
737 && !targetm.have_tls && targetm.emutls.tmpl_section
738 && DECL_THREAD_LOCAL_P (decl))
740 error ("section of %q+D cannot be overridden", *node);
741 *no_add_attrs = true;
743 else
744 set_decl_section_name (decl,
745 TREE_STRING_POINTER (TREE_VALUE (args)));
747 else
749 error ("section attribute not allowed for %q+D", *node);
750 *no_add_attrs = true;
753 else
755 error_at (DECL_SOURCE_LOCATION (*node),
756 "section attributes are not supported for this target");
757 *no_add_attrs = true;
760 return NULL_TREE;
763 /* Handle an "alias" attribute; arguments as in
764 struct attribute_spec.handler. */
766 static tree
767 d_handle_alias_attribute (tree *node, tree name, tree args, int,
768 bool *no_add_attrs)
770 tree decl = *node;
772 if (TREE_CODE (decl) != FUNCTION_DECL
773 && TREE_CODE (decl) != VAR_DECL)
775 warning (OPT_Wattributes, "%qE attribute ignored", name);
776 *no_add_attrs = true;
777 return NULL_TREE;
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)))
787 error ("%q+D defined both normally and as %qE attribute", decl, name);
788 *no_add_attrs = true;
789 return NULL_TREE;
791 else if (decl_function_context (decl))
793 error ("%q+D alias functions must be global", name);
794 *no_add_attrs = true;
795 return NULL_TREE;
797 else
799 tree id;
801 id = TREE_VALUE (args);
802 if (TREE_CODE (id) != STRING_CST)
804 error ("attribute %qE argument not a string", name);
805 *no_add_attrs = true;
806 return NULL_TREE;
808 id = get_identifier (TREE_STRING_POINTER (id));
809 /* This counts as a use of the object pointed to. */
810 TREE_USED (id) = 1;
812 if (TREE_CODE (decl) == FUNCTION_DECL)
813 DECL_INITIAL (decl) = error_mark_node;
814 else
815 TREE_STATIC (decl) = 1;
817 return NULL_TREE;
821 /* Handle a "weak" attribute; arguments as in
822 struct attribute_spec.handler. */
824 static tree
825 d_handle_weak_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
827 if (TREE_CODE (*node) == FUNCTION_DECL
828 && DECL_DECLARED_INLINE_P (*node))
830 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
831 *no_add_attrs = true;
833 else if (VAR_OR_FUNCTION_DECL_P (*node))
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);
840 else
841 warning (OPT_Wattributes, "%qE attribute ignored", name);
843 return NULL_TREE;