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
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
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
34 #include "langhooks.h"
36 static void init_attributes
PARAMS ((void));
38 /* Table of the tables of attributes (common, language, format, machine)
40 static const struct attribute_spec
*attribute_tables
[4];
42 static bool attributes_initialized
= false;
44 /* Default empty table of attributes. */
45 static const struct attribute_spec empty_attribute_table
[] =
47 { NULL
, 0, 0, false, false, false, NULL
}
50 /* Initialize attribute tables, and make some sanity checks
51 if --enable-checking. */
58 attribute_tables
[0] = lang_hooks
.common_attribute_table
;
59 attribute_tables
[1] = lang_hooks
.attribute_table
;
60 attribute_tables
[2] = lang_hooks
.format_attribute_table
;
61 attribute_tables
[3] = targetm
.attribute_table
;
63 /* Translate NULL pointers to pointers to the empty table. */
64 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
65 if (attribute_tables
[i
] == NULL
)
66 attribute_tables
[i
] = empty_attribute_table
;
68 #ifdef ENABLE_CHECKING
69 /* Make some sanity checks on the attribute tables. */
70 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
74 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
76 /* The name must not begin and end with __. */
77 const char *name
= attribute_tables
[i
][j
].name
;
78 int len
= strlen (name
);
79 if (name
[0] == '_' && name
[1] == '_'
80 && name
[len
- 1] == '_' && name
[len
- 2] == '_')
82 /* The minimum and maximum lengths must be consistent. */
83 if (attribute_tables
[i
][j
].min_length
< 0)
85 if (attribute_tables
[i
][j
].max_length
!= -1
86 && (attribute_tables
[i
][j
].max_length
87 < attribute_tables
[i
][j
].min_length
))
89 /* An attribute cannot require both a DECL and a TYPE. */
90 if (attribute_tables
[i
][j
].decl_required
91 && attribute_tables
[i
][j
].type_required
)
93 /* If an attribute requires a function type, in particular
94 it requires a type. */
95 if (attribute_tables
[i
][j
].function_type_required
96 && !attribute_tables
[i
][j
].type_required
)
101 /* Check that each name occurs just once in each table. */
102 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
105 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
106 for (k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
107 if (!strcmp (attribute_tables
[i
][j
].name
,
108 attribute_tables
[i
][k
].name
))
111 /* Check that no name occurs in more than one table. */
112 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
116 for (j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
117 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
118 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
119 if (!strcmp (attribute_tables
[i
][k
].name
,
120 attribute_tables
[j
][l
].name
))
125 attributes_initialized
= true;
128 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
129 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
130 it should be modified in place; if a TYPE, a copy should be created
131 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
132 information, in the form of a bitwise OR of flags in enum attribute_flags
133 from tree.h. Depending on these flags, some attributes may be
134 returned to be applied at a later stage (for example, to apply
135 a decl attribute to the declaration rather than to its type). If
136 ATTR_FLAG_BUILT_IN is not set and *NODE is a DECL, then also consider
137 whether there might be some default attributes to apply to this DECL;
138 if so, decl_attributes will be called recursively with those attributes
139 and ATTR_FLAG_BUILT_IN set. */
142 decl_attributes (node
, attributes
, flags
)
143 tree
*node
, attributes
;
147 tree returned_attrs
= NULL_TREE
;
149 if (!attributes_initialized
)
152 (*targetm
.insert_attributes
) (*node
, &attributes
);
154 if (DECL_P (*node
) && TREE_CODE (*node
) == FUNCTION_DECL
155 && !(flags
& (int) ATTR_FLAG_BUILT_IN
))
156 (*lang_hooks
.insert_default_attributes
) (*node
);
158 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
160 tree name
= TREE_PURPOSE (a
);
161 tree args
= TREE_VALUE (a
);
163 const struct attribute_spec
*spec
= NULL
;
164 bool no_add_attrs
= 0;
167 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
171 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
173 if (is_attribute_p (attribute_tables
[i
][j
].name
, name
))
175 spec
= &attribute_tables
[i
][j
];
185 warning ("`%s' attribute directive ignored",
186 IDENTIFIER_POINTER (name
));
189 else if (list_length (args
) < spec
->min_length
190 || (spec
->max_length
>= 0
191 && list_length (args
) > spec
->max_length
))
193 error ("wrong number of arguments specified for `%s' attribute",
194 IDENTIFIER_POINTER (name
));
198 if (spec
->decl_required
&& !DECL_P (*anode
))
200 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
201 | (int) ATTR_FLAG_FUNCTION_NEXT
202 | (int) ATTR_FLAG_ARRAY_NEXT
))
204 /* Pass on this attribute to be tried again. */
205 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
210 warning ("`%s' attribute does not apply to types",
211 IDENTIFIER_POINTER (name
));
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. */
220 if (spec
->type_required
&& DECL_P (*anode
))
222 anode
= &TREE_TYPE (*anode
);
223 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
226 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
227 && TREE_CODE (*anode
) != METHOD_TYPE
)
229 if (TREE_CODE (*anode
) == POINTER_TYPE
230 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
231 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
233 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
234 *anode
= build_type_copy (*anode
);
235 anode
= &TREE_TYPE (*anode
);
237 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
239 /* Pass on this attribute to be tried again. */
240 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
244 if (TREE_CODE (*anode
) != FUNCTION_TYPE
245 && TREE_CODE (*anode
) != METHOD_TYPE
)
247 warning ("`%s' attribute only applies to function types",
248 IDENTIFIER_POINTER (name
));
253 if (spec
->handler
!= NULL
)
254 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
255 flags
, &no_add_attrs
),
258 /* Layout the decl in case anything changed. */
259 if (spec
->type_required
&& DECL_P (*node
)
260 && (TREE_CODE (*node
) == VAR_DECL
261 || TREE_CODE (*node
) == PARM_DECL
262 || TREE_CODE (*node
) == RESULT_DECL
))
264 /* Force a recalculation of mode and size. */
265 DECL_MODE (*node
) = VOIDmode
;
266 DECL_SIZE (*node
) = 0;
268 layout_decl (*node
, 0);
277 old_attrs
= DECL_ATTRIBUTES (*anode
);
279 old_attrs
= TYPE_ATTRIBUTES (*anode
);
281 for (a
= lookup_attribute (spec
->name
, old_attrs
);
283 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
285 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
291 /* This attribute isn't already in the list. */
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
);
297 *anode
= build_type_attribute_variant (*anode
,
298 tree_cons (name
, args
,
304 return returned_attrs
;
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).
310 The head of the declspec list is stored in DECLSPECS.
311 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
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. */
318 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
320 tree
*declspecs
, *prefix_attributes
;
322 tree t
, s
, a
, next
, specs
, attrs
;
324 /* This can happen after an __extension__ in pedantic mode. */
325 if (specs_attrs
!= NULL_TREE
326 && TREE_CODE (specs_attrs
) == INTEGER_CST
)
328 *declspecs
= NULL_TREE
;
329 *prefix_attributes
= NULL_TREE
;
333 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
334 if (specs_attrs
!= NULL_TREE
335 && TREE_CODE (specs_attrs
) != TREE_LIST
)
337 *declspecs
= specs_attrs
;
338 *prefix_attributes
= NULL_TREE
;
342 /* Remember to keep the lists in the same order, element-wise. */
344 specs
= s
= NULL_TREE
;
345 attrs
= a
= NULL_TREE
;
346 for (t
= specs_attrs
; t
; t
= next
)
348 next
= TREE_CHAIN (t
);
349 /* Declspecs have a non-NULL TREE_VALUE. */
350 if (TREE_VALUE (t
) != NULL_TREE
)
352 if (specs
== NULL_TREE
)
360 /* The TREE_PURPOSE may also be empty in the case of
361 __attribute__(()). */
362 else if (TREE_PURPOSE (t
) != NULL_TREE
)
364 if (attrs
== NULL_TREE
)
365 attrs
= a
= TREE_PURPOSE (t
);
368 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
369 a
= TREE_PURPOSE (t
);
371 /* More attrs can be linked here, move A to the end. */
372 while (TREE_CHAIN (a
) != NULL_TREE
)
377 /* Terminate the lists. */
379 TREE_CHAIN (s
) = NULL_TREE
;
381 TREE_CHAIN (a
) = NULL_TREE
;
385 *prefix_attributes
= attrs
;
388 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
389 This function is used by the parser when a rule will accept attributes
390 in a particular position, but we don't want to support that just yet.
392 A warning is issued for every ignored attribute. */
395 strip_attrs (specs_attrs
)
400 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
404 warning ("`%s' attribute ignored",
405 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
406 attrs
= TREE_CHAIN (attrs
);