Daily bump.
[platform/upstream/gcc.git] / gcc / attribs.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
35
36 static void init_attributes             PARAMS ((void));
37
38 /* Table of the tables of attributes (common, format, language, machine)
39    searched.  */
40 static const struct attribute_spec *attribute_tables[4];
41
42 static bool attributes_initialized = false;
43
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_unused_attribute     PARAMS ((tree *, tree, tree, int,
53                                                  bool *));
54 static tree handle_const_attribute      PARAMS ((tree *, tree, tree, int,
55                                                  bool *));
56 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
57                                                         int, bool *));
58 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
59                                                   bool *));
60 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
61                                                  bool *));
62 static tree handle_mode_attribute       PARAMS ((tree *, tree, tree, int,
63                                                  bool *));
64 static tree handle_section_attribute    PARAMS ((tree *, tree, tree, int,
65                                                  bool *));
66 static tree handle_aligned_attribute    PARAMS ((tree *, tree, tree, int,
67                                                  bool *));
68 static tree handle_weak_attribute       PARAMS ((tree *, tree, tree, int,
69                                                  bool *));
70 static tree handle_alias_attribute      PARAMS ((tree *, tree, tree, int,
71                                                  bool *));
72 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
73                                                              tree, int,
74                                                              bool *));
75 static tree handle_no_check_memory_usage_attribute PARAMS ((tree *, tree, tree,
76                                                             int, bool *));
77 static tree handle_malloc_attribute     PARAMS ((tree *, tree, tree, int,
78                                                  bool *));
79 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
80                                                      bool *));
81 static tree handle_pure_attribute       PARAMS ((tree *, tree, tree, int,
82                                                  bool *));
83
84 /* Table of machine-independent attributes common to all C-like languages.  */
85 static const struct attribute_spec c_common_attribute_table[] =
86 {
87   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
88   { "packed",                 0, 0, false, false, false,
89                               handle_packed_attribute },
90   { "nocommon",               0, 0, true,  false, false,
91                               handle_nocommon_attribute },
92   { "common",                 0, 0, true,  false, false,
93                               handle_common_attribute },
94   /* FIXME: logically, noreturn attributes should be listed as
95      "false, true, true" and apply to function types.  But implementing this
96      would require all the places in the compiler that use TREE_THIS_VOLATILE
97      on a decl to identify non-returning functions to be located and fixed
98      to check the function type instead.  */
99   { "noreturn",               0, 0, true,  false, false,
100                               handle_noreturn_attribute },
101   { "volatile",               0, 0, true,  false, false,
102                               handle_noreturn_attribute },
103   { "unused",                 0, 0, false, false, false,
104                               handle_unused_attribute },
105   /* The same comments as for noreturn attributes apply to const ones.  */
106   { "const",                  0, 0, true,  false, false,
107                               handle_const_attribute },
108   { "transparent_union",      0, 0, false, false, false,
109                               handle_transparent_union_attribute },
110   { "constructor",            0, 0, true,  false, false,
111                               handle_constructor_attribute },
112   { "destructor",             0, 0, true,  false, false,
113                               handle_destructor_attribute },
114   { "mode",                   1, 1, true,  false, false,
115                               handle_mode_attribute },
116   { "section",                1, 1, true,  false, false,
117                               handle_section_attribute },
118   { "aligned",                0, 1, false, false, false,
119                               handle_aligned_attribute },
120   { "weak",                   0, 0, true,  false, false,
121                               handle_weak_attribute },
122   { "alias",                  1, 1, true,  false, false,
123                               handle_alias_attribute },
124   { "no_instrument_function", 0, 0, true,  false, false,
125                               handle_no_instrument_function_attribute },
126   { "no_check_memory_usage",  0, 0, true,  false, false,
127                               handle_no_check_memory_usage_attribute },
128   { "malloc",                 0, 0, true,  false, false,
129                               handle_malloc_attribute },
130   { "no_stack_limit",         0, 0, true,  false, false,
131                               handle_no_limit_stack_attribute },
132   { "pure",                   0, 0, true,  false, false,
133                               handle_pure_attribute },
134   { NULL,                     0, 0, false, false, false, NULL }
135 };
136
137 /* Default empty table of attributes.  */
138 static const struct attribute_spec empty_attribute_table[] =
139 {
140   { NULL, 0, 0, false, false, false, NULL }
141 };
142
143 /* Table of machine-independent attributes for checking formats, if used.  */
144 const struct attribute_spec *format_attribute_table = empty_attribute_table;
145
146 /* Table of machine-independent attributes for a particular language.  */
147 const struct attribute_spec *lang_attribute_table = empty_attribute_table;
148
149 /* Flag saying whether common language attributes are to be supported.  */
150 int lang_attribute_common = 1;
151
152 /* Initialize attribute tables, and make some sanity checks
153    if --enable-checking.  */
154
155 static void
156 init_attributes ()
157 {
158 #ifdef ENABLE_CHECKING
159   int i;
160 #endif
161
162   attribute_tables[0]
163     = lang_attribute_common ? c_common_attribute_table : empty_attribute_table;
164   attribute_tables[1] = lang_attribute_table;
165   attribute_tables[2] = format_attribute_table;
166   attribute_tables[3] = targetm.attribute_table;
167
168 #ifdef ENABLE_CHECKING
169   /* Make some sanity checks on the attribute tables.  */
170   for (i = 0;
171        i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
172        i++)
173     {
174       int j;
175
176       for (j = 0; attribute_tables[i][j].name != NULL; j++)
177         {
178           /* The name must not begin and end with __.  */
179           const char *name = attribute_tables[i][j].name;
180           int len = strlen (name);
181           if (name[0] == '_' && name[1] == '_'
182               && name[len - 1] == '_' && name[len - 2] == '_')
183             abort ();
184           /* The minimum and maximum lengths must be consistent.  */
185           if (attribute_tables[i][j].min_length < 0)
186             abort ();
187           if (attribute_tables[i][j].max_length != -1
188               && (attribute_tables[i][j].max_length
189                   < attribute_tables[i][j].min_length))
190             abort ();
191           /* An attribute cannot require both a DECL and a TYPE.  */
192           if (attribute_tables[i][j].decl_required
193               && attribute_tables[i][j].type_required)
194             abort ();
195           /* If an attribute requires a function type, in particular
196              it requires a type.  */
197           if (attribute_tables[i][j].function_type_required
198               && !attribute_tables[i][j].type_required)
199             abort ();
200         }
201     }
202
203   /* Check that each name occurs just once in each table.  */
204   for (i = 0;
205        i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
206        i++)
207     {
208       int j, k;
209       for (j = 0; attribute_tables[i][j].name != NULL; j++)
210         for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
211           if (!strcmp (attribute_tables[i][j].name,
212                        attribute_tables[i][k].name))
213             abort ();
214     }
215   /* Check that no name occurs in more than one table.  */
216   for (i = 0;
217        i < (int) (sizeof (attribute_tables) / sizeof (attribute_tables[0]));
218        i++)
219     {
220       int j, k, l;
221
222       for (j = i + 1;
223            j < ((int) (sizeof (attribute_tables)
224                        / sizeof (attribute_tables[0])));
225            j++)
226         for (k = 0; attribute_tables[i][k].name != NULL; k++)
227           for (l = 0; attribute_tables[j][l].name != NULL; l++)
228             if (!strcmp (attribute_tables[i][k].name,
229                          attribute_tables[j][l].name))
230               abort ();
231     }
232 #endif
233
234   attributes_initialized = true;
235 }
236 \f
237 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
238    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
239    it should be modified in place; if a TYPE, a copy should be created
240    unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS.  FLAGS gives further
241    information, in the form of a bitwise OR of flags in enum attribute_flags
242    from tree.h.  Depending on these flags, some attributes may be
243    returned to be applied at a later stage (for example, to apply
244    a decl attribute to the declaration rather than to its type).  If
245    ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
246    whether there might be some default attributes to apply to this DECL;
247    if so, decl_attributes will be called recusrively with those attributes
248    and ATTR_FLAG_BUILT_IN set.  */
249
250 tree
251 decl_attributes (node, attributes, flags)
252      tree *node, attributes;
253      int flags;
254 {
255   tree a;
256   tree returned_attrs = NULL_TREE;
257
258   if (!attributes_initialized)
259     init_attributes ();
260
261   (*targetm.insert_attributes) (*node, &attributes);
262
263   if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
264       && !(flags & (int) ATTR_FLAG_BUILT_IN))
265     insert_default_attributes (*node);
266
267   for (a = attributes; a; a = TREE_CHAIN (a))
268     {
269       tree name = TREE_PURPOSE (a);
270       tree args = TREE_VALUE (a);
271       tree *anode = node;
272       const struct attribute_spec *spec = NULL;
273       bool no_add_attrs = 0;
274       int i;
275
276       for (i = 0;
277            i < ((int) (sizeof (attribute_tables)
278                        / sizeof (attribute_tables[0])));
279            i++)
280         {
281           int j;
282
283           for (j = 0; attribute_tables[i][j].name != NULL; j++)
284             {
285               if (is_attribute_p (attribute_tables[i][j].name, name))
286                 {
287                   spec = &attribute_tables[i][j];
288                   break;
289                 }
290             }
291           if (spec != NULL)
292             break;
293         }
294
295       if (spec == NULL)
296         {
297           warning ("`%s' attribute directive ignored",
298                    IDENTIFIER_POINTER (name));
299           continue;
300         }
301       else if (list_length (args) < spec->min_length
302                || (spec->max_length >= 0
303                    && list_length (args) > spec->max_length))
304         {
305           error ("wrong number of arguments specified for `%s' attribute",
306                  IDENTIFIER_POINTER (name));
307           continue;
308         }
309
310       if (spec->decl_required && !DECL_P (*anode))
311         {
312           if (flags & ((int) ATTR_FLAG_DECL_NEXT
313                        | (int) ATTR_FLAG_FUNCTION_NEXT
314                        | (int) ATTR_FLAG_ARRAY_NEXT))
315             {
316               /* Pass on this attribute to be tried again.  */
317               returned_attrs = tree_cons (name, args, returned_attrs);
318               continue;
319             }
320           else
321             {
322               warning ("`%s' attribute does not apply to types",
323                        IDENTIFIER_POINTER (name));
324               continue;
325             }
326         }
327
328       if (spec->type_required && DECL_P (*anode))
329         anode = &TREE_TYPE (*anode);
330
331       if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
332           && TREE_CODE (*anode) != METHOD_TYPE)
333         {
334           if (TREE_CODE (*anode) == POINTER_TYPE
335               && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
336                   || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
337             {
338               if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
339                 *anode = build_type_copy (*anode);
340               anode = &TREE_TYPE (*anode);
341             }
342           else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
343             {
344               /* Pass on this attribute to be tried again.  */
345               returned_attrs = tree_cons (name, args, returned_attrs);
346               continue;
347             }
348
349           if (TREE_CODE (*anode) != FUNCTION_TYPE
350               && TREE_CODE (*anode) != METHOD_TYPE)
351             {
352               warning ("`%s' attribute only applies to function types",
353                        IDENTIFIER_POINTER (name));
354               continue;
355             }
356         }
357
358       if (spec->handler != NULL)
359         returned_attrs = chainon ((*spec->handler) (anode, name, args,
360                                                     flags, &no_add_attrs),
361                                   returned_attrs);
362       if (!no_add_attrs)
363         {
364           tree old_attrs;
365           tree a;
366
367           if (DECL_P (*anode))
368             old_attrs = DECL_ATTRIBUTES (*anode);
369           else
370             old_attrs = TYPE_ATTRIBUTES (*anode);
371
372           for (a = lookup_attribute (spec->name, old_attrs);
373                a != NULL_TREE;
374                a = lookup_attribute (spec->name, TREE_CHAIN (a)))
375             {
376               if (simple_cst_equal (TREE_VALUE (a), args) == 1)
377                 break;
378             }
379
380           if (a == NULL_TREE)
381             {
382               /* This attribute isn't already in the list.  */
383               if (DECL_P (*anode))
384                 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
385               else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
386                 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
387               else
388                 *anode = build_type_attribute_variant (*anode,
389                                                        tree_cons (name, args,
390                                                                   old_attrs));
391             }
392         }
393     }
394
395   return returned_attrs;
396 }
397
398 /* Handle a "packed" attribute; arguments as in
399    struct attribute_spec.handler.  */
400
401 static tree
402 handle_packed_attribute (node, name, args, flags, no_add_attrs)
403      tree *node;
404      tree name;
405      tree args ATTRIBUTE_UNUSED;
406      int flags;
407      bool *no_add_attrs;
408 {
409   tree *type = NULL;
410   if (DECL_P (*node))
411     {
412       if (TREE_CODE (*node) == TYPE_DECL)
413         type = &TREE_TYPE (*node);
414     }
415   else
416     type = node;
417
418   if (type)
419     {
420       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
421         *type = build_type_copy (*type);
422       TYPE_PACKED (*type) = 1;
423     }
424   else if (TREE_CODE (*node) == FIELD_DECL)
425     DECL_PACKED (*node) = 1;
426   /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
427      used for DECL_REGISTER.  It wouldn't mean anything anyway.  */
428   else
429     {
430       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
431       *no_add_attrs = true;
432     }
433
434   return NULL_TREE;
435 }
436
437 /* Handle a "nocommon" attribute; arguments as in
438    struct attribute_spec.handler.  */
439
440 static tree
441 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
442      tree *node;
443      tree name;
444      tree args ATTRIBUTE_UNUSED;
445      int flags ATTRIBUTE_UNUSED;
446      bool *no_add_attrs;
447 {
448   if (TREE_CODE (*node) == VAR_DECL)
449     DECL_COMMON (*node) = 0;
450   else
451     {
452       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
453       *no_add_attrs = true;
454     }
455
456   return NULL_TREE;
457 }
458
459 /* Handle a "common" attribute; arguments as in
460    struct attribute_spec.handler.  */
461
462 static tree
463 handle_common_attribute (node, name, args, flags, no_add_attrs)
464      tree *node;
465      tree name;
466      tree args ATTRIBUTE_UNUSED;
467      int flags ATTRIBUTE_UNUSED;
468      bool *no_add_attrs;
469 {
470   if (TREE_CODE (*node) == VAR_DECL)
471     DECL_COMMON (*node) = 1;
472   else
473     {
474       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
475       *no_add_attrs = true;
476     }
477
478   return NULL_TREE;
479 }
480
481 /* Handle a "noreturn" attribute; arguments as in
482    struct attribute_spec.handler.  */
483
484 static tree
485 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
486      tree *node;
487      tree name;
488      tree args ATTRIBUTE_UNUSED;
489      int flags ATTRIBUTE_UNUSED;
490      bool *no_add_attrs;
491 {
492   tree type = TREE_TYPE (*node);
493
494   /* See FIXME comment in c_common_attribute_table.  */
495   if (TREE_CODE (*node) == FUNCTION_DECL)
496     TREE_THIS_VOLATILE (*node) = 1;
497   else if (TREE_CODE (type) == POINTER_TYPE
498            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
499     TREE_TYPE (*node)
500       = build_pointer_type
501         (build_type_variant (TREE_TYPE (type),
502                              TREE_READONLY (TREE_TYPE (type)), 1));
503   else
504     {
505       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
506       *no_add_attrs = true;
507     }
508
509   return NULL_TREE;
510 }
511
512 /* Handle a "unused" attribute; arguments as in
513    struct attribute_spec.handler.  */
514
515 static tree
516 handle_unused_attribute (node, name, args, flags, no_add_attrs)
517      tree *node;
518      tree name;
519      tree args ATTRIBUTE_UNUSED;
520      int flags;
521      bool *no_add_attrs;
522 {
523   if (DECL_P (*node))
524     {
525       tree decl = *node;
526
527       if (TREE_CODE (decl) == PARM_DECL
528           || TREE_CODE (decl) == VAR_DECL
529           || TREE_CODE (decl) == FUNCTION_DECL
530           || TREE_CODE (decl) == LABEL_DECL
531           || TREE_CODE (decl) == TYPE_DECL)
532         TREE_USED (decl) = 1;
533       else
534         {
535           warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
536           *no_add_attrs = true;
537         }
538     }
539   else
540     {
541       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
542         *node = build_type_copy (*node);
543       TREE_USED (*node) = 1;
544     }
545
546   return NULL_TREE;
547 }
548
549 /* Handle a "const" attribute; arguments as in
550    struct attribute_spec.handler.  */
551
552 static tree
553 handle_const_attribute (node, name, args, flags, no_add_attrs)
554      tree *node;
555      tree name;
556      tree args ATTRIBUTE_UNUSED;
557      int flags ATTRIBUTE_UNUSED;
558      bool *no_add_attrs;
559 {
560   tree type = TREE_TYPE (*node);
561
562   /* See FIXME comment on noreturn in c_common_attribute_table.  */
563   if (TREE_CODE (*node) == FUNCTION_DECL)
564     TREE_READONLY (*node) = 1;
565   else if (TREE_CODE (type) == POINTER_TYPE
566            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
567     TREE_TYPE (*node)
568       = build_pointer_type
569         (build_type_variant (TREE_TYPE (type), 1,
570                              TREE_THIS_VOLATILE (TREE_TYPE (type))));
571   else
572     {
573       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
574       *no_add_attrs = true;
575     }
576
577   return NULL_TREE;
578 }
579
580 /* Handle a "transparent_union" attribute; arguments as in
581    struct attribute_spec.handler.  */
582
583 static tree
584 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
585      tree *node;
586      tree name;
587      tree args ATTRIBUTE_UNUSED;
588      int flags;
589      bool *no_add_attrs;
590 {
591   tree decl = NULL_TREE;
592   tree *type = NULL;
593   int is_type = 0;
594
595   if (DECL_P (*node))
596     {
597       decl = *node;
598       type = &TREE_TYPE (decl);
599       is_type = TREE_CODE (*node) == TYPE_DECL;
600     }
601   else if (TYPE_P (*node))
602     type = node, is_type = 1;
603
604   if (is_type
605       && TREE_CODE (*type) == UNION_TYPE
606       && (decl == 0
607           || (TYPE_FIELDS (*type) != 0
608               && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
609     {
610       if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
611         *type = build_type_copy (*type);
612       TYPE_TRANSPARENT_UNION (*type) = 1;
613     }
614   else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
615            && TREE_CODE (*type) == UNION_TYPE
616            && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
617     DECL_TRANSPARENT_UNION (decl) = 1;
618   else
619     {
620       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
621       *no_add_attrs = true;
622     }
623
624   return NULL_TREE;
625 }
626
627 /* Handle a "constructor" attribute; arguments as in
628    struct attribute_spec.handler.  */
629
630 static tree
631 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
632      tree *node;
633      tree name;
634      tree args ATTRIBUTE_UNUSED;
635      int flags ATTRIBUTE_UNUSED;
636      bool *no_add_attrs;
637 {
638   tree decl = *node;
639   tree type = TREE_TYPE (decl);
640
641   if (TREE_CODE (decl) == FUNCTION_DECL
642       && TREE_CODE (type) == FUNCTION_TYPE
643       && decl_function_context (decl) == 0)
644     {
645       DECL_STATIC_CONSTRUCTOR (decl) = 1;
646       TREE_USED (decl) = 1;
647     }
648   else
649     {
650       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
651       *no_add_attrs = true;
652     }
653
654   return NULL_TREE;
655 }
656
657 /* Handle a "destructor" attribute; arguments as in
658    struct attribute_spec.handler.  */
659
660 static tree
661 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
662      tree *node;
663      tree name;
664      tree args ATTRIBUTE_UNUSED;
665      int flags ATTRIBUTE_UNUSED;
666      bool *no_add_attrs;
667 {
668   tree decl = *node;
669   tree type = TREE_TYPE (decl);
670
671   if (TREE_CODE (decl) == FUNCTION_DECL
672       && TREE_CODE (type) == FUNCTION_TYPE
673       && decl_function_context (decl) == 0)
674     {
675       DECL_STATIC_DESTRUCTOR (decl) = 1;
676       TREE_USED (decl) = 1;
677     }
678   else
679     {
680       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681       *no_add_attrs = true;
682     }
683
684   return NULL_TREE;
685 }
686
687 /* Handle a "mode" attribute; arguments as in
688    struct attribute_spec.handler.  */
689
690 static tree
691 handle_mode_attribute (node, name, args, flags, no_add_attrs)
692      tree *node;
693      tree name;
694      tree args;
695      int flags ATTRIBUTE_UNUSED;
696      bool *no_add_attrs;
697 {
698   tree decl = *node;
699   tree type = TREE_TYPE (decl);
700
701   *no_add_attrs = true;
702
703   if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
704     warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
705   else
706     {
707       int j;
708       const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
709       int len = strlen (p);
710       enum machine_mode mode = VOIDmode;
711       tree typefm;
712
713       if (len > 4 && p[0] == '_' && p[1] == '_'
714           && p[len - 1] == '_' && p[len - 2] == '_')
715         {
716           char *newp = (char *) alloca (len - 1);
717
718           strcpy (newp, &p[2]);
719           newp[len - 4] = '\0';
720           p = newp;
721         }
722
723       /* Give this decl a type with the specified mode.
724          First check for the special modes.  */
725       if (! strcmp (p, "byte"))
726         mode = byte_mode;
727       else if (!strcmp (p, "word"))
728         mode = word_mode;
729       else if (! strcmp (p, "pointer"))
730         mode = ptr_mode;
731       else
732         for (j = 0; j < NUM_MACHINE_MODES; j++)
733           if (!strcmp (p, GET_MODE_NAME (j)))
734             mode = (enum machine_mode) j;
735
736       if (mode == VOIDmode)
737         error ("unknown machine mode `%s'", p);
738       else if (0 == (typefm = type_for_mode (mode,
739                                              TREE_UNSIGNED (type))))
740         error ("no data type for mode `%s'", p);
741       else
742         {
743           TREE_TYPE (decl) = type = typefm;
744           DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
745           if (TREE_CODE (decl) != FIELD_DECL)
746             layout_decl (decl, 0);
747         }
748     }
749
750   return NULL_TREE;
751 }
752
753 /* Handle a "section" attribute; arguments as in
754    struct attribute_spec.handler.  */
755
756 static tree
757 handle_section_attribute (node, name, args, flags, no_add_attrs)
758      tree *node;
759      tree name ATTRIBUTE_UNUSED;
760      tree args;
761      int flags ATTRIBUTE_UNUSED;
762      bool *no_add_attrs;
763 {
764   tree decl = *node;
765
766   if (targetm.have_named_sections)
767     {
768       if ((TREE_CODE (decl) == FUNCTION_DECL
769            || TREE_CODE (decl) == VAR_DECL)
770           && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
771         {
772           if (TREE_CODE (decl) == VAR_DECL
773               && current_function_decl != NULL_TREE
774               && ! TREE_STATIC (decl))
775             {
776               error_with_decl (decl,
777                                "section attribute cannot be specified for local variables");
778               *no_add_attrs = true;
779             }
780
781           /* The decl may have already been given a section attribute
782              from a previous declaration.  Ensure they match.  */
783           else if (DECL_SECTION_NAME (decl) != NULL_TREE
784                    && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
785                               TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
786             {
787               error_with_decl (*node,
788                                "section of `%s' conflicts with previous declaration");
789               *no_add_attrs = true;
790             }
791           else
792             DECL_SECTION_NAME (decl) = TREE_VALUE (args);
793         }
794       else
795         {
796           error_with_decl (*node,
797                            "section attribute not allowed for `%s'");
798           *no_add_attrs = true;
799         }
800     }
801   else
802     {
803       error_with_decl (*node,
804                        "section attributes are not supported for this target");
805       *no_add_attrs = true;
806     }
807
808   return NULL_TREE;
809 }
810
811 /* Handle a "aligned" attribute; arguments as in
812    struct attribute_spec.handler.  */
813
814 static tree
815 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
816      tree *node;
817      tree name ATTRIBUTE_UNUSED;
818      tree args;
819      int flags;
820      bool *no_add_attrs;
821 {
822   tree decl = NULL_TREE;
823   tree *type = NULL;
824   int is_type = 0;
825   tree align_expr = (args ? TREE_VALUE (args)
826                      : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
827   int i;
828
829   if (DECL_P (*node))
830     {
831       decl = *node;
832       type = &TREE_TYPE (decl);
833       is_type = TREE_CODE (*node) == TYPE_DECL;
834     }
835   else if (TYPE_P (*node))
836     type = node, is_type = 1;
837
838   /* Strip any NOPs of any kind.  */
839   while (TREE_CODE (align_expr) == NOP_EXPR
840          || TREE_CODE (align_expr) == CONVERT_EXPR
841          || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
842     align_expr = TREE_OPERAND (align_expr, 0);
843
844   if (TREE_CODE (align_expr) != INTEGER_CST)
845     {
846       error ("requested alignment is not a constant");
847       *no_add_attrs = true;
848     }
849   else if ((i = tree_log2 (align_expr)) == -1)
850     {
851       error ("requested alignment is not a power of 2");
852       *no_add_attrs = true;
853     }
854   else if (i > HOST_BITS_PER_INT - 2)
855     {
856       error ("requested alignment is too large");
857       *no_add_attrs = true;
858     }
859   else if (is_type)
860     {
861       /* If we have a TYPE_DECL, then copy the type, so that we
862          don't accidentally modify a builtin type.  See pushdecl.  */
863       if (decl && TREE_TYPE (decl) != error_mark_node
864           && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
865         {
866           tree tt = TREE_TYPE (decl);
867           *type = build_type_copy (*type);
868           DECL_ORIGINAL_TYPE (decl) = tt;
869           TYPE_NAME (*type) = decl;
870           TREE_USED (*type) = TREE_USED (decl);
871           TREE_TYPE (decl) = *type;
872         }
873       else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
874         *type = build_type_copy (*type);
875
876       TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
877       TYPE_USER_ALIGN (*type) = 1;
878     }
879   else if (TREE_CODE (decl) != VAR_DECL
880            && TREE_CODE (decl) != FIELD_DECL)
881     {
882       error_with_decl (decl,
883                        "alignment may not be specified for `%s'");
884       *no_add_attrs = true;
885     }
886   else
887     {
888       DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
889       DECL_USER_ALIGN (decl) = 1;
890     }
891
892   return NULL_TREE;
893 }
894
895 /* Handle a "weak" attribute; arguments as in
896    struct attribute_spec.handler.  */
897
898 static tree
899 handle_weak_attribute (node, name, args, flags, no_add_attrs)
900      tree *node;
901      tree name ATTRIBUTE_UNUSED;
902      tree args ATTRIBUTE_UNUSED;
903      int flags ATTRIBUTE_UNUSED;
904      bool *no_add_attrs ATTRIBUTE_UNUSED;
905 {
906   declare_weak (*node);
907
908   return NULL_TREE;
909 }
910
911 /* Handle an "alias" attribute; arguments as in
912    struct attribute_spec.handler.  */
913
914 static tree
915 handle_alias_attribute (node, name, args, flags, no_add_attrs)
916      tree *node;
917      tree name;
918      tree args;
919      int flags ATTRIBUTE_UNUSED;
920      bool *no_add_attrs;
921 {
922   tree decl = *node;
923
924   if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
925       || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
926     {
927       error_with_decl (decl,
928                        "`%s' defined both normally and as an alias");
929       *no_add_attrs = true;
930     }
931   else if (decl_function_context (decl) == 0)
932     {
933       tree id;
934
935       id = TREE_VALUE (args);
936       if (TREE_CODE (id) != STRING_CST)
937         {
938           error ("alias arg not a string");
939           *no_add_attrs = true;
940           return NULL_TREE;
941         }
942       id = get_identifier (TREE_STRING_POINTER (id));
943       /* This counts as a use of the object pointed to.  */
944       TREE_USED (id) = 1;
945
946       if (TREE_CODE (decl) == FUNCTION_DECL)
947         DECL_INITIAL (decl) = error_mark_node;
948       else
949         DECL_EXTERNAL (decl) = 0;
950       assemble_alias (decl, id);
951     }
952   else
953     {
954       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
955       *no_add_attrs = true;
956     }
957
958   return NULL_TREE;
959 }
960
961 /* Handle a "no_instrument_function" attribute; arguments as in
962    struct attribute_spec.handler.  */
963
964 static tree
965 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
966      tree *node;
967      tree name;
968      tree args ATTRIBUTE_UNUSED;
969      int flags ATTRIBUTE_UNUSED;
970      bool *no_add_attrs;
971 {
972   tree decl = *node;
973
974   if (TREE_CODE (decl) != FUNCTION_DECL)
975     {
976       error_with_decl (decl,
977                        "`%s' attribute applies only to functions",
978                        IDENTIFIER_POINTER (name));
979       *no_add_attrs = true;
980     }
981   else if (DECL_INITIAL (decl))
982     {
983       error_with_decl (decl,
984                        "can't set `%s' attribute after definition",
985                        IDENTIFIER_POINTER (name));
986       *no_add_attrs = true;
987     }
988   else
989     DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
990
991   return NULL_TREE;
992 }
993
994 /* Handle a "no_check_memory_usage" attribute; arguments as in
995    struct attribute_spec.handler.  */
996
997 static tree
998 handle_no_check_memory_usage_attribute (node, name, args, flags, no_add_attrs)
999      tree *node;
1000      tree name;
1001      tree args ATTRIBUTE_UNUSED;
1002      int flags ATTRIBUTE_UNUSED;
1003      bool *no_add_attrs;
1004 {
1005   tree decl = *node;
1006
1007   if (TREE_CODE (decl) != FUNCTION_DECL)
1008     {
1009       error_with_decl (decl,
1010                        "`%s' attribute applies only to functions",
1011                        IDENTIFIER_POINTER (name));
1012       *no_add_attrs = true;
1013     }
1014   else if (DECL_INITIAL (decl))
1015     {
1016       error_with_decl (decl,
1017                        "can't set `%s' attribute after definition",
1018                        IDENTIFIER_POINTER (name));
1019       *no_add_attrs = true;
1020     }
1021   else
1022     DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1023
1024   return NULL_TREE;
1025 }
1026
1027 /* Handle a "malloc" attribute; arguments as in
1028    struct attribute_spec.handler.  */
1029
1030 static tree
1031 handle_malloc_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;
1037 {
1038   if (TREE_CODE (*node) == FUNCTION_DECL)
1039     DECL_IS_MALLOC (*node) = 1;
1040   /* ??? TODO: Support types.  */
1041   else
1042     {
1043       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1044       *no_add_attrs = true;
1045     }
1046
1047   return NULL_TREE;
1048 }
1049
1050 /* Handle a "no_limit_stack" attribute; arguments as in
1051    struct attribute_spec.handler.  */
1052
1053 static tree
1054 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
1055      tree *node;
1056      tree name;
1057      tree args ATTRIBUTE_UNUSED;
1058      int flags ATTRIBUTE_UNUSED;
1059      bool *no_add_attrs;
1060 {
1061   tree decl = *node;
1062
1063   if (TREE_CODE (decl) != FUNCTION_DECL)
1064     {
1065       error_with_decl (decl,
1066                        "`%s' attribute applies only to functions",
1067                        IDENTIFIER_POINTER (name));
1068       *no_add_attrs = true;
1069     }
1070   else if (DECL_INITIAL (decl))
1071     {
1072       error_with_decl (decl,
1073                        "can't set `%s' attribute after definition",
1074                        IDENTIFIER_POINTER (name));
1075       *no_add_attrs = true;
1076     }
1077   else
1078     DECL_NO_LIMIT_STACK (decl) = 1;
1079
1080   return NULL_TREE;
1081 }
1082
1083 /* Handle a "pure" attribute; arguments as in
1084    struct attribute_spec.handler.  */
1085
1086 static tree
1087 handle_pure_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;
1093 {
1094   if (TREE_CODE (*node) == FUNCTION_DECL)
1095     DECL_IS_PURE (*node) = 1;
1096   /* ??? TODO: Support types.  */
1097   else
1098     {
1099       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1100       *no_add_attrs = true;
1101     }
1102
1103   return NULL_TREE;
1104 }
1105
1106 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1107    lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1108
1109    The head of the declspec list is stored in DECLSPECS.
1110    The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1111
1112    Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1113    the list elements.  We drop the containing TREE_LIST nodes and link the
1114    resulting attributes together the way decl_attributes expects them.  */
1115
1116 void
1117 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1118      tree specs_attrs;
1119      tree *declspecs, *prefix_attributes;
1120 {
1121   tree t, s, a, next, specs, attrs;
1122
1123   /* This can happen after an __extension__ in pedantic mode.  */
1124   if (specs_attrs != NULL_TREE 
1125       && TREE_CODE (specs_attrs) == INTEGER_CST)
1126     {
1127       *declspecs = NULL_TREE;
1128       *prefix_attributes = NULL_TREE;
1129       return;
1130     }
1131
1132   /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
1133   if (specs_attrs != NULL_TREE
1134       && TREE_CODE (specs_attrs) != TREE_LIST)
1135     {
1136       *declspecs = specs_attrs;
1137       *prefix_attributes = NULL_TREE;
1138       return;
1139     }
1140
1141   /* Remember to keep the lists in the same order, element-wise.  */
1142
1143   specs = s = NULL_TREE;
1144   attrs = a = NULL_TREE;
1145   for (t = specs_attrs; t; t = next)
1146     {
1147       next = TREE_CHAIN (t);
1148       /* Declspecs have a non-NULL TREE_VALUE.  */
1149       if (TREE_VALUE (t) != NULL_TREE)
1150         {
1151           if (specs == NULL_TREE)
1152             specs = s = t;
1153           else
1154             {
1155               TREE_CHAIN (s) = t;
1156               s = t;
1157             }
1158         }
1159       /* The TREE_PURPOSE may also be empty in the case of
1160          __attribute__(()).  */
1161       else if (TREE_PURPOSE (t) != NULL_TREE)
1162         {
1163           if (attrs == NULL_TREE)
1164             attrs = a = TREE_PURPOSE (t);
1165           else
1166             {
1167               TREE_CHAIN (a) = TREE_PURPOSE (t);
1168               a = TREE_PURPOSE (t);
1169             }
1170           /* More attrs can be linked here, move A to the end.  */
1171           while (TREE_CHAIN (a) != NULL_TREE)
1172             a = TREE_CHAIN (a);
1173         }
1174     }
1175
1176   /* Terminate the lists.  */
1177   if (s != NULL_TREE)
1178     TREE_CHAIN (s) = NULL_TREE;
1179   if (a != NULL_TREE)
1180     TREE_CHAIN (a) = NULL_TREE;
1181
1182   /* All done.  */
1183   *declspecs = specs;
1184   *prefix_attributes = attrs;
1185 }
1186
1187 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1188    This function is used by the parser when a rule will accept attributes
1189    in a particular position, but we don't want to support that just yet.
1190
1191    A warning is issued for every ignored attribute.  */
1192
1193 tree
1194 strip_attrs (specs_attrs)
1195      tree specs_attrs;
1196 {
1197   tree specs, attrs;
1198
1199   split_specs_attrs (specs_attrs, &specs, &attrs);
1200
1201   while (attrs)
1202     {
1203       warning ("`%s' attribute ignored",
1204                IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1205       attrs = TREE_CHAIN (attrs);
1206     }
1207
1208   return specs;
1209 }