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