S/390: Add static OSC breaker if necessary.
[official-gcc.git] / gcc / c-family / c-attribs.c
blob925f1b20c5096bb09724aa82df144c8a122ec49a
1 /* C-family attributes handling.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "tree.h"
26 #include "memmodel.h"
27 #include "c-common.h"
28 #include "gimple-expr.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "intl.h"
34 #include "stor-layout.h"
35 #include "calls.h"
36 #include "attribs.h"
37 #include "varasm.h"
38 #include "trans-mem.h"
39 #include "c-objc.h"
40 #include "common/common-target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
43 #include "toplev.h"
44 #include "tree-iterator.h"
45 #include "opts.h"
46 #include "gimplify.h"
48 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
55 int, bool *);
56 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
57 int, bool *);
58 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
59 bool *);
60 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
61 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
62 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
63 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
64 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
65 static tree handle_always_inline_attribute (tree *, tree, tree, int,
66 bool *);
67 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
73 bool *);
74 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
75 bool *);
76 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_transparent_union_attribute (tree *, tree, tree,
78 int, bool *);
79 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
80 int, bool *);
81 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
82 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
83 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
86 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
87 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
88 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
89 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
92 static tree handle_visibility_attribute (tree *, tree, tree, int,
93 bool *);
94 static tree handle_tls_model_attribute (tree *, tree, tree, int,
95 bool *);
96 static tree handle_no_instrument_function_attribute (tree *, tree,
97 tree, int, bool *);
98 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
99 tree, int, bool *);
100 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
101 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
102 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
103 bool *);
104 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
105 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
106 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
107 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
108 static tree handle_deprecated_attribute (tree *, tree, tree, int,
109 bool *);
110 static tree handle_vector_size_attribute (tree *, tree, tree, int,
111 bool *);
112 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
113 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
116 bool *);
117 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
118 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
119 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
120 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
121 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
125 static tree ignore_attribute (tree *, tree, tree, int, bool *);
126 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
127 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
131 bool *);
132 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
134 bool *);
135 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
136 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
138 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
139 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
141 /* Table of machine-independent attributes common to all C-like languages.
143 All attributes referencing arguments should be additionally processed
144 in chkp_copy_function_type_adding_bounds for correct instrumentation
145 by Pointer Bounds Checker.
146 Current list of processed common attributes: nonnull. */
147 const struct attribute_spec c_common_attribute_table[] =
149 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
150 affects_type_identity } */
151 { "packed", 0, 0, false, false, false,
152 handle_packed_attribute , false},
153 { "nocommon", 0, 0, true, false, false,
154 handle_nocommon_attribute, false},
155 { "common", 0, 0, true, false, false,
156 handle_common_attribute, false },
157 /* FIXME: logically, noreturn attributes should be listed as
158 "false, true, true" and apply to function types. But implementing this
159 would require all the places in the compiler that use TREE_THIS_VOLATILE
160 on a decl to identify non-returning functions to be located and fixed
161 to check the function type instead. */
162 { "noreturn", 0, 0, true, false, false,
163 handle_noreturn_attribute, false },
164 { "volatile", 0, 0, true, false, false,
165 handle_noreturn_attribute, false },
166 { "stack_protect", 0, 0, true, false, false,
167 handle_stack_protect_attribute, false },
168 { "noinline", 0, 0, true, false, false,
169 handle_noinline_attribute, false },
170 { "noclone", 0, 0, true, false, false,
171 handle_noclone_attribute, false },
172 { "no_icf", 0, 0, true, false, false,
173 handle_noicf_attribute, false },
174 { "leaf", 0, 0, true, false, false,
175 handle_leaf_attribute, false },
176 { "always_inline", 0, 0, true, false, false,
177 handle_always_inline_attribute, false },
178 { "gnu_inline", 0, 0, true, false, false,
179 handle_gnu_inline_attribute, false },
180 { "artificial", 0, 0, true, false, false,
181 handle_artificial_attribute, false },
182 { "flatten", 0, 0, true, false, false,
183 handle_flatten_attribute, false },
184 { "used", 0, 0, true, false, false,
185 handle_used_attribute, false },
186 { "unused", 0, 0, false, false, false,
187 handle_unused_attribute, false },
188 { "externally_visible", 0, 0, true, false, false,
189 handle_externally_visible_attribute, false },
190 { "no_reorder", 0, 0, true, false, false,
191 handle_no_reorder_attribute, false },
192 /* The same comments as for noreturn attributes apply to const ones. */
193 { "const", 0, 0, true, false, false,
194 handle_const_attribute, false },
195 { "scalar_storage_order", 1, 1, false, false, false,
196 handle_scalar_storage_order_attribute, false },
197 { "transparent_union", 0, 0, false, false, false,
198 handle_transparent_union_attribute, false },
199 { "constructor", 0, 1, true, false, false,
200 handle_constructor_attribute, false },
201 { "destructor", 0, 1, true, false, false,
202 handle_destructor_attribute, false },
203 { "mode", 1, 1, false, true, false,
204 handle_mode_attribute, false },
205 { "section", 1, 1, true, false, false,
206 handle_section_attribute, false },
207 { "aligned", 0, 1, false, false, false,
208 handle_aligned_attribute, false },
209 { "weak", 0, 0, true, false, false,
210 handle_weak_attribute, false },
211 { "noplt", 0, 0, true, false, false,
212 handle_noplt_attribute, false },
213 { "ifunc", 1, 1, true, false, false,
214 handle_ifunc_attribute, false },
215 { "alias", 1, 1, true, false, false,
216 handle_alias_attribute, false },
217 { "weakref", 0, 1, true, false, false,
218 handle_weakref_attribute, false },
219 { "no_instrument_function", 0, 0, true, false, false,
220 handle_no_instrument_function_attribute,
221 false },
222 { "no_profile_instrument_function", 0, 0, true, false, false,
223 handle_no_profile_instrument_function_attribute,
224 false },
225 { "malloc", 0, 0, true, false, false,
226 handle_malloc_attribute, false },
227 { "returns_twice", 0, 0, true, false, false,
228 handle_returns_twice_attribute, false },
229 { "no_stack_limit", 0, 0, true, false, false,
230 handle_no_limit_stack_attribute, false },
231 { "pure", 0, 0, true, false, false,
232 handle_pure_attribute, false },
233 { "transaction_callable", 0, 0, false, true, false,
234 handle_tm_attribute, false },
235 { "transaction_unsafe", 0, 0, false, true, false,
236 handle_tm_attribute, true },
237 { "transaction_safe", 0, 0, false, true, false,
238 handle_tm_attribute, true },
239 { "transaction_safe_dynamic", 0, 0, true, false, false,
240 handle_tm_attribute, false },
241 { "transaction_may_cancel_outer", 0, 0, false, true, false,
242 handle_tm_attribute, false },
243 /* ??? These two attributes didn't make the transition from the
244 Intel language document to the multi-vendor language document. */
245 { "transaction_pure", 0, 0, false, true, false,
246 handle_tm_attribute, false },
247 { "transaction_wrap", 1, 1, true, false, false,
248 handle_tm_wrap_attribute, false },
249 /* For internal use (marking of builtins) only. The name contains space
250 to prevent its usage in source code. */
251 { "no vops", 0, 0, true, false, false,
252 handle_novops_attribute, false },
253 { "deprecated", 0, 1, false, false, false,
254 handle_deprecated_attribute, false },
255 { "vector_size", 1, 1, false, true, false,
256 handle_vector_size_attribute, true },
257 { "visibility", 1, 1, false, false, false,
258 handle_visibility_attribute, false },
259 { "tls_model", 1, 1, true, false, false,
260 handle_tls_model_attribute, false },
261 { "nonnull", 0, -1, false, true, true,
262 handle_nonnull_attribute, false },
263 { "nothrow", 0, 0, true, false, false,
264 handle_nothrow_attribute, false },
265 { "may_alias", 0, 0, false, true, false, NULL, false },
266 { "cleanup", 1, 1, true, false, false,
267 handle_cleanup_attribute, false },
268 { "warn_unused_result", 0, 0, false, true, true,
269 handle_warn_unused_result_attribute, false },
270 { "sentinel", 0, 1, false, true, true,
271 handle_sentinel_attribute, false },
272 /* For internal use (marking of builtins) only. The name contains space
273 to prevent its usage in source code. */
274 { "type generic", 0, 0, false, true, true,
275 handle_type_generic_attribute, false },
276 { "alloc_size", 1, 2, false, true, true,
277 handle_alloc_size_attribute, false },
278 { "cold", 0, 0, true, false, false,
279 handle_cold_attribute, false },
280 { "hot", 0, 0, true, false, false,
281 handle_hot_attribute, false },
282 { "no_address_safety_analysis",
283 0, 0, true, false, false,
284 handle_no_address_safety_analysis_attribute,
285 false },
286 { "no_sanitize_address", 0, 0, true, false, false,
287 handle_no_sanitize_address_attribute,
288 false },
289 { "no_sanitize_thread", 0, 0, true, false, false,
290 handle_no_sanitize_address_attribute,
291 false },
292 { "no_sanitize_undefined", 0, 0, true, false, false,
293 handle_no_sanitize_undefined_attribute,
294 false },
295 { "warning", 1, 1, true, false, false,
296 handle_error_attribute, false },
297 { "error", 1, 1, true, false, false,
298 handle_error_attribute, false },
299 { "target", 1, -1, true, false, false,
300 handle_target_attribute, false },
301 { "target_clones", 1, -1, true, false, false,
302 handle_target_clones_attribute, false },
303 { "optimize", 1, -1, true, false, false,
304 handle_optimize_attribute, false },
305 /* For internal use only. The leading '*' both prevents its usage in
306 source code and signals that it may be overridden by machine tables. */
307 { "*tm regparm", 0, 0, false, true, true,
308 ignore_attribute, false },
309 { "no_split_stack", 0, 0, true, false, false,
310 handle_no_split_stack_attribute, false },
311 /* For internal use (marking of builtins and runtime functions) only.
312 The name contains space to prevent its usage in source code. */
313 { "fn spec", 1, 1, false, true, true,
314 handle_fnspec_attribute, false },
315 { "warn_unused", 0, 0, false, false, false,
316 handle_warn_unused_attribute, false },
317 { "returns_nonnull", 0, 0, false, true, true,
318 handle_returns_nonnull_attribute, false },
319 { "omp declare simd", 0, -1, true, false, false,
320 handle_omp_declare_simd_attribute, false },
321 { "cilk simd function", 0, -1, true, false, false,
322 handle_omp_declare_simd_attribute, false },
323 { "simd", 0, 1, true, false, false,
324 handle_simd_attribute, false },
325 { "omp declare target", 0, 0, true, false, false,
326 handle_omp_declare_target_attribute, false },
327 { "omp declare target link", 0, 0, true, false, false,
328 handle_omp_declare_target_attribute, false },
329 { "alloc_align", 1, 1, false, true, true,
330 handle_alloc_align_attribute, false },
331 { "assume_aligned", 1, 2, false, true, true,
332 handle_assume_aligned_attribute, false },
333 { "designated_init", 0, 0, false, true, false,
334 handle_designated_init_attribute, false },
335 { "bnd_variable_size", 0, 0, true, false, false,
336 handle_bnd_variable_size_attribute, false },
337 { "bnd_legacy", 0, 0, true, false, false,
338 handle_bnd_legacy, false },
339 { "bnd_instrument", 0, 0, true, false, false,
340 handle_bnd_instrument, false },
341 { "fallthrough", 0, 0, false, false, false,
342 handle_fallthrough_attribute, false },
343 { NULL, 0, 0, false, false, false, NULL, false }
346 /* Give the specifications for the format attributes, used by C and all
347 descendants.
349 All attributes referencing arguments should be additionally processed
350 in chkp_copy_function_type_adding_bounds for correct instrumentation
351 by Pointer Bounds Checker.
352 Current list of processed format attributes: format, format_arg. */
353 const struct attribute_spec c_common_format_attribute_table[] =
355 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
356 affects_type_identity } */
357 { "format", 3, 3, false, true, true,
358 handle_format_attribute, false },
359 { "format_arg", 1, 1, false, true, true,
360 handle_format_arg_attribute, false },
361 { NULL, 0, 0, false, false, false, NULL, false }
364 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
365 identifier as an argument, so the front end shouldn't look it up. */
367 bool
368 attribute_takes_identifier_p (const_tree attr_id)
370 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
371 if (spec == NULL)
372 /* Unknown attribute that we'll end up ignoring, return true so we
373 don't complain about an identifier argument. */
374 return true;
375 else if (!strcmp ("mode", spec->name)
376 || !strcmp ("format", spec->name)
377 || !strcmp ("cleanup", spec->name))
378 return true;
379 else
380 return targetm.attribute_takes_identifier_p (attr_id);
383 /* Attribute handlers common to C front ends. */
385 /* Handle a "packed" attribute; arguments as in
386 struct attribute_spec.handler. */
388 static tree
389 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
390 int flags, bool *no_add_attrs)
392 if (TYPE_P (*node))
394 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
395 *node = build_variant_type_copy (*node);
396 TYPE_PACKED (*node) = 1;
398 else if (TREE_CODE (*node) == FIELD_DECL)
400 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
401 /* Still pack bitfields. */
402 && ! DECL_INITIAL (*node))
403 warning (OPT_Wattributes,
404 "%qE attribute ignored for field of type %qT",
405 name, TREE_TYPE (*node));
406 else
407 DECL_PACKED (*node) = 1;
409 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
410 used for DECL_REGISTER. It wouldn't mean anything anyway.
411 We can't set DECL_PACKED on the type of a TYPE_DECL, because
412 that changes what the typedef is typing. */
413 else
415 warning (OPT_Wattributes, "%qE attribute ignored", name);
416 *no_add_attrs = true;
419 return NULL_TREE;
422 /* Handle a "nocommon" attribute; arguments as in
423 struct attribute_spec.handler. */
425 static tree
426 handle_nocommon_attribute (tree *node, tree name,
427 tree ARG_UNUSED (args),
428 int ARG_UNUSED (flags), bool *no_add_attrs)
430 if (VAR_P (*node))
431 DECL_COMMON (*node) = 0;
432 else
434 warning (OPT_Wattributes, "%qE attribute ignored", name);
435 *no_add_attrs = true;
438 return NULL_TREE;
441 /* Handle a "common" attribute; arguments as in
442 struct attribute_spec.handler. */
444 static tree
445 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
446 int ARG_UNUSED (flags), bool *no_add_attrs)
448 if (VAR_P (*node))
449 DECL_COMMON (*node) = 1;
450 else
452 warning (OPT_Wattributes, "%qE attribute ignored", name);
453 *no_add_attrs = true;
456 return NULL_TREE;
459 /* Handle a "noreturn" attribute; arguments as in
460 struct attribute_spec.handler. */
462 static tree
463 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
464 int ARG_UNUSED (flags), bool *no_add_attrs)
466 tree type = TREE_TYPE (*node);
468 /* See FIXME comment in c_common_attribute_table. */
469 if (TREE_CODE (*node) == FUNCTION_DECL
470 || objc_method_decl (TREE_CODE (*node)))
471 TREE_THIS_VOLATILE (*node) = 1;
472 else if (TREE_CODE (type) == POINTER_TYPE
473 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
474 TREE_TYPE (*node)
475 = (build_qualified_type
476 (build_pointer_type
477 (build_type_variant (TREE_TYPE (type),
478 TYPE_READONLY (TREE_TYPE (type)), 1)),
479 TYPE_QUALS (type)));
480 else
482 warning (OPT_Wattributes, "%qE attribute ignored", name);
483 *no_add_attrs = true;
486 return NULL_TREE;
489 /* Handle a "hot" and attribute; arguments as in
490 struct attribute_spec.handler. */
492 static tree
493 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
494 int ARG_UNUSED (flags), bool *no_add_attrs)
496 if (TREE_CODE (*node) == FUNCTION_DECL
497 || TREE_CODE (*node) == LABEL_DECL)
499 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
501 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
502 "with attribute %qs", name, "cold");
503 *no_add_attrs = true;
505 /* Most of the rest of the hot processing is done later with
506 lookup_attribute. */
508 else
510 warning (OPT_Wattributes, "%qE attribute ignored", name);
511 *no_add_attrs = true;
514 return NULL_TREE;
517 /* Handle a "cold" and attribute; arguments as in
518 struct attribute_spec.handler. */
520 static tree
521 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
522 int ARG_UNUSED (flags), bool *no_add_attrs)
524 if (TREE_CODE (*node) == FUNCTION_DECL
525 || TREE_CODE (*node) == LABEL_DECL)
527 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
529 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
530 "with attribute %qs", name, "hot");
531 *no_add_attrs = true;
533 /* Most of the rest of the cold processing is done later with
534 lookup_attribute. */
536 else
538 warning (OPT_Wattributes, "%qE attribute ignored", name);
539 *no_add_attrs = true;
542 return NULL_TREE;
545 /* Handle a "no_sanitize_address" attribute; arguments as in
546 struct attribute_spec.handler. */
548 static tree
549 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
550 bool *no_add_attrs)
552 if (TREE_CODE (*node) != FUNCTION_DECL)
554 warning (OPT_Wattributes, "%qE attribute ignored", name);
555 *no_add_attrs = true;
558 return NULL_TREE;
561 /* Handle a "no_address_safety_analysis" attribute; arguments as in
562 struct attribute_spec.handler. */
564 static tree
565 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
566 bool *no_add_attrs)
568 if (TREE_CODE (*node) != FUNCTION_DECL)
569 warning (OPT_Wattributes, "%qE attribute ignored", name);
570 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
571 DECL_ATTRIBUTES (*node)
572 = tree_cons (get_identifier ("no_sanitize_address"),
573 NULL_TREE, DECL_ATTRIBUTES (*node));
574 *no_add_attrs = true;
575 return NULL_TREE;
578 /* Handle a "no_sanitize_undefined" attribute; arguments as in
579 struct attribute_spec.handler. */
581 static tree
582 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
583 bool *no_add_attrs)
585 if (TREE_CODE (*node) != FUNCTION_DECL)
587 warning (OPT_Wattributes, "%qE attribute ignored", name);
588 *no_add_attrs = true;
591 return NULL_TREE;
594 /* Handle a "stack_protect" attribute; arguments as in
595 struct attribute_spec.handler. */
597 static tree
598 handle_stack_protect_attribute (tree *node, tree name, tree, int,
599 bool *no_add_attrs)
601 if (TREE_CODE (*node) != FUNCTION_DECL)
603 warning (OPT_Wattributes, "%qE attribute ignored", name);
604 *no_add_attrs = true;
606 else
607 DECL_ATTRIBUTES (*node)
608 = tree_cons (get_identifier ("stack_protect"),
609 NULL_TREE, DECL_ATTRIBUTES (*node));
611 return NULL_TREE;
614 /* Handle a "noinline" attribute; arguments as in
615 struct attribute_spec.handler. */
617 static tree
618 handle_noinline_attribute (tree *node, tree name,
619 tree ARG_UNUSED (args),
620 int ARG_UNUSED (flags), bool *no_add_attrs)
622 if (TREE_CODE (*node) == FUNCTION_DECL)
624 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
626 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
627 "with attribute %qs", name, "always_inline");
628 *no_add_attrs = true;
630 else
631 DECL_UNINLINABLE (*node) = 1;
633 else
635 warning (OPT_Wattributes, "%qE attribute ignored", name);
636 *no_add_attrs = true;
639 return NULL_TREE;
642 /* Handle a "noclone" attribute; arguments as in
643 struct attribute_spec.handler. */
645 static tree
646 handle_noclone_attribute (tree *node, tree name,
647 tree ARG_UNUSED (args),
648 int ARG_UNUSED (flags), bool *no_add_attrs)
650 if (TREE_CODE (*node) != FUNCTION_DECL)
652 warning (OPT_Wattributes, "%qE attribute ignored", name);
653 *no_add_attrs = true;
656 return NULL_TREE;
659 /* Handle a "no_icf" attribute; arguments as in
660 struct attribute_spec.handler. */
662 static tree
663 handle_noicf_attribute (tree *node, tree name,
664 tree ARG_UNUSED (args),
665 int ARG_UNUSED (flags), bool *no_add_attrs)
667 if (TREE_CODE (*node) != FUNCTION_DECL)
669 warning (OPT_Wattributes, "%qE attribute ignored", name);
670 *no_add_attrs = true;
673 return NULL_TREE;
677 /* Handle a "always_inline" attribute; arguments as in
678 struct attribute_spec.handler. */
680 static tree
681 handle_always_inline_attribute (tree *node, tree name,
682 tree ARG_UNUSED (args),
683 int ARG_UNUSED (flags),
684 bool *no_add_attrs)
686 if (TREE_CODE (*node) == FUNCTION_DECL)
688 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
690 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
691 "with %qs attribute", name, "noinline");
692 *no_add_attrs = true;
694 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
696 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
697 "with %qs attribute", name, "target_clones");
698 *no_add_attrs = true;
700 else
701 /* Set the attribute and mark it for disregarding inline
702 limits. */
703 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
705 else
707 warning (OPT_Wattributes, "%qE attribute ignored", name);
708 *no_add_attrs = true;
711 return NULL_TREE;
714 /* Handle a "gnu_inline" attribute; arguments as in
715 struct attribute_spec.handler. */
717 static tree
718 handle_gnu_inline_attribute (tree *node, tree name,
719 tree ARG_UNUSED (args),
720 int ARG_UNUSED (flags),
721 bool *no_add_attrs)
723 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
725 /* Do nothing else, just set the attribute. We'll get at
726 it later with lookup_attribute. */
728 else
730 warning (OPT_Wattributes, "%qE attribute ignored", name);
731 *no_add_attrs = true;
734 return NULL_TREE;
737 /* Handle a "leaf" attribute; arguments as in
738 struct attribute_spec.handler. */
740 static tree
741 handle_leaf_attribute (tree *node, tree name,
742 tree ARG_UNUSED (args),
743 int ARG_UNUSED (flags), bool *no_add_attrs)
745 if (TREE_CODE (*node) != FUNCTION_DECL)
747 warning (OPT_Wattributes, "%qE attribute ignored", name);
748 *no_add_attrs = true;
750 if (!TREE_PUBLIC (*node))
752 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
753 "functions", name);
754 *no_add_attrs = true;
757 return NULL_TREE;
760 /* Handle an "artificial" attribute; arguments as in
761 struct attribute_spec.handler. */
763 static tree
764 handle_artificial_attribute (tree *node, tree name,
765 tree ARG_UNUSED (args),
766 int ARG_UNUSED (flags),
767 bool *no_add_attrs)
769 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
771 /* Do nothing else, just set the attribute. We'll get at
772 it later with lookup_attribute. */
774 else
776 warning (OPT_Wattributes, "%qE attribute ignored", name);
777 *no_add_attrs = true;
780 return NULL_TREE;
783 /* Handle a "flatten" attribute; arguments as in
784 struct attribute_spec.handler. */
786 static tree
787 handle_flatten_attribute (tree *node, tree name,
788 tree args ATTRIBUTE_UNUSED,
789 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
791 if (TREE_CODE (*node) == FUNCTION_DECL)
792 /* Do nothing else, just set the attribute. We'll get at
793 it later with lookup_attribute. */
795 else
797 warning (OPT_Wattributes, "%qE attribute ignored", name);
798 *no_add_attrs = true;
801 return NULL_TREE;
804 /* Handle a "warning" or "error" attribute; arguments as in
805 struct attribute_spec.handler. */
807 static tree
808 handle_error_attribute (tree *node, tree name, tree args,
809 int ARG_UNUSED (flags), bool *no_add_attrs)
811 if (TREE_CODE (*node) == FUNCTION_DECL
812 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
813 /* Do nothing else, just set the attribute. We'll get at
814 it later with lookup_attribute. */
816 else
818 warning (OPT_Wattributes, "%qE attribute ignored", name);
819 *no_add_attrs = true;
822 return NULL_TREE;
825 /* Handle a "used" attribute; arguments as in
826 struct attribute_spec.handler. */
828 static tree
829 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
830 int ARG_UNUSED (flags), bool *no_add_attrs)
832 tree node = *pnode;
834 if (TREE_CODE (node) == FUNCTION_DECL
835 || (VAR_P (node) && TREE_STATIC (node))
836 || (TREE_CODE (node) == TYPE_DECL))
838 TREE_USED (node) = 1;
839 DECL_PRESERVE_P (node) = 1;
840 if (VAR_P (node))
841 DECL_READ_P (node) = 1;
843 else
845 warning (OPT_Wattributes, "%qE attribute ignored", name);
846 *no_add_attrs = true;
849 return NULL_TREE;
852 /* Handle a "unused" attribute; arguments as in
853 struct attribute_spec.handler. */
855 tree
856 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
857 int flags, bool *no_add_attrs)
859 if (DECL_P (*node))
861 tree decl = *node;
863 if (TREE_CODE (decl) == PARM_DECL
864 || VAR_OR_FUNCTION_DECL_P (decl)
865 || TREE_CODE (decl) == LABEL_DECL
866 || TREE_CODE (decl) == CONST_DECL
867 || TREE_CODE (decl) == TYPE_DECL)
869 TREE_USED (decl) = 1;
870 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
871 DECL_READ_P (decl) = 1;
873 else
875 warning (OPT_Wattributes, "%qE attribute ignored", name);
876 *no_add_attrs = true;
879 else
881 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
882 *node = build_variant_type_copy (*node);
883 TREE_USED (*node) = 1;
886 return NULL_TREE;
889 /* Handle a "externally_visible" attribute; arguments as in
890 struct attribute_spec.handler. */
892 static tree
893 handle_externally_visible_attribute (tree *pnode, tree name,
894 tree ARG_UNUSED (args),
895 int ARG_UNUSED (flags),
896 bool *no_add_attrs)
898 tree node = *pnode;
900 if (VAR_OR_FUNCTION_DECL_P (node))
902 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
903 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
905 warning (OPT_Wattributes,
906 "%qE attribute have effect only on public objects", name);
907 *no_add_attrs = true;
910 else
912 warning (OPT_Wattributes, "%qE attribute ignored", name);
913 *no_add_attrs = true;
916 return NULL_TREE;
919 /* Handle the "no_reorder" attribute. Arguments as in
920 struct attribute_spec.handler. */
922 static tree
923 handle_no_reorder_attribute (tree *pnode,
924 tree name,
925 tree,
926 int,
927 bool *no_add_attrs)
929 tree node = *pnode;
931 if (!VAR_OR_FUNCTION_DECL_P (node)
932 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
934 warning (OPT_Wattributes,
935 "%qE attribute only affects top level objects",
936 name);
937 *no_add_attrs = true;
940 return NULL_TREE;
943 /* Handle a "const" attribute; arguments as in
944 struct attribute_spec.handler. */
946 static tree
947 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
948 int ARG_UNUSED (flags), bool *no_add_attrs)
950 tree type = TREE_TYPE (*node);
952 /* See FIXME comment on noreturn in c_common_attribute_table. */
953 if (TREE_CODE (*node) == FUNCTION_DECL)
954 TREE_READONLY (*node) = 1;
955 else if (TREE_CODE (type) == POINTER_TYPE
956 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
957 TREE_TYPE (*node)
958 = (build_qualified_type
959 (build_pointer_type
960 (build_type_variant (TREE_TYPE (type), 1,
961 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
962 TYPE_QUALS (type)));
963 else
965 warning (OPT_Wattributes, "%qE attribute ignored", name);
966 *no_add_attrs = true;
969 return NULL_TREE;
972 /* Handle a "scalar_storage_order" attribute; arguments as in
973 struct attribute_spec.handler. */
975 static tree
976 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
977 int flags, bool *no_add_attrs)
979 tree id = TREE_VALUE (args);
980 tree type;
982 if (TREE_CODE (*node) == TYPE_DECL
983 && ! (flags & ATTR_FLAG_CXX11))
984 node = &TREE_TYPE (*node);
985 type = *node;
987 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
989 error ("scalar_storage_order is not supported because endianness "
990 "is not uniform");
991 return NULL_TREE;
994 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
996 bool reverse = false;
998 if (TREE_CODE (id) == STRING_CST
999 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1000 reverse = !BYTES_BIG_ENDIAN;
1001 else if (TREE_CODE (id) == STRING_CST
1002 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1003 reverse = BYTES_BIG_ENDIAN;
1004 else
1006 error ("scalar_storage_order argument must be one of \"big-endian\""
1007 " or \"little-endian\"");
1008 return NULL_TREE;
1011 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1013 if (reverse)
1014 /* A type variant isn't good enough, since we don't want a cast
1015 to such a type to be removed as a no-op. */
1016 *node = type = build_duplicate_type (type);
1019 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1020 return NULL_TREE;
1023 warning (OPT_Wattributes, "%qE attribute ignored", name);
1024 *no_add_attrs = true;
1025 return NULL_TREE;
1028 /* Handle a "transparent_union" attribute; arguments as in
1029 struct attribute_spec.handler. */
1031 static tree
1032 handle_transparent_union_attribute (tree *node, tree name,
1033 tree ARG_UNUSED (args), int flags,
1034 bool *no_add_attrs)
1036 tree type;
1038 *no_add_attrs = true;
1040 if (TREE_CODE (*node) == TYPE_DECL
1041 && ! (flags & ATTR_FLAG_CXX11))
1042 node = &TREE_TYPE (*node);
1043 type = *node;
1045 if (TREE_CODE (type) == UNION_TYPE)
1047 /* Make sure that the first field will work for a transparent union.
1048 If the type isn't complete yet, leave the check to the code in
1049 finish_struct. */
1050 if (TYPE_SIZE (type))
1052 tree first = first_field (type);
1053 if (first == NULL_TREE
1054 || DECL_ARTIFICIAL (first)
1055 || TYPE_MODE (type) != DECL_MODE (first))
1056 goto ignored;
1059 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1061 /* If the type isn't complete yet, setting the flag
1062 on a variant wouldn't ever be checked. */
1063 if (!TYPE_SIZE (type))
1064 goto ignored;
1066 /* build_duplicate_type doesn't work for C++. */
1067 if (c_dialect_cxx ())
1068 goto ignored;
1070 /* A type variant isn't good enough, since we don't want a cast
1071 to such a type to be removed as a no-op. */
1072 *node = type = build_duplicate_type (type);
1075 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1076 TYPE_TRANSPARENT_AGGR (t) = 1;
1077 return NULL_TREE;
1080 ignored:
1081 warning (OPT_Wattributes, "%qE attribute ignored", name);
1082 return NULL_TREE;
1085 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1086 get the requested priority for a constructor or destructor,
1087 possibly issuing diagnostics for invalid or reserved
1088 priorities. */
1090 static priority_type
1091 get_priority (tree args, bool is_destructor)
1093 HOST_WIDE_INT pri;
1094 tree arg;
1096 if (!args)
1097 return DEFAULT_INIT_PRIORITY;
1099 if (!SUPPORTS_INIT_PRIORITY)
1101 if (is_destructor)
1102 error ("destructor priorities are not supported");
1103 else
1104 error ("constructor priorities are not supported");
1105 return DEFAULT_INIT_PRIORITY;
1108 arg = TREE_VALUE (args);
1109 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1110 goto invalid;
1111 if (arg == error_mark_node)
1112 return DEFAULT_INIT_PRIORITY;
1113 arg = default_conversion (arg);
1114 if (!tree_fits_shwi_p (arg)
1115 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1116 goto invalid;
1118 pri = tree_to_shwi (arg);
1119 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1120 goto invalid;
1122 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1124 if (is_destructor)
1125 warning (0,
1126 "destructor priorities from 0 to %d are reserved "
1127 "for the implementation",
1128 MAX_RESERVED_INIT_PRIORITY);
1129 else
1130 warning (0,
1131 "constructor priorities from 0 to %d are reserved "
1132 "for the implementation",
1133 MAX_RESERVED_INIT_PRIORITY);
1135 return pri;
1137 invalid:
1138 if (is_destructor)
1139 error ("destructor priorities must be integers from 0 to %d inclusive",
1140 MAX_INIT_PRIORITY);
1141 else
1142 error ("constructor priorities must be integers from 0 to %d inclusive",
1143 MAX_INIT_PRIORITY);
1144 return DEFAULT_INIT_PRIORITY;
1147 /* Handle a "constructor" attribute; arguments as in
1148 struct attribute_spec.handler. */
1150 static tree
1151 handle_constructor_attribute (tree *node, tree name, tree args,
1152 int ARG_UNUSED (flags),
1153 bool *no_add_attrs)
1155 tree decl = *node;
1156 tree type = TREE_TYPE (decl);
1158 if (TREE_CODE (decl) == FUNCTION_DECL
1159 && TREE_CODE (type) == FUNCTION_TYPE
1160 && decl_function_context (decl) == 0)
1162 priority_type priority;
1163 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1164 priority = get_priority (args, /*is_destructor=*/false);
1165 SET_DECL_INIT_PRIORITY (decl, priority);
1166 TREE_USED (decl) = 1;
1168 else
1170 warning (OPT_Wattributes, "%qE attribute ignored", name);
1171 *no_add_attrs = true;
1174 return NULL_TREE;
1177 /* Handle a "destructor" attribute; arguments as in
1178 struct attribute_spec.handler. */
1180 static tree
1181 handle_destructor_attribute (tree *node, tree name, tree args,
1182 int ARG_UNUSED (flags),
1183 bool *no_add_attrs)
1185 tree decl = *node;
1186 tree type = TREE_TYPE (decl);
1188 if (TREE_CODE (decl) == FUNCTION_DECL
1189 && TREE_CODE (type) == FUNCTION_TYPE
1190 && decl_function_context (decl) == 0)
1192 priority_type priority;
1193 DECL_STATIC_DESTRUCTOR (decl) = 1;
1194 priority = get_priority (args, /*is_destructor=*/true);
1195 SET_DECL_FINI_PRIORITY (decl, priority);
1196 TREE_USED (decl) = 1;
1198 else
1200 warning (OPT_Wattributes, "%qE attribute ignored", name);
1201 *no_add_attrs = true;
1204 return NULL_TREE;
1207 /* Nonzero if the mode is a valid vector mode for this architecture.
1208 This returns nonzero even if there is no hardware support for the
1209 vector mode, but we can emulate with narrower modes. */
1211 static bool
1212 vector_mode_valid_p (machine_mode mode)
1214 enum mode_class mclass = GET_MODE_CLASS (mode);
1215 machine_mode innermode;
1217 /* Doh! What's going on? */
1218 if (mclass != MODE_VECTOR_INT
1219 && mclass != MODE_VECTOR_FLOAT
1220 && mclass != MODE_VECTOR_FRACT
1221 && mclass != MODE_VECTOR_UFRACT
1222 && mclass != MODE_VECTOR_ACCUM
1223 && mclass != MODE_VECTOR_UACCUM)
1224 return false;
1226 /* Hardware support. Woo hoo! */
1227 if (targetm.vector_mode_supported_p (mode))
1228 return true;
1230 innermode = GET_MODE_INNER (mode);
1232 /* We should probably return 1 if requesting V4DI and we have no DI,
1233 but we have V2DI, but this is probably very unlikely. */
1235 /* If we have support for the inner mode, we can safely emulate it.
1236 We may not have V2DI, but me can emulate with a pair of DIs. */
1237 return targetm.scalar_mode_supported_p (innermode);
1241 /* Handle a "mode" attribute; arguments as in
1242 struct attribute_spec.handler. */
1244 static tree
1245 handle_mode_attribute (tree *node, tree name, tree args,
1246 int ARG_UNUSED (flags), bool *no_add_attrs)
1248 tree type = *node;
1249 tree ident = TREE_VALUE (args);
1251 *no_add_attrs = true;
1253 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1254 warning (OPT_Wattributes, "%qE attribute ignored", name);
1255 else
1257 int j;
1258 const char *p = IDENTIFIER_POINTER (ident);
1259 int len = strlen (p);
1260 machine_mode mode = VOIDmode;
1261 tree typefm;
1262 bool valid_mode;
1264 if (len > 4 && p[0] == '_' && p[1] == '_'
1265 && p[len - 1] == '_' && p[len - 2] == '_')
1267 char *newp = (char *) alloca (len - 1);
1269 strcpy (newp, &p[2]);
1270 newp[len - 4] = '\0';
1271 p = newp;
1274 /* Change this type to have a type with the specified mode.
1275 First check for the special modes. */
1276 if (!strcmp (p, "byte"))
1277 mode = byte_mode;
1278 else if (!strcmp (p, "word"))
1279 mode = word_mode;
1280 else if (!strcmp (p, "pointer"))
1281 mode = ptr_mode;
1282 else if (!strcmp (p, "libgcc_cmp_return"))
1283 mode = targetm.libgcc_cmp_return_mode ();
1284 else if (!strcmp (p, "libgcc_shift_count"))
1285 mode = targetm.libgcc_shift_count_mode ();
1286 else if (!strcmp (p, "unwind_word"))
1287 mode = targetm.unwind_word_mode ();
1288 else
1289 for (j = 0; j < NUM_MACHINE_MODES; j++)
1290 if (!strcmp (p, GET_MODE_NAME (j)))
1292 mode = (machine_mode) j;
1293 break;
1296 if (mode == VOIDmode)
1298 error ("unknown machine mode %qE", ident);
1299 return NULL_TREE;
1302 valid_mode = false;
1303 switch (GET_MODE_CLASS (mode))
1305 case MODE_INT:
1306 case MODE_PARTIAL_INT:
1307 case MODE_FLOAT:
1308 case MODE_DECIMAL_FLOAT:
1309 case MODE_FRACT:
1310 case MODE_UFRACT:
1311 case MODE_ACCUM:
1312 case MODE_UACCUM:
1313 valid_mode = targetm.scalar_mode_supported_p (mode);
1314 break;
1316 case MODE_COMPLEX_INT:
1317 case MODE_COMPLEX_FLOAT:
1318 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1319 break;
1321 case MODE_VECTOR_INT:
1322 case MODE_VECTOR_FLOAT:
1323 case MODE_VECTOR_FRACT:
1324 case MODE_VECTOR_UFRACT:
1325 case MODE_VECTOR_ACCUM:
1326 case MODE_VECTOR_UACCUM:
1327 warning (OPT_Wattributes, "specifying vector types with "
1328 "__attribute__ ((mode)) is deprecated");
1329 warning (OPT_Wattributes,
1330 "use __attribute__ ((vector_size)) instead");
1331 valid_mode = vector_mode_valid_p (mode);
1332 break;
1334 default:
1335 break;
1337 if (!valid_mode)
1339 error ("unable to emulate %qs", p);
1340 return NULL_TREE;
1343 if (POINTER_TYPE_P (type))
1345 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1346 tree (*fn)(tree, machine_mode, bool);
1348 if (!targetm.addr_space.valid_pointer_mode (mode, as))
1350 error ("invalid pointer mode %qs", p);
1351 return NULL_TREE;
1354 if (TREE_CODE (type) == POINTER_TYPE)
1355 fn = build_pointer_type_for_mode;
1356 else
1357 fn = build_reference_type_for_mode;
1358 typefm = fn (TREE_TYPE (type), mode, false);
1360 else
1362 /* For fixed-point modes, we need to test if the signness of type
1363 and the machine mode are consistent. */
1364 if (ALL_FIXED_POINT_MODE_P (mode)
1365 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1367 error ("signedness of type and machine mode %qs don%'t match", p);
1368 return NULL_TREE;
1370 /* For fixed-point modes, we need to pass saturating info. */
1371 typefm = lang_hooks.types.type_for_mode (mode,
1372 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1373 : TYPE_UNSIGNED (type));
1376 if (typefm == NULL_TREE)
1378 error ("no data type for mode %qs", p);
1379 return NULL_TREE;
1381 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1383 /* For enumeral types, copy the precision from the integer
1384 type returned above. If not an INTEGER_TYPE, we can't use
1385 this mode for this type. */
1386 if (TREE_CODE (typefm) != INTEGER_TYPE)
1388 error ("cannot use mode %qs for enumeral types", p);
1389 return NULL_TREE;
1392 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1394 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1395 typefm = type;
1397 else
1399 /* We cannot build a type variant, as there's code that assumes
1400 that TYPE_MAIN_VARIANT has the same mode. This includes the
1401 debug generators. Instead, create a subrange type. This
1402 results in all of the enumeral values being emitted only once
1403 in the original, and the subtype gets them by reference. */
1404 if (TYPE_UNSIGNED (type))
1405 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1406 else
1407 typefm = make_signed_type (TYPE_PRECISION (typefm));
1408 TREE_TYPE (typefm) = type;
1411 else if (VECTOR_MODE_P (mode)
1412 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1413 : TREE_CODE (type) != TREE_CODE (typefm))
1415 error ("mode %qs applied to inappropriate type", p);
1416 return NULL_TREE;
1419 *node = typefm;
1422 return NULL_TREE;
1425 /* Handle a "section" attribute; arguments as in
1426 struct attribute_spec.handler. */
1428 static tree
1429 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1430 int ARG_UNUSED (flags), bool *no_add_attrs)
1432 tree decl = *node;
1434 if (!targetm_common.have_named_sections)
1436 error_at (DECL_SOURCE_LOCATION (*node),
1437 "section attributes are not supported for this target");
1438 goto fail;
1441 user_defined_section_attribute = true;
1443 if (!VAR_OR_FUNCTION_DECL_P (decl))
1445 error ("section attribute not allowed for %q+D", *node);
1446 goto fail;
1449 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1451 error ("section attribute argument not a string constant");
1452 goto fail;
1455 if (VAR_P (decl)
1456 && current_function_decl != NULL_TREE
1457 && !TREE_STATIC (decl))
1459 error_at (DECL_SOURCE_LOCATION (decl),
1460 "section attribute cannot be specified for local variables");
1461 goto fail;
1464 /* The decl may have already been given a section attribute
1465 from a previous declaration. Ensure they match. */
1466 if (DECL_SECTION_NAME (decl) != NULL
1467 && strcmp (DECL_SECTION_NAME (decl),
1468 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1470 error ("section of %q+D conflicts with previous declaration", *node);
1471 goto fail;
1474 if (VAR_P (decl)
1475 && !targetm.have_tls && targetm.emutls.tmpl_section
1476 && DECL_THREAD_LOCAL_P (decl))
1478 error ("section of %q+D cannot be overridden", *node);
1479 goto fail;
1482 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1483 return NULL_TREE;
1485 fail:
1486 *no_add_attrs = true;
1487 return NULL_TREE;
1490 /* If in c++-11, check if the c++-11 alignment constraint with respect
1491 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1492 c++-11 mode, does nothing.
1494 [dcl.align]2/ says:
1496 [* if the constant expression evaluates to a fundamental alignment,
1497 the alignment requirement of the declared entity shall be the
1498 specified fundamental alignment.
1500 * if the constant expression evaluates to an extended alignment
1501 and the implementation supports that alignment in the context
1502 of the declaration, the alignment of the declared entity shall
1503 be that alignment
1505 * if the constant expression evaluates to an extended alignment
1506 and the implementation does not support that alignment in the
1507 context of the declaration, the program is ill-formed]. */
1509 static bool
1510 check_cxx_fundamental_alignment_constraints (tree node,
1511 unsigned align_log,
1512 int flags)
1514 bool alignment_too_large_p = false;
1515 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1516 unsigned max_align = 0;
1518 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1519 || (node == NULL_TREE || node == error_mark_node))
1520 return true;
1522 if (cxx_fundamental_alignment_p (requested_alignment))
1523 return true;
1525 if (VAR_P (node))
1527 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1528 /* For file scope variables and static members, the target supports
1529 alignments that are at most MAX_OFILE_ALIGNMENT. */
1530 max_align = MAX_OFILE_ALIGNMENT;
1531 else
1532 /* For stack variables, the target supports at most
1533 MAX_STACK_ALIGNMENT. */
1534 max_align = MAX_STACK_ALIGNMENT;
1535 if (requested_alignment > max_align)
1536 alignment_too_large_p = true;
1538 /* Let's be liberal for types and fields; don't limit their alignment any
1539 more than check_user_alignment already did. */
1541 if (alignment_too_large_p)
1542 pedwarn (input_location, OPT_Wattributes,
1543 "requested alignment %d is larger than %d",
1544 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1546 return !alignment_too_large_p;
1549 /* Handle a "aligned" attribute; arguments as in
1550 struct attribute_spec.handler. */
1552 static tree
1553 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1554 int flags, bool *no_add_attrs)
1556 tree decl = NULL_TREE;
1557 tree *type = NULL;
1558 int is_type = 0;
1559 tree align_expr;
1560 int i;
1562 if (args)
1564 align_expr = TREE_VALUE (args);
1565 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1566 && TREE_CODE (align_expr) != FUNCTION_DECL)
1567 align_expr = default_conversion (align_expr);
1569 else
1570 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1572 if (DECL_P (*node))
1574 decl = *node;
1575 type = &TREE_TYPE (decl);
1576 is_type = TREE_CODE (*node) == TYPE_DECL;
1578 else if (TYPE_P (*node))
1579 type = node, is_type = 1;
1581 if ((i = check_user_alignment (align_expr, true)) == -1
1582 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
1583 *no_add_attrs = true;
1584 else if (is_type)
1586 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1587 /* OK, modify the type in place. */;
1588 /* If we have a TYPE_DECL, then copy the type, so that we
1589 don't accidentally modify a builtin type. See pushdecl. */
1590 else if (decl && TREE_TYPE (decl) != error_mark_node
1591 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1593 tree tt = TREE_TYPE (decl);
1594 *type = build_variant_type_copy (*type);
1595 DECL_ORIGINAL_TYPE (decl) = tt;
1596 TYPE_NAME (*type) = decl;
1597 TREE_USED (*type) = TREE_USED (decl);
1598 TREE_TYPE (decl) = *type;
1600 else
1601 *type = build_variant_type_copy (*type);
1603 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1604 TYPE_USER_ALIGN (*type) = 1;
1606 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1607 && TREE_CODE (decl) != FIELD_DECL)
1609 error ("alignment may not be specified for %q+D", decl);
1610 *no_add_attrs = true;
1612 else if (DECL_USER_ALIGN (decl)
1613 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1614 /* C++-11 [dcl.align/4]:
1616 When multiple alignment-specifiers are specified for an
1617 entity, the alignment requirement shall be set to the
1618 strictest specified alignment.
1620 This formally comes from the c++11 specification but we are
1621 doing it for the GNU attribute syntax as well. */
1622 *no_add_attrs = true;
1623 else if (TREE_CODE (decl) == FUNCTION_DECL
1624 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1626 if (DECL_USER_ALIGN (decl))
1627 error ("alignment for %q+D was previously specified as %d "
1628 "and may not be decreased", decl,
1629 DECL_ALIGN (decl) / BITS_PER_UNIT);
1630 else
1631 error ("alignment for %q+D must be at least %d", decl,
1632 DECL_ALIGN (decl) / BITS_PER_UNIT);
1633 *no_add_attrs = true;
1635 else
1637 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1638 DECL_USER_ALIGN (decl) = 1;
1641 return NULL_TREE;
1644 /* Handle a "weak" attribute; arguments as in
1645 struct attribute_spec.handler. */
1647 static tree
1648 handle_weak_attribute (tree *node, tree name,
1649 tree ARG_UNUSED (args),
1650 int ARG_UNUSED (flags),
1651 bool * ARG_UNUSED (no_add_attrs))
1653 if (TREE_CODE (*node) == FUNCTION_DECL
1654 && DECL_DECLARED_INLINE_P (*node))
1656 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1657 *no_add_attrs = true;
1659 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1661 error ("indirect function %q+D cannot be declared weak", *node);
1662 *no_add_attrs = true;
1663 return NULL_TREE;
1665 else if (VAR_OR_FUNCTION_DECL_P (*node))
1666 declare_weak (*node);
1667 else
1668 warning (OPT_Wattributes, "%qE attribute ignored", name);
1670 return NULL_TREE;
1673 /* Handle a "noplt" attribute; arguments as in
1674 struct attribute_spec.handler. */
1676 static tree
1677 handle_noplt_attribute (tree *node, tree name,
1678 tree ARG_UNUSED (args),
1679 int ARG_UNUSED (flags),
1680 bool * ARG_UNUSED (no_add_attrs))
1682 if (TREE_CODE (*node) != FUNCTION_DECL)
1684 warning (OPT_Wattributes,
1685 "%qE attribute is only applicable on functions", name);
1686 *no_add_attrs = true;
1687 return NULL_TREE;
1689 return NULL_TREE;
1692 /* Handle an "alias" or "ifunc" attribute; arguments as in
1693 struct attribute_spec.handler, except that IS_ALIAS tells us
1694 whether this is an alias as opposed to ifunc attribute. */
1696 static tree
1697 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
1698 bool *no_add_attrs)
1700 tree decl = *node;
1702 if (TREE_CODE (decl) != FUNCTION_DECL
1703 && (!is_alias || !VAR_P (decl)))
1705 warning (OPT_Wattributes, "%qE attribute ignored", name);
1706 *no_add_attrs = true;
1708 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1709 || (TREE_CODE (decl) != FUNCTION_DECL
1710 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
1711 /* A static variable declaration is always a tentative definition,
1712 but the alias is a non-tentative definition which overrides. */
1713 || (TREE_CODE (decl) != FUNCTION_DECL
1714 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
1716 error ("%q+D defined both normally and as %qE attribute", decl, name);
1717 *no_add_attrs = true;
1718 return NULL_TREE;
1720 else if (!is_alias
1721 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
1722 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
1724 error ("weak %q+D cannot be defined %qE", decl, name);
1725 *no_add_attrs = true;
1726 return NULL_TREE;
1729 /* Note that the very first time we process a nested declaration,
1730 decl_function_context will not be set. Indeed, *would* never
1731 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
1732 we do below. After such frobbery, pushdecl would set the context.
1733 In any case, this is never what we want. */
1734 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
1736 tree id;
1738 id = TREE_VALUE (args);
1739 if (TREE_CODE (id) != STRING_CST)
1741 error ("attribute %qE argument not a string", name);
1742 *no_add_attrs = true;
1743 return NULL_TREE;
1745 id = get_identifier (TREE_STRING_POINTER (id));
1746 /* This counts as a use of the object pointed to. */
1747 TREE_USED (id) = 1;
1749 if (TREE_CODE (decl) == FUNCTION_DECL)
1750 DECL_INITIAL (decl) = error_mark_node;
1751 else
1752 TREE_STATIC (decl) = 1;
1754 if (!is_alias)
1755 /* ifuncs are also aliases, so set that attribute too. */
1756 DECL_ATTRIBUTES (decl)
1757 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
1759 else
1761 warning (OPT_Wattributes, "%qE attribute ignored", name);
1762 *no_add_attrs = true;
1765 if (decl_in_symtab_p (*node))
1767 struct symtab_node *n = symtab_node::get (decl);
1768 if (n && n->refuse_visibility_changes)
1770 if (is_alias)
1771 error ("%+D declared alias after being used", decl);
1772 else
1773 error ("%+D declared ifunc after being used", decl);
1778 return NULL_TREE;
1781 /* Handle an "alias" or "ifunc" attribute; arguments as in
1782 struct attribute_spec.handler. */
1784 static tree
1785 handle_ifunc_attribute (tree *node, tree name, tree args,
1786 int ARG_UNUSED (flags), bool *no_add_attrs)
1788 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
1791 /* Handle an "alias" or "ifunc" attribute; arguments as in
1792 struct attribute_spec.handler. */
1794 static tree
1795 handle_alias_attribute (tree *node, tree name, tree args,
1796 int ARG_UNUSED (flags), bool *no_add_attrs)
1798 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
1801 /* Handle a "weakref" attribute; arguments as in struct
1802 attribute_spec.handler. */
1804 static tree
1805 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1806 int flags, bool *no_add_attrs)
1808 tree attr = NULL_TREE;
1810 /* We must ignore the attribute when it is associated with
1811 local-scoped decls, since attribute alias is ignored and many
1812 such symbols do not even have a DECL_WEAK field. */
1813 if (decl_function_context (*node)
1814 || current_function_decl
1815 || !VAR_OR_FUNCTION_DECL_P (*node))
1817 warning (OPT_Wattributes, "%qE attribute ignored", name);
1818 *no_add_attrs = true;
1819 return NULL_TREE;
1822 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1824 error ("indirect function %q+D cannot be declared weakref", *node);
1825 *no_add_attrs = true;
1826 return NULL_TREE;
1829 /* The idea here is that `weakref("name")' mutates into `weakref,
1830 alias("name")', and weakref without arguments, in turn,
1831 implicitly adds weak. */
1833 if (args)
1835 attr = tree_cons (get_identifier ("alias"), args, attr);
1836 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
1838 *no_add_attrs = true;
1840 decl_attributes (node, attr, flags);
1842 else
1844 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
1845 error_at (DECL_SOURCE_LOCATION (*node),
1846 "weakref attribute must appear before alias attribute");
1848 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
1849 and that isn't supported; and because it wants to add it to
1850 the list of weak decls, which isn't helpful. */
1851 DECL_WEAK (*node) = 1;
1854 if (decl_in_symtab_p (*node))
1856 struct symtab_node *n = symtab_node::get (*node);
1857 if (n && n->refuse_visibility_changes)
1858 error ("%+D declared weakref after being used", *node);
1861 return NULL_TREE;
1864 /* Handle an "visibility" attribute; arguments as in
1865 struct attribute_spec.handler. */
1867 static tree
1868 handle_visibility_attribute (tree *node, tree name, tree args,
1869 int ARG_UNUSED (flags),
1870 bool *ARG_UNUSED (no_add_attrs))
1872 tree decl = *node;
1873 tree id = TREE_VALUE (args);
1874 enum symbol_visibility vis;
1876 if (TYPE_P (*node))
1878 if (TREE_CODE (*node) == ENUMERAL_TYPE)
1879 /* OK */;
1880 else if (!RECORD_OR_UNION_TYPE_P (*node))
1882 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
1883 name);
1884 return NULL_TREE;
1886 else if (TYPE_FIELDS (*node))
1888 error ("%qE attribute ignored because %qT is already defined",
1889 name, *node);
1890 return NULL_TREE;
1893 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
1895 warning (OPT_Wattributes, "%qE attribute ignored", name);
1896 return NULL_TREE;
1899 if (TREE_CODE (id) != STRING_CST)
1901 error ("visibility argument not a string");
1902 return NULL_TREE;
1905 /* If this is a type, set the visibility on the type decl. */
1906 if (TYPE_P (decl))
1908 decl = TYPE_NAME (decl);
1909 if (!decl)
1910 return NULL_TREE;
1911 if (TREE_CODE (decl) == IDENTIFIER_NODE)
1913 warning (OPT_Wattributes, "%qE attribute ignored on types",
1914 name);
1915 return NULL_TREE;
1919 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
1920 vis = VISIBILITY_DEFAULT;
1921 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
1922 vis = VISIBILITY_INTERNAL;
1923 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
1924 vis = VISIBILITY_HIDDEN;
1925 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
1926 vis = VISIBILITY_PROTECTED;
1927 else
1929 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
1930 vis = VISIBILITY_DEFAULT;
1933 if (DECL_VISIBILITY_SPECIFIED (decl)
1934 && vis != DECL_VISIBILITY (decl))
1936 tree attributes = (TYPE_P (*node)
1937 ? TYPE_ATTRIBUTES (*node)
1938 : DECL_ATTRIBUTES (decl));
1939 if (lookup_attribute ("visibility", attributes))
1940 error ("%qD redeclared with different visibility", decl);
1941 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1942 && lookup_attribute ("dllimport", attributes))
1943 error ("%qD was declared %qs which implies default visibility",
1944 decl, "dllimport");
1945 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1946 && lookup_attribute ("dllexport", attributes))
1947 error ("%qD was declared %qs which implies default visibility",
1948 decl, "dllexport");
1951 DECL_VISIBILITY (decl) = vis;
1952 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1954 /* Go ahead and attach the attribute to the node as well. This is needed
1955 so we can determine whether we have VISIBILITY_DEFAULT because the
1956 visibility was not specified, or because it was explicitly overridden
1957 from the containing scope. */
1959 return NULL_TREE;
1962 /* Handle an "tls_model" attribute; arguments as in
1963 struct attribute_spec.handler. */
1965 static tree
1966 handle_tls_model_attribute (tree *node, tree name, tree args,
1967 int ARG_UNUSED (flags), bool *no_add_attrs)
1969 tree id;
1970 tree decl = *node;
1971 enum tls_model kind;
1973 *no_add_attrs = true;
1975 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
1977 warning (OPT_Wattributes, "%qE attribute ignored", name);
1978 return NULL_TREE;
1981 kind = DECL_TLS_MODEL (decl);
1982 id = TREE_VALUE (args);
1983 if (TREE_CODE (id) != STRING_CST)
1985 error ("tls_model argument not a string");
1986 return NULL_TREE;
1989 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
1990 kind = TLS_MODEL_LOCAL_EXEC;
1991 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
1992 kind = TLS_MODEL_INITIAL_EXEC;
1993 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
1994 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
1995 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
1996 kind = TLS_MODEL_GLOBAL_DYNAMIC;
1997 else
1998 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2000 set_decl_tls_model (decl, kind);
2001 return NULL_TREE;
2004 /* Handle a "no_instrument_function" attribute; arguments as in
2005 struct attribute_spec.handler. */
2007 static tree
2008 handle_no_instrument_function_attribute (tree *node, tree name,
2009 tree ARG_UNUSED (args),
2010 int ARG_UNUSED (flags),
2011 bool *no_add_attrs)
2013 tree decl = *node;
2015 if (TREE_CODE (decl) != FUNCTION_DECL)
2017 error_at (DECL_SOURCE_LOCATION (decl),
2018 "%qE attribute applies only to functions", name);
2019 *no_add_attrs = true;
2021 else
2022 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2024 return NULL_TREE;
2027 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2028 struct attribute_spec.handler. */
2030 static tree
2031 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2032 int, bool *no_add_attrs)
2034 if (TREE_CODE (*node) != FUNCTION_DECL)
2036 warning (OPT_Wattributes, "%qE attribute ignored", name);
2037 *no_add_attrs = true;
2040 return NULL_TREE;
2043 /* Handle a "malloc" attribute; arguments as in
2044 struct attribute_spec.handler. */
2046 static tree
2047 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2048 int ARG_UNUSED (flags), bool *no_add_attrs)
2050 if (TREE_CODE (*node) == FUNCTION_DECL
2051 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2052 DECL_IS_MALLOC (*node) = 1;
2053 else
2055 warning (OPT_Wattributes, "%qE attribute ignored", name);
2056 *no_add_attrs = true;
2059 return NULL_TREE;
2062 /* Handle a "alloc_size" attribute; arguments as in
2063 struct attribute_spec.handler. */
2065 static tree
2066 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2067 int ARG_UNUSED (flags), bool *no_add_attrs)
2069 unsigned arg_count = type_num_arguments (*node);
2070 for (; args; args = TREE_CHAIN (args))
2072 tree position = TREE_VALUE (args);
2073 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2074 && TREE_CODE (position) != FUNCTION_DECL)
2075 position = default_conversion (position);
2077 if (!tree_fits_uhwi_p (position)
2078 || !arg_count
2079 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2081 warning (OPT_Wattributes,
2082 "alloc_size parameter outside range");
2083 *no_add_attrs = true;
2084 return NULL_TREE;
2087 return NULL_TREE;
2090 /* Handle a "alloc_align" attribute; arguments as in
2091 struct attribute_spec.handler. */
2093 static tree
2094 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2095 bool *no_add_attrs)
2097 unsigned arg_count = type_num_arguments (*node);
2098 tree position = TREE_VALUE (args);
2099 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2100 && TREE_CODE (position) != FUNCTION_DECL)
2101 position = default_conversion (position);
2103 if (!tree_fits_uhwi_p (position)
2104 || !arg_count
2105 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2107 warning (OPT_Wattributes,
2108 "alloc_align parameter outside range");
2109 *no_add_attrs = true;
2110 return NULL_TREE;
2112 return NULL_TREE;
2115 /* Handle a "assume_aligned" attribute; arguments as in
2116 struct attribute_spec.handler. */
2118 static tree
2119 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2120 bool *no_add_attrs)
2122 for (; args; args = TREE_CHAIN (args))
2124 tree position = TREE_VALUE (args);
2125 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2126 && TREE_CODE (position) != FUNCTION_DECL)
2127 position = default_conversion (position);
2129 if (TREE_CODE (position) != INTEGER_CST)
2131 warning (OPT_Wattributes,
2132 "assume_aligned parameter not integer constant");
2133 *no_add_attrs = true;
2134 return NULL_TREE;
2137 return NULL_TREE;
2140 /* Handle a "fn spec" attribute; arguments as in
2141 struct attribute_spec.handler. */
2143 static tree
2144 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2145 tree args, int ARG_UNUSED (flags),
2146 bool *no_add_attrs ATTRIBUTE_UNUSED)
2148 gcc_assert (args
2149 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2150 && !TREE_CHAIN (args));
2151 return NULL_TREE;
2154 /* Handle a "bnd_variable_size" attribute; arguments as in
2155 struct attribute_spec.handler. */
2157 static tree
2158 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2159 int ARG_UNUSED (flags), bool *no_add_attrs)
2161 if (TREE_CODE (*node) != FIELD_DECL)
2163 warning (OPT_Wattributes, "%qE attribute ignored", name);
2164 *no_add_attrs = true;
2167 return NULL_TREE;
2170 /* Handle a "bnd_legacy" attribute; arguments as in
2171 struct attribute_spec.handler. */
2173 static tree
2174 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
2175 int ARG_UNUSED (flags), bool *no_add_attrs)
2177 if (TREE_CODE (*node) != FUNCTION_DECL)
2179 warning (OPT_Wattributes, "%qE attribute ignored", name);
2180 *no_add_attrs = true;
2183 return NULL_TREE;
2186 /* Handle a "bnd_instrument" attribute; arguments as in
2187 struct attribute_spec.handler. */
2189 static tree
2190 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
2191 int ARG_UNUSED (flags), bool *no_add_attrs)
2193 if (TREE_CODE (*node) != FUNCTION_DECL)
2195 warning (OPT_Wattributes, "%qE attribute ignored", name);
2196 *no_add_attrs = true;
2199 return NULL_TREE;
2202 /* Handle a "warn_unused" attribute; arguments as in
2203 struct attribute_spec.handler. */
2205 static tree
2206 handle_warn_unused_attribute (tree *node, tree name,
2207 tree args ATTRIBUTE_UNUSED,
2208 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2210 if (TYPE_P (*node))
2211 /* Do nothing else, just set the attribute. We'll get at
2212 it later with lookup_attribute. */
2214 else
2216 warning (OPT_Wattributes, "%qE attribute ignored", name);
2217 *no_add_attrs = true;
2220 return NULL_TREE;
2223 /* Handle an "omp declare simd" attribute; arguments as in
2224 struct attribute_spec.handler. */
2226 static tree
2227 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2229 return NULL_TREE;
2232 /* Handle a "simd" attribute. */
2234 static tree
2235 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2237 if (TREE_CODE (*node) == FUNCTION_DECL)
2239 if (lookup_attribute ("cilk simd function",
2240 DECL_ATTRIBUTES (*node)) != NULL)
2242 error_at (DECL_SOURCE_LOCATION (*node),
2243 "%<__simd__%> attribute cannot be used in the same "
2244 "function marked as a Cilk Plus SIMD-enabled function");
2245 *no_add_attrs = true;
2247 else
2249 tree t = get_identifier ("omp declare simd");
2250 tree attr = NULL_TREE;
2251 if (args)
2253 tree id = TREE_VALUE (args);
2255 if (TREE_CODE (id) != STRING_CST)
2257 error ("attribute %qE argument not a string", name);
2258 *no_add_attrs = true;
2259 return NULL_TREE;
2262 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2263 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2264 OMP_CLAUSE_NOTINBRANCH);
2265 else
2266 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2267 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2268 OMP_CLAUSE_INBRANCH);
2269 else
2271 error ("only %<inbranch%> and %<notinbranch%> flags are "
2272 "allowed for %<__simd__%> attribute");
2273 *no_add_attrs = true;
2274 return NULL_TREE;
2278 DECL_ATTRIBUTES (*node) = tree_cons (t,
2279 build_tree_list (NULL_TREE,
2280 attr),
2281 DECL_ATTRIBUTES (*node));
2284 else
2286 warning (OPT_Wattributes, "%qE attribute ignored", name);
2287 *no_add_attrs = true;
2290 return NULL_TREE;
2293 /* Handle an "omp declare target" attribute; arguments as in
2294 struct attribute_spec.handler. */
2296 static tree
2297 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2299 return NULL_TREE;
2302 /* Handle a "returns_twice" attribute; arguments as in
2303 struct attribute_spec.handler. */
2305 static tree
2306 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2307 int ARG_UNUSED (flags), bool *no_add_attrs)
2309 if (TREE_CODE (*node) == FUNCTION_DECL)
2310 DECL_IS_RETURNS_TWICE (*node) = 1;
2311 else
2313 warning (OPT_Wattributes, "%qE attribute ignored", name);
2314 *no_add_attrs = true;
2317 return NULL_TREE;
2320 /* Handle a "no_limit_stack" attribute; arguments as in
2321 struct attribute_spec.handler. */
2323 static tree
2324 handle_no_limit_stack_attribute (tree *node, tree name,
2325 tree ARG_UNUSED (args),
2326 int ARG_UNUSED (flags),
2327 bool *no_add_attrs)
2329 tree decl = *node;
2331 if (TREE_CODE (decl) != FUNCTION_DECL)
2333 error_at (DECL_SOURCE_LOCATION (decl),
2334 "%qE attribute applies only to functions", name);
2335 *no_add_attrs = true;
2337 else if (DECL_INITIAL (decl))
2339 error_at (DECL_SOURCE_LOCATION (decl),
2340 "can%'t set %qE attribute after definition", name);
2341 *no_add_attrs = true;
2343 else
2344 DECL_NO_LIMIT_STACK (decl) = 1;
2346 return NULL_TREE;
2349 /* Handle a "pure" attribute; arguments as in
2350 struct attribute_spec.handler. */
2352 static tree
2353 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2354 int ARG_UNUSED (flags), bool *no_add_attrs)
2356 if (TREE_CODE (*node) == FUNCTION_DECL)
2357 DECL_PURE_P (*node) = 1;
2358 /* ??? TODO: Support types. */
2359 else
2361 warning (OPT_Wattributes, "%qE attribute ignored", name);
2362 *no_add_attrs = true;
2365 return NULL_TREE;
2368 /* Digest an attribute list destined for a transactional memory statement.
2369 ALLOWED is the set of attributes that are allowed for this statement;
2370 return the attribute we parsed. Multiple attributes are never allowed. */
2373 parse_tm_stmt_attr (tree attrs, int allowed)
2375 tree a_seen = NULL;
2376 int m_seen = 0;
2378 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2380 tree a = TREE_PURPOSE (attrs);
2381 int m = 0;
2383 if (is_attribute_p ("outer", a))
2384 m = TM_STMT_ATTR_OUTER;
2386 if ((m & allowed) == 0)
2388 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2389 continue;
2392 if (m_seen == 0)
2394 a_seen = a;
2395 m_seen = m;
2397 else if (m_seen == m)
2398 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2399 else
2400 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2403 return m_seen;
2406 /* Transform a TM attribute name into a maskable integer and back.
2407 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2408 to how the lack of an attribute is treated. */
2411 tm_attr_to_mask (tree attr)
2413 if (attr == NULL)
2414 return 0;
2415 if (is_attribute_p ("transaction_safe", attr))
2416 return TM_ATTR_SAFE;
2417 if (is_attribute_p ("transaction_callable", attr))
2418 return TM_ATTR_CALLABLE;
2419 if (is_attribute_p ("transaction_pure", attr))
2420 return TM_ATTR_PURE;
2421 if (is_attribute_p ("transaction_unsafe", attr))
2422 return TM_ATTR_IRREVOCABLE;
2423 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2424 return TM_ATTR_MAY_CANCEL_OUTER;
2425 return 0;
2428 tree
2429 tm_mask_to_attr (int mask)
2431 const char *str;
2432 switch (mask)
2434 case TM_ATTR_SAFE:
2435 str = "transaction_safe";
2436 break;
2437 case TM_ATTR_CALLABLE:
2438 str = "transaction_callable";
2439 break;
2440 case TM_ATTR_PURE:
2441 str = "transaction_pure";
2442 break;
2443 case TM_ATTR_IRREVOCABLE:
2444 str = "transaction_unsafe";
2445 break;
2446 case TM_ATTR_MAY_CANCEL_OUTER:
2447 str = "transaction_may_cancel_outer";
2448 break;
2449 default:
2450 gcc_unreachable ();
2452 return get_identifier (str);
2455 /* Return the first TM attribute seen in LIST. */
2457 tree
2458 find_tm_attribute (tree list)
2460 for (; list ; list = TREE_CHAIN (list))
2462 tree name = TREE_PURPOSE (list);
2463 if (tm_attr_to_mask (name) != 0)
2464 return name;
2466 return NULL_TREE;
2469 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2470 Here we accept only function types, and verify that none of the other
2471 function TM attributes are also applied. */
2472 /* ??? We need to accept class types for C++, but not C. This greatly
2473 complicates this function, since we can no longer rely on the extra
2474 processing given by function_type_required. */
2476 static tree
2477 handle_tm_attribute (tree *node, tree name, tree args,
2478 int flags, bool *no_add_attrs)
2480 /* Only one path adds the attribute; others don't. */
2481 *no_add_attrs = true;
2483 switch (TREE_CODE (*node))
2485 case RECORD_TYPE:
2486 case UNION_TYPE:
2487 /* Only tm_callable and tm_safe apply to classes. */
2488 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2489 goto ignored;
2490 /* FALLTHRU */
2492 case FUNCTION_TYPE:
2493 case METHOD_TYPE:
2495 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2496 if (old_name == name)
2498 else if (old_name != NULL_TREE)
2499 error ("type was previously declared %qE", old_name);
2500 else
2501 *no_add_attrs = false;
2503 break;
2505 case FUNCTION_DECL:
2507 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2508 want to set transaction_safe on the type. */
2509 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2510 if (!TYPE_P (DECL_CONTEXT (*node)))
2511 error_at (DECL_SOURCE_LOCATION (*node),
2512 "%<transaction_safe_dynamic%> may only be specified for "
2513 "a virtual function");
2514 *no_add_attrs = false;
2515 decl_attributes (&TREE_TYPE (*node),
2516 build_tree_list (get_identifier ("transaction_safe"),
2517 NULL_TREE),
2519 break;
2522 case POINTER_TYPE:
2524 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2525 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2527 tree fn_tmp = TREE_TYPE (*node);
2528 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2529 *node = build_pointer_type (fn_tmp);
2530 break;
2533 /* FALLTHRU */
2535 default:
2536 /* If a function is next, pass it on to be tried next. */
2537 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2538 return tree_cons (name, args, NULL);
2540 ignored:
2541 warning (OPT_Wattributes, "%qE attribute ignored", name);
2542 break;
2545 return NULL_TREE;
2548 /* Handle the TM_WRAP attribute; arguments as in
2549 struct attribute_spec.handler. */
2551 static tree
2552 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2553 int ARG_UNUSED (flags), bool *no_add_attrs)
2555 tree decl = *node;
2557 /* We don't need the attribute even on success, since we
2558 record the entry in an external table. */
2559 *no_add_attrs = true;
2561 if (TREE_CODE (decl) != FUNCTION_DECL)
2562 warning (OPT_Wattributes, "%qE attribute ignored", name);
2563 else
2565 tree wrap_decl = TREE_VALUE (args);
2566 if (error_operand_p (wrap_decl))
2568 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2569 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2570 error ("%qE argument not an identifier", name);
2571 else
2573 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2574 wrap_decl = lookup_name (wrap_decl);
2575 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2577 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2578 TREE_TYPE (wrap_decl)))
2579 record_tm_replacement (wrap_decl, decl);
2580 else
2581 error ("%qD is not compatible with %qD", wrap_decl, decl);
2583 else
2584 error ("%qE argument is not a function", name);
2588 return NULL_TREE;
2591 /* Ignore the given attribute. Used when this attribute may be usefully
2592 overridden by the target, but is not used generically. */
2594 static tree
2595 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2596 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2597 bool *no_add_attrs)
2599 *no_add_attrs = true;
2600 return NULL_TREE;
2603 /* Handle a "no vops" attribute; arguments as in
2604 struct attribute_spec.handler. */
2606 static tree
2607 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2608 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2609 bool *ARG_UNUSED (no_add_attrs))
2611 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2612 DECL_IS_NOVOPS (*node) = 1;
2613 return NULL_TREE;
2616 /* Handle a "deprecated" attribute; arguments as in
2617 struct attribute_spec.handler. */
2619 static tree
2620 handle_deprecated_attribute (tree *node, tree name,
2621 tree args, int flags,
2622 bool *no_add_attrs)
2624 tree type = NULL_TREE;
2625 int warn = 0;
2626 tree what = NULL_TREE;
2628 if (!args)
2629 *no_add_attrs = true;
2630 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2632 error ("deprecated message is not a string");
2633 *no_add_attrs = true;
2636 if (DECL_P (*node))
2638 tree decl = *node;
2639 type = TREE_TYPE (decl);
2641 if (TREE_CODE (decl) == TYPE_DECL
2642 || TREE_CODE (decl) == PARM_DECL
2643 || VAR_OR_FUNCTION_DECL_P (decl)
2644 || TREE_CODE (decl) == FIELD_DECL
2645 || TREE_CODE (decl) == CONST_DECL
2646 || objc_method_decl (TREE_CODE (decl)))
2647 TREE_DEPRECATED (decl) = 1;
2648 else
2649 warn = 1;
2651 else if (TYPE_P (*node))
2653 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2654 *node = build_variant_type_copy (*node);
2655 TREE_DEPRECATED (*node) = 1;
2656 type = *node;
2658 else
2659 warn = 1;
2661 if (warn)
2663 *no_add_attrs = true;
2664 if (type && TYPE_NAME (type))
2666 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2667 what = TYPE_NAME (*node);
2668 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2669 && DECL_NAME (TYPE_NAME (type)))
2670 what = DECL_NAME (TYPE_NAME (type));
2672 if (what)
2673 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2674 else
2675 warning (OPT_Wattributes, "%qE attribute ignored", name);
2678 return NULL_TREE;
2681 /* Handle a "vector_size" attribute; arguments as in
2682 struct attribute_spec.handler. */
2684 static tree
2685 handle_vector_size_attribute (tree *node, tree name, tree args,
2686 int ARG_UNUSED (flags),
2687 bool *no_add_attrs)
2689 unsigned HOST_WIDE_INT vecsize, nunits;
2690 machine_mode orig_mode;
2691 tree type = *node, new_type, size;
2693 *no_add_attrs = true;
2695 size = TREE_VALUE (args);
2696 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2697 && TREE_CODE (size) != FUNCTION_DECL)
2698 size = default_conversion (size);
2700 if (!tree_fits_uhwi_p (size))
2702 warning (OPT_Wattributes, "%qE attribute ignored", name);
2703 return NULL_TREE;
2706 /* Get the vector size (in bytes). */
2707 vecsize = tree_to_uhwi (size);
2709 /* We need to provide for vector pointers, vector arrays, and
2710 functions returning vectors. For example:
2712 __attribute__((vector_size(16))) short *foo;
2714 In this case, the mode is SI, but the type being modified is
2715 HI, so we need to look further. */
2717 while (POINTER_TYPE_P (type)
2718 || TREE_CODE (type) == FUNCTION_TYPE
2719 || TREE_CODE (type) == METHOD_TYPE
2720 || TREE_CODE (type) == ARRAY_TYPE
2721 || TREE_CODE (type) == OFFSET_TYPE)
2722 type = TREE_TYPE (type);
2724 /* Get the mode of the type being modified. */
2725 orig_mode = TYPE_MODE (type);
2727 if ((!INTEGRAL_TYPE_P (type)
2728 && !SCALAR_FLOAT_TYPE_P (type)
2729 && !FIXED_POINT_TYPE_P (type))
2730 || (!SCALAR_FLOAT_MODE_P (orig_mode)
2731 && GET_MODE_CLASS (orig_mode) != MODE_INT
2732 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
2733 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
2734 || TREE_CODE (type) == BOOLEAN_TYPE)
2736 error ("invalid vector type for attribute %qE", name);
2737 return NULL_TREE;
2740 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2742 error ("vector size not an integral multiple of component size");
2743 return NULL;
2746 if (vecsize == 0)
2748 error ("zero vector size");
2749 return NULL;
2752 /* Calculate how many units fit in the vector. */
2753 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
2754 if (nunits & (nunits - 1))
2756 error ("number of components of the vector not a power of two");
2757 return NULL_TREE;
2760 new_type = build_vector_type (type, nunits);
2762 /* Build back pointers if needed. */
2763 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
2765 return NULL_TREE;
2768 /* Handle the "nonnull" attribute. */
2770 static tree
2771 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
2772 tree args, int ARG_UNUSED (flags),
2773 bool *no_add_attrs)
2775 tree type = *node;
2776 unsigned HOST_WIDE_INT attr_arg_num;
2778 /* If no arguments are specified, all pointer arguments should be
2779 non-null. Verify a full prototype is given so that the arguments
2780 will have the correct types when we actually check them later.
2781 Avoid diagnosing type-generic built-ins since those have no
2782 prototype. */
2783 if (!args)
2785 if (!prototype_p (type)
2786 && (!TYPE_ATTRIBUTES (type)
2787 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
2789 error ("nonnull attribute without arguments on a non-prototype");
2790 *no_add_attrs = true;
2792 return NULL_TREE;
2795 /* Argument list specified. Verify that each argument number references
2796 a pointer argument. */
2797 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
2799 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
2801 tree arg = TREE_VALUE (args);
2802 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
2803 && TREE_CODE (arg) != FUNCTION_DECL)
2804 arg = default_conversion (arg);
2806 if (!get_nonnull_operand (arg, &arg_num))
2808 error ("nonnull argument has invalid operand number (argument %lu)",
2809 (unsigned long) attr_arg_num);
2810 *no_add_attrs = true;
2811 return NULL_TREE;
2814 if (prototype_p (type))
2816 function_args_iterator iter;
2817 tree argument;
2819 function_args_iter_init (&iter, type);
2820 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
2822 argument = function_args_iter_cond (&iter);
2823 if (argument == NULL_TREE || ck_num == arg_num)
2824 break;
2827 if (!argument
2828 || TREE_CODE (argument) == VOID_TYPE)
2830 error ("nonnull argument with out-of-range operand number "
2831 "(argument %lu, operand %lu)",
2832 (unsigned long) attr_arg_num, (unsigned long) arg_num);
2833 *no_add_attrs = true;
2834 return NULL_TREE;
2837 if (TREE_CODE (argument) != POINTER_TYPE)
2839 error ("nonnull argument references non-pointer operand "
2840 "(argument %lu, operand %lu)",
2841 (unsigned long) attr_arg_num, (unsigned long) arg_num);
2842 *no_add_attrs = true;
2843 return NULL_TREE;
2848 return NULL_TREE;
2851 /* Handle a "nothrow" attribute; arguments as in
2852 struct attribute_spec.handler. */
2854 static tree
2855 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2856 int ARG_UNUSED (flags), bool *no_add_attrs)
2858 if (TREE_CODE (*node) == FUNCTION_DECL)
2859 TREE_NOTHROW (*node) = 1;
2860 /* ??? TODO: Support types. */
2861 else
2863 warning (OPT_Wattributes, "%qE attribute ignored", name);
2864 *no_add_attrs = true;
2867 return NULL_TREE;
2870 /* Handle a "cleanup" attribute; arguments as in
2871 struct attribute_spec.handler. */
2873 static tree
2874 handle_cleanup_attribute (tree *node, tree name, tree args,
2875 int ARG_UNUSED (flags), bool *no_add_attrs)
2877 tree decl = *node;
2878 tree cleanup_id, cleanup_decl;
2880 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
2881 for global destructors in C++. This requires infrastructure that
2882 we don't have generically at the moment. It's also not a feature
2883 we'd be missing too much, since we do have attribute constructor. */
2884 if (!VAR_P (decl) || TREE_STATIC (decl))
2886 warning (OPT_Wattributes, "%qE attribute ignored", name);
2887 *no_add_attrs = true;
2888 return NULL_TREE;
2891 /* Verify that the argument is a function in scope. */
2892 /* ??? We could support pointers to functions here as well, if
2893 that was considered desirable. */
2894 cleanup_id = TREE_VALUE (args);
2895 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
2897 error ("cleanup argument not an identifier");
2898 *no_add_attrs = true;
2899 return NULL_TREE;
2901 cleanup_decl = lookup_name (cleanup_id);
2902 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
2904 error ("cleanup argument not a function");
2905 *no_add_attrs = true;
2906 return NULL_TREE;
2909 /* That the function has proper type is checked with the
2910 eventual call to build_function_call. */
2912 return NULL_TREE;
2915 /* Handle a "warn_unused_result" attribute. No special handling. */
2917 static tree
2918 handle_warn_unused_result_attribute (tree *node, tree name,
2919 tree ARG_UNUSED (args),
2920 int ARG_UNUSED (flags), bool *no_add_attrs)
2922 /* Ignore the attribute for functions not returning any value. */
2923 if (VOID_TYPE_P (TREE_TYPE (*node)))
2925 warning (OPT_Wattributes, "%qE attribute ignored", name);
2926 *no_add_attrs = true;
2929 return NULL_TREE;
2932 /* Handle a "sentinel" attribute. */
2934 static tree
2935 handle_sentinel_attribute (tree *node, tree name, tree args,
2936 int ARG_UNUSED (flags), bool *no_add_attrs)
2938 if (!prototype_p (*node))
2940 warning (OPT_Wattributes,
2941 "%qE attribute requires prototypes with named arguments", name);
2942 *no_add_attrs = true;
2944 else
2946 if (!stdarg_p (*node))
2948 warning (OPT_Wattributes,
2949 "%qE attribute only applies to variadic functions", name);
2950 *no_add_attrs = true;
2954 if (args)
2956 tree position = TREE_VALUE (args);
2957 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2958 && TREE_CODE (position) != FUNCTION_DECL)
2959 position = default_conversion (position);
2961 if (TREE_CODE (position) != INTEGER_CST
2962 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
2964 warning (OPT_Wattributes,
2965 "requested position is not an integer constant");
2966 *no_add_attrs = true;
2968 else
2970 if (tree_int_cst_lt (position, integer_zero_node))
2972 warning (OPT_Wattributes,
2973 "requested position is less than zero");
2974 *no_add_attrs = true;
2979 return NULL_TREE;
2982 /* Handle a "type_generic" attribute. */
2984 static tree
2985 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
2986 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2987 bool * ARG_UNUSED (no_add_attrs))
2989 /* Ensure we have a function type. */
2990 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
2992 /* Ensure we have a variadic function. */
2993 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
2995 return NULL_TREE;
2998 /* Handle a "target" attribute. */
3000 static tree
3001 handle_target_attribute (tree *node, tree name, tree args, int flags,
3002 bool *no_add_attrs)
3004 /* Ensure we have a function type. */
3005 if (TREE_CODE (*node) != FUNCTION_DECL)
3007 warning (OPT_Wattributes, "%qE attribute ignored", name);
3008 *no_add_attrs = true;
3010 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3012 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3013 "with %qs attribute", name, "target_clones");
3014 *no_add_attrs = true;
3016 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3017 flags))
3018 *no_add_attrs = true;
3020 return NULL_TREE;
3023 /* Handle a "target_clones" attribute. */
3025 static tree
3026 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3027 int ARG_UNUSED (flags), bool *no_add_attrs)
3029 /* Ensure we have a function type. */
3030 if (TREE_CODE (*node) == FUNCTION_DECL)
3032 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3034 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3035 "with %qs attribute", name, "always_inline");
3036 *no_add_attrs = true;
3038 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3040 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3041 "with %qs attribute", name, "target");
3042 *no_add_attrs = true;
3044 else
3045 /* Do not inline functions with multiple clone targets. */
3046 DECL_UNINLINABLE (*node) = 1;
3048 else
3050 warning (OPT_Wattributes, "%qE attribute ignored", name);
3051 *no_add_attrs = true;
3053 return NULL_TREE;
3056 /* For handling "optimize" attribute. arguments as in
3057 struct attribute_spec.handler. */
3059 static tree
3060 handle_optimize_attribute (tree *node, tree name, tree args,
3061 int ARG_UNUSED (flags), bool *no_add_attrs)
3063 /* Ensure we have a function type. */
3064 if (TREE_CODE (*node) != FUNCTION_DECL)
3066 warning (OPT_Wattributes, "%qE attribute ignored", name);
3067 *no_add_attrs = true;
3069 else
3071 struct cl_optimization cur_opts;
3072 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3074 /* Save current options. */
3075 cl_optimization_save (&cur_opts, &global_options);
3077 /* If we previously had some optimization options, use them as the
3078 default. */
3079 if (old_opts)
3080 cl_optimization_restore (&global_options,
3081 TREE_OPTIMIZATION (old_opts));
3083 /* Parse options, and update the vector. */
3084 parse_optimize_options (args, true);
3085 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3086 = build_optimization_node (&global_options);
3088 /* Restore current options. */
3089 cl_optimization_restore (&global_options, &cur_opts);
3092 return NULL_TREE;
3095 /* Handle a "no_split_stack" attribute. */
3097 static tree
3098 handle_no_split_stack_attribute (tree *node, tree name,
3099 tree ARG_UNUSED (args),
3100 int ARG_UNUSED (flags),
3101 bool *no_add_attrs)
3103 tree decl = *node;
3105 if (TREE_CODE (decl) != FUNCTION_DECL)
3107 error_at (DECL_SOURCE_LOCATION (decl),
3108 "%qE attribute applies only to functions", name);
3109 *no_add_attrs = true;
3111 else if (DECL_INITIAL (decl))
3113 error_at (DECL_SOURCE_LOCATION (decl),
3114 "can%'t set %qE attribute after definition", name);
3115 *no_add_attrs = true;
3118 return NULL_TREE;
3121 /* Handle a "returns_nonnull" attribute; arguments as in
3122 struct attribute_spec.handler. */
3124 static tree
3125 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3126 bool *no_add_attrs)
3128 // Even without a prototype we still have a return type we can check.
3129 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3131 error ("returns_nonnull attribute on a function not returning a pointer");
3132 *no_add_attrs = true;
3134 return NULL_TREE;
3137 /* Handle a "designated_init" attribute; arguments as in
3138 struct attribute_spec.handler. */
3140 static tree
3141 handle_designated_init_attribute (tree *node, tree name, tree, int,
3142 bool *no_add_attrs)
3144 if (TREE_CODE (*node) != RECORD_TYPE)
3146 error ("%qE attribute is only valid on %<struct%> type", name);
3147 *no_add_attrs = true;
3149 return NULL_TREE;
3153 /* Handle a "fallthrough" attribute; arguments as in struct
3154 attribute_spec.handler. */
3156 static tree
3157 handle_fallthrough_attribute (tree *, tree name, tree, int,
3158 bool *no_add_attrs)
3160 warning (OPT_Wattributes, "%qE attribute ignored", name);
3161 *no_add_attrs = true;
3162 return NULL_TREE;