OSDN Git Service

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