]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
Cast to unsigned long.
[thirdparty/gcc.git] / gcc / attribs.c
CommitLineData
e3f6ce11 1/* Functions dealing with attribute handling, used by most front ends.
80fabb90 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
7cf0dbf3 3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
e3f6ce11 5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
e3f6ce11 11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
e3f6ce11 21
22#include "config.h"
23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
e3f6ce11 26#include "tree.h"
27#include "flags.h"
0b205f4c 28#include "diagnostic-core.h"
e3f6ce11 29#include "ggc.h"
e3f6ce11 30#include "tm_p.h"
e3f6ce11 31#include "cpplib.h"
32#include "target.h"
26ca6c20 33#include "langhooks.h"
5b634bfa 34#include "hashtab.h"
e3fced1a 35#include "plugin.h"
e3f6ce11 36
aecda0d6 37static void init_attributes (void);
e3f6ce11 38
f8e93a2e 39/* Table of the tables of attributes (common, language, format, machine)
e3f6ce11 40 searched. */
41static const struct attribute_spec *attribute_tables[4];
42
5b634bfa 43/* Hashtable mapping names (represented as substrings) to attribute specs. */
44static htab_t attribute_hash;
45
46/* Substring representation. */
47
48struct substring
49{
50 const char *str;
51 int length;
52};
53
e3f6ce11 54static bool attributes_initialized = false;
55
e3f6ce11 56/* Default empty table of attributes. */
5b634bfa 57
e3f6ce11 58static const struct attribute_spec empty_attribute_table[] =
59{
60 { NULL, 0, 0, false, false, false, NULL }
61};
62
5b634bfa 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
66static void
67extract_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
79static inline hashval_t
80substring_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
87static hashval_t
88hash_attr (const void *p)
89{
b7bf20db 90 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
91 const int l = strlen (spec->name);
5b634bfa 92
93 return substring_hash (spec->name, l);
94}
95
96/* Used for attribute_hash. */
97
98static int
99eq_attr (const void *p, const void *q)
100{
b7bf20db 101 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
102 const struct substring *const str = (const struct substring *) q;
5b634bfa 103
104 return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
105}
106
e3f6ce11 107/* Initialize attribute tables, and make some sanity checks
108 if --enable-checking. */
109
110static void
aecda0d6 111init_attributes (void)
e3f6ce11 112{
3585dac7 113 size_t i;
5b634bfa 114 int k;
e3f6ce11 115
f8e93a2e 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;
e3f6ce11 119 attribute_tables[3] = targetm.attribute_table;
120
f8e93a2e 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
e3f6ce11 126#ifdef ENABLE_CHECKING
127 /* Make some sanity checks on the attribute tables. */
3585dac7 128 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 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);
a0c938f0 137
64db345d 138 gcc_assert (!(name[0] == '_' && name[1] == '_'
139 && name[len - 1] == '_' && name[len - 2] == '_'));
a0c938f0 140
e3f6ce11 141 /* The minimum and maximum lengths must be consistent. */
64db345d 142 gcc_assert (attribute_tables[i][j].min_length >= 0);
a0c938f0 143
64db345d 144 gcc_assert (attribute_tables[i][j].max_length == -1
145 || (attribute_tables[i][j].max_length
146 >= attribute_tables[i][j].min_length));
a0c938f0 147
e3f6ce11 148 /* An attribute cannot require both a DECL and a TYPE. */
64db345d 149 gcc_assert (!attribute_tables[i][j].decl_required
150 || !attribute_tables[i][j].type_required);
a0c938f0 151
e3f6ce11 152 /* If an attribute requires a function type, in particular
153 it requires a type. */
64db345d 154 gcc_assert (!attribute_tables[i][j].function_type_required
155 || attribute_tables[i][j].type_required);
e3f6ce11 156 }
157 }
158
159 /* Check that each name occurs just once in each table. */
3585dac7 160 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 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++)
64db345d 165 gcc_assert (strcmp (attribute_tables[i][j].name,
166 attribute_tables[i][k].name));
e3f6ce11 167 }
168 /* Check that no name occurs in more than one table. */
3585dac7 169 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 170 {
3585dac7 171 size_t j, k, l;
e3f6ce11 172
3585dac7 173 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
e3f6ce11 174 for (k = 0; attribute_tables[i][k].name != NULL; k++)
175 for (l = 0; attribute_tables[j][l].name != NULL; l++)
64db345d 176 gcc_assert (strcmp (attribute_tables[i][k].name,
177 attribute_tables[j][l].name));
e3f6ce11 178 }
179#endif
180
5b634bfa 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 {
e3fced1a 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
193void
48e1416a 194register_attribute (const struct attribute_spec *attr)
e3fced1a 195{
4fb84273 196 struct substring str;
402ba866 197 void **slot;
4fb84273 198
199 str.str = attr->name;
200 str.length = strlen (str.str);
402ba866 201 slot = htab_find_slot_with_hash (attribute_hash, &str,
202 substring_hash (str.str, str.length),
203 INSERT);
4fb84273 204 gcc_assert (!*slot);
402ba866 205 *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
e3f6ce11 206}
2fdd6488 207
208/* Return the spec for the attribute named NAME. */
209
210const struct attribute_spec *
211lookup_attribute_spec (tree name)
212{
213 struct substring attr;
214
215 attr.str = IDENTIFIER_POINTER (name);
216 attr.length = IDENTIFIER_LENGTH (name);
217 extract_attribute_substring (&attr);
364c0c59 218 return (const struct attribute_spec *)
219 htab_find_with_hash (attribute_hash, &attr,
220 substring_hash (attr.str, attr.length));
2fdd6488 221}
e3f6ce11 222\f
223/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
224 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
225 it should be modified in place; if a TYPE, a copy should be created
226 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
227 information, in the form of a bitwise OR of flags in enum attribute_flags
228 from tree.h. Depending on these flags, some attributes may be
229 returned to be applied at a later stage (for example, to apply
101cc430 230 a decl attribute to the declaration rather than to its type). */
e3f6ce11 231
232tree
aecda0d6 233decl_attributes (tree *node, tree attributes, int flags)
e3f6ce11 234{
235 tree a;
236 tree returned_attrs = NULL_TREE;
237
1ccba7b7 238 if (TREE_TYPE (*node) == error_mark_node)
239 return NULL_TREE;
240
e3f6ce11 241 if (!attributes_initialized)
242 init_attributes ();
243
46f8e3b0 244 /* If this is a function and the user used #pragma GCC optimize, add the
245 options to the attribute((optimize(...))) list. */
246 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
247 {
248 tree cur_attr = lookup_attribute ("optimize", attributes);
249 tree opts = copy_list (current_optimize_pragma);
250
251 if (! cur_attr)
252 attributes
253 = tree_cons (get_identifier ("optimize"), opts, attributes);
254 else
255 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
256 }
257
258 if (TREE_CODE (*node) == FUNCTION_DECL
259 && optimization_current_node != optimization_default_node
260 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
261 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
262
24470055 263 /* If this is a function and the user used #pragma GCC target, add the
264 options to the attribute((target(...))) list. */
46f8e3b0 265 if (TREE_CODE (*node) == FUNCTION_DECL
24470055 266 && current_target_pragma
46f8e3b0 267 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
24470055 268 current_target_pragma, 0))
46f8e3b0 269 {
24470055 270 tree cur_attr = lookup_attribute ("target", attributes);
271 tree opts = copy_list (current_target_pragma);
46f8e3b0 272
273 if (! cur_attr)
24470055 274 attributes = tree_cons (get_identifier ("target"), opts, attributes);
46f8e3b0 275 else
276 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
277 }
278
883b2e73 279 targetm.insert_attributes (*node, &attributes);
e3f6ce11 280
281 for (a = attributes; a; a = TREE_CHAIN (a))
282 {
283 tree name = TREE_PURPOSE (a);
284 tree args = TREE_VALUE (a);
285 tree *anode = node;
2fdd6488 286 const struct attribute_spec *spec = lookup_attribute_spec (name);
e3f6ce11 287 bool no_add_attrs = 0;
ce6dcb60 288 int fn_ptr_quals = 0;
79bdd5ff 289 tree fn_ptr_tmp = NULL_TREE;
e3f6ce11 290
291 if (spec == NULL)
292 {
abd3e6b5 293 warning (OPT_Wattributes, "%qE attribute directive ignored",
294 name);
e3f6ce11 295 continue;
296 }
297 else if (list_length (args) < spec->min_length
298 || (spec->max_length >= 0
299 && list_length (args) > spec->max_length))
300 {
abd3e6b5 301 error ("wrong number of arguments specified for %qE attribute",
302 name);
e3f6ce11 303 continue;
304 }
5b634bfa 305 gcc_assert (is_attribute_p (spec->name, name));
e3f6ce11 306
307 if (spec->decl_required && !DECL_P (*anode))
308 {
309 if (flags & ((int) ATTR_FLAG_DECL_NEXT
310 | (int) ATTR_FLAG_FUNCTION_NEXT
311 | (int) ATTR_FLAG_ARRAY_NEXT))
312 {
313 /* Pass on this attribute to be tried again. */
314 returned_attrs = tree_cons (name, args, returned_attrs);
315 continue;
316 }
317 else
318 {
abd3e6b5 319 warning (OPT_Wattributes, "%qE attribute does not apply to types",
320 name);
e3f6ce11 321 continue;
322 }
323 }
324
aa9c60c1 325 /* If we require a type, but were passed a decl, set up to make a
326 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
327 would have applied if we'd been passed a type, but we cannot modify
328 the decl's type in place here. */
e3f6ce11 329 if (spec->type_required && DECL_P (*anode))
aa9c60c1 330 {
331 anode = &TREE_TYPE (*anode);
3ef0a05e 332 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
333 if (!(TREE_CODE (*anode) == TYPE_DECL
334 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
335 (TREE_TYPE (*anode)))))
336 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
aa9c60c1 337 }
e3f6ce11 338
339 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
340 && TREE_CODE (*anode) != METHOD_TYPE)
341 {
342 if (TREE_CODE (*anode) == POINTER_TYPE
343 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
344 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
345 {
79bdd5ff 346 /* OK, this is a bit convoluted. We can't just make a copy
347 of the pointer type and modify its TREE_TYPE, because if
348 we change the attributes of the target type the pointer
349 type needs to have a different TYPE_MAIN_VARIANT. So we
350 pull out the target type now, frob it as appropriate, and
351 rebuild the pointer type later.
352
a0c938f0 353 This would all be simpler if attributes were part of the
354 declarator, grumble grumble. */
79bdd5ff 355 fn_ptr_tmp = TREE_TYPE (*anode);
ce6dcb60 356 fn_ptr_quals = TYPE_QUALS (*anode);
79bdd5ff 357 anode = &fn_ptr_tmp;
358 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
e3f6ce11 359 }
360 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
361 {
362 /* Pass on this attribute to be tried again. */
363 returned_attrs = tree_cons (name, args, returned_attrs);
364 continue;
365 }
366
367 if (TREE_CODE (*anode) != FUNCTION_TYPE
368 && TREE_CODE (*anode) != METHOD_TYPE)
369 {
9b2d6d13 370 warning (OPT_Wattributes,
abd3e6b5 371 "%qE attribute only applies to function types",
372 name);
e3f6ce11 373 continue;
374 }
375 }
376
4a2849cb 377 if (TYPE_P (*anode)
378 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
379 && TYPE_SIZE (*anode) != NULL_TREE)
380 {
381 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
382 continue;
383 }
384
e3f6ce11 385 if (spec->handler != NULL)
386 returned_attrs = chainon ((*spec->handler) (anode, name, args,
387 flags, &no_add_attrs),
388 returned_attrs);
ae4718db 389
390 /* Layout the decl in case anything changed. */
391 if (spec->type_required && DECL_P (*node)
e56de52f 392 && (TREE_CODE (*node) == VAR_DECL
393 || TREE_CODE (*node) == PARM_DECL
394 || TREE_CODE (*node) == RESULT_DECL))
1c0a6d1e 395 relayout_decl (*node);
ae4718db 396
e3f6ce11 397 if (!no_add_attrs)
398 {
399 tree old_attrs;
400 tree a;
401
402 if (DECL_P (*anode))
403 old_attrs = DECL_ATTRIBUTES (*anode);
404 else
405 old_attrs = TYPE_ATTRIBUTES (*anode);
406
407 for (a = lookup_attribute (spec->name, old_attrs);
408 a != NULL_TREE;
409 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
410 {
411 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
412 break;
413 }
414
415 if (a == NULL_TREE)
416 {
417 /* This attribute isn't already in the list. */
418 if (DECL_P (*anode))
419 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
420 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
316e17ae 421 {
422 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
423 /* If this is the main variant, also push the attributes
424 out to the other variants. */
425 if (*anode == TYPE_MAIN_VARIANT (*anode))
426 {
427 tree variant;
428 for (variant = *anode; variant;
429 variant = TYPE_NEXT_VARIANT (variant))
430 {
431 if (TYPE_ATTRIBUTES (variant) == old_attrs)
432 TYPE_ATTRIBUTES (variant)
433 = TYPE_ATTRIBUTES (*anode);
434 else if (!lookup_attribute
435 (spec->name, TYPE_ATTRIBUTES (variant)))
436 TYPE_ATTRIBUTES (variant) = tree_cons
437 (name, args, TYPE_ATTRIBUTES (variant));
438 }
439 }
440 }
e3f6ce11 441 else
442 *anode = build_type_attribute_variant (*anode,
443 tree_cons (name, args,
444 old_attrs));
445 }
446 }
79bdd5ff 447
448 if (fn_ptr_tmp)
449 {
450 /* Rebuild the function pointer type and put it in the
451 appropriate place. */
452 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
ce6dcb60 453 if (fn_ptr_quals)
454 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
79bdd5ff 455 if (DECL_P (*node))
456 TREE_TYPE (*node) = fn_ptr_tmp;
79bdd5ff 457 else
64db345d 458 {
459 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
460 *node = fn_ptr_tmp;
461 }
79bdd5ff 462 }
e3f6ce11 463 }
464
465 return returned_attrs;
466}