* class.c (build_class_ref): Mark _Jv_fooClass DECL_EXTERNAL.
[official-gcc.git] / gcc / attribs.c
blob73d9d39961f267021cb1da0d161795dfd0978da1
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "rtl.h"
29 #include "ggc.h"
30 #include "expr.h"
31 #include "tm_p.h"
32 #include "obstack.h"
33 #include "cpplib.h"
34 #include "target.h"
36 static void init_attributes PARAMS ((void));
38 /* Table of the tables of attributes (common, format, language, machine)
39 searched. */
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,
45 bool *));
46 static tree handle_nocommon_attribute PARAMS ((tree *, tree, tree, int,
47 bool *));
48 static tree handle_common_attribute PARAMS ((tree *, tree, tree, int,
49 bool *));
50 static tree handle_noreturn_attribute PARAMS ((tree *, tree, tree, int,
51 bool *));
52 static tree handle_noinline_attribute PARAMS ((tree *, tree, tree, int,
53 bool *));
54 static tree handle_used_attribute PARAMS ((tree *, tree, tree, int,
55 bool *));
56 static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
57 bool *));
58 static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
59 bool *));
60 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
61 int, bool *));
62 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
63 bool *));
64 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
65 bool *));
66 static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
67 bool *));
68 static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
69 bool *));
70 static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
71 bool *));
72 static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
73 bool *));
74 static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
75 bool *));
76 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
77 tree, int,
78 bool *));
79 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
80 bool *));
81 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
82 bool *));
83 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
84 bool *));
85 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
86 bool *));
87 static tree vector_size_helper PARAMS ((tree, tree));
89 /* Table of machine-independent attributes common to all C-like languages. */
90 static const struct attribute_spec c_common_attribute_table[] =
92 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
93 { "packed", 0, 0, false, false, false,
94 handle_packed_attribute },
95 { "nocommon", 0, 0, true, false, false,
96 handle_nocommon_attribute },
97 { "common", 0, 0, true, false, false,
98 handle_common_attribute },
99 /* FIXME: logically, noreturn attributes should be listed as
100 "false, true, true" and apply to function types. But implementing this
101 would require all the places in the compiler that use TREE_THIS_VOLATILE
102 on a decl to identify non-returning functions to be located and fixed
103 to check the function type instead. */
104 { "noreturn", 0, 0, true, false, false,
105 handle_noreturn_attribute },
106 { "volatile", 0, 0, true, false, false,
107 handle_noreturn_attribute },
108 { "noinline", 0, 0, true, false, false,
109 handle_noinline_attribute },
110 { "used", 0, 0, true, false, false,
111 handle_used_attribute },
112 { "unused", 0, 0, false, false, false,
113 handle_unused_attribute },
114 /* The same comments as for noreturn attributes apply to const ones. */
115 { "const", 0, 0, true, false, false,
116 handle_const_attribute },
117 { "transparent_union", 0, 0, false, false, false,
118 handle_transparent_union_attribute },
119 { "constructor", 0, 0, true, false, false,
120 handle_constructor_attribute },
121 { "destructor", 0, 0, true, false, false,
122 handle_destructor_attribute },
123 { "mode", 1, 1, false, true, false,
124 handle_mode_attribute },
125 { "section", 1, 1, true, false, false,
126 handle_section_attribute },
127 { "aligned", 0, 1, false, false, false,
128 handle_aligned_attribute },
129 { "weak", 0, 0, true, false, false,
130 handle_weak_attribute },
131 { "alias", 1, 1, true, false, false,
132 handle_alias_attribute },
133 { "no_instrument_function", 0, 0, true, false, false,
134 handle_no_instrument_function_attribute },
135 { "malloc", 0, 0, true, false, false,
136 handle_malloc_attribute },
137 { "no_stack_limit", 0, 0, true, false, false,
138 handle_no_limit_stack_attribute },
139 { "pure", 0, 0, true, false, false,
140 handle_pure_attribute },
141 { "vector_size", 1, 1, false, true, false,
142 handle_vector_size_attribute },
143 { NULL, 0, 0, false, false, false, NULL }
146 /* Default empty table of attributes. */
147 static const struct attribute_spec empty_attribute_table[] =
149 { NULL, 0, 0, false, false, false, NULL }
152 /* Table of machine-independent attributes for checking formats, if used. */
153 const struct attribute_spec *format_attribute_table = empty_attribute_table;
155 /* Table of machine-independent attributes for a particular language. */
156 const struct attribute_spec *lang_attribute_table = empty_attribute_table;
158 /* Flag saying whether common language attributes are to be supported. */
159 int lang_attribute_common = 1;
161 /* Initialize attribute tables, and make some sanity checks
162 if --enable-checking. */
164 static void
165 init_attributes ()
167 #ifdef ENABLE_CHECKING
168 int i;
169 #endif
171 attribute_tables[0]
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. */
179 for (i = 0;
180 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
181 i++)
183 int j;
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] == '_')
192 abort ();
193 /* The minimum and maximum lengths must be consistent. */
194 if (attribute_tables[i][j].min_length < 0)
195 abort ();
196 if (attribute_tables[i][j].max_length != -1
197 && (attribute_tables[i][j].max_length
198 < attribute_tables[i][j].min_length))
199 abort ();
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)
203 abort ();
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)
208 abort ();
212 /* Check that each name occurs just once in each table. */
213 for (i = 0;
214 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
215 i++)
217 int j, k;
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))
222 abort ();
224 /* Check that no name occurs in more than one table. */
225 for (i = 0;
226 i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
227 i++)
229 int j, k, l;
231 for (j = i + 1;
232 j < ((int) (sizeof (attribute_tables)
233 / sizeof (attribute_tables[0])));
234 j++)
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))
239 abort ();
241 #endif
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. */
259 tree
260 decl_attributes (node, attributes, flags)
261 tree *node, attributes;
262 int flags;
264 tree a;
265 tree returned_attrs = NULL_TREE;
267 if (!attributes_initialized)
268 init_attributes ();
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);
280 tree *anode = node;
281 const struct attribute_spec *spec = NULL;
282 bool no_add_attrs = 0;
283 int i;
285 for (i = 0;
286 i < ((int) (sizeof (attribute_tables)
287 / sizeof (attribute_tables[0])));
288 i++)
290 int j;
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];
297 break;
300 if (spec != NULL)
301 break;
304 if (spec == NULL)
306 warning ("`%s' attribute directive ignored",
307 IDENTIFIER_POINTER (name));
308 continue;
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));
316 continue;
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);
327 continue;
329 else
331 warning ("`%s' attribute does not apply to types",
332 IDENTIFIER_POINTER (name));
333 continue;
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);
355 continue;
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));
363 continue;
367 if (spec->handler != NULL)
368 returned_attrs = chainon ((*spec->handler) (anode, name, args,
369 flags, &no_add_attrs),
370 returned_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);
383 if (!no_add_attrs)
385 tree old_attrs;
386 tree a;
388 if (DECL_P (*anode))
389 old_attrs = DECL_ATTRIBUTES (*anode);
390 else
391 old_attrs = TYPE_ATTRIBUTES (*anode);
393 for (a = lookup_attribute (spec->name, old_attrs);
394 a != NULL_TREE;
395 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
397 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
398 break;
401 if (a == NULL_TREE)
403 /* This attribute isn't already in the list. */
404 if (DECL_P (*anode))
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);
408 else
409 *anode = build_type_attribute_variant (*anode,
410 tree_cons (name, args,
411 old_attrs));
416 return returned_attrs;
419 /* Handle a "packed" attribute; arguments as in
420 struct attribute_spec.handler. */
422 static tree
423 handle_packed_attribute (node, name, args, flags, no_add_attrs)
424 tree *node;
425 tree name;
426 tree args ATTRIBUTE_UNUSED;
427 int flags;
428 bool *no_add_attrs;
430 tree *type = NULL;
431 if (DECL_P (*node))
433 if (TREE_CODE (*node) == TYPE_DECL)
434 type = &TREE_TYPE (*node);
436 else
437 type = node;
439 if (type)
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. */
449 else
451 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
452 *no_add_attrs = true;
455 return NULL_TREE;
458 /* Handle a "nocommon" attribute; arguments as in
459 struct attribute_spec.handler. */
461 static tree
462 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
463 tree *node;
464 tree name;
465 tree args ATTRIBUTE_UNUSED;
466 int flags ATTRIBUTE_UNUSED;
467 bool *no_add_attrs;
469 if (TREE_CODE (*node) == VAR_DECL)
470 DECL_COMMON (*node) = 0;
471 else
473 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
474 *no_add_attrs = true;
477 return NULL_TREE;
480 /* Handle a "common" attribute; arguments as in
481 struct attribute_spec.handler. */
483 static tree
484 handle_common_attribute (node, name, args, flags, no_add_attrs)
485 tree *node;
486 tree name;
487 tree args ATTRIBUTE_UNUSED;
488 int flags ATTRIBUTE_UNUSED;
489 bool *no_add_attrs;
491 if (TREE_CODE (*node) == VAR_DECL)
492 DECL_COMMON (*node) = 1;
493 else
495 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
496 *no_add_attrs = true;
499 return NULL_TREE;
502 /* Handle a "noreturn" attribute; arguments as in
503 struct attribute_spec.handler. */
505 static tree
506 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
507 tree *node;
508 tree name;
509 tree args ATTRIBUTE_UNUSED;
510 int flags ATTRIBUTE_UNUSED;
511 bool *no_add_attrs;
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)
520 TREE_TYPE (*node)
521 = build_pointer_type
522 (build_type_variant (TREE_TYPE (type),
523 TREE_READONLY (TREE_TYPE (type)), 1));
524 else
526 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
527 *no_add_attrs = true;
530 return NULL_TREE;
533 /* Handle a "noinline" attribute; arguments as in
534 struct attribute_spec.handler. */
536 static tree
537 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
538 tree *node;
539 tree name;
540 tree args ATTRIBUTE_UNUSED;
541 int flags ATTRIBUTE_UNUSED;
542 bool *no_add_attrs;
544 if (TREE_CODE (*node) == FUNCTION_DECL)
545 DECL_UNINLINABLE (*node) = 1;
546 else
548 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
549 *no_add_attrs = true;
552 return NULL_TREE;
555 /* Handle a "used" attribute; arguments as in
556 struct attribute_spec.handler. */
558 static tree
559 handle_used_attribute (node, name, args, flags, no_add_attrs)
560 tree *node;
561 tree name;
562 tree args ATTRIBUTE_UNUSED;
563 int flags ATTRIBUTE_UNUSED;
564 bool *no_add_attrs;
566 if (TREE_CODE (*node) == FUNCTION_DECL)
567 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
568 = TREE_USED (*node) = 1;
569 else
571 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
572 *no_add_attrs = true;
575 return NULL_TREE;
578 /* Handle a "unused" attribute; arguments as in
579 struct attribute_spec.handler. */
581 static tree
582 handle_unused_attribute (node, name, args, flags, no_add_attrs)
583 tree *node;
584 tree name;
585 tree args ATTRIBUTE_UNUSED;
586 int flags;
587 bool *no_add_attrs;
589 if (DECL_P (*node))
591 tree decl = *node;
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;
599 else
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 *no_add_attrs = true;
605 else
607 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
608 *node = build_type_copy (*node);
609 TREE_USED (*node) = 1;
612 return NULL_TREE;
615 /* Handle a "const" attribute; arguments as in
616 struct attribute_spec.handler. */
618 static tree
619 handle_const_attribute (node, name, args, flags, no_add_attrs)
620 tree *node;
621 tree name;
622 tree args ATTRIBUTE_UNUSED;
623 int flags ATTRIBUTE_UNUSED;
624 bool *no_add_attrs;
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)
633 TREE_TYPE (*node)
634 = build_pointer_type
635 (build_type_variant (TREE_TYPE (type), 1,
636 TREE_THIS_VOLATILE (TREE_TYPE (type))));
637 else
639 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
640 *no_add_attrs = true;
643 return NULL_TREE;
646 /* Handle a "transparent_union" attribute; arguments as in
647 struct attribute_spec.handler. */
649 static tree
650 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
651 tree *node;
652 tree name;
653 tree args ATTRIBUTE_UNUSED;
654 int flags;
655 bool *no_add_attrs;
657 tree decl = NULL_TREE;
658 tree *type = NULL;
659 int is_type = 0;
661 if (DECL_P (*node))
663 decl = *node;
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;
670 if (is_type
671 && TREE_CODE (*type) == UNION_TYPE
672 && (decl == 0
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;
684 else
686 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
687 *no_add_attrs = true;
690 return NULL_TREE;
693 /* Handle a "constructor" attribute; arguments as in
694 struct attribute_spec.handler. */
696 static tree
697 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
698 tree *node;
699 tree name;
700 tree args ATTRIBUTE_UNUSED;
701 int flags ATTRIBUTE_UNUSED;
702 bool *no_add_attrs;
704 tree decl = *node;
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;
714 else
716 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
717 *no_add_attrs = true;
720 return NULL_TREE;
723 /* Handle a "destructor" attribute; arguments as in
724 struct attribute_spec.handler. */
726 static tree
727 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
728 tree *node;
729 tree name;
730 tree args ATTRIBUTE_UNUSED;
731 int flags ATTRIBUTE_UNUSED;
732 bool *no_add_attrs;
734 tree decl = *node;
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;
744 else
746 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
747 *no_add_attrs = true;
750 return NULL_TREE;
753 /* Handle a "mode" attribute; arguments as in
754 struct attribute_spec.handler. */
756 static tree
757 handle_mode_attribute (node, name, args, flags, no_add_attrs)
758 tree *node;
759 tree name;
760 tree args;
761 int flags ATTRIBUTE_UNUSED;
762 bool *no_add_attrs;
764 tree type = *node;
766 *no_add_attrs = true;
768 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
769 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
770 else
772 int j;
773 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
774 int len = strlen (p);
775 enum machine_mode mode = VOIDmode;
776 tree typefm;
778 if (len > 4 && p[0] == '_' && p[1] == '_'
779 && p[len - 1] == '_' && p[len - 2] == '_')
781 char *newp = (char *) alloca (len - 1);
783 strcpy (newp, &p[2]);
784 newp[len - 4] = '\0';
785 p = newp;
788 /* Change this type to have a type with the specified mode.
789 First check for the special modes. */
790 if (! strcmp (p, "byte"))
791 mode = byte_mode;
792 else if (!strcmp (p, "word"))
793 mode = word_mode;
794 else if (! strcmp (p, "pointer"))
795 mode = ptr_mode;
796 else
797 for (j = 0; j < NUM_MACHINE_MODES; j++)
798 if (!strcmp (p, GET_MODE_NAME (j)))
799 mode = (enum machine_mode) j;
801 if (mode == VOIDmode)
802 error ("unknown machine mode `%s'", p);
803 else if (0 == (typefm = type_for_mode (mode,
804 TREE_UNSIGNED (type))))
805 error ("no data type for mode `%s'", p);
806 else
807 *node = typefm;
808 /* No need to layout the type here. The caller should do this. */
811 return NULL_TREE;
814 /* Handle a "section" attribute; arguments as in
815 struct attribute_spec.handler. */
817 static tree
818 handle_section_attribute (node, name, args, flags, no_add_attrs)
819 tree *node;
820 tree name ATTRIBUTE_UNUSED;
821 tree args;
822 int flags ATTRIBUTE_UNUSED;
823 bool *no_add_attrs;
825 tree decl = *node;
827 if (targetm.have_named_sections)
829 if ((TREE_CODE (decl) == FUNCTION_DECL
830 || TREE_CODE (decl) == VAR_DECL)
831 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
833 if (TREE_CODE (decl) == VAR_DECL
834 && current_function_decl != NULL_TREE
835 && ! TREE_STATIC (decl))
837 error_with_decl (decl,
838 "section attribute cannot be specified for local variables");
839 *no_add_attrs = true;
842 /* The decl may have already been given a section attribute
843 from a previous declaration. Ensure they match. */
844 else if (DECL_SECTION_NAME (decl) != NULL_TREE
845 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
846 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
848 error_with_decl (*node,
849 "section of `%s' conflicts with previous declaration");
850 *no_add_attrs = true;
852 else
853 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
855 else
857 error_with_decl (*node,
858 "section attribute not allowed for `%s'");
859 *no_add_attrs = true;
862 else
864 error_with_decl (*node,
865 "section attributes are not supported for this target");
866 *no_add_attrs = true;
869 return NULL_TREE;
872 /* Handle a "aligned" attribute; arguments as in
873 struct attribute_spec.handler. */
875 static tree
876 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
877 tree *node;
878 tree name ATTRIBUTE_UNUSED;
879 tree args;
880 int flags;
881 bool *no_add_attrs;
883 tree decl = NULL_TREE;
884 tree *type = NULL;
885 int is_type = 0;
886 tree align_expr = (args ? TREE_VALUE (args)
887 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
888 int i;
890 if (DECL_P (*node))
892 decl = *node;
893 type = &TREE_TYPE (decl);
894 is_type = TREE_CODE (*node) == TYPE_DECL;
896 else if (TYPE_P (*node))
897 type = node, is_type = 1;
899 /* Strip any NOPs of any kind. */
900 while (TREE_CODE (align_expr) == NOP_EXPR
901 || TREE_CODE (align_expr) == CONVERT_EXPR
902 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
903 align_expr = TREE_OPERAND (align_expr, 0);
905 if (TREE_CODE (align_expr) != INTEGER_CST)
907 error ("requested alignment is not a constant");
908 *no_add_attrs = true;
910 else if ((i = tree_log2 (align_expr)) == -1)
912 error ("requested alignment is not a power of 2");
913 *no_add_attrs = true;
915 else if (i > HOST_BITS_PER_INT - 2)
917 error ("requested alignment is too large");
918 *no_add_attrs = true;
920 else if (is_type)
922 /* If we have a TYPE_DECL, then copy the type, so that we
923 don't accidentally modify a builtin type. See pushdecl. */
924 if (decl && TREE_TYPE (decl) != error_mark_node
925 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
927 tree tt = TREE_TYPE (decl);
928 *type = build_type_copy (*type);
929 DECL_ORIGINAL_TYPE (decl) = tt;
930 TYPE_NAME (*type) = decl;
931 TREE_USED (*type) = TREE_USED (decl);
932 TREE_TYPE (decl) = *type;
934 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
935 *type = build_type_copy (*type);
937 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
938 TYPE_USER_ALIGN (*type) = 1;
940 else if (TREE_CODE (decl) != VAR_DECL
941 && TREE_CODE (decl) != FIELD_DECL)
943 error_with_decl (decl,
944 "alignment may not be specified for `%s'");
945 *no_add_attrs = true;
947 else
949 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
950 DECL_USER_ALIGN (decl) = 1;
953 return NULL_TREE;
956 /* Handle a "weak" attribute; arguments as in
957 struct attribute_spec.handler. */
959 static tree
960 handle_weak_attribute (node, name, args, flags, no_add_attrs)
961 tree *node;
962 tree name ATTRIBUTE_UNUSED;
963 tree args ATTRIBUTE_UNUSED;
964 int flags ATTRIBUTE_UNUSED;
965 bool *no_add_attrs ATTRIBUTE_UNUSED;
967 declare_weak (*node);
969 return NULL_TREE;
972 /* Handle an "alias" attribute; arguments as in
973 struct attribute_spec.handler. */
975 static tree
976 handle_alias_attribute (node, name, args, flags, no_add_attrs)
977 tree *node;
978 tree name;
979 tree args;
980 int flags ATTRIBUTE_UNUSED;
981 bool *no_add_attrs;
983 tree decl = *node;
985 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
986 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
988 error_with_decl (decl,
989 "`%s' defined both normally and as an alias");
990 *no_add_attrs = true;
992 else if (decl_function_context (decl) == 0)
994 tree id;
996 id = TREE_VALUE (args);
997 if (TREE_CODE (id) != STRING_CST)
999 error ("alias arg not a string");
1000 *no_add_attrs = true;
1001 return NULL_TREE;
1003 id = get_identifier (TREE_STRING_POINTER (id));
1004 /* This counts as a use of the object pointed to. */
1005 TREE_USED (id) = 1;
1007 if (TREE_CODE (decl) == FUNCTION_DECL)
1008 DECL_INITIAL (decl) = error_mark_node;
1009 else
1010 DECL_EXTERNAL (decl) = 0;
1011 assemble_alias (decl, id);
1013 else
1015 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1016 *no_add_attrs = true;
1019 return NULL_TREE;
1022 /* Handle a "no_instrument_function" attribute; arguments as in
1023 struct attribute_spec.handler. */
1025 static tree
1026 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1027 tree *node;
1028 tree name;
1029 tree args ATTRIBUTE_UNUSED;
1030 int flags ATTRIBUTE_UNUSED;
1031 bool *no_add_attrs;
1033 tree decl = *node;
1035 if (TREE_CODE (decl) != FUNCTION_DECL)
1037 error_with_decl (decl,
1038 "`%s' attribute applies only to functions",
1039 IDENTIFIER_POINTER (name));
1040 *no_add_attrs = true;
1042 else if (DECL_INITIAL (decl))
1044 error_with_decl (decl,
1045 "can't set `%s' attribute after definition",
1046 IDENTIFIER_POINTER (name));
1047 *no_add_attrs = true;
1049 else
1050 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1052 return NULL_TREE;
1055 /* Handle a "malloc" attribute; arguments as in
1056 struct attribute_spec.handler. */
1058 static tree
1059 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1060 tree *node;
1061 tree name;
1062 tree args ATTRIBUTE_UNUSED;
1063 int flags ATTRIBUTE_UNUSED;
1064 bool *no_add_attrs;
1066 if (TREE_CODE (*node) == FUNCTION_DECL)
1067 DECL_IS_MALLOC (*node) = 1;
1068 /* ??? TODO: Support types. */
1069 else
1071 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1072 *no_add_attrs = true;
1075 return NULL_TREE;
1078 /* Handle a "no_limit_stack" attribute; arguments as in
1079 struct attribute_spec.handler. */
1081 static tree
1082 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1083 tree *node;
1084 tree name;
1085 tree args ATTRIBUTE_UNUSED;
1086 int flags ATTRIBUTE_UNUSED;
1087 bool *no_add_attrs;
1089 tree decl = *node;
1091 if (TREE_CODE (decl) != FUNCTION_DECL)
1093 error_with_decl (decl,
1094 "`%s' attribute applies only to functions",
1095 IDENTIFIER_POINTER (name));
1096 *no_add_attrs = true;
1098 else if (DECL_INITIAL (decl))
1100 error_with_decl (decl,
1101 "can't set `%s' attribute after definition",
1102 IDENTIFIER_POINTER (name));
1103 *no_add_attrs = true;
1105 else
1106 DECL_NO_LIMIT_STACK (decl) = 1;
1108 return NULL_TREE;
1111 /* Handle a "pure" attribute; arguments as in
1112 struct attribute_spec.handler. */
1114 static tree
1115 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1116 tree *node;
1117 tree name;
1118 tree args ATTRIBUTE_UNUSED;
1119 int flags ATTRIBUTE_UNUSED;
1120 bool *no_add_attrs;
1122 if (TREE_CODE (*node) == FUNCTION_DECL)
1123 DECL_IS_PURE (*node) = 1;
1124 /* ??? TODO: Support types. */
1125 else
1127 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1128 *no_add_attrs = true;
1131 return NULL_TREE;
1134 /* Handle a "vector_size" attribute; arguments as in
1135 struct attribute_spec.handler. */
1137 static tree
1138 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1139 tree *node;
1140 tree name;
1141 tree args;
1142 int flags ATTRIBUTE_UNUSED;
1143 bool *no_add_attrs;
1145 unsigned int vecsize, nunits;
1146 enum machine_mode mode, orig_mode, new_mode;
1147 tree type = *node, new_type;
1149 *no_add_attrs = true;
1151 if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
1153 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1154 return NULL_TREE;
1157 /* Get the vector size (in bytes). */
1158 vecsize = TREE_INT_CST_LOW (TREE_VALUE (args));
1160 /* We need to provide for vector pointers, vector arrays, and
1161 functions returning vectors. For example:
1163 __attribute__((vector_size(16))) short *foo;
1165 In this case, the mode is SI, but the type being modified is
1166 HI, so we need to look further. */
1168 while (POINTER_TYPE_P (type)
1169 || TREE_CODE (type) == FUNCTION_TYPE
1170 || TREE_CODE (type) == ARRAY_TYPE)
1171 type = TREE_TYPE (type);
1173 /* Get the mode of the type being modified. */
1174 orig_mode = TYPE_MODE (type);
1176 if (TREE_CODE (type) == RECORD_TYPE ||
1177 (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1178 && GET_MODE_CLASS (orig_mode) != MODE_INT))
1180 error ("invalid vector type for attribute `%s'",
1181 IDENTIFIER_POINTER (name));
1182 return NULL_TREE;
1185 /* Calculate how many units fit in the vector. */
1186 nunits = vecsize / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
1188 /* Find a suitably sized vector. */
1189 new_mode = VOIDmode;
1190 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
1191 ? MODE_VECTOR_INT
1192 : MODE_VECTOR_FLOAT);
1193 mode != VOIDmode;
1194 mode = GET_MODE_WIDER_MODE (mode))
1195 if (vecsize == GET_MODE_SIZE (mode) && nunits == GET_MODE_NUNITS (mode))
1197 new_mode = mode;
1198 break;
1201 if (new_mode == VOIDmode)
1202 error ("no vector mode with the size and type specified could be found");
1203 else
1205 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1206 if (!new_type)
1207 error ("no vector mode with the size and type specified could be found");
1208 else
1209 /* Build back pointers if needed. */
1210 *node = vector_size_helper (*node, new_type);
1213 return NULL_TREE;
1216 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1217 better way.
1219 If we requested a pointer to a vector, build up the pointers that
1220 we stripped off while looking for the inner type. Similarly for
1221 return values from functions.
1223 The argument "type" is the top of the chain, and "bottom" is the
1224 new type which we will point to. */
1226 static tree
1227 vector_size_helper (type, bottom)
1228 tree type, bottom;
1230 tree inner, outer;
1232 if (POINTER_TYPE_P (type))
1234 inner = vector_size_helper (TREE_TYPE (type), bottom);
1235 outer = build_pointer_type (inner);
1237 else if (TREE_CODE (type) == ARRAY_TYPE)
1239 inner = vector_size_helper (TREE_TYPE (type), bottom);
1240 outer = build_array_type (inner, TYPE_VALUES (type));
1242 else if (TREE_CODE (type) == FUNCTION_TYPE)
1244 inner = vector_size_helper (TREE_TYPE (type), bottom);
1245 outer = build_function_type (inner, TYPE_VALUES (type));
1247 else
1248 return bottom;
1250 TREE_READONLY (outer) = TREE_READONLY (type);
1251 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1253 return outer;
1256 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1257 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1259 The head of the declspec list is stored in DECLSPECS.
1260 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1262 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1263 the list elements. We drop the containing TREE_LIST nodes and link the
1264 resulting attributes together the way decl_attributes expects them. */
1266 void
1267 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1268 tree specs_attrs;
1269 tree *declspecs, *prefix_attributes;
1271 tree t, s, a, next, specs, attrs;
1273 /* This can happen after an __extension__ in pedantic mode. */
1274 if (specs_attrs != NULL_TREE
1275 && TREE_CODE (specs_attrs) == INTEGER_CST)
1277 *declspecs = NULL_TREE;
1278 *prefix_attributes = NULL_TREE;
1279 return;
1282 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1283 if (specs_attrs != NULL_TREE
1284 && TREE_CODE (specs_attrs) != TREE_LIST)
1286 *declspecs = specs_attrs;
1287 *prefix_attributes = NULL_TREE;
1288 return;
1291 /* Remember to keep the lists in the same order, element-wise. */
1293 specs = s = NULL_TREE;
1294 attrs = a = NULL_TREE;
1295 for (t = specs_attrs; t; t = next)
1297 next = TREE_CHAIN (t);
1298 /* Declspecs have a non-NULL TREE_VALUE. */
1299 if (TREE_VALUE (t) != NULL_TREE)
1301 if (specs == NULL_TREE)
1302 specs = s = t;
1303 else
1305 TREE_CHAIN (s) = t;
1306 s = t;
1309 /* The TREE_PURPOSE may also be empty in the case of
1310 __attribute__(()). */
1311 else if (TREE_PURPOSE (t) != NULL_TREE)
1313 if (attrs == NULL_TREE)
1314 attrs = a = TREE_PURPOSE (t);
1315 else
1317 TREE_CHAIN (a) = TREE_PURPOSE (t);
1318 a = TREE_PURPOSE (t);
1320 /* More attrs can be linked here, move A to the end. */
1321 while (TREE_CHAIN (a) != NULL_TREE)
1322 a = TREE_CHAIN (a);
1326 /* Terminate the lists. */
1327 if (s != NULL_TREE)
1328 TREE_CHAIN (s) = NULL_TREE;
1329 if (a != NULL_TREE)
1330 TREE_CHAIN (a) = NULL_TREE;
1332 /* All done. */
1333 *declspecs = specs;
1334 *prefix_attributes = attrs;
1337 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1338 This function is used by the parser when a rule will accept attributes
1339 in a particular position, but we don't want to support that just yet.
1341 A warning is issued for every ignored attribute. */
1343 tree
1344 strip_attrs (specs_attrs)
1345 tree specs_attrs;
1347 tree specs, attrs;
1349 split_specs_attrs (specs_attrs, &specs, &attrs);
1351 while (attrs)
1353 warning ("`%s' attribute ignored",
1354 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1355 attrs = TREE_CHAIN (attrs);
1358 return specs;