gcc50/csu: Skip depends step to avoid possible race
[dragonfly.git] / contrib / gcc-4.4 / 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, 2004, 2005, 2007, 2008, 2010 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "toplev.h"
28 #include "output.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "tm_p.h"
32 #include "cpplib.h"
33 #include "target.h"
34 #include "langhooks.h"
35 #include "hashtab.h"
36 #include "c-common.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 /* Hashtable mapping names (represented as substrings) to attribute specs. */
45 static htab_t attribute_hash;
46
47 /* Substring representation.  */
48
49 struct substring
50 {
51   const char *str;
52   int length;
53 };
54
55 static bool attributes_initialized = false;
56
57 /* Default empty table of attributes.  */
58
59 static const struct attribute_spec empty_attribute_table[] =
60 {
61   { NULL, 0, 0, false, false, false, NULL }
62 };
63
64 /* Return base name of the attribute.  Ie '__attr__' is turned into 'attr'.
65    To avoid need for copying, we simply return length of the string.  */
66
67 static void
68 extract_attribute_substring (struct substring *str)
69 {
70   if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
71       && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
72     {
73       str->length -= 4;
74       str->str += 2;
75     }
76 }
77
78 /* Simple hash function to avoid need to scan whole string.  */
79
80 static inline hashval_t
81 substring_hash (const char *str, int l)
82 {
83   return str[0] + str[l - 1] * 256 + l * 65536;
84 }
85
86 /* Used for attribute_hash.  */
87
88 static hashval_t
89 hash_attr (const void *p)
90 {
91   const struct attribute_spec *const spec = (const struct attribute_spec *) p;
92   const int l = strlen (spec->name);
93
94   return substring_hash (spec->name, l);
95 }
96
97 /* Used for attribute_hash.  */
98
99 static int
100 eq_attr (const void *p, const void *q)
101 {
102   const struct attribute_spec *const spec = (const struct attribute_spec *) p;
103   const struct substring *const str = (const struct substring *) q;
104
105   return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
106 }
107
108 /* Initialize attribute tables, and make some sanity checks
109    if --enable-checking.  */
110
111 static void
112 init_attributes (void)
113 {
114   size_t i;
115   int k;
116
117   attribute_tables[0] = lang_hooks.common_attribute_table;
118   attribute_tables[1] = lang_hooks.attribute_table;
119   attribute_tables[2] = lang_hooks.format_attribute_table;
120   attribute_tables[3] = targetm.attribute_table;
121
122   /* Translate NULL pointers to pointers to the empty table.  */
123   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
124     if (attribute_tables[i] == NULL)
125       attribute_tables[i] = empty_attribute_table;
126
127 #ifdef ENABLE_CHECKING
128   /* Make some sanity checks on the attribute tables.  */
129   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
130     {
131       int j;
132
133       for (j = 0; attribute_tables[i][j].name != NULL; j++)
134         {
135           /* The name must not begin and end with __.  */
136           const char *name = attribute_tables[i][j].name;
137           int len = strlen (name);
138
139           gcc_assert (!(name[0] == '_' && name[1] == '_'
140                         && name[len - 1] == '_' && name[len - 2] == '_'));
141
142           /* The minimum and maximum lengths must be consistent.  */
143           gcc_assert (attribute_tables[i][j].min_length >= 0);
144
145           gcc_assert (attribute_tables[i][j].max_length == -1
146                       || (attribute_tables[i][j].max_length
147                           >= attribute_tables[i][j].min_length));
148
149           /* An attribute cannot require both a DECL and a TYPE.  */
150           gcc_assert (!attribute_tables[i][j].decl_required
151                       || !attribute_tables[i][j].type_required);
152
153           /* If an attribute requires a function type, in particular
154              it requires a type.  */
155           gcc_assert (!attribute_tables[i][j].function_type_required
156                       || attribute_tables[i][j].type_required);
157         }
158     }
159
160   /* Check that each name occurs just once in each table.  */
161   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
162     {
163       int j, k;
164       for (j = 0; attribute_tables[i][j].name != NULL; j++)
165         for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
166           gcc_assert (strcmp (attribute_tables[i][j].name,
167                               attribute_tables[i][k].name));
168     }
169   /* Check that no name occurs in more than one table.  */
170   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
171     {
172       size_t j, k, l;
173
174       for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
175         for (k = 0; attribute_tables[i][k].name != NULL; k++)
176           for (l = 0; attribute_tables[j][l].name != NULL; l++)
177             gcc_assert (strcmp (attribute_tables[i][k].name,
178                                 attribute_tables[j][l].name));
179     }
180 #endif
181
182   attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
183   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
184     for (k = 0; attribute_tables[i][k].name != NULL; k++)
185       {
186         struct substring str;
187         const void **slot;
188
189         str.str = attribute_tables[i][k].name;
190         str.length = strlen (attribute_tables[i][k].name);
191         slot = (const void **)htab_find_slot_with_hash (attribute_hash, &str,
192                                          substring_hash (str.str, str.length),
193                                          INSERT);
194         gcc_assert (!*slot);
195         *slot = &attribute_tables[i][k];
196       }
197   attributes_initialized = true;
198 }
199
200 /* Return the spec for the attribute named NAME.  */
201
202 const struct attribute_spec *
203 lookup_attribute_spec (tree name)
204 {
205   struct substring attr;
206
207   attr.str = IDENTIFIER_POINTER (name);
208   attr.length = IDENTIFIER_LENGTH (name);
209   extract_attribute_substring (&attr);
210   return (const struct attribute_spec *)
211     htab_find_with_hash (attribute_hash, &attr,
212                          substring_hash (attr.str, attr.length));
213 }
214 \f
215 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
216    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
217    it should be modified in place; if a TYPE, a copy should be created
218    unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS.  FLAGS gives further
219    information, in the form of a bitwise OR of flags in enum attribute_flags
220    from tree.h.  Depending on these flags, some attributes may be
221    returned to be applied at a later stage (for example, to apply
222    a decl attribute to the declaration rather than to its type).  */
223
224 tree
225 decl_attributes (tree *node, tree attributes, int flags)
226 {
227   tree a;
228   tree returned_attrs = NULL_TREE;
229
230   if (TREE_TYPE (*node) == error_mark_node)
231     return NULL_TREE;
232
233   if (!attributes_initialized)
234     init_attributes ();
235
236   /* If this is a function and the user used #pragma GCC optimize, add the
237      options to the attribute((optimize(...))) list.  */
238   if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
239     {
240       tree cur_attr = lookup_attribute ("optimize", attributes);
241       tree opts = copy_list (current_optimize_pragma);
242
243       if (! cur_attr)
244         attributes
245           = tree_cons (get_identifier ("optimize"), opts, attributes);
246       else
247         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
248     }
249
250   if (TREE_CODE (*node) == FUNCTION_DECL
251       && optimization_current_node != optimization_default_node
252       && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
253     DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
254
255   /* If this is a function and the user used #pragma GCC target, add the
256      options to the attribute((target(...))) list.  */
257   if (TREE_CODE (*node) == FUNCTION_DECL
258       && current_target_pragma
259       && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
260                                                   current_target_pragma, 0))
261     {
262       tree cur_attr = lookup_attribute ("target", attributes);
263       tree opts = copy_list (current_target_pragma);
264
265       if (! cur_attr)
266         attributes = tree_cons (get_identifier ("target"), opts, attributes);
267       else
268         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
269     }
270
271   targetm.insert_attributes (*node, &attributes);
272
273   for (a = attributes; a; a = TREE_CHAIN (a))
274     {
275       tree name = TREE_PURPOSE (a);
276       tree args = TREE_VALUE (a);
277       tree *anode = node;
278       const struct attribute_spec *spec = lookup_attribute_spec (name);
279       bool no_add_attrs = 0;
280       int fn_ptr_quals = 0;
281       tree fn_ptr_tmp = NULL_TREE;
282
283       if (spec == NULL)
284         {
285           warning (OPT_Wattributes, "%qs attribute directive ignored",
286                    IDENTIFIER_POINTER (name));
287           continue;
288         }
289       else if (list_length (args) < spec->min_length
290                || (spec->max_length >= 0
291                    && list_length (args) > spec->max_length))
292         {
293           error ("wrong number of arguments specified for %qs attribute",
294                  IDENTIFIER_POINTER (name));
295           continue;
296         }
297       gcc_assert (is_attribute_p (spec->name, name));
298
299       if (spec->decl_required && !DECL_P (*anode))
300         {
301           if (flags & ((int) ATTR_FLAG_DECL_NEXT
302                        | (int) ATTR_FLAG_FUNCTION_NEXT
303                        | (int) ATTR_FLAG_ARRAY_NEXT))
304             {
305               /* Pass on this attribute to be tried again.  */
306               returned_attrs = tree_cons (name, args, returned_attrs);
307               continue;
308             }
309           else
310             {
311               warning (OPT_Wattributes, "%qs attribute does not apply to types",
312                        IDENTIFIER_POINTER (name));
313               continue;
314             }
315         }
316
317       /* If we require a type, but were passed a decl, set up to make a
318          new type and update the one in the decl.  ATTR_FLAG_TYPE_IN_PLACE
319          would have applied if we'd been passed a type, but we cannot modify
320          the decl's type in place here.  */
321       if (spec->type_required && DECL_P (*anode))
322         {
323           anode = &TREE_TYPE (*anode);
324           /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl.  */
325           if (!(TREE_CODE (*anode) == TYPE_DECL
326                 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
327                                         (TREE_TYPE (*anode)))))
328             flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
329         }
330
331       if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
332           && TREE_CODE (*anode) != METHOD_TYPE)
333         {
334           if (TREE_CODE (*anode) == POINTER_TYPE
335               && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
336                   || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
337             {
338               /* OK, this is a bit convoluted.  We can't just make a copy
339                  of the pointer type and modify its TREE_TYPE, because if
340                  we change the attributes of the target type the pointer
341                  type needs to have a different TYPE_MAIN_VARIANT.  So we
342                  pull out the target type now, frob it as appropriate, and
343                  rebuild the pointer type later.
344
345                  This would all be simpler if attributes were part of the
346                  declarator, grumble grumble.  */
347               fn_ptr_tmp = TREE_TYPE (*anode);
348               fn_ptr_quals = TYPE_QUALS (*anode);
349               anode = &fn_ptr_tmp;
350               flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
351             }
352           else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
353             {
354               /* Pass on this attribute to be tried again.  */
355               returned_attrs = tree_cons (name, args, returned_attrs);
356               continue;
357             }
358
359           if (TREE_CODE (*anode) != FUNCTION_TYPE
360               && TREE_CODE (*anode) != METHOD_TYPE)
361             {
362               warning (OPT_Wattributes,
363                        "%qs attribute only applies to function types",
364                        IDENTIFIER_POINTER (name));
365               continue;
366             }
367         }
368
369       if (TYPE_P (*anode)
370           && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
371           && TYPE_SIZE (*anode) != NULL_TREE)
372         {
373           warning (OPT_Wattributes, "type attributes ignored after type is already defined");
374           continue;
375         }
376
377       if (spec->handler != NULL)
378         returned_attrs = chainon ((*spec->handler) (anode, name, args,
379                                                     flags, &no_add_attrs),
380                                   returned_attrs);
381
382       /* Layout the decl in case anything changed.  */
383       if (spec->type_required && DECL_P (*node)
384           && (TREE_CODE (*node) == VAR_DECL
385               || TREE_CODE (*node) == PARM_DECL
386               || TREE_CODE (*node) == RESULT_DECL))
387         relayout_decl (*node);
388
389       if (!no_add_attrs)
390         {
391           tree old_attrs;
392           tree a;
393
394           if (DECL_P (*anode))
395             old_attrs = DECL_ATTRIBUTES (*anode);
396           else
397             old_attrs = TYPE_ATTRIBUTES (*anode);
398
399           for (a = lookup_attribute (spec->name, old_attrs);
400                a != NULL_TREE;
401                a = lookup_attribute (spec->name, TREE_CHAIN (a)))
402             {
403               if (simple_cst_equal (TREE_VALUE (a), args) == 1)
404                 break;
405             }
406
407           if (a == NULL_TREE)
408             {
409               /* This attribute isn't already in the list.  */
410               if (DECL_P (*anode))
411                 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
412               else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
413                 {
414                   TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
415                   /* If this is the main variant, also push the attributes
416                      out to the other variants.  */
417                   if (*anode == TYPE_MAIN_VARIANT (*anode))
418                     {
419                       tree variant;
420                       for (variant = *anode; variant;
421                            variant = TYPE_NEXT_VARIANT (variant))
422                         {
423                           if (TYPE_ATTRIBUTES (variant) == old_attrs)
424                             TYPE_ATTRIBUTES (variant)
425                               = TYPE_ATTRIBUTES (*anode);
426                           else if (!lookup_attribute
427                                    (spec->name, TYPE_ATTRIBUTES (variant)))
428                             TYPE_ATTRIBUTES (variant) = tree_cons
429                               (name, args, TYPE_ATTRIBUTES (variant));
430                         }
431                     }
432                 }
433               else
434                 *anode = build_type_attribute_variant (*anode,
435                                                        tree_cons (name, args,
436                                                                   old_attrs));
437             }
438         }
439
440       if (fn_ptr_tmp)
441         {
442           /* Rebuild the function pointer type and put it in the
443              appropriate place.  */
444           fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
445           if (fn_ptr_quals)
446             fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
447           if (DECL_P (*node))
448             TREE_TYPE (*node) = fn_ptr_tmp;
449           else
450             {
451               gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
452               *node = fn_ptr_tmp;
453             }
454         }
455     }
456
457   return returned_attrs;
458 }