]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
gimple.texi (Logical Operators): Describe is_gimple_ip_invariant and is_gimple_ip_inv...
[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,
67165eb3 3 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
bb9f8221
RK
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
bb9f8221
RK
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
bb9f8221
RK
20
21#include "config.h"
22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
bb9f8221
RK
25#include "tree.h"
26#include "flags.h"
27#include "toplev.h"
28#include "output.h"
29#include "rtl.h"
30#include "ggc.h"
bb9f8221 31#include "tm_p.h"
bb9f8221
RK
32#include "cpplib.h"
33#include "target.h"
7ffb4fd2 34#include "langhooks.h"
23b43207 35#include "hashtab.h"
d1c8e08a 36#include "plugin.h"
bb9f8221 37
4682ae04 38static void init_attributes (void);
bb9f8221 39
349ae713 40/* Table of the tables of attributes (common, language, format, machine)
bb9f8221
RK
41 searched. */
42static const struct attribute_spec *attribute_tables[4];
43
23b43207
JH
44/* Hashtable mapping names (represented as substrings) to attribute specs. */
45static htab_t attribute_hash;
46
47/* Substring representation. */
48
49struct substring
50{
51 const char *str;
52 int length;
53};
54
bb9f8221
RK
55static bool attributes_initialized = false;
56
bb9f8221 57/* Default empty table of attributes. */
23b43207 58
bb9f8221
RK
59static const struct attribute_spec empty_attribute_table[] =
60{
61 { NULL, 0, 0, false, false, false, NULL }
62};
63
23b43207
JH
64/* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
65 To avoid need for copying, we simply return length of the string. */
66
67static void
68extract_attribute_substring (struct substring *str)
69{
70 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
71 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
72 {
73 str->length -= 4;
74 str->str += 2;
75 }
76}
77
78/* Simple hash function to avoid need to scan whole string. */
79
80static inline hashval_t
81substring_hash (const char *str, int l)
82{
83 return str[0] + str[l - 1] * 256 + l * 65536;
84}
85
86/* Used for attribute_hash. */
87
88static hashval_t
89hash_attr (const void *p)
90{
fa233e34
KG
91 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
92 const int l = strlen (spec->name);
23b43207
JH
93
94 return substring_hash (spec->name, l);
95}
96
97/* Used for attribute_hash. */
98
99static int
100eq_attr (const void *p, const void *q)
101{
fa233e34
KG
102 const struct attribute_spec *const spec = (const struct attribute_spec *) p;
103 const struct substring *const str = (const struct substring *) q;
23b43207
JH
104
105 return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
106}
107
bb9f8221
RK
108/* Initialize attribute tables, and make some sanity checks
109 if --enable-checking. */
110
111static void
4682ae04 112init_attributes (void)
bb9f8221 113{
ca7558fc 114 size_t i;
23b43207 115 int k;
bb9f8221 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);
67165eb3
ILT
202 slot = htab_find_slot_with_hash (attribute_hash, &str,
203 substring_hash (str.str, str.length),
204 INSERT);
16f7ad42 205 gcc_assert (!*slot);
67165eb3 206 *slot = (void *) CONST_CAST (struct attribute_spec *, attr);
bb9f8221 207}
a7f6bc8c
JM
208
209/* Return the spec for the attribute named NAME. */
210
211const struct attribute_spec *
212lookup_attribute_spec (tree name)
213{
214 struct substring attr;
215
216 attr.str = IDENTIFIER_POINTER (name);
217 attr.length = IDENTIFIER_LENGTH (name);
218 extract_attribute_substring (&attr);
f883e0a7
KG
219 return (const struct attribute_spec *)
220 htab_find_with_hash (attribute_hash, &attr,
221 substring_hash (attr.str, attr.length));
a7f6bc8c 222}
bb9f8221
RK
223\f
224/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
225 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
226 it should be modified in place; if a TYPE, a copy should be created
227 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
228 information, in the form of a bitwise OR of flags in enum attribute_flags
229 from tree.h. Depending on these flags, some attributes may be
230 returned to be applied at a later stage (for example, to apply
03aa99d4 231 a decl attribute to the declaration rather than to its type). */
bb9f8221
RK
232
233tree
4682ae04 234decl_attributes (tree *node, tree attributes, int flags)
bb9f8221
RK
235{
236 tree a;
237 tree returned_attrs = NULL_TREE;
238
21516d64
VR
239 if (TREE_TYPE (*node) == error_mark_node)
240 return NULL_TREE;
241
bb9f8221
RK
242 if (!attributes_initialized)
243 init_attributes ();
244
ab442df7
MM
245 /* If this is a function and the user used #pragma GCC optimize, add the
246 options to the attribute((optimize(...))) list. */
247 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
248 {
249 tree cur_attr = lookup_attribute ("optimize", attributes);
250 tree opts = copy_list (current_optimize_pragma);
251
252 if (! cur_attr)
253 attributes
254 = tree_cons (get_identifier ("optimize"), opts, attributes);
255 else
256 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
257 }
258
259 if (TREE_CODE (*node) == FUNCTION_DECL
260 && optimization_current_node != optimization_default_node
261 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
262 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
263
5779e713
MM
264 /* If this is a function and the user used #pragma GCC target, add the
265 options to the attribute((target(...))) list. */
ab442df7 266 if (TREE_CODE (*node) == FUNCTION_DECL
5779e713 267 && current_target_pragma
ab442df7 268 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
5779e713 269 current_target_pragma, 0))
ab442df7 270 {
5779e713
MM
271 tree cur_attr = lookup_attribute ("target", attributes);
272 tree opts = copy_list (current_target_pragma);
ab442df7
MM
273
274 if (! cur_attr)
5779e713 275 attributes = tree_cons (get_identifier ("target"), opts, attributes);
ab442df7
MM
276 else
277 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
278 }
279
5fd9b178 280 targetm.insert_attributes (*node, &attributes);
bb9f8221
RK
281
282 for (a = attributes; a; a = TREE_CHAIN (a))
283 {
284 tree name = TREE_PURPOSE (a);
285 tree args = TREE_VALUE (a);
286 tree *anode = node;
a7f6bc8c 287 const struct attribute_spec *spec = lookup_attribute_spec (name);
bb9f8221 288 bool no_add_attrs = 0;
3acef2ae 289 tree fn_ptr_tmp = NULL_TREE;
bb9f8221
RK
290
291 if (spec == NULL)
292 {
4f1e4960
JM
293 warning (OPT_Wattributes, "%qE attribute directive ignored",
294 name);
bb9f8221
RK
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 {
4f1e4960
JM
301 error ("wrong number of arguments specified for %qE attribute",
302 name);
bb9f8221
RK
303 continue;
304 }
23b43207 305 gcc_assert (is_attribute_p (spec->name, name));
bb9f8221
RK
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 {
4f1e4960
JM
319 warning (OPT_Wattributes, "%qE attribute does not apply to types",
320 name);
bb9f8221
RK
321 continue;
322 }
323 }
324
dd4dc3cd
RK
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. */
bb9f8221 329 if (spec->type_required && DECL_P (*anode))
dd4dc3cd
RK
330 {
331 anode = &TREE_TYPE (*anode);
317c435f
JM
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;
dd4dc3cd 337 }
bb9f8221
RK
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 {
3acef2ae
JM
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
c22cacf3
MS
353 This would all be simpler if attributes were part of the
354 declarator, grumble grumble. */
3acef2ae
JM
355 fn_ptr_tmp = TREE_TYPE (*anode);
356 anode = &fn_ptr_tmp;
357 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
bb9f8221
RK
358 }
359 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
360 {
361 /* Pass on this attribute to be tried again. */
362 returned_attrs = tree_cons (name, args, returned_attrs);
363 continue;
364 }
365
366 if (TREE_CODE (*anode) != FUNCTION_TYPE
367 && TREE_CODE (*anode) != METHOD_TYPE)
368 {
5c498b10 369 warning (OPT_Wattributes,
4f1e4960
JM
370 "%qE attribute only applies to function types",
371 name);
bb9f8221
RK
372 continue;
373 }
374 }
375
b9e75696
JM
376 if (TYPE_P (*anode)
377 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
378 && TYPE_SIZE (*anode) != NULL_TREE)
379 {
380 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
381 continue;
382 }
383
bb9f8221
RK
384 if (spec->handler != NULL)
385 returned_attrs = chainon ((*spec->handler) (anode, name, args,
386 flags, &no_add_attrs),
387 returned_attrs);
1b9191d2
AH
388
389 /* Layout the decl in case anything changed. */
390 if (spec->type_required && DECL_P (*node)
67282790
JM
391 && (TREE_CODE (*node) == VAR_DECL
392 || TREE_CODE (*node) == PARM_DECL
393 || TREE_CODE (*node) == RESULT_DECL))
d1838621 394 relayout_decl (*node);
1b9191d2 395
bb9f8221
RK
396 if (!no_add_attrs)
397 {
398 tree old_attrs;
399 tree a;
400
401 if (DECL_P (*anode))
402 old_attrs = DECL_ATTRIBUTES (*anode);
403 else
404 old_attrs = TYPE_ATTRIBUTES (*anode);
405
406 for (a = lookup_attribute (spec->name, old_attrs);
407 a != NULL_TREE;
408 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
409 {
410 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
411 break;
412 }
413
414 if (a == NULL_TREE)
415 {
416 /* This attribute isn't already in the list. */
417 if (DECL_P (*anode))
418 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
419 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
67214984
JM
420 {
421 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
422 /* If this is the main variant, also push the attributes
423 out to the other variants. */
424 if (*anode == TYPE_MAIN_VARIANT (*anode))
425 {
426 tree variant;
427 for (variant = *anode; variant;
428 variant = TYPE_NEXT_VARIANT (variant))
429 {
430 if (TYPE_ATTRIBUTES (variant) == old_attrs)
431 TYPE_ATTRIBUTES (variant)
432 = TYPE_ATTRIBUTES (*anode);
433 else if (!lookup_attribute
434 (spec->name, TYPE_ATTRIBUTES (variant)))
435 TYPE_ATTRIBUTES (variant) = tree_cons
436 (name, args, TYPE_ATTRIBUTES (variant));
437 }
438 }
439 }
bb9f8221
RK
440 else
441 *anode = build_type_attribute_variant (*anode,
442 tree_cons (name, args,
443 old_attrs));
444 }
445 }
3acef2ae
JM
446
447 if (fn_ptr_tmp)
448 {
449 /* Rebuild the function pointer type and put it in the
450 appropriate place. */
451 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
452 if (DECL_P (*node))
453 TREE_TYPE (*node) = fn_ptr_tmp;
3acef2ae 454 else
298e6adc
NS
455 {
456 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
457 *node = fn_ptr_tmp;
458 }
3acef2ae 459 }
bb9f8221
RK
460 }
461
462 return returned_attrs;
463}