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_no_instrument_function_attribute PARAMS ((tree *, tree,
81 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
83 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
85 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
87 static tree handle_deprecated_attribute PARAMS ((tree *, tree, tree, int,
89 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
91 static tree vector_size_helper PARAMS ((tree, tree));
93 /* Table of machine-independent attributes common to all C-like languages. */
94 static const struct attribute_spec c_common_attribute_table[] =
96 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
97 { "packed", 0, 0, false, false, false,
98 handle_packed_attribute },
99 { "nocommon", 0, 0, true, false, false,
100 handle_nocommon_attribute },
101 { "common", 0, 0, true, false, false,
102 handle_common_attribute },
103 /* FIXME: logically, noreturn attributes should be listed as
104 "false, true, true" and apply to function types. But implementing this
105 would require all the places in the compiler that use TREE_THIS_VOLATILE
106 on a decl to identify non-returning functions to be located and fixed
107 to check the function type instead. */
108 { "noreturn", 0, 0, true, false, false,
109 handle_noreturn_attribute },
110 { "volatile", 0, 0, true, false, false,
111 handle_noreturn_attribute },
112 { "noinline", 0, 0, true, false, false,
113 handle_noinline_attribute },
114 { "always_inline", 0, 0, true, false, false,
115 handle_always_inline_attribute },
116 { "used", 0, 0, true, false, false,
117 handle_used_attribute },
118 { "unused", 0, 0, false, false, false,
119 handle_unused_attribute },
120 /* The same comments as for noreturn attributes apply to const ones. */
121 { "const", 0, 0, true, false, false,
122 handle_const_attribute },
123 { "transparent_union", 0, 0, false, false, false,
124 handle_transparent_union_attribute },
125 { "constructor", 0, 0, true, false, false,
126 handle_constructor_attribute },
127 { "destructor", 0, 0, true, false, false,
128 handle_destructor_attribute },
129 { "mode", 1, 1, false, true, false,
130 handle_mode_attribute },
131 { "section", 1, 1, true, false, false,
132 handle_section_attribute },
133 { "aligned", 0, 1, false, false, false,
134 handle_aligned_attribute },
135 { "weak", 0, 0, true, false, false,
136 handle_weak_attribute },
137 { "alias", 1, 1, true, false, false,
138 handle_alias_attribute },
139 { "no_instrument_function", 0, 0, true, false, false,
140 handle_no_instrument_function_attribute },
141 { "malloc", 0, 0, true, false, false,
142 handle_malloc_attribute },
143 { "no_stack_limit", 0, 0, true, false, false,
144 handle_no_limit_stack_attribute },
145 { "pure", 0, 0, true, false, false,
146 handle_pure_attribute },
147 { "deprecated", 0, 0, false, false, false,
148 handle_deprecated_attribute },
149 { "vector_size", 1, 1, false, true, false,
150 handle_vector_size_attribute },
151 { NULL, 0, 0, false, false, false, NULL }
154 /* Default empty table of attributes. */
155 static const struct attribute_spec empty_attribute_table[] =
157 { NULL, 0, 0, false, false, false, NULL }
160 /* Table of machine-independent attributes for checking formats, if used. */
161 const struct attribute_spec *format_attribute_table = empty_attribute_table;
163 /* Table of machine-independent attributes for a particular language. */
164 const struct attribute_spec *lang_attribute_table = empty_attribute_table;
166 /* Flag saying whether common language attributes are to be supported. */
167 int lang_attribute_common = 1;
169 /* Initialize attribute tables, and make some sanity checks
170 if --enable-checking. */
175 #ifdef ENABLE_CHECKING
180 = lang_attribute_common ? c_common_attribute_table : empty_attribute_table;
181 attribute_tables[1] = lang_attribute_table;
182 attribute_tables[2] = format_attribute_table;
183 attribute_tables[3] = targetm.attribute_table;
185 #ifdef ENABLE_CHECKING
186 /* Make some sanity checks on the attribute tables. */
188 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
193 for (j = 0; attribute_tables[i][j].name != NULL; j++)
195 /* The name must not begin and end with __. */
196 const char *name = attribute_tables[i][j].name;
197 int len = strlen (name);
198 if (name[0] == '_' && name[1] == '_'
199 && name[len - 1] == '_' && name[len - 2] == '_')
201 /* The minimum and maximum lengths must be consistent. */
202 if (attribute_tables[i][j].min_length < 0)
204 if (attribute_tables[i][j].max_length != -1
205 && (attribute_tables[i][j].max_length
206 < attribute_tables[i][j].min_length))
208 /* An attribute cannot require both a DECL and a TYPE. */
209 if (attribute_tables[i][j].decl_required
210 && attribute_tables[i][j].type_required)
212 /* If an attribute requires a function type, in particular
213 it requires a type. */
214 if (attribute_tables[i][j].function_type_required
215 && !attribute_tables[i][j].type_required)
220 /* Check that each name occurs just once in each table. */
222 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
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. */
234 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
240 j < ((int) (sizeof (attribute_tables)
241 / sizeof (attribute_tables[0])));
243 for (k = 0; attribute_tables[i][k].name != NULL; k++)
244 for (l = 0; attribute_tables[j][l].name != NULL; l++)
245 if (!strcmp (attribute_tables[i][k].name,
246 attribute_tables[j][l].name))
251 attributes_initialized = true;
254 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
255 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
256 it should be modified in place; if a TYPE, a copy should be created
257 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
258 information, in the form of a bitwise OR of flags in enum attribute_flags
259 from tree.h. Depending on these flags, some attributes may be
260 returned to be applied at a later stage (for example, to apply
261 a decl attribute to the declaration rather than to its type). If
262 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
263 whether there might be some default attributes to apply to this DECL;
264 if so, decl_attributes will be called recursively with those attributes
265 and ATTR_FLAG_BUILT_IN set. */
268 decl_attributes (node, attributes, flags)
269 tree *node, attributes;
273 tree returned_attrs = NULL_TREE;
275 if (!attributes_initialized)
278 (*targetm.insert_attributes) (*node, &attributes);
280 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
281 && !(flags & (int) ATTR_FLAG_BUILT_IN))
282 insert_default_attributes (*node);
284 for (a = attributes; a; a = TREE_CHAIN (a))
286 tree name = TREE_PURPOSE (a);
287 tree args = TREE_VALUE (a);
289 const struct attribute_spec *spec = NULL;
290 bool no_add_attrs = 0;
294 i < ((int) (sizeof (attribute_tables)
295 / sizeof (attribute_tables[0])));
300 for (j = 0; attribute_tables[i][j].name != NULL; j++)
302 if (is_attribute_p (attribute_tables[i][j].name, name))
304 spec = &attribute_tables[i][j];
314 warning ("`%s' attribute directive ignored",
315 IDENTIFIER_POINTER (name));
318 else if (list_length (args) < spec->min_length
319 || (spec->max_length >= 0
320 && list_length (args) > spec->max_length))
322 error ("wrong number of arguments specified for `%s' attribute",
323 IDENTIFIER_POINTER (name));
327 if (spec->decl_required && !DECL_P (*anode))
329 if (flags & ((int) ATTR_FLAG_DECL_NEXT
330 | (int) ATTR_FLAG_FUNCTION_NEXT
331 | (int) ATTR_FLAG_ARRAY_NEXT))
333 /* Pass on this attribute to be tried again. */
334 returned_attrs = tree_cons (name, args, returned_attrs);
339 warning ("`%s' attribute does not apply to types",
340 IDENTIFIER_POINTER (name));
345 /* If we require a type, but were passed a decl, set up to make a
346 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
347 would have applied if we'd been passed a type, but we cannot modify
348 the decl's type in place here. */
349 if (spec->type_required && DECL_P (*anode))
351 anode = &TREE_TYPE (*anode);
352 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
355 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
356 && TREE_CODE (*anode) != METHOD_TYPE)
358 if (TREE_CODE (*anode) == POINTER_TYPE
359 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
360 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
362 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
363 *anode = build_type_copy (*anode);
364 anode = &TREE_TYPE (*anode);
366 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
368 /* Pass on this attribute to be tried again. */
369 returned_attrs = tree_cons (name, args, returned_attrs);
373 if (TREE_CODE (*anode) != FUNCTION_TYPE
374 && TREE_CODE (*anode) != METHOD_TYPE)
376 warning ("`%s' attribute only applies to function types",
377 IDENTIFIER_POINTER (name));
382 if (spec->handler != NULL)
383 returned_attrs = chainon ((*spec->handler) (anode, name, args,
384 flags, &no_add_attrs),
387 /* Layout the decl in case anything changed. */
388 if (spec->type_required && DECL_P (*node)
389 && (TREE_CODE (*node) == VAR_DECL
390 || TREE_CODE (*node) == PARM_DECL
391 || TREE_CODE (*node) == RESULT_DECL))
393 /* Force a recalculation of mode and size. */
394 DECL_MODE (*node) = VOIDmode;
395 DECL_SIZE (*node) = 0;
397 layout_decl (*node, 0);
406 old_attrs = DECL_ATTRIBUTES (*anode);
408 old_attrs = TYPE_ATTRIBUTES (*anode);
410 for (a = lookup_attribute (spec->name, old_attrs);
412 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
414 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
420 /* This attribute isn't already in the list. */
422 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
423 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
424 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
426 *anode = build_type_attribute_variant (*anode,
427 tree_cons (name, args,
433 return returned_attrs;
436 /* Handle a "packed" attribute; arguments as in
437 struct attribute_spec.handler. */
440 handle_packed_attribute (node, name, args, flags, no_add_attrs)
443 tree args ATTRIBUTE_UNUSED;
450 if (TREE_CODE (*node) == TYPE_DECL)
451 type = &TREE_TYPE (*node);
458 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
459 *type = build_type_copy (*type);
460 TYPE_PACKED (*type) = 1;
462 else if (TREE_CODE (*node) == FIELD_DECL)
463 DECL_PACKED (*node) = 1;
464 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
465 used for DECL_REGISTER. It wouldn't mean anything anyway. */
468 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
469 *no_add_attrs = true;
475 /* Handle a "nocommon" attribute; arguments as in
476 struct attribute_spec.handler. */
479 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
482 tree args ATTRIBUTE_UNUSED;
483 int flags ATTRIBUTE_UNUSED;
486 if (TREE_CODE (*node) == VAR_DECL)
487 DECL_COMMON (*node) = 0;
490 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
491 *no_add_attrs = true;
497 /* Handle a "common" attribute; arguments as in
498 struct attribute_spec.handler. */
501 handle_common_attribute (node, name, args, flags, no_add_attrs)
504 tree args ATTRIBUTE_UNUSED;
505 int flags ATTRIBUTE_UNUSED;
508 if (TREE_CODE (*node) == VAR_DECL)
509 DECL_COMMON (*node) = 1;
512 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
513 *no_add_attrs = true;
519 /* Handle a "noreturn" attribute; arguments as in
520 struct attribute_spec.handler. */
523 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
526 tree args ATTRIBUTE_UNUSED;
527 int flags ATTRIBUTE_UNUSED;
530 tree type = TREE_TYPE (*node);
532 /* See FIXME comment in c_common_attribute_table. */
533 if (TREE_CODE (*node) == FUNCTION_DECL)
534 TREE_THIS_VOLATILE (*node) = 1;
535 else if (TREE_CODE (type) == POINTER_TYPE
536 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
539 (build_type_variant (TREE_TYPE (type),
540 TREE_READONLY (TREE_TYPE (type)), 1));
543 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
544 *no_add_attrs = true;
550 /* Handle a "noinline" attribute; arguments as in
551 struct attribute_spec.handler. */
554 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
557 tree args ATTRIBUTE_UNUSED;
558 int flags ATTRIBUTE_UNUSED;
561 if (TREE_CODE (*node) == FUNCTION_DECL)
562 DECL_UNINLINABLE (*node) = 1;
565 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
566 *no_add_attrs = true;
572 /* Handle a "always_inline" attribute; arguments as in
573 struct attribute_spec.handler. */
576 handle_always_inline_attribute (node, name, args, flags, no_add_attrs)
579 tree args ATTRIBUTE_UNUSED;
580 int flags ATTRIBUTE_UNUSED;
583 if (TREE_CODE (*node) == FUNCTION_DECL)
585 /* Do nothing else, just set the attribute. We'll get at
586 it later with lookup_attribute. */
590 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
591 *no_add_attrs = true;
597 /* Handle a "used" attribute; arguments as in
598 struct attribute_spec.handler. */
601 handle_used_attribute (node, name, args, flags, no_add_attrs)
604 tree args ATTRIBUTE_UNUSED;
605 int flags ATTRIBUTE_UNUSED;
608 if (TREE_CODE (*node) == FUNCTION_DECL)
609 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
610 = TREE_USED (*node) = 1;
613 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
614 *no_add_attrs = true;
620 /* Handle a "unused" attribute; arguments as in
621 struct attribute_spec.handler. */
624 handle_unused_attribute (node, name, args, flags, no_add_attrs)
627 tree args ATTRIBUTE_UNUSED;
635 if (TREE_CODE (decl) == PARM_DECL
636 || TREE_CODE (decl) == VAR_DECL
637 || TREE_CODE (decl) == FUNCTION_DECL
638 || TREE_CODE (decl) == LABEL_DECL
639 || TREE_CODE (decl) == TYPE_DECL)
640 TREE_USED (decl) = 1;
643 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
644 *no_add_attrs = true;
649 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
650 *node = build_type_copy (*node);
651 TREE_USED (*node) = 1;
657 /* Handle a "const" attribute; arguments as in
658 struct attribute_spec.handler. */
661 handle_const_attribute (node, name, args, flags, no_add_attrs)
664 tree args ATTRIBUTE_UNUSED;
665 int flags ATTRIBUTE_UNUSED;
668 tree type = TREE_TYPE (*node);
670 /* See FIXME comment on noreturn in c_common_attribute_table. */
671 if (TREE_CODE (*node) == FUNCTION_DECL)
672 TREE_READONLY (*node) = 1;
673 else if (TREE_CODE (type) == POINTER_TYPE
674 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
677 (build_type_variant (TREE_TYPE (type), 1,
678 TREE_THIS_VOLATILE (TREE_TYPE (type))));
681 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
682 *no_add_attrs = true;
688 /* Handle a "transparent_union" attribute; arguments as in
689 struct attribute_spec.handler. */
692 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
695 tree args ATTRIBUTE_UNUSED;
699 tree decl = NULL_TREE;
706 type = &TREE_TYPE (decl);
707 is_type = TREE_CODE (*node) == TYPE_DECL;
709 else if (TYPE_P (*node))
710 type = node, is_type = 1;
713 && TREE_CODE (*type) == UNION_TYPE
715 || (TYPE_FIELDS (*type) != 0
716 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
718 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
719 *type = build_type_copy (*type);
720 TYPE_TRANSPARENT_UNION (*type) = 1;
722 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
723 && TREE_CODE (*type) == UNION_TYPE
724 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
725 DECL_TRANSPARENT_UNION (decl) = 1;
728 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
729 *no_add_attrs = true;
735 /* Handle a "constructor" attribute; arguments as in
736 struct attribute_spec.handler. */
739 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
742 tree args ATTRIBUTE_UNUSED;
743 int flags ATTRIBUTE_UNUSED;
747 tree type = TREE_TYPE (decl);
749 if (TREE_CODE (decl) == FUNCTION_DECL
750 && TREE_CODE (type) == FUNCTION_TYPE
751 && decl_function_context (decl) == 0)
753 DECL_STATIC_CONSTRUCTOR (decl) = 1;
754 TREE_USED (decl) = 1;
758 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
759 *no_add_attrs = true;
765 /* Handle a "destructor" attribute; arguments as in
766 struct attribute_spec.handler. */
769 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
772 tree args ATTRIBUTE_UNUSED;
773 int flags ATTRIBUTE_UNUSED;
777 tree type = TREE_TYPE (decl);
779 if (TREE_CODE (decl) == FUNCTION_DECL
780 && TREE_CODE (type) == FUNCTION_TYPE
781 && decl_function_context (decl) == 0)
783 DECL_STATIC_DESTRUCTOR (decl) = 1;
784 TREE_USED (decl) = 1;
788 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
789 *no_add_attrs = true;
795 /* Handle a "mode" attribute; arguments as in
796 struct attribute_spec.handler. */
799 handle_mode_attribute (node, name, args, flags, no_add_attrs)
803 int flags ATTRIBUTE_UNUSED;
808 *no_add_attrs = true;
810 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
811 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
815 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
816 int len = strlen (p);
817 enum machine_mode mode = VOIDmode;
820 if (len > 4 && p[0] == '_' && p[1] == '_'
821 && p[len - 1] == '_' && p[len - 2] == '_')
823 char *newp = (char *) alloca (len - 1);
825 strcpy (newp, &p[2]);
826 newp[len - 4] = '\0';
830 /* Change this type to have a type with the specified mode.
831 First check for the special modes. */
832 if (! strcmp (p, "byte"))
834 else if (!strcmp (p, "word"))
836 else if (! strcmp (p, "pointer"))
839 for (j = 0; j < NUM_MACHINE_MODES; j++)
840 if (!strcmp (p, GET_MODE_NAME (j)))
841 mode = (enum machine_mode) j;
843 if (mode == VOIDmode)
844 error ("unknown machine mode `%s'", p);
845 else if (0 == (typefm = type_for_mode (mode,
846 TREE_UNSIGNED (type))))
847 error ("no data type for mode `%s'", p);
850 /* No need to layout the type here. The caller should do this. */
856 /* Handle a "section" attribute; arguments as in
857 struct attribute_spec.handler. */
860 handle_section_attribute (node, name, args, flags, no_add_attrs)
862 tree name ATTRIBUTE_UNUSED;
864 int flags ATTRIBUTE_UNUSED;
869 if (targetm.have_named_sections)
871 if ((TREE_CODE (decl) == FUNCTION_DECL
872 || TREE_CODE (decl) == VAR_DECL)
873 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
875 if (TREE_CODE (decl) == VAR_DECL
876 && current_function_decl != NULL_TREE
877 && ! TREE_STATIC (decl))
879 error_with_decl (decl,
880 "section attribute cannot be specified for local variables");
881 *no_add_attrs = true;
884 /* The decl may have already been given a section attribute
885 from a previous declaration. Ensure they match. */
886 else if (DECL_SECTION_NAME (decl) != NULL_TREE
887 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
888 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
890 error_with_decl (*node,
891 "section of `%s' conflicts with previous declaration");
892 *no_add_attrs = true;
895 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
899 error_with_decl (*node,
900 "section attribute not allowed for `%s'");
901 *no_add_attrs = true;
906 error_with_decl (*node,
907 "section attributes are not supported for this target");
908 *no_add_attrs = true;
914 /* Handle a "aligned" attribute; arguments as in
915 struct attribute_spec.handler. */
918 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
920 tree name ATTRIBUTE_UNUSED;
925 tree decl = NULL_TREE;
928 tree align_expr = (args ? TREE_VALUE (args)
929 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
935 type = &TREE_TYPE (decl);
936 is_type = TREE_CODE (*node) == TYPE_DECL;
938 else if (TYPE_P (*node))
939 type = node, is_type = 1;
941 /* Strip any NOPs of any kind. */
942 while (TREE_CODE (align_expr) == NOP_EXPR
943 || TREE_CODE (align_expr) == CONVERT_EXPR
944 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
945 align_expr = TREE_OPERAND (align_expr, 0);
947 if (TREE_CODE (align_expr) != INTEGER_CST)
949 error ("requested alignment is not a constant");
950 *no_add_attrs = true;
952 else if ((i = tree_log2 (align_expr)) == -1)
954 error ("requested alignment is not a power of 2");
955 *no_add_attrs = true;
957 else if (i > HOST_BITS_PER_INT - 2)
959 error ("requested alignment is too large");
960 *no_add_attrs = true;
964 /* If we have a TYPE_DECL, then copy the type, so that we
965 don't accidentally modify a builtin type. See pushdecl. */
966 if (decl && TREE_TYPE (decl) != error_mark_node
967 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
969 tree tt = TREE_TYPE (decl);
970 *type = build_type_copy (*type);
971 DECL_ORIGINAL_TYPE (decl) = tt;
972 TYPE_NAME (*type) = decl;
973 TREE_USED (*type) = TREE_USED (decl);
974 TREE_TYPE (decl) = *type;
976 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
977 *type = build_type_copy (*type);
979 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
980 TYPE_USER_ALIGN (*type) = 1;
982 else if (TREE_CODE (decl) != VAR_DECL
983 && TREE_CODE (decl) != FIELD_DECL)
985 error_with_decl (decl,
986 "alignment may not be specified for `%s'");
987 *no_add_attrs = true;
991 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
992 DECL_USER_ALIGN (decl) = 1;
998 /* Handle a "weak" attribute; arguments as in
999 struct attribute_spec.handler. */
1002 handle_weak_attribute (node, name, args, flags, no_add_attrs)
1004 tree name ATTRIBUTE_UNUSED;
1005 tree args ATTRIBUTE_UNUSED;
1006 int flags ATTRIBUTE_UNUSED;
1007 bool *no_add_attrs ATTRIBUTE_UNUSED;
1009 declare_weak (*node);
1014 /* Handle an "alias" attribute; arguments as in
1015 struct attribute_spec.handler. */
1018 handle_alias_attribute (node, name, args, flags, no_add_attrs)
1022 int flags ATTRIBUTE_UNUSED;
1027 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1028 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1030 error_with_decl (decl,
1031 "`%s' defined both normally and as an alias");
1032 *no_add_attrs = true;
1034 else if (decl_function_context (decl) == 0)
1038 id = TREE_VALUE (args);
1039 if (TREE_CODE (id) != STRING_CST)
1041 error ("alias arg not a string");
1042 *no_add_attrs = true;
1045 id = get_identifier (TREE_STRING_POINTER (id));
1046 /* This counts as a use of the object pointed to. */
1049 if (TREE_CODE (decl) == FUNCTION_DECL)
1050 DECL_INITIAL (decl) = error_mark_node;
1052 DECL_EXTERNAL (decl) = 0;
1053 assemble_alias (decl, id);
1057 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1058 *no_add_attrs = true;
1064 /* Handle a "no_instrument_function" attribute; arguments as in
1065 struct attribute_spec.handler. */
1068 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1071 tree args ATTRIBUTE_UNUSED;
1072 int flags ATTRIBUTE_UNUSED;
1077 if (TREE_CODE (decl) != FUNCTION_DECL)
1079 error_with_decl (decl,
1080 "`%s' attribute applies only to functions",
1081 IDENTIFIER_POINTER (name));
1082 *no_add_attrs = true;
1084 else if (DECL_INITIAL (decl))
1086 error_with_decl (decl,
1087 "can't set `%s' attribute after definition",
1088 IDENTIFIER_POINTER (name));
1089 *no_add_attrs = true;
1092 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1097 /* Handle a "malloc" attribute; arguments as in
1098 struct attribute_spec.handler. */
1101 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1104 tree args ATTRIBUTE_UNUSED;
1105 int flags ATTRIBUTE_UNUSED;
1108 if (TREE_CODE (*node) == FUNCTION_DECL)
1109 DECL_IS_MALLOC (*node) = 1;
1110 /* ??? TODO: Support types. */
1113 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1114 *no_add_attrs = true;
1120 /* Handle a "no_limit_stack" attribute; arguments as in
1121 struct attribute_spec.handler. */
1124 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1127 tree args ATTRIBUTE_UNUSED;
1128 int flags ATTRIBUTE_UNUSED;
1133 if (TREE_CODE (decl) != FUNCTION_DECL)
1135 error_with_decl (decl,
1136 "`%s' attribute applies only to functions",
1137 IDENTIFIER_POINTER (name));
1138 *no_add_attrs = true;
1140 else if (DECL_INITIAL (decl))
1142 error_with_decl (decl,
1143 "can't set `%s' attribute after definition",
1144 IDENTIFIER_POINTER (name));
1145 *no_add_attrs = true;
1148 DECL_NO_LIMIT_STACK (decl) = 1;
1153 /* Handle a "pure" attribute; arguments as in
1154 struct attribute_spec.handler. */
1157 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1160 tree args ATTRIBUTE_UNUSED;
1161 int flags ATTRIBUTE_UNUSED;
1164 if (TREE_CODE (*node) == FUNCTION_DECL)
1165 DECL_IS_PURE (*node) = 1;
1166 /* ??? TODO: Support types. */
1169 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1170 *no_add_attrs = true;
1176 /* Handle a "deprecated" attribute; arguments as in
1177 struct attribute_spec.handler. */
1180 handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
1183 tree args ATTRIBUTE_UNUSED;
1187 tree type = NULL_TREE;
1189 const char *what = NULL;
1194 type = TREE_TYPE (decl);
1196 if (TREE_CODE (decl) == TYPE_DECL
1197 || TREE_CODE (decl) == PARM_DECL
1198 || TREE_CODE (decl) == VAR_DECL
1199 || TREE_CODE (decl) == FUNCTION_DECL
1200 || TREE_CODE (decl) == FIELD_DECL)
1201 TREE_DEPRECATED (decl) = 1;
1205 else if (TYPE_P (*node))
1207 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1208 *node = build_type_copy (*node);
1209 TREE_DEPRECATED (*node) = 1;
1217 *no_add_attrs = true;
1218 if (type && TYPE_NAME (type))
1220 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1221 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
1222 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1223 && DECL_NAME (TYPE_NAME (type)))
1224 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1227 warning ("`%s' attribute ignored for `%s'",
1228 IDENTIFIER_POINTER (name), what);
1230 warning ("`%s' attribute ignored",
1231 IDENTIFIER_POINTER (name));
1237 /* Handle a "vector_size" attribute; arguments as in
1238 struct attribute_spec.handler. */
1241 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1245 int flags ATTRIBUTE_UNUSED;
1248 unsigned HOST_WIDE_INT vecsize, nunits;
1249 enum machine_mode mode, orig_mode, new_mode;
1250 tree type = *node, new_type;
1252 *no_add_attrs = true;
1254 if (! host_integerp (TREE_VALUE (args), 1))
1256 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1260 /* Get the vector size (in bytes). */
1261 vecsize = tree_low_cst (TREE_VALUE (args), 1);
1263 /* We need to provide for vector pointers, vector arrays, and
1264 functions returning vectors. For example:
1266 __attribute__((vector_size(16))) short *foo;
1268 In this case, the mode is SI, but the type being modified is
1269 HI, so we need to look further. */
1271 while (POINTER_TYPE_P (type)
1272 || TREE_CODE (type) == FUNCTION_TYPE
1273 || TREE_CODE (type) == ARRAY_TYPE)
1274 type = TREE_TYPE (type);
1276 /* Get the mode of the type being modified. */
1277 orig_mode = TYPE_MODE (type);
1279 if (TREE_CODE (type) == RECORD_TYPE
1280 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1281 && GET_MODE_CLASS (orig_mode) != MODE_INT)
1282 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
1284 error ("invalid vector type for attribute `%s'",
1285 IDENTIFIER_POINTER (name));
1289 /* Calculate how many units fit in the vector. */
1290 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1292 /* Find a suitably sized vector. */
1293 new_mode = VOIDmode;
1294 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
1296 : MODE_VECTOR_FLOAT);
1298 mode = GET_MODE_WIDER_MODE (mode))
1299 if (vecsize == GET_MODE_SIZE (mode)
1300 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
1306 if (new_mode == VOIDmode)
1307 error ("no vector mode with the size and type specified could be found");
1310 tree index, array, rt;
1312 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1316 error ("no vector mode with the size and type specified could be found");
1320 new_type = build_type_copy (new_type);
1322 /* Set the debug information here, because this is the only
1323 place where we know the underlying type for a vector made
1324 with vector_size. For debugging purposes we pretend a vector
1325 is an array within a structure. */
1326 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
1327 array = build_array_type (type, build_index_type (index));
1328 rt = make_node (RECORD_TYPE);
1330 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
1331 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
1333 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
1335 /* Build back pointers if needed. */
1336 *node = vector_size_helper (*node, new_type);
1342 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1345 If we requested a pointer to a vector, build up the pointers that
1346 we stripped off while looking for the inner type. Similarly for
1347 return values from functions.
1349 The argument "type" is the top of the chain, and "bottom" is the
1350 new type which we will point to. */
1353 vector_size_helper (type, bottom)
1358 if (POINTER_TYPE_P (type))
1360 inner = vector_size_helper (TREE_TYPE (type), bottom);
1361 outer = build_pointer_type (inner);
1363 else if (TREE_CODE (type) == ARRAY_TYPE)
1365 inner = vector_size_helper (TREE_TYPE (type), bottom);
1366 outer = build_array_type (inner, TYPE_VALUES (type));
1368 else if (TREE_CODE (type) == FUNCTION_TYPE)
1370 inner = vector_size_helper (TREE_TYPE (type), bottom);
1371 outer = build_function_type (inner, TYPE_VALUES (type));
1376 TREE_READONLY (outer) = TREE_READONLY (type);
1377 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1382 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1383 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1385 The head of the declspec list is stored in DECLSPECS.
1386 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1388 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1389 the list elements. We drop the containing TREE_LIST nodes and link the
1390 resulting attributes together the way decl_attributes expects them. */
1393 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1395 tree *declspecs, *prefix_attributes;
1397 tree t, s, a, next, specs, attrs;
1399 /* This can happen after an __extension__ in pedantic mode. */
1400 if (specs_attrs != NULL_TREE
1401 && TREE_CODE (specs_attrs) == INTEGER_CST)
1403 *declspecs = NULL_TREE;
1404 *prefix_attributes = NULL_TREE;
1408 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1409 if (specs_attrs != NULL_TREE
1410 && TREE_CODE (specs_attrs) != TREE_LIST)
1412 *declspecs = specs_attrs;
1413 *prefix_attributes = NULL_TREE;
1417 /* Remember to keep the lists in the same order, element-wise. */
1419 specs = s = NULL_TREE;
1420 attrs = a = NULL_TREE;
1421 for (t = specs_attrs; t; t = next)
1423 next = TREE_CHAIN (t);
1424 /* Declspecs have a non-NULL TREE_VALUE. */
1425 if (TREE_VALUE (t) != NULL_TREE)
1427 if (specs == NULL_TREE)
1435 /* The TREE_PURPOSE may also be empty in the case of
1436 __attribute__(()). */
1437 else if (TREE_PURPOSE (t) != NULL_TREE)
1439 if (attrs == NULL_TREE)
1440 attrs = a = TREE_PURPOSE (t);
1443 TREE_CHAIN (a) = TREE_PURPOSE (t);
1444 a = TREE_PURPOSE (t);
1446 /* More attrs can be linked here, move A to the end. */
1447 while (TREE_CHAIN (a) != NULL_TREE)
1452 /* Terminate the lists. */
1454 TREE_CHAIN (s) = NULL_TREE;
1456 TREE_CHAIN (a) = NULL_TREE;
1460 *prefix_attributes = attrs;
1463 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1464 This function is used by the parser when a rule will accept attributes
1465 in a particular position, but we don't want to support that just yet.
1467 A warning is issued for every ignored attribute. */
1470 strip_attrs (specs_attrs)
1475 split_specs_attrs (specs_attrs, &specs, &attrs);
1479 warning ("`%s' attribute ignored",
1480 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1481 attrs = TREE_CHAIN (attrs);