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 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
34 #include "langhooks.h"
36 static void init_attributes (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. */
54 init_attributes (void)
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
);
80 gcc_assert (!(name
[0] == '_' && name
[1] == '_'
81 && name
[len
- 1] == '_' && name
[len
- 2] == '_'));
83 /* The minimum and maximum lengths must be consistent. */
84 gcc_assert (attribute_tables
[i
][j
].min_length
>= 0);
86 gcc_assert (attribute_tables
[i
][j
].max_length
== -1
87 || (attribute_tables
[i
][j
].max_length
88 >= attribute_tables
[i
][j
].min_length
));
90 /* An attribute cannot require both a DECL and a TYPE. */
91 gcc_assert (!attribute_tables
[i
][j
].decl_required
92 || !attribute_tables
[i
][j
].type_required
);
94 /* If an attribute requires a function type, in particular
95 it requires a type. */
96 gcc_assert (!attribute_tables
[i
][j
].function_type_required
97 || 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 gcc_assert (strcmp (attribute_tables
[i
][j
].name
,
108 attribute_tables
[i
][k
].name
));
110 /* Check that no name occurs in more than one table. */
111 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
115 for (j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
116 for (k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
117 for (l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
118 gcc_assert (strcmp (attribute_tables
[i
][k
].name
,
119 attribute_tables
[j
][l
].name
));
123 attributes_initialized
= true;
126 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
127 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
128 it should be modified in place; if a TYPE, a copy should be created
129 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
130 information, in the form of a bitwise OR of flags in enum attribute_flags
131 from tree.h. Depending on these flags, some attributes may be
132 returned to be applied at a later stage (for example, to apply
133 a decl attribute to the declaration rather than to its type). */
136 decl_attributes (tree
*node
, tree attributes
, int flags
)
139 tree returned_attrs
= NULL_TREE
;
141 if (TREE_TYPE (*node
) == error_mark_node
)
144 if (!attributes_initialized
)
147 targetm
.insert_attributes (*node
, &attributes
);
149 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
151 tree name
= TREE_PURPOSE (a
);
152 tree args
= TREE_VALUE (a
);
154 const struct attribute_spec
*spec
= NULL
;
155 bool no_add_attrs
= 0;
156 tree fn_ptr_tmp
= NULL_TREE
;
159 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
163 for (j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
165 if (is_attribute_p (attribute_tables
[i
][j
].name
, name
))
167 spec
= &attribute_tables
[i
][j
];
177 warning (OPT_Wattributes
, "%qs attribute directive ignored",
178 IDENTIFIER_POINTER (name
));
181 else if (list_length (args
) < spec
->min_length
182 || (spec
->max_length
>= 0
183 && list_length (args
) > spec
->max_length
))
185 error ("wrong number of arguments specified for %qs attribute",
186 IDENTIFIER_POINTER (name
));
190 if (spec
->decl_required
&& !DECL_P (*anode
))
192 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
193 | (int) ATTR_FLAG_FUNCTION_NEXT
194 | (int) ATTR_FLAG_ARRAY_NEXT
))
196 /* Pass on this attribute to be tried again. */
197 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
202 warning (OPT_Wattributes
, "%qs attribute does not apply to types",
203 IDENTIFIER_POINTER (name
));
208 /* If we require a type, but were passed a decl, set up to make a
209 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
210 would have applied if we'd been passed a type, but we cannot modify
211 the decl's type in place here. */
212 if (spec
->type_required
&& DECL_P (*anode
))
214 anode
= &TREE_TYPE (*anode
);
215 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
218 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
219 && TREE_CODE (*anode
) != METHOD_TYPE
)
221 if (TREE_CODE (*anode
) == POINTER_TYPE
222 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
223 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
225 /* OK, this is a bit convoluted. We can't just make a copy
226 of the pointer type and modify its TREE_TYPE, because if
227 we change the attributes of the target type the pointer
228 type needs to have a different TYPE_MAIN_VARIANT. So we
229 pull out the target type now, frob it as appropriate, and
230 rebuild the pointer type later.
232 This would all be simpler if attributes were part of the
233 declarator, grumble grumble. */
234 fn_ptr_tmp
= TREE_TYPE (*anode
);
236 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
238 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
240 /* Pass on this attribute to be tried again. */
241 returned_attrs
= tree_cons (name
, args
, returned_attrs
);
245 if (TREE_CODE (*anode
) != FUNCTION_TYPE
246 && TREE_CODE (*anode
) != METHOD_TYPE
)
248 warning (OPT_Wattributes
,
249 "%qs attribute only applies to function types",
250 IDENTIFIER_POINTER (name
));
256 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
257 && TYPE_SIZE (*anode
) != NULL_TREE
)
259 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
263 if (spec
->handler
!= NULL
)
264 returned_attrs
= chainon ((*spec
->handler
) (anode
, name
, args
,
265 flags
, &no_add_attrs
),
268 /* Layout the decl in case anything changed. */
269 if (spec
->type_required
&& DECL_P (*node
)
270 && (TREE_CODE (*node
) == VAR_DECL
271 || TREE_CODE (*node
) == PARM_DECL
272 || TREE_CODE (*node
) == RESULT_DECL
))
273 relayout_decl (*node
);
281 old_attrs
= DECL_ATTRIBUTES (*anode
);
283 old_attrs
= TYPE_ATTRIBUTES (*anode
);
285 for (a
= lookup_attribute (spec
->name
, old_attrs
);
287 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
289 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
295 /* This attribute isn't already in the list. */
297 DECL_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
298 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
300 TYPE_ATTRIBUTES (*anode
) = tree_cons (name
, args
, old_attrs
);
301 /* If this is the main variant, also push the attributes
302 out to the other variants. */
303 if (*anode
== TYPE_MAIN_VARIANT (*anode
))
306 for (variant
= *anode
; variant
;
307 variant
= TYPE_NEXT_VARIANT (variant
))
309 if (TYPE_ATTRIBUTES (variant
) == old_attrs
)
310 TYPE_ATTRIBUTES (variant
)
311 = TYPE_ATTRIBUTES (*anode
);
312 else if (!lookup_attribute
313 (spec
->name
, TYPE_ATTRIBUTES (variant
)))
314 TYPE_ATTRIBUTES (variant
) = tree_cons
315 (name
, args
, TYPE_ATTRIBUTES (variant
));
320 *anode
= build_type_attribute_variant (*anode
,
321 tree_cons (name
, args
,
328 /* Rebuild the function pointer type and put it in the
329 appropriate place. */
330 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
332 TREE_TYPE (*node
) = fn_ptr_tmp
;
335 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
341 return returned_attrs
;