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. */
192 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
197 for (j = 0; attribute_tables[i][j].name != NULL; j++)
199 /* The name must not begin and end with __. */
200 const char *name = attribute_tables[i][j].name;
201 int len = strlen (name);
202 if (name[0] == '_' && name[1] == '_'
203 && name[len - 1] == '_' && name[len - 2] == '_')
205 /* The minimum and maximum lengths must be consistent. */
206 if (attribute_tables[i][j].min_length < 0)
208 if (attribute_tables[i][j].max_length != -1
209 && (attribute_tables[i][j].max_length
210 < attribute_tables[i][j].min_length))
212 /* An attribute cannot require both a DECL and a TYPE. */
213 if (attribute_tables[i][j].decl_required
214 && attribute_tables[i][j].type_required)
216 /* If an attribute requires a function type, in particular
217 it requires a type. */
218 if (attribute_tables[i][j].function_type_required
219 && !attribute_tables[i][j].type_required)
224 /* Check that each name occurs just once in each table. */
226 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
230 for (j = 0; attribute_tables[i][j].name != NULL; j++)
231 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
232 if (!strcmp (attribute_tables[i][j].name,
233 attribute_tables[i][k].name))
236 /* Check that no name occurs in more than one table. */
238 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
244 j < ((int) (sizeof (attribute_tables)
245 / sizeof (attribute_tables[0])));
247 for (k = 0; attribute_tables[i][k].name != NULL; k++)
248 for (l = 0; attribute_tables[j][l].name != NULL; l++)
249 if (!strcmp (attribute_tables[i][k].name,
250 attribute_tables[j][l].name))
255 attributes_initialized = true;
258 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
259 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
260 it should be modified in place; if a TYPE, a copy should be created
261 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
262 information, in the form of a bitwise OR of flags in enum attribute_flags
263 from tree.h. Depending on these flags, some attributes may be
264 returned to be applied at a later stage (for example, to apply
265 a decl attribute to the declaration rather than to its type). If
266 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
267 whether there might be some default attributes to apply to this DECL;
268 if so, decl_attributes will be called recursively with those attributes
269 and ATTR_FLAG_BUILT_IN set. */
272 decl_attributes (node, attributes, flags)
273 tree *node, attributes;
277 tree returned_attrs = NULL_TREE;
279 if (!attributes_initialized)
282 (*targetm.insert_attributes) (*node, &attributes);
284 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
285 && !(flags & (int) ATTR_FLAG_BUILT_IN))
286 insert_default_attributes (*node);
288 for (a = attributes; a; a = TREE_CHAIN (a))
290 tree name = TREE_PURPOSE (a);
291 tree args = TREE_VALUE (a);
293 const struct attribute_spec *spec = NULL;
294 bool no_add_attrs = 0;
298 i < ((int) (sizeof (attribute_tables)
299 / sizeof (attribute_tables[0])));
304 for (j = 0; attribute_tables[i][j].name != NULL; j++)
306 if (is_attribute_p (attribute_tables[i][j].name, name))
308 spec = &attribute_tables[i][j];
318 warning ("`%s' attribute directive ignored",
319 IDENTIFIER_POINTER (name));
322 else if (list_length (args) < spec->min_length
323 || (spec->max_length >= 0
324 && list_length (args) > spec->max_length))
326 error ("wrong number of arguments specified for `%s' attribute",
327 IDENTIFIER_POINTER (name));
331 if (spec->decl_required && !DECL_P (*anode))
333 if (flags & ((int) ATTR_FLAG_DECL_NEXT
334 | (int) ATTR_FLAG_FUNCTION_NEXT
335 | (int) ATTR_FLAG_ARRAY_NEXT))
337 /* Pass on this attribute to be tried again. */
338 returned_attrs = tree_cons (name, args, returned_attrs);
343 warning ("`%s' attribute does not apply to types",
344 IDENTIFIER_POINTER (name));
349 /* If we require a type, but were passed a decl, set up to make a
350 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
351 would have applied if we'd been passed a type, but we cannot modify
352 the decl's type in place here. */
353 if (spec->type_required && DECL_P (*anode))
355 anode = &TREE_TYPE (*anode);
356 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
359 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
360 && TREE_CODE (*anode) != METHOD_TYPE)
362 if (TREE_CODE (*anode) == POINTER_TYPE
363 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
364 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
366 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
367 *anode = build_type_copy (*anode);
368 anode = &TREE_TYPE (*anode);
370 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
372 /* Pass on this attribute to be tried again. */
373 returned_attrs = tree_cons (name, args, returned_attrs);
377 if (TREE_CODE (*anode) != FUNCTION_TYPE
378 && TREE_CODE (*anode) != METHOD_TYPE)
380 warning ("`%s' attribute only applies to function types",
381 IDENTIFIER_POINTER (name));
386 if (spec->handler != NULL)
387 returned_attrs = chainon ((*spec->handler) (anode, name, args,
388 flags, &no_add_attrs),
391 /* Layout the decl in case anything changed. */
392 if (spec->type_required && DECL_P (*node)
393 && (TREE_CODE (*node) == VAR_DECL
394 || TREE_CODE (*node) == PARM_DECL
395 || TREE_CODE (*node) == RESULT_DECL))
397 /* Force a recalculation of mode and size. */
398 DECL_MODE (*node) = VOIDmode;
399 DECL_SIZE (*node) = 0;
401 layout_decl (*node, 0);
410 old_attrs = DECL_ATTRIBUTES (*anode);
412 old_attrs = TYPE_ATTRIBUTES (*anode);
414 for (a = lookup_attribute (spec->name, old_attrs);
416 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
418 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
424 /* This attribute isn't already in the list. */
426 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
427 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
428 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
430 *anode = build_type_attribute_variant (*anode,
431 tree_cons (name, args,
437 return returned_attrs;
440 /* Handle a "packed" attribute; arguments as in
441 struct attribute_spec.handler. */
444 handle_packed_attribute (node, name, args, flags, no_add_attrs)
447 tree args ATTRIBUTE_UNUSED;
454 if (TREE_CODE (*node) == TYPE_DECL)
455 type = &TREE_TYPE (*node);
462 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
463 *type = build_type_copy (*type);
464 TYPE_PACKED (*type) = 1;
466 else if (TREE_CODE (*node) == FIELD_DECL)
467 DECL_PACKED (*node) = 1;
468 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
469 used for DECL_REGISTER. It wouldn't mean anything anyway. */
472 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
473 *no_add_attrs = true;
479 /* Handle a "nocommon" attribute; arguments as in
480 struct attribute_spec.handler. */
483 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
486 tree args ATTRIBUTE_UNUSED;
487 int flags ATTRIBUTE_UNUSED;
490 if (TREE_CODE (*node) == VAR_DECL)
491 DECL_COMMON (*node) = 0;
494 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
495 *no_add_attrs = true;
501 /* Handle a "common" attribute; arguments as in
502 struct attribute_spec.handler. */
505 handle_common_attribute (node, name, args, flags, no_add_attrs)
508 tree args ATTRIBUTE_UNUSED;
509 int flags ATTRIBUTE_UNUSED;
512 if (TREE_CODE (*node) == VAR_DECL)
513 DECL_COMMON (*node) = 1;
516 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
517 *no_add_attrs = true;
523 /* Handle a "noreturn" attribute; arguments as in
524 struct attribute_spec.handler. */
527 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
530 tree args ATTRIBUTE_UNUSED;
531 int flags ATTRIBUTE_UNUSED;
534 tree type = TREE_TYPE (*node);
536 /* See FIXME comment in c_common_attribute_table. */
537 if (TREE_CODE (*node) == FUNCTION_DECL)
538 TREE_THIS_VOLATILE (*node) = 1;
539 else if (TREE_CODE (type) == POINTER_TYPE
540 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
543 (build_type_variant (TREE_TYPE (type),
544 TREE_READONLY (TREE_TYPE (type)), 1));
547 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
548 *no_add_attrs = true;
554 /* Handle a "noinline" attribute; arguments as in
555 struct attribute_spec.handler. */
558 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
561 tree args ATTRIBUTE_UNUSED;
562 int flags ATTRIBUTE_UNUSED;
565 if (TREE_CODE (*node) == FUNCTION_DECL)
566 DECL_UNINLINABLE (*node) = 1;
569 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
570 *no_add_attrs = true;
576 /* Handle a "always_inline" attribute; arguments as in
577 struct attribute_spec.handler. */
580 handle_always_inline_attribute (node, name, args, flags, no_add_attrs)
583 tree args ATTRIBUTE_UNUSED;
584 int flags ATTRIBUTE_UNUSED;
587 if (TREE_CODE (*node) == FUNCTION_DECL)
589 /* Do nothing else, just set the attribute. We'll get at
590 it later with lookup_attribute. */
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
595 *no_add_attrs = true;
601 /* Handle a "used" attribute; arguments as in
602 struct attribute_spec.handler. */
605 handle_used_attribute (node, name, args, flags, no_add_attrs)
608 tree args ATTRIBUTE_UNUSED;
609 int flags ATTRIBUTE_UNUSED;
612 if (TREE_CODE (*node) == FUNCTION_DECL)
613 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
614 = TREE_USED (*node) = 1;
617 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
618 *no_add_attrs = true;
624 /* Handle a "unused" attribute; arguments as in
625 struct attribute_spec.handler. */
628 handle_unused_attribute (node, name, args, flags, no_add_attrs)
631 tree args ATTRIBUTE_UNUSED;
639 if (TREE_CODE (decl) == PARM_DECL
640 || TREE_CODE (decl) == VAR_DECL
641 || TREE_CODE (decl) == FUNCTION_DECL
642 || TREE_CODE (decl) == LABEL_DECL
643 || TREE_CODE (decl) == TYPE_DECL)
644 TREE_USED (decl) = 1;
647 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
648 *no_add_attrs = true;
653 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
654 *node = build_type_copy (*node);
655 TREE_USED (*node) = 1;
661 /* Handle a "const" attribute; arguments as in
662 struct attribute_spec.handler. */
665 handle_const_attribute (node, name, args, flags, no_add_attrs)
668 tree args ATTRIBUTE_UNUSED;
669 int flags ATTRIBUTE_UNUSED;
672 tree type = TREE_TYPE (*node);
674 /* See FIXME comment on noreturn in c_common_attribute_table. */
675 if (TREE_CODE (*node) == FUNCTION_DECL)
676 TREE_READONLY (*node) = 1;
677 else if (TREE_CODE (type) == POINTER_TYPE
678 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
681 (build_type_variant (TREE_TYPE (type), 1,
682 TREE_THIS_VOLATILE (TREE_TYPE (type))));
685 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
686 *no_add_attrs = true;
692 /* Handle a "transparent_union" attribute; arguments as in
693 struct attribute_spec.handler. */
696 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
699 tree args ATTRIBUTE_UNUSED;
703 tree decl = NULL_TREE;
710 type = &TREE_TYPE (decl);
711 is_type = TREE_CODE (*node) == TYPE_DECL;
713 else if (TYPE_P (*node))
714 type = node, is_type = 1;
717 && TREE_CODE (*type) == UNION_TYPE
719 || (TYPE_FIELDS (*type) != 0
720 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
722 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
723 *type = build_type_copy (*type);
724 TYPE_TRANSPARENT_UNION (*type) = 1;
726 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
727 && TREE_CODE (*type) == UNION_TYPE
728 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
729 DECL_TRANSPARENT_UNION (decl) = 1;
732 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
733 *no_add_attrs = true;
739 /* Handle a "constructor" attribute; arguments as in
740 struct attribute_spec.handler. */
743 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
746 tree args ATTRIBUTE_UNUSED;
747 int flags ATTRIBUTE_UNUSED;
751 tree type = TREE_TYPE (decl);
753 if (TREE_CODE (decl) == FUNCTION_DECL
754 && TREE_CODE (type) == FUNCTION_TYPE
755 && decl_function_context (decl) == 0)
757 DECL_STATIC_CONSTRUCTOR (decl) = 1;
758 TREE_USED (decl) = 1;
762 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
763 *no_add_attrs = true;
769 /* Handle a "destructor" attribute; arguments as in
770 struct attribute_spec.handler. */
773 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
776 tree args ATTRIBUTE_UNUSED;
777 int flags ATTRIBUTE_UNUSED;
781 tree type = TREE_TYPE (decl);
783 if (TREE_CODE (decl) == FUNCTION_DECL
784 && TREE_CODE (type) == FUNCTION_TYPE
785 && decl_function_context (decl) == 0)
787 DECL_STATIC_DESTRUCTOR (decl) = 1;
788 TREE_USED (decl) = 1;
792 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
793 *no_add_attrs = true;
799 /* Handle a "mode" attribute; arguments as in
800 struct attribute_spec.handler. */
803 handle_mode_attribute (node, name, args, flags, no_add_attrs)
807 int flags ATTRIBUTE_UNUSED;
812 *no_add_attrs = true;
814 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
815 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
819 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
820 int len = strlen (p);
821 enum machine_mode mode = VOIDmode;
824 if (len > 4 && p[0] == '_' && p[1] == '_'
825 && p[len - 1] == '_' && p[len - 2] == '_')
827 char *newp = (char *) alloca (len - 1);
829 strcpy (newp, &p[2]);
830 newp[len - 4] = '\0';
834 /* Change this type to have a type with the specified mode.
835 First check for the special modes. */
836 if (! strcmp (p, "byte"))
838 else if (!strcmp (p, "word"))
840 else if (! strcmp (p, "pointer"))
843 for (j = 0; j < NUM_MACHINE_MODES; j++)
844 if (!strcmp (p, GET_MODE_NAME (j)))
845 mode = (enum machine_mode) j;
847 if (mode == VOIDmode)
848 error ("unknown machine mode `%s'", p);
849 else if (0 == (typefm = type_for_mode (mode,
850 TREE_UNSIGNED (type))))
851 error ("no data type for mode `%s'", p);
854 /* No need to layout the type here. The caller should do this. */
860 /* Handle a "section" attribute; arguments as in
861 struct attribute_spec.handler. */
864 handle_section_attribute (node, name, args, flags, no_add_attrs)
866 tree name ATTRIBUTE_UNUSED;
868 int flags ATTRIBUTE_UNUSED;
873 if (targetm.have_named_sections)
875 if ((TREE_CODE (decl) == FUNCTION_DECL
876 || TREE_CODE (decl) == VAR_DECL)
877 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
879 if (TREE_CODE (decl) == VAR_DECL
880 && current_function_decl != NULL_TREE
881 && ! TREE_STATIC (decl))
883 error_with_decl (decl,
884 "section attribute cannot be specified for local variables");
885 *no_add_attrs = true;
888 /* The decl may have already been given a section attribute
889 from a previous declaration. Ensure they match. */
890 else if (DECL_SECTION_NAME (decl) != NULL_TREE
891 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
892 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
894 error_with_decl (*node,
895 "section of `%s' conflicts with previous declaration");
896 *no_add_attrs = true;
899 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
903 error_with_decl (*node,
904 "section attribute not allowed for `%s'");
905 *no_add_attrs = true;
910 error_with_decl (*node,
911 "section attributes are not supported for this target");
912 *no_add_attrs = true;
918 /* Handle a "aligned" attribute; arguments as in
919 struct attribute_spec.handler. */
922 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
924 tree name ATTRIBUTE_UNUSED;
929 tree decl = NULL_TREE;
932 tree align_expr = (args ? TREE_VALUE (args)
933 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
939 type = &TREE_TYPE (decl);
940 is_type = TREE_CODE (*node) == TYPE_DECL;
942 else if (TYPE_P (*node))
943 type = node, is_type = 1;
945 /* Strip any NOPs of any kind. */
946 while (TREE_CODE (align_expr) == NOP_EXPR
947 || TREE_CODE (align_expr) == CONVERT_EXPR
948 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
949 align_expr = TREE_OPERAND (align_expr, 0);
951 if (TREE_CODE (align_expr) != INTEGER_CST)
953 error ("requested alignment is not a constant");
954 *no_add_attrs = true;
956 else if ((i = tree_log2 (align_expr)) == -1)
958 error ("requested alignment is not a power of 2");
959 *no_add_attrs = true;
961 else if (i > HOST_BITS_PER_INT - 2)
963 error ("requested alignment is too large");
964 *no_add_attrs = true;
968 /* If we have a TYPE_DECL, then copy the type, so that we
969 don't accidentally modify a builtin type. See pushdecl. */
970 if (decl && TREE_TYPE (decl) != error_mark_node
971 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
973 tree tt = TREE_TYPE (decl);
974 *type = build_type_copy (*type);
975 DECL_ORIGINAL_TYPE (decl) = tt;
976 TYPE_NAME (*type) = decl;
977 TREE_USED (*type) = TREE_USED (decl);
978 TREE_TYPE (decl) = *type;
980 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
981 *type = build_type_copy (*type);
983 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
984 TYPE_USER_ALIGN (*type) = 1;
986 else if (TREE_CODE (decl) != VAR_DECL
987 && TREE_CODE (decl) != FIELD_DECL)
989 error_with_decl (decl,
990 "alignment may not be specified for `%s'");
991 *no_add_attrs = true;
995 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
996 DECL_USER_ALIGN (decl) = 1;
1002 /* Handle a "weak" attribute; arguments as in
1003 struct attribute_spec.handler. */
1006 handle_weak_attribute (node, name, args, flags, no_add_attrs)
1008 tree name ATTRIBUTE_UNUSED;
1009 tree args ATTRIBUTE_UNUSED;
1010 int flags ATTRIBUTE_UNUSED;
1011 bool *no_add_attrs ATTRIBUTE_UNUSED;
1013 declare_weak (*node);
1018 /* Handle an "alias" attribute; arguments as in
1019 struct attribute_spec.handler. */
1022 handle_alias_attribute (node, name, args, flags, no_add_attrs)
1026 int flags ATTRIBUTE_UNUSED;
1031 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1032 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1034 error_with_decl (decl,
1035 "`%s' defined both normally and as an alias");
1036 *no_add_attrs = true;
1038 else if (decl_function_context (decl) == 0)
1042 id = TREE_VALUE (args);
1043 if (TREE_CODE (id) != STRING_CST)
1045 error ("alias arg not a string");
1046 *no_add_attrs = true;
1049 id = get_identifier (TREE_STRING_POINTER (id));
1050 /* This counts as a use of the object pointed to. */
1053 if (TREE_CODE (decl) == FUNCTION_DECL)
1054 DECL_INITIAL (decl) = error_mark_node;
1056 DECL_EXTERNAL (decl) = 0;
1060 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1061 *no_add_attrs = true;
1067 /* Handle an "visibility" attribute; arguments as in
1068 struct attribute_spec.handler. */
1071 handle_visibility_attribute (node, name, args, flags, no_add_attrs)
1075 int flags ATTRIBUTE_UNUSED;
1080 if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
1082 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1083 *no_add_attrs = true;
1089 id = TREE_VALUE (args);
1090 if (TREE_CODE (id) != STRING_CST)
1092 error ("visibility arg not a string");
1093 *no_add_attrs = true;
1096 if (strcmp (TREE_STRING_POINTER (id), "hidden")
1097 && strcmp (TREE_STRING_POINTER (id), "protected")
1098 && strcmp (TREE_STRING_POINTER (id), "internal"))
1100 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
1101 *no_add_attrs = true;
1109 /* Handle a "no_instrument_function" attribute; arguments as in
1110 struct attribute_spec.handler. */
1113 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1116 tree args ATTRIBUTE_UNUSED;
1117 int flags ATTRIBUTE_UNUSED;
1122 if (TREE_CODE (decl) != FUNCTION_DECL)
1124 error_with_decl (decl,
1125 "`%s' attribute applies only to functions",
1126 IDENTIFIER_POINTER (name));
1127 *no_add_attrs = true;
1129 else if (DECL_INITIAL (decl))
1131 error_with_decl (decl,
1132 "can't set `%s' attribute after definition",
1133 IDENTIFIER_POINTER (name));
1134 *no_add_attrs = true;
1137 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1142 /* Handle a "malloc" attribute; arguments as in
1143 struct attribute_spec.handler. */
1146 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1149 tree args ATTRIBUTE_UNUSED;
1150 int flags ATTRIBUTE_UNUSED;
1153 if (TREE_CODE (*node) == FUNCTION_DECL)
1154 DECL_IS_MALLOC (*node) = 1;
1155 /* ??? TODO: Support types. */
1158 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1159 *no_add_attrs = true;
1165 /* Handle a "no_limit_stack" attribute; arguments as in
1166 struct attribute_spec.handler. */
1169 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1172 tree args ATTRIBUTE_UNUSED;
1173 int flags ATTRIBUTE_UNUSED;
1178 if (TREE_CODE (decl) != FUNCTION_DECL)
1180 error_with_decl (decl,
1181 "`%s' attribute applies only to functions",
1182 IDENTIFIER_POINTER (name));
1183 *no_add_attrs = true;
1185 else if (DECL_INITIAL (decl))
1187 error_with_decl (decl,
1188 "can't set `%s' attribute after definition",
1189 IDENTIFIER_POINTER (name));
1190 *no_add_attrs = true;
1193 DECL_NO_LIMIT_STACK (decl) = 1;
1198 /* Handle a "pure" attribute; arguments as in
1199 struct attribute_spec.handler. */
1202 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1205 tree args ATTRIBUTE_UNUSED;
1206 int flags ATTRIBUTE_UNUSED;
1209 if (TREE_CODE (*node) == FUNCTION_DECL)
1210 DECL_IS_PURE (*node) = 1;
1211 /* ??? TODO: Support types. */
1214 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1215 *no_add_attrs = true;
1221 /* Handle a "deprecated" attribute; arguments as in
1222 struct attribute_spec.handler. */
1225 handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
1228 tree args ATTRIBUTE_UNUSED;
1232 tree type = NULL_TREE;
1234 const char *what = NULL;
1239 type = TREE_TYPE (decl);
1241 if (TREE_CODE (decl) == TYPE_DECL
1242 || TREE_CODE (decl) == PARM_DECL
1243 || TREE_CODE (decl) == VAR_DECL
1244 || TREE_CODE (decl) == FUNCTION_DECL
1245 || TREE_CODE (decl) == FIELD_DECL)
1246 TREE_DEPRECATED (decl) = 1;
1250 else if (TYPE_P (*node))
1252 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1253 *node = build_type_copy (*node);
1254 TREE_DEPRECATED (*node) = 1;
1262 *no_add_attrs = true;
1263 if (type && TYPE_NAME (type))
1265 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1266 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
1267 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1268 && DECL_NAME (TYPE_NAME (type)))
1269 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1272 warning ("`%s' attribute ignored for `%s'",
1273 IDENTIFIER_POINTER (name), what);
1275 warning ("`%s' attribute ignored",
1276 IDENTIFIER_POINTER (name));
1282 /* Handle a "vector_size" attribute; arguments as in
1283 struct attribute_spec.handler. */
1286 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1290 int flags ATTRIBUTE_UNUSED;
1293 unsigned HOST_WIDE_INT vecsize, nunits;
1294 enum machine_mode mode, orig_mode, new_mode;
1295 tree type = *node, new_type;
1297 *no_add_attrs = true;
1299 if (! host_integerp (TREE_VALUE (args), 1))
1301 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1305 /* Get the vector size (in bytes). */
1306 vecsize = tree_low_cst (TREE_VALUE (args), 1);
1308 /* We need to provide for vector pointers, vector arrays, and
1309 functions returning vectors. For example:
1311 __attribute__((vector_size(16))) short *foo;
1313 In this case, the mode is SI, but the type being modified is
1314 HI, so we need to look further. */
1316 while (POINTER_TYPE_P (type)
1317 || TREE_CODE (type) == FUNCTION_TYPE
1318 || TREE_CODE (type) == ARRAY_TYPE)
1319 type = TREE_TYPE (type);
1321 /* Get the mode of the type being modified. */
1322 orig_mode = TYPE_MODE (type);
1324 if (TREE_CODE (type) == RECORD_TYPE
1325 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1326 && GET_MODE_CLASS (orig_mode) != MODE_INT)
1327 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
1329 error ("invalid vector type for attribute `%s'",
1330 IDENTIFIER_POINTER (name));
1334 /* Calculate how many units fit in the vector. */
1335 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1337 /* Find a suitably sized vector. */
1338 new_mode = VOIDmode;
1339 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
1341 : MODE_VECTOR_FLOAT);
1343 mode = GET_MODE_WIDER_MODE (mode))
1344 if (vecsize == GET_MODE_SIZE (mode)
1345 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
1351 if (new_mode == VOIDmode)
1352 error ("no vector mode with the size and type specified could be found");
1355 tree index, array, rt;
1357 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1361 error ("no vector mode with the size and type specified could be found");
1365 new_type = build_type_copy (new_type);
1367 /* Set the debug information here, because this is the only
1368 place where we know the underlying type for a vector made
1369 with vector_size. For debugging purposes we pretend a vector
1370 is an array within a structure. */
1371 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
1372 array = build_array_type (type, build_index_type (index));
1373 rt = make_node (RECORD_TYPE);
1375 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
1376 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
1378 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
1380 /* Build back pointers if needed. */
1381 *node = vector_size_helper (*node, new_type);
1387 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1390 If we requested a pointer to a vector, build up the pointers that
1391 we stripped off while looking for the inner type. Similarly for
1392 return values from functions.
1394 The argument "type" is the top of the chain, and "bottom" is the
1395 new type which we will point to. */
1398 vector_size_helper (type, bottom)
1403 if (POINTER_TYPE_P (type))
1405 inner = vector_size_helper (TREE_TYPE (type), bottom);
1406 outer = build_pointer_type (inner);
1408 else if (TREE_CODE (type) == ARRAY_TYPE)
1410 inner = vector_size_helper (TREE_TYPE (type), bottom);
1411 outer = build_array_type (inner, TYPE_VALUES (type));
1413 else if (TREE_CODE (type) == FUNCTION_TYPE)
1415 inner = vector_size_helper (TREE_TYPE (type), bottom);
1416 outer = build_function_type (inner, TYPE_VALUES (type));
1421 TREE_READONLY (outer) = TREE_READONLY (type);
1422 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1427 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1428 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1430 The head of the declspec list is stored in DECLSPECS.
1431 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1433 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1434 the list elements. We drop the containing TREE_LIST nodes and link the
1435 resulting attributes together the way decl_attributes expects them. */
1438 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1440 tree *declspecs, *prefix_attributes;
1442 tree t, s, a, next, specs, attrs;
1444 /* This can happen after an __extension__ in pedantic mode. */
1445 if (specs_attrs != NULL_TREE
1446 && TREE_CODE (specs_attrs) == INTEGER_CST)
1448 *declspecs = NULL_TREE;
1449 *prefix_attributes = NULL_TREE;
1453 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1454 if (specs_attrs != NULL_TREE
1455 && TREE_CODE (specs_attrs) != TREE_LIST)
1457 *declspecs = specs_attrs;
1458 *prefix_attributes = NULL_TREE;
1462 /* Remember to keep the lists in the same order, element-wise. */
1464 specs = s = NULL_TREE;
1465 attrs = a = NULL_TREE;
1466 for (t = specs_attrs; t; t = next)
1468 next = TREE_CHAIN (t);
1469 /* Declspecs have a non-NULL TREE_VALUE. */
1470 if (TREE_VALUE (t) != NULL_TREE)
1472 if (specs == NULL_TREE)
1480 /* The TREE_PURPOSE may also be empty in the case of
1481 __attribute__(()). */
1482 else if (TREE_PURPOSE (t) != NULL_TREE)
1484 if (attrs == NULL_TREE)
1485 attrs = a = TREE_PURPOSE (t);
1488 TREE_CHAIN (a) = TREE_PURPOSE (t);
1489 a = TREE_PURPOSE (t);
1491 /* More attrs can be linked here, move A to the end. */
1492 while (TREE_CHAIN (a) != NULL_TREE)
1497 /* Terminate the lists. */
1499 TREE_CHAIN (s) = NULL_TREE;
1501 TREE_CHAIN (a) = NULL_TREE;
1505 *prefix_attributes = attrs;
1508 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1509 This function is used by the parser when a rule will accept attributes
1510 in a particular position, but we don't want to support that just yet.
1512 A warning is issued for every ignored attribute. */
1515 strip_attrs (specs_attrs)
1520 split_specs_attrs (specs_attrs, &specs, &attrs);
1524 warning ("`%s' attribute ignored",
1525 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1526 attrs = TREE_CHAIN (attrs);