]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
2008-04-10 Oleg Ryjkov <olegr@google.com>
[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,
8c4c00c1 3 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
e3f6ce11 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
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
e3f6ce11 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
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
e3f6ce11 20
21#include "config.h"
22#include "system.h"
805e22b2 23#include "coretypes.h"
24#include "tm.h"
e3f6ce11 25#include "tree.h"
26#include "flags.h"
27#include "toplev.h"
28#include "output.h"
29#include "rtl.h"
30#include "ggc.h"
e3f6ce11 31#include "tm_p.h"
e3f6ce11 32#include "cpplib.h"
33#include "target.h"
26ca6c20 34#include "langhooks.h"
5b634bfa 35#include "hashtab.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 {
185 struct substring str;
b7bf20db 186 const void **slot;
5b634bfa 187
188 str.str = attribute_tables[i][k].name;
189 str.length = strlen (attribute_tables[i][k].name);
b7bf20db 190 slot = (const void **)htab_find_slot_with_hash (attribute_hash, &str,
5b634bfa 191 substring_hash (str.str, str.length),
192 INSERT);
193 gcc_assert (!*slot);
b7bf20db 194 *slot = &attribute_tables[i][k];
5b634bfa 195 }
e3f6ce11 196 attributes_initialized = true;
197}
2fdd6488 198
199/* Return the spec for the attribute named NAME. */
200
201const struct attribute_spec *
202lookup_attribute_spec (tree name)
203{
204 struct substring attr;
205
206 attr.str = IDENTIFIER_POINTER (name);
207 attr.length = IDENTIFIER_LENGTH (name);
208 extract_attribute_substring (&attr);
209 return htab_find_with_hash (attribute_hash, &attr,
210 substring_hash (attr.str, attr.length));
211}
e3f6ce11 212\f
213/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
214 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
215 it should be modified in place; if a TYPE, a copy should be created
216 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
217 information, in the form of a bitwise OR of flags in enum attribute_flags
218 from tree.h. Depending on these flags, some attributes may be
219 returned to be applied at a later stage (for example, to apply
101cc430 220 a decl attribute to the declaration rather than to its type). */
e3f6ce11 221
222tree
aecda0d6 223decl_attributes (tree *node, tree attributes, int flags)
e3f6ce11 224{
225 tree a;
226 tree returned_attrs = NULL_TREE;
227
228 if (!attributes_initialized)
229 init_attributes ();
230
883b2e73 231 targetm.insert_attributes (*node, &attributes);
e3f6ce11 232
233 for (a = attributes; a; a = TREE_CHAIN (a))
234 {
235 tree name = TREE_PURPOSE (a);
236 tree args = TREE_VALUE (a);
237 tree *anode = node;
2fdd6488 238 const struct attribute_spec *spec = lookup_attribute_spec (name);
e3f6ce11 239 bool no_add_attrs = 0;
79bdd5ff 240 tree fn_ptr_tmp = NULL_TREE;
e3f6ce11 241
242 if (spec == NULL)
243 {
9b2d6d13 244 warning (OPT_Wattributes, "%qs attribute directive ignored",
e3f6ce11 245 IDENTIFIER_POINTER (name));
246 continue;
247 }
248 else if (list_length (args) < spec->min_length
249 || (spec->max_length >= 0
250 && list_length (args) > spec->max_length))
251 {
eb586f2c 252 error ("wrong number of arguments specified for %qs attribute",
e3f6ce11 253 IDENTIFIER_POINTER (name));
254 continue;
255 }
5b634bfa 256 gcc_assert (is_attribute_p (spec->name, name));
e3f6ce11 257
258 if (spec->decl_required && !DECL_P (*anode))
259 {
260 if (flags & ((int) ATTR_FLAG_DECL_NEXT
261 | (int) ATTR_FLAG_FUNCTION_NEXT
262 | (int) ATTR_FLAG_ARRAY_NEXT))
263 {
264 /* Pass on this attribute to be tried again. */
265 returned_attrs = tree_cons (name, args, returned_attrs);
266 continue;
267 }
268 else
269 {
9b2d6d13 270 warning (OPT_Wattributes, "%qs attribute does not apply to types",
e3f6ce11 271 IDENTIFIER_POINTER (name));
272 continue;
273 }
274 }
275
aa9c60c1 276 /* If we require a type, but were passed a decl, set up to make a
277 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
278 would have applied if we'd been passed a type, but we cannot modify
279 the decl's type in place here. */
e3f6ce11 280 if (spec->type_required && DECL_P (*anode))
aa9c60c1 281 {
282 anode = &TREE_TYPE (*anode);
3ef0a05e 283 /* Allow ATTR_FLAG_TYPE_IN_PLACE for the type's naming decl. */
284 if (!(TREE_CODE (*anode) == TYPE_DECL
285 && *anode == TYPE_NAME (TYPE_MAIN_VARIANT
286 (TREE_TYPE (*anode)))))
287 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
aa9c60c1 288 }
e3f6ce11 289
290 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
291 && TREE_CODE (*anode) != METHOD_TYPE)
292 {
293 if (TREE_CODE (*anode) == POINTER_TYPE
294 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
295 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
296 {
79bdd5ff 297 /* OK, this is a bit convoluted. We can't just make a copy
298 of the pointer type and modify its TREE_TYPE, because if
299 we change the attributes of the target type the pointer
300 type needs to have a different TYPE_MAIN_VARIANT. So we
301 pull out the target type now, frob it as appropriate, and
302 rebuild the pointer type later.
303
a0c938f0 304 This would all be simpler if attributes were part of the
305 declarator, grumble grumble. */
79bdd5ff 306 fn_ptr_tmp = TREE_TYPE (*anode);
307 anode = &fn_ptr_tmp;
308 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
e3f6ce11 309 }
310 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
311 {
312 /* Pass on this attribute to be tried again. */
313 returned_attrs = tree_cons (name, args, returned_attrs);
314 continue;
315 }
316
317 if (TREE_CODE (*anode) != FUNCTION_TYPE
318 && TREE_CODE (*anode) != METHOD_TYPE)
319 {
9b2d6d13 320 warning (OPT_Wattributes,
321 "%qs attribute only applies to function types",
e3f6ce11 322 IDENTIFIER_POINTER (name));
323 continue;
324 }
325 }
326
4a2849cb 327 if (TYPE_P (*anode)
328 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
329 && TYPE_SIZE (*anode) != NULL_TREE)
330 {
331 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
332 continue;
333 }
334
e3f6ce11 335 if (spec->handler != NULL)
336 returned_attrs = chainon ((*spec->handler) (anode, name, args,
337 flags, &no_add_attrs),
338 returned_attrs);
ae4718db 339
340 /* Layout the decl in case anything changed. */
341 if (spec->type_required && DECL_P (*node)
e56de52f 342 && (TREE_CODE (*node) == VAR_DECL
343 || TREE_CODE (*node) == PARM_DECL
344 || TREE_CODE (*node) == RESULT_DECL))
1c0a6d1e 345 relayout_decl (*node);
ae4718db 346
e3f6ce11 347 if (!no_add_attrs)
348 {
349 tree old_attrs;
350 tree a;
351
352 if (DECL_P (*anode))
353 old_attrs = DECL_ATTRIBUTES (*anode);
354 else
355 old_attrs = TYPE_ATTRIBUTES (*anode);
356
357 for (a = lookup_attribute (spec->name, old_attrs);
358 a != NULL_TREE;
359 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
360 {
361 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
362 break;
363 }
364
365 if (a == NULL_TREE)
366 {
367 /* This attribute isn't already in the list. */
368 if (DECL_P (*anode))
369 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
370 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
316e17ae 371 {
372 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
373 /* If this is the main variant, also push the attributes
374 out to the other variants. */
375 if (*anode == TYPE_MAIN_VARIANT (*anode))
376 {
377 tree variant;
378 for (variant = *anode; variant;
379 variant = TYPE_NEXT_VARIANT (variant))
380 {
381 if (TYPE_ATTRIBUTES (variant) == old_attrs)
382 TYPE_ATTRIBUTES (variant)
383 = TYPE_ATTRIBUTES (*anode);
384 else if (!lookup_attribute
385 (spec->name, TYPE_ATTRIBUTES (variant)))
386 TYPE_ATTRIBUTES (variant) = tree_cons
387 (name, args, TYPE_ATTRIBUTES (variant));
388 }
389 }
390 }
e3f6ce11 391 else
392 *anode = build_type_attribute_variant (*anode,
393 tree_cons (name, args,
394 old_attrs));
395 }
396 }
79bdd5ff 397
398 if (fn_ptr_tmp)
399 {
400 /* Rebuild the function pointer type and put it in the
401 appropriate place. */
402 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
403 if (DECL_P (*node))
404 TREE_TYPE (*node) = fn_ptr_tmp;
79bdd5ff 405 else
64db345d 406 {
407 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
408 *node = fn_ptr_tmp;
409 }
79bdd5ff 410 }
e3f6ce11 411 }
412
413 return returned_attrs;
414}