1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 static void init_attributes PARAMS ((void));
38 /* Table of the tables of attributes (common, format, language, machine)
40 static const struct attribute_spec *attribute_tables[4];
42 static bool attributes_initialized = false;
44 static tree handle_packed_attribute PARAMS ((tree *, tree, tree, int,
46 static tree handle_nocommon_attribute PARAMS ((tree *, tree, tree, int,
48 static tree handle_common_attribute PARAMS ((tree *, tree, tree, int,
50 static tree handle_noreturn_attribute PARAMS ((tree *, tree, tree, int,
52 static tree handle_noinline_attribute PARAMS ((tree *, tree, tree, int,
54 static tree handle_always_inline_attribute PARAMS ((tree *, tree, tree, int,
56 static tree handle_used_attribute PARAMS ((tree *, tree, tree, int,
58 static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
60 static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
62 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
64 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
66 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
68 static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
70 static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
72 static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
74 static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
76 static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
78 static tree handle_visibility_attribute PARAMS ((tree *, tree, tree, int,
80 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
83 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
85 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
87 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
89 static tree handle_deprecated_attribute PARAMS ((tree *, tree, tree, int,
91 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
93 static tree vector_size_helper PARAMS ((tree, tree));
95 /* Table of machine-independent attributes common to all C-like languages. */
96 static const struct attribute_spec c_common_attribute_table[] =
98 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
99 { "packed", 0, 0, false, false, false,
100 handle_packed_attribute },
101 { "nocommon", 0, 0, true, false, false,
102 handle_nocommon_attribute },
103 { "common", 0, 0, true, false, false,
104 handle_common_attribute },
105 /* FIXME: logically, noreturn attributes should be listed as
106 "false, true, true" and apply to function types. But implementing this
107 would require all the places in the compiler that use TREE_THIS_VOLATILE
108 on a decl to identify non-returning functions to be located and fixed
109 to check the function type instead. */
110 { "noreturn", 0, 0, true, false, false,
111 handle_noreturn_attribute },
112 { "volatile", 0, 0, true, false, false,
113 handle_noreturn_attribute },
114 { "noinline", 0, 0, true, false, false,
115 handle_noinline_attribute },
116 { "always_inline", 0, 0, true, false, false,
117 handle_always_inline_attribute },
118 { "used", 0, 0, true, false, false,
119 handle_used_attribute },
120 { "unused", 0, 0, false, false, false,
121 handle_unused_attribute },
122 /* The same comments as for noreturn attributes apply to const ones. */
123 { "const", 0, 0, true, false, false,
124 handle_const_attribute },
125 { "transparent_union", 0, 0, false, false, false,
126 handle_transparent_union_attribute },
127 { "constructor", 0, 0, true, false, false,
128 handle_constructor_attribute },
129 { "destructor", 0, 0, true, false, false,
130 handle_destructor_attribute },
131 { "mode", 1, 1, false, true, false,
132 handle_mode_attribute },
133 { "section", 1, 1, true, false, false,
134 handle_section_attribute },
135 { "aligned", 0, 1, false, false, false,
136 handle_aligned_attribute },
137 { "weak", 0, 0, true, false, false,
138 handle_weak_attribute },
139 { "alias", 1, 1, true, false, false,
140 handle_alias_attribute },
141 { "no_instrument_function", 0, 0, true, false, false,
142 handle_no_instrument_function_attribute },
143 { "malloc", 0, 0, true, false, false,
144 handle_malloc_attribute },
145 { "no_stack_limit", 0, 0, true, false, false,
146 handle_no_limit_stack_attribute },
147 { "pure", 0, 0, true, false, false,
148 handle_pure_attribute },
149 { "deprecated", 0, 0, false, false, false,
150 handle_deprecated_attribute },
151 { "vector_size", 1, 1, false, true, false,
152 handle_vector_size_attribute },
153 { "visibility", 1, 1, true, false, false,
154 handle_visibility_attribute },
155 { NULL, 0, 0, false, false, false, NULL }
158 /* Default empty table of attributes. */
159 static const struct attribute_spec empty_attribute_table[] =
161 { NULL, 0, 0, false, false, false, NULL }
164 /* Table of machine-independent attributes for checking formats, if used. */
165 const struct attribute_spec *format_attribute_table = empty_attribute_table;
167 /* Table of machine-independent attributes for a particular language. */
168 const struct attribute_spec *lang_attribute_table = empty_attribute_table;
170 /* Flag saying whether common language attributes are to be supported. */
171 int lang_attribute_common = 1;
173 /* Initialize attribute tables, and make some sanity checks
174 if --enable-checking. */
179 #ifdef ENABLE_CHECKING
184 = lang_attribute_common ? c_common_attribute_table : empty_attribute_table;
185 attribute_tables[1] = lang_attribute_table;
186 attribute_tables[2] = format_attribute_table;
187 attribute_tables[3] = targetm.attribute_table;
189 #ifdef ENABLE_CHECKING
190 /* Make some sanity checks on the attribute tables. */
191 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
195 for (j = 0; attribute_tables[i][j].name != NULL; j++)
197 /* The name must not begin and end with __. */
198 const char *name = attribute_tables[i][j].name;
199 int len = strlen (name);
200 if (name[0] == '_' && name[1] == '_'
201 && name[len - 1] == '_' && name[len - 2] == '_')
203 /* The minimum and maximum lengths must be consistent. */
204 if (attribute_tables[i][j].min_length < 0)
206 if (attribute_tables[i][j].max_length != -1
207 && (attribute_tables[i][j].max_length
208 < attribute_tables[i][j].min_length))
210 /* An attribute cannot require both a DECL and a TYPE. */
211 if (attribute_tables[i][j].decl_required
212 && attribute_tables[i][j].type_required)
214 /* If an attribute requires a function type, in particular
215 it requires a type. */
216 if (attribute_tables[i][j].function_type_required
217 && !attribute_tables[i][j].type_required)
222 /* Check that each name occurs just once in each table. */
223 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
226 for (j = 0; attribute_tables[i][j].name != NULL; j++)
227 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
228 if (!strcmp (attribute_tables[i][j].name,
229 attribute_tables[i][k].name))
232 /* Check that no name occurs in more than one table. */
233 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
237 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
238 for (k = 0; attribute_tables[i][k].name != NULL; k++)
239 for (l = 0; attribute_tables[j][l].name != NULL; l++)
240 if (!strcmp (attribute_tables[i][k].name,
241 attribute_tables[j][l].name))
246 attributes_initialized = true;
249 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
250 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
251 it should be modified in place; if a TYPE, a copy should be created
252 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
253 information, in the form of a bitwise OR of flags in enum attribute_flags
254 from tree.h. Depending on these flags, some attributes may be
255 returned to be applied at a later stage (for example, to apply
256 a decl attribute to the declaration rather than to its type). If
257 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
258 whether there might be some default attributes to apply to this DECL;
259 if so, decl_attributes will be called recursively with those attributes
260 and ATTR_FLAG_BUILT_IN set. */
263 decl_attributes (node, attributes, flags)
264 tree *node, attributes;
268 tree returned_attrs = NULL_TREE;
270 if (!attributes_initialized)
273 (*targetm.insert_attributes) (*node, &attributes);
275 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
276 && !(flags & (int) ATTR_FLAG_BUILT_IN))
277 insert_default_attributes (*node);
279 for (a = attributes; a; a = TREE_CHAIN (a))
281 tree name = TREE_PURPOSE (a);
282 tree args = TREE_VALUE (a);
284 const struct attribute_spec *spec = NULL;
285 bool no_add_attrs = 0;
288 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
292 for (j = 0; attribute_tables[i][j].name != NULL; j++)
294 if (is_attribute_p (attribute_tables[i][j].name, name))
296 spec = &attribute_tables[i][j];
306 warning ("`%s' attribute directive ignored",
307 IDENTIFIER_POINTER (name));
310 else if (list_length (args) < spec->min_length
311 || (spec->max_length >= 0
312 && list_length (args) > spec->max_length))
314 error ("wrong number of arguments specified for `%s' attribute",
315 IDENTIFIER_POINTER (name));
319 if (spec->decl_required && !DECL_P (*anode))
321 if (flags & ((int) ATTR_FLAG_DECL_NEXT
322 | (int) ATTR_FLAG_FUNCTION_NEXT
323 | (int) ATTR_FLAG_ARRAY_NEXT))
325 /* Pass on this attribute to be tried again. */
326 returned_attrs = tree_cons (name, args, returned_attrs);
331 warning ("`%s' attribute does not apply to types",
332 IDENTIFIER_POINTER (name));
337 /* If we require a type, but were passed a decl, set up to make a
338 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
339 would have applied if we'd been passed a type, but we cannot modify
340 the decl's type in place here. */
341 if (spec->type_required && DECL_P (*anode))
343 anode = &TREE_TYPE (*anode);
344 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
347 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
348 && TREE_CODE (*anode) != METHOD_TYPE)
350 if (TREE_CODE (*anode) == POINTER_TYPE
351 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
352 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
354 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
355 *anode = build_type_copy (*anode);
356 anode = &TREE_TYPE (*anode);
358 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
360 /* Pass on this attribute to be tried again. */
361 returned_attrs = tree_cons (name, args, returned_attrs);
365 if (TREE_CODE (*anode) != FUNCTION_TYPE
366 && TREE_CODE (*anode) != METHOD_TYPE)
368 warning ("`%s' attribute only applies to function types",
369 IDENTIFIER_POINTER (name));
374 if (spec->handler != NULL)
375 returned_attrs = chainon ((*spec->handler) (anode, name, args,
376 flags, &no_add_attrs),
379 /* Layout the decl in case anything changed. */
380 if (spec->type_required && DECL_P (*node)
381 && (TREE_CODE (*node) == VAR_DECL
382 || TREE_CODE (*node) == PARM_DECL
383 || TREE_CODE (*node) == RESULT_DECL))
385 /* Force a recalculation of mode and size. */
386 DECL_MODE (*node) = VOIDmode;
387 DECL_SIZE (*node) = 0;
389 layout_decl (*node, 0);
398 old_attrs = DECL_ATTRIBUTES (*anode);
400 old_attrs = TYPE_ATTRIBUTES (*anode);
402 for (a = lookup_attribute (spec->name, old_attrs);
404 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
406 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
412 /* This attribute isn't already in the list. */
414 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
415 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
416 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
418 *anode = build_type_attribute_variant (*anode,
419 tree_cons (name, args,
425 return returned_attrs;
428 /* Handle a "packed" attribute; arguments as in
429 struct attribute_spec.handler. */
432 handle_packed_attribute (node, name, args, flags, no_add_attrs)
435 tree args ATTRIBUTE_UNUSED;
442 if (TREE_CODE (*node) == TYPE_DECL)
443 type = &TREE_TYPE (*node);
450 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
451 *type = build_type_copy (*type);
452 TYPE_PACKED (*type) = 1;
454 else if (TREE_CODE (*node) == FIELD_DECL)
455 DECL_PACKED (*node) = 1;
456 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
457 used for DECL_REGISTER. It wouldn't mean anything anyway. */
460 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
461 *no_add_attrs = true;
467 /* Handle a "nocommon" attribute; arguments as in
468 struct attribute_spec.handler. */
471 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
474 tree args ATTRIBUTE_UNUSED;
475 int flags ATTRIBUTE_UNUSED;
478 if (TREE_CODE (*node) == VAR_DECL)
479 DECL_COMMON (*node) = 0;
482 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
483 *no_add_attrs = true;
489 /* Handle a "common" attribute; arguments as in
490 struct attribute_spec.handler. */
493 handle_common_attribute (node, name, args, flags, no_add_attrs)
496 tree args ATTRIBUTE_UNUSED;
497 int flags ATTRIBUTE_UNUSED;
500 if (TREE_CODE (*node) == VAR_DECL)
501 DECL_COMMON (*node) = 1;
504 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
505 *no_add_attrs = true;
511 /* Handle a "noreturn" attribute; arguments as in
512 struct attribute_spec.handler. */
515 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
518 tree args ATTRIBUTE_UNUSED;
519 int flags ATTRIBUTE_UNUSED;
522 tree type = TREE_TYPE (*node);
524 /* See FIXME comment in c_common_attribute_table. */
525 if (TREE_CODE (*node) == FUNCTION_DECL)
526 TREE_THIS_VOLATILE (*node) = 1;
527 else if (TREE_CODE (type) == POINTER_TYPE
528 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
531 (build_type_variant (TREE_TYPE (type),
532 TREE_READONLY (TREE_TYPE (type)), 1));
535 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
536 *no_add_attrs = true;
542 /* Handle a "noinline" attribute; arguments as in
543 struct attribute_spec.handler. */
546 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
549 tree args ATTRIBUTE_UNUSED;
550 int flags ATTRIBUTE_UNUSED;
553 if (TREE_CODE (*node) == FUNCTION_DECL)
554 DECL_UNINLINABLE (*node) = 1;
557 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
558 *no_add_attrs = true;
564 /* Handle a "always_inline" attribute; arguments as in
565 struct attribute_spec.handler. */
568 handle_always_inline_attribute (node, name, args, flags, no_add_attrs)
571 tree args ATTRIBUTE_UNUSED;
572 int flags ATTRIBUTE_UNUSED;
575 if (TREE_CODE (*node) == FUNCTION_DECL)
577 /* Do nothing else, just set the attribute. We'll get at
578 it later with lookup_attribute. */
582 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
583 *no_add_attrs = true;
589 /* Handle a "used" attribute; arguments as in
590 struct attribute_spec.handler. */
593 handle_used_attribute (node, name, args, flags, no_add_attrs)
596 tree args ATTRIBUTE_UNUSED;
597 int flags ATTRIBUTE_UNUSED;
600 if (TREE_CODE (*node) == FUNCTION_DECL)
601 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
602 = TREE_USED (*node) = 1;
605 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
606 *no_add_attrs = true;
612 /* Handle a "unused" attribute; arguments as in
613 struct attribute_spec.handler. */
616 handle_unused_attribute (node, name, args, flags, no_add_attrs)
619 tree args ATTRIBUTE_UNUSED;
627 if (TREE_CODE (decl) == PARM_DECL
628 || TREE_CODE (decl) == VAR_DECL
629 || TREE_CODE (decl) == FUNCTION_DECL
630 || TREE_CODE (decl) == LABEL_DECL
631 || TREE_CODE (decl) == TYPE_DECL)
632 TREE_USED (decl) = 1;
635 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
636 *no_add_attrs = true;
641 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
642 *node = build_type_copy (*node);
643 TREE_USED (*node) = 1;
649 /* Handle a "const" attribute; arguments as in
650 struct attribute_spec.handler. */
653 handle_const_attribute (node, name, args, flags, no_add_attrs)
656 tree args ATTRIBUTE_UNUSED;
657 int flags ATTRIBUTE_UNUSED;
660 tree type = TREE_TYPE (*node);
662 /* See FIXME comment on noreturn in c_common_attribute_table. */
663 if (TREE_CODE (*node) == FUNCTION_DECL)
664 TREE_READONLY (*node) = 1;
665 else if (TREE_CODE (type) == POINTER_TYPE
666 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
669 (build_type_variant (TREE_TYPE (type), 1,
670 TREE_THIS_VOLATILE (TREE_TYPE (type))));
673 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
674 *no_add_attrs = true;
680 /* Handle a "transparent_union" attribute; arguments as in
681 struct attribute_spec.handler. */
684 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
687 tree args ATTRIBUTE_UNUSED;
691 tree decl = NULL_TREE;
698 type = &TREE_TYPE (decl);
699 is_type = TREE_CODE (*node) == TYPE_DECL;
701 else if (TYPE_P (*node))
702 type = node, is_type = 1;
705 && TREE_CODE (*type) == UNION_TYPE
707 || (TYPE_FIELDS (*type) != 0
708 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
710 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
711 *type = build_type_copy (*type);
712 TYPE_TRANSPARENT_UNION (*type) = 1;
714 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
715 && TREE_CODE (*type) == UNION_TYPE
716 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
717 DECL_TRANSPARENT_UNION (decl) = 1;
720 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
721 *no_add_attrs = true;
727 /* Handle a "constructor" attribute; arguments as in
728 struct attribute_spec.handler. */
731 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
734 tree args ATTRIBUTE_UNUSED;
735 int flags ATTRIBUTE_UNUSED;
739 tree type = TREE_TYPE (decl);
741 if (TREE_CODE (decl) == FUNCTION_DECL
742 && TREE_CODE (type) == FUNCTION_TYPE
743 && decl_function_context (decl) == 0)
745 DECL_STATIC_CONSTRUCTOR (decl) = 1;
746 TREE_USED (decl) = 1;
750 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
751 *no_add_attrs = true;
757 /* Handle a "destructor" attribute; arguments as in
758 struct attribute_spec.handler. */
761 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
764 tree args ATTRIBUTE_UNUSED;
765 int flags ATTRIBUTE_UNUSED;
769 tree type = TREE_TYPE (decl);
771 if (TREE_CODE (decl) == FUNCTION_DECL
772 && TREE_CODE (type) == FUNCTION_TYPE
773 && decl_function_context (decl) == 0)
775 DECL_STATIC_DESTRUCTOR (decl) = 1;
776 TREE_USED (decl) = 1;
780 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
781 *no_add_attrs = true;
787 /* Handle a "mode" attribute; arguments as in
788 struct attribute_spec.handler. */
791 handle_mode_attribute (node, name, args, flags, no_add_attrs)
795 int flags ATTRIBUTE_UNUSED;
800 *no_add_attrs = true;
802 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
803 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
807 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
808 int len = strlen (p);
809 enum machine_mode mode = VOIDmode;
812 if (len > 4 && p[0] == '_' && p[1] == '_'
813 && p[len - 1] == '_' && p[len - 2] == '_')
815 char *newp = (char *) alloca (len - 1);
817 strcpy (newp, &p[2]);
818 newp[len - 4] = '\0';
822 /* Change this type to have a type with the specified mode.
823 First check for the special modes. */
824 if (! strcmp (p, "byte"))
826 else if (!strcmp (p, "word"))
828 else if (! strcmp (p, "pointer"))
831 for (j = 0; j < NUM_MACHINE_MODES; j++)
832 if (!strcmp (p, GET_MODE_NAME (j)))
833 mode = (enum machine_mode) j;
835 if (mode == VOIDmode)
836 error ("unknown machine mode `%s'", p);
837 else if (0 == (typefm = type_for_mode (mode,
838 TREE_UNSIGNED (type))))
839 error ("no data type for mode `%s'", p);
842 /* No need to layout the type here. The caller should do this. */
848 /* Handle a "section" attribute; arguments as in
849 struct attribute_spec.handler. */
852 handle_section_attribute (node, name, args, flags, no_add_attrs)
854 tree name ATTRIBUTE_UNUSED;
856 int flags ATTRIBUTE_UNUSED;
861 if (targetm.have_named_sections)
863 if ((TREE_CODE (decl) == FUNCTION_DECL
864 || TREE_CODE (decl) == VAR_DECL)
865 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
867 if (TREE_CODE (decl) == VAR_DECL
868 && current_function_decl != NULL_TREE
869 && ! TREE_STATIC (decl))
871 error_with_decl (decl,
872 "section attribute cannot be specified for local variables");
873 *no_add_attrs = true;
876 /* The decl may have already been given a section attribute
877 from a previous declaration. Ensure they match. */
878 else if (DECL_SECTION_NAME (decl) != NULL_TREE
879 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
880 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
882 error_with_decl (*node,
883 "section of `%s' conflicts with previous declaration");
884 *no_add_attrs = true;
887 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
891 error_with_decl (*node,
892 "section attribute not allowed for `%s'");
893 *no_add_attrs = true;
898 error_with_decl (*node,
899 "section attributes are not supported for this target");
900 *no_add_attrs = true;
906 /* Handle a "aligned" attribute; arguments as in
907 struct attribute_spec.handler. */
910 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
912 tree name ATTRIBUTE_UNUSED;
917 tree decl = NULL_TREE;
920 tree align_expr = (args ? TREE_VALUE (args)
921 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
927 type = &TREE_TYPE (decl);
928 is_type = TREE_CODE (*node) == TYPE_DECL;
930 else if (TYPE_P (*node))
931 type = node, is_type = 1;
933 /* Strip any NOPs of any kind. */
934 while (TREE_CODE (align_expr) == NOP_EXPR
935 || TREE_CODE (align_expr) == CONVERT_EXPR
936 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
937 align_expr = TREE_OPERAND (align_expr, 0);
939 if (TREE_CODE (align_expr) != INTEGER_CST)
941 error ("requested alignment is not a constant");
942 *no_add_attrs = true;
944 else if ((i = tree_log2 (align_expr)) == -1)
946 error ("requested alignment is not a power of 2");
947 *no_add_attrs = true;
949 else if (i > HOST_BITS_PER_INT - 2)
951 error ("requested alignment is too large");
952 *no_add_attrs = true;
956 /* If we have a TYPE_DECL, then copy the type, so that we
957 don't accidentally modify a builtin type. See pushdecl. */
958 if (decl && TREE_TYPE (decl) != error_mark_node
959 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
961 tree tt = TREE_TYPE (decl);
962 *type = build_type_copy (*type);
963 DECL_ORIGINAL_TYPE (decl) = tt;
964 TYPE_NAME (*type) = decl;
965 TREE_USED (*type) = TREE_USED (decl);
966 TREE_TYPE (decl) = *type;
968 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
969 *type = build_type_copy (*type);
971 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
972 TYPE_USER_ALIGN (*type) = 1;
974 else if (TREE_CODE (decl) != VAR_DECL
975 && TREE_CODE (decl) != FIELD_DECL)
977 error_with_decl (decl,
978 "alignment may not be specified for `%s'");
979 *no_add_attrs = true;
983 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
984 DECL_USER_ALIGN (decl) = 1;
990 /* Handle a "weak" attribute; arguments as in
991 struct attribute_spec.handler. */
994 handle_weak_attribute (node, name, args, flags, no_add_attrs)
996 tree name ATTRIBUTE_UNUSED;
997 tree args ATTRIBUTE_UNUSED;
998 int flags ATTRIBUTE_UNUSED;
999 bool *no_add_attrs ATTRIBUTE_UNUSED;
1001 declare_weak (*node);
1006 /* Handle an "alias" attribute; arguments as in
1007 struct attribute_spec.handler. */
1010 handle_alias_attribute (node, name, args, flags, no_add_attrs)
1014 int flags ATTRIBUTE_UNUSED;
1019 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1020 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1022 error_with_decl (decl,
1023 "`%s' defined both normally and as an alias");
1024 *no_add_attrs = true;
1026 else if (decl_function_context (decl) == 0)
1030 id = TREE_VALUE (args);
1031 if (TREE_CODE (id) != STRING_CST)
1033 error ("alias arg not a string");
1034 *no_add_attrs = true;
1037 id = get_identifier (TREE_STRING_POINTER (id));
1038 /* This counts as a use of the object pointed to. */
1041 if (TREE_CODE (decl) == FUNCTION_DECL)
1042 DECL_INITIAL (decl) = error_mark_node;
1044 DECL_EXTERNAL (decl) = 0;
1048 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1049 *no_add_attrs = true;
1055 /* Handle an "visibility" attribute; arguments as in
1056 struct attribute_spec.handler. */
1059 handle_visibility_attribute (node, name, args, flags, no_add_attrs)
1063 int flags ATTRIBUTE_UNUSED;
1068 if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
1070 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1071 *no_add_attrs = true;
1077 id = TREE_VALUE (args);
1078 if (TREE_CODE (id) != STRING_CST)
1080 error ("visibility arg not a string");
1081 *no_add_attrs = true;
1084 if (strcmp (TREE_STRING_POINTER (id), "hidden")
1085 && strcmp (TREE_STRING_POINTER (id), "protected")
1086 && strcmp (TREE_STRING_POINTER (id), "internal"))
1088 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
1089 *no_add_attrs = true;
1097 /* Handle a "no_instrument_function" attribute; arguments as in
1098 struct attribute_spec.handler. */
1101 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1104 tree args ATTRIBUTE_UNUSED;
1105 int flags ATTRIBUTE_UNUSED;
1110 if (TREE_CODE (decl) != FUNCTION_DECL)
1112 error_with_decl (decl,
1113 "`%s' attribute applies only to functions",
1114 IDENTIFIER_POINTER (name));
1115 *no_add_attrs = true;
1117 else if (DECL_INITIAL (decl))
1119 error_with_decl (decl,
1120 "can't set `%s' attribute after definition",
1121 IDENTIFIER_POINTER (name));
1122 *no_add_attrs = true;
1125 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1130 /* Handle a "malloc" attribute; arguments as in
1131 struct attribute_spec.handler. */
1134 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1137 tree args ATTRIBUTE_UNUSED;
1138 int flags ATTRIBUTE_UNUSED;
1141 if (TREE_CODE (*node) == FUNCTION_DECL)
1142 DECL_IS_MALLOC (*node) = 1;
1143 /* ??? TODO: Support types. */
1146 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1147 *no_add_attrs = true;
1153 /* Handle a "no_limit_stack" attribute; arguments as in
1154 struct attribute_spec.handler. */
1157 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1160 tree args ATTRIBUTE_UNUSED;
1161 int flags ATTRIBUTE_UNUSED;
1166 if (TREE_CODE (decl) != FUNCTION_DECL)
1168 error_with_decl (decl,
1169 "`%s' attribute applies only to functions",
1170 IDENTIFIER_POINTER (name));
1171 *no_add_attrs = true;
1173 else if (DECL_INITIAL (decl))
1175 error_with_decl (decl,
1176 "can't set `%s' attribute after definition",
1177 IDENTIFIER_POINTER (name));
1178 *no_add_attrs = true;
1181 DECL_NO_LIMIT_STACK (decl) = 1;
1186 /* Handle a "pure" attribute; arguments as in
1187 struct attribute_spec.handler. */
1190 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1193 tree args ATTRIBUTE_UNUSED;
1194 int flags ATTRIBUTE_UNUSED;
1197 if (TREE_CODE (*node) == FUNCTION_DECL)
1198 DECL_IS_PURE (*node) = 1;
1199 /* ??? TODO: Support types. */
1202 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1203 *no_add_attrs = true;
1209 /* Handle a "deprecated" attribute; arguments as in
1210 struct attribute_spec.handler. */
1213 handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
1216 tree args ATTRIBUTE_UNUSED;
1220 tree type = NULL_TREE;
1222 const char *what = NULL;
1227 type = TREE_TYPE (decl);
1229 if (TREE_CODE (decl) == TYPE_DECL
1230 || TREE_CODE (decl) == PARM_DECL
1231 || TREE_CODE (decl) == VAR_DECL
1232 || TREE_CODE (decl) == FUNCTION_DECL
1233 || TREE_CODE (decl) == FIELD_DECL)
1234 TREE_DEPRECATED (decl) = 1;
1238 else if (TYPE_P (*node))
1240 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1241 *node = build_type_copy (*node);
1242 TREE_DEPRECATED (*node) = 1;
1250 *no_add_attrs = true;
1251 if (type && TYPE_NAME (type))
1253 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1254 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
1255 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1256 && DECL_NAME (TYPE_NAME (type)))
1257 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1260 warning ("`%s' attribute ignored for `%s'",
1261 IDENTIFIER_POINTER (name), what);
1263 warning ("`%s' attribute ignored",
1264 IDENTIFIER_POINTER (name));
1270 /* Handle a "vector_size" attribute; arguments as in
1271 struct attribute_spec.handler. */
1274 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1278 int flags ATTRIBUTE_UNUSED;
1281 unsigned HOST_WIDE_INT vecsize, nunits;
1282 enum machine_mode mode, orig_mode, new_mode;
1283 tree type = *node, new_type;
1285 *no_add_attrs = true;
1287 if (! host_integerp (TREE_VALUE (args), 1))
1289 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1293 /* Get the vector size (in bytes). */
1294 vecsize = tree_low_cst (TREE_VALUE (args), 1);
1296 /* We need to provide for vector pointers, vector arrays, and
1297 functions returning vectors. For example:
1299 __attribute__((vector_size(16))) short *foo;
1301 In this case, the mode is SI, but the type being modified is
1302 HI, so we need to look further. */
1304 while (POINTER_TYPE_P (type)
1305 || TREE_CODE (type) == FUNCTION_TYPE
1306 || TREE_CODE (type) == ARRAY_TYPE)
1307 type = TREE_TYPE (type);
1309 /* Get the mode of the type being modified. */
1310 orig_mode = TYPE_MODE (type);
1312 if (TREE_CODE (type) == RECORD_TYPE
1313 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1314 && GET_MODE_CLASS (orig_mode) != MODE_INT)
1315 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
1317 error ("invalid vector type for attribute `%s'",
1318 IDENTIFIER_POINTER (name));
1322 /* Calculate how many units fit in the vector. */
1323 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1325 /* Find a suitably sized vector. */
1326 new_mode = VOIDmode;
1327 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
1329 : MODE_VECTOR_FLOAT);
1331 mode = GET_MODE_WIDER_MODE (mode))
1332 if (vecsize == GET_MODE_SIZE (mode)
1333 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
1339 if (new_mode == VOIDmode)
1340 error ("no vector mode with the size and type specified could be found");
1343 tree index, array, rt;
1345 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1349 error ("no vector mode with the size and type specified could be found");
1353 new_type = build_type_copy (new_type);
1355 /* Set the debug information here, because this is the only
1356 place where we know the underlying type for a vector made
1357 with vector_size. For debugging purposes we pretend a vector
1358 is an array within a structure. */
1359 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
1360 array = build_array_type (type, build_index_type (index));
1361 rt = make_node (RECORD_TYPE);
1363 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
1364 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
1366 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
1368 /* Build back pointers if needed. */
1369 *node = vector_size_helper (*node, new_type);
1375 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1378 If we requested a pointer to a vector, build up the pointers that
1379 we stripped off while looking for the inner type. Similarly for
1380 return values from functions.
1382 The argument "type" is the top of the chain, and "bottom" is the
1383 new type which we will point to. */
1386 vector_size_helper (type, bottom)
1391 if (POINTER_TYPE_P (type))
1393 inner = vector_size_helper (TREE_TYPE (type), bottom);
1394 outer = build_pointer_type (inner);
1396 else if (TREE_CODE (type) == ARRAY_TYPE)
1398 inner = vector_size_helper (TREE_TYPE (type), bottom);
1399 outer = build_array_type (inner, TYPE_VALUES (type));
1401 else if (TREE_CODE (type) == FUNCTION_TYPE)
1403 inner = vector_size_helper (TREE_TYPE (type), bottom);
1404 outer = build_function_type (inner, TYPE_VALUES (type));
1409 TREE_READONLY (outer) = TREE_READONLY (type);
1410 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1415 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1416 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1418 The head of the declspec list is stored in DECLSPECS.
1419 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1421 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1422 the list elements. We drop the containing TREE_LIST nodes and link the
1423 resulting attributes together the way decl_attributes expects them. */
1426 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1428 tree *declspecs, *prefix_attributes;
1430 tree t, s, a, next, specs, attrs;
1432 /* This can happen after an __extension__ in pedantic mode. */
1433 if (specs_attrs != NULL_TREE
1434 && TREE_CODE (specs_attrs) == INTEGER_CST)
1436 *declspecs = NULL_TREE;
1437 *prefix_attributes = NULL_TREE;
1441 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1442 if (specs_attrs != NULL_TREE
1443 && TREE_CODE (specs_attrs) != TREE_LIST)
1445 *declspecs = specs_attrs;
1446 *prefix_attributes = NULL_TREE;
1450 /* Remember to keep the lists in the same order, element-wise. */
1452 specs = s = NULL_TREE;
1453 attrs = a = NULL_TREE;
1454 for (t = specs_attrs; t; t = next)
1456 next = TREE_CHAIN (t);
1457 /* Declspecs have a non-NULL TREE_VALUE. */
1458 if (TREE_VALUE (t) != NULL_TREE)
1460 if (specs == NULL_TREE)
1468 /* The TREE_PURPOSE may also be empty in the case of
1469 __attribute__(()). */
1470 else if (TREE_PURPOSE (t) != NULL_TREE)
1472 if (attrs == NULL_TREE)
1473 attrs = a = TREE_PURPOSE (t);
1476 TREE_CHAIN (a) = TREE_PURPOSE (t);
1477 a = TREE_PURPOSE (t);
1479 /* More attrs can be linked here, move A to the end. */
1480 while (TREE_CHAIN (a) != NULL_TREE)
1485 /* Terminate the lists. */
1487 TREE_CHAIN (s) = NULL_TREE;
1489 TREE_CHAIN (a) = NULL_TREE;
1493 *prefix_attributes = attrs;
1496 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1497 This function is used by the parser when a rule will accept attributes
1498 in a particular position, but we don't want to support that just yet.
1500 A warning is issued for every ignored attribute. */
1503 strip_attrs (specs_attrs)
1508 split_specs_attrs (specs_attrs, &specs, &attrs);
1512 warning ("`%s' attribute ignored",
1513 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1514 attrs = TREE_CHAIN (attrs);