1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 static void init_attributes
PARAMS ((void));
38 /* Table of the tables of attributes (common, format, language, machine)
40 static const struct attribute_spec
*attribute_tables
[4];
42 static bool attributes_initialized
= false;
44 static tree handle_packed_attribute
PARAMS ((tree
*, tree
, tree
, int,
46 static tree handle_nocommon_attribute
PARAMS ((tree
*, tree
, tree
, int,
48 static tree handle_common_attribute
PARAMS ((tree
*, tree
, tree
, int,
50 static tree handle_noreturn_attribute
PARAMS ((tree
*, tree
, tree
, int,
52 static tree handle_noinline_attribute
PARAMS ((tree
*, tree
, tree
, int,
54 static tree handle_used_attribute
PARAMS ((tree
*, tree
, tree
, int,
56 static tree handle_unused_attribute
PARAMS ((tree
*, tree
, tree
, int,
58 static tree handle_const_attribute
PARAMS ((tree
*, tree
, tree
, int,
60 static tree handle_transparent_union_attribute
PARAMS ((tree
*, tree
, tree
,
62 static tree handle_constructor_attribute
PARAMS ((tree
*, tree
, tree
, int,
64 static tree handle_destructor_attribute
PARAMS ((tree
*, tree
, tree
, int,
66 static tree handle_mode_attribute
PARAMS ((tree
*, tree
, tree
, int,
68 static tree handle_section_attribute
PARAMS ((tree
*, tree
, tree
, int,
70 static tree handle_aligned_attribute
PARAMS ((tree
*, tree
, tree
, int,
72 static tree handle_weak_attribute
PARAMS ((tree
*, tree
, tree
, int,
74 static tree handle_alias_attribute
PARAMS ((tree
*, tree
, tree
, int,
76 static tree handle_no_instrument_function_attribute
PARAMS ((tree
*, tree
,
79 static tree handle_malloc_attribute
PARAMS ((tree
*, tree
, tree
, int,
81 static tree handle_no_limit_stack_attribute
PARAMS ((tree
*, tree
, tree
, int,
83 static tree handle_pure_attribute
PARAMS ((tree
*, tree
, tree
, int,
85 static tree handle_vector_size_attribute
PARAMS ((tree
*, tree
, tree
, int,
87 static tree vector_size_helper
PARAMS ((tree
, tree
));
89 /* Table of machine-independent attributes common to all C-like languages. */
90 static const struct attribute_spec c_common_attribute_table
[] =
92 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
93 { "packed", 0, 0, false, false, false,
94 handle_packed_attribute
},
95 { "nocommon", 0, 0, true, false, false,
96 handle_nocommon_attribute
},
97 { "common", 0, 0, true, false, false,
98 handle_common_attribute
},
99 /* FIXME: logically, noreturn attributes should be listed as
100 "false, true, true" and apply to function types. But implementing this
101 would require all the places in the compiler that use TREE_THIS_VOLATILE
102 on a decl to identify non-returning functions to be located and fixed
103 to check the function type instead. */
104 { "noreturn", 0, 0, true, false, false,
105 handle_noreturn_attribute
},
106 { "volatile", 0, 0, true, false, false,
107 handle_noreturn_attribute
},
108 { "noinline", 0, 0, true, false, false,
109 handle_noinline_attribute
},
110 { "used", 0, 0, true, false, false,
111 handle_used_attribute
},
112 { "unused", 0, 0, false, false, false,
113 handle_unused_attribute
},
114 /* The same comments as for noreturn attributes apply to const ones. */
115 { "const", 0, 0, true, false, false,
116 handle_const_attribute
},
117 { "transparent_union", 0, 0, false, false, false,
118 handle_transparent_union_attribute
},
119 { "constructor", 0, 0, true, false, false,
120 handle_constructor_attribute
},
121 { "destructor", 0, 0, true, false, false,
122 handle_destructor_attribute
},
123 { "mode", 1, 1, true, false, false,
124 handle_mode_attribute
},
125 { "section", 1, 1, true, false, false,
126 handle_section_attribute
},
127 { "aligned", 0, 1, false, false, false,
128 handle_aligned_attribute
},
129 { "weak", 0, 0, true, false, false,
130 handle_weak_attribute
},
131 { "alias", 1, 1, true, false, false,
132 handle_alias_attribute
},
133 { "no_instrument_function", 0, 0, true, false, false,
134 handle_no_instrument_function_attribute
},
135 { "malloc", 0, 0, true, false, false,
136 handle_malloc_attribute
},
137 { "no_stack_limit", 0, 0, true, false, false,
138 handle_no_limit_stack_attribute
},
139 { "pure", 0, 0, true, false, false,
140 handle_pure_attribute
},
141 { "vector_size", 1, 1, false, true, false,
142 handle_vector_size_attribute
},
143 { NULL
, 0, 0, false, false, false, NULL
}
146 /* Default empty table of attributes. */
147 static const struct attribute_spec empty_attribute_table
[] =
149 { NULL
, 0, 0, false, false, false, NULL
}
152 /* Table of machine-independent attributes for checking formats, if used. */
153 const struct attribute_spec
*format_attribute_table
= empty_attribute_table
;
155 /* Table of machine-independent attributes for a particular language. */
156 const struct attribute_spec
*lang_attribute_table
= empty_attribute_table
;
158 /* Flag saying whether common language attributes are to be supported. */
159 int lang_attribute_common
= 1;
161 /* Initialize attribute tables, and make some sanity checks
162 if --enable-checking. */
167 #ifdef ENABLE_CHECKING
172 = lang_attribute_common
? c_common_attribute_table
: empty_attribute_table
;
173 attribute_tables
[1] = lang_attribute_table
;
174 attribute_tables
[2] = format_attribute_table
;
175 attribute_tables
[3] = targetm
.attribute_table
;
177 #ifdef ENABLE_CHECKING
178 /* Make some sanity checks on the attribute tables. */
180 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
185 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
187 /* The name must not begin and end with __. */
188 const char *name
= attribute_tables
[i
][j
].name
;
189 int len
= strlen (name
);
190 if (name
[0] == '_' && name
[1] == '_'
191 && name
[len
- 1] == '_' && name
[len
- 2] == '_')
193 /* The minimum and maximum lengths must be consistent. */
194 if (attribute_tables
[i
][j
].min_length
< 0)
196 if (attribute_tables
[i
][j
].max_length
!= -1
197 && (attribute_tables
[i
][j
].max_length
198 < attribute_tables
[i
][j
].min_length
))
200 /* An attribute cannot require both a DECL and a TYPE. */
201 if (attribute_tables
[i
][j
].decl_required
202 && attribute_tables
[i
][j
].type_required
)
204 /* If an attribute requires a function type, in particular
205 it requires a type. */
206 if (attribute_tables
[i
][j
].function_type_required
207 && !attribute_tables
[i
][j
].type_required
)
212 /* Check that each name occurs just once in each table. */
214 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
218 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
219 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
220 if (!strcmp (attribute_tables
[i
][j
].name
,
221 attribute_tables
[i
][k
].name
))
224 /* Check that no name occurs in more than one table. */
226 i
< (int) (sizeof (attribute_tables
) / sizeof (attribute_tables
[0]));
232 j
< ((int) (sizeof (attribute_tables
)
233 / sizeof (attribute_tables
[0])));
235 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
236 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
237 if (!strcmp (attribute_tables
[i
][k
].name
,
238 attribute_tables
[j
][l
].name
))
243 attributes_initialized
= true;
246 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
247 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
248 it should be modified in place; if a TYPE, a copy should be created
249 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
250 information, in the form of a bitwise OR of flags in enum attribute_flags
251 from tree.h. Depending on these flags, some attributes may be
252 returned to be applied at a later stage (for example, to apply
253 a decl attribute to the declaration rather than to its type). If
254 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
255 whether there might be some default attributes to apply to this DECL;
256 if so, decl_attributes will be called recursively with those attributes
257 and ATTR_FLAG_BUILT_IN set. */
260 decl_attributes (node
, attributes
, flags
)
261 tree
*node
, attributes
;
265 tree returned_attrs
= NULL_TREE
;
267 if (!attributes_initialized
)
270 (*targetm
.insert_attributes
) (*node
, &attributes
);
272 if (DECL_P (*node
) && TREE_CODE (*node
) == FUNCTION_DECL
273 && !(flags
& (int) ATTR_FLAG_BUILT_IN
))
274 insert_default_attributes (*node
);
276 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
278 tree name
= TREE_PURPOSE (a
);
279 tree args
= TREE_VALUE (a
);
281 const struct attribute_spec
*spec
= NULL
;
282 bool no_add_attrs
= 0;
286 i
< ((int) (sizeof (attribute_tables
)
287 / sizeof (attribute_tables
[0])));
292 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
294 if (is_attribute_p (attribute_tables
[i
][j
].name
, name
))
296 spec
= &attribute_tables
[i
][j
];
306 warning ("`%s' attribute directive ignored",
307 IDENTIFIER_POINTER (name
));
310 else if (list_length (args
) < spec
->min_length
311 || (spec
->max_length
>= 0
312 && list_length (args
) > spec
->max_length
))
314 error ("wrong number of arguments specified for `%s' attribute",
315 IDENTIFIER_POINTER (name
));
319 if (spec
->decl_required
&& !DECL_P (*anode
))
321 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
322 | (int) ATTR_FLAG_FUNCTION_NEXT
323 | (int) ATTR_FLAG_ARRAY_NEXT
))
325 /* Pass on this attribute to be tried again. */
326 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
331 warning ("`%s' attribute does not apply to types",
332 IDENTIFIER_POINTER (name
));
337 if (spec
->type_required
&& DECL_P (*anode
))
338 anode
= &TREE_TYPE (*anode
);
340 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
341 && TREE_CODE (*anode
) != METHOD_TYPE
)
343 if (TREE_CODE (*anode
) == POINTER_TYPE
344 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
345 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
347 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
348 *anode
= build_type_copy (*anode
);
349 anode
= &TREE_TYPE (*anode
);
351 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
353 /* Pass on this attribute to be tried again. */
354 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
358 if (TREE_CODE (*anode
) != FUNCTION_TYPE
359 && TREE_CODE (*anode
) != METHOD_TYPE
)
361 warning ("`%s' attribute only applies to function types",
362 IDENTIFIER_POINTER (name
));
367 if (spec
->handler
!= NULL
)
368 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
369 flags
, &no_add_attrs
),
372 /* Layout the decl in case anything changed. */
373 if (spec
->type_required
&& DECL_P (*node
)
374 && TREE_CODE (*node
) == VAR_DECL
)
376 /* Force a recalculation of mode and size. */
377 DECL_MODE (*node
) = VOIDmode
;
378 DECL_SIZE (*node
) = 0;
380 layout_decl (*node
, 0);
389 old_attrs
= DECL_ATTRIBUTES (*anode
);
391 old_attrs
= TYPE_ATTRIBUTES (*anode
);
393 for (a
= lookup_attribute (spec
->name
, old_attrs
);
395 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
397 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
403 /* This attribute isn't already in the list. */
405 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
406 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
407 TYPE_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
409 *anode
= build_type_attribute_variant (*anode
,
410 tree_cons (name
, args
,
416 return returned_attrs
;
419 /* Handle a "packed" attribute; arguments as in
420 struct attribute_spec.handler. */
423 handle_packed_attribute (node
, name
, args
, flags
, no_add_attrs
)
426 tree args ATTRIBUTE_UNUSED
;
433 if (TREE_CODE (*node
) == TYPE_DECL
)
434 type
= &TREE_TYPE (*node
);
441 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
442 *type
= build_type_copy (*type
);
443 TYPE_PACKED (*type
) = 1;
445 else if (TREE_CODE (*node
) == FIELD_DECL
)
446 DECL_PACKED (*node
) = 1;
447 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
448 used for DECL_REGISTER. It wouldn't mean anything anyway. */
451 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
452 *no_add_attrs
= true;
458 /* Handle a "nocommon" attribute; arguments as in
459 struct attribute_spec.handler. */
462 handle_nocommon_attribute (node
, name
, args
, flags
, no_add_attrs
)
465 tree args ATTRIBUTE_UNUSED
;
466 int flags ATTRIBUTE_UNUSED
;
469 if (TREE_CODE (*node
) == VAR_DECL
)
470 DECL_COMMON (*node
) = 0;
473 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
474 *no_add_attrs
= true;
480 /* Handle a "common" attribute; arguments as in
481 struct attribute_spec.handler. */
484 handle_common_attribute (node
, name
, args
, flags
, no_add_attrs
)
487 tree args ATTRIBUTE_UNUSED
;
488 int flags ATTRIBUTE_UNUSED
;
491 if (TREE_CODE (*node
) == VAR_DECL
)
492 DECL_COMMON (*node
) = 1;
495 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
496 *no_add_attrs
= true;
502 /* Handle a "noreturn" attribute; arguments as in
503 struct attribute_spec.handler. */
506 handle_noreturn_attribute (node
, name
, args
, flags
, no_add_attrs
)
509 tree args ATTRIBUTE_UNUSED
;
510 int flags ATTRIBUTE_UNUSED
;
513 tree type
= TREE_TYPE (*node
);
515 /* See FIXME comment in c_common_attribute_table. */
516 if (TREE_CODE (*node
) == FUNCTION_DECL
)
517 TREE_THIS_VOLATILE (*node
) = 1;
518 else if (TREE_CODE (type
) == POINTER_TYPE
519 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
522 (build_type_variant (TREE_TYPE (type
),
523 TREE_READONLY (TREE_TYPE (type
)), 1));
526 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
527 *no_add_attrs
= true;
533 /* Handle a "noinline" attribute; arguments as in
534 struct attribute_spec.handler. */
537 handle_noinline_attribute (node
, name
, args
, flags
, no_add_attrs
)
540 tree args ATTRIBUTE_UNUSED
;
541 int flags ATTRIBUTE_UNUSED
;
544 if (TREE_CODE (*node
) == FUNCTION_DECL
)
545 DECL_UNINLINABLE (*node
) = 1;
548 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
549 *no_add_attrs
= true;
555 /* Handle a "used" attribute; arguments as in
556 struct attribute_spec.handler. */
559 handle_used_attribute (node
, name
, args
, flags
, no_add_attrs
)
562 tree args ATTRIBUTE_UNUSED
;
563 int flags ATTRIBUTE_UNUSED
;
566 if (TREE_CODE (*node
) == FUNCTION_DECL
)
567 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node
))
568 = TREE_USED (*node
) = 1;
571 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
572 *no_add_attrs
= true;
578 /* Handle a "unused" attribute; arguments as in
579 struct attribute_spec.handler. */
582 handle_unused_attribute (node
, name
, args
, flags
, no_add_attrs
)
585 tree args ATTRIBUTE_UNUSED
;
593 if (TREE_CODE (decl
) == PARM_DECL
594 || TREE_CODE (decl
) == VAR_DECL
595 || TREE_CODE (decl
) == FUNCTION_DECL
596 || TREE_CODE (decl
) == LABEL_DECL
597 || TREE_CODE (decl
) == TYPE_DECL
)
598 TREE_USED (decl
) = 1;
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
602 *no_add_attrs
= true;
607 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
608 *node
= build_type_copy (*node
);
609 TREE_USED (*node
) = 1;
615 /* Handle a "const" attribute; arguments as in
616 struct attribute_spec.handler. */
619 handle_const_attribute (node
, name
, args
, flags
, no_add_attrs
)
622 tree args ATTRIBUTE_UNUSED
;
623 int flags ATTRIBUTE_UNUSED
;
626 tree type
= TREE_TYPE (*node
);
628 /* See FIXME comment on noreturn in c_common_attribute_table. */
629 if (TREE_CODE (*node
) == FUNCTION_DECL
)
630 TREE_READONLY (*node
) = 1;
631 else if (TREE_CODE (type
) == POINTER_TYPE
632 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
635 (build_type_variant (TREE_TYPE (type
), 1,
636 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
639 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
640 *no_add_attrs
= true;
646 /* Handle a "transparent_union" attribute; arguments as in
647 struct attribute_spec.handler. */
650 handle_transparent_union_attribute (node
, name
, args
, flags
, no_add_attrs
)
653 tree args ATTRIBUTE_UNUSED
;
657 tree decl
= NULL_TREE
;
664 type
= &TREE_TYPE (decl
);
665 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
667 else if (TYPE_P (*node
))
668 type
= node
, is_type
= 1;
671 && TREE_CODE (*type
) == UNION_TYPE
673 || (TYPE_FIELDS (*type
) != 0
674 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))))
676 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
677 *type
= build_type_copy (*type
);
678 TYPE_TRANSPARENT_UNION (*type
) = 1;
680 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
681 && TREE_CODE (*type
) == UNION_TYPE
682 && TYPE_MODE (*type
) == DECL_MODE (TYPE_FIELDS (*type
)))
683 DECL_TRANSPARENT_UNION (decl
) = 1;
686 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
687 *no_add_attrs
= true;
693 /* Handle a "constructor" attribute; arguments as in
694 struct attribute_spec.handler. */
697 handle_constructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
700 tree args ATTRIBUTE_UNUSED
;
701 int flags ATTRIBUTE_UNUSED
;
705 tree type
= TREE_TYPE (decl
);
707 if (TREE_CODE (decl
) == FUNCTION_DECL
708 && TREE_CODE (type
) == FUNCTION_TYPE
709 && decl_function_context (decl
) == 0)
711 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
712 TREE_USED (decl
) = 1;
716 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
717 *no_add_attrs
= true;
723 /* Handle a "destructor" attribute; arguments as in
724 struct attribute_spec.handler. */
727 handle_destructor_attribute (node
, name
, args
, flags
, no_add_attrs
)
730 tree args ATTRIBUTE_UNUSED
;
731 int flags ATTRIBUTE_UNUSED
;
735 tree type
= TREE_TYPE (decl
);
737 if (TREE_CODE (decl
) == FUNCTION_DECL
738 && TREE_CODE (type
) == FUNCTION_TYPE
739 && decl_function_context (decl
) == 0)
741 DECL_STATIC_DESTRUCTOR (decl
) = 1;
742 TREE_USED (decl
) = 1;
746 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
747 *no_add_attrs
= true;
753 /* Handle a "mode" attribute; arguments as in
754 struct attribute_spec.handler. */
757 handle_mode_attribute (node
, name
, args
, flags
, no_add_attrs
)
761 int flags ATTRIBUTE_UNUSED
;
765 tree type
= TREE_TYPE (decl
);
767 *no_add_attrs
= true;
769 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
770 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
774 const char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
775 int len
= strlen (p
);
776 enum machine_mode mode
= VOIDmode
;
779 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
780 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
782 char *newp
= (char *) alloca (len
- 1);
784 strcpy (newp
, &p
[2]);
785 newp
[len
- 4] = '\0';
789 /* Give this decl a type with the specified mode.
790 First check for the special modes. */
791 if (! strcmp (p
, "byte"))
793 else if (!strcmp (p
, "word"))
795 else if (! strcmp (p
, "pointer"))
798 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
799 if (!strcmp (p
, GET_MODE_NAME (j
)))
800 mode
= (enum machine_mode
) j
;
802 if (mode
== VOIDmode
)
803 error ("unknown machine mode `%s'", p
);
804 else if (0 == (typefm
= type_for_mode (mode
,
805 TREE_UNSIGNED (type
))))
806 error ("no data type for mode `%s'", p
);
809 TREE_TYPE (decl
) = type
= typefm
;
810 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = 0;
811 if (TREE_CODE (decl
) != FIELD_DECL
)
812 layout_decl (decl
, 0);
819 /* Handle a "section" attribute; arguments as in
820 struct attribute_spec.handler. */
823 handle_section_attribute (node
, name
, args
, flags
, no_add_attrs
)
825 tree name ATTRIBUTE_UNUSED
;
827 int flags ATTRIBUTE_UNUSED
;
832 if (targetm
.have_named_sections
)
834 if ((TREE_CODE (decl
) == FUNCTION_DECL
835 || TREE_CODE (decl
) == VAR_DECL
)
836 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
838 if (TREE_CODE (decl
) == VAR_DECL
839 && current_function_decl
!= NULL_TREE
840 && ! TREE_STATIC (decl
))
842 error_with_decl (decl
,
843 "section attribute cannot be specified for local variables");
844 *no_add_attrs
= true;
847 /* The decl may have already been given a section attribute
848 from a previous declaration. Ensure they match. */
849 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
850 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
851 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
853 error_with_decl (*node
,
854 "section of `%s' conflicts with previous declaration");
855 *no_add_attrs
= true;
858 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
862 error_with_decl (*node
,
863 "section attribute not allowed for `%s'");
864 *no_add_attrs
= true;
869 error_with_decl (*node
,
870 "section attributes are not supported for this target");
871 *no_add_attrs
= true;
877 /* Handle a "aligned" attribute; arguments as in
878 struct attribute_spec.handler. */
881 handle_aligned_attribute (node
, name
, args
, flags
, no_add_attrs
)
883 tree name ATTRIBUTE_UNUSED
;
888 tree decl
= NULL_TREE
;
891 tree align_expr
= (args
? TREE_VALUE (args
)
892 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
898 type
= &TREE_TYPE (decl
);
899 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
901 else if (TYPE_P (*node
))
902 type
= node
, is_type
= 1;
904 /* Strip any NOPs of any kind. */
905 while (TREE_CODE (align_expr
) == NOP_EXPR
906 || TREE_CODE (align_expr
) == CONVERT_EXPR
907 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
908 align_expr
= TREE_OPERAND (align_expr
, 0);
910 if (TREE_CODE (align_expr
) != INTEGER_CST
)
912 error ("requested alignment is not a constant");
913 *no_add_attrs
= true;
915 else if ((i
= tree_log2 (align_expr
)) == -1)
917 error ("requested alignment is not a power of 2");
918 *no_add_attrs
= true;
920 else if (i
> HOST_BITS_PER_INT
- 2)
922 error ("requested alignment is too large");
923 *no_add_attrs
= true;
927 /* If we have a TYPE_DECL, then copy the type, so that we
928 don't accidentally modify a builtin type. See pushdecl. */
929 if (decl
&& TREE_TYPE (decl
) != error_mark_node
930 && DECL_ORIGINAL_TYPE (decl
) == NULL_TREE
)
932 tree tt
= TREE_TYPE (decl
);
933 *type
= build_type_copy (*type
);
934 DECL_ORIGINAL_TYPE (decl
) = tt
;
935 TYPE_NAME (*type
) = decl
;
936 TREE_USED (*type
) = TREE_USED (decl
);
937 TREE_TYPE (decl
) = *type
;
939 else if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
940 *type
= build_type_copy (*type
);
942 TYPE_ALIGN (*type
) = (1 << i
) * BITS_PER_UNIT
;
943 TYPE_USER_ALIGN (*type
) = 1;
945 else if (TREE_CODE (decl
) != VAR_DECL
946 && TREE_CODE (decl
) != FIELD_DECL
)
948 error_with_decl (decl
,
949 "alignment may not be specified for `%s'");
950 *no_add_attrs
= true;
954 DECL_ALIGN (decl
) = (1 << i
) * BITS_PER_UNIT
;
955 DECL_USER_ALIGN (decl
) = 1;
961 /* Handle a "weak" attribute; arguments as in
962 struct attribute_spec.handler. */
965 handle_weak_attribute (node
, name
, args
, flags
, no_add_attrs
)
967 tree name ATTRIBUTE_UNUSED
;
968 tree args ATTRIBUTE_UNUSED
;
969 int flags ATTRIBUTE_UNUSED
;
970 bool *no_add_attrs ATTRIBUTE_UNUSED
;
972 declare_weak (*node
);
977 /* Handle an "alias" attribute; arguments as in
978 struct attribute_spec.handler. */
981 handle_alias_attribute (node
, name
, args
, flags
, no_add_attrs
)
985 int flags ATTRIBUTE_UNUSED
;
990 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
991 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
993 error_with_decl (decl
,
994 "`%s' defined both normally and as an alias");
995 *no_add_attrs
= true;
997 else if (decl_function_context (decl
) == 0)
1001 id
= TREE_VALUE (args
);
1002 if (TREE_CODE (id
) != STRING_CST
)
1004 error ("alias arg not a string");
1005 *no_add_attrs
= true;
1008 id
= get_identifier (TREE_STRING_POINTER (id
));
1009 /* This counts as a use of the object pointed to. */
1012 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1013 DECL_INITIAL (decl
) = error_mark_node
;
1015 DECL_EXTERNAL (decl
) = 0;
1016 assemble_alias (decl
, id
);
1020 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1021 *no_add_attrs
= true;
1027 /* Handle a "no_instrument_function" attribute; arguments as in
1028 struct attribute_spec.handler. */
1031 handle_no_instrument_function_attribute (node
, name
, args
, flags
, no_add_attrs
)
1034 tree args ATTRIBUTE_UNUSED
;
1035 int flags ATTRIBUTE_UNUSED
;
1040 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1042 error_with_decl (decl
,
1043 "`%s' attribute applies only to functions",
1044 IDENTIFIER_POINTER (name
));
1045 *no_add_attrs
= true;
1047 else if (DECL_INITIAL (decl
))
1049 error_with_decl (decl
,
1050 "can't set `%s' attribute after definition",
1051 IDENTIFIER_POINTER (name
));
1052 *no_add_attrs
= true;
1055 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1060 /* Handle a "malloc" attribute; arguments as in
1061 struct attribute_spec.handler. */
1064 handle_malloc_attribute (node
, name
, args
, flags
, no_add_attrs
)
1067 tree args ATTRIBUTE_UNUSED
;
1068 int flags ATTRIBUTE_UNUSED
;
1071 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1072 DECL_IS_MALLOC (*node
) = 1;
1073 /* ??? TODO: Support types. */
1076 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1077 *no_add_attrs
= true;
1083 /* Handle a "no_limit_stack" attribute; arguments as in
1084 struct attribute_spec.handler. */
1087 handle_no_limit_stack_attribute (node
, name
, args
, flags
, no_add_attrs
)
1090 tree args ATTRIBUTE_UNUSED
;
1091 int flags ATTRIBUTE_UNUSED
;
1096 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1098 error_with_decl (decl
,
1099 "`%s' attribute applies only to functions",
1100 IDENTIFIER_POINTER (name
));
1101 *no_add_attrs
= true;
1103 else if (DECL_INITIAL (decl
))
1105 error_with_decl (decl
,
1106 "can't set `%s' attribute after definition",
1107 IDENTIFIER_POINTER (name
));
1108 *no_add_attrs
= true;
1111 DECL_NO_LIMIT_STACK (decl
) = 1;
1116 /* Handle a "pure" attribute; arguments as in
1117 struct attribute_spec.handler. */
1120 handle_pure_attribute (node
, name
, args
, flags
, no_add_attrs
)
1123 tree args ATTRIBUTE_UNUSED
;
1124 int flags ATTRIBUTE_UNUSED
;
1127 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1128 DECL_IS_PURE (*node
) = 1;
1129 /* ??? TODO: Support types. */
1132 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1133 *no_add_attrs
= true;
1139 /* Handle a "vector_size" attribute; arguments as in
1140 struct attribute_spec.handler. */
1143 handle_vector_size_attribute (node
, name
, args
, flags
, no_add_attrs
)
1147 int flags ATTRIBUTE_UNUSED
;
1150 unsigned int vecsize
, nunits
;
1151 enum machine_mode mode
, orig_mode
, new_mode
;
1152 tree type
= *node
, new_type
;
1154 *no_add_attrs
= true;
1156 if (TREE_CODE (TREE_VALUE (args
)) != INTEGER_CST
)
1158 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
1162 /* Get the vector size (in bytes). */
1163 vecsize
= TREE_INT_CST_LOW (TREE_VALUE (args
));
1165 /* We need to provide for vector pointers, vector arrays, and
1166 functions returning vectors. For example:
1168 __attribute__((vector_size(16))) short *foo;
1170 In this case, the mode is SI, but the type being modified is
1171 HI, so we need to look further. */
1173 while (POINTER_TYPE_P (type
)
1174 || TREE_CODE (type
) == FUNCTION_TYPE
1175 || TREE_CODE (type
) == ARRAY_TYPE
)
1176 type
= TREE_TYPE (type
);
1178 /* Get the mode of the type being modified. */
1179 orig_mode
= TYPE_MODE (type
);
1181 if (TREE_CODE (type
) == RECORD_TYPE
||
1182 (GET_MODE_CLASS (orig_mode
) != MODE_FLOAT
1183 && GET_MODE_CLASS (orig_mode
) != MODE_INT
))
1185 error ("invalid vector type for attribute `%s'",
1186 IDENTIFIER_POINTER (name
));
1190 /* Calculate how many units fit in the vector. */
1191 nunits
= vecsize
/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
1193 /* Find a suitably sized vector. */
1194 new_mode
= VOIDmode
;
1195 for (mode
= GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode
) == MODE_INT
1197 : MODE_VECTOR_FLOAT
);
1199 mode
= GET_MODE_WIDER_MODE (mode
))
1200 if (vecsize
== GET_MODE_SIZE (mode
) && nunits
== GET_MODE_NUNITS (mode
))
1206 if (new_mode
== VOIDmode
)
1207 error ("no vector mode with the size and type specified could be found");
1210 new_type
= type_for_mode (new_mode
, TREE_UNSIGNED (type
));
1212 error ("no vector mode with the size and type specified could be found");
1214 /* Build back pointers if needed. */
1215 *node
= vector_size_helper (*node
, new_type
);
1221 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1224 If we requested a pointer to a vector, build up the pointers that
1225 we stripped off while looking for the inner type. Similarly for
1226 return values from functions.
1228 The argument "type" is the top of the chain, and "bottom" is the
1229 new type which we will point to. */
1232 vector_size_helper (type
, bottom
)
1237 if (POINTER_TYPE_P (type
))
1239 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1240 outer
= build_pointer_type (inner
);
1242 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1244 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1245 outer
= build_array_type (inner
, TYPE_VALUES (type
));
1247 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
1249 inner
= vector_size_helper (TREE_TYPE (type
), bottom
);
1250 outer
= build_function_type (inner
, TYPE_VALUES (type
));
1255 TREE_READONLY (outer
) = TREE_READONLY (type
);
1256 TREE_THIS_VOLATILE (outer
) = TREE_THIS_VOLATILE (type
);
1261 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1262 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1264 The head of the declspec list is stored in DECLSPECS.
1265 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1267 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1268 the list elements. We drop the containing TREE_LIST nodes and link the
1269 resulting attributes together the way decl_attributes expects them. */
1272 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
1274 tree
*declspecs
, *prefix_attributes
;
1276 tree t
, s
, a
, next
, specs
, attrs
;
1278 /* This can happen after an __extension__ in pedantic mode. */
1279 if (specs_attrs
!= NULL_TREE
1280 && TREE_CODE (specs_attrs
) == INTEGER_CST
)
1282 *declspecs
= NULL_TREE
;
1283 *prefix_attributes
= NULL_TREE
;
1287 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1288 if (specs_attrs
!= NULL_TREE
1289 && TREE_CODE (specs_attrs
) != TREE_LIST
)
1291 *declspecs
= specs_attrs
;
1292 *prefix_attributes
= NULL_TREE
;
1296 /* Remember to keep the lists in the same order, element-wise. */
1298 specs
= s
= NULL_TREE
;
1299 attrs
= a
= NULL_TREE
;
1300 for (t
= specs_attrs
; t
; t
= next
)
1302 next
= TREE_CHAIN (t
);
1303 /* Declspecs have a non-NULL TREE_VALUE. */
1304 if (TREE_VALUE (t
) != NULL_TREE
)
1306 if (specs
== NULL_TREE
)
1314 /* The TREE_PURPOSE may also be empty in the case of
1315 __attribute__(()). */
1316 else if (TREE_PURPOSE (t
) != NULL_TREE
)
1318 if (attrs
== NULL_TREE
)
1319 attrs
= a
= TREE_PURPOSE (t
);
1322 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
1323 a
= TREE_PURPOSE (t
);
1325 /* More attrs can be linked here, move A to the end. */
1326 while (TREE_CHAIN (a
) != NULL_TREE
)
1331 /* Terminate the lists. */
1333 TREE_CHAIN (s
) = NULL_TREE
;
1335 TREE_CHAIN (a
) = NULL_TREE
;
1339 *prefix_attributes
= attrs
;
1342 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1343 This function is used by the parser when a rule will accept attributes
1344 in a particular position, but we don't want to support that just yet.
1346 A warning is issued for every ignored attribute. */
1349 strip_attrs (specs_attrs
)
1354 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
1358 warning ("`%s' attribute ignored",
1359 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
1360 attrs
= TREE_CHAIN (attrs
);