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_no_check_memory_usage_attribute
PARAMS ((tree
*, 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,
88 /* Table of machine-independent attributes common to all C-like languages. */
89 static const struct attribute_spec c_common_attribute_table
[] =
91 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
92 { "packed", 0, 0, false, false, false,
93 handle_packed_attribute
},
94 { "nocommon", 0, 0, true, false, false,
95 handle_nocommon_attribute
},
96 { "common", 0, 0, true, false, false,
97 handle_common_attribute
},
98 /* FIXME: logically, noreturn attributes should be listed as
99 "false, true, true" and apply to function types. But implementing this
100 would require all the places in the compiler that use TREE_THIS_VOLATILE
101 on a decl to identify non-returning functions to be located and fixed
102 to check the function type instead. */
103 { "noreturn", 0, 0, true, false, false,
104 handle_noreturn_attribute
},
105 { "volatile", 0, 0, true, false, false,
106 handle_noreturn_attribute
},
107 { "noinline", 0, 0, true, false, false,
108 handle_noinline_attribute
},
109 { "used", 0, 0, true, false, false,
110 handle_used_attribute
},
111 { "unused", 0, 0, false, false, false,
112 handle_unused_attribute
},
113 /* The same comments as for noreturn attributes apply to const ones. */
114 { "const", 0, 0, true, false, false,
115 handle_const_attribute
},
116 { "transparent_union", 0, 0, false, false, false,
117 handle_transparent_union_attribute
},
118 { "constructor", 0, 0, true, false, false,
119 handle_constructor_attribute
},
120 { "destructor", 0, 0, true, false, false,
121 handle_destructor_attribute
},
122 { "mode", 1, 1, true, false, false,
123 handle_mode_attribute
},
124 { "section", 1, 1, true, false, false,
125 handle_section_attribute
},
126 { "aligned", 0, 1, false, false, false,
127 handle_aligned_attribute
},
128 { "weak", 0, 0, true, false, false,
129 handle_weak_attribute
},
130 { "alias", 1, 1, true, false, false,
131 handle_alias_attribute
},
132 { "no_instrument_function", 0, 0, true, false, false,
133 handle_no_instrument_function_attribute
},
134 { "no_check_memory_usage", 0, 0, true, false, false,
135 handle_no_check_memory_usage_attribute
},
136 { "malloc", 0, 0, true, false, false,
137 handle_malloc_attribute
},
138 { "no_stack_limit", 0, 0, true, false, false,
139 handle_no_limit_stack_attribute
},
140 { "pure", 0, 0, true, false, false,
141 handle_pure_attribute
},
142 { NULL
, 0, 0, false, false, false, NULL
}
145 /* Default empty table of attributes. */
146 static const struct attribute_spec empty_attribute_table
[] =
148 { NULL
, 0, 0, false, false, false, NULL
}
151 /* Table of machine-independent attributes for checking formats, if used. */
152 const struct attribute_spec
*format_attribute_table
= empty_attribute_table
;
154 /* Table of machine-independent attributes for a particular language. */
155 const struct attribute_spec
*lang_attribute_table
= empty_attribute_table
;
157 /* Flag saying whether common language attributes are to be supported. */
158 int lang_attribute_common
= 1;
160 /* Initialize attribute tables, and make some sanity checks
161 if --enable-checking. */
166 #ifdef ENABLE_CHECKING
171 = lang_attribute_common
? c_common_attribute_table
: empty_attribute_table
;
172 attribute_tables
[1] = lang_attribute_table
;
173 attribute_tables
[2] = format_attribute_table
;
174 attribute_tables
[3] = targetm
.attribute_table
;
176 #ifdef ENABLE_CHECKING
177 /* Make some sanity checks on the attribute tables. */
179 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
184 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
186 /* The name must not begin and end with __. */
187 const char *name
= attribute_tables
[i
][j
].name
;
188 int len
= strlen (name
);
189 if (name
[0] == '_' && name
[1] == '_'
190 && name
[len
- 1] == '_' && name
[len
- 2] == '_')
192 /* The minimum and maximum lengths must be consistent. */
193 if (attribute_tables
[i
][j
].min_length
< 0)
195 if (attribute_tables
[i
][j
].max_length
!= -1
196 && (attribute_tables
[i
][j
].max_length
197 < attribute_tables
[i
][j
].min_length
))
199 /* An attribute cannot require both a DECL and a TYPE. */
200 if (attribute_tables
[i
][j
].decl_required
201 && attribute_tables
[i
][j
].type_required
)
203 /* If an attribute requires a function type, in particular
204 it requires a type. */
205 if (attribute_tables
[i
][j
].function_type_required
206 && !attribute_tables
[i
][j
].type_required
)
211 /* Check that each name occurs just once in each table. */
213 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
217 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
218 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
219 if (!strcmp (attribute_tables
[i
][j
].name
,
220 attribute_tables
[i
][k
].name
))
223 /* Check that no name occurs in more than one table. */
225 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
231 j
< ((int) (sizeof (attribute_tables
)
232 / sizeof (attribute_tables
[0])));
234 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
235 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
236 if (!strcmp (attribute_tables
[i
][k
].name
,
237 attribute_tables
[j
][l
].name
))
242 attributes_initialized
= true;
245 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
246 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
247 it should be modified in place; if a TYPE, a copy should be created
248 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
249 information, in the form of a bitwise OR of flags in enum attribute_flags
250 from tree.h. Depending on these flags, some attributes may be
251 returned to be applied at a later stage (for example, to apply
252 a decl attribute to the declaration rather than to its type). If
253 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
254 whether there might be some default attributes to apply to this DECL;
255 if so, decl_attributes will be called recusrively with those attributes
256 and ATTR_FLAG_BUILT_IN set. */
259 decl_attributes (node
, attributes
, flags
)
260 tree
*node
, attributes
;
264 tree returned_attrs
= NULL_TREE
;
266 if (!attributes_initialized
)
269 (*targetm
.insert_attributes
) (*node
, &attributes
);
271 if (DECL_P (*node
) && TREE_CODE (*node
) == FUNCTION_DECL
272 && !(flags
& (int) ATTR_FLAG_BUILT_IN
))
273 insert_default_attributes (*node
);
275 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
277 tree name
= TREE_PURPOSE (a
);
278 tree args
= TREE_VALUE (a
);
280 const struct attribute_spec
*spec
= NULL
;
281 bool no_add_attrs
= 0;
285 i
< ((int) (sizeof (attribute_tables
)
286 / sizeof (attribute_tables
[0])));
291 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
293 if (is_attribute_p (attribute_tables
[i
][j
].name
, name
))
295 spec
= &attribute_tables
[i
][j
];
305 warning ("`%s' attribute directive ignored",
306 IDENTIFIER_POINTER (name
));
309 else if (list_length (args
) < spec
->min_length
310 || (spec
->max_length
>= 0
311 && list_length (args
) > spec
->max_length
))
313 error ("wrong number of arguments specified for `%s' attribute",
314 IDENTIFIER_POINTER (name
));
318 if (spec
->decl_required
&& !DECL_P (*anode
))
320 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
321 | (int) ATTR_FLAG_FUNCTION_NEXT
322 | (int) ATTR_FLAG_ARRAY_NEXT
))
324 /* Pass on this attribute to be tried again. */
325 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
330 warning ("`%s' attribute does not apply to types",
331 IDENTIFIER_POINTER (name
));
336 if (spec
->type_required
&& DECL_P (*anode
))
337 anode
= &TREE_TYPE (*anode
);
339 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
340 && TREE_CODE (*anode
) != METHOD_TYPE
)
342 if (TREE_CODE (*anode
) == POINTER_TYPE
343 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
344 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
346 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
347 *anode
= build_type_copy (*anode
);
348 anode
= &TREE_TYPE (*anode
);
350 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
352 /* Pass on this attribute to be tried again. */
353 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
357 if (TREE_CODE (*anode
) != FUNCTION_TYPE
358 && TREE_CODE (*anode
) != METHOD_TYPE
)
360 warning ("`%s' attribute only applies to function types",
361 IDENTIFIER_POINTER (name
));
366 if (spec
->handler
!= NULL
)
367 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
368 flags
, &no_add_attrs
),
376 old_attrs
= DECL_ATTRIBUTES (*anode
);
378 old_attrs
= TYPE_ATTRIBUTES (*anode
);
380 for (a
= lookup_attribute (spec
->name
, old_attrs
);
382 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
384 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
390 /* This attribute isn't already in the list. */
392 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
393 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
394 TYPE_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
396 *anode
= build_type_attribute_variant (*anode
,
397 tree_cons (name
, args
,
403 return returned_attrs
;
406 /* Handle a "packed" attribute; arguments as in
407 struct attribute_spec.handler. */
410 handle_packed_attribute (node
, name
, args
, flags
, no_add_attrs
)
413 tree args ATTRIBUTE_UNUSED
;
420 if (TREE_CODE (*node
) == TYPE_DECL
)
421 type
= &TREE_TYPE (*node
);
428 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
429 *type
= build_type_copy (*type
);
430 TYPE_PACKED (*type
) = 1;
432 else if (TREE_CODE (*node
) == FIELD_DECL
)
433 DECL_PACKED (*node
) = 1;
434 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
435 used for DECL_REGISTER. It wouldn't mean anything anyway. */
438 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
439 *no_add_attrs
= true;
445 /* Handle a "nocommon" attribute; arguments as in
446 struct attribute_spec.handler. */
449 handle_nocommon_attribute (node
, name
, args
, flags
, no_add_attrs
)
452 tree args ATTRIBUTE_UNUSED
;
453 int flags ATTRIBUTE_UNUSED
;
456 if (TREE_CODE (*node
) == VAR_DECL
)
457 DECL_COMMON (*node
) = 0;
460 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
461 *no_add_attrs
= true;
467 /* Handle a "common" attribute; arguments as in
468 struct attribute_spec.handler. */
471 handle_common_attribute (node
, name
, args
, flags
, no_add_attrs
)
474 tree args ATTRIBUTE_UNUSED
;
475 int flags ATTRIBUTE_UNUSED
;
478 if (TREE_CODE (*node
) == VAR_DECL
)
479 DECL_COMMON (*node
) = 1;
482 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
483 *no_add_attrs
= true;
489 /* Handle a "noreturn" attribute; arguments as in
490 struct attribute_spec.handler. */
493 handle_noreturn_attribute (node
, name
, args
, flags
, no_add_attrs
)
496 tree args ATTRIBUTE_UNUSED
;
497 int flags ATTRIBUTE_UNUSED
;
500 tree type
= TREE_TYPE (*node
);
502 /* See FIXME comment in c_common_attribute_table. */
503 if (TREE_CODE (*node
) == FUNCTION_DECL
)
504 TREE_THIS_VOLATILE (*node
) = 1;
505 else if (TREE_CODE (type
) == POINTER_TYPE
506 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
509 (build_type_variant (TREE_TYPE (type
),
510 TREE_READONLY (TREE_TYPE (type
)), 1));
513 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
514 *no_add_attrs
= true;
520 /* Handle a "noinline" attribute; arguments as in
521 struct attribute_spec.handler. */
524 handle_noinline_attribute (node
, name
, args
, flags
, no_add_attrs
)
527 tree args ATTRIBUTE_UNUSED
;
528 int flags ATTRIBUTE_UNUSED
;
531 if (TREE_CODE (*node
) == FUNCTION_DECL
)
532 DECL_UNINLINABLE (*node
) = 1;
535 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
536 *no_add_attrs
= true;
542 /* Handle a "used" attribute; arguments as in
543 struct attribute_spec.handler. */
546 handle_used_attribute (node
, name
, args
, flags
, no_add_attrs
)
549 tree args ATTRIBUTE_UNUSED
;
550 int flags ATTRIBUTE_UNUSED
;
553 if (TREE_CODE (*node
) == FUNCTION_DECL
)
554 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node
))
555 = TREE_USED (*node
) = 1;
558 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
559 *no_add_attrs
= true;
565 /* Handle a "unused" attribute; arguments as in
566 struct attribute_spec.handler. */
569 handle_unused_attribute (node
, name
, args
, flags
, no_add_attrs
)
572 tree args ATTRIBUTE_UNUSED
;
580 if (TREE_CODE (decl
) == PARM_DECL
581 || TREE_CODE (decl
) == VAR_DECL
582 || TREE_CODE (decl
) == FUNCTION_DECL
583 || TREE_CODE (decl
) == LABEL_DECL
584 || TREE_CODE (decl
) == TYPE_DECL
)
585 TREE_USED (decl
) = 1;
588 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
589 *no_add_attrs
= true;
594 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
595 *node
= build_type_copy (*node
);
596 TREE_USED (*node
) = 1;
602 /* Handle a "const" attribute; arguments as in
603 struct attribute_spec.handler. */
606 handle_const_attribute (node
, name
, args
, flags
, no_add_attrs
)
609 tree args ATTRIBUTE_UNUSED
;
610 int flags ATTRIBUTE_UNUSED
;
613 tree type
= TREE_TYPE (*node
);
615 /* See FIXME comment on noreturn in c_common_attribute_table. */
616 if (TREE_CODE (*node
) == FUNCTION_DECL
)
617 TREE_READONLY (*node
) = 1;
618 else if (TREE_CODE (type
) == POINTER_TYPE
619 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
622 (build_type_variant (TREE_TYPE (type
), 1,
623 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
626 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
627 *no_add_attrs
= true;
633 /* Handle a "transparent_union" attribute; arguments as in
634 struct attribute_spec.handler. */
637 handle_transparent_union_attribute (node
, name
, args
, flags
, no_add_attrs
)
640 tree args ATTRIBUTE_UNUSED
;
644 tree decl
= NULL_TREE
;
651 type
= &TREE_TYPE (decl
);
652 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
654 else if (TYPE_P (*node
))
655 type
= node
, is_type
= 1;
658 && TREE_CODE (*type
) == UNION_TYPE
660 || (TYPE_FIELDS (*type
) != 0
661 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))))
663 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
664 *type
= build_type_copy (*type
);
665 TYPE_TRANSPARENT_UNION (*type
) = 1;
667 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
668 && TREE_CODE (*type
) == UNION_TYPE
669 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))
670 DECL_TRANSPARENT_UNION (decl
) = 1;
673 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
674 *no_add_attrs
= true;
680 /* Handle a "constructor" attribute; arguments as in
681 struct attribute_spec.handler. */
684 handle_constructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
687 tree args ATTRIBUTE_UNUSED
;
688 int flags ATTRIBUTE_UNUSED
;
692 tree type
= TREE_TYPE (decl
);
694 if (TREE_CODE (decl
) == FUNCTION_DECL
695 && TREE_CODE (type
) == FUNCTION_TYPE
696 && decl_function_context (decl
) == 0)
698 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
699 TREE_USED (decl
) = 1;
703 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
704 *no_add_attrs
= true;
710 /* Handle a "destructor" attribute; arguments as in
711 struct attribute_spec.handler. */
714 handle_destructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
717 tree args ATTRIBUTE_UNUSED
;
718 int flags ATTRIBUTE_UNUSED
;
722 tree type
= TREE_TYPE (decl
);
724 if (TREE_CODE (decl
) == FUNCTION_DECL
725 && TREE_CODE (type
) == FUNCTION_TYPE
726 && decl_function_context (decl
) == 0)
728 DECL_STATIC_DESTRUCTOR (decl
) = 1;
729 TREE_USED (decl
) = 1;
733 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
734 *no_add_attrs
= true;
740 /* Handle a "mode" attribute; arguments as in
741 struct attribute_spec.handler. */
744 handle_mode_attribute (node
, name
, args
, flags
, no_add_attrs
)
748 int flags ATTRIBUTE_UNUSED
;
752 tree type
= TREE_TYPE (decl
);
754 *no_add_attrs
= true;
756 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
757 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
761 const char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
762 int len
= strlen (p
);
763 enum machine_mode mode
= VOIDmode
;
766 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
767 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
769 char *newp
= (char *) alloca (len
- 1);
771 strcpy (newp
, &p
[2]);
772 newp
[len
- 4] = '\0';
776 /* Give this decl a type with the specified mode.
777 First check for the special modes. */
778 if (! strcmp (p
, "byte"))
780 else if (!strcmp (p
, "word"))
782 else if (! strcmp (p
, "pointer"))
785 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
786 if (!strcmp (p
, GET_MODE_NAME (j
)))
787 mode
= (enum machine_mode
) j
;
789 if (mode
== VOIDmode
)
790 error ("unknown machine mode `%s'", p
);
791 else if (0 == (typefm
= type_for_mode (mode
,
792 TREE_UNSIGNED (type
))))
793 error ("no data type for mode `%s'", p
);
796 TREE_TYPE (decl
) = type
= typefm
;
797 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = 0;
798 if (TREE_CODE (decl
) != FIELD_DECL
)
799 layout_decl (decl
, 0);
806 /* Handle a "section" attribute; arguments as in
807 struct attribute_spec.handler. */
810 handle_section_attribute (node
, name
, args
, flags
, no_add_attrs
)
812 tree name ATTRIBUTE_UNUSED
;
814 int flags ATTRIBUTE_UNUSED
;
819 if (targetm
.have_named_sections
)
821 if ((TREE_CODE (decl
) == FUNCTION_DECL
822 || TREE_CODE (decl
) == VAR_DECL
)
823 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
825 if (TREE_CODE (decl
) == VAR_DECL
826 && current_function_decl
!= NULL_TREE
827 && ! TREE_STATIC (decl
))
829 error_with_decl (decl
,
830 "section attribute cannot be specified for local variables");
831 *no_add_attrs
= true;
834 /* The decl may have already been given a section attribute
835 from a previous declaration. Ensure they match. */
836 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
837 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
838 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
840 error_with_decl (*node
,
841 "section of `%s' conflicts with previous declaration");
842 *no_add_attrs
= true;
845 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
849 error_with_decl (*node
,
850 "section attribute not allowed for `%s'");
851 *no_add_attrs
= true;
856 error_with_decl (*node
,
857 "section attributes are not supported for this target");
858 *no_add_attrs
= true;
864 /* Handle a "aligned" attribute; arguments as in
865 struct attribute_spec.handler. */
868 handle_aligned_attribute (node
, name
, args
, flags
, no_add_attrs
)
870 tree name ATTRIBUTE_UNUSED
;
875 tree decl
= NULL_TREE
;
878 tree align_expr
= (args
? TREE_VALUE (args
)
879 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
885 type
= &TREE_TYPE (decl
);
886 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
888 else if (TYPE_P (*node
))
889 type
= node
, is_type
= 1;
891 /* Strip any NOPs of any kind. */
892 while (TREE_CODE (align_expr
) == NOP_EXPR
893 || TREE_CODE (align_expr
) == CONVERT_EXPR
894 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
895 align_expr
= TREE_OPERAND (align_expr
, 0);
897 if (TREE_CODE (align_expr
) != INTEGER_CST
)
899 error ("requested alignment is not a constant");
900 *no_add_attrs
= true;
902 else if ((i
= tree_log2 (align_expr
)) == -1)
904 error ("requested alignment is not a power of 2");
905 *no_add_attrs
= true;
907 else if (i
> HOST_BITS_PER_INT
- 2)
909 error ("requested alignment is too large");
910 *no_add_attrs
= true;
914 /* If we have a TYPE_DECL, then copy the type, so that we
915 don't accidentally modify a builtin type. See pushdecl. */
916 if (decl
&& TREE_TYPE (decl
) != error_mark_node
917 && DECL_ORIGINAL_TYPE (decl
) == NULL_TREE
)
919 tree tt
= TREE_TYPE (decl
);
920 *type
= build_type_copy (*type
);
921 DECL_ORIGINAL_TYPE (decl
) = tt
;
922 TYPE_NAME (*type
) = decl
;
923 TREE_USED (*type
) = TREE_USED (decl
);
924 TREE_TYPE (decl
) = *type
;
926 else if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
927 *type
= build_type_copy (*type
);
929 TYPE_ALIGN (*type
) = (1 << i
) * BITS_PER_UNIT
;
930 TYPE_USER_ALIGN (*type
) = 1;
932 else if (TREE_CODE (decl
) != VAR_DECL
933 && TREE_CODE (decl
) != FIELD_DECL
)
935 error_with_decl (decl
,
936 "alignment may not be specified for `%s'");
937 *no_add_attrs
= true;
941 DECL_ALIGN (decl
) = (1 << i
) * BITS_PER_UNIT
;
942 DECL_USER_ALIGN (decl
) = 1;
948 /* Handle a "weak" attribute; arguments as in
949 struct attribute_spec.handler. */
952 handle_weak_attribute (node
, name
, args
, flags
, no_add_attrs
)
954 tree name ATTRIBUTE_UNUSED
;
955 tree args ATTRIBUTE_UNUSED
;
956 int flags ATTRIBUTE_UNUSED
;
957 bool *no_add_attrs ATTRIBUTE_UNUSED
;
959 declare_weak (*node
);
964 /* Handle an "alias" attribute; arguments as in
965 struct attribute_spec.handler. */
968 handle_alias_attribute (node
, name
, args
, flags
, no_add_attrs
)
972 int flags ATTRIBUTE_UNUSED
;
977 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
978 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
980 error_with_decl (decl
,
981 "`%s' defined both normally and as an alias");
982 *no_add_attrs
= true;
984 else if (decl_function_context (decl
) == 0)
988 id
= TREE_VALUE (args
);
989 if (TREE_CODE (id
) != STRING_CST
)
991 error ("alias arg not a string");
992 *no_add_attrs
= true;
995 id
= get_identifier (TREE_STRING_POINTER (id
));
996 /* This counts as a use of the object pointed to. */
999 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1000 DECL_INITIAL (decl
) = error_mark_node
;
1002 DECL_EXTERNAL (decl
) = 0;
1003 assemble_alias (decl
, id
);
1007 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1008 *no_add_attrs
= true;
1014 /* Handle a "no_instrument_function" attribute; arguments as in
1015 struct attribute_spec.handler. */
1018 handle_no_instrument_function_attribute (node
, name
, args
, flags
, no_add_attrs
)
1021 tree args ATTRIBUTE_UNUSED
;
1022 int flags ATTRIBUTE_UNUSED
;
1027 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1029 error_with_decl (decl
,
1030 "`%s' attribute applies only to functions",
1031 IDENTIFIER_POINTER (name
));
1032 *no_add_attrs
= true;
1034 else if (DECL_INITIAL (decl
))
1036 error_with_decl (decl
,
1037 "can't set `%s' attribute after definition",
1038 IDENTIFIER_POINTER (name
));
1039 *no_add_attrs
= true;
1042 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1047 /* Handle a "no_check_memory_usage" attribute; arguments as in
1048 struct attribute_spec.handler. */
1051 handle_no_check_memory_usage_attribute (node
, name
, args
, flags
, no_add_attrs
)
1054 tree args ATTRIBUTE_UNUSED
;
1055 int flags ATTRIBUTE_UNUSED
;
1060 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1062 error_with_decl (decl
,
1063 "`%s' attribute applies only to functions",
1064 IDENTIFIER_POINTER (name
));
1065 *no_add_attrs
= true;
1067 else if (DECL_INITIAL (decl
))
1069 error_with_decl (decl
,
1070 "can't set `%s' attribute after definition",
1071 IDENTIFIER_POINTER (name
));
1072 *no_add_attrs
= true;
1075 DECL_NO_CHECK_MEMORY_USAGE (decl
) = 1;
1080 /* Handle a "malloc" attribute; arguments as in
1081 struct attribute_spec.handler. */
1084 handle_malloc_attribute (node
, name
, args
, flags
, no_add_attrs
)
1087 tree args ATTRIBUTE_UNUSED
;
1088 int flags ATTRIBUTE_UNUSED
;
1091 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1092 DECL_IS_MALLOC (*node
) = 1;
1093 /* ??? TODO: Support types. */
1096 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1097 *no_add_attrs
= true;
1103 /* Handle a "no_limit_stack" attribute; arguments as in
1104 struct attribute_spec.handler. */
1107 handle_no_limit_stack_attribute (node
, name
, args
, flags
, no_add_attrs
)
1110 tree args ATTRIBUTE_UNUSED
;
1111 int flags ATTRIBUTE_UNUSED
;
1116 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1118 error_with_decl (decl
,
1119 "`%s' attribute applies only to functions",
1120 IDENTIFIER_POINTER (name
));
1121 *no_add_attrs
= true;
1123 else if (DECL_INITIAL (decl
))
1125 error_with_decl (decl
,
1126 "can't set `%s' attribute after definition",
1127 IDENTIFIER_POINTER (name
));
1128 *no_add_attrs
= true;
1131 DECL_NO_LIMIT_STACK (decl
) = 1;
1136 /* Handle a "pure" attribute; arguments as in
1137 struct attribute_spec.handler. */
1140 handle_pure_attribute (node
, name
, args
, flags
, no_add_attrs
)
1143 tree args ATTRIBUTE_UNUSED
;
1144 int flags ATTRIBUTE_UNUSED
;
1147 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1148 DECL_IS_PURE (*node
) = 1;
1149 /* ??? TODO: Support types. */
1152 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1153 *no_add_attrs
= true;
1159 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1160 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1162 The head of the declspec list is stored in DECLSPECS.
1163 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1165 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1166 the list elements. We drop the containing TREE_LIST nodes and link the
1167 resulting attributes together the way decl_attributes expects them. */
1170 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
1172 tree
*declspecs
, *prefix_attributes
;
1174 tree t
, s
, a
, next
, specs
, attrs
;
1176 /* This can happen after an __extension__ in pedantic mode. */
1177 if (specs_attrs
!= NULL_TREE
1178 && TREE_CODE (specs_attrs
) == INTEGER_CST
)
1180 *declspecs
= NULL_TREE
;
1181 *prefix_attributes
= NULL_TREE
;
1185 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1186 if (specs_attrs
!= NULL_TREE
1187 && TREE_CODE (specs_attrs
) != TREE_LIST
)
1189 *declspecs
= specs_attrs
;
1190 *prefix_attributes
= NULL_TREE
;
1194 /* Remember to keep the lists in the same order, element-wise. */
1196 specs
= s
= NULL_TREE
;
1197 attrs
= a
= NULL_TREE
;
1198 for (t
= specs_attrs
; t
; t
= next
)
1200 next
= TREE_CHAIN (t
);
1201 /* Declspecs have a non-NULL TREE_VALUE. */
1202 if (TREE_VALUE (t
) != NULL_TREE
)
1204 if (specs
== NULL_TREE
)
1212 /* The TREE_PURPOSE may also be empty in the case of
1213 __attribute__(()). */
1214 else if (TREE_PURPOSE (t
) != NULL_TREE
)
1216 if (attrs
== NULL_TREE
)
1217 attrs
= a
= TREE_PURPOSE (t
);
1220 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
1221 a
= TREE_PURPOSE (t
);
1223 /* More attrs can be linked here, move A to the end. */
1224 while (TREE_CHAIN (a
) != NULL_TREE
)
1229 /* Terminate the lists. */
1231 TREE_CHAIN (s
) = NULL_TREE
;
1233 TREE_CHAIN (a
) = NULL_TREE
;
1237 *prefix_attributes
= attrs
;
1240 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1241 This function is used by the parser when a rule will accept attributes
1242 in a particular position, but we don't want to support that just yet.
1244 A warning is issued for every ignored attribute. */
1247 strip_attrs (specs_attrs
)
1252 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
1256 warning ("`%s' attribute ignored",
1257 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
1258 attrs
= TREE_CHAIN (attrs
);