* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / attribs.c
blob313ebbf524316cf29cdc08e463f780d1c7d25d16
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "output.h"
30 #include "rtl.h"
31 #include "ggc.h"
32 #include "expr.h"
33 #include "tm_p.h"
34 #include "cpplib.h"
35 #include "target.h"
36 #include "langhooks.h"
38 static void init_attributes PARAMS ((void));
40 /* Table of the tables of attributes (common, language, format, machine)
41 searched. */
42 static const struct attribute_spec *attribute_tables[4];
44 static bool attributes_initialized = false;
46 /* Default empty table of attributes. */
47 static const struct attribute_spec empty_attribute_table[] =
49 { NULL, 0, 0, false, false, false, NULL }
52 /* Initialize attribute tables, and make some sanity checks
53 if --enable-checking. */
55 static void
56 init_attributes ()
58 size_t i;
60 attribute_tables[0] = lang_hooks.common_attribute_table;
61 attribute_tables[1] = lang_hooks.attribute_table;
62 attribute_tables[2] = lang_hooks.format_attribute_table;
63 attribute_tables[3] = targetm.attribute_table;
65 /* Translate NULL pointers to pointers to the empty table. */
66 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
67 if (attribute_tables[i] == NULL)
68 attribute_tables[i] = empty_attribute_table;
70 #ifdef ENABLE_CHECKING
71 /* Make some sanity checks on the attribute tables. */
72 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
74 int j;
76 for (j = 0; attribute_tables[i][j].name != NULL; j++)
78 /* The name must not begin and end with __. */
79 const char *name = attribute_tables[i][j].name;
80 int len = strlen (name);
81 if (name[0] == '_' && name[1] == '_'
82 && name[len - 1] == '_' && name[len - 2] == '_')
83 abort ();
84 /* The minimum and maximum lengths must be consistent. */
85 if (attribute_tables[i][j].min_length < 0)
86 abort ();
87 if (attribute_tables[i][j].max_length != -1
88 && (attribute_tables[i][j].max_length
89 < attribute_tables[i][j].min_length))
90 abort ();
91 /* An attribute cannot require both a DECL and a TYPE. */
92 if (attribute_tables[i][j].decl_required
93 && attribute_tables[i][j].type_required)
94 abort ();
95 /* If an attribute requires a function type, in particular
96 it requires a type. */
97 if (attribute_tables[i][j].function_type_required
98 && !attribute_tables[i][j].type_required)
99 abort ();
103 /* Check that each name occurs just once in each table. */
104 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
106 int j, k;
107 for (j = 0; attribute_tables[i][j].name != NULL; j++)
108 for (k = j + 1; attribute_tables[i][k].name != NULL; k++)
109 if (!strcmp (attribute_tables[i][j].name,
110 attribute_tables[i][k].name))
111 abort ();
113 /* Check that no name occurs in more than one table. */
114 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
116 size_t j, k, l;
118 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
119 for (k = 0; attribute_tables[i][k].name != NULL; k++)
120 for (l = 0; attribute_tables[j][l].name != NULL; l++)
121 if (!strcmp (attribute_tables[i][k].name,
122 attribute_tables[j][l].name))
123 abort ();
125 #endif
127 attributes_initialized = true;
130 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
131 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
132 it should be modified in place; if a TYPE, a copy should be created
133 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
134 information, in the form of a bitwise OR of flags in enum attribute_flags
135 from tree.h. Depending on these flags, some attributes may be
136 returned to be applied at a later stage (for example, to apply
137 a decl attribute to the declaration rather than to its type). If
138 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
139 whether there might be some default attributes to apply to this DECL;
140 if so, decl_attributes will be called recursively with those attributes
141 and ATTR_FLAG_BUILT_IN set. */
143 tree
144 decl_attributes (node, attributes, flags)
145 tree *node, attributes;
146 int flags;
148 tree a;
149 tree returned_attrs = NULL_TREE;
151 if (!attributes_initialized)
152 init_attributes ();
154 (*targetm.insert_attributes) (*node, &attributes);
156 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
157 && !(flags & (int) ATTR_FLAG_BUILT_IN))
158 (*lang_hooks.insert_default_attributes) (*node);
160 for (a = attributes; a; a = TREE_CHAIN (a))
162 tree name = TREE_PURPOSE (a);
163 tree args = TREE_VALUE (a);
164 tree *anode = node;
165 const struct attribute_spec *spec = NULL;
166 bool no_add_attrs = 0;
167 size_t i;
169 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
171 int j;
173 for (j = 0; attribute_tables[i][j].name != NULL; j++)
175 if (is_attribute_p (attribute_tables[i][j].name, name))
177 spec = &attribute_tables[i][j];
178 break;
181 if (spec != NULL)
182 break;
185 if (spec == NULL)
187 warning ("`%s' attribute directive ignored",
188 IDENTIFIER_POINTER (name));
189 continue;
191 else if (list_length (args) < spec->min_length
192 || (spec->max_length >= 0
193 && list_length (args) > spec->max_length))
195 error ("wrong number of arguments specified for `%s' attribute",
196 IDENTIFIER_POINTER (name));
197 continue;
200 if (spec->decl_required && !DECL_P (*anode))
202 if (flags & ((int) ATTR_FLAG_DECL_NEXT
203 | (int) ATTR_FLAG_FUNCTION_NEXT
204 | (int) ATTR_FLAG_ARRAY_NEXT))
206 /* Pass on this attribute to be tried again. */
207 returned_attrs = tree_cons (name, args, returned_attrs);
208 continue;
210 else
212 warning ("`%s' attribute does not apply to types",
213 IDENTIFIER_POINTER (name));
214 continue;
218 /* If we require a type, but were passed a decl, set up to make a
219 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
220 would have applied if we'd been passed a type, but we cannot modify
221 the decl's type in place here. */
222 if (spec->type_required && DECL_P (*anode))
224 anode = &TREE_TYPE (*anode);
225 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
228 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
229 && TREE_CODE (*anode) != METHOD_TYPE)
231 if (TREE_CODE (*anode) == POINTER_TYPE
232 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
233 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
235 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
236 *anode = build_type_copy (*anode);
237 anode = &TREE_TYPE (*anode);
239 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
241 /* Pass on this attribute to be tried again. */
242 returned_attrs = tree_cons (name, args, returned_attrs);
243 continue;
246 if (TREE_CODE (*anode) != FUNCTION_TYPE
247 && TREE_CODE (*anode) != METHOD_TYPE)
249 warning ("`%s' attribute only applies to function types",
250 IDENTIFIER_POINTER (name));
251 continue;
255 if (spec->handler != NULL)
256 returned_attrs = chainon ((*spec->handler) (anode, name, args,
257 flags, &no_add_attrs),
258 returned_attrs);
260 /* Layout the decl in case anything changed. */
261 if (spec->type_required && DECL_P (*node)
262 && (TREE_CODE (*node) == VAR_DECL
263 || TREE_CODE (*node) == PARM_DECL
264 || TREE_CODE (*node) == RESULT_DECL))
266 /* Force a recalculation of mode and size. */
267 DECL_MODE (*node) = VOIDmode;
268 DECL_SIZE (*node) = 0;
270 layout_decl (*node, 0);
273 if (!no_add_attrs)
275 tree old_attrs;
276 tree a;
278 if (DECL_P (*anode))
279 old_attrs = DECL_ATTRIBUTES (*anode);
280 else
281 old_attrs = TYPE_ATTRIBUTES (*anode);
283 for (a = lookup_attribute (spec->name, old_attrs);
284 a != NULL_TREE;
285 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
287 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
288 break;
291 if (a == NULL_TREE)
293 /* This attribute isn't already in the list. */
294 if (DECL_P (*anode))
295 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
296 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
297 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
298 else
299 *anode = build_type_attribute_variant (*anode,
300 tree_cons (name, args,
301 old_attrs));
306 return returned_attrs;
309 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
310 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
312 The head of the declspec list is stored in DECLSPECS.
313 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
315 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
316 the list elements. We drop the containing TREE_LIST nodes and link the
317 resulting attributes together the way decl_attributes expects them. */
319 void
320 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
321 tree specs_attrs;
322 tree *declspecs, *prefix_attributes;
324 tree t, s, a, next, specs, attrs;
326 /* This can happen after an __extension__ in pedantic mode. */
327 if (specs_attrs != NULL_TREE
328 && TREE_CODE (specs_attrs) == INTEGER_CST)
330 *declspecs = NULL_TREE;
331 *prefix_attributes = NULL_TREE;
332 return;
335 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
336 if (specs_attrs != NULL_TREE
337 && TREE_CODE (specs_attrs) != TREE_LIST)
339 *declspecs = specs_attrs;
340 *prefix_attributes = NULL_TREE;
341 return;
344 /* Remember to keep the lists in the same order, element-wise. */
346 specs = s = NULL_TREE;
347 attrs = a = NULL_TREE;
348 for (t = specs_attrs; t; t = next)
350 next = TREE_CHAIN (t);
351 /* Declspecs have a non-NULL TREE_VALUE. */
352 if (TREE_VALUE (t) != NULL_TREE)
354 if (specs == NULL_TREE)
355 specs = s = t;
356 else
358 TREE_CHAIN (s) = t;
359 s = t;
362 /* The TREE_PURPOSE may also be empty in the case of
363 __attribute__(()). */
364 else if (TREE_PURPOSE (t) != NULL_TREE)
366 if (attrs == NULL_TREE)
367 attrs = a = TREE_PURPOSE (t);
368 else
370 TREE_CHAIN (a) = TREE_PURPOSE (t);
371 a = TREE_PURPOSE (t);
373 /* More attrs can be linked here, move A to the end. */
374 while (TREE_CHAIN (a) != NULL_TREE)
375 a = TREE_CHAIN (a);
379 /* Terminate the lists. */
380 if (s != NULL_TREE)
381 TREE_CHAIN (s) = NULL_TREE;
382 if (a != NULL_TREE)
383 TREE_CHAIN (a) = NULL_TREE;
385 /* All done. */
386 *declspecs = specs;
387 *prefix_attributes = attrs;
390 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
391 This function is used by the parser when a rule will accept attributes
392 in a particular position, but we don't want to support that just yet.
394 A warning is issued for every ignored attribute. */
396 tree
397 strip_attrs (specs_attrs)
398 tree specs_attrs;
400 tree specs, attrs;
402 split_specs_attrs (specs_attrs, &specs, &attrs);
404 while (attrs)
406 warning ("`%s' attribute ignored",
407 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
408 attrs = TREE_CHAIN (attrs);
411 return specs;