OSDN Git Service

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