OSDN Git Service

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