OSDN Git Service

In gcc/:
[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, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "diagnostic-core.h"
29 #include "ggc.h"
30 #include "tm_p.h"
31 #include "cpplib.h"
32 #include "target.h"
33 #include "langhooks.h"
34 #include "hashtab.h"
35 #include "plugin.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, false }
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         register_attribute (&attribute_tables[i][k]);
186       }
187   invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
188   attributes_initialized = true;
189 }
190
191 /* Insert a single ATTR into the attribute table.  */
192
193 void
194 register_attribute (const struct attribute_spec *attr)
195 {
196   struct substring str;
197   void **slot;
198
199   str.str = attr->name;
200   str.length = strlen (str.str);
201
202   /* Attribute names in the table must be in the form 'text' and not
203      in the form '__text__'.  */
204   gcc_assert (str.length > 0 && str.str[0] != '_');
205
206   slot = htab_find_slot_with_hash (attribute_hash, &str,
207                                    substring_hash (str.str, str.length),
208                                    INSERT);
209   gcc_assert (!*slot);
210   *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
211 }
212
213 /* Return the spec for the attribute named NAME.  */
214
215 const struct attribute_spec *
216 lookup_attribute_spec (const_tree name)
217 {
218   struct substring attr;
219
220   attr.str = IDENTIFIER_POINTER (name);
221   attr.length = IDENTIFIER_LENGTH (name);
222   extract_attribute_substring (&attr);
223   return (const struct attribute_spec *)
224     htab_find_with_hash (attribute_hash, &attr,
225                          substring_hash (attr.str, attr.length));
226 }
227 \f
228 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
229    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
230    it should be modified in place; if a TYPE, a copy should be created
231    unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS.  FLAGS gives further
232    information, in the form of a bitwise OR of flags in enum attribute_flags
233    from tree.h.  Depending on these flags, some attributes may be
234    returned to be applied at a later stage (for example, to apply
235    a decl attribute to the declaration rather than to its type).  */
236
237 tree
238 decl_attributes (tree *node, tree attributes, int flags)
239 {
240   tree a;
241   tree returned_attrs = NULL_TREE;
242
243   if (TREE_TYPE (*node) == error_mark_node)
244     return NULL_TREE;
245
246   if (!attributes_initialized)
247     init_attributes ();
248
249   /* If this is a function and the user used #pragma GCC optimize, add the
250      options to the attribute((optimize(...))) list.  */
251   if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
252     {
253       tree cur_attr = lookup_attribute ("optimize", attributes);
254       tree opts = copy_list (current_optimize_pragma);
255
256       if (! cur_attr)
257         attributes
258           = tree_cons (get_identifier ("optimize"), opts, attributes);
259       else
260         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
261     }
262
263   if (TREE_CODE (*node) == FUNCTION_DECL
264       && optimization_current_node != optimization_default_node
265       && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
266     DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
267
268   /* If this is a function and the user used #pragma GCC target, add the
269      options to the attribute((target(...))) list.  */
270   if (TREE_CODE (*node) == FUNCTION_DECL
271       && current_target_pragma
272       && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
273                                                   current_target_pragma, 0))
274     {
275       tree cur_attr = lookup_attribute ("target", attributes);
276       tree opts = copy_list (current_target_pragma);
277
278       if (! cur_attr)
279         attributes = tree_cons (get_identifier ("target"), opts, attributes);
280       else
281         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
282     }
283
284   /* A "naked" function attribute implies "noinline" and "noclone" for
285      those targets that support it.  */
286   if (TREE_CODE (*node) == FUNCTION_DECL
287       && attributes
288       && lookup_attribute_spec (get_identifier ("naked"))
289       && lookup_attribute ("naked", attributes) != NULL)
290     {
291       if (lookup_attribute ("noinline", attributes) == NULL)
292         attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
293
294       if (lookup_attribute ("noclone", attributes) == NULL)
295         attributes = tree_cons (get_identifier ("noclone"),  NULL, attributes);
296     }
297
298   targetm.insert_attributes (*node, &attributes);
299
300   for (a = attributes; a; a = TREE_CHAIN (a))
301     {
302       tree name = TREE_PURPOSE (a);
303       tree args = TREE_VALUE (a);
304       tree *anode = node;
305       const struct attribute_spec *spec = lookup_attribute_spec (name);
306       bool no_add_attrs = 0;
307       int fn_ptr_quals = 0;
308       tree fn_ptr_tmp = NULL_TREE;
309
310       if (spec == NULL)
311         {
312           warning (OPT_Wattributes, "%qE attribute directive ignored",
313                    name);
314           continue;
315         }
316       else if (list_length (args) < spec->min_length
317                || (spec->max_length >= 0
318                    && list_length (args) > spec->max_length))
319         {
320           error ("wrong number of arguments specified for %qE attribute",
321                  name);
322           continue;
323         }
324       gcc_assert (is_attribute_p (spec->name, name));
325
326       if (spec->decl_required && !DECL_P (*anode))
327         {
328           if (flags & ((int) ATTR_FLAG_DECL_NEXT
329                        | (int) ATTR_FLAG_FUNCTION_NEXT
330                        | (int) ATTR_FLAG_ARRAY_NEXT))
331             {
332               /* Pass on this attribute to be tried again.  */
333               returned_attrs = tree_cons (name, args, returned_attrs);
334               continue;
335             }
336           else
337             {
338               warning (OPT_Wattributes, "%qE attribute does not apply to types",
339                        name);
340               continue;
341             }
342         }
343
344       /* If we require a type, but were passed a decl, set up to make a
345          new type and update the one in the decl.  ATTR_FLAG_TYPE_IN_PLACE
346          would have applied if we'd been passed a type, but we cannot modify
347          the decl's type in place here.  */
348       if (spec->type_required && DECL_P (*anode))
349         {
350           anode = &TREE_TYPE (*anode);
351           /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl.  */
352           if (!(TREE_CODE (*anode) == TYPE_DECL
353                 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
354                                         (TREE_TYPE (*anode)))))
355             flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
356         }
357
358       if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
359           && TREE_CODE (*anode) != METHOD_TYPE)
360         {
361           if (TREE_CODE (*anode) == POINTER_TYPE
362               && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
363                   || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
364             {
365               /* OK, this is a bit convoluted.  We can't just make a copy
366                  of the pointer type and modify its TREE_TYPE, because if
367                  we change the attributes of the target type the pointer
368                  type needs to have a different TYPE_MAIN_VARIANT.  So we
369                  pull out the target type now, frob it as appropriate, and
370                  rebuild the pointer type later.
371
372                  This would all be simpler if attributes were part of the
373                  declarator, grumble grumble.  */
374               fn_ptr_tmp = TREE_TYPE (*anode);
375               fn_ptr_quals = TYPE_QUALS (*anode);
376               anode = &fn_ptr_tmp;
377               flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
378             }
379           else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
380             {
381               /* Pass on this attribute to be tried again.  */
382               returned_attrs = tree_cons (name, args, returned_attrs);
383               continue;
384             }
385
386           if (TREE_CODE (*anode) != FUNCTION_TYPE
387               && TREE_CODE (*anode) != METHOD_TYPE)
388             {
389               warning (OPT_Wattributes,
390                        "%qE attribute only applies to function types",
391                        name);
392               continue;
393             }
394         }
395
396       if (TYPE_P (*anode)
397           && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
398           && TYPE_SIZE (*anode) != NULL_TREE)
399         {
400           warning (OPT_Wattributes, "type attributes ignored after type is already defined");
401           continue;
402         }
403
404       if (spec->handler != NULL)
405         returned_attrs = chainon ((*spec->handler) (anode, name, args,
406                                                     flags, &no_add_attrs),
407                                   returned_attrs);
408
409       /* Layout the decl in case anything changed.  */
410       if (spec->type_required && DECL_P (*node)
411           && (TREE_CODE (*node) == VAR_DECL
412               || TREE_CODE (*node) == PARM_DECL
413               || TREE_CODE (*node) == RESULT_DECL))
414         relayout_decl (*node);
415
416       if (!no_add_attrs)
417         {
418           tree old_attrs;
419           tree a;
420
421           if (DECL_P (*anode))
422             old_attrs = DECL_ATTRIBUTES (*anode);
423           else
424             old_attrs = TYPE_ATTRIBUTES (*anode);
425
426           for (a = lookup_attribute (spec->name, old_attrs);
427                a != NULL_TREE;
428                a = lookup_attribute (spec->name, TREE_CHAIN (a)))
429             {
430               if (simple_cst_equal (TREE_VALUE (a), args) == 1)
431                 break;
432             }
433
434           if (a == NULL_TREE)
435             {
436               /* This attribute isn't already in the list.  */
437               if (DECL_P (*anode))
438                 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
439               else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
440                 {
441                   TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
442                   /* If this is the main variant, also push the attributes
443                      out to the other variants.  */
444                   if (*anode == TYPE_MAIN_VARIANT (*anode))
445                     {
446                       tree variant;
447                       for (variant = *anode; variant;
448                            variant = TYPE_NEXT_VARIANT (variant))
449                         {
450                           if (TYPE_ATTRIBUTES (variant) == old_attrs)
451                             TYPE_ATTRIBUTES (variant)
452                               = TYPE_ATTRIBUTES (*anode);
453                           else if (!lookup_attribute
454                                    (spec->name, TYPE_ATTRIBUTES (variant)))
455                             TYPE_ATTRIBUTES (variant) = tree_cons
456                               (name, args, TYPE_ATTRIBUTES (variant));
457                         }
458                     }
459                 }
460               else
461                 *anode = build_type_attribute_variant (*anode,
462                                                        tree_cons (name, args,
463                                                                   old_attrs));
464             }
465         }
466
467       if (fn_ptr_tmp)
468         {
469           /* Rebuild the function pointer type and put it in the
470              appropriate place.  */
471           fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
472           if (fn_ptr_quals)
473             fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
474           if (DECL_P (*node))
475             TREE_TYPE (*node) = fn_ptr_tmp;
476           else
477             {
478               gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
479               *node = fn_ptr_tmp;
480             }
481         }
482     }
483
484   return returned_attrs;
485 }