[AArch64] Fix -mlow-precision-div (PR 86838)
[official-gcc.git] / gcc / c-family / c-attribs.c
blob9da9c2765e34737dae7a44eb4e72854ba8fa8458
1 /* C-family attributes handling.
2 Copyright (C) 1992-2018 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_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
56 int, bool *);
57 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
58 int, bool *);
59 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
60 int, bool *);
61 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
62 bool *);
63 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
64 bool *);
65 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
66 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_always_inline_attribute (tree *, tree, tree, int,
73 bool *);
74 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
80 bool *);
81 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
82 bool *);
83 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_transparent_union_attribute (tree *, tree, tree,
85 int, bool *);
86 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
87 int, bool *);
88 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
94 int, bool *);
95 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
96 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
97 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
98 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
101 static tree handle_visibility_attribute (tree *, tree, tree, int,
102 bool *);
103 static tree handle_tls_model_attribute (tree *, tree, tree, int,
104 bool *);
105 static tree handle_no_instrument_function_attribute (tree *, tree,
106 tree, int, bool *);
107 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
108 tree, int, bool *);
109 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
111 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
112 bool *);
113 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_deprecated_attribute (tree *, tree, tree, int,
118 bool *);
119 static tree handle_vector_size_attribute (tree *, tree, tree, int,
120 bool *);
121 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
126 bool *);
127 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
135 static tree ignore_attribute (tree *, tree, tree, int, bool *);
136 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
140 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
141 bool *);
142 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
143 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
144 bool *);
145 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
147 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
148 int, bool *);
150 /* Helper to define attribute exclusions. */
151 #define ATTR_EXCL(name, function, type, variable) \
152 { name, function, type, variable }
154 /* Define attributes that are mutually exclusive with one another. */
155 static const struct attribute_spec::exclusions attr_aligned_exclusions[] =
157 /* Attribute name exclusion applies to:
158 function, type, variable */
159 ATTR_EXCL ("aligned", true, false, false),
160 ATTR_EXCL ("packed", true, false, false),
161 ATTR_EXCL (NULL, false, false, false)
164 static const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
166 ATTR_EXCL ("cold", true, true, true),
167 ATTR_EXCL ("hot", true, true, true),
168 ATTR_EXCL (NULL, false, false, false)
171 static const struct attribute_spec::exclusions attr_common_exclusions[] =
173 ATTR_EXCL ("common", true, true, true),
174 ATTR_EXCL ("nocommon", true, true, true),
175 ATTR_EXCL (NULL, false, false, false),
178 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
180 ATTR_EXCL ("noinline", true, true, true),
181 ATTR_EXCL (NULL, false, false, false),
184 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
186 ATTR_EXCL ("always_inline", true, true, true),
187 ATTR_EXCL ("gnu_inline", true, true, true),
188 ATTR_EXCL (NULL, false, false, false),
191 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
193 ATTR_EXCL ("alloc_align", true, true, true),
194 ATTR_EXCL ("alloc_size", true, true, true),
195 ATTR_EXCL ("const", true, true, true),
196 ATTR_EXCL ("malloc", true, true, true),
197 ATTR_EXCL ("pure", true, true, true),
198 ATTR_EXCL ("returns_twice", true, true, true),
199 ATTR_EXCL ("warn_unused_result", true, true, true),
200 ATTR_EXCL (NULL, false, false, false),
203 static const struct attribute_spec::exclusions
204 attr_warn_unused_result_exclusions[] =
206 ATTR_EXCL ("noreturn", true, true, true),
207 ATTR_EXCL ("warn_unused_result", true, true, true),
208 ATTR_EXCL (NULL, false, false, false),
211 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
213 ATTR_EXCL ("noreturn", true, true, true),
214 ATTR_EXCL (NULL, false, false, false),
217 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
218 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
220 ATTR_EXCL ("const", true, true, true),
221 ATTR_EXCL ("noreturn", true, true, true),
222 ATTR_EXCL ("pure", true, true, true),
223 ATTR_EXCL (NULL, false, false, false),
226 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
228 ATTR_EXCL ("const", true, true, true),
229 ATTR_EXCL ("alloc_align", true, true, true),
230 ATTR_EXCL ("alloc_size", true, true, true),
231 ATTR_EXCL ("malloc", true, true, true),
232 ATTR_EXCL ("noreturn", true, true, true),
233 ATTR_EXCL ("pure", true, true, true),
234 ATTR_EXCL (NULL, false, false, false)
237 /* Table of machine-independent attributes common to all C-like languages.
239 Current list of processed common attributes: nonnull. */
240 const struct attribute_spec c_common_attribute_table[] =
242 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
243 affects_type_identity, handler, exclude } */
244 { "packed", 0, 0, false, false, false, false,
245 handle_packed_attribute,
246 attr_aligned_exclusions },
247 { "nocommon", 0, 0, true, false, false, false,
248 handle_nocommon_attribute,
249 attr_common_exclusions },
250 { "common", 0, 0, true, false, false, false,
251 handle_common_attribute,
252 attr_common_exclusions },
253 /* FIXME: logically, noreturn attributes should be listed as
254 "false, true, true" and apply to function types. But implementing this
255 would require all the places in the compiler that use TREE_THIS_VOLATILE
256 on a decl to identify non-returning functions to be located and fixed
257 to check the function type instead. */
258 { "noreturn", 0, 0, true, false, false, false,
259 handle_noreturn_attribute,
260 attr_noreturn_exclusions },
261 { "volatile", 0, 0, true, false, false, false,
262 handle_noreturn_attribute, NULL },
263 { "stack_protect", 0, 0, true, false, false, false,
264 handle_stack_protect_attribute, NULL },
265 { "noinline", 0, 0, true, false, false, false,
266 handle_noinline_attribute,
267 attr_noinline_exclusions },
268 { "noclone", 0, 0, true, false, false, false,
269 handle_noclone_attribute, NULL },
270 { "no_icf", 0, 0, true, false, false, false,
271 handle_noicf_attribute, NULL },
272 { "noipa", 0, 0, true, false, false, false,
273 handle_noipa_attribute, NULL },
274 { "leaf", 0, 0, true, false, false, false,
275 handle_leaf_attribute, NULL },
276 { "always_inline", 0, 0, true, false, false, false,
277 handle_always_inline_attribute,
278 attr_inline_exclusions },
279 { "gnu_inline", 0, 0, true, false, false, false,
280 handle_gnu_inline_attribute,
281 attr_inline_exclusions },
282 { "artificial", 0, 0, true, false, false, false,
283 handle_artificial_attribute, NULL },
284 { "flatten", 0, 0, true, false, false, false,
285 handle_flatten_attribute, NULL },
286 { "used", 0, 0, true, false, false, false,
287 handle_used_attribute, NULL },
288 { "unused", 0, 0, false, false, false, false,
289 handle_unused_attribute, NULL },
290 { "externally_visible", 0, 0, true, false, false, false,
291 handle_externally_visible_attribute, NULL },
292 { "no_reorder", 0, 0, true, false, false, false,
293 handle_no_reorder_attribute, NULL },
294 /* The same comments as for noreturn attributes apply to const ones. */
295 { "const", 0, 0, true, false, false, false,
296 handle_const_attribute,
297 attr_const_pure_exclusions },
298 { "scalar_storage_order", 1, 1, false, false, false, false,
299 handle_scalar_storage_order_attribute, NULL },
300 { "transparent_union", 0, 0, false, false, false, false,
301 handle_transparent_union_attribute, NULL },
302 { "constructor", 0, 1, true, false, false, false,
303 handle_constructor_attribute, NULL },
304 { "destructor", 0, 1, true, false, false, false,
305 handle_destructor_attribute, NULL },
306 { "mode", 1, 1, false, true, false, false,
307 handle_mode_attribute, NULL },
308 { "section", 1, 1, true, false, false, false,
309 handle_section_attribute, NULL },
310 { "aligned", 0, 1, false, false, false, false,
311 handle_aligned_attribute,
312 attr_aligned_exclusions },
313 { "warn_if_not_aligned", 0, 1, false, false, false, false,
314 handle_warn_if_not_aligned_attribute, NULL },
315 { "weak", 0, 0, true, false, false, false,
316 handle_weak_attribute, NULL },
317 { "noplt", 0, 0, true, false, false, false,
318 handle_noplt_attribute, NULL },
319 { "ifunc", 1, 1, true, false, false, false,
320 handle_ifunc_attribute, NULL },
321 { "alias", 1, 1, true, false, false, false,
322 handle_alias_attribute, NULL },
323 { "weakref", 0, 1, true, false, false, false,
324 handle_weakref_attribute, NULL },
325 { "no_instrument_function", 0, 0, true, false, false, false,
326 handle_no_instrument_function_attribute,
327 NULL },
328 { "no_profile_instrument_function", 0, 0, true, false, false, false,
329 handle_no_profile_instrument_function_attribute,
330 NULL },
331 { "malloc", 0, 0, true, false, false, false,
332 handle_malloc_attribute, attr_alloc_exclusions },
333 { "returns_twice", 0, 0, true, false, false, false,
334 handle_returns_twice_attribute,
335 attr_returns_twice_exclusions },
336 { "no_stack_limit", 0, 0, true, false, false, false,
337 handle_no_limit_stack_attribute, NULL },
338 { "pure", 0, 0, true, false, false, false,
339 handle_pure_attribute,
340 attr_const_pure_exclusions },
341 { "transaction_callable", 0, 0, false, true, false, false,
342 handle_tm_attribute, NULL },
343 { "transaction_unsafe", 0, 0, false, true, false, true,
344 handle_tm_attribute, NULL },
345 { "transaction_safe", 0, 0, false, true, false, true,
346 handle_tm_attribute, NULL },
347 { "transaction_safe_dynamic", 0, 0, true, false, false, false,
348 handle_tm_attribute, NULL },
349 { "transaction_may_cancel_outer", 0, 0, false, true, false, false,
350 handle_tm_attribute, NULL },
351 /* ??? These two attributes didn't make the transition from the
352 Intel language document to the multi-vendor language document. */
353 { "transaction_pure", 0, 0, false, true, false, false,
354 handle_tm_attribute, NULL },
355 { "transaction_wrap", 1, 1, true, false, false, false,
356 handle_tm_wrap_attribute, NULL },
357 /* For internal use (marking of builtins) only. The name contains space
358 to prevent its usage in source code. */
359 { "no vops", 0, 0, true, false, false, false,
360 handle_novops_attribute, NULL },
361 { "deprecated", 0, 1, false, false, false, false,
362 handle_deprecated_attribute, NULL },
363 { "vector_size", 1, 1, false, true, false, true,
364 handle_vector_size_attribute, NULL },
365 { "visibility", 1, 1, false, false, false, false,
366 handle_visibility_attribute, NULL },
367 { "tls_model", 1, 1, true, false, false, false,
368 handle_tls_model_attribute, NULL },
369 { "nonnull", 0, -1, false, true, true, false,
370 handle_nonnull_attribute, NULL },
371 { "nonstring", 0, 0, true, false, false, false,
372 handle_nonstring_attribute, NULL },
373 { "nothrow", 0, 0, true, false, false, false,
374 handle_nothrow_attribute, NULL },
375 { "may_alias", 0, 0, false, true, false, false, NULL, NULL },
376 { "cleanup", 1, 1, true, false, false, false,
377 handle_cleanup_attribute, NULL },
378 { "warn_unused_result", 0, 0, false, true, true, false,
379 handle_warn_unused_result_attribute,
380 attr_warn_unused_result_exclusions },
381 { "sentinel", 0, 1, false, true, true, false,
382 handle_sentinel_attribute, NULL },
383 /* For internal use (marking of builtins) only. The name contains space
384 to prevent its usage in source code. */
385 { "type generic", 0, 0, false, true, true, false,
386 handle_type_generic_attribute, NULL },
387 { "alloc_size", 1, 2, false, true, true, false,
388 handle_alloc_size_attribute,
389 attr_alloc_exclusions },
390 { "cold", 0, 0, true, false, false, false,
391 handle_cold_attribute,
392 attr_cold_hot_exclusions },
393 { "hot", 0, 0, true, false, false, false,
394 handle_hot_attribute,
395 attr_cold_hot_exclusions },
396 { "no_address_safety_analysis",
397 0, 0, true, false, false, false,
398 handle_no_address_safety_analysis_attribute,
399 NULL },
400 { "no_sanitize", 1, -1, true, false, false, false,
401 handle_no_sanitize_attribute, NULL },
402 { "no_sanitize_address", 0, 0, true, false, false, false,
403 handle_no_sanitize_address_attribute, NULL },
404 { "no_sanitize_thread", 0, 0, true, false, false, false,
405 handle_no_sanitize_thread_attribute, NULL },
406 { "no_sanitize_undefined", 0, 0, true, false, false, false,
407 handle_no_sanitize_undefined_attribute, NULL },
408 { "asan odr indicator", 0, 0, true, false, false, false,
409 handle_asan_odr_indicator_attribute, NULL },
410 { "warning", 1, 1, true, false, false, false,
411 handle_error_attribute, NULL },
412 { "error", 1, 1, true, false, false, false,
413 handle_error_attribute, NULL },
414 { "target", 1, -1, true, false, false, false,
415 handle_target_attribute, NULL },
416 { "target_clones", 1, -1, true, false, false, false,
417 handle_target_clones_attribute, NULL },
418 { "optimize", 1, -1, true, false, false, false,
419 handle_optimize_attribute, NULL },
420 /* For internal use only. The leading '*' both prevents its usage in
421 source code and signals that it may be overridden by machine tables. */
422 { "*tm regparm", 0, 0, false, true, true, false,
423 ignore_attribute, NULL },
424 { "no_split_stack", 0, 0, true, false, false, false,
425 handle_no_split_stack_attribute, NULL },
426 /* For internal use (marking of builtins and runtime functions) only.
427 The name contains space to prevent its usage in source code. */
428 { "fn spec", 1, 1, false, true, true, false,
429 handle_fnspec_attribute, NULL },
430 { "warn_unused", 0, 0, false, false, false, false,
431 handle_warn_unused_attribute, NULL },
432 { "returns_nonnull", 0, 0, false, true, true, false,
433 handle_returns_nonnull_attribute, NULL },
434 { "omp declare simd", 0, -1, true, false, false, false,
435 handle_omp_declare_simd_attribute, NULL },
436 { "simd", 0, 1, true, false, false, false,
437 handle_simd_attribute, NULL },
438 { "omp declare target", 0, 0, true, false, false, false,
439 handle_omp_declare_target_attribute, NULL },
440 { "omp declare target link", 0, 0, true, false, false, false,
441 handle_omp_declare_target_attribute, NULL },
442 { "omp declare target implicit", 0, 0, true, false, false, false,
443 handle_omp_declare_target_attribute, NULL },
444 { "alloc_align", 1, 1, false, true, true, false,
445 handle_alloc_align_attribute,
446 attr_alloc_exclusions },
447 { "assume_aligned", 1, 2, false, true, true, false,
448 handle_assume_aligned_attribute, NULL },
449 { "designated_init", 0, 0, false, true, false, false,
450 handle_designated_init_attribute, NULL },
451 { "fallthrough", 0, 0, false, false, false, false,
452 handle_fallthrough_attribute, NULL },
453 { "patchable_function_entry", 1, 2, true, false, false, false,
454 handle_patchable_function_entry_attribute,
455 NULL },
456 { "nocf_check", 0, 0, false, true, true, true,
457 handle_nocf_check_attribute, NULL },
458 { NULL, 0, 0, false, false, false, false, NULL, NULL }
461 /* Give the specifications for the format attributes, used by C and all
462 descendants.
464 Current list of processed format attributes: format, format_arg. */
465 const struct attribute_spec c_common_format_attribute_table[] =
467 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
468 affects_type_identity, handler, exclude } */
469 { "format", 3, 3, false, true, true, false,
470 handle_format_attribute, NULL },
471 { "format_arg", 1, 1, false, true, true, false,
472 handle_format_arg_attribute, NULL },
473 { NULL, 0, 0, false, false, false, false, NULL, NULL }
476 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
477 identifier as an argument, so the front end shouldn't look it up. */
479 bool
480 attribute_takes_identifier_p (const_tree attr_id)
482 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
483 if (spec == NULL)
484 /* Unknown attribute that we'll end up ignoring, return true so we
485 don't complain about an identifier argument. */
486 return true;
487 else if (!strcmp ("mode", spec->name)
488 || !strcmp ("format", spec->name)
489 || !strcmp ("cleanup", spec->name))
490 return true;
491 else
492 return targetm.attribute_takes_identifier_p (attr_id);
495 /* Attribute handlers common to C front ends. */
497 /* Handle a "packed" attribute; arguments as in
498 struct attribute_spec.handler. */
500 static tree
501 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
502 int flags, bool *no_add_attrs)
504 if (TYPE_P (*node))
506 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
508 warning (OPT_Wattributes,
509 "%qE attribute ignored for type %qT", name, *node);
510 *no_add_attrs = true;
512 else
513 TYPE_PACKED (*node) = 1;
515 else if (TREE_CODE (*node) == FIELD_DECL)
517 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
518 /* Still pack bitfields. */
519 && ! DECL_C_BIT_FIELD (*node))
520 warning (OPT_Wattributes,
521 "%qE attribute ignored for field of type %qT",
522 name, TREE_TYPE (*node));
523 else
524 DECL_PACKED (*node) = 1;
526 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
527 used for DECL_REGISTER. It wouldn't mean anything anyway.
528 We can't set DECL_PACKED on the type of a TYPE_DECL, because
529 that changes what the typedef is typing. */
530 else
532 warning (OPT_Wattributes, "%qE attribute ignored", name);
533 *no_add_attrs = true;
536 return NULL_TREE;
539 /* Handle a "nocommon" attribute; arguments as in
540 struct attribute_spec.handler. */
542 static tree
543 handle_nocommon_attribute (tree *node, tree name,
544 tree ARG_UNUSED (args),
545 int ARG_UNUSED (flags), bool *no_add_attrs)
547 if (VAR_P (*node))
548 DECL_COMMON (*node) = 0;
549 else
551 warning (OPT_Wattributes, "%qE attribute ignored", name);
552 *no_add_attrs = true;
555 return NULL_TREE;
558 /* Handle a "common" attribute; arguments as in
559 struct attribute_spec.handler. */
561 static tree
562 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
563 int ARG_UNUSED (flags), bool *no_add_attrs)
565 if (VAR_P (*node))
566 DECL_COMMON (*node) = 1;
567 else
569 warning (OPT_Wattributes, "%qE attribute ignored", name);
570 *no_add_attrs = true;
573 return NULL_TREE;
576 /* Handle a "noreturn" attribute; arguments as in
577 struct attribute_spec.handler. */
579 static tree
580 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
581 int ARG_UNUSED (flags), bool *no_add_attrs)
583 tree type = TREE_TYPE (*node);
585 /* See FIXME comment in c_common_attribute_table. */
586 if (TREE_CODE (*node) == FUNCTION_DECL
587 || objc_method_decl (TREE_CODE (*node)))
588 TREE_THIS_VOLATILE (*node) = 1;
589 else if (TREE_CODE (type) == POINTER_TYPE
590 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
591 TREE_TYPE (*node)
592 = (build_qualified_type
593 (build_pointer_type
594 (build_type_variant (TREE_TYPE (type),
595 TYPE_READONLY (TREE_TYPE (type)), 1)),
596 TYPE_QUALS (type)));
597 else
599 warning (OPT_Wattributes, "%qE attribute ignored", name);
600 *no_add_attrs = true;
603 return NULL_TREE;
606 /* Handle a "hot" and attribute; arguments as in
607 struct attribute_spec.handler. */
609 static tree
610 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
611 int ARG_UNUSED (flags), bool *no_add_attrs)
613 if (TREE_CODE (*node) == FUNCTION_DECL
614 || TREE_CODE (*node) == LABEL_DECL)
616 /* Attribute hot processing is done later with lookup_attribute. */
618 else
620 warning (OPT_Wattributes, "%qE attribute ignored", name);
621 *no_add_attrs = true;
624 return NULL_TREE;
627 /* Handle a "cold" and attribute; arguments as in
628 struct attribute_spec.handler. */
630 static tree
631 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
632 int ARG_UNUSED (flags), bool *no_add_attrs)
634 if (TREE_CODE (*node) == FUNCTION_DECL
635 || TREE_CODE (*node) == LABEL_DECL)
637 /* Attribute cold processing is done later with lookup_attribute. */
639 else
641 warning (OPT_Wattributes, "%qE attribute ignored", name);
642 *no_add_attrs = true;
645 return NULL_TREE;
648 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
650 void
651 add_no_sanitize_value (tree node, unsigned int flags)
653 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
654 if (attr)
656 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
657 flags |= old_value;
659 if (flags == old_value)
660 return;
662 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
664 else
665 DECL_ATTRIBUTES (node)
666 = tree_cons (get_identifier ("no_sanitize"),
667 build_int_cst (unsigned_type_node, flags),
668 DECL_ATTRIBUTES (node));
671 /* Handle a "no_sanitize" attribute; arguments as in
672 struct attribute_spec.handler. */
674 static tree
675 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
676 bool *no_add_attrs)
678 unsigned int flags = 0;
679 *no_add_attrs = true;
680 if (TREE_CODE (*node) != FUNCTION_DECL)
682 warning (OPT_Wattributes, "%qE attribute ignored", name);
683 return NULL_TREE;
686 for (; args; args = TREE_CHAIN (args))
688 tree id = TREE_VALUE (args);
689 if (TREE_CODE (id) != STRING_CST)
691 error ("no_sanitize argument not a string");
692 return NULL_TREE;
695 char *string = ASTRDUP (TREE_STRING_POINTER (id));
696 flags |= parse_no_sanitize_attribute (string);
699 add_no_sanitize_value (*node, flags);
701 return NULL_TREE;
704 /* Handle a "no_sanitize_address" attribute; arguments as in
705 struct attribute_spec.handler. */
707 static tree
708 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
709 bool *no_add_attrs)
711 *no_add_attrs = true;
712 if (TREE_CODE (*node) != FUNCTION_DECL)
713 warning (OPT_Wattributes, "%qE attribute ignored", name);
714 else
715 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
717 return NULL_TREE;
720 /* Handle a "no_sanitize_thread" attribute; arguments as in
721 struct attribute_spec.handler. */
723 static tree
724 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
725 bool *no_add_attrs)
727 *no_add_attrs = true;
728 if (TREE_CODE (*node) != FUNCTION_DECL)
729 warning (OPT_Wattributes, "%qE attribute ignored", name);
730 else
731 add_no_sanitize_value (*node, SANITIZE_THREAD);
733 return NULL_TREE;
737 /* Handle a "no_address_safety_analysis" attribute; arguments as in
738 struct attribute_spec.handler. */
740 static tree
741 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
742 bool *no_add_attrs)
744 *no_add_attrs = true;
745 if (TREE_CODE (*node) != FUNCTION_DECL)
746 warning (OPT_Wattributes, "%qE attribute ignored", name);
747 else
748 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
750 return NULL_TREE;
753 /* Handle a "no_sanitize_undefined" attribute; arguments as in
754 struct attribute_spec.handler. */
756 static tree
757 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
758 bool *no_add_attrs)
760 *no_add_attrs = true;
761 if (TREE_CODE (*node) != FUNCTION_DECL)
762 warning (OPT_Wattributes, "%qE attribute ignored", name);
763 else
764 add_no_sanitize_value (*node,
765 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
767 return NULL_TREE;
770 /* Handle an "asan odr indicator" attribute; arguments as in
771 struct attribute_spec.handler. */
773 static tree
774 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
776 return NULL_TREE;
779 /* Handle a "stack_protect" attribute; arguments as in
780 struct attribute_spec.handler. */
782 static tree
783 handle_stack_protect_attribute (tree *node, tree name, tree, int,
784 bool *no_add_attrs)
786 if (TREE_CODE (*node) != FUNCTION_DECL)
788 warning (OPT_Wattributes, "%qE attribute ignored", name);
789 *no_add_attrs = true;
792 return NULL_TREE;
795 /* Handle a "noipa" attribute; arguments as in
796 struct attribute_spec.handler. */
798 static tree
799 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
801 if (TREE_CODE (*node) != FUNCTION_DECL)
803 warning (OPT_Wattributes, "%qE attribute ignored", name);
804 *no_add_attrs = true;
807 return NULL_TREE;
810 /* Handle a "noinline" attribute; arguments as in
811 struct attribute_spec.handler. */
813 static tree
814 handle_noinline_attribute (tree *node, tree name,
815 tree ARG_UNUSED (args),
816 int ARG_UNUSED (flags), bool *no_add_attrs)
818 if (TREE_CODE (*node) == FUNCTION_DECL)
820 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
822 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
823 "with attribute %qs", name, "always_inline");
824 *no_add_attrs = true;
826 else
827 DECL_UNINLINABLE (*node) = 1;
829 else
831 warning (OPT_Wattributes, "%qE attribute ignored", name);
832 *no_add_attrs = true;
835 return NULL_TREE;
838 /* Handle a "noclone" attribute; arguments as in
839 struct attribute_spec.handler. */
841 static tree
842 handle_noclone_attribute (tree *node, tree name,
843 tree ARG_UNUSED (args),
844 int ARG_UNUSED (flags), bool *no_add_attrs)
846 if (TREE_CODE (*node) != FUNCTION_DECL)
848 warning (OPT_Wattributes, "%qE attribute ignored", name);
849 *no_add_attrs = true;
852 return NULL_TREE;
855 /* Handle a "nocf_check" attribute; arguments as in
856 struct attribute_spec.handler. */
858 static tree
859 handle_nocf_check_attribute (tree *node, tree name,
860 tree ARG_UNUSED (args),
861 int ARG_UNUSED (flags), bool *no_add_attrs)
863 if (TREE_CODE (*node) != FUNCTION_TYPE
864 && TREE_CODE (*node) != METHOD_TYPE)
866 warning (OPT_Wattributes, "%qE attribute ignored", name);
867 *no_add_attrs = true;
869 else if (!(flag_cf_protection & CF_BRANCH))
871 warning (OPT_Wattributes, "%qE attribute ignored. Use "
872 "-fcf-protection option to enable it", name);
873 *no_add_attrs = true;
876 return NULL_TREE;
879 /* Handle a "no_icf" attribute; arguments as in
880 struct attribute_spec.handler. */
882 static tree
883 handle_noicf_attribute (tree *node, tree name,
884 tree ARG_UNUSED (args),
885 int ARG_UNUSED (flags), bool *no_add_attrs)
887 if (TREE_CODE (*node) != FUNCTION_DECL)
889 warning (OPT_Wattributes, "%qE attribute ignored", name);
890 *no_add_attrs = true;
893 return NULL_TREE;
897 /* Handle a "always_inline" attribute; arguments as in
898 struct attribute_spec.handler. */
900 static tree
901 handle_always_inline_attribute (tree *node, tree name,
902 tree ARG_UNUSED (args),
903 int ARG_UNUSED (flags),
904 bool *no_add_attrs)
906 if (TREE_CODE (*node) == FUNCTION_DECL)
908 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
910 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
911 "with %qs attribute", name, "noinline");
912 *no_add_attrs = true;
914 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
916 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
917 "with %qs attribute", name, "target_clones");
918 *no_add_attrs = true;
920 else
921 /* Set the attribute and mark it for disregarding inline
922 limits. */
923 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
925 else
927 warning (OPT_Wattributes, "%qE attribute ignored", name);
928 *no_add_attrs = true;
931 return NULL_TREE;
934 /* Handle a "gnu_inline" attribute; arguments as in
935 struct attribute_spec.handler. */
937 static tree
938 handle_gnu_inline_attribute (tree *node, tree name,
939 tree ARG_UNUSED (args),
940 int ARG_UNUSED (flags),
941 bool *no_add_attrs)
943 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
945 /* Do nothing else, just set the attribute. We'll get at
946 it later with lookup_attribute. */
948 else
950 warning (OPT_Wattributes, "%qE attribute ignored", name);
951 *no_add_attrs = true;
954 return NULL_TREE;
957 /* Handle a "leaf" attribute; arguments as in
958 struct attribute_spec.handler. */
960 static tree
961 handle_leaf_attribute (tree *node, tree name,
962 tree ARG_UNUSED (args),
963 int ARG_UNUSED (flags), bool *no_add_attrs)
965 if (TREE_CODE (*node) != FUNCTION_DECL)
967 warning (OPT_Wattributes, "%qE attribute ignored", name);
968 *no_add_attrs = true;
970 if (!TREE_PUBLIC (*node))
972 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
973 "functions", name);
974 *no_add_attrs = true;
977 return NULL_TREE;
980 /* Handle an "artificial" attribute; arguments as in
981 struct attribute_spec.handler. */
983 static tree
984 handle_artificial_attribute (tree *node, tree name,
985 tree ARG_UNUSED (args),
986 int ARG_UNUSED (flags),
987 bool *no_add_attrs)
989 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
991 /* Do nothing else, just set the attribute. We'll get at
992 it later with lookup_attribute. */
994 else
996 warning (OPT_Wattributes, "%qE attribute ignored", name);
997 *no_add_attrs = true;
1000 return NULL_TREE;
1003 /* Handle a "flatten" attribute; arguments as in
1004 struct attribute_spec.handler. */
1006 static tree
1007 handle_flatten_attribute (tree *node, tree name,
1008 tree args ATTRIBUTE_UNUSED,
1009 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1011 if (TREE_CODE (*node) == FUNCTION_DECL)
1012 /* Do nothing else, just set the attribute. We'll get at
1013 it later with lookup_attribute. */
1015 else
1017 warning (OPT_Wattributes, "%qE attribute ignored", name);
1018 *no_add_attrs = true;
1021 return NULL_TREE;
1024 /* Handle a "warning" or "error" attribute; arguments as in
1025 struct attribute_spec.handler. */
1027 static tree
1028 handle_error_attribute (tree *node, tree name, tree args,
1029 int ARG_UNUSED (flags), bool *no_add_attrs)
1031 if (TREE_CODE (*node) == FUNCTION_DECL
1032 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
1033 /* Do nothing else, just set the attribute. We'll get at
1034 it later with lookup_attribute. */
1036 else
1038 warning (OPT_Wattributes, "%qE attribute ignored", name);
1039 *no_add_attrs = true;
1042 return NULL_TREE;
1045 /* Handle a "used" attribute; arguments as in
1046 struct attribute_spec.handler. */
1048 static tree
1049 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1050 int ARG_UNUSED (flags), bool *no_add_attrs)
1052 tree node = *pnode;
1054 if (TREE_CODE (node) == FUNCTION_DECL
1055 || (VAR_P (node) && TREE_STATIC (node))
1056 || (TREE_CODE (node) == TYPE_DECL))
1058 TREE_USED (node) = 1;
1059 DECL_PRESERVE_P (node) = 1;
1060 if (VAR_P (node))
1061 DECL_READ_P (node) = 1;
1063 else
1065 warning (OPT_Wattributes, "%qE attribute ignored", name);
1066 *no_add_attrs = true;
1069 return NULL_TREE;
1072 /* Handle a "unused" attribute; arguments as in
1073 struct attribute_spec.handler. */
1075 tree
1076 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1077 int flags, bool *no_add_attrs)
1079 if (DECL_P (*node))
1081 tree decl = *node;
1083 if (TREE_CODE (decl) == PARM_DECL
1084 || VAR_OR_FUNCTION_DECL_P (decl)
1085 || TREE_CODE (decl) == LABEL_DECL
1086 || TREE_CODE (decl) == CONST_DECL
1087 || TREE_CODE (decl) == TYPE_DECL)
1089 TREE_USED (decl) = 1;
1090 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1091 DECL_READ_P (decl) = 1;
1093 else
1095 warning (OPT_Wattributes, "%qE attribute ignored", name);
1096 *no_add_attrs = true;
1099 else
1101 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1102 *node = build_variant_type_copy (*node);
1103 TREE_USED (*node) = 1;
1106 return NULL_TREE;
1109 /* Handle a "externally_visible" attribute; arguments as in
1110 struct attribute_spec.handler. */
1112 static tree
1113 handle_externally_visible_attribute (tree *pnode, tree name,
1114 tree ARG_UNUSED (args),
1115 int ARG_UNUSED (flags),
1116 bool *no_add_attrs)
1118 tree node = *pnode;
1120 if (VAR_OR_FUNCTION_DECL_P (node))
1122 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1123 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1125 warning (OPT_Wattributes,
1126 "%qE attribute have effect only on public objects", name);
1127 *no_add_attrs = true;
1130 else
1132 warning (OPT_Wattributes, "%qE attribute ignored", name);
1133 *no_add_attrs = true;
1136 return NULL_TREE;
1139 /* Handle the "no_reorder" attribute. Arguments as in
1140 struct attribute_spec.handler. */
1142 static tree
1143 handle_no_reorder_attribute (tree *pnode,
1144 tree name,
1145 tree,
1146 int,
1147 bool *no_add_attrs)
1149 tree node = *pnode;
1151 if (!VAR_OR_FUNCTION_DECL_P (node)
1152 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1154 warning (OPT_Wattributes,
1155 "%qE attribute only affects top level objects",
1156 name);
1157 *no_add_attrs = true;
1160 return NULL_TREE;
1163 /* Handle a "const" attribute; arguments as in
1164 struct attribute_spec.handler. */
1166 static tree
1167 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1168 int flags, bool *no_add_attrs)
1170 tree type = TREE_TYPE (*node);
1172 /* See FIXME comment on noreturn in c_common_attribute_table. */
1173 if (TREE_CODE (*node) == FUNCTION_DECL)
1174 TREE_READONLY (*node) = 1;
1175 else if (TREE_CODE (type) == POINTER_TYPE
1176 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1177 TREE_TYPE (*node)
1178 = (build_qualified_type
1179 (build_pointer_type
1180 (build_type_variant (TREE_TYPE (type), 1,
1181 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1182 TYPE_QUALS (type)));
1183 else
1185 warning (OPT_Wattributes, "%qE attribute ignored", name);
1186 *no_add_attrs = true;
1189 /* void __builtin_unreachable(void) is const. Accept other such
1190 built-ins but warn on user-defined functions that return void. */
1191 if (!(flags & ATTR_FLAG_BUILT_IN)
1192 && TREE_CODE (*node) == FUNCTION_DECL
1193 && VOID_TYPE_P (TREE_TYPE (type)))
1194 warning (OPT_Wattributes, "%qE attribute on function "
1195 "returning %<void%>", name);
1197 return NULL_TREE;
1200 /* Handle a "scalar_storage_order" attribute; arguments as in
1201 struct attribute_spec.handler. */
1203 static tree
1204 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1205 int flags, bool *no_add_attrs)
1207 tree id = TREE_VALUE (args);
1208 tree type;
1210 if (TREE_CODE (*node) == TYPE_DECL
1211 && ! (flags & ATTR_FLAG_CXX11))
1212 node = &TREE_TYPE (*node);
1213 type = *node;
1215 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1217 error ("scalar_storage_order is not supported because endianness "
1218 "is not uniform");
1219 return NULL_TREE;
1222 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1224 bool reverse = false;
1226 if (TREE_CODE (id) == STRING_CST
1227 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1228 reverse = !BYTES_BIG_ENDIAN;
1229 else if (TREE_CODE (id) == STRING_CST
1230 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1231 reverse = BYTES_BIG_ENDIAN;
1232 else
1234 error ("scalar_storage_order argument must be one of \"big-endian\""
1235 " or \"little-endian\"");
1236 return NULL_TREE;
1239 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1241 if (reverse)
1242 /* A type variant isn't good enough, since we don't want a cast
1243 to such a type to be removed as a no-op. */
1244 *node = type = build_duplicate_type (type);
1247 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1248 return NULL_TREE;
1251 warning (OPT_Wattributes, "%qE attribute ignored", name);
1252 *no_add_attrs = true;
1253 return NULL_TREE;
1256 /* Handle a "transparent_union" attribute; arguments as in
1257 struct attribute_spec.handler. */
1259 static tree
1260 handle_transparent_union_attribute (tree *node, tree name,
1261 tree ARG_UNUSED (args), int flags,
1262 bool *no_add_attrs)
1264 tree type;
1266 *no_add_attrs = true;
1268 if (TREE_CODE (*node) == TYPE_DECL
1269 && ! (flags & ATTR_FLAG_CXX11))
1270 node = &TREE_TYPE (*node);
1271 type = *node;
1273 if (TREE_CODE (type) == UNION_TYPE)
1275 /* Make sure that the first field will work for a transparent union.
1276 If the type isn't complete yet, leave the check to the code in
1277 finish_struct. */
1278 if (TYPE_SIZE (type))
1280 tree first = first_field (type);
1281 if (first == NULL_TREE
1282 || DECL_ARTIFICIAL (first)
1283 || TYPE_MODE (type) != DECL_MODE (first))
1284 goto ignored;
1287 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1289 /* If the type isn't complete yet, setting the flag
1290 on a variant wouldn't ever be checked. */
1291 if (!TYPE_SIZE (type))
1292 goto ignored;
1294 /* build_duplicate_type doesn't work for C++. */
1295 if (c_dialect_cxx ())
1296 goto ignored;
1298 /* A type variant isn't good enough, since we don't want a cast
1299 to such a type to be removed as a no-op. */
1300 *node = type = build_duplicate_type (type);
1303 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1304 TYPE_TRANSPARENT_AGGR (t) = 1;
1305 return NULL_TREE;
1308 ignored:
1309 warning (OPT_Wattributes, "%qE attribute ignored", name);
1310 return NULL_TREE;
1313 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1314 get the requested priority for a constructor or destructor,
1315 possibly issuing diagnostics for invalid or reserved
1316 priorities. */
1318 static priority_type
1319 get_priority (tree args, bool is_destructor)
1321 HOST_WIDE_INT pri;
1322 tree arg;
1324 if (!args)
1325 return DEFAULT_INIT_PRIORITY;
1327 if (!SUPPORTS_INIT_PRIORITY)
1329 if (is_destructor)
1330 error ("destructor priorities are not supported");
1331 else
1332 error ("constructor priorities are not supported");
1333 return DEFAULT_INIT_PRIORITY;
1336 arg = TREE_VALUE (args);
1337 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1338 goto invalid;
1339 if (arg == error_mark_node)
1340 return DEFAULT_INIT_PRIORITY;
1341 arg = default_conversion (arg);
1342 if (!tree_fits_shwi_p (arg)
1343 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1344 goto invalid;
1346 pri = tree_to_shwi (arg);
1347 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1348 goto invalid;
1350 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1352 if (is_destructor)
1353 warning (0,
1354 "destructor priorities from 0 to %d are reserved "
1355 "for the implementation",
1356 MAX_RESERVED_INIT_PRIORITY);
1357 else
1358 warning (0,
1359 "constructor priorities from 0 to %d are reserved "
1360 "for the implementation",
1361 MAX_RESERVED_INIT_PRIORITY);
1363 return pri;
1365 invalid:
1366 if (is_destructor)
1367 error ("destructor priorities must be integers from 0 to %d inclusive",
1368 MAX_INIT_PRIORITY);
1369 else
1370 error ("constructor priorities must be integers from 0 to %d inclusive",
1371 MAX_INIT_PRIORITY);
1372 return DEFAULT_INIT_PRIORITY;
1375 /* Handle a "constructor" attribute; arguments as in
1376 struct attribute_spec.handler. */
1378 static tree
1379 handle_constructor_attribute (tree *node, tree name, tree args,
1380 int ARG_UNUSED (flags),
1381 bool *no_add_attrs)
1383 tree decl = *node;
1384 tree type = TREE_TYPE (decl);
1386 if (TREE_CODE (decl) == FUNCTION_DECL
1387 && TREE_CODE (type) == FUNCTION_TYPE
1388 && decl_function_context (decl) == 0)
1390 priority_type priority;
1391 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1392 priority = get_priority (args, /*is_destructor=*/false);
1393 SET_DECL_INIT_PRIORITY (decl, priority);
1394 TREE_USED (decl) = 1;
1396 else
1398 warning (OPT_Wattributes, "%qE attribute ignored", name);
1399 *no_add_attrs = true;
1402 return NULL_TREE;
1405 /* Handle a "destructor" attribute; arguments as in
1406 struct attribute_spec.handler. */
1408 static tree
1409 handle_destructor_attribute (tree *node, tree name, tree args,
1410 int ARG_UNUSED (flags),
1411 bool *no_add_attrs)
1413 tree decl = *node;
1414 tree type = TREE_TYPE (decl);
1416 if (TREE_CODE (decl) == FUNCTION_DECL
1417 && TREE_CODE (type) == FUNCTION_TYPE
1418 && decl_function_context (decl) == 0)
1420 priority_type priority;
1421 DECL_STATIC_DESTRUCTOR (decl) = 1;
1422 priority = get_priority (args, /*is_destructor=*/true);
1423 SET_DECL_FINI_PRIORITY (decl, priority);
1424 TREE_USED (decl) = 1;
1426 else
1428 warning (OPT_Wattributes, "%qE attribute ignored", name);
1429 *no_add_attrs = true;
1432 return NULL_TREE;
1435 /* Nonzero if the mode is a valid vector mode for this architecture.
1436 This returns nonzero even if there is no hardware support for the
1437 vector mode, but we can emulate with narrower modes. */
1439 static bool
1440 vector_mode_valid_p (machine_mode mode)
1442 enum mode_class mclass = GET_MODE_CLASS (mode);
1444 /* Doh! What's going on? */
1445 if (mclass != MODE_VECTOR_INT
1446 && mclass != MODE_VECTOR_FLOAT
1447 && mclass != MODE_VECTOR_FRACT
1448 && mclass != MODE_VECTOR_UFRACT
1449 && mclass != MODE_VECTOR_ACCUM
1450 && mclass != MODE_VECTOR_UACCUM)
1451 return false;
1453 /* Hardware support. Woo hoo! */
1454 if (targetm.vector_mode_supported_p (mode))
1455 return true;
1457 /* We should probably return 1 if requesting V4DI and we have no DI,
1458 but we have V2DI, but this is probably very unlikely. */
1460 /* If we have support for the inner mode, we can safely emulate it.
1461 We may not have V2DI, but me can emulate with a pair of DIs. */
1462 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1466 /* Handle a "mode" attribute; arguments as in
1467 struct attribute_spec.handler. */
1469 static tree
1470 handle_mode_attribute (tree *node, tree name, tree args,
1471 int ARG_UNUSED (flags), bool *no_add_attrs)
1473 tree type = *node;
1474 tree ident = TREE_VALUE (args);
1476 *no_add_attrs = true;
1478 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1479 warning (OPT_Wattributes, "%qE attribute ignored", name);
1480 else
1482 int j;
1483 const char *p = IDENTIFIER_POINTER (ident);
1484 int len = strlen (p);
1485 machine_mode mode = VOIDmode;
1486 tree typefm;
1487 bool valid_mode;
1489 if (len > 4 && p[0] == '_' && p[1] == '_'
1490 && p[len - 1] == '_' && p[len - 2] == '_')
1492 char *newp = (char *) alloca (len - 1);
1494 strcpy (newp, &p[2]);
1495 newp[len - 4] = '\0';
1496 p = newp;
1499 /* Change this type to have a type with the specified mode.
1500 First check for the special modes. */
1501 if (!strcmp (p, "byte"))
1502 mode = byte_mode;
1503 else if (!strcmp (p, "word"))
1504 mode = word_mode;
1505 else if (!strcmp (p, "pointer"))
1506 mode = ptr_mode;
1507 else if (!strcmp (p, "libgcc_cmp_return"))
1508 mode = targetm.libgcc_cmp_return_mode ();
1509 else if (!strcmp (p, "libgcc_shift_count"))
1510 mode = targetm.libgcc_shift_count_mode ();
1511 else if (!strcmp (p, "unwind_word"))
1512 mode = targetm.unwind_word_mode ();
1513 else
1514 for (j = 0; j < NUM_MACHINE_MODES; j++)
1515 if (!strcmp (p, GET_MODE_NAME (j)))
1517 mode = (machine_mode) j;
1518 break;
1521 if (mode == VOIDmode)
1523 error ("unknown machine mode %qE", ident);
1524 return NULL_TREE;
1527 /* Allow the target a chance to translate MODE into something supported.
1528 See PR86324. */
1529 mode = targetm.translate_mode_attribute (mode);
1531 valid_mode = false;
1532 switch (GET_MODE_CLASS (mode))
1534 case MODE_INT:
1535 case MODE_PARTIAL_INT:
1536 case MODE_FLOAT:
1537 case MODE_DECIMAL_FLOAT:
1538 case MODE_FRACT:
1539 case MODE_UFRACT:
1540 case MODE_ACCUM:
1541 case MODE_UACCUM:
1542 valid_mode
1543 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
1544 break;
1546 case MODE_COMPLEX_INT:
1547 case MODE_COMPLEX_FLOAT:
1548 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1549 break;
1551 case MODE_VECTOR_INT:
1552 case MODE_VECTOR_FLOAT:
1553 case MODE_VECTOR_FRACT:
1554 case MODE_VECTOR_UFRACT:
1555 case MODE_VECTOR_ACCUM:
1556 case MODE_VECTOR_UACCUM:
1557 warning (OPT_Wattributes, "specifying vector types with "
1558 "__attribute__ ((mode)) is deprecated");
1559 warning (OPT_Wattributes,
1560 "use __attribute__ ((vector_size)) instead");
1561 valid_mode = vector_mode_valid_p (mode);
1562 break;
1564 default:
1565 break;
1567 if (!valid_mode)
1569 error ("unable to emulate %qs", p);
1570 return NULL_TREE;
1573 if (POINTER_TYPE_P (type))
1575 scalar_int_mode addr_mode;
1576 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1577 tree (*fn)(tree, machine_mode, bool);
1579 if (!is_a <scalar_int_mode> (mode, &addr_mode)
1580 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
1582 error ("invalid pointer mode %qs", p);
1583 return NULL_TREE;
1586 if (TREE_CODE (type) == POINTER_TYPE)
1587 fn = build_pointer_type_for_mode;
1588 else
1589 fn = build_reference_type_for_mode;
1590 typefm = fn (TREE_TYPE (type), addr_mode, false);
1592 else
1594 /* For fixed-point modes, we need to test if the signness of type
1595 and the machine mode are consistent. */
1596 if (ALL_FIXED_POINT_MODE_P (mode)
1597 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1599 error ("signedness of type and machine mode %qs don%'t match", p);
1600 return NULL_TREE;
1602 /* For fixed-point modes, we need to pass saturating info. */
1603 typefm = lang_hooks.types.type_for_mode (mode,
1604 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1605 : TYPE_UNSIGNED (type));
1608 if (typefm == NULL_TREE)
1610 error ("no data type for mode %qs", p);
1611 return NULL_TREE;
1613 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1615 /* For enumeral types, copy the precision from the integer
1616 type returned above. If not an INTEGER_TYPE, we can't use
1617 this mode for this type. */
1618 if (TREE_CODE (typefm) != INTEGER_TYPE)
1620 error ("cannot use mode %qs for enumeral types", p);
1621 return NULL_TREE;
1624 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1626 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1627 typefm = type;
1629 else
1631 /* We cannot build a type variant, as there's code that assumes
1632 that TYPE_MAIN_VARIANT has the same mode. This includes the
1633 debug generators. Instead, create a subrange type. This
1634 results in all of the enumeral values being emitted only once
1635 in the original, and the subtype gets them by reference. */
1636 if (TYPE_UNSIGNED (type))
1637 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1638 else
1639 typefm = make_signed_type (TYPE_PRECISION (typefm));
1640 TREE_TYPE (typefm) = type;
1643 else if (VECTOR_MODE_P (mode)
1644 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1645 : TREE_CODE (type) != TREE_CODE (typefm))
1647 error ("mode %qs applied to inappropriate type", p);
1648 return NULL_TREE;
1651 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1654 return NULL_TREE;
1657 /* Handle a "section" attribute; arguments as in
1658 struct attribute_spec.handler. */
1660 static tree
1661 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1662 int ARG_UNUSED (flags), bool *no_add_attrs)
1664 tree decl = *node;
1666 if (!targetm_common.have_named_sections)
1668 error_at (DECL_SOURCE_LOCATION (*node),
1669 "section attributes are not supported for this target");
1670 goto fail;
1673 if (!VAR_OR_FUNCTION_DECL_P (decl))
1675 error ("section attribute not allowed for %q+D", *node);
1676 goto fail;
1679 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1681 error ("section attribute argument not a string constant");
1682 goto fail;
1685 if (VAR_P (decl)
1686 && current_function_decl != NULL_TREE
1687 && !TREE_STATIC (decl))
1689 error_at (DECL_SOURCE_LOCATION (decl),
1690 "section attribute cannot be specified for local variables");
1691 goto fail;
1694 /* The decl may have already been given a section attribute
1695 from a previous declaration. Ensure they match. */
1696 if (DECL_SECTION_NAME (decl) != NULL
1697 && strcmp (DECL_SECTION_NAME (decl),
1698 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1700 error ("section of %q+D conflicts with previous declaration", *node);
1701 goto fail;
1704 if (VAR_P (decl)
1705 && !targetm.have_tls && targetm.emutls.tmpl_section
1706 && DECL_THREAD_LOCAL_P (decl))
1708 error ("section of %q+D cannot be overridden", *node);
1709 goto fail;
1712 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1713 return NULL_TREE;
1715 fail:
1716 *no_add_attrs = true;
1717 return NULL_TREE;
1720 /* If in c++-11, check if the c++-11 alignment constraint with respect
1721 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1722 c++-11 mode, does nothing.
1724 [dcl.align]2/ says:
1726 [* if the constant expression evaluates to a fundamental alignment,
1727 the alignment requirement of the declared entity shall be the
1728 specified fundamental alignment.
1730 * if the constant expression evaluates to an extended alignment
1731 and the implementation supports that alignment in the context
1732 of the declaration, the alignment of the declared entity shall
1733 be that alignment
1735 * if the constant expression evaluates to an extended alignment
1736 and the implementation does not support that alignment in the
1737 context of the declaration, the program is ill-formed]. */
1739 static bool
1740 check_cxx_fundamental_alignment_constraints (tree node,
1741 unsigned align_log,
1742 int flags)
1744 bool alignment_too_large_p = false;
1745 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1746 unsigned max_align = 0;
1748 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1749 || (node == NULL_TREE || node == error_mark_node))
1750 return true;
1752 if (cxx_fundamental_alignment_p (requested_alignment))
1753 return true;
1755 if (VAR_P (node))
1757 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1758 /* For file scope variables and static members, the target supports
1759 alignments that are at most MAX_OFILE_ALIGNMENT. */
1760 max_align = MAX_OFILE_ALIGNMENT;
1761 else
1762 /* For stack variables, the target supports at most
1763 MAX_STACK_ALIGNMENT. */
1764 max_align = MAX_STACK_ALIGNMENT;
1765 if (requested_alignment > max_align)
1766 alignment_too_large_p = true;
1768 /* Let's be liberal for types and fields; don't limit their alignment any
1769 more than check_user_alignment already did. */
1771 if (alignment_too_large_p)
1772 pedwarn (input_location, OPT_Wattributes,
1773 "requested alignment %d is larger than %d",
1774 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1776 return !alignment_too_large_p;
1779 /* Common codes shared by handle_warn_if_not_aligned_attribute and
1780 handle_aligned_attribute. */
1782 static tree
1783 common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
1784 bool *no_add_attrs,
1785 bool warn_if_not_aligned_p)
1787 tree decl = NULL_TREE;
1788 tree *type = NULL;
1789 bool is_type = false;
1790 tree align_expr;
1792 /* The last (already pushed) declaration with all validated attributes
1793 merged in or the current about-to-be-pushed one if one hasn't been
1794 yet. */
1795 tree last_decl = node[1] ? node[1] : *node;
1797 if (args)
1799 align_expr = TREE_VALUE (args);
1800 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1801 && TREE_CODE (align_expr) != FUNCTION_DECL)
1802 align_expr = default_conversion (align_expr);
1804 else
1805 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1807 if (DECL_P (*node))
1809 decl = *node;
1810 type = &TREE_TYPE (decl);
1811 is_type = TREE_CODE (*node) == TYPE_DECL;
1813 else if (TYPE_P (*node))
1814 type = node, is_type = true;
1816 /* Log2 of specified alignment. */
1817 int pow2align = check_user_alignment (align_expr, true);
1818 if (pow2align == -1
1819 || !check_cxx_fundamental_alignment_constraints (*node, pow2align, flags))
1821 *no_add_attrs = true;
1822 return NULL_TREE;
1825 /* The alignment in bits corresponding to the specified alignment. */
1826 unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
1828 /* The alignment of the current declaration and that of the last
1829 pushed declaration, determined on demand below. */
1830 unsigned curalign = 0;
1831 unsigned lastalign = 0;
1833 if (is_type)
1835 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1836 /* OK, modify the type in place. */;
1837 /* If we have a TYPE_DECL, then copy the type, so that we
1838 don't accidentally modify a builtin type. See pushdecl. */
1839 else if (decl && TREE_TYPE (decl) != error_mark_node
1840 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1842 tree tt = TREE_TYPE (decl);
1843 *type = build_variant_type_copy (*type);
1844 DECL_ORIGINAL_TYPE (decl) = tt;
1845 TYPE_NAME (*type) = decl;
1846 TREE_USED (*type) = TREE_USED (decl);
1847 TREE_TYPE (decl) = *type;
1849 else
1850 *type = build_variant_type_copy (*type);
1852 if (warn_if_not_aligned_p)
1854 SET_TYPE_WARN_IF_NOT_ALIGN (*type, bitalign);
1855 warn_if_not_aligned_p = false;
1857 else
1859 SET_TYPE_ALIGN (*type, bitalign);
1860 TYPE_USER_ALIGN (*type) = 1;
1863 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1864 && TREE_CODE (decl) != FIELD_DECL)
1866 error ("alignment may not be specified for %q+D", decl);
1867 *no_add_attrs = true;
1869 else if (TREE_CODE (decl) == FUNCTION_DECL
1870 && ((curalign = DECL_ALIGN (decl)) > bitalign
1871 || ((lastalign = DECL_ALIGN (last_decl)) > bitalign)))
1873 /* Either a prior attribute on the same declaration or one
1874 on a prior declaration of the same function specifies
1875 stricter alignment than this attribute. */
1876 bool note = lastalign != 0;
1877 if (lastalign)
1878 curalign = lastalign;
1880 curalign /= BITS_PER_UNIT;
1881 bitalign /= BITS_PER_UNIT;
1883 bool diagd = true;
1884 if (DECL_USER_ALIGN (decl) || DECL_USER_ALIGN (last_decl))
1885 diagd = warning (OPT_Wattributes,
1886 "ignoring attribute %<%E (%u)%> because it conflicts "
1887 "with attribute %<%E (%u)%>",
1888 name, bitalign, name, curalign);
1889 else if (!warn_if_not_aligned_p)
1890 /* Do not error out for attribute warn_if_not_aligned. */
1891 error ("alignment for %q+D must be at least %d", decl, curalign);
1893 if (diagd && note)
1894 inform (DECL_SOURCE_LOCATION (last_decl), "previous declaration here");
1896 *no_add_attrs = true;
1898 else if (DECL_USER_ALIGN (decl)
1899 && DECL_ALIGN (decl) > bitalign)
1900 /* C++-11 [dcl.align/4]:
1902 When multiple alignment-specifiers are specified for an
1903 entity, the alignment requirement shall be set to the
1904 strictest specified alignment.
1906 This formally comes from the c++11 specification but we are
1907 doing it for the GNU attribute syntax as well. */
1908 *no_add_attrs = true;
1909 else if (!warn_if_not_aligned_p
1910 && TREE_CODE (decl) == FUNCTION_DECL
1911 && DECL_ALIGN (decl) > bitalign)
1913 /* Don't warn function alignment here if warn_if_not_aligned_p is
1914 true. It will be warned later. */
1915 if (DECL_USER_ALIGN (decl))
1916 error ("alignment for %q+D was previously specified as %d "
1917 "and may not be decreased", decl,
1918 DECL_ALIGN (decl) / BITS_PER_UNIT);
1919 else
1920 error ("alignment for %q+D must be at least %d", decl,
1921 DECL_ALIGN (decl) / BITS_PER_UNIT);
1922 *no_add_attrs = true;
1924 else
1926 if (warn_if_not_aligned_p)
1928 if (TREE_CODE (decl) == FIELD_DECL && !DECL_C_BIT_FIELD (decl))
1930 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
1931 warn_if_not_aligned_p = false;
1934 else
1936 SET_DECL_ALIGN (decl, bitalign);
1937 DECL_USER_ALIGN (decl) = 1;
1941 if (warn_if_not_aligned_p)
1943 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
1944 decl);
1945 *no_add_attrs = true;
1948 return NULL_TREE;
1951 /* Handle a "aligned" attribute; arguments as in
1952 struct attribute_spec.handler. */
1954 static tree
1955 handle_aligned_attribute (tree *node, tree name, tree args,
1956 int flags, bool *no_add_attrs)
1958 return common_handle_aligned_attribute (node, name, args, flags,
1959 no_add_attrs, false);
1962 /* Handle a "warn_if_not_aligned" attribute; arguments as in
1963 struct attribute_spec.handler. */
1965 static tree
1966 handle_warn_if_not_aligned_attribute (tree *node, tree name,
1967 tree args, int flags,
1968 bool *no_add_attrs)
1970 return common_handle_aligned_attribute (node, name, args, flags,
1971 no_add_attrs, true);
1974 /* Handle a "weak" attribute; arguments as in
1975 struct attribute_spec.handler. */
1977 static tree
1978 handle_weak_attribute (tree *node, tree name,
1979 tree ARG_UNUSED (args),
1980 int ARG_UNUSED (flags),
1981 bool * ARG_UNUSED (no_add_attrs))
1983 if (TREE_CODE (*node) == FUNCTION_DECL
1984 && DECL_DECLARED_INLINE_P (*node))
1986 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1987 *no_add_attrs = true;
1989 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1991 error ("indirect function %q+D cannot be declared weak", *node);
1992 *no_add_attrs = true;
1993 return NULL_TREE;
1995 else if (VAR_OR_FUNCTION_DECL_P (*node))
1996 declare_weak (*node);
1997 else
1998 warning (OPT_Wattributes, "%qE attribute ignored", name);
2000 return NULL_TREE;
2003 /* Handle a "noplt" attribute; arguments as in
2004 struct attribute_spec.handler. */
2006 static tree
2007 handle_noplt_attribute (tree *node, tree name,
2008 tree ARG_UNUSED (args),
2009 int ARG_UNUSED (flags),
2010 bool * ARG_UNUSED (no_add_attrs))
2012 if (TREE_CODE (*node) != FUNCTION_DECL)
2014 warning (OPT_Wattributes,
2015 "%qE attribute is only applicable on functions", name);
2016 *no_add_attrs = true;
2017 return NULL_TREE;
2019 return NULL_TREE;
2022 /* Handle an "alias" or "ifunc" attribute; arguments as in
2023 struct attribute_spec.handler, except that IS_ALIAS tells us
2024 whether this is an alias as opposed to ifunc attribute. */
2026 static tree
2027 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
2028 bool *no_add_attrs)
2030 tree decl = *node;
2032 if (TREE_CODE (decl) != FUNCTION_DECL
2033 && (!is_alias || !VAR_P (decl)))
2035 warning (OPT_Wattributes, "%qE attribute ignored", name);
2036 *no_add_attrs = true;
2038 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
2039 || (TREE_CODE (decl) != FUNCTION_DECL
2040 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
2041 /* A static variable declaration is always a tentative definition,
2042 but the alias is a non-tentative definition which overrides. */
2043 || (TREE_CODE (decl) != FUNCTION_DECL
2044 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
2046 error ("%q+D defined both normally and as %qE attribute", decl, name);
2047 *no_add_attrs = true;
2048 return NULL_TREE;
2050 else if (!is_alias
2051 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2052 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
2054 error ("weak %q+D cannot be defined %qE", decl, name);
2055 *no_add_attrs = true;
2056 return NULL_TREE;
2059 /* Note that the very first time we process a nested declaration,
2060 decl_function_context will not be set. Indeed, *would* never
2061 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2062 we do below. After such frobbery, pushdecl would set the context.
2063 In any case, this is never what we want. */
2064 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
2066 tree id;
2068 id = TREE_VALUE (args);
2069 if (TREE_CODE (id) != STRING_CST)
2071 error ("attribute %qE argument not a string", name);
2072 *no_add_attrs = true;
2073 return NULL_TREE;
2075 id = get_identifier (TREE_STRING_POINTER (id));
2076 /* This counts as a use of the object pointed to. */
2077 TREE_USED (id) = 1;
2079 if (TREE_CODE (decl) == FUNCTION_DECL)
2080 DECL_INITIAL (decl) = error_mark_node;
2081 else
2082 TREE_STATIC (decl) = 1;
2084 if (!is_alias)
2086 /* ifuncs are also aliases, so set that attribute too. */
2087 DECL_ATTRIBUTES (decl)
2088 = tree_cons (get_identifier ("alias"), args,
2089 DECL_ATTRIBUTES (decl));
2090 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
2091 NULL, DECL_ATTRIBUTES (decl));
2094 else
2096 warning (OPT_Wattributes, "%qE attribute ignored", name);
2097 *no_add_attrs = true;
2100 if (decl_in_symtab_p (*node))
2102 struct symtab_node *n = symtab_node::get (decl);
2103 if (n && n->refuse_visibility_changes)
2105 if (is_alias)
2106 error ("%+qD declared alias after being used", decl);
2107 else
2108 error ("%+qD declared ifunc after being used", decl);
2113 return NULL_TREE;
2116 /* Handle an "alias" or "ifunc" attribute; arguments as in
2117 struct attribute_spec.handler. */
2119 static tree
2120 handle_ifunc_attribute (tree *node, tree name, tree args,
2121 int ARG_UNUSED (flags), bool *no_add_attrs)
2123 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
2126 /* Handle an "alias" or "ifunc" attribute; arguments as in
2127 struct attribute_spec.handler. */
2129 static tree
2130 handle_alias_attribute (tree *node, tree name, tree args,
2131 int ARG_UNUSED (flags), bool *no_add_attrs)
2133 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2136 /* Handle a "weakref" attribute; arguments as in struct
2137 attribute_spec.handler. */
2139 static tree
2140 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2141 int flags, bool *no_add_attrs)
2143 tree attr = NULL_TREE;
2145 /* We must ignore the attribute when it is associated with
2146 local-scoped decls, since attribute alias is ignored and many
2147 such symbols do not even have a DECL_WEAK field. */
2148 if (decl_function_context (*node)
2149 || current_function_decl
2150 || !VAR_OR_FUNCTION_DECL_P (*node))
2152 warning (OPT_Wattributes, "%qE attribute ignored", name);
2153 *no_add_attrs = true;
2154 return NULL_TREE;
2157 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2159 error ("indirect function %q+D cannot be declared weakref", *node);
2160 *no_add_attrs = true;
2161 return NULL_TREE;
2164 /* The idea here is that `weakref("name")' mutates into `weakref,
2165 alias("name")', and weakref without arguments, in turn,
2166 implicitly adds weak. */
2168 if (args)
2170 attr = tree_cons (get_identifier ("alias"), args, attr);
2171 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2173 *no_add_attrs = true;
2175 decl_attributes (node, attr, flags);
2177 else
2179 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2180 error_at (DECL_SOURCE_LOCATION (*node),
2181 "weakref attribute must appear before alias attribute");
2183 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2184 and that isn't supported; and because it wants to add it to
2185 the list of weak decls, which isn't helpful. */
2186 DECL_WEAK (*node) = 1;
2189 if (decl_in_symtab_p (*node))
2191 struct symtab_node *n = symtab_node::get (*node);
2192 if (n && n->refuse_visibility_changes)
2193 error ("%+qD declared weakref after being used", *node);
2196 return NULL_TREE;
2199 /* Handle an "visibility" attribute; arguments as in
2200 struct attribute_spec.handler. */
2202 static tree
2203 handle_visibility_attribute (tree *node, tree name, tree args,
2204 int ARG_UNUSED (flags),
2205 bool *ARG_UNUSED (no_add_attrs))
2207 tree decl = *node;
2208 tree id = TREE_VALUE (args);
2209 enum symbol_visibility vis;
2211 if (TYPE_P (*node))
2213 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2214 /* OK */;
2215 else if (!RECORD_OR_UNION_TYPE_P (*node))
2217 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2218 name);
2219 return NULL_TREE;
2221 else if (TYPE_FIELDS (*node))
2223 error ("%qE attribute ignored because %qT is already defined",
2224 name, *node);
2225 return NULL_TREE;
2228 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2230 warning (OPT_Wattributes, "%qE attribute ignored", name);
2231 return NULL_TREE;
2234 if (TREE_CODE (id) != STRING_CST)
2236 error ("visibility argument not a string");
2237 return NULL_TREE;
2240 /* If this is a type, set the visibility on the type decl. */
2241 if (TYPE_P (decl))
2243 decl = TYPE_NAME (decl);
2244 if (!decl)
2245 return NULL_TREE;
2246 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2248 warning (OPT_Wattributes, "%qE attribute ignored on types",
2249 name);
2250 return NULL_TREE;
2254 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2255 vis = VISIBILITY_DEFAULT;
2256 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2257 vis = VISIBILITY_INTERNAL;
2258 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2259 vis = VISIBILITY_HIDDEN;
2260 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2261 vis = VISIBILITY_PROTECTED;
2262 else
2264 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2265 vis = VISIBILITY_DEFAULT;
2268 if (DECL_VISIBILITY_SPECIFIED (decl)
2269 && vis != DECL_VISIBILITY (decl))
2271 tree attributes = (TYPE_P (*node)
2272 ? TYPE_ATTRIBUTES (*node)
2273 : DECL_ATTRIBUTES (decl));
2274 if (lookup_attribute ("visibility", attributes))
2275 error ("%qD redeclared with different visibility", decl);
2276 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2277 && lookup_attribute ("dllimport", attributes))
2278 error ("%qD was declared %qs which implies default visibility",
2279 decl, "dllimport");
2280 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2281 && lookup_attribute ("dllexport", attributes))
2282 error ("%qD was declared %qs which implies default visibility",
2283 decl, "dllexport");
2286 DECL_VISIBILITY (decl) = vis;
2287 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2289 /* Go ahead and attach the attribute to the node as well. This is needed
2290 so we can determine whether we have VISIBILITY_DEFAULT because the
2291 visibility was not specified, or because it was explicitly overridden
2292 from the containing scope. */
2294 return NULL_TREE;
2297 /* Handle an "tls_model" attribute; arguments as in
2298 struct attribute_spec.handler. */
2300 static tree
2301 handle_tls_model_attribute (tree *node, tree name, tree args,
2302 int ARG_UNUSED (flags),
2303 bool *ARG_UNUSED (no_add_attrs))
2305 tree id;
2306 tree decl = *node;
2307 enum tls_model kind;
2309 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
2311 warning (OPT_Wattributes, "%qE attribute ignored", name);
2312 return NULL_TREE;
2315 kind = DECL_TLS_MODEL (decl);
2316 id = TREE_VALUE (args);
2317 if (TREE_CODE (id) != STRING_CST)
2319 error ("tls_model argument not a string");
2320 return NULL_TREE;
2323 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2324 kind = TLS_MODEL_LOCAL_EXEC;
2325 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2326 kind = TLS_MODEL_INITIAL_EXEC;
2327 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2328 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2329 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2330 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2331 else
2332 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2334 set_decl_tls_model (decl, kind);
2335 return NULL_TREE;
2338 /* Handle a "no_instrument_function" attribute; arguments as in
2339 struct attribute_spec.handler. */
2341 static tree
2342 handle_no_instrument_function_attribute (tree *node, tree name,
2343 tree ARG_UNUSED (args),
2344 int ARG_UNUSED (flags),
2345 bool *no_add_attrs)
2347 tree decl = *node;
2349 if (TREE_CODE (decl) != FUNCTION_DECL)
2351 error_at (DECL_SOURCE_LOCATION (decl),
2352 "%qE attribute applies only to functions", name);
2353 *no_add_attrs = true;
2355 else
2356 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2358 return NULL_TREE;
2361 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2362 struct attribute_spec.handler. */
2364 static tree
2365 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2366 int, bool *no_add_attrs)
2368 if (TREE_CODE (*node) != FUNCTION_DECL)
2370 warning (OPT_Wattributes, "%qE attribute ignored", name);
2371 *no_add_attrs = true;
2374 return NULL_TREE;
2377 /* Handle a "malloc" attribute; arguments as in
2378 struct attribute_spec.handler. */
2380 static tree
2381 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2382 int ARG_UNUSED (flags), bool *no_add_attrs)
2384 if (TREE_CODE (*node) == FUNCTION_DECL
2385 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2386 DECL_IS_MALLOC (*node) = 1;
2387 else
2389 warning (OPT_Wattributes, "%qE attribute ignored", name);
2390 *no_add_attrs = true;
2393 return NULL_TREE;
2396 /* Handle a "alloc_size" attribute; arguments as in
2397 struct attribute_spec.handler. */
2399 static tree
2400 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2401 int ARG_UNUSED (flags), bool *no_add_attrs)
2403 unsigned arg_count = type_num_arguments (*node);
2404 for (; args; args = TREE_CHAIN (args))
2406 tree position = TREE_VALUE (args);
2407 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2408 && TREE_CODE (position) != FUNCTION_DECL)
2409 position = default_conversion (position);
2411 if (!tree_fits_uhwi_p (position)
2412 || !arg_count
2413 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2415 warning (OPT_Wattributes,
2416 "alloc_size parameter outside range");
2417 *no_add_attrs = true;
2418 return NULL_TREE;
2421 return NULL_TREE;
2424 /* Handle a "alloc_align" attribute; arguments as in
2425 struct attribute_spec.handler. */
2427 static tree
2428 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2429 bool *no_add_attrs)
2431 unsigned arg_count = type_num_arguments (*node);
2432 tree position = TREE_VALUE (args);
2433 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2434 && TREE_CODE (position) != FUNCTION_DECL)
2435 position = default_conversion (position);
2437 if (!tree_fits_uhwi_p (position)
2438 || !arg_count
2439 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2441 warning (OPT_Wattributes,
2442 "alloc_align parameter outside range");
2443 *no_add_attrs = true;
2444 return NULL_TREE;
2446 return NULL_TREE;
2449 /* Handle a "assume_aligned" attribute; arguments as in
2450 struct attribute_spec.handler. */
2452 static tree
2453 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2454 bool *no_add_attrs)
2456 for (; args; args = TREE_CHAIN (args))
2458 tree position = TREE_VALUE (args);
2459 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2460 && TREE_CODE (position) != FUNCTION_DECL)
2461 position = default_conversion (position);
2463 if (TREE_CODE (position) != INTEGER_CST)
2465 warning (OPT_Wattributes,
2466 "assume_aligned parameter not integer constant");
2467 *no_add_attrs = true;
2468 return NULL_TREE;
2471 return NULL_TREE;
2474 /* Handle a "fn spec" attribute; arguments as in
2475 struct attribute_spec.handler. */
2477 static tree
2478 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2479 tree args, int ARG_UNUSED (flags),
2480 bool *no_add_attrs ATTRIBUTE_UNUSED)
2482 gcc_assert (args
2483 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2484 && !TREE_CHAIN (args));
2485 return NULL_TREE;
2488 /* Handle a "warn_unused" attribute; arguments as in
2489 struct attribute_spec.handler. */
2491 static tree
2492 handle_warn_unused_attribute (tree *node, tree name,
2493 tree args ATTRIBUTE_UNUSED,
2494 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2496 if (TYPE_P (*node))
2497 /* Do nothing else, just set the attribute. We'll get at
2498 it later with lookup_attribute. */
2500 else
2502 warning (OPT_Wattributes, "%qE attribute ignored", name);
2503 *no_add_attrs = true;
2506 return NULL_TREE;
2509 /* Handle an "omp declare simd" attribute; arguments as in
2510 struct attribute_spec.handler. */
2512 static tree
2513 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2515 return NULL_TREE;
2518 /* Handle a "simd" attribute. */
2520 static tree
2521 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2523 if (TREE_CODE (*node) == FUNCTION_DECL)
2525 tree t = get_identifier ("omp declare simd");
2526 tree attr = NULL_TREE;
2527 if (args)
2529 tree id = TREE_VALUE (args);
2531 if (TREE_CODE (id) != STRING_CST)
2533 error ("attribute %qE argument not a string", name);
2534 *no_add_attrs = true;
2535 return NULL_TREE;
2538 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2539 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2540 OMP_CLAUSE_NOTINBRANCH);
2541 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2542 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2543 OMP_CLAUSE_INBRANCH);
2544 else
2546 error ("only %<inbranch%> and %<notinbranch%> flags are "
2547 "allowed for %<__simd__%> attribute");
2548 *no_add_attrs = true;
2549 return NULL_TREE;
2553 DECL_ATTRIBUTES (*node)
2554 = tree_cons (t, build_tree_list (NULL_TREE, attr),
2555 DECL_ATTRIBUTES (*node));
2557 else
2559 warning (OPT_Wattributes, "%qE attribute ignored", name);
2560 *no_add_attrs = true;
2563 return NULL_TREE;
2566 /* Handle an "omp declare target" attribute; arguments as in
2567 struct attribute_spec.handler. */
2569 static tree
2570 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2572 return NULL_TREE;
2575 /* Handle a "returns_twice" attribute; arguments as in
2576 struct attribute_spec.handler. */
2578 static tree
2579 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2580 int ARG_UNUSED (flags), bool *no_add_attrs)
2582 if (TREE_CODE (*node) == FUNCTION_DECL)
2583 DECL_IS_RETURNS_TWICE (*node) = 1;
2584 else
2586 warning (OPT_Wattributes, "%qE attribute ignored", name);
2587 *no_add_attrs = true;
2590 return NULL_TREE;
2593 /* Handle a "no_limit_stack" attribute; arguments as in
2594 struct attribute_spec.handler. */
2596 static tree
2597 handle_no_limit_stack_attribute (tree *node, tree name,
2598 tree ARG_UNUSED (args),
2599 int ARG_UNUSED (flags),
2600 bool *no_add_attrs)
2602 tree decl = *node;
2604 if (TREE_CODE (decl) != FUNCTION_DECL)
2606 error_at (DECL_SOURCE_LOCATION (decl),
2607 "%qE attribute applies only to functions", name);
2608 *no_add_attrs = true;
2610 else if (DECL_INITIAL (decl))
2612 error_at (DECL_SOURCE_LOCATION (decl),
2613 "can%'t set %qE attribute after definition", name);
2614 *no_add_attrs = true;
2616 else
2617 DECL_NO_LIMIT_STACK (decl) = 1;
2619 return NULL_TREE;
2622 /* Handle a "pure" attribute; arguments as in
2623 struct attribute_spec.handler. */
2625 static tree
2626 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2627 int ARG_UNUSED (flags), bool *no_add_attrs)
2629 if (TREE_CODE (*node) == FUNCTION_DECL)
2631 tree type = TREE_TYPE (*node);
2632 if (VOID_TYPE_P (TREE_TYPE (type)))
2633 warning (OPT_Wattributes, "%qE attribute on function "
2634 "returning %<void%>", name);
2636 DECL_PURE_P (*node) = 1;
2637 /* ??? TODO: Support types. */
2639 else
2641 warning (OPT_Wattributes, "%qE attribute ignored", name);
2642 *no_add_attrs = true;
2645 return NULL_TREE;
2648 /* Digest an attribute list destined for a transactional memory statement.
2649 ALLOWED is the set of attributes that are allowed for this statement;
2650 return the attribute we parsed. Multiple attributes are never allowed. */
2653 parse_tm_stmt_attr (tree attrs, int allowed)
2655 tree a_seen = NULL;
2656 int m_seen = 0;
2658 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2660 tree a = TREE_PURPOSE (attrs);
2661 int m = 0;
2663 if (is_attribute_p ("outer", a))
2664 m = TM_STMT_ATTR_OUTER;
2666 if ((m & allowed) == 0)
2668 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2669 continue;
2672 if (m_seen == 0)
2674 a_seen = a;
2675 m_seen = m;
2677 else if (m_seen == m)
2678 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2679 else
2680 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2683 return m_seen;
2686 /* Transform a TM attribute name into a maskable integer and back.
2687 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2688 to how the lack of an attribute is treated. */
2691 tm_attr_to_mask (tree attr)
2693 if (attr == NULL)
2694 return 0;
2695 if (is_attribute_p ("transaction_safe", attr))
2696 return TM_ATTR_SAFE;
2697 if (is_attribute_p ("transaction_callable", attr))
2698 return TM_ATTR_CALLABLE;
2699 if (is_attribute_p ("transaction_pure", attr))
2700 return TM_ATTR_PURE;
2701 if (is_attribute_p ("transaction_unsafe", attr))
2702 return TM_ATTR_IRREVOCABLE;
2703 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2704 return TM_ATTR_MAY_CANCEL_OUTER;
2705 return 0;
2708 tree
2709 tm_mask_to_attr (int mask)
2711 const char *str;
2712 switch (mask)
2714 case TM_ATTR_SAFE:
2715 str = "transaction_safe";
2716 break;
2717 case TM_ATTR_CALLABLE:
2718 str = "transaction_callable";
2719 break;
2720 case TM_ATTR_PURE:
2721 str = "transaction_pure";
2722 break;
2723 case TM_ATTR_IRREVOCABLE:
2724 str = "transaction_unsafe";
2725 break;
2726 case TM_ATTR_MAY_CANCEL_OUTER:
2727 str = "transaction_may_cancel_outer";
2728 break;
2729 default:
2730 gcc_unreachable ();
2732 return get_identifier (str);
2735 /* Return the first TM attribute seen in LIST. */
2737 tree
2738 find_tm_attribute (tree list)
2740 for (; list ; list = TREE_CHAIN (list))
2742 tree name = TREE_PURPOSE (list);
2743 if (tm_attr_to_mask (name) != 0)
2744 return name;
2746 return NULL_TREE;
2749 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2750 Here we accept only function types, and verify that none of the other
2751 function TM attributes are also applied. */
2752 /* ??? We need to accept class types for C++, but not C. This greatly
2753 complicates this function, since we can no longer rely on the extra
2754 processing given by function_type_required. */
2756 static tree
2757 handle_tm_attribute (tree *node, tree name, tree args,
2758 int flags, bool *no_add_attrs)
2760 /* Only one path adds the attribute; others don't. */
2761 *no_add_attrs = true;
2763 switch (TREE_CODE (*node))
2765 case RECORD_TYPE:
2766 case UNION_TYPE:
2767 /* Only tm_callable and tm_safe apply to classes. */
2768 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2769 goto ignored;
2770 /* FALLTHRU */
2772 case FUNCTION_TYPE:
2773 case METHOD_TYPE:
2775 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2776 if (old_name == name)
2778 else if (old_name != NULL_TREE)
2779 error ("type was previously declared %qE", old_name);
2780 else
2781 *no_add_attrs = false;
2783 break;
2785 case FUNCTION_DECL:
2787 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2788 want to set transaction_safe on the type. */
2789 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2790 if (!TYPE_P (DECL_CONTEXT (*node)))
2791 error_at (DECL_SOURCE_LOCATION (*node),
2792 "%<transaction_safe_dynamic%> may only be specified for "
2793 "a virtual function");
2794 *no_add_attrs = false;
2795 decl_attributes (&TREE_TYPE (*node),
2796 build_tree_list (get_identifier ("transaction_safe"),
2797 NULL_TREE),
2799 break;
2802 case POINTER_TYPE:
2804 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2805 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2807 tree fn_tmp = TREE_TYPE (*node);
2808 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2809 *node = build_pointer_type (fn_tmp);
2810 break;
2813 /* FALLTHRU */
2815 default:
2816 /* If a function is next, pass it on to be tried next. */
2817 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2818 return tree_cons (name, args, NULL);
2820 ignored:
2821 warning (OPT_Wattributes, "%qE attribute ignored", name);
2822 break;
2825 return NULL_TREE;
2828 /* Handle the TM_WRAP attribute; arguments as in
2829 struct attribute_spec.handler. */
2831 static tree
2832 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2833 int ARG_UNUSED (flags), bool *no_add_attrs)
2835 tree decl = *node;
2837 /* We don't need the attribute even on success, since we
2838 record the entry in an external table. */
2839 *no_add_attrs = true;
2841 if (TREE_CODE (decl) != FUNCTION_DECL)
2842 warning (OPT_Wattributes, "%qE attribute ignored", name);
2843 else
2845 tree wrap_decl = TREE_VALUE (args);
2846 if (error_operand_p (wrap_decl))
2848 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2849 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2850 error ("%qE argument not an identifier", name);
2851 else
2853 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2854 wrap_decl = lookup_name (wrap_decl);
2855 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2857 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2858 TREE_TYPE (wrap_decl)))
2859 record_tm_replacement (wrap_decl, decl);
2860 else
2861 error ("%qD is not compatible with %qD", wrap_decl, decl);
2863 else
2864 error ("%qE argument is not a function", name);
2868 return NULL_TREE;
2871 /* Ignore the given attribute. Used when this attribute may be usefully
2872 overridden by the target, but is not used generically. */
2874 static tree
2875 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2876 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2877 bool *no_add_attrs)
2879 *no_add_attrs = true;
2880 return NULL_TREE;
2883 /* Handle a "no vops" attribute; arguments as in
2884 struct attribute_spec.handler. */
2886 static tree
2887 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2888 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2889 bool *ARG_UNUSED (no_add_attrs))
2891 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2892 DECL_IS_NOVOPS (*node) = 1;
2893 return NULL_TREE;
2896 /* Handle a "deprecated" attribute; arguments as in
2897 struct attribute_spec.handler. */
2899 static tree
2900 handle_deprecated_attribute (tree *node, tree name,
2901 tree args, int flags,
2902 bool *no_add_attrs)
2904 tree type = NULL_TREE;
2905 int warn = 0;
2906 tree what = NULL_TREE;
2908 if (!args)
2909 *no_add_attrs = true;
2910 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2912 error ("deprecated message is not a string");
2913 *no_add_attrs = true;
2916 if (DECL_P (*node))
2918 tree decl = *node;
2919 type = TREE_TYPE (decl);
2921 if (TREE_CODE (decl) == TYPE_DECL
2922 || TREE_CODE (decl) == PARM_DECL
2923 || VAR_OR_FUNCTION_DECL_P (decl)
2924 || TREE_CODE (decl) == FIELD_DECL
2925 || TREE_CODE (decl) == CONST_DECL
2926 || objc_method_decl (TREE_CODE (decl)))
2927 TREE_DEPRECATED (decl) = 1;
2928 else
2929 warn = 1;
2931 else if (TYPE_P (*node))
2933 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2934 *node = build_variant_type_copy (*node);
2935 TREE_DEPRECATED (*node) = 1;
2936 type = *node;
2938 else
2939 warn = 1;
2941 if (warn)
2943 *no_add_attrs = true;
2944 if (type && TYPE_NAME (type))
2946 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2947 what = TYPE_NAME (*node);
2948 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2949 && DECL_NAME (TYPE_NAME (type)))
2950 what = DECL_NAME (TYPE_NAME (type));
2952 if (what)
2953 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2954 else
2955 warning (OPT_Wattributes, "%qE attribute ignored", name);
2958 return NULL_TREE;
2961 /* Handle a "vector_size" attribute; arguments as in
2962 struct attribute_spec.handler. */
2964 static tree
2965 handle_vector_size_attribute (tree *node, tree name, tree args,
2966 int ARG_UNUSED (flags),
2967 bool *no_add_attrs)
2969 unsigned HOST_WIDE_INT vecsize, nunits;
2970 machine_mode orig_mode;
2971 tree type = *node, new_type, size;
2973 *no_add_attrs = true;
2975 size = TREE_VALUE (args);
2976 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2977 && TREE_CODE (size) != FUNCTION_DECL)
2978 size = default_conversion (size);
2980 if (!tree_fits_uhwi_p (size))
2982 warning (OPT_Wattributes, "%qE attribute ignored", name);
2983 return NULL_TREE;
2986 /* Get the vector size (in bytes). */
2987 vecsize = tree_to_uhwi (size);
2989 /* We need to provide for vector pointers, vector arrays, and
2990 functions returning vectors. For example:
2992 __attribute__((vector_size(16))) short *foo;
2994 In this case, the mode is SI, but the type being modified is
2995 HI, so we need to look further. */
2997 while (POINTER_TYPE_P (type)
2998 || TREE_CODE (type) == FUNCTION_TYPE
2999 || TREE_CODE (type) == METHOD_TYPE
3000 || TREE_CODE (type) == ARRAY_TYPE
3001 || TREE_CODE (type) == OFFSET_TYPE)
3002 type = TREE_TYPE (type);
3004 /* Get the mode of the type being modified. */
3005 orig_mode = TYPE_MODE (type);
3007 if ((!INTEGRAL_TYPE_P (type)
3008 && !SCALAR_FLOAT_TYPE_P (type)
3009 && !FIXED_POINT_TYPE_P (type))
3010 || (!SCALAR_FLOAT_MODE_P (orig_mode)
3011 && GET_MODE_CLASS (orig_mode) != MODE_INT
3012 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
3013 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
3014 || TREE_CODE (type) == BOOLEAN_TYPE)
3016 error ("invalid vector type for attribute %qE", name);
3017 return NULL_TREE;
3020 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
3022 error ("vector size not an integral multiple of component size");
3023 return NULL;
3026 if (vecsize == 0)
3028 error ("zero vector size");
3029 return NULL;
3032 /* Calculate how many units fit in the vector. */
3033 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
3034 if (nunits & (nunits - 1))
3036 error ("number of components of the vector not a power of two");
3037 return NULL_TREE;
3040 new_type = build_vector_type (type, nunits);
3042 /* Build back pointers if needed. */
3043 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
3045 return NULL_TREE;
3048 /* Handle the "nonnull" attribute. */
3050 static tree
3051 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
3052 tree args, int ARG_UNUSED (flags),
3053 bool *no_add_attrs)
3055 tree type = *node;
3056 unsigned HOST_WIDE_INT attr_arg_num;
3058 /* If no arguments are specified, all pointer arguments should be
3059 non-null. Verify a full prototype is given so that the arguments
3060 will have the correct types when we actually check them later.
3061 Avoid diagnosing type-generic built-ins since those have no
3062 prototype. */
3063 if (!args)
3065 if (!prototype_p (type)
3066 && (!TYPE_ATTRIBUTES (type)
3067 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
3069 error ("nonnull attribute without arguments on a non-prototype");
3070 *no_add_attrs = true;
3072 return NULL_TREE;
3075 /* Argument list specified. Verify that each argument number references
3076 a pointer argument. */
3077 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
3079 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
3081 tree arg = TREE_VALUE (args);
3082 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
3083 && TREE_CODE (arg) != FUNCTION_DECL)
3084 TREE_VALUE (args) = arg = default_conversion (arg);
3086 if (!get_nonnull_operand (arg, &arg_num))
3088 error ("nonnull argument has invalid operand number (argument %lu)",
3089 (unsigned long) attr_arg_num);
3090 *no_add_attrs = true;
3091 return NULL_TREE;
3094 if (prototype_p (type))
3096 function_args_iterator iter;
3097 tree argument;
3099 function_args_iter_init (&iter, type);
3100 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
3102 argument = function_args_iter_cond (&iter);
3103 if (argument == NULL_TREE || ck_num == arg_num)
3104 break;
3107 if (!argument
3108 || TREE_CODE (argument) == VOID_TYPE)
3110 error ("nonnull argument with out-of-range operand number "
3111 "(argument %lu, operand %lu)",
3112 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3113 *no_add_attrs = true;
3114 return NULL_TREE;
3117 if (TREE_CODE (argument) != POINTER_TYPE)
3119 error ("nonnull argument references non-pointer operand "
3120 "(argument %lu, operand %lu)",
3121 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3122 *no_add_attrs = true;
3123 return NULL_TREE;
3128 return NULL_TREE;
3131 /* Handle the "nonstring" variable attribute. */
3133 static tree
3134 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3135 int ARG_UNUSED (flags), bool *no_add_attrs)
3137 gcc_assert (!args);
3138 tree_code code = TREE_CODE (*node);
3140 if (VAR_P (*node)
3141 || code == FIELD_DECL
3142 || code == PARM_DECL)
3144 tree type = TREE_TYPE (*node);
3146 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3148 /* Accept the attribute on arrays and pointers to all three
3149 narrow character types. */
3150 tree eltype = TREE_TYPE (type);
3151 eltype = TYPE_MAIN_VARIANT (eltype);
3152 if (eltype == char_type_node
3153 || eltype == signed_char_type_node
3154 || eltype == unsigned_char_type_node)
3155 return NULL_TREE;
3158 warning (OPT_Wattributes,
3159 "%qE attribute ignored on objects of type %qT",
3160 name, type);
3161 *no_add_attrs = true;
3162 return NULL_TREE;
3165 if (code == FUNCTION_DECL)
3166 warning (OPT_Wattributes,
3167 "%qE attribute does not apply to functions", name);
3168 else if (code == TYPE_DECL)
3169 warning (OPT_Wattributes,
3170 "%qE attribute does not apply to types", name);
3171 else
3172 warning (OPT_Wattributes, "%qE attribute ignored", name);
3174 *no_add_attrs = true;
3175 return NULL_TREE;
3178 /* Handle a "nothrow" attribute; arguments as in
3179 struct attribute_spec.handler. */
3181 static tree
3182 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3183 int ARG_UNUSED (flags), bool *no_add_attrs)
3185 if (TREE_CODE (*node) == FUNCTION_DECL)
3186 TREE_NOTHROW (*node) = 1;
3187 /* ??? TODO: Support types. */
3188 else
3190 warning (OPT_Wattributes, "%qE attribute ignored", name);
3191 *no_add_attrs = true;
3194 return NULL_TREE;
3197 /* Handle a "cleanup" attribute; arguments as in
3198 struct attribute_spec.handler. */
3200 static tree
3201 handle_cleanup_attribute (tree *node, tree name, tree args,
3202 int ARG_UNUSED (flags), bool *no_add_attrs)
3204 tree decl = *node;
3205 tree cleanup_id, cleanup_decl;
3207 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3208 for global destructors in C++. This requires infrastructure that
3209 we don't have generically at the moment. It's also not a feature
3210 we'd be missing too much, since we do have attribute constructor. */
3211 if (!VAR_P (decl) || TREE_STATIC (decl))
3213 warning (OPT_Wattributes, "%qE attribute ignored", name);
3214 *no_add_attrs = true;
3215 return NULL_TREE;
3218 /* Verify that the argument is a function in scope. */
3219 /* ??? We could support pointers to functions here as well, if
3220 that was considered desirable. */
3221 cleanup_id = TREE_VALUE (args);
3222 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3224 error ("cleanup argument not an identifier");
3225 *no_add_attrs = true;
3226 return NULL_TREE;
3228 cleanup_decl = lookup_name (cleanup_id);
3229 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3231 error ("cleanup argument not a function");
3232 *no_add_attrs = true;
3233 return NULL_TREE;
3236 /* That the function has proper type is checked with the
3237 eventual call to build_function_call. */
3239 return NULL_TREE;
3242 /* Handle a "warn_unused_result" attribute. No special handling. */
3244 static tree
3245 handle_warn_unused_result_attribute (tree *node, tree name,
3246 tree ARG_UNUSED (args),
3247 int ARG_UNUSED (flags), bool *no_add_attrs)
3249 /* Ignore the attribute for functions not returning any value. */
3250 if (VOID_TYPE_P (TREE_TYPE (*node)))
3252 warning (OPT_Wattributes, "%qE attribute ignored", name);
3253 *no_add_attrs = true;
3256 return NULL_TREE;
3259 /* Handle a "sentinel" attribute. */
3261 static tree
3262 handle_sentinel_attribute (tree *node, tree name, tree args,
3263 int ARG_UNUSED (flags), bool *no_add_attrs)
3265 if (!prototype_p (*node))
3267 warning (OPT_Wattributes,
3268 "%qE attribute requires prototypes with named arguments", name);
3269 *no_add_attrs = true;
3271 else
3273 if (!stdarg_p (*node))
3275 warning (OPT_Wattributes,
3276 "%qE attribute only applies to variadic functions", name);
3277 *no_add_attrs = true;
3281 if (args)
3283 tree position = TREE_VALUE (args);
3284 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3285 && TREE_CODE (position) != FUNCTION_DECL)
3286 position = default_conversion (position);
3288 if (TREE_CODE (position) != INTEGER_CST
3289 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3291 warning (OPT_Wattributes,
3292 "requested position is not an integer constant");
3293 *no_add_attrs = true;
3295 else
3297 if (tree_int_cst_lt (position, integer_zero_node))
3299 warning (OPT_Wattributes,
3300 "requested position is less than zero");
3301 *no_add_attrs = true;
3306 return NULL_TREE;
3309 /* Handle a "type_generic" attribute. */
3311 static tree
3312 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3313 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3314 bool * ARG_UNUSED (no_add_attrs))
3316 /* Ensure we have a function type. */
3317 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3319 /* Ensure we have a variadic function. */
3320 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3322 return NULL_TREE;
3325 /* Handle a "target" attribute. */
3327 static tree
3328 handle_target_attribute (tree *node, tree name, tree args, int flags,
3329 bool *no_add_attrs)
3331 /* Ensure we have a function type. */
3332 if (TREE_CODE (*node) != FUNCTION_DECL)
3334 warning (OPT_Wattributes, "%qE attribute ignored", name);
3335 *no_add_attrs = true;
3337 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3339 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3340 "with %qs attribute", name, "target_clones");
3341 *no_add_attrs = true;
3343 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3344 flags))
3345 *no_add_attrs = true;
3347 /* Check that there's no empty string in values of the attribute. */
3348 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3350 tree value = TREE_VALUE (t);
3351 if (TREE_CODE (value) == STRING_CST
3352 && TREE_STRING_LENGTH (value) == 1
3353 && TREE_STRING_POINTER (value)[0] == '\0')
3355 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3356 *no_add_attrs = true;
3360 return NULL_TREE;
3363 /* Handle a "target_clones" attribute. */
3365 static tree
3366 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3367 int ARG_UNUSED (flags), bool *no_add_attrs)
3369 /* Ensure we have a function type. */
3370 if (TREE_CODE (*node) == FUNCTION_DECL)
3372 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3374 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3375 "with %qs attribute", name, "always_inline");
3376 *no_add_attrs = true;
3378 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3380 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3381 "with %qs attribute", name, "target");
3382 *no_add_attrs = true;
3384 else
3385 /* Do not inline functions with multiple clone targets. */
3386 DECL_UNINLINABLE (*node) = 1;
3388 else
3390 warning (OPT_Wattributes, "%qE attribute ignored", name);
3391 *no_add_attrs = true;
3393 return NULL_TREE;
3396 /* For handling "optimize" attribute. arguments as in
3397 struct attribute_spec.handler. */
3399 static tree
3400 handle_optimize_attribute (tree *node, tree name, tree args,
3401 int ARG_UNUSED (flags), bool *no_add_attrs)
3403 /* Ensure we have a function type. */
3404 if (TREE_CODE (*node) != FUNCTION_DECL)
3406 warning (OPT_Wattributes, "%qE attribute ignored", name);
3407 *no_add_attrs = true;
3409 else
3411 struct cl_optimization cur_opts;
3412 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3414 /* Save current options. */
3415 cl_optimization_save (&cur_opts, &global_options);
3417 /* If we previously had some optimization options, use them as the
3418 default. */
3419 if (old_opts)
3420 cl_optimization_restore (&global_options,
3421 TREE_OPTIMIZATION (old_opts));
3423 /* Parse options, and update the vector. */
3424 parse_optimize_options (args, true);
3425 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3426 = build_optimization_node (&global_options);
3428 /* Restore current options. */
3429 cl_optimization_restore (&global_options, &cur_opts);
3432 return NULL_TREE;
3435 /* Handle a "no_split_stack" attribute. */
3437 static tree
3438 handle_no_split_stack_attribute (tree *node, tree name,
3439 tree ARG_UNUSED (args),
3440 int ARG_UNUSED (flags),
3441 bool *no_add_attrs)
3443 tree decl = *node;
3445 if (TREE_CODE (decl) != FUNCTION_DECL)
3447 error_at (DECL_SOURCE_LOCATION (decl),
3448 "%qE attribute applies only to functions", name);
3449 *no_add_attrs = true;
3451 else if (DECL_INITIAL (decl))
3453 error_at (DECL_SOURCE_LOCATION (decl),
3454 "can%'t set %qE attribute after definition", name);
3455 *no_add_attrs = true;
3458 return NULL_TREE;
3461 /* Handle a "returns_nonnull" attribute; arguments as in
3462 struct attribute_spec.handler. */
3464 static tree
3465 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3466 bool *no_add_attrs)
3468 // Even without a prototype we still have a return type we can check.
3469 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3471 error ("returns_nonnull attribute on a function not returning a pointer");
3472 *no_add_attrs = true;
3474 return NULL_TREE;
3477 /* Handle a "designated_init" attribute; arguments as in
3478 struct attribute_spec.handler. */
3480 static tree
3481 handle_designated_init_attribute (tree *node, tree name, tree, int,
3482 bool *no_add_attrs)
3484 if (TREE_CODE (*node) != RECORD_TYPE)
3486 error ("%qE attribute is only valid on %<struct%> type", name);
3487 *no_add_attrs = true;
3489 return NULL_TREE;
3493 /* Handle a "fallthrough" attribute; arguments as in struct
3494 attribute_spec.handler. */
3496 static tree
3497 handle_fallthrough_attribute (tree *, tree name, tree, int,
3498 bool *no_add_attrs)
3500 warning (OPT_Wattributes, "%qE attribute ignored", name);
3501 *no_add_attrs = true;
3502 return NULL_TREE;
3505 static tree
3506 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3508 /* Nothing to be done here. */
3509 return NULL_TREE;