]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
re PR libfortran/47970 (c99_functions.c:611:5: warning: implicit declaration of funct...
[thirdparty/gcc.git] / gcc / attribs.c
CommitLineData
bb9f8221 1/* Functions dealing with attribute handling, used by most front ends.
2cc2d4bb 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
c75c517d
SB
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
bb9f8221
RK
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
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
bb9f8221
RK
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
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
bb9f8221
RK
21
22#include "config.h"
23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
bb9f8221
RK
26#include "tree.h"
27#include "flags.h"
718f9c0f 28#include "diagnostic-core.h"
bb9f8221 29#include "ggc.h"
bb9f8221 30#include "tm_p.h"
bb9f8221
RK
31#include "cpplib.h"
32#include "target.h"
7ffb4fd2 33#include "langhooks.h"
23b43207 34#include "hashtab.h"
d1c8e08a 35#include "plugin.h"
bb9f8221 36
349ae713 37/* Table of the tables of attributes (common, language, format, machine)
bb9f8221
RK
38 searched. */
39static const struct attribute_spec *attribute_tables[4];
40
23b43207
JH
41/* Hashtable mapping names (represented as substrings) to attribute specs. */
42static htab_t attribute_hash;
43
44/* Substring representation. */
45
46struct substring
47{
48 const char *str;
49 int length;
50};
51
bb9f8221
RK
52static bool attributes_initialized = false;
53
bb9f8221 54/* Default empty table of attributes. */
23b43207 55
bb9f8221
RK
56static const struct attribute_spec empty_attribute_table[] =
57{
62d784f7 58 { NULL, 0, 0, false, false, false, NULL, false }
bb9f8221
RK
59};
60
23b43207
JH
61/* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
62 To avoid need for copying, we simply return length of the string. */
63
64static void
65extract_attribute_substring (struct substring *str)
66{
67 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
68 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
69 {
70 str->length -= 4;
71 str->str += 2;
72 }
73}
74
75/* Simple hash function to avoid need to scan whole string. */
76
77static inline hashval_t
78substring_hash (const char *str, int l)
79{
80 return str[0] + str[l - 1] * 256 + l * 65536;
81}
82
83/* Used for attribute_hash. */
84
85static hashval_t
86hash_attr (const void *p)
87{
fa233e34
KG
88 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
89 const int l = strlen (spec->name);
23b43207
JH
90
91 return substring_hash (spec->name, l);
92}
93
94/* Used for attribute_hash. */
95
96static int
97eq_attr (const void *p, const void *q)
98{
fa233e34
KG
99 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
100 const struct substring *const str = (const struct substring *) q;
23b43207
JH
101
102 return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
103}
104
bb9f8221
RK
105/* Initialize attribute tables, and make some sanity checks
106 if --enable-checking. */
107
8dd00781 108void
4682ae04 109init_attributes (void)
bb9f8221 110{
ca7558fc 111 size_t i;
23b43207 112 int k;
bb9f8221 113
8dd00781
JJ
114 if (attributes_initialized)
115 return;
116
349ae713
NB
117 attribute_tables[0] = lang_hooks.common_attribute_table;
118 attribute_tables[1] = lang_hooks.attribute_table;
119 attribute_tables[2] = lang_hooks.format_attribute_table;
bb9f8221
RK
120 attribute_tables[3] = targetm.attribute_table;
121
349ae713
NB
122 /* Translate NULL pointers to pointers to the empty table. */
123 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
124 if (attribute_tables[i] == NULL)
125 attribute_tables[i] = empty_attribute_table;
126
bb9f8221
RK
127#ifdef ENABLE_CHECKING
128 /* Make some sanity checks on the attribute tables. */
ca7558fc 129 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
bb9f8221
RK
130 {
131 int j;
132
133 for (j = 0; attribute_tables[i][j].name != NULL; j++)
134 {
135 /* The name must not begin and end with __. */
136 const char *name = attribute_tables[i][j].name;
137 int len = strlen (name);
c22cacf3 138
298e6adc
NS
139 gcc_assert (!(name[0] == '_' && name[1] == '_'
140 && name[len - 1] == '_' && name[len - 2] == '_'));
c22cacf3 141
bb9f8221 142 /* The minimum and maximum lengths must be consistent. */
298e6adc 143 gcc_assert (attribute_tables[i][j].min_length >= 0);
c22cacf3 144
298e6adc
NS
145 gcc_assert (attribute_tables[i][j].max_length == -1
146 || (attribute_tables[i][j].max_length
147 >= attribute_tables[i][j].min_length));
c22cacf3 148
bb9f8221 149 /* An attribute cannot require both a DECL and a TYPE. */
298e6adc
NS
150 gcc_assert (!attribute_tables[i][j].decl_required
151 || !attribute_tables[i][j].type_required);
c22cacf3 152
bb9f8221
RK
153 /* If an attribute requires a function type, in particular
154 it requires a type. */
298e6adc
NS
155 gcc_assert (!attribute_tables[i][j].function_type_required
156 || attribute_tables[i][j].type_required);
bb9f8221
RK
157 }
158 }
159
160 /* Check that each name occurs just once in each table. */
ca7558fc 161 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
bb9f8221
RK
162 {
163 int j, k;
164 for (j = 0; attribute_tables[i][j].name != NULL; j++)
165 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
298e6adc
NS
166 gcc_assert (strcmp (attribute_tables[i][j].name,
167 attribute_tables[i][k].name));
bb9f8221
RK
168 }
169 /* Check that no name occurs in more than one table. */
ca7558fc 170 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
bb9f8221 171 {
ca7558fc 172 size_t j, k, l;
bb9f8221 173
ca7558fc 174 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
bb9f8221
RK
175 for (k = 0; attribute_tables[i][k].name != NULL; k++)
176 for (l = 0; attribute_tables[j][l].name != NULL; l++)
298e6adc
NS
177 gcc_assert (strcmp (attribute_tables[i][k].name,
178 attribute_tables[j][l].name));
bb9f8221
RK
179 }
180#endif
181
23b43207
JH
182 attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
183 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
184 for (k = 0; attribute_tables[i][k].name != NULL; k++)
185 {
d1c8e08a
TG
186 register_attribute (&attribute_tables[i][k]);
187 }
188 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
189 attributes_initialized = true;
190}
191
192/* Insert a single ATTR into the attribute table. */
193
194void
b8698a0f 195register_attribute (const struct attribute_spec *attr)
d1c8e08a 196{
16f7ad42 197 struct substring str;
67165eb3 198 void **slot;
16f7ad42
TG
199
200 str.str = attr->name;
201 str.length = strlen (str.str);
70e41a6a
NP
202
203 /* Attribute names in the table must be in the form 'text' and not
204 in the form '__text__'. */
205 gcc_assert (str.length > 0 && str.str[0] != '_');
206
67165eb3
ILT
207 slot = htab_find_slot_with_hash (attribute_hash, &str,
208 substring_hash (str.str, str.length),
209 INSERT);
16f7ad42 210 gcc_assert (!*slot);
67165eb3 211 *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
bb9f8221 212}
a7f6bc8c
JM
213
214/* Return the spec for the attribute named NAME. */
215
216const struct attribute_spec *
f231b5ff 217lookup_attribute_spec (const_tree name)
a7f6bc8c
JM
218{
219 struct substring attr;
220
221 attr.str = IDENTIFIER_POINTER (name);
222 attr.length = IDENTIFIER_LENGTH (name);
223 extract_attribute_substring (&attr);
f883e0a7
KG
224 return (const struct attribute_spec *)
225 htab_find_with_hash (attribute_hash, &attr,
226 substring_hash (attr.str, attr.length));
a7f6bc8c 227}
bb9f8221
RK
228\f
229/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
230 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
231 it should be modified in place; if a TYPE, a copy should be created
232 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
233 information, in the form of a bitwise OR of flags in enum attribute_flags
234 from tree.h. Depending on these flags, some attributes may be
235 returned to be applied at a later stage (for example, to apply
03aa99d4 236 a decl attribute to the declaration rather than to its type). */
bb9f8221
RK
237
238tree
4682ae04 239decl_attributes (tree *node, tree attributes, int flags)
bb9f8221
RK
240{
241 tree a;
242 tree returned_attrs = NULL_TREE;
243
21516d64
VR
244 if (TREE_TYPE (*node) == error_mark_node)
245 return NULL_TREE;
246
bb9f8221
RK
247 if (!attributes_initialized)
248 init_attributes ();
249
ab442df7
MM
250 /* If this is a function and the user used #pragma GCC optimize, add the
251 options to the attribute((optimize(...))) list. */
252 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
253 {
254 tree cur_attr = lookup_attribute ("optimize", attributes);
255 tree opts = copy_list (current_optimize_pragma);
256
257 if (! cur_attr)
258 attributes
259 = tree_cons (get_identifier ("optimize"), opts, attributes);
260 else
261 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
262 }
263
264 if (TREE_CODE (*node) == FUNCTION_DECL
265 && optimization_current_node != optimization_default_node
266 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
267 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
268
5779e713
MM
269 /* If this is a function and the user used #pragma GCC target, add the
270 options to the attribute((target(...))) list. */
ab442df7 271 if (TREE_CODE (*node) == FUNCTION_DECL
5779e713 272 && current_target_pragma
ab442df7 273 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
5779e713 274 current_target_pragma, 0))
ab442df7 275 {
5779e713
MM
276 tree cur_attr = lookup_attribute ("target", attributes);
277 tree opts = copy_list (current_target_pragma);
ab442df7
MM
278
279 if (! cur_attr)
5779e713 280 attributes = tree_cons (get_identifier ("target"), opts, attributes);
ab442df7
MM
281 else
282 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
283 }
284
61044492
JZ
285 /* A "naked" function attribute implies "noinline" and "noclone" for
286 those targets that support it. */
287 if (TREE_CODE (*node) == FUNCTION_DECL
70e41a6a 288 && attributes
61044492
JZ
289 && lookup_attribute_spec (get_identifier ("naked"))
290 && lookup_attribute ("naked", attributes) != NULL)
291 {
292 if (lookup_attribute ("noinline", attributes) == NULL)
293 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
294
295 if (lookup_attribute ("noclone", attributes) == NULL)
296 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
297 }
298
5fd9b178 299 targetm.insert_attributes (*node, &attributes);
bb9f8221
RK
300
301 for (a = attributes; a; a = TREE_CHAIN (a))
302 {
303 tree name = TREE_PURPOSE (a);
304 tree args = TREE_VALUE (a);
305 tree *anode = node;
a7f6bc8c 306 const struct attribute_spec *spec = lookup_attribute_spec (name);
bb9f8221 307 bool no_add_attrs = 0;
f9ceed32 308 int fn_ptr_quals = 0;
3acef2ae 309 tree fn_ptr_tmp = NULL_TREE;
bb9f8221
RK
310
311 if (spec == NULL)
312 {
4f1e4960
JM
313 warning (OPT_Wattributes, "%qE attribute directive ignored",
314 name);
bb9f8221
RK
315 continue;
316 }
317 else if (list_length (args) < spec->min_length
318 || (spec->max_length >= 0
319 && list_length (args) > spec->max_length))
320 {
4f1e4960
JM
321 error ("wrong number of arguments specified for %qE attribute",
322 name);
bb9f8221
RK
323 continue;
324 }
23b43207 325 gcc_assert (is_attribute_p (spec->name, name));
bb9f8221
RK
326
327 if (spec->decl_required && !DECL_P (*anode))
328 {
329 if (flags & ((int) ATTR_FLAG_DECL_NEXT
330 | (int) ATTR_FLAG_FUNCTION_NEXT
331 | (int) ATTR_FLAG_ARRAY_NEXT))
332 {
333 /* Pass on this attribute to be tried again. */
334 returned_attrs = tree_cons (name, args, returned_attrs);
335 continue;
336 }
337 else
338 {
4f1e4960
JM
339 warning (OPT_Wattributes, "%qE attribute does not apply to types",
340 name);
bb9f8221
RK
341 continue;
342 }
343 }
344
dd4dc3cd
RK
345 /* If we require a type, but were passed a decl, set up to make a
346 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
347 would have applied if we'd been passed a type, but we cannot modify
348 the decl's type in place here. */
bb9f8221 349 if (spec->type_required && DECL_P (*anode))
dd4dc3cd
RK
350 {
351 anode = &TREE_TYPE (*anode);
317c435f
JM
352 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
353 if (!(TREE_CODE (*anode) == TYPE_DECL
354 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
355 (TREE_TYPE (*anode)))))
356 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
dd4dc3cd 357 }
bb9f8221
RK
358
359 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
360 && TREE_CODE (*anode) != METHOD_TYPE)
361 {
362 if (TREE_CODE (*anode) == POINTER_TYPE
363 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
364 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
365 {
3acef2ae
JM
366 /* OK, this is a bit convoluted. We can't just make a copy
367 of the pointer type and modify its TREE_TYPE, because if
368 we change the attributes of the target type the pointer
369 type needs to have a different TYPE_MAIN_VARIANT. So we
370 pull out the target type now, frob it as appropriate, and
371 rebuild the pointer type later.
372
c22cacf3
MS
373 This would all be simpler if attributes were part of the
374 declarator, grumble grumble. */
3acef2ae 375 fn_ptr_tmp = TREE_TYPE (*anode);
f9ceed32 376 fn_ptr_quals = TYPE_QUALS (*anode);
3acef2ae
JM
377 anode = &fn_ptr_tmp;
378 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
bb9f8221
RK
379 }
380 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
381 {
382 /* Pass on this attribute to be tried again. */
383 returned_attrs = tree_cons (name, args, returned_attrs);
384 continue;
385 }
386
387 if (TREE_CODE (*anode) != FUNCTION_TYPE
388 && TREE_CODE (*anode) != METHOD_TYPE)
389 {
5c498b10 390 warning (OPT_Wattributes,
4f1e4960
JM
391 "%qE attribute only applies to function types",
392 name);
bb9f8221
RK
393 continue;
394 }
395 }
396
b9e75696
JM
397 if (TYPE_P (*anode)
398 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
399 && TYPE_SIZE (*anode) != NULL_TREE)
400 {
401 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
402 continue;
403 }
404
bb9f8221
RK
405 if (spec->handler != NULL)
406 returned_attrs = chainon ((*spec->handler) (anode, name, args,
407 flags, &no_add_attrs),
408 returned_attrs);
1b9191d2
AH
409
410 /* Layout the decl in case anything changed. */
411 if (spec->type_required && DECL_P (*node)
67282790
JM
412 && (TREE_CODE (*node) == VAR_DECL
413 || TREE_CODE (*node) == PARM_DECL
414 || TREE_CODE (*node) == RESULT_DECL))
d1838621 415 relayout_decl (*node);
1b9191d2 416
bb9f8221
RK
417 if (!no_add_attrs)
418 {
419 tree old_attrs;
420 tree a;
421
422 if (DECL_P (*anode))
423 old_attrs = DECL_ATTRIBUTES (*anode);
424 else
425 old_attrs = TYPE_ATTRIBUTES (*anode);
426
427 for (a = lookup_attribute (spec->name, old_attrs);
428 a != NULL_TREE;
429 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
430 {
431 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
432 break;
433 }
434
435 if (a == NULL_TREE)
436 {
437 /* This attribute isn't already in the list. */
438 if (DECL_P (*anode))
439 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
440 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
67214984
JM
441 {
442 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
443 /* If this is the main variant, also push the attributes
444 out to the other variants. */
445 if (*anode == TYPE_MAIN_VARIANT (*anode))
446 {
447 tree variant;
448 for (variant = *anode; variant;
449 variant = TYPE_NEXT_VARIANT (variant))
450 {
451 if (TYPE_ATTRIBUTES (variant) == old_attrs)
452 TYPE_ATTRIBUTES (variant)
453 = TYPE_ATTRIBUTES (*anode);
454 else if (!lookup_attribute
455 (spec->name, TYPE_ATTRIBUTES (variant)))
456 TYPE_ATTRIBUTES (variant) = tree_cons
457 (name, args, TYPE_ATTRIBUTES (variant));
458 }
459 }
460 }
bb9f8221
RK
461 else
462 *anode = build_type_attribute_variant (*anode,
463 tree_cons (name, args,
464 old_attrs));
465 }
466 }
3acef2ae
JM
467
468 if (fn_ptr_tmp)
469 {
470 /* Rebuild the function pointer type and put it in the
471 appropriate place. */
472 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
f9ceed32
MM
473 if (fn_ptr_quals)
474 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
3acef2ae
JM
475 if (DECL_P (*node))
476 TREE_TYPE (*node) = fn_ptr_tmp;
3acef2ae 477 else
298e6adc
NS
478 {
479 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
480 *node = fn_ptr_tmp;
481 }
3acef2ae 482 }
bb9f8221
RK
483 }
484
485 return returned_attrs;
486}