OSDN Git Service

* c-common.c (flag_noniso_default_format_attributes): Delete.
[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    2002, 2003 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 "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "output.h"
30 #include "rtl.h"
31 #include "ggc.h"
32 #include "expr.h"
33 #include "tm_p.h"
34 #include "cpplib.h"
35 #include "target.h"
36 #include "langhooks.h"
37
38 static void init_attributes (void);
39
40 /* Table of the tables of attributes (common, language, format, machine)
41    searched.  */
42 static const struct attribute_spec *attribute_tables[4];
43
44 static bool attributes_initialized = false;
45
46 /* Default empty table of attributes.  */
47 static const struct attribute_spec empty_attribute_table[] =
48 {
49   { NULL, 0, 0, false, false, false, NULL }
50 };
51
52 /* Initialize attribute tables, and make some sanity checks
53    if --enable-checking.  */
54
55 static void
56 init_attributes (void)
57 {
58   size_t i;
59
60   attribute_tables[0] = lang_hooks.common_attribute_table;
61   attribute_tables[1] = lang_hooks.attribute_table;
62   attribute_tables[2] = lang_hooks.format_attribute_table;
63   attribute_tables[3] = targetm.attribute_table;
64
65   /* Translate NULL pointers to pointers to the empty table.  */
66   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
67     if (attribute_tables[i] == NULL)
68       attribute_tables[i] = empty_attribute_table;
69
70 #ifdef ENABLE_CHECKING
71   /* Make some sanity checks on the attribute tables.  */
72   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
73     {
74       int j;
75
76       for (j = 0; attribute_tables[i][j].name != NULL; j++)
77         {
78           /* The name must not begin and end with __.  */
79           const char *name = attribute_tables[i][j].name;
80           int len = strlen (name);
81           if (name[0] == '_' && name[1] == '_'
82               && name[len - 1] == '_' && name[len - 2] == '_')
83             abort ();
84           /* The minimum and maximum lengths must be consistent.  */
85           if (attribute_tables[i][j].min_length < 0)
86             abort ();
87           if (attribute_tables[i][j].max_length != -1
88               && (attribute_tables[i][j].max_length
89                   < attribute_tables[i][j].min_length))
90             abort ();
91           /* An attribute cannot require both a DECL and a TYPE.  */
92           if (attribute_tables[i][j].decl_required
93               && attribute_tables[i][j].type_required)
94             abort ();
95           /* If an attribute requires a function type, in particular
96              it requires a type.  */
97           if (attribute_tables[i][j].function_type_required
98               && !attribute_tables[i][j].type_required)
99             abort ();
100         }
101     }
102
103   /* Check that each name occurs just once in each table.  */
104   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
105     {
106       int j, k;
107       for (j = 0; attribute_tables[i][j].name != NULL; j++)
108         for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
109           if (!strcmp (attribute_tables[i][j].name,
110                        attribute_tables[i][k].name))
111             abort ();
112     }
113   /* Check that no name occurs in more than one table.  */
114   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
115     {
116       size_t j, k, l;
117
118       for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
119         for (k = 0; attribute_tables[i][k].name != NULL; k++)
120           for (l = 0; attribute_tables[j][l].name != NULL; l++)
121             if (!strcmp (attribute_tables[i][k].name,
122                          attribute_tables[j][l].name))
123               abort ();
124     }
125 #endif
126
127   attributes_initialized = true;
128 }
129 \f
130 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
131    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
132    it should be modified in place; if a TYPE, a copy should be created
133    unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS.  FLAGS gives further
134    information, in the form of a bitwise OR of flags in enum attribute_flags
135    from tree.h.  Depending on these flags, some attributes may be
136    returned to be applied at a later stage (for example, to apply
137    a decl attribute to the declaration rather than to its type).  */
138
139 tree
140 decl_attributes (tree *node, tree attributes, int flags)
141 {
142   tree a;
143   tree returned_attrs = NULL_TREE;
144
145   if (!attributes_initialized)
146     init_attributes ();
147
148   (*targetm.insert_attributes) (*node, &attributes);
149
150   for (a = attributes; a; a = TREE_CHAIN (a))
151     {
152       tree name = TREE_PURPOSE (a);
153       tree args = TREE_VALUE (a);
154       tree *anode = node;
155       const struct attribute_spec *spec = NULL;
156       bool no_add_attrs = 0;
157       size_t i;
158
159       for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
160         {
161           int j;
162
163           for (j = 0; attribute_tables[i][j].name != NULL; j++)
164             {
165               if (is_attribute_p (attribute_tables[i][j].name, name))
166                 {
167                   spec = &attribute_tables[i][j];
168                   break;
169                 }
170             }
171           if (spec != NULL)
172             break;
173         }
174
175       if (spec == NULL)
176         {
177           warning ("`%s' attribute directive ignored",
178                    IDENTIFIER_POINTER (name));
179           continue;
180         }
181       else if (list_length (args) < spec->min_length
182                || (spec->max_length >= 0
183                    && list_length (args) > spec->max_length))
184         {
185           error ("wrong number of arguments specified for `%s' attribute",
186                  IDENTIFIER_POINTER (name));
187           continue;
188         }
189
190       if (spec->decl_required && !DECL_P (*anode))
191         {
192           if (flags & ((int) ATTR_FLAG_DECL_NEXT
193                        | (int) ATTR_FLAG_FUNCTION_NEXT
194                        | (int) ATTR_FLAG_ARRAY_NEXT))
195             {
196               /* Pass on this attribute to be tried again.  */
197               returned_attrs = tree_cons (name, args, returned_attrs);
198               continue;
199             }
200           else
201             {
202               warning ("`%s' attribute does not apply to types",
203                        IDENTIFIER_POINTER (name));
204               continue;
205             }
206         }
207
208       /* If we require a type, but were passed a decl, set up to make a
209          new type and update the one in the decl.  ATTR_FLAG_TYPE_IN_PLACE
210          would have applied if we'd been passed a type, but we cannot modify
211          the decl's type in place here.  */
212       if (spec->type_required && DECL_P (*anode))
213         {
214           anode = &TREE_TYPE (*anode);
215           flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
216         }
217
218       if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
219           && TREE_CODE (*anode) != METHOD_TYPE)
220         {
221           if (TREE_CODE (*anode) == POINTER_TYPE
222               && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
223                   || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
224             {
225               if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
226                 *anode = build_type_copy (*anode);
227               anode = &TREE_TYPE (*anode);
228             }
229           else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
230             {
231               /* Pass on this attribute to be tried again.  */
232               returned_attrs = tree_cons (name, args, returned_attrs);
233               continue;
234             }
235
236           if (TREE_CODE (*anode) != FUNCTION_TYPE
237               && TREE_CODE (*anode) != METHOD_TYPE)
238             {
239               warning ("`%s' attribute only applies to function types",
240                        IDENTIFIER_POINTER (name));
241               continue;
242             }
243         }
244
245       if (spec->handler != NULL)
246         returned_attrs = chainon ((*spec->handler) (anode, name, args,
247                                                     flags, &no_add_attrs),
248                                   returned_attrs);
249
250       /* Layout the decl in case anything changed.  */
251       if (spec->type_required && DECL_P (*node)
252           && (TREE_CODE (*node) == VAR_DECL
253               || TREE_CODE (*node) == PARM_DECL
254               || TREE_CODE (*node) == RESULT_DECL))
255         {
256           /* Force a recalculation of mode and size.  */
257           DECL_MODE (*node) = VOIDmode;
258           DECL_SIZE (*node) = 0;
259
260           layout_decl (*node, 0);
261         }
262
263       if (!no_add_attrs)
264         {
265           tree old_attrs;
266           tree a;
267
268           if (DECL_P (*anode))
269             old_attrs = DECL_ATTRIBUTES (*anode);
270           else
271             old_attrs = TYPE_ATTRIBUTES (*anode);
272
273           for (a = lookup_attribute (spec->name, old_attrs);
274                a != NULL_TREE;
275                a = lookup_attribute (spec->name, TREE_CHAIN (a)))
276             {
277               if (simple_cst_equal (TREE_VALUE (a), args) == 1)
278                 break;
279             }
280
281           if (a == NULL_TREE)
282             {
283               /* This attribute isn't already in the list.  */
284               if (DECL_P (*anode))
285                 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
286               else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
287                 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
288               else
289                 *anode = build_type_attribute_variant (*anode,
290                                                        tree_cons (name, args,
291                                                                   old_attrs));
292             }
293         }
294     }
295
296   return returned_attrs;
297 }
298
299 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
300    lists.  SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
301
302    The head of the declspec list is stored in DECLSPECS.
303    The head of the attribute list is stored in PREFIX_ATTRIBUTES.
304
305    Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
306    the list elements.  We drop the containing TREE_LIST nodes and link the
307    resulting attributes together the way decl_attributes expects them.  */
308
309 void
310 split_specs_attrs (tree specs_attrs, tree *declspecs, tree *prefix_attributes)
311 {
312   tree t, s, a, next, specs, attrs;
313
314   /* This can happen after an __extension__ in pedantic mode.  */
315   if (specs_attrs != NULL_TREE
316       && TREE_CODE (specs_attrs) == INTEGER_CST)
317     {
318       *declspecs = NULL_TREE;
319       *prefix_attributes = NULL_TREE;
320       return;
321     }
322
323   /* This can happen in c++ (eg: decl: typespec initdecls ';').  */
324   if (specs_attrs != NULL_TREE
325       && TREE_CODE (specs_attrs) != TREE_LIST)
326     {
327       *declspecs = specs_attrs;
328       *prefix_attributes = NULL_TREE;
329       return;
330     }
331
332   /* Remember to keep the lists in the same order, element-wise.  */
333
334   specs = s = NULL_TREE;
335   attrs = a = NULL_TREE;
336   for (t = specs_attrs; t; t = next)
337     {
338       next = TREE_CHAIN (t);
339       /* Declspecs have a non-NULL TREE_VALUE.  */
340       if (TREE_VALUE (t) != NULL_TREE)
341         {
342           if (specs == NULL_TREE)
343             specs = s = t;
344           else
345             {
346               TREE_CHAIN (s) = t;
347               s = t;
348             }
349         }
350       /* The TREE_PURPOSE may also be empty in the case of
351          __attribute__(()).  */
352       else if (TREE_PURPOSE (t) != NULL_TREE)
353         {
354           if (attrs == NULL_TREE)
355             attrs = a = TREE_PURPOSE (t);
356           else
357             {
358               TREE_CHAIN (a) = TREE_PURPOSE (t);
359               a = TREE_PURPOSE (t);
360             }
361           /* More attrs can be linked here, move A to the end.  */
362           while (TREE_CHAIN (a) != NULL_TREE)
363             a = TREE_CHAIN (a);
364         }
365     }
366
367   /* Terminate the lists.  */
368   if (s != NULL_TREE)
369     TREE_CHAIN (s) = NULL_TREE;
370   if (a != NULL_TREE)
371     TREE_CHAIN (a) = NULL_TREE;
372
373   /* All done.  */
374   *declspecs = specs;
375   *prefix_attributes = attrs;
376 }
377
378 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
379    This function is used by the parser when a rule will accept attributes
380    in a particular position, but we don't want to support that just yet.
381
382    A warning is issued for every ignored attribute.  */
383
384 tree
385 strip_attrs (tree specs_attrs)
386 {
387   tree specs, attrs;
388
389   split_specs_attrs (specs_attrs, &specs, &attrs);
390
391   while (attrs)
392     {
393       warning ("`%s' attribute ignored",
394                IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
395       attrs = TREE_CHAIN (attrs);
396     }
397
398   return specs;
399 }