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 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_used_attribute PARAMS ((tree *, tree, tree, int,
56 static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
58 static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
60 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
62 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
64 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
66 static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
68 static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
70 static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
72 static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
74 static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
76 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
79 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
81 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
83 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
85 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
87 static tree vector_size_helper PARAMS ((tree, tree));
89 /* Table of machine-independent attributes common to all C-like languages. */
90 static const struct attribute_spec c_common_attribute_table[] =
92 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
93 { "packed", 0, 0, false, false, false,
94 handle_packed_attribute },
95 { "nocommon", 0, 0, true, false, false,
96 handle_nocommon_attribute },
97 { "common", 0, 0, true, false, false,
98 handle_common_attribute },
99 /* FIXME: logically, noreturn attributes should be listed as
100 "false, true, true" and apply to function types. But implementing this
101 would require all the places in the compiler that use TREE_THIS_VOLATILE
102 on a decl to identify non-returning functions to be located and fixed
103 to check the function type instead. */
104 { "noreturn", 0, 0, true, false, false,
105 handle_noreturn_attribute },
106 { "volatile", 0, 0, true, false, false,
107 handle_noreturn_attribute },
108 { "noinline", 0, 0, true, false, false,
109 handle_noinline_attribute },
110 { "used", 0, 0, true, false, false,
111 handle_used_attribute },
112 { "unused", 0, 0, false, false, false,
113 handle_unused_attribute },
114 /* The same comments as for noreturn attributes apply to const ones. */
115 { "const", 0, 0, true, false, false,
116 handle_const_attribute },
117 { "transparent_union", 0, 0, false, false, false,
118 handle_transparent_union_attribute },
119 { "constructor", 0, 0, true, false, false,
120 handle_constructor_attribute },
121 { "destructor", 0, 0, true, false, false,
122 handle_destructor_attribute },
123 { "mode", 1, 1, false, true, false,
124 handle_mode_attribute },
125 { "section", 1, 1, true, false, false,
126 handle_section_attribute },
127 { "aligned", 0, 1, false, false, false,
128 handle_aligned_attribute },
129 { "weak", 0, 0, true, false, false,
130 handle_weak_attribute },
131 { "alias", 1, 1, true, false, false,
132 handle_alias_attribute },
133 { "no_instrument_function", 0, 0, true, false, false,
134 handle_no_instrument_function_attribute },
135 { "malloc", 0, 0, true, false, false,
136 handle_malloc_attribute },
137 { "no_stack_limit", 0, 0, true, false, false,
138 handle_no_limit_stack_attribute },
139 { "pure", 0, 0, true, false, false,
140 handle_pure_attribute },
141 { "vector_size", 1, 1, false, true, false,
142 handle_vector_size_attribute },
143 { NULL, 0, 0, false, false, false, NULL }
146 /* Default empty table of attributes. */
147 static const struct attribute_spec empty_attribute_table[] =
149 { NULL, 0, 0, false, false, false, NULL }
152 /* Table of machine-independent attributes for checking formats, if used. */
153 const struct attribute_spec *format_attribute_table = empty_attribute_table;
155 /* Table of machine-independent attributes for a particular language. */
156 const struct attribute_spec *lang_attribute_table = empty_attribute_table;
158 /* Flag saying whether common language attributes are to be supported. */
159 int lang_attribute_common = 1;
161 /* Initialize attribute tables, and make some sanity checks
162 if --enable-checking. */
167 #ifdef ENABLE_CHECKING
172 = lang_attribute_common ? c_common_attribute_table : empty_attribute_table;
173 attribute_tables[1] = lang_attribute_table;
174 attribute_tables[2] = format_attribute_table;
175 attribute_tables[3] = targetm.attribute_table;
177 #ifdef ENABLE_CHECKING
178 /* Make some sanity checks on the attribute tables. */
180 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
185 for (j = 0; attribute_tables[i][j].name != NULL; j++)
187 /* The name must not begin and end with __. */
188 const char *name = attribute_tables[i][j].name;
189 int len = strlen (name);
190 if (name[0] == '_' && name[1] == '_'
191 && name[len - 1] == '_' && name[len - 2] == '_')
193 /* The minimum and maximum lengths must be consistent. */
194 if (attribute_tables[i][j].min_length < 0)
196 if (attribute_tables[i][j].max_length != -1
197 && (attribute_tables[i][j].max_length
198 < attribute_tables[i][j].min_length))
200 /* An attribute cannot require both a DECL and a TYPE. */
201 if (attribute_tables[i][j].decl_required
202 && attribute_tables[i][j].type_required)
204 /* If an attribute requires a function type, in particular
205 it requires a type. */
206 if (attribute_tables[i][j].function_type_required
207 && !attribute_tables[i][j].type_required)
212 /* Check that each name occurs just once in each table. */
214 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
218 for (j = 0; attribute_tables[i][j].name != NULL; j++)
219 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
220 if (!strcmp (attribute_tables[i][j].name,
221 attribute_tables[i][k].name))
224 /* Check that no name occurs in more than one table. */
226 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
232 j < ((int) (sizeof (attribute_tables)
233 / sizeof (attribute_tables[0])));
235 for (k = 0; attribute_tables[i][k].name != NULL; k++)
236 for (l = 0; attribute_tables[j][l].name != NULL; l++)
237 if (!strcmp (attribute_tables[i][k].name,
238 attribute_tables[j][l].name))
243 attributes_initialized = true;
246 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
247 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
248 it should be modified in place; if a TYPE, a copy should be created
249 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
250 information, in the form of a bitwise OR of flags in enum attribute_flags
251 from tree.h. Depending on these flags, some attributes may be
252 returned to be applied at a later stage (for example, to apply
253 a decl attribute to the declaration rather than to its type). If
254 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
255 whether there might be some default attributes to apply to this DECL;
256 if so, decl_attributes will be called recursively with those attributes
257 and ATTR_FLAG_BUILT_IN set. */
260 decl_attributes (node, attributes, flags)
261 tree *node, attributes;
265 tree returned_attrs = NULL_TREE;
267 if (!attributes_initialized)
270 (*targetm.insert_attributes) (*node, &attributes);
272 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
273 && !(flags & (int) ATTR_FLAG_BUILT_IN))
274 insert_default_attributes (*node);
276 for (a = attributes; a; a = TREE_CHAIN (a))
278 tree name = TREE_PURPOSE (a);
279 tree args = TREE_VALUE (a);
281 const struct attribute_spec *spec = NULL;
282 bool no_add_attrs = 0;
286 i < ((int) (sizeof (attribute_tables)
287 / sizeof (attribute_tables[0])));
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 (spec->type_required && DECL_P (*anode))
338 anode = &TREE_TYPE (*anode);
340 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
341 && TREE_CODE (*anode) != METHOD_TYPE)
343 if (TREE_CODE (*anode) == POINTER_TYPE
344 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
345 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
347 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
348 *anode = build_type_copy (*anode);
349 anode = &TREE_TYPE (*anode);
351 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
353 /* Pass on this attribute to be tried again. */
354 returned_attrs = tree_cons (name, args, returned_attrs);
358 if (TREE_CODE (*anode) != FUNCTION_TYPE
359 && TREE_CODE (*anode) != METHOD_TYPE)
361 warning ("`%s' attribute only applies to function types",
362 IDENTIFIER_POINTER (name));
367 if (spec->handler != NULL)
368 returned_attrs = chainon ((*spec->handler) (anode, name, args,
369 flags, &no_add_attrs),
372 /* Layout the decl in case anything changed. */
373 if (spec->type_required && DECL_P (*node)
374 && TREE_CODE (*node) == VAR_DECL)
376 /* Force a recalculation of mode and size. */
377 DECL_MODE (*node) = VOIDmode;
378 DECL_SIZE (*node) = 0;
380 layout_decl (*node, 0);
389 old_attrs = DECL_ATTRIBUTES (*anode);
391 old_attrs = TYPE_ATTRIBUTES (*anode);
393 for (a = lookup_attribute (spec->name, old_attrs);
395 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
397 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
403 /* This attribute isn't already in the list. */
405 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
406 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
407 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
409 *anode = build_type_attribute_variant (*anode,
410 tree_cons (name, args,
416 return returned_attrs;
419 /* Handle a "packed" attribute; arguments as in
420 struct attribute_spec.handler. */
423 handle_packed_attribute (node, name, args, flags, no_add_attrs)
426 tree args ATTRIBUTE_UNUSED;
433 if (TREE_CODE (*node) == TYPE_DECL)
434 type = &TREE_TYPE (*node);
441 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
442 *type = build_type_copy (*type);
443 TYPE_PACKED (*type) = 1;
445 else if (TREE_CODE (*node) == FIELD_DECL)
446 DECL_PACKED (*node) = 1;
447 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
448 used for DECL_REGISTER. It wouldn't mean anything anyway. */
451 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
452 *no_add_attrs = true;
458 /* Handle a "nocommon" attribute; arguments as in
459 struct attribute_spec.handler. */
462 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
465 tree args ATTRIBUTE_UNUSED;
466 int flags ATTRIBUTE_UNUSED;
469 if (TREE_CODE (*node) == VAR_DECL)
470 DECL_COMMON (*node) = 0;
473 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
474 *no_add_attrs = true;
480 /* Handle a "common" attribute; arguments as in
481 struct attribute_spec.handler. */
484 handle_common_attribute (node, name, args, flags, no_add_attrs)
487 tree args ATTRIBUTE_UNUSED;
488 int flags ATTRIBUTE_UNUSED;
491 if (TREE_CODE (*node) == VAR_DECL)
492 DECL_COMMON (*node) = 1;
495 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
496 *no_add_attrs = true;
502 /* Handle a "noreturn" attribute; arguments as in
503 struct attribute_spec.handler. */
506 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
509 tree args ATTRIBUTE_UNUSED;
510 int flags ATTRIBUTE_UNUSED;
513 tree type = TREE_TYPE (*node);
515 /* See FIXME comment in c_common_attribute_table. */
516 if (TREE_CODE (*node) == FUNCTION_DECL)
517 TREE_THIS_VOLATILE (*node) = 1;
518 else if (TREE_CODE (type) == POINTER_TYPE
519 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
522 (build_type_variant (TREE_TYPE (type),
523 TREE_READONLY (TREE_TYPE (type)), 1));
526 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
527 *no_add_attrs = true;
533 /* Handle a "noinline" attribute; arguments as in
534 struct attribute_spec.handler. */
537 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
540 tree args ATTRIBUTE_UNUSED;
541 int flags ATTRIBUTE_UNUSED;
544 if (TREE_CODE (*node) == FUNCTION_DECL)
545 DECL_UNINLINABLE (*node) = 1;
548 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
549 *no_add_attrs = true;
555 /* Handle a "used" attribute; arguments as in
556 struct attribute_spec.handler. */
559 handle_used_attribute (node, name, args, flags, no_add_attrs)
562 tree args ATTRIBUTE_UNUSED;
563 int flags ATTRIBUTE_UNUSED;
566 if (TREE_CODE (*node) == FUNCTION_DECL)
567 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
568 = TREE_USED (*node) = 1;
571 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
572 *no_add_attrs = true;
578 /* Handle a "unused" attribute; arguments as in
579 struct attribute_spec.handler. */
582 handle_unused_attribute (node, name, args, flags, no_add_attrs)
585 tree args ATTRIBUTE_UNUSED;
593 if (TREE_CODE (decl) == PARM_DECL
594 || TREE_CODE (decl) == VAR_DECL
595 || TREE_CODE (decl) == FUNCTION_DECL
596 || TREE_CODE (decl) == LABEL_DECL
597 || TREE_CODE (decl) == TYPE_DECL)
598 TREE_USED (decl) = 1;
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 *no_add_attrs = true;
607 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
608 *node = build_type_copy (*node);
609 TREE_USED (*node) = 1;
615 /* Handle a "const" attribute; arguments as in
616 struct attribute_spec.handler. */
619 handle_const_attribute (node, name, args, flags, no_add_attrs)
622 tree args ATTRIBUTE_UNUSED;
623 int flags ATTRIBUTE_UNUSED;
626 tree type = TREE_TYPE (*node);
628 /* See FIXME comment on noreturn in c_common_attribute_table. */
629 if (TREE_CODE (*node) == FUNCTION_DECL)
630 TREE_READONLY (*node) = 1;
631 else if (TREE_CODE (type) == POINTER_TYPE
632 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
635 (build_type_variant (TREE_TYPE (type), 1,
636 TREE_THIS_VOLATILE (TREE_TYPE (type))));
639 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
640 *no_add_attrs = true;
646 /* Handle a "transparent_union" attribute; arguments as in
647 struct attribute_spec.handler. */
650 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
653 tree args ATTRIBUTE_UNUSED;
657 tree decl = NULL_TREE;
664 type = &TREE_TYPE (decl);
665 is_type = TREE_CODE (*node) == TYPE_DECL;
667 else if (TYPE_P (*node))
668 type = node, is_type = 1;
671 && TREE_CODE (*type) == UNION_TYPE
673 || (TYPE_FIELDS (*type) != 0
674 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
676 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
677 *type = build_type_copy (*type);
678 TYPE_TRANSPARENT_UNION (*type) = 1;
680 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
681 && TREE_CODE (*type) == UNION_TYPE
682 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
683 DECL_TRANSPARENT_UNION (decl) = 1;
686 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
687 *no_add_attrs = true;
693 /* Handle a "constructor" attribute; arguments as in
694 struct attribute_spec.handler. */
697 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
700 tree args ATTRIBUTE_UNUSED;
701 int flags ATTRIBUTE_UNUSED;
705 tree type = TREE_TYPE (decl);
707 if (TREE_CODE (decl) == FUNCTION_DECL
708 && TREE_CODE (type) == FUNCTION_TYPE
709 && decl_function_context (decl) == 0)
711 DECL_STATIC_CONSTRUCTOR (decl) = 1;
712 TREE_USED (decl) = 1;
716 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
717 *no_add_attrs = true;
723 /* Handle a "destructor" attribute; arguments as in
724 struct attribute_spec.handler. */
727 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
730 tree args ATTRIBUTE_UNUSED;
731 int flags ATTRIBUTE_UNUSED;
735 tree type = TREE_TYPE (decl);
737 if (TREE_CODE (decl) == FUNCTION_DECL
738 && TREE_CODE (type) == FUNCTION_TYPE
739 && decl_function_context (decl) == 0)
741 DECL_STATIC_DESTRUCTOR (decl) = 1;
742 TREE_USED (decl) = 1;
746 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
747 *no_add_attrs = true;
753 /* Handle a "mode" attribute; arguments as in
754 struct attribute_spec.handler. */
757 handle_mode_attribute (node, name, args, flags, no_add_attrs)
761 int flags ATTRIBUTE_UNUSED;
766 *no_add_attrs = true;
768 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
769 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
773 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
774 int len = strlen (p);
775 enum machine_mode mode = VOIDmode;
778 if (len > 4 && p[0] == '_' && p[1] == '_'
779 && p[len - 1] == '_' && p[len - 2] == '_')
781 char *newp = (char *) alloca (len - 1);
783 strcpy (newp, &p[2]);
784 newp[len - 4] = '\0';
788 /* Change this type to have a type with the specified mode.
789 First check for the special modes. */
790 if (! strcmp (p, "byte"))
792 else if (!strcmp (p, "word"))
794 else if (! strcmp (p, "pointer"))
797 for (j = 0; j < NUM_MACHINE_MODES; j++)
798 if (!strcmp (p, GET_MODE_NAME (j)))
799 mode = (enum machine_mode) j;
801 if (mode == VOIDmode)
802 error ("unknown machine mode `%s'", p);
803 else if (0 == (typefm = type_for_mode (mode,
804 TREE_UNSIGNED (type))))
805 error ("no data type for mode `%s'", p);
808 /* No need to layout the type here. The caller should do this. */
814 /* Handle a "section" attribute; arguments as in
815 struct attribute_spec.handler. */
818 handle_section_attribute (node, name, args, flags, no_add_attrs)
820 tree name ATTRIBUTE_UNUSED;
822 int flags ATTRIBUTE_UNUSED;
827 if (targetm.have_named_sections)
829 if ((TREE_CODE (decl) == FUNCTION_DECL
830 || TREE_CODE (decl) == VAR_DECL)
831 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
833 if (TREE_CODE (decl) == VAR_DECL
834 && current_function_decl != NULL_TREE
835 && ! TREE_STATIC (decl))
837 error_with_decl (decl,
838 "section attribute cannot be specified for local variables");
839 *no_add_attrs = true;
842 /* The decl may have already been given a section attribute
843 from a previous declaration. Ensure they match. */
844 else if (DECL_SECTION_NAME (decl) != NULL_TREE
845 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
846 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
848 error_with_decl (*node,
849 "section of `%s' conflicts with previous declaration");
850 *no_add_attrs = true;
853 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
857 error_with_decl (*node,
858 "section attribute not allowed for `%s'");
859 *no_add_attrs = true;
864 error_with_decl (*node,
865 "section attributes are not supported for this target");
866 *no_add_attrs = true;
872 /* Handle a "aligned" attribute; arguments as in
873 struct attribute_spec.handler. */
876 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
878 tree name ATTRIBUTE_UNUSED;
883 tree decl = NULL_TREE;
886 tree align_expr = (args ? TREE_VALUE (args)
887 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
893 type = &TREE_TYPE (decl);
894 is_type = TREE_CODE (*node) == TYPE_DECL;
896 else if (TYPE_P (*node))
897 type = node, is_type = 1;
899 /* Strip any NOPs of any kind. */
900 while (TREE_CODE (align_expr) == NOP_EXPR
901 || TREE_CODE (align_expr) == CONVERT_EXPR
902 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
903 align_expr = TREE_OPERAND (align_expr, 0);
905 if (TREE_CODE (align_expr) != INTEGER_CST)
907 error ("requested alignment is not a constant");
908 *no_add_attrs = true;
910 else if ((i = tree_log2 (align_expr)) == -1)
912 error ("requested alignment is not a power of 2");
913 *no_add_attrs = true;
915 else if (i > HOST_BITS_PER_INT - 2)
917 error ("requested alignment is too large");
918 *no_add_attrs = true;
922 /* If we have a TYPE_DECL, then copy the type, so that we
923 don't accidentally modify a builtin type. See pushdecl. */
924 if (decl && TREE_TYPE (decl) != error_mark_node
925 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
927 tree tt = TREE_TYPE (decl);
928 *type = build_type_copy (*type);
929 DECL_ORIGINAL_TYPE (decl) = tt;
930 TYPE_NAME (*type) = decl;
931 TREE_USED (*type) = TREE_USED (decl);
932 TREE_TYPE (decl) = *type;
934 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
935 *type = build_type_copy (*type);
937 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
938 TYPE_USER_ALIGN (*type) = 1;
940 else if (TREE_CODE (decl) != VAR_DECL
941 && TREE_CODE (decl) != FIELD_DECL)
943 error_with_decl (decl,
944 "alignment may not be specified for `%s'");
945 *no_add_attrs = true;
949 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
950 DECL_USER_ALIGN (decl) = 1;
956 /* Handle a "weak" attribute; arguments as in
957 struct attribute_spec.handler. */
960 handle_weak_attribute (node, name, args, flags, no_add_attrs)
962 tree name ATTRIBUTE_UNUSED;
963 tree args ATTRIBUTE_UNUSED;
964 int flags ATTRIBUTE_UNUSED;
965 bool *no_add_attrs ATTRIBUTE_UNUSED;
967 declare_weak (*node);
972 /* Handle an "alias" attribute; arguments as in
973 struct attribute_spec.handler. */
976 handle_alias_attribute (node, name, args, flags, no_add_attrs)
980 int flags ATTRIBUTE_UNUSED;
985 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
986 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
988 error_with_decl (decl,
989 "`%s' defined both normally and as an alias");
990 *no_add_attrs = true;
992 else if (decl_function_context (decl) == 0)
996 id = TREE_VALUE (args);
997 if (TREE_CODE (id) != STRING_CST)
999 error ("alias arg not a string");
1000 *no_add_attrs = true;
1003 id = get_identifier (TREE_STRING_POINTER (id));
1004 /* This counts as a use of the object pointed to. */
1007 if (TREE_CODE (decl) == FUNCTION_DECL)
1008 DECL_INITIAL (decl) = error_mark_node;
1010 DECL_EXTERNAL (decl) = 0;
1011 assemble_alias (decl, id);
1015 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1016 *no_add_attrs = true;
1022 /* Handle a "no_instrument_function" attribute; arguments as in
1023 struct attribute_spec.handler. */
1026 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1029 tree args ATTRIBUTE_UNUSED;
1030 int flags ATTRIBUTE_UNUSED;
1035 if (TREE_CODE (decl) != FUNCTION_DECL)
1037 error_with_decl (decl,
1038 "`%s' attribute applies only to functions",
1039 IDENTIFIER_POINTER (name));
1040 *no_add_attrs = true;
1042 else if (DECL_INITIAL (decl))
1044 error_with_decl (decl,
1045 "can't set `%s' attribute after definition",
1046 IDENTIFIER_POINTER (name));
1047 *no_add_attrs = true;
1050 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1055 /* Handle a "malloc" attribute; arguments as in
1056 struct attribute_spec.handler. */
1059 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1062 tree args ATTRIBUTE_UNUSED;
1063 int flags ATTRIBUTE_UNUSED;
1066 if (TREE_CODE (*node) == FUNCTION_DECL)
1067 DECL_IS_MALLOC (*node) = 1;
1068 /* ??? TODO: Support types. */
1071 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1072 *no_add_attrs = true;
1078 /* Handle a "no_limit_stack" attribute; arguments as in
1079 struct attribute_spec.handler. */
1082 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1085 tree args ATTRIBUTE_UNUSED;
1086 int flags ATTRIBUTE_UNUSED;
1091 if (TREE_CODE (decl) != FUNCTION_DECL)
1093 error_with_decl (decl,
1094 "`%s' attribute applies only to functions",
1095 IDENTIFIER_POINTER (name));
1096 *no_add_attrs = true;
1098 else if (DECL_INITIAL (decl))
1100 error_with_decl (decl,
1101 "can't set `%s' attribute after definition",
1102 IDENTIFIER_POINTER (name));
1103 *no_add_attrs = true;
1106 DECL_NO_LIMIT_STACK (decl) = 1;
1111 /* Handle a "pure" attribute; arguments as in
1112 struct attribute_spec.handler. */
1115 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1118 tree args ATTRIBUTE_UNUSED;
1119 int flags ATTRIBUTE_UNUSED;
1122 if (TREE_CODE (*node) == FUNCTION_DECL)
1123 DECL_IS_PURE (*node) = 1;
1124 /* ??? TODO: Support types. */
1127 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1128 *no_add_attrs = true;
1134 /* Handle a "vector_size" attribute; arguments as in
1135 struct attribute_spec.handler. */
1138 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1142 int flags ATTRIBUTE_UNUSED;
1145 unsigned int vecsize, nunits;
1146 enum machine_mode mode, orig_mode, new_mode;
1147 tree type = *node, new_type;
1149 *no_add_attrs = true;
1151 if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
1153 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1157 /* Get the vector size (in bytes). */
1158 vecsize = TREE_INT_CST_LOW (TREE_VALUE (args));
1160 /* We need to provide for vector pointers, vector arrays, and
1161 functions returning vectors. For example:
1163 __attribute__((vector_size(16))) short *foo;
1165 In this case, the mode is SI, but the type being modified is
1166 HI, so we need to look further. */
1168 while (POINTER_TYPE_P (type)
1169 || TREE_CODE (type) == FUNCTION_TYPE
1170 || TREE_CODE (type) == ARRAY_TYPE)
1171 type = TREE_TYPE (type);
1173 /* Get the mode of the type being modified. */
1174 orig_mode = TYPE_MODE (type);
1176 if (TREE_CODE (type) == RECORD_TYPE ||
1177 (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1178 && GET_MODE_CLASS (orig_mode) != MODE_INT))
1180 error ("invalid vector type for attribute `%s'",
1181 IDENTIFIER_POINTER (name));
1185 /* Calculate how many units fit in the vector. */
1186 nunits = vecsize / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
1188 /* Find a suitably sized vector. */
1189 new_mode = VOIDmode;
1190 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
1192 : MODE_VECTOR_FLOAT);
1194 mode = GET_MODE_WIDER_MODE (mode))
1195 if (vecsize == GET_MODE_SIZE (mode) && nunits == GET_MODE_NUNITS (mode))
1201 if (new_mode == VOIDmode)
1202 error ("no vector mode with the size and type specified could be found");
1205 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1207 error ("no vector mode with the size and type specified could be found");
1209 /* Build back pointers if needed. */
1210 *node = vector_size_helper (*node, new_type);
1216 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1219 If we requested a pointer to a vector, build up the pointers that
1220 we stripped off while looking for the inner type. Similarly for
1221 return values from functions.
1223 The argument "type" is the top of the chain, and "bottom" is the
1224 new type which we will point to. */
1227 vector_size_helper (type, bottom)
1232 if (POINTER_TYPE_P (type))
1234 inner = vector_size_helper (TREE_TYPE (type), bottom);
1235 outer = build_pointer_type (inner);
1237 else if (TREE_CODE (type) == ARRAY_TYPE)
1239 inner = vector_size_helper (TREE_TYPE (type), bottom);
1240 outer = build_array_type (inner, TYPE_VALUES (type));
1242 else if (TREE_CODE (type) == FUNCTION_TYPE)
1244 inner = vector_size_helper (TREE_TYPE (type), bottom);
1245 outer = build_function_type (inner, TYPE_VALUES (type));
1250 TREE_READONLY (outer) = TREE_READONLY (type);
1251 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1256 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1257 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1259 The head of the declspec list is stored in DECLSPECS.
1260 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1262 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1263 the list elements. We drop the containing TREE_LIST nodes and link the
1264 resulting attributes together the way decl_attributes expects them. */
1267 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1269 tree *declspecs, *prefix_attributes;
1271 tree t, s, a, next, specs, attrs;
1273 /* This can happen after an __extension__ in pedantic mode. */
1274 if (specs_attrs != NULL_TREE
1275 && TREE_CODE (specs_attrs) == INTEGER_CST)
1277 *declspecs = NULL_TREE;
1278 *prefix_attributes = NULL_TREE;
1282 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1283 if (specs_attrs != NULL_TREE
1284 && TREE_CODE (specs_attrs) != TREE_LIST)
1286 *declspecs = specs_attrs;
1287 *prefix_attributes = NULL_TREE;
1291 /* Remember to keep the lists in the same order, element-wise. */
1293 specs = s = NULL_TREE;
1294 attrs = a = NULL_TREE;
1295 for (t = specs_attrs; t; t = next)
1297 next = TREE_CHAIN (t);
1298 /* Declspecs have a non-NULL TREE_VALUE. */
1299 if (TREE_VALUE (t) != NULL_TREE)
1301 if (specs == NULL_TREE)
1309 /* The TREE_PURPOSE may also be empty in the case of
1310 __attribute__(()). */
1311 else if (TREE_PURPOSE (t) != NULL_TREE)
1313 if (attrs == NULL_TREE)
1314 attrs = a = TREE_PURPOSE (t);
1317 TREE_CHAIN (a) = TREE_PURPOSE (t);
1318 a = TREE_PURPOSE (t);
1320 /* More attrs can be linked here, move A to the end. */
1321 while (TREE_CHAIN (a) != NULL_TREE)
1326 /* Terminate the lists. */
1328 TREE_CHAIN (s) = NULL_TREE;
1330 TREE_CHAIN (a) = NULL_TREE;
1334 *prefix_attributes = attrs;
1337 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1338 This function is used by the parser when a rule will accept attributes
1339 in a particular position, but we don't want to support that just yet.
1341 A warning is issued for every ignored attribute. */
1344 strip_attrs (specs_attrs)
1349 split_specs_attrs (specs_attrs, &specs, &attrs);
1353 warning ("`%s' attribute ignored",
1354 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1355 attrs = TREE_CHAIN (attrs);