Update change log
[platform/upstream/gcc48.git] / gcc / attribs.c
1 /* Functions dealing with attribute handling, used by most front ends.
2    Copyright (C) 1992-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "diagnostic-core.h"
27 #include "ggc.h"
28 #include "tm_p.h"
29 #include "cpplib.h"
30 #include "target.h"
31 #include "langhooks.h"
32 #include "hashtab.h"
33 #include "plugin.h"
34
35 /* Table of the tables of attributes (common, language, format, machine)
36    searched.  */
37 static const struct attribute_spec *attribute_tables[4];
38
39 /* Substring representation.  */
40
41 struct substring
42 {
43   const char *str;
44   int length;
45 };
46
47 /* Scoped attribute name representation.  */
48
49 struct scoped_attributes
50 {
51   const char *ns;
52   vec<attribute_spec> attributes;
53   htab_t attribute_hash;
54 };
55
56 /* The table of scope attributes.  */
57 static vec<scoped_attributes> attributes_table;
58
59 static scoped_attributes* find_attribute_namespace (const char*);
60 static void register_scoped_attribute (const struct attribute_spec *,
61                                        scoped_attributes *);
62
63 static bool attributes_initialized = false;
64
65 /* Default empty table of attributes.  */
66
67 static const struct attribute_spec empty_attribute_table[] =
68 {
69   { NULL, 0, 0, false, false, false, NULL, false }
70 };
71
72 /* Return base name of the attribute.  Ie '__attr__' is turned into 'attr'.
73    To avoid need for copying, we simply return length of the string.  */
74
75 static void
76 extract_attribute_substring (struct substring *str)
77 {
78   if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
79       && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
80     {
81       str->length -= 4;
82       str->str += 2;
83     }
84 }
85
86 /* Simple hash function to avoid need to scan whole string.  */
87
88 static inline hashval_t
89 substring_hash (const char *str, int l)
90 {
91   return str[0] + str[l - 1] * 256 + l * 65536;
92 }
93
94 /* Used for attribute_hash.  */
95
96 static hashval_t
97 hash_attr (const void *p)
98 {
99   const struct attribute_spec *const spec = (const struct attribute_spec *) p;
100   const int l = strlen (spec->name);
101
102   return substring_hash (spec->name, l);
103 }
104
105 /* Used for attribute_hash.  */
106
107 static int
108 eq_attr (const void *p, const void *q)
109 {
110   const struct attribute_spec *const spec = (const struct attribute_spec *) p;
111   const struct substring *const str = (const struct substring *) q;
112
113   return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
114 }
115
116 /* Insert an array of attributes ATTRIBUTES into a namespace.  This
117    array must be NULL terminated.  NS is the name of attribute
118    namespace.  The function returns the namespace into which the
119    attributes have been registered.  */
120
121 scoped_attributes*
122 register_scoped_attributes (const struct attribute_spec * attributes,
123                             const char* ns)
124 {
125   scoped_attributes *result = NULL;
126
127   /* See if we already have attributes in the namespace NS.  */
128   result = find_attribute_namespace (ns);
129
130   if (result == NULL)
131     {
132       /* We don't have any namespace NS yet.  Create one.  */
133       scoped_attributes sa;
134
135       if (!attributes_table.is_empty ())
136         attributes_table.create (64);
137
138       memset (&sa, 0, sizeof (sa));
139       sa.ns = ns;
140       sa.attributes.create (64);
141       result = attributes_table.safe_push (sa);
142       result->attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
143     }
144
145   /* Really add the attributes to their namespace now.  */
146   for (unsigned i = 0; attributes[i].name != NULL; ++i)
147     {
148       result->attributes.safe_push (attributes[i]);
149       register_scoped_attribute (&attributes[i], result);
150     }
151
152   gcc_assert (result != NULL);
153
154   return result;
155 }
156
157 /* Return the namespace which name is NS, NULL if none exist.  */
158
159 static scoped_attributes*
160 find_attribute_namespace (const char* ns)
161 {
162   unsigned ix;
163   scoped_attributes *iter;
164
165   FOR_EACH_VEC_ELT (attributes_table, ix, iter)
166     if (ns == iter->ns
167         || (iter->ns != NULL
168             && ns != NULL
169             && !strcmp (iter->ns, ns)))
170       return iter;
171   return NULL;
172 }
173
174 /* Initialize attribute tables, and make some sanity checks
175    if --enable-checking.  */
176
177 void
178 init_attributes (void)
179 {
180   size_t i;
181
182   if (attributes_initialized)
183     return;
184
185   attribute_tables[0] = lang_hooks.common_attribute_table;
186   attribute_tables[1] = lang_hooks.attribute_table;
187   attribute_tables[2] = lang_hooks.format_attribute_table;
188   attribute_tables[3] = targetm.attribute_table;
189
190   /* Translate NULL pointers to pointers to the empty table.  */
191   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
192     if (attribute_tables[i] == NULL)
193       attribute_tables[i] = empty_attribute_table;
194
195 #ifdef ENABLE_CHECKING
196   /* Make some sanity checks on the attribute tables.  */
197   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
198     {
199       int j;
200
201       for (j = 0; attribute_tables[i][j].name != NULL; j++)
202         {
203           /* The name must not begin and end with __.  */
204           const char *name = attribute_tables[i][j].name;
205           int len = strlen (name);
206
207           gcc_assert (!(name[0] == '_' && name[1] == '_'
208                         && name[len - 1] == '_' && name[len - 2] == '_'));
209
210           /* The minimum and maximum lengths must be consistent.  */
211           gcc_assert (attribute_tables[i][j].min_length >= 0);
212
213           gcc_assert (attribute_tables[i][j].max_length == -1
214                       || (attribute_tables[i][j].max_length
215                           >= attribute_tables[i][j].min_length));
216
217           /* An attribute cannot require both a DECL and a TYPE.  */
218           gcc_assert (!attribute_tables[i][j].decl_required
219                       || !attribute_tables[i][j].type_required);
220
221           /* If an attribute requires a function type, in particular
222              it requires a type.  */
223           gcc_assert (!attribute_tables[i][j].function_type_required
224                       || attribute_tables[i][j].type_required);
225         }
226     }
227
228   /* Check that each name occurs just once in each table.  */
229   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
230     {
231       int j, k;
232       for (j = 0; attribute_tables[i][j].name != NULL; j++)
233         for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
234           gcc_assert (strcmp (attribute_tables[i][j].name,
235                               attribute_tables[i][k].name));
236     }
237   /* Check that no name occurs in more than one table.  Names that
238      begin with '*' are exempt, and may be overridden.  */
239   for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
240     {
241       size_t j, k, l;
242
243       for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
244         for (k = 0; attribute_tables[i][k].name != NULL; k++)
245           for (l = 0; attribute_tables[j][l].name != NULL; l++)
246             gcc_assert (attribute_tables[i][k].name[0] == '*'
247                         || strcmp (attribute_tables[i][k].name,
248                                    attribute_tables[j][l].name));
249     }
250 #endif
251
252   for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
253     /* Put all the GNU attributes into the "gnu" namespace.  */
254     register_scoped_attributes (attribute_tables[i], "gnu");
255
256   invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
257   attributes_initialized = true;
258 }
259
260 /* Insert a single ATTR into the attribute table.  */
261
262 void
263 register_attribute (const struct attribute_spec *attr)
264 {
265   register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
266 }
267
268 /* Insert a single attribute ATTR into a namespace of attributes.  */
269
270 static void
271 register_scoped_attribute (const struct attribute_spec *attr,
272                            scoped_attributes *name_space)
273 {
274   struct substring str;
275   void **slot;
276
277   gcc_assert (attr != NULL && name_space != NULL);
278
279   gcc_assert (name_space->attribute_hash != NULL);
280
281   str.str = attr->name;
282   str.length = strlen (str.str);
283
284   /* Attribute names in the table must be in the form 'text' and not
285      in the form '__text__'.  */
286   gcc_assert (str.length > 0 && str.str[0] != '_');
287
288   slot = htab_find_slot_with_hash (name_space->attribute_hash, &str,
289                                    substring_hash (str.str, str.length),
290                                    INSERT);
291   gcc_assert (!*slot || attr->name[0] == '*');
292   *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
293 }
294
295 /* Return the spec for the scoped attribute with namespace NS and
296    name NAME.   */
297
298 const struct attribute_spec *
299 lookup_scoped_attribute_spec (const_tree ns, const_tree name)
300 {
301   struct substring attr;
302   scoped_attributes *attrs;
303
304   const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
305
306   attrs = find_attribute_namespace (ns_str);
307
308   if (attrs == NULL)
309     return NULL;
310
311   attr.str = IDENTIFIER_POINTER (name);
312   attr.length = IDENTIFIER_LENGTH (name);
313   extract_attribute_substring (&attr);
314   return (const struct attribute_spec *)
315     htab_find_with_hash (attrs->attribute_hash, &attr,
316                          substring_hash (attr.str, attr.length));
317 }
318
319 /* Return the spec for the attribute named NAME.  If NAME is a TREE_LIST,
320    it also specifies the attribute namespace.  */
321
322 const struct attribute_spec *
323 lookup_attribute_spec (const_tree name)
324 {
325   tree ns;
326   if (TREE_CODE (name) == TREE_LIST)
327     {
328       ns = TREE_PURPOSE (name);
329       name = TREE_VALUE (name);
330     }
331   else
332     ns = get_identifier ("gnu");
333   return lookup_scoped_attribute_spec (ns, name);
334 }
335
336 \f
337 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
338    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
339    it should be modified in place; if a TYPE, a copy should be created
340    unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS.  FLAGS gives further
341    information, in the form of a bitwise OR of flags in enum attribute_flags
342    from tree.h.  Depending on these flags, some attributes may be
343    returned to be applied at a later stage (for example, to apply
344    a decl attribute to the declaration rather than to its type).  */
345
346 tree
347 decl_attributes (tree *node, tree attributes, int flags)
348 {
349   tree a;
350   tree returned_attrs = NULL_TREE;
351
352   if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
353     return NULL_TREE;
354
355   if (!attributes_initialized)
356     init_attributes ();
357
358   /* If this is a function and the user used #pragma GCC optimize, add the
359      options to the attribute((optimize(...))) list.  */
360   if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
361     {
362       tree cur_attr = lookup_attribute ("optimize", attributes);
363       tree opts = copy_list (current_optimize_pragma);
364
365       if (! cur_attr)
366         attributes
367           = tree_cons (get_identifier ("optimize"), opts, attributes);
368       else
369         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
370     }
371
372   if (TREE_CODE (*node) == FUNCTION_DECL
373       && optimization_current_node != optimization_default_node
374       && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
375     DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
376
377   /* If this is a function and the user used #pragma GCC target, add the
378      options to the attribute((target(...))) list.  */
379   if (TREE_CODE (*node) == FUNCTION_DECL
380       && current_target_pragma
381       && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
382                                                   current_target_pragma, 0))
383     {
384       tree cur_attr = lookup_attribute ("target", attributes);
385       tree opts = copy_list (current_target_pragma);
386
387       if (! cur_attr)
388         attributes = tree_cons (get_identifier ("target"), opts, attributes);
389       else
390         TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
391     }
392
393   /* A "naked" function attribute implies "noinline" and "noclone" for
394      those targets that support it.  */
395   if (TREE_CODE (*node) == FUNCTION_DECL
396       && attributes
397       && lookup_attribute_spec (get_identifier ("naked"))
398       && lookup_attribute ("naked", attributes) != NULL)
399     {
400       if (lookup_attribute ("noinline", attributes) == NULL)
401         attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
402
403       if (lookup_attribute ("noclone", attributes) == NULL)
404         attributes = tree_cons (get_identifier ("noclone"),  NULL, attributes);
405     }
406
407   targetm.insert_attributes (*node, &attributes);
408
409   for (a = attributes; a; a = TREE_CHAIN (a))
410     {
411       tree ns = get_attribute_namespace (a);
412       tree name = get_attribute_name (a);
413       tree args = TREE_VALUE (a);
414       tree *anode = node;
415       const struct attribute_spec *spec =
416         lookup_scoped_attribute_spec (ns, name);
417       bool no_add_attrs = 0;
418       int fn_ptr_quals = 0;
419       tree fn_ptr_tmp = NULL_TREE;
420
421       if (spec == NULL)
422         {
423           if (!(flags & (int) ATTR_FLAG_BUILT_IN))
424             {
425               if (ns == NULL_TREE || !cxx11_attribute_p (a))
426                 warning (OPT_Wattributes, "%qE attribute directive ignored",
427                          name);
428               else
429                 warning (OPT_Wattributes,
430                          "%<%E::%E%> scoped attribute directive ignored",
431                          ns, name);
432             }
433           continue;
434         }
435       else if (list_length (args) < spec->min_length
436                || (spec->max_length >= 0
437                    && list_length (args) > spec->max_length))
438         {
439           error ("wrong number of arguments specified for %qE attribute",
440                  name);
441           continue;
442         }
443       gcc_assert (is_attribute_p (spec->name, name));
444
445       if (TYPE_P (*node)
446           && cxx11_attribute_p (a)
447           && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
448         {
449           /* This is a c++11 attribute that appertains to a
450              type-specifier, outside of the definition of, a class
451              type.  Ignore it.  */
452           warning (OPT_Wattributes, "attribute ignored");
453           inform (input_location,
454                   "an attribute that appertains to a type-specifier "
455                   "is ignored");
456           continue;
457         }
458
459       if (spec->decl_required && !DECL_P (*anode))
460         {
461           if (flags & ((int) ATTR_FLAG_DECL_NEXT
462                        | (int) ATTR_FLAG_FUNCTION_NEXT
463                        | (int) ATTR_FLAG_ARRAY_NEXT))
464             {
465               /* Pass on this attribute to be tried again.  */
466               returned_attrs = tree_cons (name, args, returned_attrs);
467               continue;
468             }
469           else
470             {
471               warning (OPT_Wattributes, "%qE attribute does not apply to types",
472                        name);
473               continue;
474             }
475         }
476
477       /* If we require a type, but were passed a decl, set up to make a
478          new type and update the one in the decl.  ATTR_FLAG_TYPE_IN_PLACE
479          would have applied if we'd been passed a type, but we cannot modify
480          the decl's type in place here.  */
481       if (spec->type_required && DECL_P (*anode))
482         {
483           anode = &TREE_TYPE (*anode);
484           /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl.  */
485           if (!(TREE_CODE (*anode) == TYPE_DECL
486                 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
487                                         (TREE_TYPE (*anode)))))
488             flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
489         }
490
491       if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
492           && TREE_CODE (*anode) != METHOD_TYPE)
493         {
494           if (TREE_CODE (*anode) == POINTER_TYPE
495               && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
496                   || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
497             {
498               /* OK, this is a bit convoluted.  We can't just make a copy
499                  of the pointer type and modify its TREE_TYPE, because if
500                  we change the attributes of the target type the pointer
501                  type needs to have a different TYPE_MAIN_VARIANT.  So we
502                  pull out the target type now, frob it as appropriate, and
503                  rebuild the pointer type later.
504
505                  This would all be simpler if attributes were part of the
506                  declarator, grumble grumble.  */
507               fn_ptr_tmp = TREE_TYPE (*anode);
508               fn_ptr_quals = TYPE_QUALS (*anode);
509               anode = &fn_ptr_tmp;
510               flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
511             }
512           else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
513             {
514               /* Pass on this attribute to be tried again.  */
515               returned_attrs = tree_cons (name, args, returned_attrs);
516               continue;
517             }
518
519           if (TREE_CODE (*anode) != FUNCTION_TYPE
520               && TREE_CODE (*anode) != METHOD_TYPE)
521             {
522               warning (OPT_Wattributes,
523                        "%qE attribute only applies to function types",
524                        name);
525               continue;
526             }
527         }
528
529       if (TYPE_P (*anode)
530           && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
531           && TYPE_SIZE (*anode) != NULL_TREE)
532         {
533           warning (OPT_Wattributes, "type attributes ignored after type is already defined");
534           continue;
535         }
536
537       if (spec->handler != NULL)
538         {
539           int cxx11_flag =
540             cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
541
542           returned_attrs = chainon ((*spec->handler) (anode, name, args,
543                                                       flags|cxx11_flag,
544                                                       &no_add_attrs),
545                                     returned_attrs);
546         }
547
548       /* Layout the decl in case anything changed.  */
549       if (spec->type_required && DECL_P (*node)
550           && (TREE_CODE (*node) == VAR_DECL
551               || TREE_CODE (*node) == PARM_DECL
552               || TREE_CODE (*node) == RESULT_DECL))
553         relayout_decl (*node);
554
555       if (!no_add_attrs)
556         {
557           tree old_attrs;
558           tree a;
559
560           if (DECL_P (*anode))
561             old_attrs = DECL_ATTRIBUTES (*anode);
562           else
563             old_attrs = TYPE_ATTRIBUTES (*anode);
564
565           for (a = lookup_attribute (spec->name, old_attrs);
566                a != NULL_TREE;
567                a = lookup_attribute (spec->name, TREE_CHAIN (a)))
568             {
569               if (simple_cst_equal (TREE_VALUE (a), args) == 1)
570                 break;
571             }
572
573           if (a == NULL_TREE)
574             {
575               /* This attribute isn't already in the list.  */
576               if (DECL_P (*anode))
577                 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
578               else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
579                 {
580                   TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
581                   /* If this is the main variant, also push the attributes
582                      out to the other variants.  */
583                   if (*anode == TYPE_MAIN_VARIANT (*anode))
584                     {
585                       tree variant;
586                       for (variant = *anode; variant;
587                            variant = TYPE_NEXT_VARIANT (variant))
588                         {
589                           if (TYPE_ATTRIBUTES (variant) == old_attrs)
590                             TYPE_ATTRIBUTES (variant)
591                               = TYPE_ATTRIBUTES (*anode);
592                           else if (!lookup_attribute
593                                    (spec->name, TYPE_ATTRIBUTES (variant)))
594                             TYPE_ATTRIBUTES (variant) = tree_cons
595                               (name, args, TYPE_ATTRIBUTES (variant));
596                         }
597                     }
598                 }
599               else
600                 *anode = build_type_attribute_variant (*anode,
601                                                        tree_cons (name, args,
602                                                                   old_attrs));
603             }
604         }
605
606       if (fn_ptr_tmp)
607         {
608           /* Rebuild the function pointer type and put it in the
609              appropriate place.  */
610           fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
611           if (fn_ptr_quals)
612             fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
613           if (DECL_P (*node))
614             TREE_TYPE (*node) = fn_ptr_tmp;
615           else
616             {
617               gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
618               *node = fn_ptr_tmp;
619             }
620         }
621     }
622
623   return returned_attrs;
624 }
625
626 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
627    attribute.
628
629    When G++ parses a C++11 attribute, it is represented as
630    a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST.  TREE_PURPOSE
631    (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
632    TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name.  Please
633    use get_attribute_namespace and get_attribute_name to retrieve the
634    namespace and name of the attribute, as these accessors work with
635    GNU attributes as well.  */
636
637 bool
638 cxx11_attribute_p (const_tree attr)
639 {
640   if (attr == NULL_TREE
641       || TREE_CODE (attr) != TREE_LIST)
642     return false;
643
644   return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
645 }
646
647 /* Return the name of the attribute ATTR.  This accessor works on GNU
648    and C++11 (scoped) attributes.
649
650    Please read the comments of cxx11_attribute_p to understand the
651    format of attributes.  */
652
653 tree
654 get_attribute_name (const_tree attr)
655 {
656   if (cxx11_attribute_p (attr))
657     return TREE_VALUE (TREE_PURPOSE (attr));
658   return TREE_PURPOSE (attr);
659 }
660
661 /* Return the namespace of the attribute ATTR.  This accessor works on
662    GNU and C++11 (scoped) attributes.  On GNU attributes,
663    it returns an identifier tree for the string "gnu".
664
665    Please read the comments of cxx11_attribute_p to understand the
666    format of attributes.  */
667
668 tree
669 get_attribute_namespace (const_tree attr)
670 {
671   if (cxx11_attribute_p (attr))
672     return TREE_PURPOSE (TREE_PURPOSE (attr));
673   return get_identifier ("gnu");
674 }
675
676 /* Subroutine of set_method_tm_attributes.  Apply TM attribute ATTR
677    to the method FNDECL.  */
678
679 void
680 apply_tm_attr (tree fndecl, tree attr)
681 {
682   decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
683 }