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
35 #include "langhooks.h"
37 static void init_attributes
PARAMS ((void));
39 /* Table of the tables of attributes (common, format, language, machine)
41 static const struct attribute_spec
*attribute_tables
[4];
43 static bool attributes_initialized
= false;
45 static tree handle_packed_attribute
PARAMS ((tree
*, tree
, tree
, int,
47 static tree handle_nocommon_attribute
PARAMS ((tree
*, tree
, tree
, int,
49 static tree handle_common_attribute
PARAMS ((tree
*, tree
, tree
, int,
51 static tree handle_noreturn_attribute
PARAMS ((tree
*, tree
, tree
, int,
53 static tree handle_noinline_attribute
PARAMS ((tree
*, tree
, tree
, int,
55 static tree handle_always_inline_attribute
PARAMS ((tree
*, tree
, tree
, int,
57 static tree handle_used_attribute
PARAMS ((tree
*, tree
, tree
, int,
59 static tree handle_unused_attribute
PARAMS ((tree
*, tree
, tree
, int,
61 static tree handle_const_attribute
PARAMS ((tree
*, tree
, tree
, int,
63 static tree handle_transparent_union_attribute
PARAMS ((tree
*, tree
, tree
,
65 static tree handle_constructor_attribute
PARAMS ((tree
*, tree
, tree
, int,
67 static tree handle_destructor_attribute
PARAMS ((tree
*, tree
, tree
, int,
69 static tree handle_mode_attribute
PARAMS ((tree
*, tree
, tree
, int,
71 static tree handle_section_attribute
PARAMS ((tree
*, tree
, tree
, int,
73 static tree handle_aligned_attribute
PARAMS ((tree
*, tree
, tree
, int,
75 static tree handle_weak_attribute
PARAMS ((tree
*, tree
, tree
, int,
77 static tree handle_alias_attribute
PARAMS ((tree
*, tree
, tree
, int,
79 static tree handle_visibility_attribute
PARAMS ((tree
*, tree
, tree
, int,
81 static tree handle_no_instrument_function_attribute
PARAMS ((tree
*, tree
,
84 static tree handle_malloc_attribute
PARAMS ((tree
*, tree
, tree
, int,
86 static tree handle_no_limit_stack_attribute
PARAMS ((tree
*, tree
, tree
, int,
88 static tree handle_pure_attribute
PARAMS ((tree
*, tree
, tree
, int,
90 static tree handle_deprecated_attribute
PARAMS ((tree
*, tree
, tree
, int,
92 static tree handle_vector_size_attribute
PARAMS ((tree
*, tree
, tree
, int,
94 static tree vector_size_helper
PARAMS ((tree
, tree
));
96 /* Table of machine-independent attributes common to all C-like languages. */
97 static const struct attribute_spec c_common_attribute_table
[] =
99 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
100 { "packed", 0, 0, false, false, false,
101 handle_packed_attribute
},
102 { "nocommon", 0, 0, true, false, false,
103 handle_nocommon_attribute
},
104 { "common", 0, 0, true, false, false,
105 handle_common_attribute
},
106 /* FIXME: logically, noreturn attributes should be listed as
107 "false, true, true" and apply to function types. But implementing this
108 would require all the places in the compiler that use TREE_THIS_VOLATILE
109 on a decl to identify non-returning functions to be located and fixed
110 to check the function type instead. */
111 { "noreturn", 0, 0, true, false, false,
112 handle_noreturn_attribute
},
113 { "volatile", 0, 0, true, false, false,
114 handle_noreturn_attribute
},
115 { "noinline", 0, 0, true, false, false,
116 handle_noinline_attribute
},
117 { "always_inline", 0, 0, true, false, false,
118 handle_always_inline_attribute
},
119 { "used", 0, 0, true, false, false,
120 handle_used_attribute
},
121 { "unused", 0, 0, false, false, false,
122 handle_unused_attribute
},
123 /* The same comments as for noreturn attributes apply to const ones. */
124 { "const", 0, 0, true, false, false,
125 handle_const_attribute
},
126 { "transparent_union", 0, 0, false, false, false,
127 handle_transparent_union_attribute
},
128 { "constructor", 0, 0, true, false, false,
129 handle_constructor_attribute
},
130 { "destructor", 0, 0, true, false, false,
131 handle_destructor_attribute
},
132 { "mode", 1, 1, false, true, false,
133 handle_mode_attribute
},
134 { "section", 1, 1, true, false, false,
135 handle_section_attribute
},
136 { "aligned", 0, 1, false, false, false,
137 handle_aligned_attribute
},
138 { "weak", 0, 0, true, false, false,
139 handle_weak_attribute
},
140 { "alias", 1, 1, true, false, false,
141 handle_alias_attribute
},
142 { "no_instrument_function", 0, 0, true, false, false,
143 handle_no_instrument_function_attribute
},
144 { "malloc", 0, 0, true, false, false,
145 handle_malloc_attribute
},
146 { "no_stack_limit", 0, 0, true, false, false,
147 handle_no_limit_stack_attribute
},
148 { "pure", 0, 0, true, false, false,
149 handle_pure_attribute
},
150 { "deprecated", 0, 0, false, false, false,
151 handle_deprecated_attribute
},
152 { "vector_size", 1, 1, false, true, false,
153 handle_vector_size_attribute
},
154 { "visibility", 1, 1, true, false, false,
155 handle_visibility_attribute
},
156 { NULL
, 0, 0, false, false, false, NULL
}
159 /* Default empty table of attributes. */
160 static const struct attribute_spec empty_attribute_table
[] =
162 { NULL
, 0, 0, false, false, false, NULL
}
165 /* Table of machine-independent attributes for checking formats, if used. */
166 const struct attribute_spec
*format_attribute_table
= empty_attribute_table
;
168 /* Table of machine-independent attributes for a particular language. */
169 const struct attribute_spec
*lang_attribute_table
= empty_attribute_table
;
171 /* Flag saying whether common language attributes are to be supported. */
172 int lang_attribute_common
= 1;
174 /* Initialize attribute tables, and make some sanity checks
175 if --enable-checking. */
180 #ifdef ENABLE_CHECKING
185 = lang_attribute_common
? c_common_attribute_table
: empty_attribute_table
;
186 attribute_tables
[1] = lang_attribute_table
;
187 attribute_tables
[2] = format_attribute_table
;
188 attribute_tables
[3] = targetm
.attribute_table
;
190 #ifdef ENABLE_CHECKING
191 /* Make some sanity checks on the attribute tables. */
192 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
196 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
198 /* The name must not begin and end with __. */
199 const char *name
= attribute_tables
[i
][j
].name
;
200 int len
= strlen (name
);
201 if (name
[0] == '_' && name
[1] == '_'
202 && name
[len
- 1] == '_' && name
[len
- 2] == '_')
204 /* The minimum and maximum lengths must be consistent. */
205 if (attribute_tables
[i
][j
].min_length
< 0)
207 if (attribute_tables
[i
][j
].max_length
!= -1
208 && (attribute_tables
[i
][j
].max_length
209 < attribute_tables
[i
][j
].min_length
))
211 /* An attribute cannot require both a DECL and a TYPE. */
212 if (attribute_tables
[i
][j
].decl_required
213 && attribute_tables
[i
][j
].type_required
)
215 /* If an attribute requires a function type, in particular
216 it requires a type. */
217 if (attribute_tables
[i
][j
].function_type_required
218 && !attribute_tables
[i
][j
].type_required
)
223 /* Check that each name occurs just once in each table. */
224 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
227 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
228 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
229 if (!strcmp (attribute_tables
[i
][j
].name
,
230 attribute_tables
[i
][k
].name
))
233 /* Check that no name occurs in more than one table. */
234 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
238 for (j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
239 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
240 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
241 if (!strcmp (attribute_tables
[i
][k
].name
,
242 attribute_tables
[j
][l
].name
))
247 attributes_initialized
= true;
250 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
251 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
252 it should be modified in place; if a TYPE, a copy should be created
253 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
254 information, in the form of a bitwise OR of flags in enum attribute_flags
255 from tree.h. Depending on these flags, some attributes may be
256 returned to be applied at a later stage (for example, to apply
257 a decl attribute to the declaration rather than to its type). If
258 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
259 whether there might be some default attributes to apply to this DECL;
260 if so, decl_attributes will be called recursively with those attributes
261 and ATTR_FLAG_BUILT_IN set. */
264 decl_attributes (node
, attributes
, flags
)
265 tree
*node
, attributes
;
269 tree returned_attrs
= NULL_TREE
;
271 if (!attributes_initialized
)
274 (*targetm
.insert_attributes
) (*node
, &attributes
);
276 if (DECL_P (*node
) && TREE_CODE (*node
) == FUNCTION_DECL
277 && !(flags
& (int) ATTR_FLAG_BUILT_IN
))
278 (*lang_hooks
.insert_default_attributes
) (*node
);
280 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
282 tree name
= TREE_PURPOSE (a
);
283 tree args
= TREE_VALUE (a
);
285 const struct attribute_spec
*spec
= NULL
;
286 bool no_add_attrs
= 0;
289 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
293 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
295 if (is_attribute_p (attribute_tables
[i
][j
].name
, name
))
297 spec
= &attribute_tables
[i
][j
];
307 warning ("`%s' attribute directive ignored",
308 IDENTIFIER_POINTER (name
));
311 else if (list_length (args
) < spec
->min_length
312 || (spec
->max_length
>= 0
313 && list_length (args
) > spec
->max_length
))
315 error ("wrong number of arguments specified for `%s' attribute",
316 IDENTIFIER_POINTER (name
));
320 if (spec
->decl_required
&& !DECL_P (*anode
))
322 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
323 | (int) ATTR_FLAG_FUNCTION_NEXT
324 | (int) ATTR_FLAG_ARRAY_NEXT
))
326 /* Pass on this attribute to be tried again. */
327 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
332 warning ("`%s' attribute does not apply to types",
333 IDENTIFIER_POINTER (name
));
338 /* If we require a type, but were passed a decl, set up to make a
339 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
340 would have applied if we'd been passed a type, but we cannot modify
341 the decl's type in place here. */
342 if (spec
->type_required
&& DECL_P (*anode
))
344 anode
= &TREE_TYPE (*anode
);
345 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
348 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
349 && TREE_CODE (*anode
) != METHOD_TYPE
)
351 if (TREE_CODE (*anode
) == POINTER_TYPE
352 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
353 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
355 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
356 *anode
= build_type_copy (*anode
);
357 anode
= &TREE_TYPE (*anode
);
359 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
361 /* Pass on this attribute to be tried again. */
362 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
366 if (TREE_CODE (*anode
) != FUNCTION_TYPE
367 && TREE_CODE (*anode
) != METHOD_TYPE
)
369 warning ("`%s' attribute only applies to function types",
370 IDENTIFIER_POINTER (name
));
375 if (spec
->handler
!= NULL
)
376 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
377 flags
, &no_add_attrs
),
380 /* Layout the decl in case anything changed. */
381 if (spec
->type_required
&& DECL_P (*node
)
382 && (TREE_CODE (*node
) == VAR_DECL
383 || TREE_CODE (*node
) == PARM_DECL
384 || TREE_CODE (*node
) == RESULT_DECL
))
386 /* Force a recalculation of mode and size. */
387 DECL_MODE (*node
) = VOIDmode
;
388 DECL_SIZE (*node
) = 0;
390 layout_decl (*node
, 0);
399 old_attrs
= DECL_ATTRIBUTES (*anode
);
401 old_attrs
= TYPE_ATTRIBUTES (*anode
);
403 for (a
= lookup_attribute (spec
->name
, old_attrs
);
405 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
407 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
413 /* This attribute isn't already in the list. */
415 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
416 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
417 TYPE_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
419 *anode
= build_type_attribute_variant (*anode
,
420 tree_cons (name
, args
,
426 return returned_attrs
;
429 /* Handle a "packed" attribute; arguments as in
430 struct attribute_spec.handler. */
433 handle_packed_attribute (node
, name
, args
, flags
, no_add_attrs
)
436 tree args ATTRIBUTE_UNUSED
;
443 if (TREE_CODE (*node
) == TYPE_DECL
)
444 type
= &TREE_TYPE (*node
);
451 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
452 *type
= build_type_copy (*type
);
453 TYPE_PACKED (*type
) = 1;
455 else if (TREE_CODE (*node
) == FIELD_DECL
)
456 DECL_PACKED (*node
) = 1;
457 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
458 used for DECL_REGISTER. It wouldn't mean anything anyway. */
461 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
462 *no_add_attrs
= true;
468 /* Handle a "nocommon" attribute; arguments as in
469 struct attribute_spec.handler. */
472 handle_nocommon_attribute (node
, name
, args
, flags
, no_add_attrs
)
475 tree args ATTRIBUTE_UNUSED
;
476 int flags ATTRIBUTE_UNUSED
;
479 if (TREE_CODE (*node
) == VAR_DECL
)
480 DECL_COMMON (*node
) = 0;
483 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
484 *no_add_attrs
= true;
490 /* Handle a "common" attribute; arguments as in
491 struct attribute_spec.handler. */
494 handle_common_attribute (node
, name
, args
, flags
, no_add_attrs
)
497 tree args ATTRIBUTE_UNUSED
;
498 int flags ATTRIBUTE_UNUSED
;
501 if (TREE_CODE (*node
) == VAR_DECL
)
502 DECL_COMMON (*node
) = 1;
505 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
506 *no_add_attrs
= true;
512 /* Handle a "noreturn" attribute; arguments as in
513 struct attribute_spec.handler. */
516 handle_noreturn_attribute (node
, name
, args
, flags
, no_add_attrs
)
519 tree args ATTRIBUTE_UNUSED
;
520 int flags ATTRIBUTE_UNUSED
;
523 tree type
= TREE_TYPE (*node
);
525 /* See FIXME comment in c_common_attribute_table. */
526 if (TREE_CODE (*node
) == FUNCTION_DECL
)
527 TREE_THIS_VOLATILE (*node
) = 1;
528 else if (TREE_CODE (type
) == POINTER_TYPE
529 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
532 (build_type_variant (TREE_TYPE (type
),
533 TREE_READONLY (TREE_TYPE (type
)), 1));
536 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
537 *no_add_attrs
= true;
543 /* Handle a "noinline" attribute; arguments as in
544 struct attribute_spec.handler. */
547 handle_noinline_attribute (node
, name
, args
, flags
, no_add_attrs
)
550 tree args ATTRIBUTE_UNUSED
;
551 int flags ATTRIBUTE_UNUSED
;
554 if (TREE_CODE (*node
) == FUNCTION_DECL
)
555 DECL_UNINLINABLE (*node
) = 1;
558 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
559 *no_add_attrs
= true;
565 /* Handle a "always_inline" attribute; arguments as in
566 struct attribute_spec.handler. */
569 handle_always_inline_attribute (node
, name
, args
, flags
, no_add_attrs
)
572 tree args ATTRIBUTE_UNUSED
;
573 int flags ATTRIBUTE_UNUSED
;
576 if (TREE_CODE (*node
) == FUNCTION_DECL
)
578 /* Do nothing else, just set the attribute. We'll get at
579 it later with lookup_attribute. */
583 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
584 *no_add_attrs
= true;
590 /* Handle a "used" attribute; arguments as in
591 struct attribute_spec.handler. */
594 handle_used_attribute (node
, name
, args
, flags
, no_add_attrs
)
597 tree args ATTRIBUTE_UNUSED
;
598 int flags ATTRIBUTE_UNUSED
;
601 if (TREE_CODE (*node
) == FUNCTION_DECL
)
602 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node
))
603 = TREE_USED (*node
) = 1;
606 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
607 *no_add_attrs
= true;
613 /* Handle a "unused" attribute; arguments as in
614 struct attribute_spec.handler. */
617 handle_unused_attribute (node
, name
, args
, flags
, no_add_attrs
)
620 tree args ATTRIBUTE_UNUSED
;
628 if (TREE_CODE (decl
) == PARM_DECL
629 || TREE_CODE (decl
) == VAR_DECL
630 || TREE_CODE (decl
) == FUNCTION_DECL
631 || TREE_CODE (decl
) == LABEL_DECL
632 || TREE_CODE (decl
) == TYPE_DECL
)
633 TREE_USED (decl
) = 1;
636 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
637 *no_add_attrs
= true;
642 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
643 *node
= build_type_copy (*node
);
644 TREE_USED (*node
) = 1;
650 /* Handle a "const" attribute; arguments as in
651 struct attribute_spec.handler. */
654 handle_const_attribute (node
, name
, args
, flags
, no_add_attrs
)
657 tree args ATTRIBUTE_UNUSED
;
658 int flags ATTRIBUTE_UNUSED
;
661 tree type
= TREE_TYPE (*node
);
663 /* See FIXME comment on noreturn in c_common_attribute_table. */
664 if (TREE_CODE (*node
) == FUNCTION_DECL
)
665 TREE_READONLY (*node
) = 1;
666 else if (TREE_CODE (type
) == POINTER_TYPE
667 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
670 (build_type_variant (TREE_TYPE (type
), 1,
671 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
674 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
675 *no_add_attrs
= true;
681 /* Handle a "transparent_union" attribute; arguments as in
682 struct attribute_spec.handler. */
685 handle_transparent_union_attribute (node
, name
, args
, flags
, no_add_attrs
)
688 tree args ATTRIBUTE_UNUSED
;
692 tree decl
= NULL_TREE
;
699 type
= &TREE_TYPE (decl
);
700 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
702 else if (TYPE_P (*node
))
703 type
= node
, is_type
= 1;
706 && TREE_CODE (*type
) == UNION_TYPE
708 || (TYPE_FIELDS (*type
) != 0
709 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))))
711 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
712 *type
= build_type_copy (*type
);
713 TYPE_TRANSPARENT_UNION (*type
) = 1;
715 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
716 && TREE_CODE (*type
) == UNION_TYPE
717 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))
718 DECL_TRANSPARENT_UNION (decl
) = 1;
721 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
722 *no_add_attrs
= true;
728 /* Handle a "constructor" attribute; arguments as in
729 struct attribute_spec.handler. */
732 handle_constructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
735 tree args ATTRIBUTE_UNUSED
;
736 int flags ATTRIBUTE_UNUSED
;
740 tree type
= TREE_TYPE (decl
);
742 if (TREE_CODE (decl
) == FUNCTION_DECL
743 && TREE_CODE (type
) == FUNCTION_TYPE
744 && decl_function_context (decl
) == 0)
746 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
747 TREE_USED (decl
) = 1;
751 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
752 *no_add_attrs
= true;
758 /* Handle a "destructor" attribute; arguments as in
759 struct attribute_spec.handler. */
762 handle_destructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
765 tree args ATTRIBUTE_UNUSED
;
766 int flags ATTRIBUTE_UNUSED
;
770 tree type
= TREE_TYPE (decl
);
772 if (TREE_CODE (decl
) == FUNCTION_DECL
773 && TREE_CODE (type
) == FUNCTION_TYPE
774 && decl_function_context (decl
) == 0)
776 DECL_STATIC_DESTRUCTOR (decl
) = 1;
777 TREE_USED (decl
) = 1;
781 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
782 *no_add_attrs
= true;
788 /* Handle a "mode" attribute; arguments as in
789 struct attribute_spec.handler. */
792 handle_mode_attribute (node
, name
, args
, flags
, no_add_attrs
)
796 int flags ATTRIBUTE_UNUSED
;
801 *no_add_attrs
= true;
803 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
804 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
808 const char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
809 int len
= strlen (p
);
810 enum machine_mode mode
= VOIDmode
;
813 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
814 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
816 char *newp
= (char *) alloca (len
- 1);
818 strcpy (newp
, &p
[2]);
819 newp
[len
- 4] = '\0';
823 /* Change this type to have a type with the specified mode.
824 First check for the special modes. */
825 if (! strcmp (p
, "byte"))
827 else if (!strcmp (p
, "word"))
829 else if (! strcmp (p
, "pointer"))
832 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
833 if (!strcmp (p
, GET_MODE_NAME (j
)))
834 mode
= (enum machine_mode
) j
;
836 if (mode
== VOIDmode
)
837 error ("unknown machine mode `%s'", p
);
838 else if (0 == (typefm
= (*lang_hooks
.types
.type_for_mode
)
839 (mode
, TREE_UNSIGNED (type
))))
840 error ("no data type for mode `%s'", p
);
843 /* No need to layout the type here. The caller should do this. */
849 /* Handle a "section" attribute; arguments as in
850 struct attribute_spec.handler. */
853 handle_section_attribute (node
, name
, args
, flags
, no_add_attrs
)
855 tree name ATTRIBUTE_UNUSED
;
857 int flags ATTRIBUTE_UNUSED
;
862 if (targetm
.have_named_sections
)
864 if ((TREE_CODE (decl
) == FUNCTION_DECL
865 || TREE_CODE (decl
) == VAR_DECL
)
866 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
868 if (TREE_CODE (decl
) == VAR_DECL
869 && current_function_decl
!= NULL_TREE
870 && ! TREE_STATIC (decl
))
872 error_with_decl (decl
,
873 "section attribute cannot be specified for local variables");
874 *no_add_attrs
= true;
877 /* The decl may have already been given a section attribute
878 from a previous declaration. Ensure they match. */
879 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
880 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
881 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
883 error_with_decl (*node
,
884 "section of `%s' conflicts with previous declaration");
885 *no_add_attrs
= true;
888 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
892 error_with_decl (*node
,
893 "section attribute not allowed for `%s'");
894 *no_add_attrs
= true;
899 error_with_decl (*node
,
900 "section attributes are not supported for this target");
901 *no_add_attrs
= true;
907 /* Handle a "aligned" attribute; arguments as in
908 struct attribute_spec.handler. */
911 handle_aligned_attribute (node
, name
, args
, flags
, no_add_attrs
)
913 tree name ATTRIBUTE_UNUSED
;
918 tree decl
= NULL_TREE
;
921 tree align_expr
= (args
? TREE_VALUE (args
)
922 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
928 type
= &TREE_TYPE (decl
);
929 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
931 else if (TYPE_P (*node
))
932 type
= node
, is_type
= 1;
934 /* Strip any NOPs of any kind. */
935 while (TREE_CODE (align_expr
) == NOP_EXPR
936 || TREE_CODE (align_expr
) == CONVERT_EXPR
937 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
938 align_expr
= TREE_OPERAND (align_expr
, 0);
940 if (TREE_CODE (align_expr
) != INTEGER_CST
)
942 error ("requested alignment is not a constant");
943 *no_add_attrs
= true;
945 else if ((i
= tree_log2 (align_expr
)) == -1)
947 error ("requested alignment is not a power of 2");
948 *no_add_attrs
= true;
950 else if (i
> HOST_BITS_PER_INT
- 2)
952 error ("requested alignment is too large");
953 *no_add_attrs
= true;
957 /* If we have a TYPE_DECL, then copy the type, so that we
958 don't accidentally modify a builtin type. See pushdecl. */
959 if (decl
&& TREE_TYPE (decl
) != error_mark_node
960 && DECL_ORIGINAL_TYPE (decl
) == NULL_TREE
)
962 tree tt
= TREE_TYPE (decl
);
963 *type
= build_type_copy (*type
);
964 DECL_ORIGINAL_TYPE (decl
) = tt
;
965 TYPE_NAME (*type
) = decl
;
966 TREE_USED (*type
) = TREE_USED (decl
);
967 TREE_TYPE (decl
) = *type
;
969 else if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
970 *type
= build_type_copy (*type
);
972 TYPE_ALIGN (*type
) = (1 << i
) * BITS_PER_UNIT
;
973 TYPE_USER_ALIGN (*type
) = 1;
975 else if (TREE_CODE (decl
) != VAR_DECL
976 && TREE_CODE (decl
) != FIELD_DECL
)
978 error_with_decl (decl
,
979 "alignment may not be specified for `%s'");
980 *no_add_attrs
= true;
984 DECL_ALIGN (decl
) = (1 << i
) * BITS_PER_UNIT
;
985 DECL_USER_ALIGN (decl
) = 1;
991 /* Handle a "weak" attribute; arguments as in
992 struct attribute_spec.handler. */
995 handle_weak_attribute (node
, name
, args
, flags
, no_add_attrs
)
997 tree name ATTRIBUTE_UNUSED
;
998 tree args ATTRIBUTE_UNUSED
;
999 int flags ATTRIBUTE_UNUSED
;
1000 bool *no_add_attrs ATTRIBUTE_UNUSED
;
1002 declare_weak (*node
);
1007 /* Handle an "alias" attribute; arguments as in
1008 struct attribute_spec.handler. */
1011 handle_alias_attribute (node
, name
, args
, flags
, no_add_attrs
)
1015 int flags ATTRIBUTE_UNUSED
;
1020 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
1021 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
1023 error_with_decl (decl
,
1024 "`%s' defined both normally and as an alias");
1025 *no_add_attrs
= true;
1027 else if (decl_function_context (decl
) == 0)
1031 id
= TREE_VALUE (args
);
1032 if (TREE_CODE (id
) != STRING_CST
)
1034 error ("alias arg not a string");
1035 *no_add_attrs
= true;
1038 id
= get_identifier (TREE_STRING_POINTER (id
));
1039 /* This counts as a use of the object pointed to. */
1042 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1043 DECL_INITIAL (decl
) = error_mark_node
;
1045 DECL_EXTERNAL (decl
) = 0;
1049 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1050 *no_add_attrs
= true;
1056 /* Handle an "visibility" attribute; arguments as in
1057 struct attribute_spec.handler. */
1060 handle_visibility_attribute (node
, name
, args
, flags
, no_add_attrs
)
1064 int flags ATTRIBUTE_UNUSED
;
1069 if (decl_function_context (decl
) != 0 || ! TREE_PUBLIC (decl
))
1071 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1072 *no_add_attrs
= true;
1078 id
= TREE_VALUE (args
);
1079 if (TREE_CODE (id
) != STRING_CST
)
1081 error ("visibility arg not a string");
1082 *no_add_attrs
= true;
1085 if (strcmp (TREE_STRING_POINTER (id
), "hidden")
1086 && strcmp (TREE_STRING_POINTER (id
), "protected")
1087 && strcmp (TREE_STRING_POINTER (id
), "internal"))
1089 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
1090 *no_add_attrs
= true;
1098 /* Handle a "no_instrument_function" attribute; arguments as in
1099 struct attribute_spec.handler. */
1102 handle_no_instrument_function_attribute (node
, name
, args
, flags
, no_add_attrs
)
1105 tree args ATTRIBUTE_UNUSED
;
1106 int flags ATTRIBUTE_UNUSED
;
1111 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1113 error_with_decl (decl
,
1114 "`%s' attribute applies only to functions",
1115 IDENTIFIER_POINTER (name
));
1116 *no_add_attrs
= true;
1118 else if (DECL_INITIAL (decl
))
1120 error_with_decl (decl
,
1121 "can't set `%s' attribute after definition",
1122 IDENTIFIER_POINTER (name
));
1123 *no_add_attrs
= true;
1126 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1131 /* Handle a "malloc" attribute; arguments as in
1132 struct attribute_spec.handler. */
1135 handle_malloc_attribute (node
, name
, args
, flags
, no_add_attrs
)
1138 tree args ATTRIBUTE_UNUSED
;
1139 int flags ATTRIBUTE_UNUSED
;
1142 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1143 DECL_IS_MALLOC (*node
) = 1;
1144 /* ??? TODO: Support types. */
1147 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1148 *no_add_attrs
= true;
1154 /* Handle a "no_limit_stack" attribute; arguments as in
1155 struct attribute_spec.handler. */
1158 handle_no_limit_stack_attribute (node
, name
, args
, flags
, no_add_attrs
)
1161 tree args ATTRIBUTE_UNUSED
;
1162 int flags ATTRIBUTE_UNUSED
;
1167 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1169 error_with_decl (decl
,
1170 "`%s' attribute applies only to functions",
1171 IDENTIFIER_POINTER (name
));
1172 *no_add_attrs
= true;
1174 else if (DECL_INITIAL (decl
))
1176 error_with_decl (decl
,
1177 "can't set `%s' attribute after definition",
1178 IDENTIFIER_POINTER (name
));
1179 *no_add_attrs
= true;
1182 DECL_NO_LIMIT_STACK (decl
) = 1;
1187 /* Handle a "pure" attribute; arguments as in
1188 struct attribute_spec.handler. */
1191 handle_pure_attribute (node
, name
, args
, flags
, no_add_attrs
)
1194 tree args ATTRIBUTE_UNUSED
;
1195 int flags ATTRIBUTE_UNUSED
;
1198 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1199 DECL_IS_PURE (*node
) = 1;
1200 /* ??? TODO: Support types. */
1203 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1204 *no_add_attrs
= true;
1210 /* Handle a "deprecated" attribute; arguments as in
1211 struct attribute_spec.handler. */
1214 handle_deprecated_attribute (node
, name
, args
, flags
, no_add_attrs
)
1217 tree args ATTRIBUTE_UNUSED
;
1221 tree type
= NULL_TREE
;
1223 const char *what
= NULL
;
1228 type
= TREE_TYPE (decl
);
1230 if (TREE_CODE (decl
) == TYPE_DECL
1231 || TREE_CODE (decl
) == PARM_DECL
1232 || TREE_CODE (decl
) == VAR_DECL
1233 || TREE_CODE (decl
) == FUNCTION_DECL
1234 || TREE_CODE (decl
) == FIELD_DECL
)
1235 TREE_DEPRECATED (decl
) = 1;
1239 else if (TYPE_P (*node
))
1241 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
1242 *node
= build_type_copy (*node
);
1243 TREE_DEPRECATED (*node
) = 1;
1251 *no_add_attrs
= true;
1252 if (type
&& TYPE_NAME (type
))
1254 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
1255 what
= IDENTIFIER_POINTER (TYPE_NAME (*node
));
1256 else if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1257 && DECL_NAME (TYPE_NAME (type
)))
1258 what
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
1261 warning ("`%s' attribute ignored for `%s'",
1262 IDENTIFIER_POINTER (name
), what
);
1264 warning ("`%s' attribute ignored",
1265 IDENTIFIER_POINTER (name
));
1271 /* Handle a "vector_size" attribute; arguments as in
1272 struct attribute_spec.handler. */
1275 handle_vector_size_attribute (node
, name
, args
, flags
, no_add_attrs
)
1279 int flags ATTRIBUTE_UNUSED
;
1282 unsigned HOST_WIDE_INT vecsize
, nunits
;
1283 enum machine_mode mode
, orig_mode
, new_mode
;
1284 tree type
= *node
, new_type
;
1286 *no_add_attrs
= true;
1288 if (! host_integerp (TREE_VALUE (args
), 1))
1290 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1294 /* Get the vector size (in bytes). */
1295 vecsize
= tree_low_cst (TREE_VALUE (args
), 1);
1297 /* We need to provide for vector pointers, vector arrays, and
1298 functions returning vectors. For example:
1300 __attribute__((vector_size(16))) short *foo;
1302 In this case, the mode is SI, but the type being modified is
1303 HI, so we need to look further. */
1305 while (POINTER_TYPE_P (type
)
1306 || TREE_CODE (type
) == FUNCTION_TYPE
1307 || TREE_CODE (type
) == ARRAY_TYPE
)
1308 type
= TREE_TYPE (type
);
1310 /* Get the mode of the type being modified. */
1311 orig_mode
= TYPE_MODE (type
);
1313 if (TREE_CODE (type
) == RECORD_TYPE
1314 || (GET_MODE_CLASS (orig_mode
) != MODE_FLOAT
1315 && GET_MODE_CLASS (orig_mode
) != MODE_INT
)
1316 || ! host_integerp (TYPE_SIZE_UNIT (type
), 1))
1318 error ("invalid vector type for attribute `%s'",
1319 IDENTIFIER_POINTER (name
));
1323 /* Calculate how many units fit in the vector. */
1324 nunits
= vecsize
/ tree_low_cst (TYPE_SIZE_UNIT (type
), 1);
1326 /* Find a suitably sized vector. */
1327 new_mode
= VOIDmode
;
1328 for (mode
= GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode
) == MODE_INT
1330 : MODE_VECTOR_FLOAT
);
1332 mode
= GET_MODE_WIDER_MODE (mode
))
1333 if (vecsize
== GET_MODE_SIZE (mode
)
1334 && nunits
== (unsigned HOST_WIDE_INT
) GET_MODE_NUNITS (mode
))
1340 if (new_mode
== VOIDmode
)
1341 error ("no vector mode with the size and type specified could be found");
1344 tree index
, array
, rt
;
1346 new_type
= (*lang_hooks
.types
.type_for_mode
) (new_mode
,
1347 TREE_UNSIGNED (type
));
1351 error ("no vector mode with the size and type specified could be found");
1355 new_type
= build_type_copy (new_type
);
1357 /* Set the debug information here, because this is the only
1358 place where we know the underlying type for a vector made
1359 with vector_size. For debugging purposes we pretend a vector
1360 is an array within a structure. */
1361 index
= build_int_2 (TYPE_VECTOR_SUBPARTS (new_type
) - 1, 0);
1362 array
= build_array_type (type
, build_index_type (index
));
1363 rt
= make_node (RECORD_TYPE
);
1365 TYPE_FIELDS (rt
) = build_decl (FIELD_DECL
, get_identifier ("f"), array
);
1366 DECL_CONTEXT (TYPE_FIELDS (rt
)) = rt
;
1368 TYPE_DEBUG_REPRESENTATION_TYPE (new_type
) = rt
;
1370 /* Build back pointers if needed. */
1371 *node
= vector_size_helper (*node
, new_type
);
1377 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1380 If we requested a pointer to a vector, build up the pointers that
1381 we stripped off while looking for the inner type. Similarly for
1382 return values from functions.
1384 The argument "type" is the top of the chain, and "bottom" is the
1385 new type which we will point to. */
1388 vector_size_helper (type
, bottom
)
1393 if (POINTER_TYPE_P (type
))
1395 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1396 outer
= build_pointer_type (inner
);
1398 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1400 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1401 outer
= build_array_type (inner
, TYPE_VALUES (type
));
1403 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1405 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1406 outer
= build_function_type (inner
, TYPE_VALUES (type
));
1411 TREE_READONLY (outer
) = TREE_READONLY (type
);
1412 TREE_THIS_VOLATILE (outer
) = TREE_THIS_VOLATILE (type
);
1417 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1418 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1420 The head of the declspec list is stored in DECLSPECS.
1421 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1423 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1424 the list elements. We drop the containing TREE_LIST nodes and link the
1425 resulting attributes together the way decl_attributes expects them. */
1428 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
1430 tree
*declspecs
, *prefix_attributes
;
1432 tree t
, s
, a
, next
, specs
, attrs
;
1434 /* This can happen after an __extension__ in pedantic mode. */
1435 if (specs_attrs
!= NULL_TREE
1436 && TREE_CODE (specs_attrs
) == INTEGER_CST
)
1438 *declspecs
= NULL_TREE
;
1439 *prefix_attributes
= NULL_TREE
;
1443 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1444 if (specs_attrs
!= NULL_TREE
1445 && TREE_CODE (specs_attrs
) != TREE_LIST
)
1447 *declspecs
= specs_attrs
;
1448 *prefix_attributes
= NULL_TREE
;
1452 /* Remember to keep the lists in the same order, element-wise. */
1454 specs
= s
= NULL_TREE
;
1455 attrs
= a
= NULL_TREE
;
1456 for (t
= specs_attrs
; t
; t
= next
)
1458 next
= TREE_CHAIN (t
);
1459 /* Declspecs have a non-NULL TREE_VALUE. */
1460 if (TREE_VALUE (t
) != NULL_TREE
)
1462 if (specs
== NULL_TREE
)
1470 /* The TREE_PURPOSE may also be empty in the case of
1471 __attribute__(()). */
1472 else if (TREE_PURPOSE (t
) != NULL_TREE
)
1474 if (attrs
== NULL_TREE
)
1475 attrs
= a
= TREE_PURPOSE (t
);
1478 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
1479 a
= TREE_PURPOSE (t
);
1481 /* More attrs can be linked here, move A to the end. */
1482 while (TREE_CHAIN (a
) != NULL_TREE
)
1487 /* Terminate the lists. */
1489 TREE_CHAIN (s
) = NULL_TREE
;
1491 TREE_CHAIN (a
) = NULL_TREE
;
1495 *prefix_attributes
= attrs
;
1498 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1499 This function is used by the parser when a rule will accept attributes
1500 in a particular position, but we don't want to support that just yet.
1502 A warning is issued for every ignored attribute. */
1505 strip_attrs (specs_attrs
)
1510 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
1514 warning ("`%s' attribute ignored",
1515 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
1516 attrs
= TREE_CHAIN (attrs
);