* emit-rtl.c (adjust_address_1): Always copy address to avoid
[official-gcc.git] / gcc / attribs.c
blob554591310d9a64f9b02cb51e6454959e1b4e87e7
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, 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. */
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 decl = *node;
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));
771 else
773 int j;
774 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
775 int len = strlen (p);
776 enum machine_mode mode = VOIDmode;
777 tree typefm;
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';
786 p = newp;
789 /* Give this decl a type with the specified mode.
790 First check for the special modes. */
791 if (! strcmp (p, "byte"))
792 mode = byte_mode;
793 else if (!strcmp (p, "word"))
794 mode = word_mode;
795 else if (! strcmp (p, "pointer"))
796 mode = ptr_mode;
797 else
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);
807 else
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);
816 return NULL_TREE;
819 /* Handle a "section" attribute; arguments as in
820 struct attribute_spec.handler. */
822 static tree
823 handle_section_attribute (node, name, args, flags, no_add_attrs)
824 tree *node;
825 tree name ATTRIBUTE_UNUSED;
826 tree args;
827 int flags ATTRIBUTE_UNUSED;
828 bool *no_add_attrs;
830 tree decl = *node;
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;
857 else
858 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
860 else
862 error_with_decl (*node,
863 "section attribute not allowed for `%s'");
864 *no_add_attrs = true;
867 else
869 error_with_decl (*node,
870 "section attributes are not supported for this target");
871 *no_add_attrs = true;
874 return NULL_TREE;
877 /* Handle a "aligned" attribute; arguments as in
878 struct attribute_spec.handler. */
880 static tree
881 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
882 tree *node;
883 tree name ATTRIBUTE_UNUSED;
884 tree args;
885 int flags;
886 bool *no_add_attrs;
888 tree decl = NULL_TREE;
889 tree *type = NULL;
890 int is_type = 0;
891 tree align_expr = (args ? TREE_VALUE (args)
892 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
893 int i;
895 if (DECL_P (*node))
897 decl = *node;
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;
925 else if (is_type)
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;
952 else
954 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
955 DECL_USER_ALIGN (decl) = 1;
958 return NULL_TREE;
961 /* Handle a "weak" attribute; arguments as in
962 struct attribute_spec.handler. */
964 static tree
965 handle_weak_attribute (node, name, args, flags, no_add_attrs)
966 tree *node;
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);
974 return NULL_TREE;
977 /* Handle an "alias" attribute; arguments as in
978 struct attribute_spec.handler. */
980 static tree
981 handle_alias_attribute (node, name, args, flags, no_add_attrs)
982 tree *node;
983 tree name;
984 tree args;
985 int flags ATTRIBUTE_UNUSED;
986 bool *no_add_attrs;
988 tree decl = *node;
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)
999 tree id;
1001 id = TREE_VALUE (args);
1002 if (TREE_CODE (id) != STRING_CST)
1004 error ("alias arg not a string");
1005 *no_add_attrs = true;
1006 return NULL_TREE;
1008 id = get_identifier (TREE_STRING_POINTER (id));
1009 /* This counts as a use of the object pointed to. */
1010 TREE_USED (id) = 1;
1012 if (TREE_CODE (decl) == FUNCTION_DECL)
1013 DECL_INITIAL (decl) = error_mark_node;
1014 else
1015 DECL_EXTERNAL (decl) = 0;
1016 assemble_alias (decl, id);
1018 else
1020 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1021 *no_add_attrs = true;
1024 return NULL_TREE;
1027 /* Handle a "no_instrument_function" attribute; arguments as in
1028 struct attribute_spec.handler. */
1030 static tree
1031 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
1032 tree *node;
1033 tree name;
1034 tree args ATTRIBUTE_UNUSED;
1035 int flags ATTRIBUTE_UNUSED;
1036 bool *no_add_attrs;
1038 tree decl = *node;
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;
1054 else
1055 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1057 return NULL_TREE;
1060 /* Handle a "malloc" attribute; arguments as in
1061 struct attribute_spec.handler. */
1063 static tree
1064 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
1065 tree *node;
1066 tree name;
1067 tree args ATTRIBUTE_UNUSED;
1068 int flags ATTRIBUTE_UNUSED;
1069 bool *no_add_attrs;
1071 if (TREE_CODE (*node) == FUNCTION_DECL)
1072 DECL_IS_MALLOC (*node) = 1;
1073 /* ??? TODO: Support types. */
1074 else
1076 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1077 *no_add_attrs = true;
1080 return NULL_TREE;
1083 /* Handle a "no_limit_stack" attribute; arguments as in
1084 struct attribute_spec.handler. */
1086 static tree
1087 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1088 tree *node;
1089 tree name;
1090 tree args ATTRIBUTE_UNUSED;
1091 int flags ATTRIBUTE_UNUSED;
1092 bool *no_add_attrs;
1094 tree decl = *node;
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;
1110 else
1111 DECL_NO_LIMIT_STACK (decl) = 1;
1113 return NULL_TREE;
1116 /* Handle a "pure" attribute; arguments as in
1117 struct attribute_spec.handler. */
1119 static tree
1120 handle_pure_attribute (node, name, args, flags, no_add_attrs)
1121 tree *node;
1122 tree name;
1123 tree args ATTRIBUTE_UNUSED;
1124 int flags ATTRIBUTE_UNUSED;
1125 bool *no_add_attrs;
1127 if (TREE_CODE (*node) == FUNCTION_DECL)
1128 DECL_IS_PURE (*node) = 1;
1129 /* ??? TODO: Support types. */
1130 else
1132 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1133 *no_add_attrs = true;
1136 return NULL_TREE;
1139 /* Handle a "vector_size" attribute; arguments as in
1140 struct attribute_spec.handler. */
1142 static tree
1143 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
1144 tree *node;
1145 tree name;
1146 tree args;
1147 int flags ATTRIBUTE_UNUSED;
1148 bool *no_add_attrs;
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));
1159 return NULL_TREE;
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));
1187 return NULL_TREE;
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
1196 ? MODE_VECTOR_INT
1197 : MODE_VECTOR_FLOAT);
1198 mode != VOIDmode;
1199 mode = GET_MODE_WIDER_MODE (mode))
1200 if (vecsize == GET_MODE_SIZE (mode) && nunits == GET_MODE_NUNITS (mode))
1202 new_mode = mode;
1203 break;
1206 if (new_mode == VOIDmode)
1207 error ("no vector mode with the size and type specified could be found");
1208 else
1210 new_type = type_for_mode (new_mode, TREE_UNSIGNED (type));
1211 if (!new_type)
1212 error ("no vector mode with the size and type specified could be found");
1213 else
1214 /* Build back pointers if needed. */
1215 *node = vector_size_helper (*node, new_type);
1218 return NULL_TREE;
1221 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
1222 better way.
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. */
1231 static tree
1232 vector_size_helper (type, bottom)
1233 tree type, bottom;
1235 tree inner, outer;
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));
1252 else
1253 return bottom;
1255 TREE_READONLY (outer) = TREE_READONLY (type);
1256 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
1258 return outer;
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. */
1271 void
1272 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1273 tree specs_attrs;
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;
1284 return;
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;
1293 return;
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)
1307 specs = s = t;
1308 else
1310 TREE_CHAIN (s) = t;
1311 s = t;
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);
1320 else
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)
1327 a = TREE_CHAIN (a);
1331 /* Terminate the lists. */
1332 if (s != NULL_TREE)
1333 TREE_CHAIN (s) = NULL_TREE;
1334 if (a != NULL_TREE)
1335 TREE_CHAIN (a) = NULL_TREE;
1337 /* All done. */
1338 *declspecs = specs;
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. */
1348 tree
1349 strip_attrs (specs_attrs)
1350 tree specs_attrs;
1352 tree specs, attrs;
1354 split_specs_attrs (specs_attrs, &specs, &attrs);
1356 while (attrs)
1358 warning ("`%s' attribute ignored",
1359 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1360 attrs = TREE_CHAIN (attrs);
1363 return specs;