Fix compilation failure with C++98 compilers
[official-gcc.git] / gcc / c-family / c-attribs.c
blob4416b5042f7592762b2850cfed33bbc6509626b9
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 (OPT_Wprio_ctor_dtor,
1354 "destructor priorities from 0 to %d are reserved "
1355 "for the implementation",
1356 MAX_RESERVED_INIT_PRIORITY);
1357 else
1358 warning (OPT_Wprio_ctor_dtor,
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 auto_diagnostic_group d;
1885 if (DECL_USER_ALIGN (decl) || DECL_USER_ALIGN (last_decl))
1886 diagd = warning (OPT_Wattributes,
1887 "ignoring attribute %<%E (%u)%> because it conflicts "
1888 "with attribute %<%E (%u)%>",
1889 name, bitalign, name, curalign);
1890 else if (!warn_if_not_aligned_p)
1891 /* Do not error out for attribute warn_if_not_aligned. */
1892 error ("alignment for %q+D must be at least %d", decl, curalign);
1894 if (diagd && note)
1895 inform (DECL_SOURCE_LOCATION (last_decl), "previous declaration here");
1897 *no_add_attrs = true;
1899 else if (DECL_USER_ALIGN (decl)
1900 && DECL_ALIGN (decl) > bitalign)
1901 /* C++-11 [dcl.align/4]:
1903 When multiple alignment-specifiers are specified for an
1904 entity, the alignment requirement shall be set to the
1905 strictest specified alignment.
1907 This formally comes from the c++11 specification but we are
1908 doing it for the GNU attribute syntax as well. */
1909 *no_add_attrs = true;
1910 else if (!warn_if_not_aligned_p
1911 && TREE_CODE (decl) == FUNCTION_DECL
1912 && DECL_ALIGN (decl) > bitalign)
1914 /* Don't warn function alignment here if warn_if_not_aligned_p is
1915 true. It will be warned later. */
1916 if (DECL_USER_ALIGN (decl))
1917 error ("alignment for %q+D was previously specified as %d "
1918 "and may not be decreased", decl,
1919 DECL_ALIGN (decl) / BITS_PER_UNIT);
1920 else
1921 error ("alignment for %q+D must be at least %d", decl,
1922 DECL_ALIGN (decl) / BITS_PER_UNIT);
1923 *no_add_attrs = true;
1925 else
1927 if (warn_if_not_aligned_p)
1929 if (TREE_CODE (decl) == FIELD_DECL && !DECL_C_BIT_FIELD (decl))
1931 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
1932 warn_if_not_aligned_p = false;
1935 else
1937 SET_DECL_ALIGN (decl, bitalign);
1938 DECL_USER_ALIGN (decl) = 1;
1942 if (warn_if_not_aligned_p)
1944 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
1945 decl);
1946 *no_add_attrs = true;
1949 return NULL_TREE;
1952 /* Handle a "aligned" attribute; arguments as in
1953 struct attribute_spec.handler. */
1955 static tree
1956 handle_aligned_attribute (tree *node, tree name, tree args,
1957 int flags, bool *no_add_attrs)
1959 return common_handle_aligned_attribute (node, name, args, flags,
1960 no_add_attrs, false);
1963 /* Handle a "warn_if_not_aligned" attribute; arguments as in
1964 struct attribute_spec.handler. */
1966 static tree
1967 handle_warn_if_not_aligned_attribute (tree *node, tree name,
1968 tree args, int flags,
1969 bool *no_add_attrs)
1971 return common_handle_aligned_attribute (node, name, args, flags,
1972 no_add_attrs, true);
1975 /* Handle a "weak" attribute; arguments as in
1976 struct attribute_spec.handler. */
1978 static tree
1979 handle_weak_attribute (tree *node, tree name,
1980 tree ARG_UNUSED (args),
1981 int ARG_UNUSED (flags),
1982 bool * ARG_UNUSED (no_add_attrs))
1984 if (TREE_CODE (*node) == FUNCTION_DECL
1985 && DECL_DECLARED_INLINE_P (*node))
1987 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1988 *no_add_attrs = true;
1990 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1992 error ("indirect function %q+D cannot be declared weak", *node);
1993 *no_add_attrs = true;
1994 return NULL_TREE;
1996 else if (VAR_OR_FUNCTION_DECL_P (*node))
1997 declare_weak (*node);
1998 else
1999 warning (OPT_Wattributes, "%qE attribute ignored", name);
2001 return NULL_TREE;
2004 /* Handle a "noplt" attribute; arguments as in
2005 struct attribute_spec.handler. */
2007 static tree
2008 handle_noplt_attribute (tree *node, tree name,
2009 tree ARG_UNUSED (args),
2010 int ARG_UNUSED (flags),
2011 bool * ARG_UNUSED (no_add_attrs))
2013 if (TREE_CODE (*node) != FUNCTION_DECL)
2015 warning (OPT_Wattributes,
2016 "%qE attribute is only applicable on functions", name);
2017 *no_add_attrs = true;
2018 return NULL_TREE;
2020 return NULL_TREE;
2023 /* Handle an "alias" or "ifunc" attribute; arguments as in
2024 struct attribute_spec.handler, except that IS_ALIAS tells us
2025 whether this is an alias as opposed to ifunc attribute. */
2027 static tree
2028 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
2029 bool *no_add_attrs)
2031 tree decl = *node;
2033 if (TREE_CODE (decl) != FUNCTION_DECL
2034 && (!is_alias || !VAR_P (decl)))
2036 warning (OPT_Wattributes, "%qE attribute ignored", name);
2037 *no_add_attrs = true;
2039 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
2040 || (TREE_CODE (decl) != FUNCTION_DECL
2041 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
2042 /* A static variable declaration is always a tentative definition,
2043 but the alias is a non-tentative definition which overrides. */
2044 || (TREE_CODE (decl) != FUNCTION_DECL
2045 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
2047 error ("%q+D defined both normally and as %qE attribute", decl, name);
2048 *no_add_attrs = true;
2049 return NULL_TREE;
2051 else if (!is_alias
2052 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2053 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
2055 error ("weak %q+D cannot be defined %qE", decl, name);
2056 *no_add_attrs = true;
2057 return NULL_TREE;
2060 /* Note that the very first time we process a nested declaration,
2061 decl_function_context will not be set. Indeed, *would* never
2062 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2063 we do below. After such frobbery, pushdecl would set the context.
2064 In any case, this is never what we want. */
2065 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
2067 tree id;
2069 id = TREE_VALUE (args);
2070 if (TREE_CODE (id) != STRING_CST)
2072 error ("attribute %qE argument not a string", name);
2073 *no_add_attrs = true;
2074 return NULL_TREE;
2076 id = get_identifier (TREE_STRING_POINTER (id));
2077 /* This counts as a use of the object pointed to. */
2078 TREE_USED (id) = 1;
2080 if (TREE_CODE (decl) == FUNCTION_DECL)
2081 DECL_INITIAL (decl) = error_mark_node;
2082 else
2083 TREE_STATIC (decl) = 1;
2085 if (!is_alias)
2087 /* ifuncs are also aliases, so set that attribute too. */
2088 DECL_ATTRIBUTES (decl)
2089 = tree_cons (get_identifier ("alias"), args,
2090 DECL_ATTRIBUTES (decl));
2091 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
2092 NULL, DECL_ATTRIBUTES (decl));
2095 else
2097 warning (OPT_Wattributes, "%qE attribute ignored", name);
2098 *no_add_attrs = true;
2101 if (decl_in_symtab_p (*node))
2103 struct symtab_node *n = symtab_node::get (decl);
2104 if (n && n->refuse_visibility_changes)
2106 if (is_alias)
2107 error ("%+qD declared alias after being used", decl);
2108 else
2109 error ("%+qD declared ifunc after being used", decl);
2114 return NULL_TREE;
2117 /* Handle an "alias" or "ifunc" attribute; arguments as in
2118 struct attribute_spec.handler. */
2120 static tree
2121 handle_ifunc_attribute (tree *node, tree name, tree args,
2122 int ARG_UNUSED (flags), bool *no_add_attrs)
2124 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
2127 /* Handle an "alias" or "ifunc" attribute; arguments as in
2128 struct attribute_spec.handler. */
2130 static tree
2131 handle_alias_attribute (tree *node, tree name, tree args,
2132 int ARG_UNUSED (flags), bool *no_add_attrs)
2134 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2137 /* Handle a "weakref" attribute; arguments as in struct
2138 attribute_spec.handler. */
2140 static tree
2141 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2142 int flags, bool *no_add_attrs)
2144 tree attr = NULL_TREE;
2146 /* We must ignore the attribute when it is associated with
2147 local-scoped decls, since attribute alias is ignored and many
2148 such symbols do not even have a DECL_WEAK field. */
2149 if (decl_function_context (*node)
2150 || current_function_decl
2151 || !VAR_OR_FUNCTION_DECL_P (*node))
2153 warning (OPT_Wattributes, "%qE attribute ignored", name);
2154 *no_add_attrs = true;
2155 return NULL_TREE;
2158 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2160 error ("indirect function %q+D cannot be declared weakref", *node);
2161 *no_add_attrs = true;
2162 return NULL_TREE;
2165 /* The idea here is that `weakref("name")' mutates into `weakref,
2166 alias("name")', and weakref without arguments, in turn,
2167 implicitly adds weak. */
2169 if (args)
2171 attr = tree_cons (get_identifier ("alias"), args, attr);
2172 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2174 *no_add_attrs = true;
2176 decl_attributes (node, attr, flags);
2178 else
2180 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2181 error_at (DECL_SOURCE_LOCATION (*node),
2182 "weakref attribute must appear before alias attribute");
2184 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2185 and that isn't supported; and because it wants to add it to
2186 the list of weak decls, which isn't helpful. */
2187 DECL_WEAK (*node) = 1;
2190 if (decl_in_symtab_p (*node))
2192 struct symtab_node *n = symtab_node::get (*node);
2193 if (n && n->refuse_visibility_changes)
2194 error ("%+qD declared weakref after being used", *node);
2197 return NULL_TREE;
2200 /* Handle an "visibility" attribute; arguments as in
2201 struct attribute_spec.handler. */
2203 static tree
2204 handle_visibility_attribute (tree *node, tree name, tree args,
2205 int ARG_UNUSED (flags),
2206 bool *ARG_UNUSED (no_add_attrs))
2208 tree decl = *node;
2209 tree id = TREE_VALUE (args);
2210 enum symbol_visibility vis;
2212 if (TYPE_P (*node))
2214 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2215 /* OK */;
2216 else if (!RECORD_OR_UNION_TYPE_P (*node))
2218 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2219 name);
2220 return NULL_TREE;
2222 else if (TYPE_FIELDS (*node))
2224 error ("%qE attribute ignored because %qT is already defined",
2225 name, *node);
2226 return NULL_TREE;
2229 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2231 warning (OPT_Wattributes, "%qE attribute ignored", name);
2232 return NULL_TREE;
2235 if (TREE_CODE (id) != STRING_CST)
2237 error ("visibility argument not a string");
2238 return NULL_TREE;
2241 /* If this is a type, set the visibility on the type decl. */
2242 if (TYPE_P (decl))
2244 decl = TYPE_NAME (decl);
2245 if (!decl)
2246 return NULL_TREE;
2247 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2249 warning (OPT_Wattributes, "%qE attribute ignored on types",
2250 name);
2251 return NULL_TREE;
2255 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2256 vis = VISIBILITY_DEFAULT;
2257 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2258 vis = VISIBILITY_INTERNAL;
2259 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2260 vis = VISIBILITY_HIDDEN;
2261 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2262 vis = VISIBILITY_PROTECTED;
2263 else
2265 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2266 vis = VISIBILITY_DEFAULT;
2269 if (DECL_VISIBILITY_SPECIFIED (decl)
2270 && vis != DECL_VISIBILITY (decl))
2272 tree attributes = (TYPE_P (*node)
2273 ? TYPE_ATTRIBUTES (*node)
2274 : DECL_ATTRIBUTES (decl));
2275 if (lookup_attribute ("visibility", attributes))
2276 error ("%qD redeclared with different visibility", decl);
2277 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2278 && lookup_attribute ("dllimport", attributes))
2279 error ("%qD was declared %qs which implies default visibility",
2280 decl, "dllimport");
2281 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2282 && lookup_attribute ("dllexport", attributes))
2283 error ("%qD was declared %qs which implies default visibility",
2284 decl, "dllexport");
2287 DECL_VISIBILITY (decl) = vis;
2288 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2290 /* Go ahead and attach the attribute to the node as well. This is needed
2291 so we can determine whether we have VISIBILITY_DEFAULT because the
2292 visibility was not specified, or because it was explicitly overridden
2293 from the containing scope. */
2295 return NULL_TREE;
2298 /* Handle an "tls_model" attribute; arguments as in
2299 struct attribute_spec.handler. */
2301 static tree
2302 handle_tls_model_attribute (tree *node, tree name, tree args,
2303 int ARG_UNUSED (flags),
2304 bool *ARG_UNUSED (no_add_attrs))
2306 tree id;
2307 tree decl = *node;
2308 enum tls_model kind;
2310 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
2312 warning (OPT_Wattributes, "%qE attribute ignored", name);
2313 return NULL_TREE;
2316 kind = DECL_TLS_MODEL (decl);
2317 id = TREE_VALUE (args);
2318 if (TREE_CODE (id) != STRING_CST)
2320 error ("tls_model argument not a string");
2321 return NULL_TREE;
2324 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2325 kind = TLS_MODEL_LOCAL_EXEC;
2326 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2327 kind = TLS_MODEL_INITIAL_EXEC;
2328 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2329 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2330 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2331 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2332 else
2333 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2335 set_decl_tls_model (decl, kind);
2336 return NULL_TREE;
2339 /* Handle a "no_instrument_function" attribute; arguments as in
2340 struct attribute_spec.handler. */
2342 static tree
2343 handle_no_instrument_function_attribute (tree *node, tree name,
2344 tree ARG_UNUSED (args),
2345 int ARG_UNUSED (flags),
2346 bool *no_add_attrs)
2348 tree decl = *node;
2350 if (TREE_CODE (decl) != FUNCTION_DECL)
2352 error_at (DECL_SOURCE_LOCATION (decl),
2353 "%qE attribute applies only to functions", name);
2354 *no_add_attrs = true;
2356 else
2357 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2359 return NULL_TREE;
2362 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2363 struct attribute_spec.handler. */
2365 static tree
2366 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2367 int, bool *no_add_attrs)
2369 if (TREE_CODE (*node) != FUNCTION_DECL)
2371 warning (OPT_Wattributes, "%qE attribute ignored", name);
2372 *no_add_attrs = true;
2375 return NULL_TREE;
2378 /* Handle a "malloc" attribute; arguments as in
2379 struct attribute_spec.handler. */
2381 static tree
2382 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2383 int ARG_UNUSED (flags), bool *no_add_attrs)
2385 if (TREE_CODE (*node) == FUNCTION_DECL
2386 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2387 DECL_IS_MALLOC (*node) = 1;
2388 else
2390 warning (OPT_Wattributes, "%qE attribute ignored", name);
2391 *no_add_attrs = true;
2394 return NULL_TREE;
2397 /* Handle a "alloc_size" attribute; arguments as in
2398 struct attribute_spec.handler. */
2400 static tree
2401 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2402 int ARG_UNUSED (flags), bool *no_add_attrs)
2404 unsigned arg_count = type_num_arguments (*node);
2405 for (; args; args = TREE_CHAIN (args))
2407 tree position = TREE_VALUE (args);
2408 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2409 && TREE_CODE (position) != FUNCTION_DECL)
2410 position = default_conversion (position);
2412 if (!tree_fits_uhwi_p (position)
2413 || !arg_count
2414 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2416 warning (OPT_Wattributes,
2417 "alloc_size parameter outside range");
2418 *no_add_attrs = true;
2419 return NULL_TREE;
2422 return NULL_TREE;
2425 /* Handle a "alloc_align" attribute; arguments as in
2426 struct attribute_spec.handler. */
2428 static tree
2429 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2430 bool *no_add_attrs)
2432 unsigned arg_count = type_num_arguments (*node);
2433 tree position = TREE_VALUE (args);
2434 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2435 && TREE_CODE (position) != FUNCTION_DECL)
2436 position = default_conversion (position);
2438 if (!tree_fits_uhwi_p (position)
2439 || !arg_count
2440 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2442 warning (OPT_Wattributes,
2443 "alloc_align parameter outside range");
2444 *no_add_attrs = true;
2445 return NULL_TREE;
2447 return NULL_TREE;
2450 /* Handle a "assume_aligned" attribute; arguments as in
2451 struct attribute_spec.handler. */
2453 static tree
2454 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2455 bool *no_add_attrs)
2457 for (; args; args = TREE_CHAIN (args))
2459 tree position = TREE_VALUE (args);
2460 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2461 && TREE_CODE (position) != FUNCTION_DECL)
2462 position = default_conversion (position);
2464 if (TREE_CODE (position) != INTEGER_CST)
2466 warning (OPT_Wattributes,
2467 "assume_aligned parameter not integer constant");
2468 *no_add_attrs = true;
2469 return NULL_TREE;
2472 return NULL_TREE;
2475 /* Handle a "fn spec" attribute; arguments as in
2476 struct attribute_spec.handler. */
2478 static tree
2479 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2480 tree args, int ARG_UNUSED (flags),
2481 bool *no_add_attrs ATTRIBUTE_UNUSED)
2483 gcc_assert (args
2484 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2485 && !TREE_CHAIN (args));
2486 return NULL_TREE;
2489 /* Handle a "warn_unused" attribute; arguments as in
2490 struct attribute_spec.handler. */
2492 static tree
2493 handle_warn_unused_attribute (tree *node, tree name,
2494 tree args ATTRIBUTE_UNUSED,
2495 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2497 if (TYPE_P (*node))
2498 /* Do nothing else, just set the attribute. We'll get at
2499 it later with lookup_attribute. */
2501 else
2503 warning (OPT_Wattributes, "%qE attribute ignored", name);
2504 *no_add_attrs = true;
2507 return NULL_TREE;
2510 /* Handle an "omp declare simd" attribute; arguments as in
2511 struct attribute_spec.handler. */
2513 static tree
2514 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2516 return NULL_TREE;
2519 /* Handle a "simd" attribute. */
2521 static tree
2522 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2524 if (TREE_CODE (*node) == FUNCTION_DECL)
2526 tree t = get_identifier ("omp declare simd");
2527 tree attr = NULL_TREE;
2528 if (args)
2530 tree id = TREE_VALUE (args);
2532 if (TREE_CODE (id) != STRING_CST)
2534 error ("attribute %qE argument not a string", name);
2535 *no_add_attrs = true;
2536 return NULL_TREE;
2539 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2540 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2541 OMP_CLAUSE_NOTINBRANCH);
2542 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2543 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2544 OMP_CLAUSE_INBRANCH);
2545 else
2547 error ("only %<inbranch%> and %<notinbranch%> flags are "
2548 "allowed for %<__simd__%> attribute");
2549 *no_add_attrs = true;
2550 return NULL_TREE;
2554 DECL_ATTRIBUTES (*node)
2555 = tree_cons (t, build_tree_list (NULL_TREE, attr),
2556 DECL_ATTRIBUTES (*node));
2558 else
2560 warning (OPT_Wattributes, "%qE attribute ignored", name);
2561 *no_add_attrs = true;
2564 return NULL_TREE;
2567 /* Handle an "omp declare target" attribute; arguments as in
2568 struct attribute_spec.handler. */
2570 static tree
2571 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2573 return NULL_TREE;
2576 /* Handle a "returns_twice" attribute; arguments as in
2577 struct attribute_spec.handler. */
2579 static tree
2580 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2581 int ARG_UNUSED (flags), bool *no_add_attrs)
2583 if (TREE_CODE (*node) == FUNCTION_DECL)
2584 DECL_IS_RETURNS_TWICE (*node) = 1;
2585 else
2587 warning (OPT_Wattributes, "%qE attribute ignored", name);
2588 *no_add_attrs = true;
2591 return NULL_TREE;
2594 /* Handle a "no_limit_stack" attribute; arguments as in
2595 struct attribute_spec.handler. */
2597 static tree
2598 handle_no_limit_stack_attribute (tree *node, tree name,
2599 tree ARG_UNUSED (args),
2600 int ARG_UNUSED (flags),
2601 bool *no_add_attrs)
2603 tree decl = *node;
2605 if (TREE_CODE (decl) != FUNCTION_DECL)
2607 error_at (DECL_SOURCE_LOCATION (decl),
2608 "%qE attribute applies only to functions", name);
2609 *no_add_attrs = true;
2611 else if (DECL_INITIAL (decl))
2613 error_at (DECL_SOURCE_LOCATION (decl),
2614 "can%'t set %qE attribute after definition", name);
2615 *no_add_attrs = true;
2617 else
2618 DECL_NO_LIMIT_STACK (decl) = 1;
2620 return NULL_TREE;
2623 /* Handle a "pure" attribute; arguments as in
2624 struct attribute_spec.handler. */
2626 static tree
2627 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2628 int ARG_UNUSED (flags), bool *no_add_attrs)
2630 if (TREE_CODE (*node) == FUNCTION_DECL)
2632 tree type = TREE_TYPE (*node);
2633 if (VOID_TYPE_P (TREE_TYPE (type)))
2634 warning (OPT_Wattributes, "%qE attribute on function "
2635 "returning %<void%>", name);
2637 DECL_PURE_P (*node) = 1;
2638 /* ??? TODO: Support types. */
2640 else
2642 warning (OPT_Wattributes, "%qE attribute ignored", name);
2643 *no_add_attrs = true;
2646 return NULL_TREE;
2649 /* Digest an attribute list destined for a transactional memory statement.
2650 ALLOWED is the set of attributes that are allowed for this statement;
2651 return the attribute we parsed. Multiple attributes are never allowed. */
2654 parse_tm_stmt_attr (tree attrs, int allowed)
2656 tree a_seen = NULL;
2657 int m_seen = 0;
2659 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2661 tree a = TREE_PURPOSE (attrs);
2662 int m = 0;
2664 if (is_attribute_p ("outer", a))
2665 m = TM_STMT_ATTR_OUTER;
2667 if ((m & allowed) == 0)
2669 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2670 continue;
2673 if (m_seen == 0)
2675 a_seen = a;
2676 m_seen = m;
2678 else if (m_seen == m)
2679 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2680 else
2681 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2684 return m_seen;
2687 /* Transform a TM attribute name into a maskable integer and back.
2688 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2689 to how the lack of an attribute is treated. */
2692 tm_attr_to_mask (tree attr)
2694 if (attr == NULL)
2695 return 0;
2696 if (is_attribute_p ("transaction_safe", attr))
2697 return TM_ATTR_SAFE;
2698 if (is_attribute_p ("transaction_callable", attr))
2699 return TM_ATTR_CALLABLE;
2700 if (is_attribute_p ("transaction_pure", attr))
2701 return TM_ATTR_PURE;
2702 if (is_attribute_p ("transaction_unsafe", attr))
2703 return TM_ATTR_IRREVOCABLE;
2704 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2705 return TM_ATTR_MAY_CANCEL_OUTER;
2706 return 0;
2709 tree
2710 tm_mask_to_attr (int mask)
2712 const char *str;
2713 switch (mask)
2715 case TM_ATTR_SAFE:
2716 str = "transaction_safe";
2717 break;
2718 case TM_ATTR_CALLABLE:
2719 str = "transaction_callable";
2720 break;
2721 case TM_ATTR_PURE:
2722 str = "transaction_pure";
2723 break;
2724 case TM_ATTR_IRREVOCABLE:
2725 str = "transaction_unsafe";
2726 break;
2727 case TM_ATTR_MAY_CANCEL_OUTER:
2728 str = "transaction_may_cancel_outer";
2729 break;
2730 default:
2731 gcc_unreachable ();
2733 return get_identifier (str);
2736 /* Return the first TM attribute seen in LIST. */
2738 tree
2739 find_tm_attribute (tree list)
2741 for (; list ; list = TREE_CHAIN (list))
2743 tree name = TREE_PURPOSE (list);
2744 if (tm_attr_to_mask (name) != 0)
2745 return name;
2747 return NULL_TREE;
2750 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2751 Here we accept only function types, and verify that none of the other
2752 function TM attributes are also applied. */
2753 /* ??? We need to accept class types for C++, but not C. This greatly
2754 complicates this function, since we can no longer rely on the extra
2755 processing given by function_type_required. */
2757 static tree
2758 handle_tm_attribute (tree *node, tree name, tree args,
2759 int flags, bool *no_add_attrs)
2761 /* Only one path adds the attribute; others don't. */
2762 *no_add_attrs = true;
2764 switch (TREE_CODE (*node))
2766 case RECORD_TYPE:
2767 case UNION_TYPE:
2768 /* Only tm_callable and tm_safe apply to classes. */
2769 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2770 goto ignored;
2771 /* FALLTHRU */
2773 case FUNCTION_TYPE:
2774 case METHOD_TYPE:
2776 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2777 if (old_name == name)
2779 else if (old_name != NULL_TREE)
2780 error ("type was previously declared %qE", old_name);
2781 else
2782 *no_add_attrs = false;
2784 break;
2786 case FUNCTION_DECL:
2788 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2789 want to set transaction_safe on the type. */
2790 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2791 if (!TYPE_P (DECL_CONTEXT (*node)))
2792 error_at (DECL_SOURCE_LOCATION (*node),
2793 "%<transaction_safe_dynamic%> may only be specified for "
2794 "a virtual function");
2795 *no_add_attrs = false;
2796 decl_attributes (&TREE_TYPE (*node),
2797 build_tree_list (get_identifier ("transaction_safe"),
2798 NULL_TREE),
2800 break;
2803 case POINTER_TYPE:
2805 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2806 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2808 tree fn_tmp = TREE_TYPE (*node);
2809 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2810 *node = build_pointer_type (fn_tmp);
2811 break;
2814 /* FALLTHRU */
2816 default:
2817 /* If a function is next, pass it on to be tried next. */
2818 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2819 return tree_cons (name, args, NULL);
2821 ignored:
2822 warning (OPT_Wattributes, "%qE attribute ignored", name);
2823 break;
2826 return NULL_TREE;
2829 /* Handle the TM_WRAP attribute; arguments as in
2830 struct attribute_spec.handler. */
2832 static tree
2833 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2834 int ARG_UNUSED (flags), bool *no_add_attrs)
2836 tree decl = *node;
2838 /* We don't need the attribute even on success, since we
2839 record the entry in an external table. */
2840 *no_add_attrs = true;
2842 if (TREE_CODE (decl) != FUNCTION_DECL)
2843 warning (OPT_Wattributes, "%qE attribute ignored", name);
2844 else
2846 tree wrap_decl = TREE_VALUE (args);
2847 if (error_operand_p (wrap_decl))
2849 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2850 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2851 error ("%qE argument not an identifier", name);
2852 else
2854 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2855 wrap_decl = lookup_name (wrap_decl);
2856 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2858 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2859 TREE_TYPE (wrap_decl)))
2860 record_tm_replacement (wrap_decl, decl);
2861 else
2862 error ("%qD is not compatible with %qD", wrap_decl, decl);
2864 else
2865 error ("%qE argument is not a function", name);
2869 return NULL_TREE;
2872 /* Ignore the given attribute. Used when this attribute may be usefully
2873 overridden by the target, but is not used generically. */
2875 static tree
2876 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2877 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2878 bool *no_add_attrs)
2880 *no_add_attrs = true;
2881 return NULL_TREE;
2884 /* Handle a "no vops" attribute; arguments as in
2885 struct attribute_spec.handler. */
2887 static tree
2888 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2889 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2890 bool *ARG_UNUSED (no_add_attrs))
2892 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2893 DECL_IS_NOVOPS (*node) = 1;
2894 return NULL_TREE;
2897 /* Handle a "deprecated" attribute; arguments as in
2898 struct attribute_spec.handler. */
2900 static tree
2901 handle_deprecated_attribute (tree *node, tree name,
2902 tree args, int flags,
2903 bool *no_add_attrs)
2905 tree type = NULL_TREE;
2906 int warn = 0;
2907 tree what = NULL_TREE;
2909 if (!args)
2910 *no_add_attrs = true;
2911 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2913 error ("deprecated message is not a string");
2914 *no_add_attrs = true;
2917 if (DECL_P (*node))
2919 tree decl = *node;
2920 type = TREE_TYPE (decl);
2922 if (TREE_CODE (decl) == TYPE_DECL
2923 || TREE_CODE (decl) == PARM_DECL
2924 || VAR_OR_FUNCTION_DECL_P (decl)
2925 || TREE_CODE (decl) == FIELD_DECL
2926 || TREE_CODE (decl) == CONST_DECL
2927 || objc_method_decl (TREE_CODE (decl)))
2928 TREE_DEPRECATED (decl) = 1;
2929 else
2930 warn = 1;
2932 else if (TYPE_P (*node))
2934 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2935 *node = build_variant_type_copy (*node);
2936 TREE_DEPRECATED (*node) = 1;
2937 type = *node;
2939 else
2940 warn = 1;
2942 if (warn)
2944 *no_add_attrs = true;
2945 if (type && TYPE_NAME (type))
2947 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2948 what = TYPE_NAME (*node);
2949 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2950 && DECL_NAME (TYPE_NAME (type)))
2951 what = DECL_NAME (TYPE_NAME (type));
2953 if (what)
2954 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2955 else
2956 warning (OPT_Wattributes, "%qE attribute ignored", name);
2959 return NULL_TREE;
2962 /* Handle a "vector_size" attribute; arguments as in
2963 struct attribute_spec.handler. */
2965 static tree
2966 handle_vector_size_attribute (tree *node, tree name, tree args,
2967 int ARG_UNUSED (flags),
2968 bool *no_add_attrs)
2970 unsigned HOST_WIDE_INT vecsize, nunits;
2971 machine_mode orig_mode;
2972 tree type = *node, new_type, size;
2974 *no_add_attrs = true;
2976 size = TREE_VALUE (args);
2977 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2978 && TREE_CODE (size) != FUNCTION_DECL)
2979 size = default_conversion (size);
2981 if (!tree_fits_uhwi_p (size))
2983 warning (OPT_Wattributes, "%qE attribute ignored", name);
2984 return NULL_TREE;
2987 /* Get the vector size (in bytes). */
2988 vecsize = tree_to_uhwi (size);
2990 /* We need to provide for vector pointers, vector arrays, and
2991 functions returning vectors. For example:
2993 __attribute__((vector_size(16))) short *foo;
2995 In this case, the mode is SI, but the type being modified is
2996 HI, so we need to look further. */
2998 while (POINTER_TYPE_P (type)
2999 || TREE_CODE (type) == FUNCTION_TYPE
3000 || TREE_CODE (type) == METHOD_TYPE
3001 || TREE_CODE (type) == ARRAY_TYPE
3002 || TREE_CODE (type) == OFFSET_TYPE)
3003 type = TREE_TYPE (type);
3005 /* Get the mode of the type being modified. */
3006 orig_mode = TYPE_MODE (type);
3008 if ((!INTEGRAL_TYPE_P (type)
3009 && !SCALAR_FLOAT_TYPE_P (type)
3010 && !FIXED_POINT_TYPE_P (type))
3011 || (!SCALAR_FLOAT_MODE_P (orig_mode)
3012 && GET_MODE_CLASS (orig_mode) != MODE_INT
3013 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
3014 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
3015 || TREE_CODE (type) == BOOLEAN_TYPE)
3017 error ("invalid vector type for attribute %qE", name);
3018 return NULL_TREE;
3021 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
3023 error ("vector size not an integral multiple of component size");
3024 return NULL;
3027 if (vecsize == 0)
3029 error ("zero vector size");
3030 return NULL;
3033 /* Calculate how many units fit in the vector. */
3034 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
3035 if (nunits & (nunits - 1))
3037 error ("number of components of the vector not a power of two");
3038 return NULL_TREE;
3041 new_type = build_vector_type (type, nunits);
3043 /* Build back pointers if needed. */
3044 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
3046 return NULL_TREE;
3049 /* Handle the "nonnull" attribute. */
3051 static tree
3052 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
3053 tree args, int ARG_UNUSED (flags),
3054 bool *no_add_attrs)
3056 tree type = *node;
3057 unsigned HOST_WIDE_INT attr_arg_num;
3059 /* If no arguments are specified, all pointer arguments should be
3060 non-null. Verify a full prototype is given so that the arguments
3061 will have the correct types when we actually check them later.
3062 Avoid diagnosing type-generic built-ins since those have no
3063 prototype. */
3064 if (!args)
3066 if (!prototype_p (type)
3067 && (!TYPE_ATTRIBUTES (type)
3068 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
3070 error ("nonnull attribute without arguments on a non-prototype");
3071 *no_add_attrs = true;
3073 return NULL_TREE;
3076 /* Argument list specified. Verify that each argument number references
3077 a pointer argument. */
3078 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
3080 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
3082 tree arg = TREE_VALUE (args);
3083 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
3084 && TREE_CODE (arg) != FUNCTION_DECL)
3085 TREE_VALUE (args) = arg = default_conversion (arg);
3087 if (!get_nonnull_operand (arg, &arg_num))
3089 error ("nonnull argument has invalid operand number (argument %lu)",
3090 (unsigned long) attr_arg_num);
3091 *no_add_attrs = true;
3092 return NULL_TREE;
3095 if (prototype_p (type))
3097 function_args_iterator iter;
3098 tree argument;
3100 function_args_iter_init (&iter, type);
3101 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
3103 argument = function_args_iter_cond (&iter);
3104 if (argument == NULL_TREE || ck_num == arg_num)
3105 break;
3108 if (!argument
3109 || TREE_CODE (argument) == VOID_TYPE)
3111 error ("nonnull argument with out-of-range operand number "
3112 "(argument %lu, operand %lu)",
3113 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3114 *no_add_attrs = true;
3115 return NULL_TREE;
3118 if (TREE_CODE (argument) != POINTER_TYPE)
3120 error ("nonnull argument references non-pointer operand "
3121 "(argument %lu, operand %lu)",
3122 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3123 *no_add_attrs = true;
3124 return NULL_TREE;
3129 return NULL_TREE;
3132 /* Handle the "nonstring" variable attribute. */
3134 static tree
3135 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3136 int ARG_UNUSED (flags), bool *no_add_attrs)
3138 gcc_assert (!args);
3139 tree_code code = TREE_CODE (*node);
3141 if (VAR_P (*node)
3142 || code == FIELD_DECL
3143 || code == PARM_DECL)
3145 tree type = TREE_TYPE (*node);
3147 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3149 /* Accept the attribute on arrays and pointers to all three
3150 narrow character types. */
3151 tree eltype = TREE_TYPE (type);
3152 eltype = TYPE_MAIN_VARIANT (eltype);
3153 if (eltype == char_type_node
3154 || eltype == signed_char_type_node
3155 || eltype == unsigned_char_type_node)
3156 return NULL_TREE;
3159 warning (OPT_Wattributes,
3160 "%qE attribute ignored on objects of type %qT",
3161 name, type);
3162 *no_add_attrs = true;
3163 return NULL_TREE;
3166 if (code == FUNCTION_DECL)
3167 warning (OPT_Wattributes,
3168 "%qE attribute does not apply to functions", name);
3169 else if (code == TYPE_DECL)
3170 warning (OPT_Wattributes,
3171 "%qE attribute does not apply to types", name);
3172 else
3173 warning (OPT_Wattributes, "%qE attribute ignored", name);
3175 *no_add_attrs = true;
3176 return NULL_TREE;
3179 /* Handle a "nothrow" attribute; arguments as in
3180 struct attribute_spec.handler. */
3182 static tree
3183 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3184 int ARG_UNUSED (flags), bool *no_add_attrs)
3186 if (TREE_CODE (*node) == FUNCTION_DECL)
3187 TREE_NOTHROW (*node) = 1;
3188 /* ??? TODO: Support types. */
3189 else
3191 warning (OPT_Wattributes, "%qE attribute ignored", name);
3192 *no_add_attrs = true;
3195 return NULL_TREE;
3198 /* Handle a "cleanup" attribute; arguments as in
3199 struct attribute_spec.handler. */
3201 static tree
3202 handle_cleanup_attribute (tree *node, tree name, tree args,
3203 int ARG_UNUSED (flags), bool *no_add_attrs)
3205 tree decl = *node;
3206 tree cleanup_id, cleanup_decl;
3208 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3209 for global destructors in C++. This requires infrastructure that
3210 we don't have generically at the moment. It's also not a feature
3211 we'd be missing too much, since we do have attribute constructor. */
3212 if (!VAR_P (decl) || TREE_STATIC (decl))
3214 warning (OPT_Wattributes, "%qE attribute ignored", name);
3215 *no_add_attrs = true;
3216 return NULL_TREE;
3219 /* Verify that the argument is a function in scope. */
3220 /* ??? We could support pointers to functions here as well, if
3221 that was considered desirable. */
3222 cleanup_id = TREE_VALUE (args);
3223 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3225 error ("cleanup argument not an identifier");
3226 *no_add_attrs = true;
3227 return NULL_TREE;
3229 cleanup_decl = lookup_name (cleanup_id);
3230 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3232 error ("cleanup argument not a function");
3233 *no_add_attrs = true;
3234 return NULL_TREE;
3237 /* That the function has proper type is checked with the
3238 eventual call to build_function_call. */
3240 return NULL_TREE;
3243 /* Handle a "warn_unused_result" attribute. No special handling. */
3245 static tree
3246 handle_warn_unused_result_attribute (tree *node, tree name,
3247 tree ARG_UNUSED (args),
3248 int ARG_UNUSED (flags), bool *no_add_attrs)
3250 /* Ignore the attribute for functions not returning any value. */
3251 if (VOID_TYPE_P (TREE_TYPE (*node)))
3253 warning (OPT_Wattributes, "%qE attribute ignored", name);
3254 *no_add_attrs = true;
3257 return NULL_TREE;
3260 /* Handle a "sentinel" attribute. */
3262 static tree
3263 handle_sentinel_attribute (tree *node, tree name, tree args,
3264 int ARG_UNUSED (flags), bool *no_add_attrs)
3266 if (!prototype_p (*node))
3268 warning (OPT_Wattributes,
3269 "%qE attribute requires prototypes with named arguments", name);
3270 *no_add_attrs = true;
3272 else
3274 if (!stdarg_p (*node))
3276 warning (OPT_Wattributes,
3277 "%qE attribute only applies to variadic functions", name);
3278 *no_add_attrs = true;
3282 if (args)
3284 tree position = TREE_VALUE (args);
3285 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3286 && TREE_CODE (position) != FUNCTION_DECL)
3287 position = default_conversion (position);
3289 if (TREE_CODE (position) != INTEGER_CST
3290 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3292 warning (OPT_Wattributes,
3293 "requested position is not an integer constant");
3294 *no_add_attrs = true;
3296 else
3298 if (tree_int_cst_lt (position, integer_zero_node))
3300 warning (OPT_Wattributes,
3301 "requested position is less than zero");
3302 *no_add_attrs = true;
3307 return NULL_TREE;
3310 /* Handle a "type_generic" attribute. */
3312 static tree
3313 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3314 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3315 bool * ARG_UNUSED (no_add_attrs))
3317 /* Ensure we have a function type. */
3318 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3320 /* Ensure we have a variadic function. */
3321 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3323 return NULL_TREE;
3326 /* Handle a "target" attribute. */
3328 static tree
3329 handle_target_attribute (tree *node, tree name, tree args, int flags,
3330 bool *no_add_attrs)
3332 /* Ensure we have a function type. */
3333 if (TREE_CODE (*node) != FUNCTION_DECL)
3335 warning (OPT_Wattributes, "%qE attribute ignored", name);
3336 *no_add_attrs = true;
3338 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3340 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3341 "with %qs attribute", name, "target_clones");
3342 *no_add_attrs = true;
3344 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3345 flags))
3346 *no_add_attrs = true;
3348 /* Check that there's no empty string in values of the attribute. */
3349 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3351 tree value = TREE_VALUE (t);
3352 if (TREE_CODE (value) == STRING_CST
3353 && TREE_STRING_LENGTH (value) == 1
3354 && TREE_STRING_POINTER (value)[0] == '\0')
3356 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3357 *no_add_attrs = true;
3361 return NULL_TREE;
3364 /* Handle a "target_clones" attribute. */
3366 static tree
3367 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3368 int ARG_UNUSED (flags), bool *no_add_attrs)
3370 /* Ensure we have a function type. */
3371 if (TREE_CODE (*node) == FUNCTION_DECL)
3373 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3375 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3376 "with %qs attribute", name, "always_inline");
3377 *no_add_attrs = true;
3379 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3381 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3382 "with %qs attribute", name, "target");
3383 *no_add_attrs = true;
3385 else
3386 /* Do not inline functions with multiple clone targets. */
3387 DECL_UNINLINABLE (*node) = 1;
3389 else
3391 warning (OPT_Wattributes, "%qE attribute ignored", name);
3392 *no_add_attrs = true;
3394 return NULL_TREE;
3397 /* For handling "optimize" attribute. arguments as in
3398 struct attribute_spec.handler. */
3400 static tree
3401 handle_optimize_attribute (tree *node, tree name, tree args,
3402 int ARG_UNUSED (flags), bool *no_add_attrs)
3404 /* Ensure we have a function type. */
3405 if (TREE_CODE (*node) != FUNCTION_DECL)
3407 warning (OPT_Wattributes, "%qE attribute ignored", name);
3408 *no_add_attrs = true;
3410 else
3412 struct cl_optimization cur_opts;
3413 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3415 /* Save current options. */
3416 cl_optimization_save (&cur_opts, &global_options);
3418 /* If we previously had some optimization options, use them as the
3419 default. */
3420 if (old_opts)
3421 cl_optimization_restore (&global_options,
3422 TREE_OPTIMIZATION (old_opts));
3424 /* Parse options, and update the vector. */
3425 parse_optimize_options (args, true);
3426 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3427 = build_optimization_node (&global_options);
3429 /* Restore current options. */
3430 cl_optimization_restore (&global_options, &cur_opts);
3433 return NULL_TREE;
3436 /* Handle a "no_split_stack" attribute. */
3438 static tree
3439 handle_no_split_stack_attribute (tree *node, tree name,
3440 tree ARG_UNUSED (args),
3441 int ARG_UNUSED (flags),
3442 bool *no_add_attrs)
3444 tree decl = *node;
3446 if (TREE_CODE (decl) != FUNCTION_DECL)
3448 error_at (DECL_SOURCE_LOCATION (decl),
3449 "%qE attribute applies only to functions", name);
3450 *no_add_attrs = true;
3452 else if (DECL_INITIAL (decl))
3454 error_at (DECL_SOURCE_LOCATION (decl),
3455 "can%'t set %qE attribute after definition", name);
3456 *no_add_attrs = true;
3459 return NULL_TREE;
3462 /* Handle a "returns_nonnull" attribute; arguments as in
3463 struct attribute_spec.handler. */
3465 static tree
3466 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3467 bool *no_add_attrs)
3469 // Even without a prototype we still have a return type we can check.
3470 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3472 error ("returns_nonnull attribute on a function not returning a pointer");
3473 *no_add_attrs = true;
3475 return NULL_TREE;
3478 /* Handle a "designated_init" attribute; arguments as in
3479 struct attribute_spec.handler. */
3481 static tree
3482 handle_designated_init_attribute (tree *node, tree name, tree, int,
3483 bool *no_add_attrs)
3485 if (TREE_CODE (*node) != RECORD_TYPE)
3487 error ("%qE attribute is only valid on %<struct%> type", name);
3488 *no_add_attrs = true;
3490 return NULL_TREE;
3494 /* Handle a "fallthrough" attribute; arguments as in struct
3495 attribute_spec.handler. */
3497 static tree
3498 handle_fallthrough_attribute (tree *, tree name, tree, int,
3499 bool *no_add_attrs)
3501 warning (OPT_Wattributes, "%qE attribute ignored", name);
3502 *no_add_attrs = true;
3503 return NULL_TREE;
3506 static tree
3507 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3509 /* Nothing to be done here. */
3510 return NULL_TREE;