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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
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
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
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/>. */
24 #include "coretypes.h"
28 #include "diagnostic-core.h"
33 #include "langhooks.h"
37 static void init_attributes (void);
39 /* Table of the tables of attributes (common, language, format, machine)
41 static const struct attribute_spec
*attribute_tables
[4];
43 /* Hashtable mapping names (represented as substrings) to attribute specs. */
44 static htab_t attribute_hash
;
46 /* Substring representation. */
54 static bool attributes_initialized
= false;
56 /* Default empty table of attributes. */
58 static const struct attribute_spec empty_attribute_table
[] =
60 { NULL
, 0, 0, false, false, false, NULL
}
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. */
67 extract_attribute_substring (struct substring
*str
)
69 if (str
->length
> 4 && str
->str
[0] == '_' && str
->str
[1] == '_'
70 && str
->str
[str
->length
- 1] == '_' && str
->str
[str
->length
- 2] == '_')
77 /* Simple hash function to avoid need to scan whole string. */
79 static inline hashval_t
80 substring_hash (const char *str
, int l
)
82 return str
[0] + str
[l
- 1] * 256 + l
* 65536;
85 /* Used for attribute_hash. */
88 hash_attr (const void *p
)
90 const struct attribute_spec
*const spec
= (const struct attribute_spec
*) p
;
91 const int l
= strlen (spec
->name
);
93 return substring_hash (spec
->name
, l
);
96 /* Used for attribute_hash. */
99 eq_attr (const void *p
, const void *q
)
101 const struct attribute_spec
*const spec
= (const struct attribute_spec
*) p
;
102 const struct substring
*const str
= (const struct substring
*) q
;
104 return (!strncmp (spec
->name
, str
->str
, str
->length
) && !spec
->name
[str
->length
]);
107 /* Initialize attribute tables, and make some sanity checks
108 if --enable-checking. */
111 init_attributes (void)
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
;
119 attribute_tables
[3] = targetm
.attribute_table
;
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
;
126 #ifdef ENABLE_CHECKING
127 /* Make some sanity checks on the attribute tables. */
128 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
132 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
134 /* The name must not begin and end with __. */
135 const char *name
= attribute_tables
[i
][j
].name
;
136 int len
= strlen (name
);
138 gcc_assert (!(name
[0] == '_' && name
[1] == '_'
139 && name
[len
- 1] == '_' && name
[len
- 2] == '_'));
141 /* The minimum and maximum lengths must be consistent. */
142 gcc_assert (attribute_tables
[i
][j
].min_length
>= 0);
144 gcc_assert (attribute_tables
[i
][j
].max_length
== -1
145 || (attribute_tables
[i
][j
].max_length
146 >= attribute_tables
[i
][j
].min_length
));
148 /* An attribute cannot require both a DECL and a TYPE. */
149 gcc_assert (!attribute_tables
[i
][j
].decl_required
150 || !attribute_tables
[i
][j
].type_required
);
152 /* If an attribute requires a function type, in particular
153 it requires a type. */
154 gcc_assert (!attribute_tables
[i
][j
].function_type_required
155 || attribute_tables
[i
][j
].type_required
);
159 /* Check that each name occurs just once in each table. */
160 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
163 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
164 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
165 gcc_assert (strcmp (attribute_tables
[i
][j
].name
,
166 attribute_tables
[i
][k
].name
));
168 /* Check that no name occurs in more than one table. */
169 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
173 for (j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
174 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
175 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
176 gcc_assert (strcmp (attribute_tables
[i
][k
].name
,
177 attribute_tables
[j
][l
].name
));
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
++)
185 register_attribute (&attribute_tables
[i
][k
]);
187 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES
, NULL
);
188 attributes_initialized
= true;
191 /* Insert a single ATTR into the attribute table. */
194 register_attribute (const struct attribute_spec
*attr
)
196 struct substring str
;
199 str
.str
= attr
->name
;
200 str
.length
= strlen (str
.str
);
201 slot
= htab_find_slot_with_hash (attribute_hash
, &str
,
202 substring_hash (str
.str
, str
.length
),
205 *slot
= (void *) CONST_CAST (struct attribute_spec
*, attr
);
208 /* Return the spec for the attribute named NAME. */
210 const struct attribute_spec
*
211 lookup_attribute_spec (tree name
)
213 struct substring attr
;
215 attr
.str
= IDENTIFIER_POINTER (name
);
216 attr
.length
= IDENTIFIER_LENGTH (name
);
217 extract_attribute_substring (&attr
);
218 return (const struct attribute_spec
*)
219 htab_find_with_hash (attribute_hash
, &attr
,
220 substring_hash (attr
.str
, attr
.length
));
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
230 a decl attribute to the declaration rather than to its type). */
233 decl_attributes (tree
*node
, tree attributes
, int flags
)
236 tree returned_attrs
= NULL_TREE
;
238 if (TREE_TYPE (*node
) == error_mark_node
)
241 if (!attributes_initialized
)
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
)
248 tree cur_attr
= lookup_attribute ("optimize", attributes
);
249 tree opts
= copy_list (current_optimize_pragma
);
253 = tree_cons (get_identifier ("optimize"), opts
, attributes
);
255 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
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
;
263 /* If this is a function and the user used #pragma GCC target, add the
264 options to the attribute((target(...))) list. */
265 if (TREE_CODE (*node
) == FUNCTION_DECL
266 && current_target_pragma
267 && targetm
.target_option
.valid_attribute_p (*node
, NULL_TREE
,
268 current_target_pragma
, 0))
270 tree cur_attr
= lookup_attribute ("target", attributes
);
271 tree opts
= copy_list (current_target_pragma
);
274 attributes
= tree_cons (get_identifier ("target"), opts
, attributes
);
276 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
279 targetm
.insert_attributes (*node
, &attributes
);
281 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
283 tree name
= TREE_PURPOSE (a
);
284 tree args
= TREE_VALUE (a
);
286 const struct attribute_spec
*spec
= lookup_attribute_spec (name
);
287 bool no_add_attrs
= 0;
288 int fn_ptr_quals
= 0;
289 tree fn_ptr_tmp
= NULL_TREE
;
293 warning (OPT_Wattributes
, "%qE attribute directive ignored",
297 else if (list_length (args
) < spec
->min_length
298 || (spec
->max_length
>= 0
299 && list_length (args
) > spec
->max_length
))
301 error ("wrong number of arguments specified for %qE attribute",
305 gcc_assert (is_attribute_p (spec
->name
, name
));
307 if (spec
->decl_required
&& !DECL_P (*anode
))
309 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
310 | (int) ATTR_FLAG_FUNCTION_NEXT
311 | (int) ATTR_FLAG_ARRAY_NEXT
))
313 /* Pass on this attribute to be tried again. */
314 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
319 warning (OPT_Wattributes
, "%qE attribute does not apply to types",
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. */
329 if (spec
->type_required
&& DECL_P (*anode
))
331 anode
= &TREE_TYPE (*anode
);
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
;
339 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
340 && TREE_CODE (*anode
) != METHOD_TYPE
)
342 if (TREE_CODE (*anode
) == POINTER_TYPE
343 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
344 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
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.
353 This would all be simpler if attributes were part of the
354 declarator, grumble grumble. */
355 fn_ptr_tmp
= TREE_TYPE (*anode
);
356 fn_ptr_quals
= TYPE_QUALS (*anode
);
358 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
360 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
362 /* Pass on this attribute to be tried again. */
363 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
367 if (TREE_CODE (*anode
) != FUNCTION_TYPE
368 && TREE_CODE (*anode
) != METHOD_TYPE
)
370 warning (OPT_Wattributes
,
371 "%qE attribute only applies to function types",
378 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
379 && TYPE_SIZE (*anode
) != NULL_TREE
)
381 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
385 if (spec
->handler
!= NULL
)
386 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
387 flags
, &no_add_attrs
),
390 /* Layout the decl in case anything changed. */
391 if (spec
->type_required
&& DECL_P (*node
)
392 && (TREE_CODE (*node
) == VAR_DECL
393 || TREE_CODE (*node
) == PARM_DECL
394 || TREE_CODE (*node
) == RESULT_DECL
))
395 relayout_decl (*node
);
403 old_attrs
= DECL_ATTRIBUTES (*anode
);
405 old_attrs
= TYPE_ATTRIBUTES (*anode
);
407 for (a
= lookup_attribute (spec
->name
, old_attrs
);
409 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
411 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
417 /* This attribute isn't already in the list. */
419 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
420 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
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
))
428 for (variant
= *anode
; variant
;
429 variant
= TYPE_NEXT_VARIANT (variant
))
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
));
442 *anode
= build_type_attribute_variant (*anode
,
443 tree_cons (name
, args
,
450 /* Rebuild the function pointer type and put it in the
451 appropriate place. */
452 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
454 fn_ptr_tmp
= build_qualified_type (fn_ptr_tmp
, fn_ptr_quals
);
456 TREE_TYPE (*node
) = fn_ptr_tmp
;
459 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
465 return returned_attrs
;