OSDN Git Service

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