]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
* config/mips/mips.c (override_options): Disable -G on targets that
[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,
aecda0d6 3 2002, 2003 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
9Software Foundation; either version 2, or (at your option) any later
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
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
e3f6ce11 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"
e3f6ce11 34#include "cpplib.h"
35#include "target.h"
26ca6c20 36#include "langhooks.h"
e3f6ce11 37
aecda0d6 38static void init_attributes (void);
e3f6ce11 39
f8e93a2e 40/* Table of the tables of attributes (common, language, format, machine)
e3f6ce11 41 searched. */
42static const struct attribute_spec *attribute_tables[4];
43
44static bool attributes_initialized = false;
45
e3f6ce11 46/* Default empty table of attributes. */
47static const struct attribute_spec empty_attribute_table[] =
48{
49 { NULL, 0, 0, false, false, false, NULL }
50};
51
e3f6ce11 52/* Initialize attribute tables, and make some sanity checks
53 if --enable-checking. */
54
55static void
aecda0d6 56init_attributes (void)
e3f6ce11 57{
3585dac7 58 size_t i;
e3f6ce11 59
f8e93a2e 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;
e3f6ce11 63 attribute_tables[3] = targetm.attribute_table;
64
f8e93a2e 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;
69
e3f6ce11 70#ifdef ENABLE_CHECKING
71 /* Make some sanity checks on the attribute tables. */
3585dac7 72 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 73 {
74 int j;
75
76 for (j = 0; attribute_tables[i][j].name != NULL; j++)
77 {
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 ();
100 }
101 }
102
103 /* Check that each name occurs just once in each table. */
3585dac7 104 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 105 {
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 ();
112 }
113 /* Check that no name occurs in more than one table. */
3585dac7 114 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 115 {
3585dac7 116 size_t j, k, l;
e3f6ce11 117
3585dac7 118 for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
e3f6ce11 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 ();
124 }
125#endif
126
127 attributes_initialized = true;
128}
129\f
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
7d3b509a 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;
35a3065a 140 if so, decl_attributes will be called recursively with those attributes
7d3b509a 141 and ATTR_FLAG_BUILT_IN set. */
e3f6ce11 142
143tree
aecda0d6 144decl_attributes (tree *node, tree attributes, int flags)
e3f6ce11 145{
146 tree a;
147 tree returned_attrs = NULL_TREE;
148
149 if (!attributes_initialized)
150 init_attributes ();
151
152 (*targetm.insert_attributes) (*node, &attributes);
153
7d3b509a 154 if (DECL_P (*node) && TREE_CODE (*node) == FUNCTION_DECL
155 && !(flags & (int) ATTR_FLAG_BUILT_IN))
26ca6c20 156 (*lang_hooks.insert_default_attributes) (*node);
7d3b509a 157
e3f6ce11 158 for (a = attributes; a; a = TREE_CHAIN (a))
159 {
160 tree name = TREE_PURPOSE (a);
161 tree args = TREE_VALUE (a);
162 tree *anode = node;
163 const struct attribute_spec *spec = NULL;
164 bool no_add_attrs = 0;
40265b77 165 size_t i;
e3f6ce11 166
3585dac7 167 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
e3f6ce11 168 {
169 int j;
170
171 for (j = 0; attribute_tables[i][j].name != NULL; j++)
172 {
173 if (is_attribute_p (attribute_tables[i][j].name, name))
174 {
175 spec = &attribute_tables[i][j];
176 break;
177 }
178 }
179 if (spec != NULL)
180 break;
181 }
182
183 if (spec == NULL)
184 {
185 warning ("`%s' attribute directive ignored",
186 IDENTIFIER_POINTER (name));
187 continue;
188 }
189 else if (list_length (args) < spec->min_length
190 || (spec->max_length >= 0
191 && list_length (args) > spec->max_length))
192 {
193 error ("wrong number of arguments specified for `%s' attribute",
194 IDENTIFIER_POINTER (name));
195 continue;
196 }
197
198 if (spec->decl_required && !DECL_P (*anode))
199 {
200 if (flags & ((int) ATTR_FLAG_DECL_NEXT
201 | (int) ATTR_FLAG_FUNCTION_NEXT
202 | (int) ATTR_FLAG_ARRAY_NEXT))
203 {
204 /* Pass on this attribute to be tried again. */
205 returned_attrs = tree_cons (name, args, returned_attrs);
206 continue;
207 }
208 else
209 {
210 warning ("`%s' attribute does not apply to types",
211 IDENTIFIER_POINTER (name));
212 continue;
213 }
214 }
215
aa9c60c1 216 /* If we require a type, but were passed a decl, set up to make a
217 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
218 would have applied if we'd been passed a type, but we cannot modify
219 the decl's type in place here. */
e3f6ce11 220 if (spec->type_required && DECL_P (*anode))
aa9c60c1 221 {
222 anode = &TREE_TYPE (*anode);
223 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
224 }
e3f6ce11 225
226 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
227 && TREE_CODE (*anode) != METHOD_TYPE)
228 {
229 if (TREE_CODE (*anode) == POINTER_TYPE
230 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
231 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
232 {
233 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
234 *anode = build_type_copy (*anode);
235 anode = &TREE_TYPE (*anode);
236 }
237 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
238 {
239 /* Pass on this attribute to be tried again. */
240 returned_attrs = tree_cons (name, args, returned_attrs);
241 continue;
242 }
243
244 if (TREE_CODE (*anode) != FUNCTION_TYPE
245 && TREE_CODE (*anode) != METHOD_TYPE)
246 {
247 warning ("`%s' attribute only applies to function types",
248 IDENTIFIER_POINTER (name));
249 continue;
250 }
251 }
252
253 if (spec->handler != NULL)
254 returned_attrs = chainon ((*spec->handler) (anode, name, args,
255 flags, &no_add_attrs),
256 returned_attrs);
ae4718db 257
258 /* Layout the decl in case anything changed. */
259 if (spec->type_required && DECL_P (*node)
e56de52f 260 && (TREE_CODE (*node) == VAR_DECL
261 || TREE_CODE (*node) == PARM_DECL
262 || TREE_CODE (*node) == RESULT_DECL))
ae4718db 263 {
264 /* Force a recalculation of mode and size. */
265 DECL_MODE (*node) = VOIDmode;
266 DECL_SIZE (*node) = 0;
267
268 layout_decl (*node, 0);
269 }
270
e3f6ce11 271 if (!no_add_attrs)
272 {
273 tree old_attrs;
274 tree a;
275
276 if (DECL_P (*anode))
277 old_attrs = DECL_ATTRIBUTES (*anode);
278 else
279 old_attrs = TYPE_ATTRIBUTES (*anode);
280
281 for (a = lookup_attribute (spec->name, old_attrs);
282 a != NULL_TREE;
283 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
284 {
285 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
286 break;
287 }
288
289 if (a == NULL_TREE)
290 {
291 /* This attribute isn't already in the list. */
292 if (DECL_P (*anode))
293 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
294 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
295 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
296 else
297 *anode = build_type_attribute_variant (*anode,
298 tree_cons (name, args,
299 old_attrs));
300 }
301 }
302 }
303
304 return returned_attrs;
305}
306
e3f6ce11 307/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
308 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
309
310 The head of the declspec list is stored in DECLSPECS.
311 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
312
313 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
314 the list elements. We drop the containing TREE_LIST nodes and link the
315 resulting attributes together the way decl_attributes expects them. */
316
317void
aecda0d6 318split_specs_attrs (tree specs_attrs, tree *declspecs, tree *prefix_attributes)
e3f6ce11 319{
320 tree t, s, a, next, specs, attrs;
321
322 /* This can happen after an __extension__ in pedantic mode. */
9342ee68 323 if (specs_attrs != NULL_TREE
e3f6ce11 324 && TREE_CODE (specs_attrs) == INTEGER_CST)
325 {
326 *declspecs = NULL_TREE;
327 *prefix_attributes = NULL_TREE;
328 return;
329 }
330
331 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
332 if (specs_attrs != NULL_TREE
333 && TREE_CODE (specs_attrs) != TREE_LIST)
334 {
335 *declspecs = specs_attrs;
336 *prefix_attributes = NULL_TREE;
337 return;
338 }
339
340 /* Remember to keep the lists in the same order, element-wise. */
341
342 specs = s = NULL_TREE;
343 attrs = a = NULL_TREE;
344 for (t = specs_attrs; t; t = next)
345 {
346 next = TREE_CHAIN (t);
347 /* Declspecs have a non-NULL TREE_VALUE. */
348 if (TREE_VALUE (t) != NULL_TREE)
349 {
350 if (specs == NULL_TREE)
351 specs = s = t;
352 else
353 {
354 TREE_CHAIN (s) = t;
355 s = t;
356 }
357 }
358 /* The TREE_PURPOSE may also be empty in the case of
359 __attribute__(()). */
360 else if (TREE_PURPOSE (t) != NULL_TREE)
361 {
362 if (attrs == NULL_TREE)
363 attrs = a = TREE_PURPOSE (t);
364 else
365 {
366 TREE_CHAIN (a) = TREE_PURPOSE (t);
367 a = TREE_PURPOSE (t);
368 }
369 /* More attrs can be linked here, move A to the end. */
370 while (TREE_CHAIN (a) != NULL_TREE)
371 a = TREE_CHAIN (a);
372 }
373 }
374
375 /* Terminate the lists. */
376 if (s != NULL_TREE)
377 TREE_CHAIN (s) = NULL_TREE;
378 if (a != NULL_TREE)
379 TREE_CHAIN (a) = NULL_TREE;
380
381 /* All done. */
382 *declspecs = specs;
383 *prefix_attributes = attrs;
384}
385
386/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
387 This function is used by the parser when a rule will accept attributes
388 in a particular position, but we don't want to support that just yet.
389
390 A warning is issued for every ignored attribute. */
391
392tree
aecda0d6 393strip_attrs (tree specs_attrs)
e3f6ce11 394{
395 tree specs, attrs;
396
397 split_specs_attrs (specs_attrs, &specs, &attrs);
398
399 while (attrs)
400 {
401 warning ("`%s' attribute ignored",
402 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
403 attrs = TREE_CHAIN (attrs);
404 }
405
406 return specs;
407}