Debian Codegraph @

gcc/c-family/c-attribs.cc

raw
1 /* C-family attributes handling.
2 Copyright (C) 1992-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #define INCLUDE_STRING
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "function.h"
26 #include "tree.h"
27 #include "memmodel.h"
28 #include "c-common.h"
29 #include "gimple-expr.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "cgraph.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "attribs.h"
38 #include "varasm.h"
39 #include "trans-mem.h"
40 #include "c-objc.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "ipa-strub.h"
45 #include "toplev.h"
46 #include "tree-iterator.h"
47 #include "opts.h"
48 #include "gimplify.h"
49 #include "tree-pretty-print.h"
50 #include "gcc-rich-location.h"
51 #include "gcc-urlifier.h"
52
53 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
57 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
58 static tree handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
59 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
60 int, bool *);
61 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
62 int, bool *);
63 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
64 int, bool *);
65 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
66 bool *);
67 static tree handle_no_sanitize_coverage_attribute (tree *, tree, tree, int,
68 bool *);
69 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
70 bool *);
71 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_no_stack_protector_function_attribute (tree *, tree,
73 tree, int, bool *);
74 static tree handle_strub_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_symver_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
80 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
81 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
82 static tree handle_always_inline_attribute (tree *, tree, tree, int,
83 bool *);
84 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
86 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
87 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
88 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_uninitialized_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
91 bool *);
92 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
93 bool *);
94 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
95 static tree handle_transparent_union_attribute (tree *, tree, tree,
96 int, bool *);
97 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
98 int, bool *);
99 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
101 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
102 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
103 static tree handle_special_var_sec_attribute (tree *, tree, tree, int, bool *);
104 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
105 int, bool *);
106 static tree handle_strict_flex_array_attribute (tree *, tree, tree,
107 int, bool *);
108 static tree handle_counted_by_attribute (tree *, tree, tree,
109 int, bool *);
110 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
111 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
112 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
113 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
116 static tree handle_visibility_attribute (tree *, tree, tree, int,
117 bool *);
118 static tree handle_tls_model_attribute (tree *, tree, tree, int,
119 bool *);
120 static tree handle_no_instrument_function_attribute (tree *, tree,
121 tree, int, bool *);
122 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
123 tree, int, bool *);
124 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_dealloc_attribute (tree *, tree, tree, int, bool *);
126 static tree handle_tainted_args_attribute (tree *, tree, tree, int, bool *);
127 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
129 bool *);
130 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_unavailable_attribute (tree *, tree, tree, int,
135 bool *);
136 static tree handle_vector_size_attribute (tree *, tree, tree, int,
137 bool *) ATTRIBUTE_NONNULL(3);
138 static tree handle_vector_mask_attribute (tree *, tree, tree, int,
139 bool *) ATTRIBUTE_NONNULL(3);
140 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
141 static tree handle_nonnull_if_nonzero_attribute (tree *, tree, tree, int,
142 bool *);
143 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
144 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
145 static tree handle_expected_throw_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
147 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
148 bool *);
149 static tree handle_access_attribute (tree *, tree, tree, int, bool *);
150
151 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
152 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
153 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
154 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
155 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
156 static tree handle_assume_attribute (tree *, tree, tree, int, bool *);
157 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
158 static tree handle_target_version_attribute (tree *, tree, tree, int, bool *);
159 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
160 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
161 static tree ignore_attribute (tree *, tree, tree, int, bool *);
162 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
163 static tree handle_zero_call_used_regs_attribute (tree *, tree, tree, int,
164 bool *);
165 static tree handle_argspec_attribute (tree *, tree, tree, int, bool *);
166 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
167 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
168 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
169 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
170 bool *);
171 static tree handle_omp_declare_variant_attribute (tree *, tree, tree, int,
172 bool *);
173 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
174 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
175 bool *);
176 static tree handle_non_overlapping_attribute (tree *, tree, tree, int, bool *);
177 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
178 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
179 int, bool *);
180 static tree handle_copy_attribute (tree *, tree, tree, int, bool *);
181 static tree handle_nsobject_attribute (tree *, tree, tree, int, bool *);
182 static tree handle_objc_root_class_attribute (tree *, tree, tree, int, bool *);
183 static tree handle_objc_nullability_attribute (tree *, tree, tree, int, bool *);
184 static tree handle_signed_bool_precision_attribute (tree *, tree, tree, int,
185 bool *);
186 static tree handle_hardbool_attribute (tree *, tree, tree, int, bool *);
187 static tree handle_retain_attribute (tree *, tree, tree, int, bool *);
188 static tree handle_fd_arg_attribute (tree *, tree, tree, int, bool *);
189 static tree handle_flag_enum_attribute (tree *, tree, tree, int, bool *);
190 static tree handle_null_terminated_string_arg_attribute (tree *, tree, tree, int, bool *);
191
192 /* Helper to define attribute exclusions. */
193 #define ATTR_EXCL(name, function, type, variable) \
194 { name, function, type, variable }
195
196 /* Define attributes that are mutually exclusive with one another. */
197 extern const struct attribute_spec::exclusions attr_aligned_exclusions[] =
198 {
199 /* Attribute name exclusion applies to:
200 function, type, variable */
201 ATTR_EXCL ("aligned", true, false, false),
202 ATTR_EXCL ("packed", true, false, false),
203 ATTR_EXCL (NULL, false, false, false)
204 };
205
206 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
207 {
208 ATTR_EXCL ("cold", true, true, true),
209 ATTR_EXCL ("hot", true, true, true),
210 ATTR_EXCL (NULL, false, false, false)
211 };
212
213 static const struct attribute_spec::exclusions attr_common_exclusions[] =
214 {
215 ATTR_EXCL ("common", true, true, true),
216 ATTR_EXCL ("nocommon", true, true, true),
217 ATTR_EXCL (NULL, false, false, false),
218 };
219
220 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
221 {
222 ATTR_EXCL ("noinline", true, true, true),
223 ATTR_EXCL (NULL, false, false, false),
224 };
225
226 static const struct attribute_spec::exclusions attr_always_inline_exclusions[] =
227 {
228 ATTR_EXCL ("noinline", true, true, true),
229 ATTR_EXCL ("target_clones", true, true, true),
230 ATTR_EXCL (NULL, false, false, false),
231 };
232
233 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
234 {
235 ATTR_EXCL ("always_inline", true, true, true),
236 ATTR_EXCL ("gnu_inline", true, true, true),
237 ATTR_EXCL (NULL, false, false, false),
238 };
239
240 static const struct attribute_spec::exclusions attr_target_exclusions[] =
241 {
242 ATTR_EXCL ("target_clones", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
243 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE),
244 ATTR_EXCL (NULL, false, false, false),
245 };
246
247 static const struct attribute_spec::exclusions attr_target_clones_exclusions[] =
248 {
249 ATTR_EXCL ("always_inline", true, true, true),
250 ATTR_EXCL ("target", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
251 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE),
252 ATTR_EXCL ("target_version", true, true, true),
253 ATTR_EXCL (NULL, false, false, false),
254 };
255
256 static const struct attribute_spec::exclusions attr_target_version_exclusions[] =
257 {
258 ATTR_EXCL ("target_clones", true, true, true),
259 ATTR_EXCL (NULL, false, false, false),
260 };
261
262 extern const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
263 {
264 ATTR_EXCL ("alloc_align", true, true, true),
265 ATTR_EXCL ("alloc_size", true, true, true),
266 ATTR_EXCL ("const", true, true, true),
267 ATTR_EXCL ("malloc", true, true, true),
268 ATTR_EXCL ("pure", true, true, true),
269 ATTR_EXCL ("returns_twice", true, true, true),
270 ATTR_EXCL ("warn_unused_result", true, true, true),
271 ATTR_EXCL (NULL, false, false, false),
272 };
273
274 static const struct attribute_spec::exclusions
275 attr_warn_unused_result_exclusions[] =
276 {
277 ATTR_EXCL ("noreturn", true, true, true),
278 ATTR_EXCL ("warn_unused_result", true, true, true),
279 ATTR_EXCL (NULL, false, false, false),
280 };
281
282 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
283 {
284 ATTR_EXCL ("noreturn", true, true, true),
285 ATTR_EXCL (NULL, false, false, false),
286 };
287
288 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
289 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
290 {
291 ATTR_EXCL ("const", true, true, true),
292 ATTR_EXCL ("noreturn", true, true, true),
293 ATTR_EXCL ("pure", true, true, true),
294 ATTR_EXCL (NULL, false, false, false),
295 };
296
297 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
298 {
299 ATTR_EXCL ("const", true, true, true),
300 ATTR_EXCL ("alloc_align", true, true, true),
301 ATTR_EXCL ("alloc_size", true, true, true),
302 ATTR_EXCL ("malloc", true, true, true),
303 ATTR_EXCL ("noreturn", true, true, true),
304 ATTR_EXCL ("pure", true, true, true),
305 ATTR_EXCL (NULL, false, false, false)
306 };
307
308 /* Exclusions that apply to attributes that put declarations in specific
309 sections. */
310 static const struct attribute_spec::exclusions attr_section_exclusions[] =
311 {
312 ATTR_EXCL ("noinit", true, true, true),
313 ATTR_EXCL ("persistent", true, true, true),
314 ATTR_EXCL ("section", true, true, true),
315 ATTR_EXCL (NULL, false, false, false),
316 };
317
318 static const struct attribute_spec::exclusions attr_stack_protect_exclusions[] =
319 {
320 ATTR_EXCL ("stack_protect", true, false, false),
321 ATTR_EXCL ("no_stack_protector", true, false, false),
322 ATTR_EXCL (NULL, false, false, false),
323 };
324
325
326 /* Table of machine-independent attributes common to all C-like languages.
327
328 Current list of processed common attributes: nonnull. */
329 const struct attribute_spec c_common_gnu_attributes[] =
330 {
331 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
332 affects_type_identity, handler, exclude } */
333 { "signed_bool_precision", 1, 1, false, true, false, true,
334 handle_signed_bool_precision_attribute, NULL },
335 { "hardbool", 0, 2, false, true, false, true,
336 handle_hardbool_attribute, NULL },
337 { "packed", 0, 0, false, false, false, false,
338 handle_packed_attribute,
339 attr_aligned_exclusions },
340 { "nocommon", 0, 0, true, false, false, false,
341 handle_nocommon_attribute,
342 attr_common_exclusions },
343 { "common", 0, 0, true, false, false, false,
344 handle_common_attribute,
345 attr_common_exclusions },
346 { "musttail", 0, 0, false, false, false,
347 false, handle_musttail_attribute, NULL },
348 /* FIXME: logically, noreturn attributes should be listed as
349 "false, true, true" and apply to function types. But implementing this
350 would require all the places in the compiler that use TREE_THIS_VOLATILE
351 on a decl to identify non-returning functions to be located and fixed
352 to check the function type instead. */
353 { "noreturn", 0, 0, true, false, false, false,
354 handle_noreturn_attribute,
355 attr_noreturn_exclusions },
356 { "volatile", 0, 0, true, false, false, false,
357 handle_noreturn_attribute, NULL },
358 { "stack_protect", 0, 0, true, false, false, false,
359 handle_stack_protect_attribute,
360 attr_stack_protect_exclusions },
361 { "no_stack_protector", 0, 0, true, false, false, false,
362 handle_no_stack_protector_function_attribute,
363 attr_stack_protect_exclusions },
364 { "strub", 0, 1, false, true, false, true,
365 handle_strub_attribute, NULL },
366 { "noinline", 0, 0, true, false, false, false,
367 handle_noinline_attribute,
368 attr_noinline_exclusions },
369 { "noclone", 0, 0, true, false, false, false,
370 handle_noclone_attribute, NULL },
371 { "no_icf", 0, 0, true, false, false, false,
372 handle_noicf_attribute, NULL },
373 { "noipa", 0, 0, true, false, false, false,
374 handle_noipa_attribute, NULL },
375 { "leaf", 0, 0, true, false, false, false,
376 handle_leaf_attribute, NULL },
377 { "always_inline", 0, 0, true, false, false, false,
378 handle_always_inline_attribute,
379 attr_always_inline_exclusions },
380 { "gnu_inline", 0, 0, true, false, false, false,
381 handle_gnu_inline_attribute,
382 attr_inline_exclusions },
383 { "artificial", 0, 0, true, false, false, false,
384 handle_artificial_attribute, NULL },
385 { "flatten", 0, 0, true, false, false, false,
386 handle_flatten_attribute, NULL },
387 { "used", 0, 0, true, false, false, false,
388 handle_used_attribute, NULL },
389 { "unused", 0, 0, false, false, false, false,
390 handle_unused_attribute, NULL },
391 { "uninitialized", 0, 0, true, false, false, false,
392 handle_uninitialized_attribute, NULL },
393 { "retain", 0, 0, true, false, false, false,
394 handle_retain_attribute, NULL },
395 { "externally_visible", 0, 0, true, false, false, false,
396 handle_externally_visible_attribute, NULL },
397 { "no_reorder", 0, 0, true, false, false, false,
398 handle_no_reorder_attribute, NULL },
399 /* The same comments as for noreturn attributes apply to const ones. */
400 { "const", 0, 0, true, false, false, false,
401 handle_const_attribute,
402 attr_const_pure_exclusions },
403 { "scalar_storage_order", 1, 1, false, false, false, false,
404 handle_scalar_storage_order_attribute, NULL },
405 { "transparent_union", 0, 0, false, false, false, false,
406 handle_transparent_union_attribute, NULL },
407 { "constructor", 0, 1, true, false, false, false,
408 handle_constructor_attribute, NULL },
409 { "destructor", 0, 1, true, false, false, false,
410 handle_destructor_attribute, NULL },
411 { "mode", 1, 1, false, true, false, false,
412 handle_mode_attribute, NULL },
413 { "section", 1, 1, true, false, false, false,
414 handle_section_attribute, attr_section_exclusions },
415 { "aligned", 0, 1, false, false, false, false,
416 handle_aligned_attribute,
417 attr_aligned_exclusions },
418 { "warn_if_not_aligned", 0, 1, false, false, false, false,
419 handle_warn_if_not_aligned_attribute, NULL },
420 { "strict_flex_array", 1, 1, true, false, false, false,
421 handle_strict_flex_array_attribute, NULL },
422 { "counted_by", 1, 1, true, false, false, false,
423 handle_counted_by_attribute, NULL },
424 { "weak", 0, 0, true, false, false, false,
425 handle_weak_attribute, NULL },
426 { "noplt", 0, 0, true, false, false, false,
427 handle_noplt_attribute, NULL },
428 { "ifunc", 1, 1, true, false, false, false,
429 handle_ifunc_attribute, NULL },
430 { "alias", 1, 1, true, false, false, false,
431 handle_alias_attribute, NULL },
432 { "weakref", 0, 1, true, false, false, false,
433 handle_weakref_attribute, NULL },
434 { "no_instrument_function", 0, 0, true, false, false, false,
435 handle_no_instrument_function_attribute,
436 NULL },
437 { "no_profile_instrument_function", 0, 0, true, false, false, false,
438 handle_no_profile_instrument_function_attribute,
439 NULL },
440 { "malloc", 0, 2, true, false, false, false,
441 handle_malloc_attribute, attr_alloc_exclusions },
442 { "returns_twice", 0, 0, true, false, false, false,
443 handle_returns_twice_attribute,
444 attr_returns_twice_exclusions },
445 { "no_stack_limit", 0, 0, true, false, false, false,
446 handle_no_limit_stack_attribute, NULL },
447 { "pure", 0, 0, true, false, false, false,
448 handle_pure_attribute,
449 attr_const_pure_exclusions },
450 { "reproducible", 0, 0, false, true, true, false,
451 handle_reproducible_attribute, NULL },
452 { "unsequenced", 0, 0, false, true, true, false,
453 handle_unsequenced_attribute, NULL },
454 { "reproducible noptr", 0, 0, false, true, true, false,
455 handle_reproducible_attribute, NULL },
456 { "unsequenced noptr", 0, 0, false, true, true, false,
457 handle_unsequenced_attribute, NULL },
458 { "transaction_callable", 0, 0, false, true, false, false,
459 handle_tm_attribute, NULL },
460 { "transaction_unsafe", 0, 0, false, true, false, true,
461 handle_tm_attribute, NULL },
462 { "transaction_safe", 0, 0, false, true, false, true,
463 handle_tm_attribute, NULL },
464 { "transaction_safe_dynamic", 0, 0, true, false, false, false,
465 handle_tm_attribute, NULL },
466 { "transaction_may_cancel_outer", 0, 0, false, true, false, false,
467 handle_tm_attribute, NULL },
468 /* ??? These two attributes didn't make the transition from the
469 Intel language document to the multi-vendor language document. */
470 { "transaction_pure", 0, 0, false, true, false, false,
471 handle_tm_attribute, NULL },
472 { "transaction_wrap", 1, 1, true, false, false, false,
473 handle_tm_wrap_attribute, NULL },
474 /* For internal use (marking of builtins) only. The name contains space
475 to prevent its usage in source code. */
476 { "no vops", 0, 0, true, false, false, false,
477 handle_novops_attribute, NULL },
478 { "deprecated", 0, 1, false, false, false, false,
479 handle_deprecated_attribute, NULL },
480 { "unavailable", 0, 1, false, false, false, false,
481 handle_unavailable_attribute, NULL },
482 { "vector_size", 1, 1, false, true, false, true,
483 handle_vector_size_attribute, NULL },
484 { "vector_mask", 0, 0, false, true, false, true,
485 handle_vector_mask_attribute, NULL },
486 { "visibility", 1, 1, false, false, false, false,
487 handle_visibility_attribute, NULL },
488 { "tls_model", 1, 1, true, false, false, false,
489 handle_tls_model_attribute, NULL },
490 { "nonnull", 0, -1, false, true, true, false,
491 handle_nonnull_attribute, NULL },
492 { "nonnull_if_nonzero", 2, 2, false, true, true, false,
493 handle_nonnull_if_nonzero_attribute, NULL },
494 { "nonstring", 0, 0, true, false, false, false,
495 handle_nonstring_attribute, NULL },
496 { "nothrow", 0, 0, true, false, false, false,
497 handle_nothrow_attribute, NULL },
498 { "expected_throw", 0, 0, true, false, false, false,
499 handle_expected_throw_attribute, NULL },
500 { "may_alias", 0, 0, false, true, false, false, NULL, NULL },
501 { "cleanup", 1, 1, true, false, false, false,
502 handle_cleanup_attribute, NULL },
503 { "warn_unused_result", 0, 0, false, true, true, false,
504 handle_warn_unused_result_attribute,
505 attr_warn_unused_result_exclusions },
506 { "sentinel", 0, 1, false, true, true, false,
507 handle_sentinel_attribute, NULL },
508 /* For internal use (marking of builtins) only. The name contains space
509 to prevent its usage in source code. */
510 { "type generic", 0, 0, false, true, true, false,
511 handle_type_generic_attribute, NULL },
512 { "alloc_size", 1, 2, false, true, true, false,
513 handle_alloc_size_attribute,
514 attr_alloc_exclusions },
515 { "cold", 0, 0, false, false, false, false,
516 handle_cold_attribute,
517 attr_cold_hot_exclusions },
518 { "hot", 0, 0, false, false, false, false,
519 handle_hot_attribute,
520 attr_cold_hot_exclusions },
521 { "no_address_safety_analysis",
522 0, 0, true, false, false, false,
523 handle_no_address_safety_analysis_attribute,
524 NULL },
525 { "no_sanitize", 1, -1, true, false, false, false,
526 handle_no_sanitize_attribute, NULL },
527 { "no_sanitize_address", 0, 0, true, false, false, false,
528 handle_no_sanitize_address_attribute, NULL },
529 { "no_sanitize_thread", 0, 0, true, false, false, false,
530 handle_no_sanitize_thread_attribute, NULL },
531 { "no_sanitize_undefined", 0, 0, true, false, false, false,
532 handle_no_sanitize_undefined_attribute, NULL },
533 { "no_sanitize_coverage", 0, 0, true, false, false, false,
534 handle_no_sanitize_coverage_attribute, NULL },
535 { "asan odr indicator", 0, 0, true, false, false, false,
536 handle_asan_odr_indicator_attribute, NULL },
537 { "warning", 1, 1, true, false, false, false,
538 handle_error_attribute, NULL },
539 { "error", 1, 1, true, false, false, false,
540 handle_error_attribute, NULL },
541 { "target", 1, -1, true, false, false, false,
542 handle_target_attribute,
543 attr_target_exclusions },
544 { "target_version", 1, 1, true, false, false, false,
545 handle_target_version_attribute,
546 attr_target_version_exclusions },
547 { "target_clones", 1, -1, true, false, false, false,
548 handle_target_clones_attribute,
549 attr_target_clones_exclusions },
550 { "optimize", 1, -1, true, false, false, false,
551 handle_optimize_attribute, NULL },
552 /* For internal use only. The leading '*' both prevents its usage in
553 source code and signals that it may be overridden by machine tables. */
554 { "*tm regparm", 0, 0, false, true, true, false,
555 ignore_attribute, NULL },
556 { "no_split_stack", 0, 0, true, false, false, false,
557 handle_no_split_stack_attribute, NULL },
558 { "zero_call_used_regs", 1, 1, true, false, false, false,
559 handle_zero_call_used_regs_attribute, NULL },
560 /* For internal use only (marking of function arguments).
561 The name contains a space to prevent its usage in source code. */
562 { "arg spec", 1, -1, true, false, false, false,
563 handle_argspec_attribute, NULL },
564 /* For internal use (marking of builtins and runtime functions) only.
565 The name contains space to prevent its usage in source code. */
566 { "fn spec", 1, 1, false, true, true, false,
567 handle_fnspec_attribute, NULL },
568 { "warn_unused", 0, 0, false, false, false, false,
569 handle_warn_unused_attribute, NULL },
570 { "returns_nonnull", 0, 0, false, true, true, false,
571 handle_returns_nonnull_attribute, NULL },
572 { "omp declare simd", 0, -1, true, false, false, false,
573 handle_omp_declare_simd_attribute, NULL },
574 { "omp declare variant base", 0, -1, true, false, false, false,
575 handle_omp_declare_variant_attribute, NULL },
576 { "omp declare variant variant", 0, -1, true, false, false, false,
577 handle_omp_declare_variant_attribute, NULL },
578 { "omp declare variant variant args", 0, -1, true, false,
579 false, false,
580 handle_omp_declare_variant_attribute, NULL },
581 { "simd", 0, 1, true, false, false, false,
582 handle_simd_attribute, NULL },
583 { "omp declare target", 0, -1, true, false, false, false,
584 handle_omp_declare_target_attribute, NULL },
585 { "omp declare target link", 0, 0, true, false, false, false,
586 handle_omp_declare_target_attribute, NULL },
587 { "omp declare target implicit", 0, 0, true, false, false, false,
588 handle_omp_declare_target_attribute, NULL },
589 { "omp declare target indirect", 0, 0, true, false, false, false,
590 handle_omp_declare_target_attribute, NULL },
591 { "omp declare target host", 0, 0, true, false, false, false,
592 handle_omp_declare_target_attribute, NULL },
593 { "omp declare target nohost", 0, 0, true, false, false, false,
594 handle_omp_declare_target_attribute, NULL },
595 { "non overlapping", 0, 0, true, false, false, false,
596 handle_non_overlapping_attribute, NULL },
597 { "alloc_align", 1, 1, false, true, true, false,
598 handle_alloc_align_attribute,
599 attr_alloc_exclusions },
600 { "assume_aligned", 1, 2, false, true, true, false,
601 handle_assume_aligned_attribute, NULL },
602 { "designated_init", 0, 0, false, true, false, false,
603 handle_designated_init_attribute, NULL },
604 { "fallthrough", 0, 0, false, false, false, false,
605 handle_fallthrough_attribute, NULL },
606 { "assume", 1, 1, false, false, false, false,
607 handle_assume_attribute, NULL },
608 { "patchable_function_entry", 1, 2, true, false, false, false,
609 handle_patchable_function_entry_attribute,
610 NULL },
611 { "nocf_check", 0, 0, false, true, true, true,
612 handle_nocf_check_attribute, NULL },
613 { "symver", 1, -1, true, false, false, false,
614 handle_symver_attribute, NULL},
615 { "copy", 1, 1, false, false, false, false,
616 handle_copy_attribute, NULL },
617 { "noinit", 0, 0, true, false, false, false,
618 handle_special_var_sec_attribute, attr_section_exclusions },
619 { "persistent", 0, 0, true, false, false, false,
620 handle_special_var_sec_attribute, attr_section_exclusions },
621 { "access", 1, 3, false, true, true, false,
622 handle_access_attribute, NULL },
623 /* Attributes used by Objective-C. */
624 { "NSObject", 0, 0, true, false, false, false,
625 handle_nsobject_attribute, NULL },
626 { "objc_root_class", 0, 0, true, false, false, false,
627 handle_objc_root_class_attribute, NULL },
628 { "objc_nullability", 1, 1, true, false, false, false,
629 handle_objc_nullability_attribute, NULL },
630 { "*dealloc", 1, 2, true, false, false, false,
631 handle_dealloc_attribute, NULL },
632 { "tainted_args", 0, 0, true, false, false, false,
633 handle_tainted_args_attribute, NULL },
634 { "fd_arg", 1, 1, false, true, true, false,
635 handle_fd_arg_attribute, NULL},
636 { "fd_arg_read", 1, 1, false, true, true, false,
637 handle_fd_arg_attribute, NULL},
638 { "fd_arg_write", 1, 1, false, true, true, false,
639 handle_fd_arg_attribute, NULL},
640 { "flag_enum", 0, 0, false, true, false, false,
641 handle_flag_enum_attribute, NULL },
642 { "null_terminated_string_arg", 1, 1, false, true, true, false,
643 handle_null_terminated_string_arg_attribute, NULL}
644 };
645
646 const struct scoped_attribute_specs c_common_gnu_attribute_table =
647 {
648 "gnu", { c_common_gnu_attributes }
649 };
650
651 /* Attributes also recognized in the clang:: namespace. */
652 const struct attribute_spec c_common_clang_attributes[] = {
653 { "flag_enum", 0, 0, false, true, false, false,
654 handle_flag_enum_attribute, NULL },
655 { "musttail", 0, 0, false, false, false,
656 false, handle_musttail_attribute, NULL }
657 };
658
659 const struct scoped_attribute_specs c_common_clang_attribute_table =
660 {
661 "clang", { c_common_clang_attributes }
662 };
663
664 /* Give the specifications for the format attributes, used by C and all
665 descendants.
666
667 Current list of processed format attributes: format, format_arg. */
668 const struct attribute_spec c_common_format_attributes[] =
669 {
670 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
671 affects_type_identity, handler, exclude } */
672 { "format", 3, 3, false, true, true, false,
673 handle_format_attribute, NULL },
674 { "format_arg", 1, 1, false, true, true, false,
675 handle_format_arg_attribute, NULL }
676 };
677
678 const struct scoped_attribute_specs c_common_format_attribute_table =
679 {
680 "gnu", { c_common_format_attributes }
681 };
682
683 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
684 identifier as an argument, so the front end shouldn't look it up. */
685
686 bool
687 attribute_takes_identifier_p (const_tree attr_id)
688 {
689 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
690 if (spec == NULL)
691 /* Unknown attribute that we'll end up ignoring, return true so we
692 don't complain about an identifier argument. */
693 return true;
694 else if (!strcmp ("mode", spec->name)
695 || !strcmp ("format", spec->name)
696 || !strcmp ("cleanup", spec->name)
697 || !strcmp ("access", spec->name)
698 || !strcmp ("counted_by", spec->name))
699 return true;
700 else
701 return targetm.attribute_takes_identifier_p (attr_id);
702 }
703
704 /* Set a musttail attribute MUSTTAIL_P on return expression RETVAL
705 at LOC. */
706
707 void
708 set_musttail_on_return (tree retval, location_t loc, bool musttail_p)
709 {
710 if (retval && musttail_p)
711 {
712 tree t = retval;
713 if (TREE_CODE (t) == TARGET_EXPR)
714 t = TARGET_EXPR_INITIAL (t);
715 if (TREE_CODE (t) != CALL_EXPR)
716 error_at (loc, "cannot tail-call: return value must be a call");
717 else
718 CALL_EXPR_MUST_TAIL_CALL (t) = 1;
719 }
720 else if (musttail_p && !retval)
721 error_at (loc, "cannot tail-call: return value must be a call");
722 }
723
724 /* Verify that argument value POS at position ARGNO to attribute NAME
725 applied to function FN (which is either a function declaration or function
726 type) refers to a function parameter at position POS and the expected type
727 CODE. Treat CODE == INTEGER_TYPE as matching all C integral types except
728 bool. If successful, return POS after default conversions (and possibly
729 adjusted by ADJUST_POS). Otherwise, issue appropriate warnings and return
730 null. A non-zero 1-based ARGNO should be passed in by callers only for
731 attributes with more than one argument.
732
733 N.B. This function modifies POS. */
734
735 tree
736 positional_argument (const_tree fn, const_tree atname, tree &pos,
737 tree_code code, int argno /* = 0 */,
738 int flags /* = posargflags () */)
739 {
740 auto_urlify_attributes sentinel;
741
742 const_tree fndecl = TYPE_P (fn) ? NULL_TREE : fn;
743 const_tree fntype = TYPE_P (fn) ? fn : TREE_TYPE (fn);
744 if (pos && TREE_CODE (pos) != IDENTIFIER_NODE
745 && TREE_CODE (pos) != FUNCTION_DECL)
746 pos = default_conversion (pos);
747
748 tree postype = TREE_TYPE (pos);
749 if (pos == error_mark_node || !postype)
750 {
751 /* Only mention the positional argument number when it's non-zero. */
752 if (argno < 1)
753 warning (OPT_Wattributes,
754 "%qE attribute argument is invalid", atname);
755 else
756 warning (OPT_Wattributes,
757 "%qE attribute argument %i is invalid", atname, argno);
758
759 return NULL_TREE;
760 }
761
762 if (!INTEGRAL_TYPE_P (postype))
763 {
764 /* Handle this case specially to avoid mentioning the value
765 of pointer constants in diagnostics. Only mention
766 the positional argument number when it's non-zero. */
767 if (argno < 1)
768 warning (OPT_Wattributes,
769 "%qE attribute argument has type %qT",
770 atname, postype);
771 else
772 warning (OPT_Wattributes,
773 "%qE attribute argument %i has type %qT",
774 atname, argno, postype);
775
776 return NULL_TREE;
777 }
778
779 if (TREE_CODE (pos) != INTEGER_CST)
780 {
781 /* Only mention the argument number when it's non-zero. */
782 if (argno < 1)
783 warning (OPT_Wattributes,
784 "%qE attribute argument value %qE is not an integer "
785 "constant",
786 atname, pos);
787 else
788 warning (OPT_Wattributes,
789 "%qE attribute argument %i value %qE is not an integer "
790 "constant",
791 atname, argno, pos);
792
793 return NULL_TREE;
794 }
795
796 /* Argument positions are 1-based. */
797 if (integer_zerop (pos))
798 {
799 if (flags & POSARG_ZERO)
800 /* Zero is explicitly allowed. */
801 return pos;
802
803 if (argno < 1)
804 warning (OPT_Wattributes,
805 "%qE attribute argument value %qE does not refer to "
806 "a function parameter",
807 atname, pos);
808 else
809 warning (OPT_Wattributes,
810 "%qE attribute argument %i value %qE does not refer to "
811 "a function parameter",
812 atname, argno, pos);
813
814 return NULL_TREE;
815 }
816
817 if (!prototype_p (fntype))
818 return pos;
819
820 /* ADJUST_POS is non-zero in C++ when the function type has invisible
821 parameters generated by the compiler, such as the in-charge or VTT
822 parameters. */
823 const int adjust_pos = maybe_adjust_arg_pos_for_attribute (fndecl);
824
825 /* Verify that the argument position does not exceed the number
826 of formal arguments to the function. When POSARG_ELLIPSIS
827 is set, ARGNO may be beyond the last argument of a vararg
828 function. */
829 unsigned nargs = type_num_arguments (fntype);
830 if (!nargs
831 || !tree_fits_uhwi_p (pos)
832 || ((flags & POSARG_ELLIPSIS) == 0
833 && !IN_RANGE (tree_to_uhwi (pos) + adjust_pos, 1, nargs)))
834 {
835
836 if (argno < 1)
837 warning (OPT_Wattributes,
838 "%qE attribute argument value %qE exceeds the number "
839 "of function parameters %u",
840 atname, pos, nargs);
841 else
842 warning (OPT_Wattributes,
843 "%qE attribute argument %i value %qE exceeds the number "
844 "of function parameters %u",
845 atname, argno, pos, nargs);
846 return NULL_TREE;
847 }
848
849 /* Verify that the type of the referenced formal argument matches
850 the expected type. Invisible parameters may have been added by
851 the compiler, so adjust the position accordingly. */
852 unsigned HOST_WIDE_INT ipos = tree_to_uhwi (pos) + adjust_pos;
853
854 /* Zero was handled above. */
855 gcc_assert (ipos != 0);
856
857 if (tree argtype = type_argument_type (fntype, ipos))
858 {
859 if (argtype == error_mark_node)
860 return NULL_TREE;
861
862 if (flags & POSARG_ELLIPSIS)
863 {
864 if (argno < 1)
865 error ("%qE attribute argument value %qE does not refer to "
866 "a variable argument list",
867 atname, pos);
868 else
869 error ("%qE attribute argument %i value %qE does not refer to "
870 "a variable argument list",
871 atname, argno, pos);
872 return NULL_TREE;
873 }
874
875 /* Where the expected code is STRING_CST accept any pointer
876 expected by attribute format (this includes possibly qualified
877 char pointers and, for targets like Darwin, also pointers to
878 struct CFString). */
879 bool type_match;
880 if (code == STRING_CST)
881 type_match = valid_format_string_type_p (argtype);
882 else if (code == INTEGER_TYPE)
883 /* For integers, accept enums, wide characters and other types
884 that match INTEGRAL_TYPE_P except for bool. */
885 type_match = (INTEGRAL_TYPE_P (argtype)
886 && TREE_CODE (argtype) != BOOLEAN_TYPE);
887 else
888 type_match = TREE_CODE (argtype) == code;
889
890 if (!type_match)
891 {
892 if (code == STRING_CST)
893 {
894 /* Reject invalid format strings with an error. */
895 if (argno < 1)
896 error ("%qE attribute argument value %qE refers to "
897 "parameter type %qT",
898 atname, pos, argtype);
899 else
900 error ("%qE attribute argument %i value %qE refers to "
901 "parameter type %qT",
902 atname, argno, pos, argtype);
903
904 return NULL_TREE;
905 }
906
907 if (argno < 1)
908 warning (OPT_Wattributes,
909 "%qE attribute argument value %qE refers to "
910 "parameter type %qT",
911 atname, pos, argtype);
912 else
913 warning (OPT_Wattributes,
914 "%qE attribute argument %i value %qE refers to "
915 "parameter type %qT",
916 atname, argno, pos, argtype);
917 return NULL_TREE;
918 }
919 }
920 else if (!(flags & POSARG_ELLIPSIS))
921 {
922 if (argno < 1)
923 warning (OPT_Wattributes,
924 "%qE attribute argument value %qE refers to "
925 "a variadic function parameter of unknown type",
926 atname, pos);
927 else
928 warning (OPT_Wattributes,
929 "%qE attribute argument %i value %qE refers to "
930 "a variadic function parameter of unknown type",
931 atname, argno, pos);
932 return NULL_TREE;
933 }
934
935 return build_int_cst (TREE_TYPE (pos), ipos);
936 }
937
938 /* Return the first of DECL or TYPE attributes installed in NODE if it's
939 a DECL, or TYPE attributes if it's a TYPE, or null otherwise. */
940
941 static tree
942 decl_or_type_attrs (tree node)
943 {
944 if (DECL_P (node))
945 {
946 if (tree attrs = DECL_ATTRIBUTES (node))
947 return attrs;
948
949 tree type = TREE_TYPE (node);
950 if (type == error_mark_node)
951 return NULL_TREE;
952 return TYPE_ATTRIBUTES (type);
953 }
954
955 if (TYPE_P (node))
956 return TYPE_ATTRIBUTES (node);
957
958 return NULL_TREE;
959 }
960
961 /* Given a pair of NODEs for arbitrary DECLs or TYPEs, validate one or
962 two integral or string attribute arguments NEWARGS to be applied to
963 NODE[0] for the absence of conflicts with the same attribute arguments
964 already applied to NODE[1]. Issue a warning for conflicts and return
965 false. Otherwise, when no conflicts are found, return true. */
966
967 static bool
968 validate_attr_args (tree node[2], tree name, tree newargs[2])
969 {
970 /* First validate the arguments against those already applied to
971 the same declaration (or type). */
972 tree self[2] = { node[0], node[0] };
973 if (node[0] != node[1] && !validate_attr_args (self, name, newargs))
974 return false;
975
976 if (!node[1])
977 return true;
978
979 /* Extract the same attribute from the previous declaration or type. */
980 tree prevattr = decl_or_type_attrs (node[1]);
981 const char* const namestr = IDENTIFIER_POINTER (name);
982 prevattr = lookup_attribute (namestr, prevattr);
983 if (!prevattr)
984 return true;
985
986 /* Extract one or both attribute arguments. */
987 tree prevargs[2];
988 prevargs[0] = TREE_VALUE (TREE_VALUE (prevattr));
989 prevargs[1] = TREE_CHAIN (TREE_VALUE (prevattr));
990 if (prevargs[1])
991 prevargs[1] = TREE_VALUE (prevargs[1]);
992
993 /* Both arguments must be equal or, for the second pair, neither must
994 be provided to succeed. */
995 bool arg1eq, arg2eq;
996 if (TREE_CODE (newargs[0]) == INTEGER_CST)
997 {
998 arg1eq = tree_int_cst_equal (newargs[0], prevargs[0]);
999 if (newargs[1] && prevargs[1])
1000 arg2eq = tree_int_cst_equal (newargs[1], prevargs[1]);
1001 else
1002 arg2eq = newargs[1] == prevargs[1];
1003 }
1004 else if (TREE_CODE (newargs[0]) == STRING_CST)
1005 {
1006 const char *s0 = TREE_STRING_POINTER (newargs[0]);
1007 const char *s1 = TREE_STRING_POINTER (prevargs[0]);
1008 arg1eq = strcmp (s0, s1) == 0;
1009 if (newargs[1] && prevargs[1])
1010 {
1011 s0 = TREE_STRING_POINTER (newargs[1]);
1012 s1 = TREE_STRING_POINTER (prevargs[1]);
1013 arg2eq = strcmp (s0, s1) == 0;
1014 }
1015 else
1016 arg2eq = newargs[1] == prevargs[1];
1017 }
1018 else
1019 gcc_unreachable ();
1020
1021 if (arg1eq && arg2eq)
1022 return true;
1023
1024 /* If the two locations are different print a note pointing to
1025 the previous one. */
1026 const location_t curloc = input_location;
1027 const location_t prevloc =
1028 DECL_P (node[1]) ? DECL_SOURCE_LOCATION (node[1]) : curloc;
1029
1030 /* Format the attribute specification for convenience. */
1031 char newspec[80], prevspec[80];
1032 if (newargs[1])
1033 snprintf (newspec, sizeof newspec, "%s (%s, %s)", namestr,
1034 print_generic_expr_to_str (newargs[0]),
1035 print_generic_expr_to_str (newargs[1]));
1036 else
1037 snprintf (newspec, sizeof newspec, "%s (%s)", namestr,
1038 print_generic_expr_to_str (newargs[0]));
1039
1040 if (prevargs[1])
1041 snprintf (prevspec, sizeof prevspec, "%s (%s, %s)", namestr,
1042 print_generic_expr_to_str (prevargs[0]),
1043 print_generic_expr_to_str (prevargs[1]));
1044 else
1045 snprintf (prevspec, sizeof prevspec, "%s (%s)", namestr,
1046 print_generic_expr_to_str (prevargs[0]));
1047
1048 if (warning_at (curloc, OPT_Wattributes,
1049 "ignoring attribute %qs because it conflicts "
1050 "with previous %qs",
1051 newspec, prevspec)
1052 && curloc != prevloc)
1053 inform (prevloc, "previous declaration here");
1054
1055 return false;
1056 }
1057
1058 /* Convenience wrapper for validate_attr_args to validate a single
1059 attribute argument. Used by handlers for attributes that take
1060 just a single argument. */
1061
1062 static bool
1063 validate_attr_arg (tree node[2], tree name, tree newarg)
1064 {
1065 tree argarray[2] = { newarg, NULL_TREE };
1066 return validate_attr_args (node, name, argarray);
1067 }
1068
1069 /* Attribute handlers common to C front ends. */
1070
1071 /* Handle a "signed_bool_precision" attribute; arguments as in
1072 struct attribute_spec.handler. */
1073
1074 static tree
1075 handle_signed_bool_precision_attribute (tree *node, tree name, tree args,
1076 int, bool *no_add_attrs)
1077 {
1078 *no_add_attrs = true;
1079 if (!flag_gimple)
1080 {
1081 warning (OPT_Wattributes, "%qE attribute ignored", name);
1082 return NULL_TREE;
1083 }
1084
1085 if (!TYPE_P (*node) || TREE_CODE (*node) != BOOLEAN_TYPE)
1086 {
1087 warning (OPT_Wattributes, "%qE attribute only supported on "
1088 "boolean types", name);
1089 return NULL_TREE;
1090 }
1091
1092 unsigned HOST_WIDE_INT prec = HOST_WIDE_INT_M1U;
1093 if (tree_fits_uhwi_p (TREE_VALUE (args)))
1094 prec = tree_to_uhwi (TREE_VALUE (args));
1095 if (prec > MAX_FIXED_MODE_SIZE)
1096 {
1097 warning (OPT_Wattributes, "%qE attribute with unsupported boolean "
1098 "precision", name);
1099 return NULL_TREE;
1100 }
1101
1102 tree new_type = build_nonstandard_boolean_type (prec);
1103 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
1104
1105 return NULL_TREE;
1106 }
1107
1108 /* Handle a "hardbool" attribute; arguments as in struct
1109 attribute_spec.handler. */
1110
1111 static tree
1112 handle_hardbool_attribute (tree *node, tree name, tree args,
1113 int /* flags */, bool *no_add_attrs)
1114 {
1115 if (c_language != clk_c)
1116 {
1117 error ("%qE attribute only supported in C", name);
1118 *no_add_attrs = TRUE;
1119 return NULL_TREE;
1120 }
1121
1122 if (!TYPE_P (*node) || TREE_CODE (*node) != INTEGER_TYPE)
1123 {
1124 error ("%qE attribute only supported on "
1125 "integral types", name);
1126 *no_add_attrs = TRUE;
1127 return NULL_TREE;
1128 }
1129
1130 tree orig = *node;
1131 *node = build_duplicate_type (orig);
1132
1133 TREE_SET_CODE (*node, ENUMERAL_TYPE);
1134 ENUM_UNDERLYING_TYPE (*node) = orig;
1135 TYPE_CANONICAL (*node) = TYPE_CANONICAL (orig);
1136
1137 tree false_value;
1138 if (args)
1139 false_value = fold_convert (*node, TREE_VALUE (args));
1140 else
1141 false_value = fold_convert (*node, integer_zero_node);
1142
1143 if (TREE_OVERFLOW_P (false_value))
1144 {
1145 warning (OPT_Wattributes,
1146 "overflows in conversion from %qT to %qT "
1147 "changes value from %qE to %qE",
1148 TREE_TYPE (TREE_VALUE (args)), *node,
1149 TREE_VALUE (args), false_value);
1150 TREE_OVERFLOW (false_value) = false;
1151 }
1152
1153 tree true_value;
1154 if (args && TREE_CHAIN (args))
1155 true_value = fold_convert (*node, TREE_VALUE (TREE_CHAIN (args)));
1156 else
1157 true_value = fold_build1 (BIT_NOT_EXPR, *node, false_value);
1158
1159 if (TREE_OVERFLOW_P (true_value))
1160 {
1161 warning (OPT_Wattributes,
1162 "overflows in conversion from %qT to %qT "
1163 "changes value from %qE to %qE",
1164 TREE_TYPE (TREE_VALUE (TREE_CHAIN (args))), *node,
1165 TREE_VALUE (TREE_CHAIN (args)), true_value);
1166 TREE_OVERFLOW (true_value) = false;
1167 }
1168
1169 if (tree_int_cst_compare (false_value, true_value) == 0)
1170 {
1171 error ("%qE attribute requires different values for"
1172 " %<false%> and %<true%> for type %qT",
1173 name, *node);
1174 *no_add_attrs = TRUE;
1175 return NULL_TREE;
1176 }
1177
1178 tree values = build_tree_list (get_identifier ("false"),
1179 false_value);
1180 TREE_CHAIN (values) = build_tree_list (get_identifier ("true"),
1181 true_value);
1182
1183 /* Do *not* set TYPE_MIN_VALUE, TYPE_MAX_VALUE, nor TYPE_PRECISION according
1184 to the false and true values. That might cause the constants to be the
1185 only acceptable values, which would drop the very hardening checks this
1186 attribute is supposed to add. */
1187
1188 TYPE_ATTRIBUTES (*node) = tree_cons (name, args,
1189 TYPE_ATTRIBUTES (*node));
1190 *no_add_attrs = TRUE;
1191
1192 gcc_checking_assert (!TYPE_CACHED_VALUES_P (*node));
1193 TYPE_VALUES (*node) = values;
1194
1195 if (TREE_CODE (orig) == TYPE_DECL)
1196 TYPE_NAME (*node) = TYPE_NAME (orig);
1197 else
1198 TYPE_NAME (*node) = NULL_TREE;
1199
1200 return NULL_TREE;
1201 }
1202
1203 /* Handle a "packed" attribute; arguments as in
1204 struct attribute_spec.handler. */
1205
1206 static tree
1207 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1208 int flags, bool *no_add_attrs)
1209 {
1210 if (TYPE_P (*node))
1211 {
1212 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1213 {
1214 warning (OPT_Wattributes,
1215 "%qE attribute ignored for type %qT", name, *node);
1216 *no_add_attrs = true;
1217 }
1218 else
1219 TYPE_PACKED (*node) = 1;
1220 }
1221 else if (TREE_CODE (*node) == FIELD_DECL)
1222 {
1223 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
1224 /* Still pack bitfields. */
1225 && ! DECL_C_BIT_FIELD (*node))
1226 warning (OPT_Wattributes,
1227 "%qE attribute ignored for field of type %qT",
1228 name, TREE_TYPE (*node));
1229 else
1230 DECL_PACKED (*node) = 1;
1231 }
1232 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
1233 used for DECL_REGISTER. It wouldn't mean anything anyway.
1234 We can't set DECL_PACKED on the type of a TYPE_DECL, because
1235 that changes what the typedef is typing. */
1236 else
1237 {
1238 warning (OPT_Wattributes, "%qE attribute ignored", name);
1239 *no_add_attrs = true;
1240 }
1241
1242 return NULL_TREE;
1243 }
1244
1245 /* Handle a "nocommon" attribute; arguments as in
1246 struct attribute_spec.handler. */
1247
1248 static tree
1249 handle_nocommon_attribute (tree *node, tree name,
1250 tree ARG_UNUSED (args),
1251 int ARG_UNUSED (flags), bool *no_add_attrs)
1252 {
1253 if (VAR_P (*node))
1254 DECL_COMMON (*node) = 0;
1255 else
1256 {
1257 warning (OPT_Wattributes, "%qE attribute ignored", name);
1258 *no_add_attrs = true;
1259 }
1260
1261 return NULL_TREE;
1262 }
1263
1264 /* Handle a "common" attribute; arguments as in
1265 struct attribute_spec.handler. */
1266
1267 static tree
1268 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1269 int ARG_UNUSED (flags), bool *no_add_attrs)
1270 {
1271 if (VAR_P (*node))
1272 DECL_COMMON (*node) = 1;
1273 else
1274 {
1275 warning (OPT_Wattributes, "%qE attribute ignored", name);
1276 *no_add_attrs = true;
1277 }
1278
1279 return NULL_TREE;
1280 }
1281
1282 /* Handle a "musttail" attribute; arguments as in
1283 struct attribute_spec.handler. */
1284
1285 tree
1286 handle_musttail_attribute (tree ARG_UNUSED (*node), tree name, tree ARG_UNUSED (args),
1287 int ARG_UNUSED (flags), bool *no_add_attrs)
1288 {
1289 /* Currently only a statement attribute, handled directly in parser. */
1290 warning (OPT_Wattributes, "%qE attribute ignored", name);
1291 *no_add_attrs = true;
1292 return NULL_TREE;
1293 }
1294
1295 /* Handle a "noreturn" attribute; arguments as in
1296 struct attribute_spec.handler. */
1297
1298 tree
1299 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1300 int ARG_UNUSED (flags), bool *no_add_attrs)
1301 {
1302 tree type = TREE_TYPE (*node);
1303
1304 /* See FIXME comment in c_common_attribute_table. */
1305 if (TREE_CODE (*node) == FUNCTION_DECL
1306 || objc_method_decl (TREE_CODE (*node)))
1307 TREE_THIS_VOLATILE (*node) = 1;
1308 else if (TREE_CODE (type) == POINTER_TYPE
1309 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1310 TREE_TYPE (*node)
1311 = (build_qualified_type
1312 (build_pointer_type
1313 (build_type_variant (TREE_TYPE (type),
1314 TYPE_READONLY (TREE_TYPE (type)), 1)),
1315 TYPE_QUALS (type)));
1316 else
1317 {
1318 warning (OPT_Wattributes, "%qE attribute ignored", name);
1319 *no_add_attrs = true;
1320 }
1321
1322 return NULL_TREE;
1323 }
1324
1325 /* Handle a "hot" and attribute; arguments as in
1326 struct attribute_spec.handler. */
1327
1328 static tree
1329 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1330 int ARG_UNUSED (flags), bool *no_add_attrs)
1331 {
1332 if (TREE_CODE (*node) == FUNCTION_DECL
1333 || TREE_CODE (*node) == LABEL_DECL)
1334 {
1335 /* Attribute hot processing is done later with lookup_attribute. */
1336 }
1337 else if ((TREE_CODE (*node) == RECORD_TYPE
1338 || TREE_CODE (*node) == UNION_TYPE)
1339 && c_dialect_cxx ()
1340 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1341 {
1342 /* Check conflict here as decl_attributes will otherwise only catch
1343 it late at the function when the attribute is used on a class. */
1344 tree cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (*node));
1345 if (cold_attr)
1346 {
1347 warning (OPT_Wattributes, "ignoring attribute %qE because it "
1348 "conflicts with attribute %qs", name, "cold");
1349 *no_add_attrs = true;
1350 }
1351 }
1352 else if (flags & ((int) ATTR_FLAG_FUNCTION_NEXT
1353 | (int) ATTR_FLAG_DECL_NEXT))
1354 {
1355 /* Avoid applying the attribute to a function return type when
1356 used as: void __attribute ((hot)) foo (void). It will be
1357 passed to the function. */
1358 *no_add_attrs = true;
1359 }
1360 else
1361 {
1362 warning (OPT_Wattributes, "%qE attribute ignored", name);
1363 *no_add_attrs = true;
1364 }
1365
1366 return NULL_TREE;
1367 }
1368
1369 /* Handle a "cold" and attribute; arguments as in
1370 struct attribute_spec.handler. */
1371
1372 static tree
1373 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1374 int ARG_UNUSED (flags), bool *no_add_attrs)
1375 {
1376 if (TREE_CODE (*node) == FUNCTION_DECL
1377 || TREE_CODE (*node) == LABEL_DECL)
1378 {
1379 /* Attribute cold processing is done later with lookup_attribute. */
1380 }
1381 else if ((TREE_CODE (*node) == RECORD_TYPE
1382 || TREE_CODE (*node) == UNION_TYPE)
1383 && c_dialect_cxx ()
1384 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1385 {
1386 /* Check conflict here as decl_attributes will otherwise only catch
1387 it late at the function when the attribute is used on a class. */
1388 tree hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (*node));
1389 if (hot_attr)
1390 {
1391 warning (OPT_Wattributes, "ignoring attribute %qE because it "
1392 "conflicts with attribute %qs", name, "hot");
1393 *no_add_attrs = true;
1394 }
1395 }
1396 else if (flags & ((int) ATTR_FLAG_FUNCTION_NEXT
1397 | (int) ATTR_FLAG_DECL_NEXT))
1398 {
1399 /* Avoid applying the attribute to a function return type when
1400 used as: void __attribute ((cold)) foo (void). It will be
1401 passed to the function. */
1402 *no_add_attrs = true;
1403 }
1404 else
1405 {
1406 warning (OPT_Wattributes, "%qE attribute ignored", name);
1407 *no_add_attrs = true;
1408 }
1409
1410 return NULL_TREE;
1411 }
1412
1413 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
1414
1415 void
1416 add_no_sanitize_value (tree node, unsigned int flags)
1417 {
1418 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
1419 if (attr)
1420 {
1421 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
1422 flags |= old_value;
1423
1424 if (flags == old_value)
1425 return;
1426
1427 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
1428 }
1429 else
1430 DECL_ATTRIBUTES (node)
1431 = tree_cons (get_identifier ("no_sanitize"),
1432 build_int_cst (unsigned_type_node, flags),
1433 DECL_ATTRIBUTES (node));
1434 }
1435
1436 /* Handle a "no_sanitize" attribute; arguments as in
1437 struct attribute_spec.handler. */
1438
1439 static tree
1440 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
1441 bool *no_add_attrs)
1442 {
1443 unsigned int flags = 0;
1444 *no_add_attrs = true;
1445 if (TREE_CODE (*node) != FUNCTION_DECL)
1446 {
1447 warning (OPT_Wattributes, "%qE attribute ignored", name);
1448 return NULL_TREE;
1449 }
1450
1451 for (; args; args = TREE_CHAIN (args))
1452 {
1453 tree id = TREE_VALUE (args);
1454 if (TREE_CODE (id) != STRING_CST)
1455 {
1456 error ("%qE argument not a string", name);
1457 return NULL_TREE;
1458 }
1459
1460 char *string = ASTRDUP (TREE_STRING_POINTER (id));
1461 flags |= parse_no_sanitize_attribute (string);
1462 }
1463
1464 add_no_sanitize_value (*node, flags);
1465
1466 return NULL_TREE;
1467 }
1468
1469 /* Handle a "no_sanitize_address" attribute; arguments as in
1470 struct attribute_spec.handler. */
1471
1472 static tree
1473 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
1474 bool *no_add_attrs)
1475 {
1476 *no_add_attrs = true;
1477 if (TREE_CODE (*node) != FUNCTION_DECL)
1478 warning (OPT_Wattributes, "%qE attribute ignored", name);
1479 else
1480 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
1481
1482 return NULL_TREE;
1483 }
1484
1485 /* Handle a "no_sanitize_thread" attribute; arguments as in
1486 struct attribute_spec.handler. */
1487
1488 static tree
1489 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
1490 bool *no_add_attrs)
1491 {
1492 *no_add_attrs = true;
1493 if (TREE_CODE (*node) != FUNCTION_DECL)
1494 warning (OPT_Wattributes, "%qE attribute ignored", name);
1495 else
1496 add_no_sanitize_value (*node, SANITIZE_THREAD);
1497
1498 return NULL_TREE;
1499 }
1500
1501
1502 /* Handle a "no_address_safety_analysis" attribute; arguments as in
1503 struct attribute_spec.handler. */
1504
1505 static tree
1506 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
1507 bool *no_add_attrs)
1508 {
1509 *no_add_attrs = true;
1510 if (TREE_CODE (*node) != FUNCTION_DECL)
1511 warning (OPT_Wattributes, "%qE attribute ignored", name);
1512 else
1513 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
1514
1515 return NULL_TREE;
1516 }
1517
1518 /* Handle a "no_sanitize_undefined" attribute; arguments as in
1519 struct attribute_spec.handler. */
1520
1521 static tree
1522 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
1523 bool *no_add_attrs)
1524 {
1525 *no_add_attrs = true;
1526 if (TREE_CODE (*node) != FUNCTION_DECL)
1527 warning (OPT_Wattributes, "%qE attribute ignored", name);
1528 else
1529 add_no_sanitize_value (*node,
1530 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
1531
1532 return NULL_TREE;
1533 }
1534
1535 /* Handle a "no_sanitize_coverage" attribute; arguments as in
1536 struct attribute_spec.handler. */
1537
1538 static tree
1539 handle_no_sanitize_coverage_attribute (tree *node, tree name, tree, int,
1540 bool *no_add_attrs)
1541 {
1542 if (TREE_CODE (*node) != FUNCTION_DECL)
1543 {
1544 warning (OPT_Wattributes, "%qE attribute ignored", name);
1545 *no_add_attrs = true;
1546 }
1547
1548 return NULL_TREE;
1549 }
1550
1551 /* Handle an "asan odr indicator" attribute; arguments as in
1552 struct attribute_spec.handler. */
1553
1554 static tree
1555 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
1556 {
1557 return NULL_TREE;
1558 }
1559
1560 /* Handle a "stack_protect" attribute; arguments as in
1561 struct attribute_spec.handler. */
1562
1563 static tree
1564 handle_stack_protect_attribute (tree *node, tree name, tree, int,
1565 bool *no_add_attrs)
1566 {
1567 if (TREE_CODE (*node) != FUNCTION_DECL)
1568 {
1569 warning (OPT_Wattributes, "%qE attribute ignored", name);
1570 *no_add_attrs = true;
1571 }
1572
1573 return NULL_TREE;
1574 }
1575
1576 /* Handle a "no_stack_protector" attribute; arguments as in
1577 struct attribute_spec.handler. */
1578
1579 static tree
1580 handle_no_stack_protector_function_attribute (tree *node, tree name, tree,
1581 int, bool *no_add_attrs)
1582 {
1583 if (TREE_CODE (*node) != FUNCTION_DECL)
1584 {
1585 warning (OPT_Wattributes, "%qE attribute ignored", name);
1586 *no_add_attrs = true;
1587 }
1588
1589 return NULL_TREE;
1590 }
1591
1592 /* Handle a "noipa" attribute; arguments as in
1593 struct attribute_spec.handler. */
1594
1595 static tree
1596 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1597 {
1598 if (TREE_CODE (*node) != FUNCTION_DECL)
1599 {
1600 warning (OPT_Wattributes, "%qE attribute ignored", name);
1601 *no_add_attrs = true;
1602 }
1603
1604 return NULL_TREE;
1605 }
1606
1607 /* Handle a "strub" attribute; arguments as in
1608 struct attribute_spec.handler. */
1609
1610 static tree
1611 handle_strub_attribute (tree *node, tree name,
1612 tree args,
1613 int ARG_UNUSED (flags), bool *no_add_attrs)
1614 {
1615 bool enable = true;
1616
1617 if (args && FUNCTION_POINTER_TYPE_P (*node))
1618 *node = TREE_TYPE (*node);
1619
1620 if (args && FUNC_OR_METHOD_TYPE_P (*node))
1621 {
1622 switch (strub_validate_fn_attr_parm (TREE_VALUE (args)))
1623 {
1624 case 1:
1625 case 2:
1626 enable = true;
1627 break;
1628
1629 case 0:
1630 warning (OPT_Wattributes,
1631 "%qE attribute ignored because of argument %qE",
1632 name, TREE_VALUE (args));
1633 *no_add_attrs = true;
1634 enable = false;
1635 break;
1636
1637 case -1:
1638 case -2:
1639 enable = false;
1640 break;
1641
1642 default:
1643 gcc_unreachable ();
1644 }
1645
1646 args = TREE_CHAIN (args);
1647 }
1648
1649 if (args)
1650 {
1651 warning (OPT_Wattributes,
1652 "ignoring attribute %qE because of excess arguments"
1653 " starting at %qE",
1654 name, TREE_VALUE (args));
1655 *no_add_attrs = true;
1656 enable = false;
1657 }
1658
1659 /* Warn about unmet expectations that the strub attribute works like a
1660 qualifier. ??? Could/should we extend it to the element/field types
1661 here? */
1662 if (TREE_CODE (*node) == ARRAY_TYPE
1663 || VECTOR_TYPE_P (*node)
1664 || TREE_CODE (*node) == COMPLEX_TYPE)
1665 warning (OPT_Wattributes,
1666 "attribute %qE does not apply to elements"
1667 " of non-scalar type %qT",
1668 name, *node);
1669 else if (RECORD_OR_UNION_TYPE_P (*node))
1670 warning (OPT_Wattributes,
1671 "attribute %qE does not apply to fields"
1672 " of aggregate type %qT",
1673 name, *node);
1674
1675 /* If we see a strub-enabling attribute, and we're at the default setting,
1676 implicitly or explicitly, note that the attribute was seen, so that we can
1677 reduce the compile-time overhead to nearly zero when the strub feature is
1678 not used. */
1679 if (enable && flag_strub < -2)
1680 flag_strub += 2;
1681
1682 return NULL_TREE;
1683 }
1684
1685 /* Handle a "noinline" attribute; arguments as in
1686 struct attribute_spec.handler. */
1687
1688 static tree
1689 handle_noinline_attribute (tree *node, tree name,
1690 tree ARG_UNUSED (args),
1691 int ARG_UNUSED (flags), bool *no_add_attrs)
1692 {
1693 if (TREE_CODE (*node) == FUNCTION_DECL)
1694 DECL_UNINLINABLE (*node) = 1;
1695 else
1696 {
1697 warning (OPT_Wattributes, "%qE attribute ignored", name);
1698 *no_add_attrs = true;
1699 }
1700
1701 return NULL_TREE;
1702 }
1703
1704 /* Handle a "noclone" attribute; arguments as in
1705 struct attribute_spec.handler. */
1706
1707 static tree
1708 handle_noclone_attribute (tree *node, tree name,
1709 tree ARG_UNUSED (args),
1710 int ARG_UNUSED (flags), bool *no_add_attrs)
1711 {
1712 if (TREE_CODE (*node) != FUNCTION_DECL)
1713 {
1714 warning (OPT_Wattributes, "%qE attribute ignored", name);
1715 *no_add_attrs = true;
1716 }
1717
1718 return NULL_TREE;
1719 }
1720
1721 /* Handle a "nocf_check" attribute; arguments as in
1722 struct attribute_spec.handler. */
1723
1724 static tree
1725 handle_nocf_check_attribute (tree *node, tree name,
1726 tree ARG_UNUSED (args),
1727 int ARG_UNUSED (flags), bool *no_add_attrs)
1728 {
1729 if (TREE_CODE (*node) != FUNCTION_TYPE
1730 && TREE_CODE (*node) != METHOD_TYPE)
1731 {
1732 warning (OPT_Wattributes, "%qE attribute ignored", name);
1733 *no_add_attrs = true;
1734 }
1735 else if (!(flag_cf_protection & CF_BRANCH))
1736 {
1737 warning (OPT_Wattributes, "%qE attribute ignored. Use "
1738 "%<-fcf-protection%> option to enable it",
1739 name);
1740 *no_add_attrs = true;
1741 }
1742
1743 return NULL_TREE;
1744 }
1745
1746 /* Handle a "no_icf" attribute; arguments as in
1747 struct attribute_spec.handler. */
1748
1749 static tree
1750 handle_noicf_attribute (tree *node, tree name,
1751 tree ARG_UNUSED (args),
1752 int ARG_UNUSED (flags), bool *no_add_attrs)
1753 {
1754 if (TREE_CODE (*node) != FUNCTION_DECL
1755 && (TREE_CODE (*node) != VAR_DECL || !is_global_var (*node)))
1756 {
1757 warning (OPT_Wattributes, "%qE attribute ignored", name);
1758 *no_add_attrs = true;
1759 }
1760
1761 return NULL_TREE;
1762 }
1763
1764
1765 /* Handle a "always_inline" attribute; arguments as in
1766 struct attribute_spec.handler. */
1767
1768 static tree
1769 handle_always_inline_attribute (tree *node, tree name,
1770 tree ARG_UNUSED (args),
1771 int ARG_UNUSED (flags),
1772 bool *no_add_attrs)
1773 {
1774 if (TREE_CODE (*node) == FUNCTION_DECL)
1775 {
1776 /* Set the attribute and mark it for disregarding inline
1777 limits. */
1778 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
1779 }
1780 else
1781 {
1782 warning (OPT_Wattributes, "%qE attribute ignored", name);
1783 *no_add_attrs = true;
1784 }
1785
1786 return NULL_TREE;
1787 }
1788
1789 /* Handle a "gnu_inline" attribute; arguments as in
1790 struct attribute_spec.handler. */
1791
1792 static tree
1793 handle_gnu_inline_attribute (tree *node, tree name,
1794 tree ARG_UNUSED (args),
1795 int ARG_UNUSED (flags),
1796 bool *no_add_attrs)
1797 {
1798 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1799 {
1800 /* Do nothing else, just set the attribute. We'll get at
1801 it later with lookup_attribute. */
1802 }
1803 else
1804 {
1805 warning (OPT_Wattributes, "%qE attribute ignored", name);
1806 *no_add_attrs = true;
1807 }
1808
1809 return NULL_TREE;
1810 }
1811
1812 /* Handle a "leaf" attribute; arguments as in
1813 struct attribute_spec.handler. */
1814
1815 static tree
1816 handle_leaf_attribute (tree *node, tree name,
1817 tree ARG_UNUSED (args),
1818 int ARG_UNUSED (flags), bool *no_add_attrs)
1819 {
1820 if (TREE_CODE (*node) != FUNCTION_DECL)
1821 {
1822 warning (OPT_Wattributes, "%qE attribute ignored", name);
1823 *no_add_attrs = true;
1824 }
1825 if (!TREE_PUBLIC (*node))
1826 {
1827 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
1828 "functions", name);
1829 *no_add_attrs = true;
1830 }
1831
1832 return NULL_TREE;
1833 }
1834
1835 /* Handle an "artificial" attribute; arguments as in
1836 struct attribute_spec.handler. */
1837
1838 static tree
1839 handle_artificial_attribute (tree *node, tree name,
1840 tree ARG_UNUSED (args),
1841 int ARG_UNUSED (flags),
1842 bool *no_add_attrs)
1843 {
1844 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1845 {
1846 /* Do nothing else, just set the attribute. We'll get at
1847 it later with lookup_attribute. */
1848 }
1849 else
1850 {
1851 warning (OPT_Wattributes, "%qE attribute ignored", name);
1852 *no_add_attrs = true;
1853 }
1854
1855 return NULL_TREE;
1856 }
1857
1858 /* Handle a "flatten" attribute; arguments as in
1859 struct attribute_spec.handler. */
1860
1861 static tree
1862 handle_flatten_attribute (tree *node, tree name,
1863 tree args ATTRIBUTE_UNUSED,
1864 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1865 {
1866 if (TREE_CODE (*node) == FUNCTION_DECL)
1867 /* Do nothing else, just set the attribute. We'll get at
1868 it later with lookup_attribute. */
1869 ;
1870 else
1871 {
1872 warning (OPT_Wattributes, "%qE attribute ignored", name);
1873 *no_add_attrs = true;
1874 }
1875
1876 return NULL_TREE;
1877 }
1878
1879 /* Handle a "warning" or "error" attribute; arguments as in
1880 struct attribute_spec.handler. */
1881
1882 static tree
1883 handle_error_attribute (tree *node, tree name, tree args,
1884 int ARG_UNUSED (flags), bool *no_add_attrs)
1885 {
1886 if (TREE_CODE (*node) == FUNCTION_DECL
1887 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
1888 /* Do nothing else, just set the attribute. We'll get at
1889 it later with lookup_attribute. */
1890 ;
1891 else
1892 {
1893 warning (OPT_Wattributes, "%qE attribute ignored", name);
1894 *no_add_attrs = true;
1895 }
1896
1897 return NULL_TREE;
1898 }
1899
1900 /* Handle a "used" attribute; arguments as in
1901 struct attribute_spec.handler. */
1902
1903 static tree
1904 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1905 int ARG_UNUSED (flags), bool *no_add_attrs)
1906 {
1907 tree node = *pnode;
1908
1909 if (TREE_CODE (node) == FUNCTION_DECL
1910 || (VAR_P (node) && TREE_STATIC (node))
1911 || (TREE_CODE (node) == TYPE_DECL))
1912 {
1913 TREE_USED (node) = 1;
1914 DECL_PRESERVE_P (node) = 1;
1915 if (VAR_P (node))
1916 DECL_READ_P (node) = 1;
1917 }
1918 else
1919 {
1920 warning (OPT_Wattributes, "%qE attribute ignored", name);
1921 *no_add_attrs = true;
1922 }
1923
1924 return NULL_TREE;
1925 }
1926
1927 /* Handle a "unused" attribute; arguments as in
1928 struct attribute_spec.handler. */
1929
1930 tree
1931 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1932 int flags, bool *no_add_attrs)
1933 {
1934 if (DECL_P (*node))
1935 {
1936 tree decl = *node;
1937
1938 if (TREE_CODE (decl) == PARM_DECL
1939 || VAR_OR_FUNCTION_DECL_P (decl)
1940 || TREE_CODE (decl) == LABEL_DECL
1941 || TREE_CODE (decl) == CONST_DECL
1942 || TREE_CODE (decl) == FIELD_DECL
1943 || TREE_CODE (decl) == TYPE_DECL)
1944 {
1945 TREE_USED (decl) = 1;
1946 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1947 DECL_READ_P (decl) = 1;
1948 }
1949 else
1950 {
1951 warning (OPT_Wattributes, "%qE attribute ignored", name);
1952 *no_add_attrs = true;
1953 }
1954 }
1955 else
1956 {
1957 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1958 *node = build_variant_type_copy (*node);
1959 TREE_USED (*node) = 1;
1960 }
1961
1962 return NULL_TREE;
1963 }
1964
1965 /* Handle a "retain" attribute; arguments as in
1966 struct attribute_spec.handler. */
1967
1968 static tree
1969 handle_retain_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1970 int ARG_UNUSED (flags), bool *no_add_attrs)
1971 {
1972 tree node = *pnode;
1973
1974 if (SUPPORTS_SHF_GNU_RETAIN
1975 && (TREE_CODE (node) == FUNCTION_DECL
1976 || (VAR_P (node) && TREE_STATIC (node))))
1977 ;
1978 else
1979 {
1980 warning (OPT_Wattributes, "%qE attribute ignored", name);
1981 *no_add_attrs = true;
1982 }
1983
1984 return NULL_TREE;
1985 }
1986
1987 /* Handle an "uninitialized" attribute; arguments as in
1988 struct attribute_spec.handler. */
1989
1990 static tree
1991 handle_uninitialized_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1992 int ARG_UNUSED (flags), bool *no_add_attrs)
1993 {
1994 tree decl = *node;
1995 if (!VAR_P (decl))
1996 {
1997 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
1998 "is not a variable", name, decl);
1999 *no_add_attrs = true;
2000 }
2001 else if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
2002 {
2003 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
2004 "is not a local variable", name, decl);
2005 *no_add_attrs = true;
2006 }
2007
2008 return NULL_TREE;
2009 }
2010
2011 /* Handle a "externally_visible" attribute; arguments as in
2012 struct attribute_spec.handler. */
2013
2014 static tree
2015 handle_externally_visible_attribute (tree *pnode, tree name,
2016 tree ARG_UNUSED (args),
2017 int ARG_UNUSED (flags),
2018 bool *no_add_attrs)
2019 {
2020 tree node = *pnode;
2021
2022 if (VAR_OR_FUNCTION_DECL_P (node))
2023 {
2024 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
2025 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
2026 {
2027 warning (OPT_Wattributes,
2028 "%qE attribute have effect only on public objects", name);
2029 *no_add_attrs = true;
2030 }
2031 }
2032 else
2033 {
2034 warning (OPT_Wattributes, "%qE attribute ignored", name);
2035 *no_add_attrs = true;
2036 }
2037
2038 return NULL_TREE;
2039 }
2040
2041 /* Handle the "no_reorder" attribute. Arguments as in
2042 struct attribute_spec.handler. */
2043
2044 static tree
2045 handle_no_reorder_attribute (tree *pnode,
2046 tree name,
2047 tree,
2048 int,
2049 bool *no_add_attrs)
2050 {
2051 tree node = *pnode;
2052
2053 if (!VAR_OR_FUNCTION_DECL_P (node)
2054 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
2055 {
2056 warning (OPT_Wattributes,
2057 "%qE attribute only affects top level objects",
2058 name);
2059 *no_add_attrs = true;
2060 }
2061
2062 return NULL_TREE;
2063 }
2064
2065 /* Handle a "const" attribute; arguments as in
2066 struct attribute_spec.handler. */
2067
2068 static tree
2069 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2070 int flags, bool *no_add_attrs)
2071 {
2072 tree type = TREE_TYPE (*node);
2073
2074 /* See FIXME comment on noreturn in c_common_attribute_table. */
2075 if (TREE_CODE (*node) == FUNCTION_DECL)
2076 TREE_READONLY (*node) = 1;
2077 else if (TREE_CODE (type) == POINTER_TYPE
2078 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
2079 TREE_TYPE (*node)
2080 = (build_qualified_type
2081 (build_pointer_type
2082 (build_type_variant (TREE_TYPE (type), 1,
2083 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
2084 TYPE_QUALS (type)));
2085 else
2086 {
2087 warning (OPT_Wattributes, "%qE attribute ignored", name);
2088 *no_add_attrs = true;
2089 }
2090
2091 /* void __builtin_unreachable(void) is const. Accept other such
2092 built-ins but warn on user-defined functions that return void. */
2093 if (!(flags & ATTR_FLAG_BUILT_IN)
2094 && TREE_CODE (*node) == FUNCTION_DECL
2095 && VOID_TYPE_P (TREE_TYPE (type)))
2096 warning (OPT_Wattributes, "%qE attribute on function "
2097 "returning %<void%>", name);
2098
2099 return NULL_TREE;
2100 }
2101
2102 /* Handle a "scalar_storage_order" attribute; arguments as in
2103 struct attribute_spec.handler. */
2104
2105 static tree
2106 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
2107 int flags, bool *no_add_attrs)
2108 {
2109 tree id = TREE_VALUE (args);
2110 tree type;
2111
2112 if (TREE_CODE (*node) == TYPE_DECL
2113 && ! (flags & ATTR_FLAG_CXX11))
2114 node = &TREE_TYPE (*node);
2115 type = *node;
2116
2117 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2118 {
2119 error ("%qE attribute is not supported because endianness is not uniform",
2120 name);
2121 return NULL_TREE;
2122 }
2123
2124 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
2125 {
2126 bool reverse = false;
2127
2128 if (TREE_CODE (id) == STRING_CST
2129 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
2130 reverse = !BYTES_BIG_ENDIAN;
2131 else if (TREE_CODE (id) == STRING_CST
2132 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
2133 reverse = BYTES_BIG_ENDIAN;
2134 else
2135 {
2136 error ("attribute %qE argument must be one of %qs or %qs",
2137 name, "big-endian", "little-endian");
2138 return NULL_TREE;
2139 }
2140
2141 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2142 {
2143 if (reverse)
2144 /* A type variant isn't good enough, since we don't want a cast
2145 to such a type to be removed as a no-op. */
2146 *node = type = build_duplicate_type (type);
2147 }
2148
2149 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
2150 return NULL_TREE;
2151 }
2152
2153 warning (OPT_Wattributes, "%qE attribute ignored", name);
2154 *no_add_attrs = true;
2155 return NULL_TREE;
2156 }
2157
2158 /* Handle a "transparent_union" attribute; arguments as in
2159 struct attribute_spec.handler. */
2160
2161 static tree
2162 handle_transparent_union_attribute (tree *node, tree name,
2163 tree ARG_UNUSED (args), int flags,
2164 bool *no_add_attrs)
2165 {
2166 tree type;
2167
2168 *no_add_attrs = true;
2169
2170 if (TREE_CODE (*node) == TYPE_DECL
2171 && ! (flags & ATTR_FLAG_CXX11))
2172 node = &TREE_TYPE (*node);
2173 type = *node;
2174
2175 if (TREE_CODE (type) == UNION_TYPE)
2176 {
2177 /* Make sure that the first field will work for a transparent union.
2178 If the type isn't complete yet, leave the check to the code in
2179 finish_struct. */
2180 if (TYPE_SIZE (type))
2181 {
2182 tree first = first_field (type);
2183 if (first == NULL_TREE
2184 || DECL_ARTIFICIAL (first)
2185 || TYPE_MODE (type) != DECL_MODE (first))
2186 goto ignored;
2187 }
2188
2189 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2190 {
2191 /* If the type isn't complete yet, setting the flag
2192 on a variant wouldn't ever be checked. */
2193 if (!TYPE_SIZE (type))
2194 goto ignored;
2195
2196 /* build_duplicate_type doesn't work for C++. */
2197 if (c_dialect_cxx ())
2198 goto ignored;
2199
2200 /* A type variant isn't good enough, since we don't want a cast
2201 to such a type to be removed as a no-op. */
2202 *node = type = build_duplicate_type (type);
2203 }
2204
2205 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2206 TYPE_TRANSPARENT_AGGR (t) = 1;
2207 return NULL_TREE;
2208 }
2209
2210 ignored:
2211 warning (OPT_Wattributes, "%qE attribute ignored", name);
2212 return NULL_TREE;
2213 }
2214
2215 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
2216 get the requested priority for a constructor or destructor,
2217 possibly issuing diagnostics for invalid or reserved
2218 priorities. */
2219
2220 static priority_type
2221 get_priority (tree args, bool is_destructor)
2222 {
2223 HOST_WIDE_INT pri;
2224 tree arg;
2225
2226 if (!args)
2227 return DEFAULT_INIT_PRIORITY;
2228
2229 if (!SUPPORTS_INIT_PRIORITY)
2230 {
2231 if (is_destructor)
2232 error ("destructor priorities are not supported");
2233 else
2234 error ("constructor priorities are not supported");
2235 return DEFAULT_INIT_PRIORITY;
2236 }
2237
2238 arg = TREE_VALUE (args);
2239 if (TREE_CODE (arg) == IDENTIFIER_NODE || TREE_CODE (arg) == FUNCTION_DECL)
2240 goto invalid;
2241 if (arg == error_mark_node)
2242 return DEFAULT_INIT_PRIORITY;
2243 arg = default_conversion (arg);
2244 if (!tree_fits_shwi_p (arg)
2245 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
2246 goto invalid;
2247
2248 pri = tree_to_shwi (arg);
2249 if (pri < 0 || pri > MAX_INIT_PRIORITY)
2250 goto invalid;
2251
2252 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2253 {
2254 if (is_destructor)
2255 warning (OPT_Wprio_ctor_dtor,
2256 "destructor priorities from 0 to %d are reserved "
2257 "for the implementation",
2258 MAX_RESERVED_INIT_PRIORITY);
2259 else
2260 warning (OPT_Wprio_ctor_dtor,
2261 "constructor priorities from 0 to %d are reserved "
2262 "for the implementation",
2263 MAX_RESERVED_INIT_PRIORITY);
2264 }
2265 return pri;
2266
2267 invalid:
2268 if (is_destructor)
2269 error ("destructor priorities must be integers from 0 to %d inclusive",
2270 MAX_INIT_PRIORITY);
2271 else
2272 error ("constructor priorities must be integers from 0 to %d inclusive",
2273 MAX_INIT_PRIORITY);
2274 return DEFAULT_INIT_PRIORITY;
2275 }
2276
2277 /* Handle a "constructor" attribute; arguments as in
2278 struct attribute_spec.handler. */
2279
2280 static tree
2281 handle_constructor_attribute (tree *node, tree name, tree args,
2282 int ARG_UNUSED (flags),
2283 bool *no_add_attrs)
2284 {
2285 tree decl = *node;
2286 tree type = TREE_TYPE (decl);
2287
2288 if (TREE_CODE (decl) == FUNCTION_DECL
2289 && TREE_CODE (type) == FUNCTION_TYPE
2290 && decl_function_context (decl) == 0)
2291 {
2292 priority_type priority;
2293 DECL_STATIC_CONSTRUCTOR (decl) = 1;
2294 priority = get_priority (args, /*is_destructor=*/false);
2295 SET_DECL_INIT_PRIORITY (decl, priority);
2296 TREE_USED (decl) = 1;
2297 }
2298 else
2299 {
2300 warning (OPT_Wattributes, "%qE attribute ignored", name);
2301 *no_add_attrs = true;
2302 }
2303
2304 return NULL_TREE;
2305 }
2306
2307 /* Handle a "destructor" attribute; arguments as in
2308 struct attribute_spec.handler. */
2309
2310 static tree
2311 handle_destructor_attribute (tree *node, tree name, tree args,
2312 int ARG_UNUSED (flags),
2313 bool *no_add_attrs)
2314 {
2315 tree decl = *node;
2316 tree type = TREE_TYPE (decl);
2317
2318 if (TREE_CODE (decl) == FUNCTION_DECL
2319 && TREE_CODE (type) == FUNCTION_TYPE
2320 && decl_function_context (decl) == 0)
2321 {
2322 priority_type priority;
2323 DECL_STATIC_DESTRUCTOR (decl) = 1;
2324 priority = get_priority (args, /*is_destructor=*/true);
2325 SET_DECL_FINI_PRIORITY (decl, priority);
2326 TREE_USED (decl) = 1;
2327 }
2328 else
2329 {
2330 warning (OPT_Wattributes, "%qE attribute ignored", name);
2331 *no_add_attrs = true;
2332 }
2333
2334 return NULL_TREE;
2335 }
2336
2337 /* Nonzero if the mode is a valid vector mode for this architecture.
2338 This returns nonzero even if there is no hardware support for the
2339 vector mode, but we can emulate with narrower modes. */
2340
2341 static bool
2342 vector_mode_valid_p (machine_mode mode)
2343 {
2344 enum mode_class mclass = GET_MODE_CLASS (mode);
2345
2346 /* Doh! What's going on? */
2347 if (mclass != MODE_VECTOR_INT
2348 && mclass != MODE_VECTOR_FLOAT
2349 && mclass != MODE_VECTOR_FRACT
2350 && mclass != MODE_VECTOR_UFRACT
2351 && mclass != MODE_VECTOR_ACCUM
2352 && mclass != MODE_VECTOR_UACCUM)
2353 return false;
2354
2355 /* Hardware support. Woo hoo! */
2356 if (targetm.vector_mode_supported_p (mode))
2357 return true;
2358
2359 /* We should probably return 1 if requesting V4DI and we have no DI,
2360 but we have V2DI, but this is probably very unlikely. */
2361
2362 /* If we have support for the inner mode, we can safely emulate it.
2363 We may not have V2DI, but me can emulate with a pair of DIs. */
2364 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
2365 }
2366
2367
2368 /* Handle a "mode" attribute; arguments as in
2369 struct attribute_spec.handler. */
2370
2371 static tree
2372 handle_mode_attribute (tree *node, tree name, tree args,
2373 int ARG_UNUSED (flags), bool *no_add_attrs)
2374 {
2375 tree type = *node;
2376 tree ident = TREE_VALUE (args);
2377
2378 *no_add_attrs = true;
2379
2380 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2381 warning (OPT_Wattributes, "%qE attribute ignored", name);
2382 else
2383 {
2384 int j;
2385 const char *p = IDENTIFIER_POINTER (ident);
2386 int len = strlen (p);
2387 machine_mode mode = VOIDmode;
2388 tree typefm;
2389 bool valid_mode;
2390
2391 if (len > 4 && p[0] == '_' && p[1] == '_'
2392 && p[len - 1] == '_' && p[len - 2] == '_')
2393 {
2394 char *newp = (char *) alloca (len - 1);
2395
2396 strcpy (newp, &p[2]);
2397 newp[len - 4] = '\0';
2398 p = newp;
2399 }
2400
2401 /* Change this type to have a type with the specified mode.
2402 First check for the special modes. */
2403 if (!strcmp (p, "byte"))
2404 mode = byte_mode;
2405 else if (!strcmp (p, "word"))
2406 mode = word_mode;
2407 else if (!strcmp (p, "pointer"))
2408 mode = ptr_mode;
2409 else if (!strcmp (p, "libgcc_cmp_return"))
2410 mode = targetm.libgcc_cmp_return_mode ();
2411 else if (!strcmp (p, "libgcc_shift_count"))
2412 mode = targetm.libgcc_shift_count_mode ();
2413 else if (!strcmp (p, "unwind_word"))
2414 mode = targetm.unwind_word_mode ();
2415 else
2416 for (j = 0; j < NUM_MACHINE_MODES; j++)
2417 if (!strcmp (p, GET_MODE_NAME (j)))
2418 {
2419 mode = (machine_mode) j;
2420 break;
2421 }
2422
2423 if (mode == VOIDmode)
2424 {
2425 error ("unknown machine mode %qE", ident);
2426 return NULL_TREE;
2427 }
2428
2429 /* Allow the target a chance to translate MODE into something supported.
2430 See PR86324. */
2431 mode = targetm.translate_mode_attribute (mode);
2432
2433 valid_mode = false;
2434 switch (GET_MODE_CLASS (mode))
2435 {
2436 case MODE_INT:
2437 case MODE_PARTIAL_INT:
2438 case MODE_FLOAT:
2439 case MODE_DECIMAL_FLOAT:
2440 case MODE_FRACT:
2441 case MODE_UFRACT:
2442 case MODE_ACCUM:
2443 case MODE_UACCUM:
2444 valid_mode
2445 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
2446 break;
2447
2448 case MODE_COMPLEX_INT:
2449 case MODE_COMPLEX_FLOAT:
2450 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
2451 break;
2452
2453 case MODE_VECTOR_INT:
2454 case MODE_VECTOR_FLOAT:
2455 case MODE_VECTOR_FRACT:
2456 case MODE_VECTOR_UFRACT:
2457 case MODE_VECTOR_ACCUM:
2458 case MODE_VECTOR_UACCUM:
2459 warning (OPT_Wattributes, "specifying vector types with "
2460 "%<__attribute__ ((mode))%> is deprecated");
2461 inform (input_location,
2462 "use %<__attribute__ ((vector_size))%> instead");
2463 valid_mode = vector_mode_valid_p (mode);
2464 break;
2465
2466 default:
2467 break;
2468 }
2469 if (!valid_mode)
2470 {
2471 error ("unable to emulate %qs", p);
2472 return NULL_TREE;
2473 }
2474
2475 if (POINTER_TYPE_P (type))
2476 {
2477 scalar_int_mode addr_mode;
2478 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
2479 tree (*fn)(tree, machine_mode, bool);
2480
2481 if (!is_a <scalar_int_mode> (mode, &addr_mode)
2482 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
2483 {
2484 error ("invalid pointer mode %qs", p);
2485 return NULL_TREE;
2486 }
2487
2488 if (TREE_CODE (type) == POINTER_TYPE)
2489 fn = build_pointer_type_for_mode;
2490 else
2491 fn = build_reference_type_for_mode;
2492 typefm = fn (TREE_TYPE (type), addr_mode, false);
2493 }
2494 else
2495 {
2496 /* For fixed-point modes, we need to test if the signness of type
2497 and the machine mode are consistent. */
2498 if (ALL_FIXED_POINT_MODE_P (mode)
2499 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
2500 {
2501 error ("signedness of type and machine mode %qs don%'t match", p);
2502 return NULL_TREE;
2503 }
2504 /* For fixed-point modes, we need to pass saturating info. */
2505 typefm = lang_hooks.types.type_for_mode (mode,
2506 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
2507 : TYPE_UNSIGNED (type));
2508 }
2509
2510 if (typefm == NULL_TREE)
2511 {
2512 error ("no data type for mode %qs", p);
2513 return NULL_TREE;
2514 }
2515 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2516 {
2517 /* For enumeral types, copy the precision from the integer
2518 type returned above. If not an INTEGER_TYPE, we can't use
2519 this mode for this type. */
2520 if (TREE_CODE (typefm) != INTEGER_TYPE)
2521 {
2522 error ("cannot use mode %qs for enumerated types", p);
2523 return NULL_TREE;
2524 }
2525
2526 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
2527 {
2528 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
2529 typefm = type;
2530 }
2531 else
2532 {
2533 /* We cannot build a type variant, as there's code that assumes
2534 that TYPE_MAIN_VARIANT has the same mode. This includes the
2535 debug generators. Instead, create a subrange type. This
2536 results in all of the enumeral values being emitted only once
2537 in the original, and the subtype gets them by reference. */
2538 if (TYPE_UNSIGNED (type))
2539 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
2540 else
2541 typefm = make_signed_type (TYPE_PRECISION (typefm));
2542 TREE_TYPE (typefm) = type;
2543 }
2544 *no_add_attrs = false;
2545 }
2546 else if (VECTOR_MODE_P (mode)
2547 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
2548 : TREE_CODE (type) != TREE_CODE (typefm))
2549 {
2550 error ("mode %qs applied to inappropriate type", p);
2551 return NULL_TREE;
2552 }
2553
2554 /* Copy any quals and attributes to the new type. */
2555 *node = build_type_attribute_qual_variant (typefm, TYPE_ATTRIBUTES (type),
2556 TYPE_QUALS (type));
2557 if (TYPE_USER_ALIGN (type))
2558 *node = build_aligned_type (*node, TYPE_ALIGN (type));
2559 }
2560
2561 return NULL_TREE;
2562 }
2563
2564 /* Handle a "section" attribute; arguments as in
2565 struct attribute_spec.handler. */
2566
2567 static tree
2568 handle_section_attribute (tree *node, tree name, tree args,
2569 int flags, bool *no_add_attrs)
2570 {
2571 tree decl = *node;
2572 tree res = NULL_TREE;
2573 tree argval = TREE_VALUE (args);
2574 const char* new_section_name;
2575
2576 if (!targetm_common.have_named_sections)
2577 {
2578 error_at (DECL_SOURCE_LOCATION (*node),
2579 "section attributes are not supported for this target");
2580 goto fail;
2581 }
2582
2583 if (!VAR_OR_FUNCTION_DECL_P (decl))
2584 {
2585 error ("section attribute not allowed for %q+D", *node);
2586 goto fail;
2587 }
2588
2589 if (TREE_CODE (argval) != STRING_CST)
2590 {
2591 error ("section attribute argument not a string constant");
2592 goto fail;
2593 }
2594
2595 if (VAR_P (decl)
2596 && current_function_decl != NULL_TREE
2597 && !TREE_STATIC (decl))
2598 {
2599 error_at (DECL_SOURCE_LOCATION (decl),
2600 "section attribute cannot be specified for local variables");
2601 goto fail;
2602 }
2603
2604 new_section_name = TREE_STRING_POINTER (argval);
2605
2606 /* The decl may have already been given a section attribute
2607 from a previous declaration. Ensure they match. */
2608 if (const char* const old_section_name = DECL_SECTION_NAME (decl))
2609 if (strcmp (old_section_name, new_section_name) != 0)
2610 {
2611 error ("section of %q+D conflicts with previous declaration",
2612 *node);
2613 goto fail;
2614 }
2615
2616 if (VAR_P (decl)
2617 && !targetm.have_tls && targetm.emutls.tmpl_section
2618 && DECL_THREAD_LOCAL_P (decl))
2619 {
2620 error ("section of %q+D cannot be overridden", *node);
2621 goto fail;
2622 }
2623
2624 if (!validate_attr_arg (node, name, argval))
2625 goto fail;
2626
2627 res = targetm.handle_generic_attribute (node, name, args, flags,
2628 no_add_attrs);
2629
2630 /* If the back end confirms the attribute can be added then continue onto
2631 final processing. */
2632 if (!(*no_add_attrs))
2633 {
2634 set_decl_section_name (decl, new_section_name);
2635 return res;
2636 }
2637
2638 fail:
2639 *no_add_attrs = true;
2640 return res;
2641 }
2642
2643 /* Common codes shared by handle_warn_if_not_aligned_attribute and
2644 handle_aligned_attribute. */
2645
2646 static tree
2647 common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
2648 bool *no_add_attrs,
2649 bool warn_if_not_aligned_p)
2650 {
2651 tree decl = NULL_TREE;
2652 tree *type = NULL;
2653 bool is_type = false;
2654 tree align_expr;
2655
2656 /* The last (already pushed) declaration with all validated attributes
2657 merged in or the current about-to-be-pushed one if one hasn't been
2658 yet. */
2659 tree last_decl = node[1] ? node[1] : *node;
2660
2661 if (args)
2662 {
2663 align_expr = TREE_VALUE (args);
2664 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
2665 && TREE_CODE (align_expr) != FUNCTION_DECL)
2666 align_expr = default_conversion (align_expr);
2667 }
2668 else
2669 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
2670
2671 if (DECL_P (*node))
2672 {
2673 decl = *node;
2674 type = &TREE_TYPE (decl);
2675 is_type = TREE_CODE (*node) == TYPE_DECL;
2676 }
2677 else if (TYPE_P (*node))
2678 type = node, is_type = true;
2679
2680 /* True to consider invalid alignments greater than MAX_OFILE_ALIGNMENT. */
2681 bool objfile = (TREE_CODE (*node) == FUNCTION_DECL
2682 || (VAR_P (*node) && TREE_STATIC (*node)));
2683 /* Log2 of specified alignment. */
2684 int pow2align = check_user_alignment (align_expr, objfile,
2685 /* warn_zero = */ true);
2686 if (pow2align == -1)
2687 {
2688 *no_add_attrs = true;
2689 return NULL_TREE;
2690 }
2691
2692 /* The alignment in bits corresponding to the specified alignment. */
2693 unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
2694
2695 /* The alignment of the current declaration and that of the last
2696 pushed declaration, determined on demand below. */
2697 unsigned curalign = 0;
2698 unsigned lastalign = 0;
2699
2700 /* True when SET_DECL_ALIGN() should be called for the decl when
2701 *NO_ADD_ATTRS is false. */
2702 bool set_align = true;
2703 if (is_type)
2704 {
2705 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2706 /* OK, modify the type in place. */;
2707 /* If we have a TYPE_DECL, then copy the type, so that we
2708 don't accidentally modify a builtin type. See pushdecl. */
2709 else if (decl && TREE_TYPE (decl) != error_mark_node
2710 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
2711 {
2712 tree tt = TREE_TYPE (decl);
2713 *type = build_variant_type_copy (*type);
2714 DECL_ORIGINAL_TYPE (decl) = tt;
2715 TYPE_NAME (*type) = decl;
2716 TREE_USED (*type) = TREE_USED (decl);
2717 TREE_TYPE (decl) = *type;
2718 }
2719 else
2720 *type = build_variant_type_copy (*type);
2721
2722 if (warn_if_not_aligned_p)
2723 {
2724 SET_TYPE_WARN_IF_NOT_ALIGN (*type, bitalign);
2725 warn_if_not_aligned_p = false;
2726 }
2727 else
2728 {
2729 SET_TYPE_ALIGN (*type, bitalign);
2730 TYPE_USER_ALIGN (*type) = 1;
2731 }
2732 }
2733 else if (! VAR_OR_FUNCTION_DECL_P (decl)
2734 && TREE_CODE (decl) != FIELD_DECL)
2735 {
2736 error ("alignment may not be specified for %q+D", decl);
2737 *no_add_attrs = true;
2738 }
2739 else if (TREE_CODE (decl) == FUNCTION_DECL
2740 && (((curalign = DECL_ALIGN (decl)) > bitalign)
2741 | ((lastalign = DECL_ALIGN (last_decl)) > bitalign)))
2742 {
2743 /* Either a prior attribute on the same declaration or one
2744 on a prior declaration of the same function specifies
2745 stricter alignment than this attribute. */
2746 bool note = (lastalign > curalign
2747 || (lastalign == curalign
2748 && (DECL_USER_ALIGN (last_decl)
2749 > DECL_USER_ALIGN (decl))));
2750 if (note)
2751 curalign = lastalign;
2752
2753 curalign /= BITS_PER_UNIT;
2754 unsigned newalign = bitalign / BITS_PER_UNIT;
2755
2756 auto_diagnostic_group d;
2757 if ((DECL_USER_ALIGN (decl)
2758 || DECL_USER_ALIGN (last_decl)))
2759 {
2760 if (warning (OPT_Wattributes,
2761 "ignoring attribute %<%E (%u)%> because it conflicts "
2762 "with attribute %<%E (%u)%>",
2763 name, newalign, name, curalign)
2764 && note)
2765 inform (DECL_SOURCE_LOCATION (last_decl),
2766 "previous declaration here");
2767 /* Only reject attempts to relax/override an alignment
2768 explicitly specified previously and accept declarations
2769 that appear to relax the implicit function alignment for
2770 the target. Both increasing and increasing the alignment
2771 set by -falign-functions setting is permitted. */
2772 *no_add_attrs = true;
2773 }
2774 else if (!warn_if_not_aligned_p)
2775 {
2776 /* Do not fail for attribute warn_if_not_aligned. Otherwise,
2777 silently avoid applying the alignment to the declaration
2778 because it's implicitly satisfied by the target. Apply
2779 the attribute nevertheless so it can be retrieved by
2780 __builtin_has_attribute. */
2781 set_align = false;
2782 }
2783 }
2784 else if (DECL_USER_ALIGN (decl)
2785 && DECL_ALIGN (decl) > bitalign)
2786 /* C++-11 [dcl.align/4]:
2787
2788 When multiple alignment-specifiers are specified for an
2789 entity, the alignment requirement shall be set to the
2790 strictest specified alignment.
2791
2792 This formally comes from the c++11 specification but we are
2793 doing it for the GNU attribute syntax as well. */
2794 *no_add_attrs = true;
2795 else if (warn_if_not_aligned_p
2796 && TREE_CODE (decl) == FIELD_DECL
2797 && !DECL_C_BIT_FIELD (decl))
2798 {
2799 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
2800 warn_if_not_aligned_p = false;
2801 set_align = false;
2802 }
2803
2804 if (warn_if_not_aligned_p)
2805 {
2806 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
2807 decl);
2808 *no_add_attrs = true;
2809 }
2810 else if (!is_type && !*no_add_attrs && set_align)
2811 {
2812 SET_DECL_ALIGN (decl, bitalign);
2813 DECL_USER_ALIGN (decl) = 1;
2814 }
2815
2816 return NULL_TREE;
2817 }
2818
2819 /* Handle a "aligned" attribute; arguments as in
2820 struct attribute_spec.handler. */
2821
2822 tree
2823 handle_aligned_attribute (tree *node, tree name, tree args,
2824 int flags, bool *no_add_attrs)
2825 {
2826 return common_handle_aligned_attribute (node, name, args, flags,
2827 no_add_attrs, false);
2828 }
2829
2830 /* Handle a "warn_if_not_aligned" attribute; arguments as in
2831 struct attribute_spec.handler. */
2832
2833 static tree
2834 handle_warn_if_not_aligned_attribute (tree *node, tree name,
2835 tree args, int flags,
2836 bool *no_add_attrs)
2837 {
2838 return common_handle_aligned_attribute (node, name, args, flags,
2839 no_add_attrs, true);
2840 }
2841
2842 /* Handle a "strict_flex_array" attribute; arguments as in
2843 struct attribute_spec.handler. */
2844
2845 static tree
2846 handle_strict_flex_array_attribute (tree *node, tree name,
2847 tree args, int ARG_UNUSED (flags),
2848 bool *no_add_attrs)
2849 {
2850 tree decl = *node;
2851 tree argval = TREE_VALUE (args);
2852
2853 /* This attribute only applies to field decls of a structure. */
2854 if (TREE_CODE (decl) != FIELD_DECL)
2855 {
2856 error_at (DECL_SOURCE_LOCATION (decl),
2857 "%qE attribute may not be specified for %q+D", name, decl);
2858 *no_add_attrs = true;
2859 }
2860 /* This attribute only applies to field with array type. */
2861 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2862 {
2863 error_at (DECL_SOURCE_LOCATION (decl),
2864 "%qE attribute may not be specified for a non-array field",
2865 name);
2866 *no_add_attrs = true;
2867 }
2868 else if (TREE_CODE (argval) != INTEGER_CST)
2869 {
2870 error_at (DECL_SOURCE_LOCATION (decl),
2871 "%qE attribute argument not an integer", name);
2872 *no_add_attrs = true;
2873 }
2874 else if (!tree_fits_uhwi_p (argval) || tree_to_uhwi (argval) > 3)
2875 {
2876 error_at (DECL_SOURCE_LOCATION (decl),
2877 "%qE attribute argument %qE is not an integer constant"
2878 " between 0 and 3", name, argval);
2879 *no_add_attrs = true;
2880 }
2881
2882 return NULL_TREE;
2883 }
2884
2885 /* Handle a "counted_by" attribute; arguments as in
2886 struct attribute_spec.handler. */
2887
2888 static tree
2889 handle_counted_by_attribute (tree *node, tree name,
2890 tree args, int ARG_UNUSED (flags),
2891 bool *no_add_attrs)
2892 {
2893 tree decl = *node;
2894 tree argval = TREE_VALUE (args);
2895 tree old_counted_by = lookup_attribute ("counted_by", DECL_ATTRIBUTES (decl));
2896
2897 /* This attribute is not supported in C++. */
2898 if (c_dialect_cxx ())
2899 {
2900 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2901 "%qE attribute is not supported for C++ for now, ignored",
2902 name);
2903 *no_add_attrs = true;
2904 }
2905 /* This attribute only applies to field decls of a structure. */
2906 else if (TREE_CODE (decl) != FIELD_DECL)
2907 {
2908 error_at (DECL_SOURCE_LOCATION (decl),
2909 "%qE attribute is not allowed for a non-field"
2910 " declaration %q+D", name, decl);
2911 *no_add_attrs = true;
2912 }
2913 /* This attribute only applies to field with array type. */
2914 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2915 {
2916 error_at (DECL_SOURCE_LOCATION (decl),
2917 "%qE attribute is not allowed for a non-array field",
2918 name);
2919 *no_add_attrs = true;
2920 }
2921 /* This attribute only applies to a C99 flexible array member type. */
2922 else if (! c_flexible_array_member_type_p (TREE_TYPE (decl)))
2923 {
2924 error_at (DECL_SOURCE_LOCATION (decl),
2925 "%qE attribute is not allowed for a non-flexible"
2926 " array member field", name);
2927 *no_add_attrs = true;
2928 }
2929 /* The argument should be an identifier. */
2930 else if (TREE_CODE (argval) != IDENTIFIER_NODE)
2931 {
2932 error_at (DECL_SOURCE_LOCATION (decl),
2933 "%<counted_by%> argument is not an identifier");
2934 *no_add_attrs = true;
2935 }
2936 /* Issue error when there is a counted_by attribute with a different
2937 field as the argument for the same flexible array member field. */
2938 else if (old_counted_by != NULL_TREE)
2939 {
2940 tree old_fieldname = TREE_VALUE (TREE_VALUE (old_counted_by));
2941 if (strcmp (IDENTIFIER_POINTER (old_fieldname),
2942 IDENTIFIER_POINTER (argval)) != 0)
2943 {
2944 error_at (DECL_SOURCE_LOCATION (decl),
2945 "%<counted_by%> argument %qE conflicts with"
2946 " previous declaration %qE", argval, old_fieldname);
2947 *no_add_attrs = true;
2948 }
2949 }
2950
2951 return NULL_TREE;
2952 }
2953
2954 /* Handle a "weak" attribute; arguments as in
2955 struct attribute_spec.handler. */
2956
2957 static tree
2958 handle_weak_attribute (tree *node, tree name,
2959 tree ARG_UNUSED (args),
2960 int ARG_UNUSED (flags),
2961 bool * ARG_UNUSED (no_add_attrs))
2962 {
2963 if (TREE_CODE (*node) == FUNCTION_DECL
2964 && DECL_DECLARED_INLINE_P (*node))
2965 {
2966 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
2967 *no_add_attrs = true;
2968 }
2969 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2970 {
2971 error ("indirect function %q+D cannot be declared weak", *node);
2972 *no_add_attrs = true;
2973 return NULL_TREE;
2974 }
2975 else if (VAR_OR_FUNCTION_DECL_P (*node))
2976 declare_weak (*node);
2977 else
2978 warning (OPT_Wattributes, "%qE attribute ignored", name);
2979
2980 return NULL_TREE;
2981 }
2982
2983 /* Handle a "noinit" or "persistent" attribute; arguments as in
2984 struct attribute_spec.handler.
2985 This generic handler is used for "special variable sections" that allow the
2986 section name to be set using a dedicated attribute. Additional validation
2987 is performed for the specific properties of the section corresponding to the
2988 attribute.
2989 The ".noinit" section *is not* loaded by the program loader, and is not
2990 initialized by the runtime startup code.
2991 The ".persistent" section *is* loaded by the program loader, but is not
2992 initialized by the runtime startup code. */
2993 static tree
2994 handle_special_var_sec_attribute (tree *node, tree name, tree args,
2995 int flags, bool *no_add_attrs)
2996 {
2997 tree decl = *node;
2998 tree res = NULL_TREE;
2999
3000 /* First perform generic validation common to "noinit" and "persistent"
3001 attributes. */
3002 if (!targetm_common.have_named_sections)
3003 {
3004 error_at (DECL_SOURCE_LOCATION (decl),
3005 "section attributes are not supported for this target");
3006 goto fail;
3007 }
3008
3009 if (!VAR_P (decl))
3010 {
3011 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
3012 "ignoring %qE attribute not set on a variable",
3013 name);
3014 goto fail;
3015 }
3016
3017 if (VAR_P (decl)
3018 && current_function_decl != NULL_TREE
3019 && !TREE_STATIC (decl))
3020 {
3021 error_at (DECL_SOURCE_LOCATION (decl),
3022 "%qE attribute cannot be specified for local variables",
3023 name);
3024 goto fail;
3025 }
3026
3027 if (VAR_P (decl)
3028 && !targetm.have_tls && targetm.emutls.tmpl_section
3029 && DECL_THREAD_LOCAL_P (decl))
3030 {
3031 error ("section of %q+D cannot be overridden", decl);
3032 goto fail;
3033 }
3034
3035 if (!targetm.have_switchable_bss_sections)
3036 {
3037 error ("%qE attribute is specific to ELF targets", name);
3038 goto fail;
3039 }
3040
3041 if (TREE_READONLY (decl))
3042 {
3043 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
3044 "ignoring %qE attribute set on const variable",
3045 name);
3046 goto fail;
3047 }
3048
3049 /* Now validate noinit/persistent individually. */
3050 if (strcmp (IDENTIFIER_POINTER (name), "noinit") == 0)
3051 {
3052 if (DECL_INITIAL (decl))
3053 {
3054 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
3055 "ignoring %qE attribute set on initialized variable",
3056 name);
3057 goto fail;
3058 }
3059 /* If this var is thought to be common, then change this. "noinit"
3060 variables must be placed in an explicit ".noinit" section. */
3061 DECL_COMMON (decl) = 0;
3062 }
3063 else if (strcmp (IDENTIFIER_POINTER (name), "persistent") == 0)
3064 {
3065 if (DECL_COMMON (decl) || DECL_INITIAL (decl) == NULL_TREE)
3066 {
3067 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
3068 "ignoring %qE attribute set on uninitialized variable",
3069 name);
3070 goto fail;
3071 }
3072 }
3073 else
3074 gcc_unreachable ();
3075
3076 res = targetm.handle_generic_attribute (node, name, args, flags,
3077 no_add_attrs);
3078
3079 /* If the back end confirms the attribute can be added then continue onto
3080 final processing. */
3081 if (!(*no_add_attrs))
3082 return res;
3083
3084 fail:
3085 *no_add_attrs = true;
3086 return res;
3087 }
3088
3089 /* Handle a "noplt" attribute; arguments as in
3090 struct attribute_spec.handler. */
3091
3092 static tree
3093 handle_noplt_attribute (tree *node, tree name,
3094 tree ARG_UNUSED (args),
3095 int ARG_UNUSED (flags),
3096 bool * ARG_UNUSED (no_add_attrs))
3097 {
3098 if (TREE_CODE (*node) != FUNCTION_DECL)
3099 {
3100 warning (OPT_Wattributes,
3101 "%qE attribute is only applicable on functions", name);
3102 *no_add_attrs = true;
3103 return NULL_TREE;
3104 }
3105 return NULL_TREE;
3106 }
3107
3108 /* Handle a "symver" attribute. */
3109
3110 static tree
3111 handle_symver_attribute (tree *node, tree ARG_UNUSED (name), tree args,
3112 int ARG_UNUSED (flags), bool *no_add_attrs)
3113 {
3114 tree symver;
3115 const char *symver_str;
3116
3117 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
3118 {
3119 warning (OPT_Wattributes,
3120 "%<symver%> attribute only applies to functions and variables");
3121 *no_add_attrs = true;
3122 return NULL_TREE;
3123 }
3124
3125 if (!decl_in_symtab_p (*node))
3126 {
3127 warning (OPT_Wattributes,
3128 "%<symver%> attribute is only applicable to symbols");
3129 *no_add_attrs = true;
3130 return NULL_TREE;
3131 }
3132
3133 for (; args; args = TREE_CHAIN (args))
3134 {
3135 symver = TREE_VALUE (args);
3136 if (TREE_CODE (symver) != STRING_CST)
3137 {
3138 error ("%<symver%> attribute argument not a string constant");
3139 *no_add_attrs = true;
3140 return NULL_TREE;
3141 }
3142
3143 symver_str = TREE_STRING_POINTER (symver);
3144
3145 int ats = 0;
3146 for (int n = 0; (int)n < TREE_STRING_LENGTH (symver); n++)
3147 if (symver_str[n] == '@')
3148 ats++;
3149
3150 if (ats != 1 && ats != 2)
3151 {
3152 error ("symver attribute argument must have format %<name@nodename%>");
3153 error ("%<symver%> attribute argument %qs must contain one or two "
3154 "%<@%>", symver_str);
3155 *no_add_attrs = true;
3156 return NULL_TREE;
3157 }
3158 }
3159
3160 return NULL_TREE;
3161 }
3162
3163
3164 /* Handle an "alias" or "ifunc" attribute; arguments as in
3165 struct attribute_spec.handler, except that IS_ALIAS tells us
3166 whether this is an alias as opposed to ifunc attribute. */
3167
3168 static tree
3169 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
3170 bool *no_add_attrs)
3171 {
3172 tree decl = *node;
3173
3174 if (TREE_CODE (decl) != FUNCTION_DECL
3175 && (!is_alias || !VAR_P (decl)))
3176 {
3177 warning (OPT_Wattributes, "%qE attribute ignored", name);
3178 *no_add_attrs = true;
3179 }
3180 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
3181 || (TREE_CODE (decl) != FUNCTION_DECL
3182 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
3183 /* A static variable declaration is always a tentative definition,
3184 but the alias is a non-tentative definition which overrides. */
3185 || (TREE_CODE (decl) != FUNCTION_DECL
3186 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
3187 {
3188 error ("%q+D defined both normally and as %qE attribute", decl, name);
3189 *no_add_attrs = true;
3190 return NULL_TREE;
3191 }
3192 else if (!is_alias
3193 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
3194 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
3195 {
3196 error ("weak %q+D cannot be defined %qE", decl, name);
3197 *no_add_attrs = true;
3198 return NULL_TREE;
3199 }
3200
3201 /* Note that the very first time we process a nested declaration,
3202 decl_function_context will not be set. Indeed, *would* never
3203 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
3204 we do below. After such frobbery, pushdecl would set the context.
3205 In any case, this is never what we want. */
3206 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
3207 {
3208 tree id;
3209
3210 id = TREE_VALUE (args);
3211 if (TREE_CODE (id) != STRING_CST)
3212 {
3213 error ("attribute %qE argument not a string", name);
3214 *no_add_attrs = true;
3215 return NULL_TREE;
3216 }
3217 id = get_identifier (TREE_STRING_POINTER (id));
3218 /* This counts as a use of the object pointed to. */
3219 TREE_USED (id) = 1;
3220
3221 if (TREE_CODE (decl) == FUNCTION_DECL)
3222 DECL_INITIAL (decl) = error_mark_node;
3223 else
3224 TREE_STATIC (decl) = 1;
3225
3226 if (!is_alias)
3227 {
3228 /* ifuncs are also aliases, so set that attribute too. */
3229 DECL_ATTRIBUTES (decl)
3230 = tree_cons (get_identifier ("alias"), args,
3231 DECL_ATTRIBUTES (decl));
3232 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
3233 NULL, DECL_ATTRIBUTES (decl));
3234 }
3235 }
3236 else
3237 {
3238 warning (OPT_Wattributes, "%qE attribute ignored", name);
3239 *no_add_attrs = true;
3240 }
3241
3242 if (decl_in_symtab_p (*node))
3243 {
3244 struct symtab_node *n = symtab_node::get (decl);
3245 if (n && n->refuse_visibility_changes)
3246 error ("%+qD declared %qs after being used",
3247 decl, is_alias ? "alias" : "ifunc");
3248 }
3249
3250
3251 return NULL_TREE;
3252 }
3253
3254 /* Handle an "alias" or "ifunc" attribute; arguments as in
3255 struct attribute_spec.handler. */
3256
3257 static tree
3258 handle_ifunc_attribute (tree *node, tree name, tree args,
3259 int ARG_UNUSED (flags), bool *no_add_attrs)
3260 {
3261 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
3262 }
3263
3264 /* Handle an "alias" or "ifunc" attribute; arguments as in
3265 struct attribute_spec.handler. */
3266
3267 static tree
3268 handle_alias_attribute (tree *node, tree name, tree args,
3269 int ARG_UNUSED (flags), bool *no_add_attrs)
3270 {
3271 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
3272 }
3273
3274 /* Handle the "copy" attribute NAME by copying the set of attributes
3275 from the symbol referenced by ARGS to the declaration of *NODE. */
3276
3277 static tree
3278 handle_copy_attribute (tree *node, tree name, tree args,
3279 int flags, bool *no_add_attrs)
3280 {
3281 /* Do not apply the copy attribute itself. It serves no purpose
3282 other than to copy other attributes. */
3283 *no_add_attrs = true;
3284
3285 tree decl = *node;
3286
3287 tree ref = TREE_VALUE (args);
3288 if (ref == error_mark_node)
3289 return NULL_TREE;
3290
3291 location_t loc = input_location;
3292 if (DECL_P (decl))
3293 loc = DECL_SOURCE_LOCATION (decl);
3294 if (TREE_CODE (ref) == STRING_CST)
3295 {
3296 /* Explicitly handle this case since using a string literal
3297 as an argument is a likely mistake. */
3298 error_at (loc, "%qE attribute argument cannot be a string", name);
3299 return NULL_TREE;
3300 }
3301
3302 if (CONSTANT_CLASS_P (ref)
3303 && (INTEGRAL_TYPE_P (TREE_TYPE (ref))
3304 || FLOAT_TYPE_P (TREE_TYPE (ref))))
3305 {
3306 /* Similar to the string case, since some function attributes
3307 accept literal numbers as arguments (e.g., alloc_size or
3308 nonnull) using one here is a likely mistake. */
3309 error_at (loc, "%qE attribute argument cannot be a constant arithmetic "
3310 "expression", name);
3311 return NULL_TREE;
3312 }
3313
3314 if (ref == node[1])
3315 {
3316 /* Another possible mistake (but indirect self-references aren't
3317 and diagnosed and shouldn't be). */
3318 if (warning_at (loc, OPT_Wattributes,
3319 "%qE attribute ignored on a redeclaration "
3320 "of the referenced symbol", name)
3321 && DECL_P (node[1]))
3322 inform (DECL_SOURCE_LOCATION (node[1]), "previous declaration here");
3323 return NULL_TREE;
3324 }
3325
3326 /* Consider address-of expressions in the attribute argument
3327 as requests to copy from the referenced entity. */
3328 if (TREE_CODE (ref) == ADDR_EXPR)
3329 ref = TREE_OPERAND (ref, 0);
3330
3331 do
3332 {
3333 /* Drill down into references to find the referenced decl. */
3334 tree_code refcode = TREE_CODE (ref);
3335 if (refcode == ARRAY_REF
3336 || refcode == INDIRECT_REF)
3337 ref = TREE_OPERAND (ref, 0);
3338 else if (refcode == COMPONENT_REF)
3339 ref = TREE_OPERAND (ref, 1);
3340 else
3341 break;
3342 }
3343 while (!DECL_P (ref));
3344
3345 /* For object pointer expressions, consider those to be requests
3346 to copy from their type, such as in:
3347 struct __attribute__ (copy ((struct T *)0)) U { ... };
3348 which copies type attributes from struct T to the declaration
3349 of struct U. */
3350 if ((CONSTANT_CLASS_P (ref) || EXPR_P (ref))
3351 && POINTER_TYPE_P (TREE_TYPE (ref))
3352 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (ref)))
3353 ref = TREE_TYPE (ref);
3354
3355 tree reftype = TYPE_P (ref) ? ref : TREE_TYPE (ref);
3356
3357 if (DECL_P (decl))
3358 {
3359 if ((VAR_P (decl)
3360 && (TREE_CODE (ref) == FUNCTION_DECL
3361 || (EXPR_P (ref)
3362 && POINTER_TYPE_P (reftype)
3363 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype)))))
3364 || (TREE_CODE (decl) == FUNCTION_DECL
3365 && (VAR_P (ref)
3366 || (EXPR_P (ref)
3367 && !FUNC_OR_METHOD_TYPE_P (reftype)
3368 && (!POINTER_TYPE_P (reftype)
3369 || !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype)))))))
3370 {
3371 /* It makes no sense to try to copy function attributes
3372 to a variable, or variable attributes to a function. */
3373 if (warning (OPT_Wattributes,
3374 "%qE attribute ignored on a declaration of "
3375 "a different kind than referenced symbol", name)
3376 && DECL_P (ref))
3377 inform (DECL_SOURCE_LOCATION (ref),
3378 "symbol %qD referenced by %qD declared here", ref, decl);
3379 return NULL_TREE;
3380 }
3381
3382 tree attrs = NULL_TREE;
3383 if (DECL_P (ref))
3384 attrs = DECL_ATTRIBUTES (ref);
3385 else if (TYPE_P (ref))
3386 attrs = TYPE_ATTRIBUTES (ref);
3387
3388 /* Copy decl attributes from REF to DECL. */
3389 for (tree at = attrs; at; at = TREE_CHAIN (at))
3390 {
3391 /* Avoid copying attributes that affect a symbol linkage,
3392 inlining, or visibility since those in all likelihood
3393 only apply to the target.
3394 FIXME: make it possible to specify which attributes to
3395 copy or not to copy in the copy attribute itself. */
3396 tree atname = get_attribute_name (at);
3397 if (is_attribute_p ("alias", atname)
3398 || is_attribute_p ("always_inline", atname)
3399 || is_attribute_p ("gnu_inline", atname)
3400 || is_attribute_p ("ifunc", atname)
3401 || is_attribute_p ("noinline", atname)
3402 || is_attribute_p ("visibility", atname)
3403 || is_attribute_p ("weak", atname)
3404 || is_attribute_p ("weakref", atname)
3405 || is_attribute_p ("target_clones", atname))
3406 continue;
3407
3408 /* Attribute leaf only applies to extern functions.
3409 Avoid copying it to static ones. */
3410 if (!TREE_PUBLIC (decl)
3411 && is_attribute_p ("leaf", atname))
3412 continue;
3413
3414 tree atargs = TREE_VALUE (at);
3415 /* Create a copy of just the one attribute ar AT, including
3416 its argumentsm and add it to DECL. */
3417 tree attr = tree_cons (atname, copy_list (atargs), NULL_TREE);
3418 decl_attributes (node, attr, flags, EXPR_P (ref) ? NULL_TREE : ref);
3419 }
3420
3421 /* Proceed to copy type attributes below. */
3422 }
3423 else if (!TYPE_P (decl))
3424 {
3425 error_at (loc, "%qE attribute must apply to a declaration", name);
3426 return NULL_TREE;
3427 }
3428
3429 /* A function declared with attribute nothrow has the attribute
3430 attached to it, but a C++ throw() function does not. */
3431 if (TREE_NOTHROW (ref))
3432 TREE_NOTHROW (decl) = true;
3433
3434 /* Similarly, a function declared with attribute noreturn has it
3435 attached on to it, but a C11 _Noreturn function does not. */
3436 if (DECL_P (ref)
3437 && TREE_THIS_VOLATILE (ref)
3438 && FUNC_OR_METHOD_TYPE_P (reftype))
3439 TREE_THIS_VOLATILE (decl) = true;
3440
3441 if (POINTER_TYPE_P (reftype))
3442 reftype = TREE_TYPE (reftype);
3443
3444 if (!TYPE_P (reftype))
3445 return NULL_TREE;
3446
3447 tree attrs = TYPE_ATTRIBUTES (reftype);
3448
3449 /* Copy type attributes from REF to DECL. Pass in REF if it's a DECL
3450 or a type but not if it's an expression. Set ATTR_FLAG_INTERNAL
3451 since the attributes' arguments may be in their internal form. */
3452 for (tree at = attrs; at; at = TREE_CHAIN (at))
3453 decl_attributes (node, at, flags | ATTR_FLAG_INTERNAL,
3454 EXPR_P (ref) ? NULL_TREE : ref);
3455
3456 return NULL_TREE;
3457 }
3458
3459 /* Handle a "weakref" attribute; arguments as in struct
3460 attribute_spec.handler. */
3461
3462 static tree
3463 handle_weakref_attribute (tree *node, tree name, tree args,
3464 int flags, bool *no_add_attrs)
3465 {
3466 tree attr = NULL_TREE;
3467
3468 /* We must ignore the attribute when it is associated with
3469 local-scoped decls, since attribute alias is ignored and many
3470 such symbols do not even have a DECL_WEAK field. */
3471 if (decl_function_context (*node)
3472 || current_function_decl
3473 || !VAR_OR_FUNCTION_DECL_P (*node))
3474 {
3475 warning (OPT_Wattributes, "%qE attribute ignored", name);
3476 *no_add_attrs = true;
3477 return NULL_TREE;
3478 }
3479
3480 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
3481 {
3482 error ("indirect function %q+D cannot be declared %qE",
3483 *node, name);
3484 *no_add_attrs = true;
3485 return NULL_TREE;
3486 }
3487
3488 /* The idea here is that `weakref("name")' mutates into `weakref,
3489 alias("name")', and weakref without arguments, in turn,
3490 implicitly adds weak. */
3491
3492 if (args)
3493 {
3494 attr = tree_cons (get_identifier ("alias"), args, attr);
3495 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
3496
3497 *no_add_attrs = true;
3498
3499 decl_attributes (node, attr, flags);
3500 }
3501 else
3502 {
3503 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
3504 error_at (DECL_SOURCE_LOCATION (*node),
3505 "%qE attribute must appear before %qs attribute",
3506 name, "alias");
3507
3508 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
3509 and that isn't supported; and because it wants to add it to
3510 the list of weak decls, which isn't helpful. */
3511 DECL_WEAK (*node) = 1;
3512 }
3513
3514 if (decl_in_symtab_p (*node))
3515 {
3516 struct symtab_node *n = symtab_node::get (*node);
3517 if (n && n->refuse_visibility_changes)
3518 error ("%+qD declared %qE after being used", *node, name);
3519 }
3520
3521 return NULL_TREE;
3522 }
3523
3524 /* Handle an "visibility" attribute; arguments as in
3525 struct attribute_spec.handler. */
3526
3527 static tree
3528 handle_visibility_attribute (tree *node, tree name, tree args,
3529 int ARG_UNUSED (flags),
3530 bool *ARG_UNUSED (no_add_attrs))
3531 {
3532 tree decl = *node;
3533 tree id = TREE_VALUE (args);
3534 enum symbol_visibility vis;
3535
3536 if (TYPE_P (*node))
3537 {
3538 if (TREE_CODE (*node) == ENUMERAL_TYPE)
3539 /* OK */;
3540 else if (!RECORD_OR_UNION_TYPE_P (*node))
3541 {
3542 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
3543 name);
3544 return NULL_TREE;
3545 }
3546 else if (TYPE_FIELDS (*node))
3547 {
3548 error ("%qE attribute ignored because %qT is already defined",
3549 name, *node);
3550 return NULL_TREE;
3551 }
3552 }
3553 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
3554 {
3555 warning (OPT_Wattributes, "%qE attribute ignored", name);
3556 return NULL_TREE;
3557 }
3558
3559 if (TREE_CODE (id) != STRING_CST)
3560 {
3561 error ("visibility argument not a string");
3562 return NULL_TREE;
3563 }
3564
3565 /* If this is a type, set the visibility on the type decl. */
3566 if (TYPE_P (decl))
3567 {
3568 decl = TYPE_NAME (decl);
3569 if (!decl)
3570 return NULL_TREE;
3571 if (TREE_CODE (decl) == IDENTIFIER_NODE)
3572 {
3573 warning (OPT_Wattributes, "%qE attribute ignored on types",
3574 name);
3575 return NULL_TREE;
3576 }
3577 }
3578
3579 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
3580 vis = VISIBILITY_DEFAULT;
3581 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
3582 vis = VISIBILITY_INTERNAL;
3583 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
3584 vis = VISIBILITY_HIDDEN;
3585 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
3586 vis = VISIBILITY_PROTECTED;
3587 else
3588 {
3589 error ("attribute %qE argument must be one of %qs, %qs, %qs, or %qs",
3590 name, "default", "hidden", "protected", "internal");
3591 vis = VISIBILITY_DEFAULT;
3592 }
3593
3594 if (DECL_VISIBILITY_SPECIFIED (decl)
3595 && vis != DECL_VISIBILITY (decl))
3596 {
3597 tree attributes = (TYPE_P (*node)
3598 ? TYPE_ATTRIBUTES (*node)
3599 : DECL_ATTRIBUTES (decl));
3600 if (lookup_attribute ("visibility", attributes))
3601 error ("%qD redeclared with different visibility", decl);
3602 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
3603 && lookup_attribute ("dllimport", attributes))
3604 error ("%qD was declared %qs which implies default visibility",
3605 decl, "dllimport");
3606 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
3607 && lookup_attribute ("dllexport", attributes))
3608 error ("%qD was declared %qs which implies default visibility",
3609 decl, "dllexport");
3610 }
3611
3612 DECL_VISIBILITY (decl) = vis;
3613 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3614
3615 /* Go ahead and attach the attribute to the node as well. This is needed
3616 so we can determine whether we have VISIBILITY_DEFAULT because the
3617 visibility was not specified, or because it was explicitly overridden
3618 from the containing scope. */
3619
3620 return NULL_TREE;
3621 }
3622
3623 /* Handle an "tls_model" attribute; arguments as in
3624 struct attribute_spec.handler. */
3625
3626 static tree
3627 handle_tls_model_attribute (tree *node, tree name, tree args,
3628 int ARG_UNUSED (flags),
3629 bool *ARG_UNUSED (no_add_attrs))
3630 {
3631 tree id;
3632 tree decl = *node;
3633 enum tls_model kind;
3634
3635 if (!VAR_P (decl))
3636 {
3637 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
3638 "is not a variable",
3639 name, decl);
3640 return NULL_TREE;
3641 }
3642
3643 if (!DECL_THREAD_LOCAL_P (decl))
3644 {
3645 warning (OPT_Wattributes, "%qE attribute ignored because %qD does "
3646 "not have thread storage duration", name, decl);
3647 return NULL_TREE;
3648 }
3649
3650 kind = DECL_TLS_MODEL (decl);
3651 id = TREE_VALUE (args);
3652 if (TREE_CODE (id) != STRING_CST)
3653 {
3654 error ("%qE argument not a string", name);
3655 return NULL_TREE;
3656 }
3657
3658 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
3659 kind = TLS_MODEL_LOCAL_EXEC;
3660 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
3661 kind = TLS_MODEL_INITIAL_EXEC;
3662 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
3663 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
3664 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
3665 kind = TLS_MODEL_GLOBAL_DYNAMIC;
3666 else
3667 error ("%qE argument must be one of %qs, %qs, %qs, or %qs",
3668 name,
3669 "local-exec", "initial-exec", "local-dynamic", "global-dynamic");
3670
3671 set_decl_tls_model (decl, kind);
3672 return NULL_TREE;
3673 }
3674
3675 /* Handle a "no_instrument_function" attribute; arguments as in
3676 struct attribute_spec.handler. */
3677
3678 static tree
3679 handle_no_instrument_function_attribute (tree *node, tree name,
3680 tree ARG_UNUSED (args),
3681 int ARG_UNUSED (flags),
3682 bool *no_add_attrs)
3683 {
3684 tree decl = *node;
3685
3686 if (TREE_CODE (decl) != FUNCTION_DECL)
3687 {
3688 error_at (DECL_SOURCE_LOCATION (decl),
3689 "%qE attribute applies only to functions", name);
3690 *no_add_attrs = true;
3691 }
3692 else
3693 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
3694
3695 return NULL_TREE;
3696 }
3697
3698 /* Handle a "no_profile_instrument_function" attribute; arguments as in
3699 struct attribute_spec.handler. */
3700
3701 static tree
3702 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
3703 int, bool *no_add_attrs)
3704 {
3705 if (TREE_CODE (*node) != FUNCTION_DECL)
3706 {
3707 warning (OPT_Wattributes, "%qE attribute ignored", name);
3708 *no_add_attrs = true;
3709 }
3710
3711 return NULL_TREE;
3712 }
3713
3714 /* If ALLOC_DECL and DEALLOC_DECL are a pair of user-defined functions,
3715 if they are declared inline issue warnings and return null. Otherwise
3716 create attribute noinline, install it in ALLOC_DECL, and return it.
3717 Otherwise return null. */
3718
3719 static tree
3720 maybe_add_noinline (tree name, tree alloc_decl, tree dealloc_decl,
3721 bool *no_add_attrs)
3722 {
3723 if (fndecl_built_in_p (alloc_decl) || fndecl_built_in_p (dealloc_decl))
3724 return NULL_TREE;
3725
3726 /* When inlining (or optimization) is enabled and the allocator and
3727 deallocator are not built-in functions, ignore the attribute on
3728 functions declared inline since it could lead to false positives
3729 when inlining one or the other call would wind up calling
3730 a mismatched allocator or deallocator. */
3731 if ((optimize && DECL_DECLARED_INLINE_P (alloc_decl))
3732 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (alloc_decl)))
3733 {
3734 warning (OPT_Wattributes,
3735 "%<%E (%E)%> attribute ignored on functions "
3736 "declared %qs", name, DECL_NAME (dealloc_decl), "inline");
3737 *no_add_attrs = true;
3738 return NULL_TREE;
3739 }
3740
3741 if ((optimize && DECL_DECLARED_INLINE_P (dealloc_decl))
3742 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (dealloc_decl)))
3743 {
3744 warning (OPT_Wattributes,
3745 "%<%E (%E)%> attribute ignored with deallocation "
3746 "functions declared %qs",
3747 name, DECL_NAME (dealloc_decl), "inline");
3748 inform (DECL_SOURCE_LOCATION (dealloc_decl),
3749 "deallocation function declared here" );
3750 *no_add_attrs = true;
3751 return NULL_TREE;
3752 }
3753
3754 /* Disable inlining for non-standard deallocators to avoid false
3755 positives due to mismatches between the inlined implementation
3756 of one and not the other pair of functions. */
3757 tree attr = tree_cons (get_identifier ("noinline"), NULL_TREE, NULL_TREE);
3758 decl_attributes (&alloc_decl, attr, 0);
3759 return attr;
3760 }
3761
3762 /* Handle the "malloc" attribute. */
3763
3764 static tree
3765 handle_malloc_attribute (tree *node, tree name, tree args, int flags,
3766 bool *no_add_attrs)
3767 {
3768 if (flags & ATTR_FLAG_INTERNAL)
3769 /* Recursive call. */
3770 return NULL_TREE;
3771
3772 tree fndecl = *node;
3773
3774 if (TREE_CODE (*node) != FUNCTION_DECL)
3775 {
3776 warning (OPT_Wattributes, "%qE attribute ignored; valid only "
3777 "for functions",
3778 name);
3779 *no_add_attrs = true;
3780 return NULL_TREE;
3781 }
3782
3783 tree rettype = TREE_TYPE (TREE_TYPE (*node));
3784 if (!POINTER_TYPE_P (rettype))
3785 {
3786 warning (OPT_Wattributes, "%qE attribute ignored on functions "
3787 "returning %qT; valid only for pointer return types",
3788 name, rettype);
3789 *no_add_attrs = true;
3790 return NULL_TREE;
3791 }
3792
3793 if (!args)
3794 {
3795 /* Only the form of the attribute with no arguments declares
3796 a function malloc-like. */
3797 DECL_IS_MALLOC (*node) = 1;
3798 return NULL_TREE;
3799 }
3800
3801 tree dealloc = TREE_VALUE (args);
3802 if (error_operand_p (dealloc))
3803 {
3804 /* If the argument is in error it will have already been diagnosed.
3805 Avoid issuing redundant errors here. */
3806 *no_add_attrs = true;
3807 return NULL_TREE;
3808 }
3809
3810 STRIP_NOPS (dealloc);
3811 if (TREE_CODE (dealloc) == ADDR_EXPR)
3812 {
3813 /* In C++ the argument may be wrapped in a cast to disambiguate
3814 one of a number of overloads (such as operator delete). To
3815 make things interesting, the cast looks different between
3816 different C++ versions. Strip it and install the attribute
3817 with the disambiguated function. */
3818 dealloc = TREE_OPERAND (dealloc, 0);
3819
3820 *no_add_attrs = true;
3821 tree attr = tree_cons (NULL_TREE, dealloc, TREE_CHAIN (args));
3822 attr = build_tree_list (name, attr);
3823 return decl_attributes (node, attr, 0);
3824 }
3825
3826 if (TREE_CODE (dealloc) != FUNCTION_DECL)
3827 {
3828 if (TREE_CODE (dealloc) == OVERLOAD)
3829 {
3830 /* Handle specially the common case of specifying one of a number
3831 of overloads, such as operator delete. */
3832 error ("%qE attribute argument 1 is ambiguous", name);
3833 inform (input_location,
3834 "use a cast to the expected type to disambiguate");
3835 *no_add_attrs = true;
3836 return NULL_TREE;
3837 }
3838
3839 error ("%qE attribute argument 1 does not name a function", name);
3840 if (DECL_P (dealloc))
3841 inform (DECL_SOURCE_LOCATION (dealloc),
3842 "argument references a symbol declared here");
3843 *no_add_attrs = true;
3844 return NULL_TREE;
3845 }
3846
3847 /* Mentioning the deallocation function qualifies as its use. */
3848 TREE_USED (dealloc) = 1;
3849
3850 tree fntype = TREE_TYPE (dealloc);
3851 tree argpos = TREE_CHAIN (args) ? TREE_VALUE (TREE_CHAIN (args)) : NULL_TREE;
3852 if (!argpos)
3853 {
3854 tree argtypes = TYPE_ARG_TYPES (fntype);
3855 if (!argtypes)
3856 {
3857 /* Reject functions without a prototype. */
3858 error ("%qE attribute argument 1 must take a pointer "
3859 "type as its first argument", name);
3860 inform (DECL_SOURCE_LOCATION (dealloc),
3861 "referenced symbol declared here");
3862 *no_add_attrs = true;
3863 return NULL_TREE;
3864 }
3865
3866 tree argtype = TREE_VALUE (argtypes);
3867 if (TREE_CODE (argtype) != POINTER_TYPE)
3868 {
3869 /* Reject functions that don't take a pointer as their first
3870 argument. */
3871 error ("%qE attribute argument 1 must take a pointer type "
3872 "as its first argument; have %qT", name, argtype);
3873 inform (DECL_SOURCE_LOCATION (dealloc),
3874 "referenced symbol declared here");
3875 *no_add_attrs = true;
3876 return NULL_TREE;
3877 }
3878
3879 /* Disable inlining for non-standard deallocators to avoid false
3880 positives (or warn if either function is explicitly inline). */
3881 tree at_noinline =
3882 maybe_add_noinline (name, fndecl, dealloc, no_add_attrs);
3883 if (*no_add_attrs)
3884 return NULL_TREE;
3885
3886 /* Add attribute *dealloc to the deallocator function associating
3887 it with this one. Ideally, the attribute would reference
3888 the DECL of the deallocator but since that changes for each
3889 redeclaration, use DECL_NAME instead. (DECL_ASSEMBLER_NAME
3890 need not be set at this point and setting it here is too early. */
3891 tree attrs = build_tree_list (NULL_TREE, DECL_NAME (fndecl));
3892 attrs = tree_cons (get_identifier ("*dealloc"), attrs, at_noinline);
3893 decl_attributes (&dealloc, attrs, 0);
3894 return NULL_TREE;
3895 }
3896
3897 /* Validate the positional argument. */
3898 argpos = positional_argument (fntype, name, argpos, POINTER_TYPE);
3899 if (!argpos)
3900 {
3901 *no_add_attrs = true;
3902 return NULL_TREE;
3903 }
3904
3905 /* As above, disable inlining for non-standard deallocators to avoid
3906 false positives (or warn). */
3907 tree at_noinline =
3908 maybe_add_noinline (name, fndecl, dealloc, no_add_attrs);
3909 if (*no_add_attrs)
3910 return NULL_TREE;
3911
3912 /* It's valid to declare the same function with multiple instances
3913 of attribute malloc, each naming the same or different deallocator
3914 functions, and each referencing either the same or a different
3915 positional argument. */
3916 tree attrs = tree_cons (NULL_TREE, argpos, NULL_TREE);
3917 attrs = tree_cons (NULL_TREE, DECL_NAME (fndecl), attrs);
3918 attrs = tree_cons (get_identifier ("*dealloc"), attrs, at_noinline);
3919 decl_attributes (&dealloc, attrs, 0);
3920 return NULL_TREE;
3921 }
3922
3923 /* Handle the internal "*dealloc" attribute added for functions declared
3924 with the one- and two-argument forms of attribute malloc. Add it
3925 to *NODE unless it's already there with the same arguments. */
3926
3927 static tree
3928 handle_dealloc_attribute (tree *node, tree name, tree args, int,
3929 bool *no_add_attrs)
3930 {
3931 tree fndecl = *node;
3932
3933 tree attrs = DECL_ATTRIBUTES (fndecl);
3934 if (!attrs)
3935 return NULL_TREE;
3936
3937 tree arg = TREE_VALUE (args);
3938 args = TREE_CHAIN (args);
3939 tree arg_pos = args ? TREE_VALUE (args) : integer_zero_node;
3940
3941 gcc_checking_assert ((DECL_P (arg)
3942 && fndecl_built_in_p (arg, BUILT_IN_NORMAL))
3943 || TREE_CODE (arg) == IDENTIFIER_NODE);
3944
3945 const char* const namestr = IDENTIFIER_POINTER (name);
3946 for (tree at = attrs; (at = lookup_attribute (namestr, at));
3947 at = TREE_CHAIN (at))
3948 {
3949 tree alloc = TREE_VALUE (at);
3950 if (!alloc)
3951 continue;
3952
3953 tree pos = TREE_CHAIN (alloc);
3954 alloc = TREE_VALUE (alloc);
3955 pos = pos ? TREE_VALUE (pos) : integer_zero_node;
3956 gcc_checking_assert ((DECL_P (alloc)
3957 && fndecl_built_in_p (alloc, BUILT_IN_NORMAL))
3958 || TREE_CODE (alloc) == IDENTIFIER_NODE);
3959
3960 if (alloc == arg && tree_int_cst_equal (pos, arg_pos))
3961 {
3962 /* The function already has the attribute either without any
3963 arguments or with the same arguments as the attribute that's
3964 being added. Return without adding another copy. */
3965 *no_add_attrs = true;
3966 return NULL_TREE;
3967 }
3968 }
3969
3970 return NULL_TREE;
3971 }
3972
3973 /* Handle the "alloc_size (argpos1 [, argpos2])" function type attribute.
3974 *NODE is the type of the function the attribute is being applied to. */
3975
3976 static tree
3977 handle_alloc_size_attribute (tree *node, tree name, tree args,
3978 int ARG_UNUSED (flags), bool *no_add_attrs)
3979 {
3980 tree fntype = *node;
3981 tree rettype = TREE_TYPE (fntype);
3982 if (!POINTER_TYPE_P (rettype))
3983 {
3984 warning (OPT_Wattributes,
3985 "%qE attribute ignored on a function returning %qT",
3986 name, rettype);
3987 *no_add_attrs = true;
3988 return NULL_TREE;
3989 }
3990
3991 tree newargs[2] = { NULL_TREE, NULL_TREE };
3992 for (int i = 1; args; ++i)
3993 {
3994 tree pos = TREE_VALUE (args);
3995 /* NEXT is null when the attribute includes just one argument.
3996 That's used to tell positional_argument to avoid mentioning
3997 the argument number in diagnostics (since there's just one
3998 mentioning it is unnecessary and coule be confusing). */
3999 tree next = TREE_CHAIN (args);
4000 if (tree val = positional_argument (fntype, name, pos, INTEGER_TYPE,
4001 next || i > 1 ? i : 0))
4002 {
4003 TREE_VALUE (args) = val;
4004 newargs[i - 1] = val;
4005 }
4006 else
4007 {
4008 *no_add_attrs = true;
4009 return NULL_TREE;
4010 }
4011
4012 args = next;
4013 }
4014
4015 if (!validate_attr_args (node, name, newargs))
4016 *no_add_attrs = true;
4017
4018 return NULL_TREE;
4019 }
4020
4021
4022 /* Handle an "alloc_align (argpos)" attribute. */
4023
4024 static tree
4025 handle_alloc_align_attribute (tree *node, tree name, tree args, int,
4026 bool *no_add_attrs)
4027 {
4028 tree fntype = *node;
4029 tree rettype = TREE_TYPE (fntype);
4030 if (!POINTER_TYPE_P (rettype))
4031 {
4032 warning (OPT_Wattributes,
4033 "%qE attribute ignored on a function returning %qT",
4034 name, rettype);
4035 *no_add_attrs = true;
4036 return NULL_TREE;
4037 }
4038
4039 if (tree val = positional_argument (*node, name, TREE_VALUE (args),
4040 INTEGER_TYPE))
4041 if (validate_attr_arg (node, name, val))
4042 return NULL_TREE;
4043
4044 *no_add_attrs = true;
4045 return NULL_TREE;
4046 }
4047
4048 /* Handle a "assume_aligned" attribute; arguments as in
4049 struct attribute_spec.handler. */
4050
4051 static tree
4052 handle_assume_aligned_attribute (tree *node, tree name, tree args, int,
4053 bool *no_add_attrs)
4054 {
4055 tree decl = *node;
4056 tree rettype = TREE_TYPE (decl);
4057 if (TREE_CODE (rettype) != POINTER_TYPE)
4058 {
4059 warning (OPT_Wattributes,
4060 "%qE attribute ignored on a function returning %qT",
4061 name, rettype);
4062 *no_add_attrs = true;
4063 return NULL_TREE;
4064 }
4065
4066 /* The alignment specified by the first argument. */
4067 tree align = NULL_TREE;
4068
4069 for (; args; args = TREE_CHAIN (args))
4070 {
4071 tree val = TREE_VALUE (args);
4072 if (val && TREE_CODE (val) != IDENTIFIER_NODE
4073 && TREE_CODE (val) != FUNCTION_DECL)
4074 val = default_conversion (val);
4075
4076 if (!tree_fits_shwi_p (val))
4077 {
4078 warning (OPT_Wattributes,
4079 "%qE attribute argument %E is not an integer constant",
4080 name, val);
4081 *no_add_attrs = true;
4082 return NULL_TREE;
4083 }
4084 else if (tree_int_cst_sgn (val) < 0)
4085 {
4086 warning (OPT_Wattributes,
4087 "%qE attribute argument %E is not positive", name, val);
4088 *no_add_attrs = true;
4089 return NULL_TREE;
4090 }
4091
4092 if (!align)
4093 {
4094 /* Validate and save the alignment. */
4095 if (!integer_pow2p (val))
4096 {
4097 warning (OPT_Wattributes,
4098 "%qE attribute argument %E is not a power of 2",
4099 name, val);
4100 *no_add_attrs = true;
4101 return NULL_TREE;
4102 }
4103
4104 align = val;
4105 }
4106 else if (tree_int_cst_le (align, val))
4107 {
4108 /* The misalignment specified by the second argument
4109 must be non-negative and less than the alignment. */
4110 warning (OPT_Wattributes,
4111 "%qE attribute argument %E is not in the range [0, %wu]",
4112 name, val, tree_to_uhwi (align) - 1);
4113 *no_add_attrs = true;
4114 return NULL_TREE;
4115 }
4116 }
4117 return NULL_TREE;
4118 }
4119
4120 /* Handle the internal-only "arg spec" attribute. */
4121
4122 static tree
4123 handle_argspec_attribute (tree *, tree, tree args, int, bool *)
4124 {
4125 /* Verify the attribute has one or two arguments and their kind. */
4126 gcc_assert (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST);
4127 for (tree next = TREE_CHAIN (args); next; next = TREE_CHAIN (next))
4128 {
4129 tree val = TREE_VALUE (next);
4130 gcc_assert (DECL_P (val) || EXPR_P (val));
4131 }
4132 return NULL_TREE;
4133 }
4134
4135 /* Handle the internal-only "fn spec" attribute. */
4136
4137 static tree
4138 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
4139 tree args, int ARG_UNUSED (flags),
4140 bool *no_add_attrs ATTRIBUTE_UNUSED)
4141 {
4142 gcc_assert (args
4143 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
4144 && !TREE_CHAIN (args));
4145 return NULL_TREE;
4146 }
4147
4148 /* Handle a "warn_unused" attribute; arguments as in
4149 struct attribute_spec.handler. */
4150
4151 static tree
4152 handle_warn_unused_attribute (tree *node, tree name,
4153 tree args ATTRIBUTE_UNUSED,
4154 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
4155 {
4156 if (TYPE_P (*node))
4157 /* Do nothing else, just set the attribute. We'll get at
4158 it later with lookup_attribute. */
4159 ;
4160 else
4161 {
4162 warning (OPT_Wattributes, "%qE attribute ignored", name);
4163 *no_add_attrs = true;
4164 }
4165
4166 return NULL_TREE;
4167 }
4168
4169 /* Handle an "omp declare simd" attribute; arguments as in
4170 struct attribute_spec.handler. */
4171
4172 static tree
4173 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
4174 {
4175 return NULL_TREE;
4176 }
4177
4178 /* Handle an "omp declare variant {base,variant}" attribute; arguments as in
4179 struct attribute_spec.handler. */
4180
4181 static tree
4182 handle_omp_declare_variant_attribute (tree *, tree, tree, int, bool *)
4183 {
4184 return NULL_TREE;
4185 }
4186
4187 /* Handle a "simd" attribute. */
4188
4189 static tree
4190 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
4191 {
4192 if (TREE_CODE (*node) == FUNCTION_DECL)
4193 {
4194 tree t = get_identifier ("omp declare simd");
4195 tree attr = NULL_TREE;
4196 if (args)
4197 {
4198 tree id = TREE_VALUE (args);
4199
4200 if (TREE_CODE (id) != STRING_CST)
4201 {
4202 error ("attribute %qE argument not a string", name);
4203 *no_add_attrs = true;
4204 return NULL_TREE;
4205 }
4206
4207 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
4208 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
4209 OMP_CLAUSE_NOTINBRANCH);
4210 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
4211 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
4212 OMP_CLAUSE_INBRANCH);
4213 else
4214 {
4215 error ("only %<inbranch%> and %<notinbranch%> flags are "
4216 "allowed for %<__simd__%> attribute");
4217 *no_add_attrs = true;
4218 return NULL_TREE;
4219 }
4220 }
4221
4222 DECL_ATTRIBUTES (*node)
4223 = tree_cons (t, build_tree_list (NULL_TREE, attr),
4224 DECL_ATTRIBUTES (*node));
4225 }
4226 else
4227 {
4228 warning (OPT_Wattributes, "%qE attribute ignored", name);
4229 *no_add_attrs = true;
4230 }
4231
4232 return NULL_TREE;
4233 }
4234
4235 /* Handle an "omp declare target" attribute; arguments as in
4236 struct attribute_spec.handler. */
4237
4238 static tree
4239 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
4240 {
4241 return NULL_TREE;
4242 }
4243
4244 /* Handle an "non overlapping" attribute; arguments as in
4245 struct attribute_spec.handler. */
4246
4247 static tree
4248 handle_non_overlapping_attribute (tree *, tree, tree, int, bool *)
4249 {
4250 return NULL_TREE;
4251 }
4252
4253 /* Handle a "returns_twice" attribute; arguments as in
4254 struct attribute_spec.handler. */
4255
4256 static tree
4257 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4258 int ARG_UNUSED (flags), bool *no_add_attrs)
4259 {
4260 if (TREE_CODE (*node) == FUNCTION_DECL)
4261 DECL_IS_RETURNS_TWICE (*node) = 1;
4262 else
4263 {
4264 warning (OPT_Wattributes, "%qE attribute ignored", name);
4265 *no_add_attrs = true;
4266 }
4267
4268 return NULL_TREE;
4269 }
4270
4271 /* Handle a "no_limit_stack" attribute; arguments as in
4272 struct attribute_spec.handler. */
4273
4274 static tree
4275 handle_no_limit_stack_attribute (tree *node, tree name,
4276 tree ARG_UNUSED (args),
4277 int ARG_UNUSED (flags),
4278 bool *no_add_attrs)
4279 {
4280 tree decl = *node;
4281
4282 if (TREE_CODE (decl) != FUNCTION_DECL)
4283 {
4284 error_at (DECL_SOURCE_LOCATION (decl),
4285 "%qE attribute applies only to functions", name);
4286 *no_add_attrs = true;
4287 }
4288 else if (DECL_INITIAL (decl))
4289 {
4290 error_at (DECL_SOURCE_LOCATION (decl),
4291 "cannot set %qE attribute after definition", name);
4292 *no_add_attrs = true;
4293 }
4294 else
4295 DECL_NO_LIMIT_STACK (decl) = 1;
4296
4297 return NULL_TREE;
4298 }
4299
4300 /* Handle a "pure" attribute; arguments as in
4301 struct attribute_spec.handler. */
4302
4303 static tree
4304 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4305 int ARG_UNUSED (flags), bool *no_add_attrs)
4306 {
4307 if (TREE_CODE (*node) == FUNCTION_DECL)
4308 {
4309 tree type = TREE_TYPE (*node);
4310 if (VOID_TYPE_P (TREE_TYPE (type)))
4311 warning (OPT_Wattributes, "%qE attribute on function "
4312 "returning %<void%>", name);
4313
4314 DECL_PURE_P (*node) = 1;
4315 /* ??? TODO: Support types. */
4316 }
4317 else
4318 {
4319 warning (OPT_Wattributes, "%qE attribute ignored", name);
4320 *no_add_attrs = true;
4321 }
4322
4323 return NULL_TREE;
4324 }
4325
4326 /* Handle an "unsequenced" attribute; arguments as in
4327 struct attribute_spec.handler. */
4328
4329 tree
4330 handle_unsequenced_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4331 int flags, bool *no_add_attrs)
4332 {
4333 tree fntype = *node;
4334 for (tree argtype = TYPE_ARG_TYPES (fntype); argtype;
4335 argtype = TREE_CHAIN (argtype))
4336 /* If any of the arguments have pointer or reference type, just
4337 add the attribute alone. */
4338 if (POINTER_TYPE_P (TREE_VALUE (argtype)))
4339 return NULL_TREE;
4340
4341 if (VOID_TYPE_P (TREE_TYPE (fntype)))
4342 warning (OPT_Wattributes, "%qE attribute on function type "
4343 "without pointer arguments returning %<void%>", name);
4344 const char *name2;
4345 if (IDENTIFIER_LENGTH (name) == sizeof ("unsequenced") - 1)
4346 name2 = "unsequenced noptr";
4347 else
4348 name2 = "reproducible noptr";
4349 if (!lookup_attribute (name2, TYPE_ATTRIBUTES (fntype)))
4350 {
4351 *no_add_attrs = true;
4352 gcc_assert ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE) == 0);
4353 tree attr = tree_cons (get_identifier (name2), NULL_TREE,
4354 TYPE_ATTRIBUTES (fntype));
4355 if (!lookup_attribute (IDENTIFIER_POINTER (name),
4356 TYPE_ATTRIBUTES (fntype)))
4357 attr = tree_cons (name, NULL_TREE, attr);
4358 *node = build_type_attribute_variant (*node, attr);
4359 }
4360 return NULL_TREE;
4361 }
4362
4363 /* Handle a "reproducible" attribute; arguments as in
4364 struct attribute_spec.handler. */
4365
4366 tree
4367 handle_reproducible_attribute (tree *node, tree name, tree args, int flags,
4368 bool *no_add_attrs)
4369 {
4370 return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4371 }
4372
4373 /* Digest an attribute list destined for a transactional memory statement.
4374 ALLOWED is the set of attributes that are allowed for this statement;
4375 return the attribute we parsed. Multiple attributes are never allowed. */
4376
4377 int
4378 parse_tm_stmt_attr (tree attrs, int allowed)
4379 {
4380 tree a_seen = NULL;
4381 int m_seen = 0;
4382
4383 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
4384 {
4385 tree a = get_attribute_name (attrs);
4386 tree ns = get_attribute_namespace (attrs);
4387 int m = 0;
4388
4389 if (is_attribute_p ("outer", a)
4390 && (ns == NULL_TREE || strcmp (IDENTIFIER_POINTER (ns), "gnu") == 0))
4391 m = TM_STMT_ATTR_OUTER;
4392
4393 if ((m & allowed) == 0)
4394 {
4395 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
4396 continue;
4397 }
4398
4399 if (m_seen == 0)
4400 {
4401 a_seen = a;
4402 m_seen = m;
4403 }
4404 else if (m_seen == m)
4405 warning (OPT_Wattributes, "%qE attribute duplicated", a);
4406 else
4407 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
4408 }
4409
4410 return m_seen;
4411 }
4412
4413 /* Transform a TM attribute name into a maskable integer and back.
4414 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
4415 to how the lack of an attribute is treated. */
4416
4417 int
4418 tm_attr_to_mask (tree attr)
4419 {
4420 if (attr == NULL)
4421 return 0;
4422 if (is_attribute_p ("transaction_safe", attr))
4423 return TM_ATTR_SAFE;
4424 if (is_attribute_p ("transaction_callable", attr))
4425 return TM_ATTR_CALLABLE;
4426 if (is_attribute_p ("transaction_pure", attr))
4427 return TM_ATTR_PURE;
4428 if (is_attribute_p ("transaction_unsafe", attr))
4429 return TM_ATTR_IRREVOCABLE;
4430 if (is_attribute_p ("transaction_may_cancel_outer", attr))
4431 return TM_ATTR_MAY_CANCEL_OUTER;
4432 return 0;
4433 }
4434
4435 tree
4436 tm_mask_to_attr (int mask)
4437 {
4438 const char *str;
4439 switch (mask)
4440 {
4441 case TM_ATTR_SAFE:
4442 str = "transaction_safe";
4443 break;
4444 case TM_ATTR_CALLABLE:
4445 str = "transaction_callable";
4446 break;
4447 case TM_ATTR_PURE:
4448 str = "transaction_pure";
4449 break;
4450 case TM_ATTR_IRREVOCABLE:
4451 str = "transaction_unsafe";
4452 break;
4453 case TM_ATTR_MAY_CANCEL_OUTER:
4454 str = "transaction_may_cancel_outer";
4455 break;
4456 default:
4457 gcc_unreachable ();
4458 }
4459 return get_identifier (str);
4460 }
4461
4462 /* Return the first TM attribute seen in LIST. */
4463
4464 tree
4465 find_tm_attribute (tree list)
4466 {
4467 for (; list ; list = TREE_CHAIN (list))
4468 {
4469 tree name = get_attribute_name (list);
4470 if (tm_attr_to_mask (name) != 0)
4471 return name;
4472 }
4473 return NULL_TREE;
4474 }
4475
4476 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
4477 Here we accept only function types, and verify that none of the other
4478 function TM attributes are also applied. */
4479 /* ??? We need to accept class types for C++, but not C. This greatly
4480 complicates this function, since we can no longer rely on the extra
4481 processing given by function_type_required. */
4482
4483 static tree
4484 handle_tm_attribute (tree *node, tree name, tree args,
4485 int flags, bool *no_add_attrs)
4486 {
4487 /* Only one path adds the attribute; others don't. */
4488 *no_add_attrs = true;
4489
4490 switch (TREE_CODE (*node))
4491 {
4492 case RECORD_TYPE:
4493 case UNION_TYPE:
4494 /* Only tm_callable and tm_safe apply to classes. */
4495 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
4496 goto ignored;
4497 /* FALLTHRU */
4498
4499 case FUNCTION_TYPE:
4500 case METHOD_TYPE:
4501 {
4502 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
4503 if (old_name == name)
4504 ;
4505 else if (old_name != NULL_TREE)
4506 error ("type was previously declared %qE", old_name);
4507 else
4508 *no_add_attrs = false;
4509 }
4510 break;
4511
4512 case FUNCTION_DECL:
4513 {
4514 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
4515 want to set transaction_safe on the type. */
4516 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
4517 if (!TYPE_P (DECL_CONTEXT (*node)))
4518 error_at (DECL_SOURCE_LOCATION (*node),
4519 "%<transaction_safe_dynamic%> may only be specified for "
4520 "a virtual function");
4521 *no_add_attrs = false;
4522 decl_attributes (&TREE_TYPE (*node),
4523 build_tree_list (get_identifier ("transaction_safe"),
4524 NULL_TREE),
4525 0);
4526 break;
4527 }
4528
4529 case POINTER_TYPE:
4530 {
4531 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
4532 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
4533 {
4534 tree fn_tmp = TREE_TYPE (*node);
4535 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
4536 *node = build_pointer_type (fn_tmp);
4537 break;
4538 }
4539 }
4540 /* FALLTHRU */
4541
4542 default:
4543 /* If a function is next, pass it on to be tried next. */
4544 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
4545 return tree_cons (name, args, NULL);
4546
4547 ignored:
4548 warning (OPT_Wattributes, "%qE attribute ignored", name);
4549 break;
4550 }
4551
4552 return NULL_TREE;
4553 }
4554
4555 /* Handle the TM_WRAP attribute; arguments as in
4556 struct attribute_spec.handler. */
4557
4558 static tree
4559 handle_tm_wrap_attribute (tree *node, tree name, tree args,
4560 int ARG_UNUSED (flags), bool *no_add_attrs)
4561 {
4562 tree decl = *node;
4563
4564 /* We don't need the attribute even on success, since we
4565 record the entry in an external table. */
4566 *no_add_attrs = true;
4567
4568 if (TREE_CODE (decl) != FUNCTION_DECL)
4569 warning (OPT_Wattributes, "%qE attribute ignored", name);
4570 else
4571 {
4572 tree wrap_decl = TREE_VALUE (args);
4573 if (error_operand_p (wrap_decl))
4574 ;
4575 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
4576 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4577 error ("%qE argument not an identifier", name);
4578 else
4579 {
4580 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
4581 wrap_decl = lookup_name (wrap_decl);
4582 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
4583 {
4584 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
4585 TREE_TYPE (wrap_decl)))
4586 record_tm_replacement (wrap_decl, decl);
4587 else
4588 error ("%qD is not compatible with %qD", wrap_decl, decl);
4589 }
4590 else
4591 error ("%qE argument is not a function", name);
4592 }
4593 }
4594
4595 return NULL_TREE;
4596 }
4597
4598 /* Ignore the given attribute. Used when this attribute may be usefully
4599 overridden by the target, but is not used generically. */
4600
4601 static tree
4602 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
4603 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4604 bool *no_add_attrs)
4605 {
4606 *no_add_attrs = true;
4607 return NULL_TREE;
4608 }
4609
4610 /* Handle a "no vops" attribute; arguments as in
4611 struct attribute_spec.handler. */
4612
4613 static tree
4614 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
4615 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4616 bool *ARG_UNUSED (no_add_attrs))
4617 {
4618 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
4619 DECL_IS_NOVOPS (*node) = 1;
4620 return NULL_TREE;
4621 }
4622
4623 /* Handle a "deprecated" attribute; arguments as in
4624 struct attribute_spec.handler. */
4625
4626 tree
4627 handle_deprecated_attribute (tree *node, tree name,
4628 tree args, int flags,
4629 bool *no_add_attrs)
4630 {
4631 tree type = NULL_TREE;
4632 int warn = 0;
4633 tree what = NULL_TREE;
4634
4635 if (!args)
4636 *no_add_attrs = true;
4637 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4638 {
4639 error ("deprecated message is not a string");
4640 *no_add_attrs = true;
4641 }
4642
4643 if (DECL_P (*node))
4644 {
4645 tree decl = *node;
4646 type = TREE_TYPE (decl);
4647
4648 if (TREE_CODE (decl) == TYPE_DECL
4649 || TREE_CODE (decl) == PARM_DECL
4650 || VAR_OR_FUNCTION_DECL_P (decl)
4651 || TREE_CODE (decl) == FIELD_DECL
4652 || TREE_CODE (decl) == CONST_DECL
4653 || objc_method_decl (TREE_CODE (decl))
4654 || TREE_CODE (decl) == CONCEPT_DECL)
4655 TREE_DEPRECATED (decl) = 1;
4656 else if (TREE_CODE (decl) == LABEL_DECL)
4657 {
4658 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4659 name);
4660 *no_add_attrs = true;
4661 return NULL_TREE;
4662 }
4663 else
4664 warn = 1;
4665 }
4666 else if (TYPE_P (*node))
4667 {
4668 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4669 *node = build_variant_type_copy (*node);
4670 TREE_DEPRECATED (*node) = 1;
4671 type = *node;
4672 }
4673 else
4674 warn = 1;
4675
4676 if (warn)
4677 {
4678 *no_add_attrs = true;
4679 if (type && TYPE_NAME (type))
4680 {
4681 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4682 what = TYPE_NAME (type);
4683 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4684 && DECL_NAME (TYPE_NAME (type)))
4685 what = DECL_NAME (TYPE_NAME (type));
4686 }
4687 if (what)
4688 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
4689 else
4690 warning (OPT_Wattributes, "%qE attribute ignored", name);
4691 }
4692
4693 return NULL_TREE;
4694 }
4695
4696 /* Handle a "unavailable" attribute; arguments as in
4697 struct attribute_spec.handler. */
4698
4699 static tree
4700 handle_unavailable_attribute (tree *node, tree name,
4701 tree args, int flags,
4702 bool *no_add_attrs)
4703 {
4704 tree type = NULL_TREE;
4705 int warn = 0;
4706 tree what = NULL_TREE;
4707
4708 if (!args)
4709 *no_add_attrs = true;
4710 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4711 {
4712 error ("the message attached to %<unavailable%> is not a string");
4713 *no_add_attrs = true;
4714 }
4715
4716 if (DECL_P (*node))
4717 {
4718 tree decl = *node;
4719 type = TREE_TYPE (decl);
4720
4721 if (TREE_CODE (decl) == TYPE_DECL
4722 || TREE_CODE (decl) == PARM_DECL
4723 || VAR_OR_FUNCTION_DECL_P (decl)
4724 || TREE_CODE (decl) == FIELD_DECL
4725 || TREE_CODE (decl) == CONST_DECL
4726 || objc_method_decl (TREE_CODE (decl)))
4727 TREE_UNAVAILABLE (decl) = 1;
4728 else
4729 warn = 1;
4730 }
4731 else if (TYPE_P (*node))
4732 {
4733 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4734 *node = build_variant_type_copy (*node);
4735 TREE_UNAVAILABLE (*node) = 1;
4736 type = *node;
4737 }
4738 else
4739 warn = 1;
4740
4741 if (warn)
4742 {
4743 *no_add_attrs = true;
4744 if (type && TYPE_NAME (type))
4745 {
4746 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4747 what = TYPE_NAME (*node);
4748 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4749 && DECL_NAME (TYPE_NAME (type)))
4750 what = DECL_NAME (TYPE_NAME (type));
4751 }
4752 if (what)
4753 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
4754 else
4755 warning (OPT_Wattributes, "%qE attribute ignored", name);
4756 }
4757
4758 return NULL_TREE;
4759 }
4760
4761 /* Return the "base" type from TYPE that is suitable to apply attribute
4762 vector_size to by stripping arrays, function types, etc. */
4763 static tree
4764 type_for_vector_size (tree type)
4765 {
4766 /* We need to provide for vector pointers, vector arrays, and
4767 functions returning vectors. For example:
4768
4769 __attribute__((vector_size(16))) short *foo;
4770
4771 In this case, the mode is SI, but the type being modified is
4772 HI, so we need to look further. */
4773
4774 while (POINTER_TYPE_P (type)
4775 || TREE_CODE (type) == FUNCTION_TYPE
4776 || TREE_CODE (type) == METHOD_TYPE
4777 || TREE_CODE (type) == ARRAY_TYPE
4778 || TREE_CODE (type) == OFFSET_TYPE)
4779 type = TREE_TYPE (type);
4780
4781 return type;
4782 }
4783
4784 /* Given TYPE, return the base type to which the vector_size attribute
4785 ATNAME with ARGS, when non-null, can be applied, if one exists.
4786 On success and when both ARGS and PTRNUNITS are non-null, set
4787 *PTRNUNINTS to the number of vector units. When PTRNUNITS is not
4788 null, issue a warning when the attribute argument is not constant
4789 and an error if there is no such type. Otherwise issue a warning
4790 in the latter case and return null. */
4791
4792 static tree
4793 type_valid_for_vector_size (tree type, tree atname, tree args,
4794 unsigned HOST_WIDE_INT *ptrnunits)
4795 {
4796 bool hardbool_p = c_hardbool_type_attr (type);
4797 bool error_p = ptrnunits != NULL || hardbool_p;
4798
4799 /* Get the mode of the type being modified. */
4800 machine_mode orig_mode = TYPE_MODE (type);
4801
4802 if ((!INTEGRAL_TYPE_P (type)
4803 && !SCALAR_FLOAT_TYPE_P (type)
4804 && !FIXED_POINT_TYPE_P (type))
4805 || (!SCALAR_FLOAT_MODE_P (orig_mode)
4806 && GET_MODE_CLASS (orig_mode) != MODE_INT
4807 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
4808 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
4809 || TREE_CODE (type) == BOOLEAN_TYPE
4810 || hardbool_p
4811 || TREE_CODE (type) == BITINT_TYPE)
4812 {
4813 if (error_p)
4814 error ("invalid vector type for attribute %qE", atname);
4815 else
4816 warning (OPT_Wattributes, "invalid vector type for attribute %qE",
4817 atname);
4818 return NULL_TREE;
4819 }
4820
4821 /* When no argument has been provided this is just a request to validate
4822 the type above. Return TYPE to indicate success. */
4823 if (!args)
4824 return type;
4825
4826 tree size = TREE_VALUE (args);
4827 /* Erroneous arguments have already been diagnosed. */
4828 if (size == error_mark_node)
4829 return NULL_TREE;
4830
4831 if (size && TREE_CODE (size) != IDENTIFIER_NODE
4832 && TREE_CODE (size) != FUNCTION_DECL)
4833 size = default_conversion (size);
4834
4835 if (TREE_CODE (size) != INTEGER_CST)
4836 {
4837 if (error_p)
4838 error ("%qE attribute argument value %qE is not an integer constant",
4839 atname, size);
4840 else
4841 warning (OPT_Wattributes,
4842 "%qE attribute argument value %qE is not an integer constant",
4843 atname, size);
4844 return NULL_TREE;
4845 }
4846
4847 if (!TYPE_UNSIGNED (TREE_TYPE (size))
4848 && tree_int_cst_sgn (size) < 0)
4849 {
4850 if (error_p)
4851 error ("%qE attribute argument value %qE is negative",
4852 atname, size);
4853 else
4854 warning (OPT_Wattributes,
4855 "%qE attribute argument value %qE is negative",
4856 atname, size);
4857 return NULL_TREE;
4858 }
4859
4860 /* The attribute argument value is constrained by the maximum bit
4861 alignment representable in unsigned int on the host. */
4862 unsigned HOST_WIDE_INT vecsize;
4863 unsigned HOST_WIDE_INT maxsize = tree_to_uhwi (max_object_size ());
4864 if (!tree_fits_uhwi_p (size)
4865 || (vecsize = tree_to_uhwi (size)) > maxsize)
4866 {
4867 if (error_p)
4868 error ("%qE attribute argument value %qE exceeds %wu",
4869 atname, size, maxsize);
4870 else
4871 warning (OPT_Wattributes,
4872 "%qE attribute argument value %qE exceeds %wu",
4873 atname, size, maxsize);
4874 return NULL_TREE;
4875 }
4876
4877 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
4878 {
4879 if (error_p)
4880 error ("vector size not an integral multiple of component size");
4881 return NULL_TREE;
4882 }
4883
4884 if (vecsize == 0)
4885 {
4886 error ("zero vector size");
4887 return NULL;
4888 }
4889
4890 /* Calculate how many units fit in the vector. */
4891 unsigned HOST_WIDE_INT nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
4892 if (nunits & (nunits - 1))
4893 {
4894 if (error_p)
4895 error ("number of vector components %wu not a power of two", nunits);
4896 else
4897 warning (OPT_Wattributes,
4898 "number of vector components %wu not a power of two", nunits);
4899 return NULL_TREE;
4900 }
4901
4902 if (nunits >= (unsigned HOST_WIDE_INT)INT_MAX)
4903 {
4904 if (error_p)
4905 error ("number of vector components %wu exceeds %d",
4906 nunits, INT_MAX - 1);
4907 else
4908 warning (OPT_Wattributes,
4909 "number of vector components %wu exceeds %d",
4910 nunits, INT_MAX - 1);
4911 return NULL_TREE;
4912 }
4913
4914 if (ptrnunits)
4915 *ptrnunits = nunits;
4916
4917 return type;
4918 }
4919
4920 /* Handle a "vector_size" attribute; arguments as in
4921 struct attribute_spec.handler. */
4922
4923 static tree
4924 handle_vector_size_attribute (tree *node, tree name, tree args,
4925 int ARG_UNUSED (flags),
4926 bool *no_add_attrs)
4927 {
4928 *no_add_attrs = true;
4929
4930 /* Determine the "base" type to apply the attribute to. */
4931 tree type = type_for_vector_size (*node);
4932
4933 /* Get the vector size (in bytes) and let the function compute
4934 the number of vector units. */
4935 unsigned HOST_WIDE_INT nunits;
4936 type = type_valid_for_vector_size (type, name, args, &nunits);
4937 if (!type)
4938 return NULL_TREE;
4939
4940 gcc_checking_assert (args != NULL);
4941
4942 tree new_type = build_vector_type (type, nunits);
4943
4944 /* Build back pointers if needed. */
4945 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
4946
4947 return NULL_TREE;
4948 }
4949
4950 /* Handle a "vector_mask" attribute; arguments as in
4951 struct attribute_spec.handler. */
4952
4953 static tree
4954 handle_vector_mask_attribute (tree *node, tree name, tree,
4955 int ARG_UNUSED (flags),
4956 bool *no_add_attrs)
4957 {
4958 *no_add_attrs = true;
4959 if (!flag_gimple)
4960 {
4961 warning (OPT_Wattributes, "%qE attribute ignored", name);
4962 return NULL_TREE;
4963 }
4964
4965 /* Determine the "base" type to apply the attribute to. */
4966 tree type = type_for_vector_size (*node);
4967 if (!VECTOR_TYPE_P (type) || VECTOR_BOOLEAN_TYPE_P (type))
4968 {
4969 warning (OPT_Wattributes, "%qE attribute only supported on "
4970 "non-mask vector types", name);
4971 return NULL_TREE;
4972 }
4973
4974 tree new_type = truth_type_for (type);
4975
4976 /* Build back pointers if needed. */
4977 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
4978
4979 return NULL_TREE;
4980 }
4981
4982 /* Handle the "nonnull" attribute. */
4983
4984 static tree
4985 handle_nonnull_attribute (tree *node, tree name,
4986 tree args, int ARG_UNUSED (flags),
4987 bool *no_add_attrs)
4988 {
4989 tree type = *node;
4990
4991 /* If no arguments are specified, all pointer arguments should be
4992 non-null. Verify a full prototype is given so that the arguments
4993 will have the correct types when we actually check them later.
4994 Avoid diagnosing type-generic built-ins since those have no
4995 prototype. */
4996 if (!args)
4997 {
4998 if (!prototype_p (type)
4999 && (!TYPE_ATTRIBUTES (type)
5000 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
5001 {
5002 error ("%qE attribute without arguments on a non-prototype",
5003 name);
5004 *no_add_attrs = true;
5005 }
5006 return NULL_TREE;
5007 }
5008
5009 for (int i = 1; args; ++i)
5010 {
5011 tree pos = TREE_VALUE (args);
5012 /* NEXT is null when the attribute includes just one argument.
5013 That's used to tell positional_argument to avoid mentioning
5014 the argument number in diagnostics (since there's just one
5015 mentioning it is unnecessary and could be confusing). */
5016 tree next = TREE_CHAIN (args);
5017 if (tree val = positional_argument (type, name, pos, POINTER_TYPE,
5018 next || i > 1 ? i : 0))
5019 TREE_VALUE (args) = val;
5020 else
5021 {
5022 *no_add_attrs = true;
5023 break;
5024 }
5025 args = next;
5026 }
5027
5028 return NULL_TREE;
5029 }
5030
5031 /* Handle the "nonnull_if_nonzero" attribute. */
5032
5033 static tree
5034 handle_nonnull_if_nonzero_attribute (tree *node, tree name,
5035 tree args, int ARG_UNUSED (flags),
5036 bool *no_add_attrs)
5037 {
5038 tree type = *node;
5039 tree pos = TREE_VALUE (args);
5040 tree pos2 = TREE_VALUE (TREE_CHAIN (args));
5041 tree val = positional_argument (type, name, pos, POINTER_TYPE, 1);
5042 tree val2 = positional_argument (type, name, pos2, INTEGER_TYPE, 2);
5043 if (val && val2)
5044 {
5045 TREE_VALUE (args) = val;
5046 TREE_VALUE (TREE_CHAIN (args)) = val2;
5047 }
5048 else
5049 *no_add_attrs = true;
5050
5051 return NULL_TREE;
5052 }
5053
5054 /* Handle the "fd_arg", "fd_arg_read" and "fd_arg_write" attributes */
5055
5056 static tree
5057 handle_fd_arg_attribute (tree *node, tree name, tree args,
5058 int ARG_UNUSED (flags), bool *no_add_attrs)
5059 {
5060 tree type = *node;
5061 if (!args)
5062 {
5063 if (!prototype_p (type))
5064 {
5065 error ("%qE attribute without arguments on a non-prototype", name);
5066 *no_add_attrs = true;
5067 }
5068 return NULL_TREE;
5069 }
5070
5071 if (positional_argument (*node, name, TREE_VALUE (args), INTEGER_TYPE))
5072 return NULL_TREE;
5073
5074 *no_add_attrs = true;
5075 return NULL_TREE;
5076 }
5077
5078 /* Handle the "flag_enum" attribute. */
5079
5080 static tree
5081 handle_flag_enum_attribute (tree *node, tree ARG_UNUSED (name),
5082 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5083 bool *no_add_attrs)
5084 {
5085 if (TREE_CODE (*node) != ENUMERAL_TYPE)
5086 {
5087 warning (OPT_Wattributes, "%qE attribute ignored on non-enum", name);
5088 *no_add_attrs = true;
5089 }
5090
5091 return NULL_TREE;
5092 }
5093
5094 /* Handle the "null_terminated_string_arg" attribute. */
5095
5096 static tree
5097 handle_null_terminated_string_arg_attribute (tree *node, tree name, tree args,
5098 int ARG_UNUSED (flags),
5099 bool *no_add_attrs)
5100 {
5101 if (positional_argument (*node, name, TREE_VALUE (args), POINTER_TYPE))
5102 return NULL_TREE;
5103
5104 *no_add_attrs = true;
5105 return NULL_TREE;
5106 }
5107
5108 /* Handle the "nonstring" variable attribute. */
5109
5110 static tree
5111 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5112 int ARG_UNUSED (flags), bool *no_add_attrs)
5113 {
5114 gcc_assert (!args);
5115 tree_code code = TREE_CODE (*node);
5116
5117 if (VAR_P (*node)
5118 || code == FIELD_DECL
5119 || code == PARM_DECL)
5120 {
5121 tree type = TREE_TYPE (*node);
5122
5123 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
5124 {
5125 /* Accept the attribute on arrays and pointers to all three
5126 narrow character types, including multi-dimensional arrays
5127 or pointers to them. */
5128 tree eltype = strip_array_types (TREE_TYPE (type));
5129 eltype = TYPE_MAIN_VARIANT (eltype);
5130 if (eltype == char_type_node
5131 || eltype == signed_char_type_node
5132 || eltype == unsigned_char_type_node)
5133 return NULL_TREE;
5134 }
5135
5136 warning (OPT_Wattributes,
5137 "%qE attribute ignored on objects of type %qT",
5138 name, type);
5139 *no_add_attrs = true;
5140 return NULL_TREE;
5141 }
5142
5143 if (code == FUNCTION_DECL)
5144 warning (OPT_Wattributes,
5145 "%qE attribute does not apply to functions", name);
5146 else if (code == TYPE_DECL)
5147 warning (OPT_Wattributes,
5148 "%qE attribute does not apply to types", name);
5149 else
5150 warning (OPT_Wattributes, "%qE attribute ignored", name);
5151
5152 *no_add_attrs = true;
5153 return NULL_TREE;
5154 }
5155
5156 /* Given a function type FUNCTYPE, returns the type of the parameter
5157 ARGNO or null if ARGNO exceeds the number of parameters. On failure
5158 set *NARGS to the number of function parameters. */
5159
5160 static tree
5161 get_argument_type (tree functype, unsigned argno, unsigned *nargs)
5162 {
5163 function_args_iterator iter;
5164 function_args_iter_init (&iter, functype);
5165
5166 unsigned count = 0;
5167
5168 for ( ; iter.next; ++count, function_args_iter_next (&iter))
5169 {
5170 if (count + 1 == argno)
5171 {
5172 tree argtype = function_args_iter_cond (&iter);
5173 if (VOID_TYPE_P (argtype))
5174 break;
5175 if (argtype != error_mark_node)
5176 return argtype;
5177 }
5178 }
5179
5180 *nargs = count;
5181 return NULL_TREE;
5182 }
5183
5184 /* Given a function FNDECL return the function argument at the zero-
5185 based position ARGNO or null if it can't be found. */
5186
5187 static tree
5188 get_argument (tree fndecl, unsigned argno)
5189 {
5190 if (!DECL_P (fndecl))
5191 return NULL_TREE;
5192
5193 unsigned i = 0;
5194 for (tree arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
5195 if (i++ == argno)
5196 return arg;
5197
5198 return NULL_TREE;
5199 }
5200
5201 /* Attempt to append attribute access specification ATTRSPEC, optionally
5202 described by the human-readable string ATTRSTR, for type T, to one in
5203 ATTRS. VBLIST is an optional list of bounds of variable length array
5204 parameters described by ATTRSTR.
5205 Issue warning for conflicts and return null if any are found.
5206 Return the concatenated access string on success. */
5207
5208 static tree
5209 append_access_attr (tree node[3], tree attrs, const char *attrstr,
5210 const char *attrspec, tree vblist = NULL_TREE)
5211 {
5212 tree argstr = build_string (strlen (attrspec) + 1, attrspec);
5213 tree ataccess = tree_cons (NULL_TREE, argstr, vblist);
5214 ataccess = tree_cons (get_identifier ("access"), ataccess, NULL_TREE);
5215
5216 /* The access specification being applied. This may be an implicit
5217 access spec synthesized for array (or VLA) parameters even for
5218 a declaration with an explicit access spec already applied, if
5219 this call corresponds to the first declaration of the function. */
5220 rdwr_map new_idxs;
5221 init_attr_rdwr_indices (&new_idxs, ataccess);
5222
5223 /* The current access specification alrady applied. */
5224 rdwr_map cur_idxs;
5225 init_attr_rdwr_indices (&cur_idxs, attrs);
5226
5227 tree args = TYPE_ARG_TYPES (node[0]);
5228 int argpos = 0;
5229 std::string spec;
5230 for (tree arg = args; arg; arg = TREE_CHAIN (arg), argpos++)
5231 {
5232 const attr_access* const newa = new_idxs.get (argpos);
5233
5234 if (!newa)
5235 continue;
5236
5237 /* The map has two equal entries for each pointer argument that
5238 has an associated size argument. Process just the entry for
5239 the former. */
5240 if ((unsigned)argpos != newa->ptrarg)
5241 continue;
5242
5243 const attr_access* const cura = cur_idxs.get (argpos);
5244 if (!cura)
5245 {
5246 /* The new attribute needs to be added. */
5247 tree str = newa->to_internal_string ();
5248 spec += TREE_STRING_POINTER (str);
5249 continue;
5250 }
5251
5252 /* The new access spec refers to an array/pointer argument for
5253 which an access spec already exists. Check and diagnose any
5254 conflicts. If no conflicts are found, merge the two. */
5255
5256 if (!attrstr)
5257 {
5258 tree str = NULL_TREE;
5259 if (newa->mode != access_deferred)
5260 str = newa->to_external_string ();
5261 else if (cura->mode != access_deferred)
5262 str = cura->to_external_string ();
5263 if (str)
5264 attrstr = TREE_STRING_POINTER (str);
5265 }
5266
5267 location_t curloc = input_location;
5268 if (node[2] && DECL_P (node[2]))
5269 curloc = DECL_SOURCE_LOCATION (node[2]);
5270
5271 location_t prevloc = UNKNOWN_LOCATION;
5272 if (node[1] && DECL_P (node[1]))
5273 prevloc = DECL_SOURCE_LOCATION (node[1]);
5274
5275 if (newa->mode != cura->mode
5276 && newa->mode != access_deferred
5277 && cura->mode != access_deferred
5278 && newa->internal_p == cura->internal_p)
5279 {
5280 /* Mismatch in access mode. */
5281 auto_diagnostic_group d;
5282 if (warning_at (curloc, OPT_Wattributes,
5283 "attribute %qs mismatch with mode %qs",
5284 attrstr, cura->mode_names[cura->mode])
5285 && prevloc != UNKNOWN_LOCATION)
5286 inform (prevloc, "previous declaration here");
5287 continue;
5288 }
5289
5290 /* Set if PTRARG refers to a VLA with an unspecified bound (T[*]).
5291 Be prepared for either CURA or NEWA to refer to it, depending
5292 on which happens to come first in the declaration. */
5293 const bool cur_vla_ub = (cura->internal_p
5294 && cura->sizarg == UINT_MAX
5295 && cura->minsize == HOST_WIDE_INT_M1U);
5296 const bool new_vla_ub = (newa->internal_p
5297 && newa->sizarg == UINT_MAX
5298 && newa->minsize == HOST_WIDE_INT_M1U);
5299
5300 if (newa->sizarg != cura->sizarg
5301 && attrstr
5302 && (!(cur_vla_ub ^ new_vla_ub)
5303 || (!cura->internal_p && !newa->internal_p)))
5304 {
5305 /* Avoid diagnosing redeclarations of functions with no explicit
5306 attribute access that add one. */
5307 if (newa->mode == access_deferred
5308 && cura->mode != access_deferred
5309 && newa->sizarg == UINT_MAX
5310 && cura->sizarg != UINT_MAX)
5311 continue;
5312
5313 if (cura->mode == access_deferred
5314 && newa->mode != access_deferred
5315 && cura->sizarg == UINT_MAX
5316 && newa->sizarg != UINT_MAX)
5317 continue;
5318
5319 /* The two specs designate different size arguments. It's okay
5320 for the explicit spec to specify a size where none is provided
5321 by the implicit (VLA) one, as in:
5322 __attribute__ ((access (read_write, 1, 2)))
5323 void f (int*, int);
5324 but not for two explicit access attributes to do that. */
5325 bool warned = false;
5326
5327 auto_diagnostic_group d;
5328
5329 if (newa->sizarg == UINT_MAX)
5330 /* Mismatch in the presence of the size argument. */
5331 warned = warning_at (curloc, OPT_Wattributes,
5332 "attribute %qs missing positional argument 2 "
5333 "provided in previous designation by argument "
5334 "%u", attrstr, cura->sizarg + 1);
5335 else if (cura->sizarg == UINT_MAX)
5336 /* Mismatch in the presence of the size argument. */
5337 warned = warning_at (curloc, OPT_Wattributes,
5338 "attribute %qs positional argument 2 "
5339 "missing in previous designation",
5340 attrstr);
5341 else if (newa->internal_p || cura->internal_p)
5342 /* Mismatch in the value of the size argument and a VLA bound. */
5343 warned = warning_at (curloc, OPT_Wattributes,
5344 "attribute %qs positional argument 2 "
5345 "conflicts with previous designation "
5346 "by argument %u",
5347 attrstr, cura->sizarg + 1);
5348 else
5349 /* Mismatch in the value of the size argument between two
5350 explicit access attributes. */
5351 warned = warning_at (curloc, OPT_Wattributes,
5352 "attribute %qs mismatched positional argument "
5353 "values %i and %i",
5354 attrstr, newa->sizarg + 1, cura->sizarg + 1);
5355
5356 if (warned)
5357 {
5358 /* If the previous declaration is a function (as opposed
5359 to a typedef of one), find the location of the array
5360 or pointer argument that uses the conflicting VLA bound
5361 and point to it in the note. */
5362 const attr_access* const pa = cura->size ? cura : newa;
5363 tree size = pa->size ? TREE_VALUE (pa->size) : NULL_TREE;
5364 if (size && DECL_P (size))
5365 {
5366 location_t argloc = UNKNOWN_LOCATION;
5367 if (tree arg = get_argument (node[2], pa->ptrarg))
5368 argloc = DECL_SOURCE_LOCATION (arg);
5369
5370 gcc_rich_location richloc (DECL_SOURCE_LOCATION (size));
5371 if (argloc != UNKNOWN_LOCATION)
5372 richloc.add_range (argloc);
5373
5374 inform (&richloc, "designating the bound of variable "
5375 "length array argument %u",
5376 pa->ptrarg + 1);
5377 }
5378 else if (prevloc != UNKNOWN_LOCATION)
5379 inform (prevloc, "previous declaration here");
5380 }
5381
5382 continue;
5383 }
5384
5385 if (newa->internal_p == cura->internal_p)
5386 continue;
5387
5388 /* Merge the CURA and NEWA. */
5389 attr_access merged = *newa;
5390
5391 /* VLA seen in a declaration takes precedence. */
5392 if (cura->minsize == HOST_WIDE_INT_M1U)
5393 merged.minsize = HOST_WIDE_INT_M1U;
5394
5395 /* Use the explicitly specified size positional argument. */
5396 if (cura->sizarg != UINT_MAX)
5397 merged.sizarg = cura->sizarg;
5398
5399 /* Use the explicitly specified mode. */
5400 if (merged.mode == access_deferred)
5401 merged.mode = cura->mode;
5402
5403 tree str = merged.to_internal_string ();
5404 spec += TREE_STRING_POINTER (str);
5405 }
5406
5407 if (!spec.length ())
5408 return NULL_TREE;
5409
5410 return build_string (spec.length (), spec.c_str ());
5411 }
5412
5413 /* Convenience wrapper for the above. */
5414
5415 static tree
5416 append_access_attr_idxs (tree node[3], tree attrs, const char *attrstr,
5417 char code, HOST_WIDE_INT idxs[2])
5418 {
5419 char attrspec[80];
5420 int n = sprintf (attrspec, "%c%u", code, (unsigned) idxs[0] - 1);
5421 if (idxs[1])
5422 n += sprintf (attrspec + n, ",%u", (unsigned) idxs[1] - 1);
5423
5424 return append_access_attr (node, attrs, attrstr, attrspec);
5425 }
5426
5427 /* Handle the access attribute for function type NODE[0], with the function
5428 DECL optionally in NODE[1]. The handler is called both in response to
5429 an explict attribute access on a declaration with a mode and one or two
5430 positional arguments, and for internally synthesized access specifications
5431 with a string argument optionally followd by a DECL or expression
5432 representing a VLA bound. To speed up parsing, the handler transforms
5433 the attribute and its arguments into a string. */
5434
5435 static tree
5436 handle_access_attribute (tree node[3], tree name, tree args, int flags,
5437 bool *no_add_attrs)
5438 {
5439 tree attrs = TYPE_ATTRIBUTES (*node);
5440 tree type = *node;
5441 if (POINTER_TYPE_P (type))
5442 {
5443 tree ptype = TREE_TYPE (type);
5444 if (FUNC_OR_METHOD_TYPE_P (ptype))
5445 type = ptype;
5446 }
5447
5448 *no_add_attrs = true;
5449
5450 /* Verify a full prototype is provided so that the argument types
5451 can be validated. Avoid diagnosing type-generic built-ins since
5452 those have no prototype. */
5453 if (!args
5454 && !prototype_p (type)
5455 && (!attrs || !lookup_attribute ("type generic", attrs)))
5456 {
5457 error ("attribute %qE without arguments on a non-prototype", name);
5458 return NULL_TREE;
5459 }
5460
5461 tree access_mode = TREE_VALUE (args);
5462 if (TREE_CODE (access_mode) == STRING_CST)
5463 {
5464 const char* const str = TREE_STRING_POINTER (access_mode);
5465 if (*str == '+')
5466 {
5467 /* This is a request to merge an internal specification for
5468 a function declaration involving arrays but no explicit
5469 attribute access. */
5470 tree vblist = TREE_CHAIN (args);
5471 tree axstr = append_access_attr (node, attrs, NULL, str + 1,
5472 vblist);
5473 if (!axstr)
5474 return NULL_TREE;
5475
5476 /* Replace any existing access attribute specification with
5477 the concatenation above. */
5478 tree axsat = tree_cons (NULL_TREE, axstr, vblist);
5479 axsat = tree_cons (name, axsat, NULL_TREE);
5480
5481 /* Recursively call self to "replace" the documented/external
5482 form of the attribute with the condensend internal form. */
5483 decl_attributes (node, axsat, flags | ATTR_FLAG_INTERNAL);
5484 return NULL_TREE;
5485 }
5486
5487 if (flags & ATTR_FLAG_INTERNAL)
5488 {
5489 /* This is a recursive call to handle the condensed internal
5490 form of the attribute (see below). Since all validation
5491 has been done simply return here, accepting the attribute
5492 as is. */
5493 *no_add_attrs = false;
5494 return NULL_TREE;
5495 }
5496 }
5497
5498 /* Set to true when the access mode has the form of a function call
5499 as in 'attribute (read_only (1, 2))'. That's an easy mistake to
5500 make and so worth a special diagnostic. */
5501 bool funcall = false;
5502 if (TREE_CODE (access_mode) == CALL_EXPR)
5503 {
5504 access_mode = CALL_EXPR_FN (access_mode);
5505 if (TREE_CODE (access_mode) != ADDR_EXPR)
5506 {
5507 error ("attribute %qE invalid mode", name);
5508 return NULL_TREE;
5509 }
5510 access_mode = TREE_OPERAND (access_mode, 0);
5511 access_mode = DECL_NAME (access_mode);
5512 funcall = true;
5513 }
5514 else if (TREE_CODE (access_mode) != IDENTIFIER_NODE)
5515 {
5516 error ("attribute %qE mode %qE is not an identifier; expected one of "
5517 "%qs, %qs, %qs, or %qs", name, access_mode,
5518 "read_only", "read_write", "write_only", "none");
5519 return NULL_TREE;
5520 }
5521
5522 const char* const access_str = IDENTIFIER_POINTER (access_mode);
5523 const char *ps = access_str;
5524 if (ps[0] == '_' && ps[1] == '_')
5525 {
5526 size_t len = strlen (ps);
5527 if (ps[len - 1] == '_' && ps[len - 2] == '_')
5528 ps += 2;
5529 }
5530
5531 int imode;
5532
5533 {
5534 const int nmodes = ARRAY_SIZE (attr_access::mode_names);
5535
5536 for (imode = 0; imode != nmodes; ++imode)
5537 if (!strncmp (ps, attr_access::mode_names[imode],
5538 strlen (attr_access::mode_names[imode])))
5539 break;
5540
5541 if (imode == nmodes)
5542 {
5543 error ("attribute %qE invalid mode %qs; expected one of "
5544 "%qs, %qs, %qs, or %qs", name, access_str,
5545 "read_only", "read_write", "write_only", "none");
5546 return NULL_TREE;
5547 }
5548 }
5549
5550 const ::access_mode mode = static_cast<::access_mode>(imode);
5551
5552 if (funcall)
5553 {
5554 error ("attribute %qE unexpected %<(%> after mode %qs; expected "
5555 "a positional argument or %<)%>",
5556 name, access_str);
5557 return NULL_TREE;
5558 }
5559
5560 args = TREE_CHAIN (args);
5561 if (!args)
5562 {
5563 /* The first positional argument is required. It may be worth
5564 dropping the requirement at some point and having read_only
5565 apply to all const-qualified pointers and read_write or
5566 write_only to the rest. */
5567 error ("attribute %<%E(%s)%> missing an argument",
5568 name, access_str);
5569 return NULL_TREE;
5570 }
5571
5572 /* One or more positional arguments have been specified. Validate
5573 them. */
5574 tree idxnodes[2] = { NULL_TREE, NULL_TREE };
5575 tree argtypes[2] = { NULL_TREE, NULL_TREE };
5576 /* 1-based attribute positional arguments or zero if not specified.
5577 Invalid negative or excessive values are also stored but used
5578 only in diagnostics. */
5579 HOST_WIDE_INT idxs[2] = { 0, 0 };
5580
5581 /* Number of function formal arguments (used in diagnostics). */
5582 unsigned nfuncargs = 0;
5583 /* Number of (optional) attribute positional arguments. */
5584 unsigned nattrargs = 0;
5585
5586 for (unsigned i = 0; i != 2; ++i, args = TREE_CHAIN (args), ++nattrargs)
5587 {
5588 if (!args)
5589 break;
5590
5591 idxnodes[i] = TREE_VALUE (args);
5592
5593 if (TREE_CODE (idxnodes[i]) != IDENTIFIER_NODE
5594 && TREE_CODE (idxnodes[i]) != FUNCTION_DECL)
5595 idxnodes[i] = default_conversion (idxnodes[i]);
5596
5597 if (tree_fits_shwi_p (idxnodes[i]))
5598 {
5599 idxs[i] = tree_to_shwi (idxnodes[i]);
5600 argtypes[i] = get_argument_type (type, idxs[i], &nfuncargs);
5601 }
5602 }
5603
5604 if ((nattrargs == 1 && !idxs[0])
5605 || (nattrargs == 2 && (!idxs[0] || !idxs[1])))
5606 {
5607 if (idxnodes[1])
5608 error ("attribute %<%E(%s, %E, %E)%> invalid positional argument %i",
5609 name, access_str, idxnodes[0], idxnodes[1], idxs[0] ? 2 : 1);
5610 else
5611 error ("attribute %<%E(%s, %E)%> invalid positional argument %i",
5612 name, access_str, idxnodes[0], idxs[0] ? 2 : 1);
5613 return NULL_TREE;
5614 }
5615
5616 /* Format the attribute specification to include in diagnostics. */
5617 char attrstr[80];
5618 if (idxnodes[1])
5619 snprintf (attrstr, sizeof attrstr, "%s(%s, %lli, %lli)",
5620 IDENTIFIER_POINTER (name), access_str,
5621 (long long) idxs[0], (long long) idxs[1]);
5622 else if (idxnodes[0])
5623 snprintf (attrstr, sizeof attrstr, "%s(%s, %lli)",
5624 IDENTIFIER_POINTER (name), access_str,
5625 (long long) idxs[0]);
5626 else
5627 snprintf (attrstr, sizeof attrstr, "%s(%s)",
5628 IDENTIFIER_POINTER (name), access_str);
5629
5630 /* Verify the positional argument values are in range. */
5631 if (!argtypes[0] || (idxnodes[1] && !argtypes[1]))
5632 {
5633 if (idxnodes[0])
5634 {
5635 if (idxs[0] < 0 || idxs[1] < 0)
5636 error ("attribute %qs positional argument %i invalid value %wi",
5637 attrstr, idxs[0] < 0 ? 1 : 2,
5638 idxs[0] < 0 ? idxs[0] : idxs[1]);
5639 else
5640 error ("attribute %qs positional argument %i value %wi exceeds "
5641 "number of function arguments %u",
5642 attrstr, idxs[0] ? 1 : 2,
5643 idxs[0] ? idxs[0] : idxs[1],
5644 nfuncargs);
5645 }
5646 else
5647 error ("attribute %qs invalid positional argument", attrstr);
5648
5649 return NULL_TREE;
5650 }
5651
5652 if (!POINTER_TYPE_P (argtypes[0]))
5653 {
5654 /* The first argument must have a pointer or reference type. */
5655 error ("attribute %qs positional argument 1 references "
5656 "non-pointer argument type %qT",
5657 attrstr, argtypes[0]);
5658 return NULL_TREE;
5659 }
5660
5661 {
5662 /* Pointers to functions are not allowed. */
5663 tree ptrtype = TREE_TYPE (argtypes[0]);
5664 if (FUNC_OR_METHOD_TYPE_P (ptrtype))
5665 {
5666 error ("attribute %qs positional argument 1 references "
5667 "argument of function type %qT",
5668 attrstr, ptrtype);
5669 return NULL_TREE;
5670 }
5671 }
5672
5673 if (mode == access_read_write || mode == access_write_only)
5674 {
5675 /* Read_write and write_only modes must reference non-const
5676 arguments. */
5677 if (TYPE_READONLY (TREE_TYPE (argtypes[0])))
5678 {
5679 error ("attribute %qs positional argument 1 references "
5680 "%qs-qualified argument type %qT",
5681 attrstr, "const", argtypes[0]);
5682 return NULL_TREE;
5683 }
5684 }
5685 else if (!TYPE_READONLY (TREE_TYPE (argtypes[0])))
5686 {
5687 /* A read_only mode should ideally reference const-qualified
5688 arguments but it's not diagnosed error if one doesn't.
5689 This makes it possible to annotate legacy, const-incorrect
5690 APIs. It might be worth a diagnostic along the lines of
5691 -Wsuggest-const. */
5692 ;
5693 }
5694
5695 if (argtypes[1] && !INTEGRAL_TYPE_P (argtypes[1]))
5696 {
5697 error ("attribute %qs positional argument 2 references "
5698 "non-integer argument type %qT",
5699 attrstr, argtypes[1]);
5700 return NULL_TREE;
5701 }
5702
5703 /* Verify that the new attribute doesn't conflict with any existing
5704 attributes specified on previous declarations of the same type
5705 and if not, concatenate the two. */
5706 const char code = attr_access::mode_chars[mode];
5707 tree new_attrs = append_access_attr_idxs (node, attrs, attrstr, code, idxs);
5708 if (!new_attrs)
5709 return NULL_TREE;
5710
5711 /* Replace any existing access attribute specification with
5712 the concatenation above. */
5713 new_attrs = tree_cons (NULL_TREE, new_attrs, NULL_TREE);
5714 new_attrs = tree_cons (name, new_attrs, NULL_TREE);
5715
5716 if (node[1])
5717 {
5718 /* Repeat for the previously declared type. */
5719 attrs = TYPE_ATTRIBUTES (TREE_TYPE (node[1]));
5720 new_attrs = append_access_attr_idxs (node, attrs, attrstr, code, idxs);
5721 if (!new_attrs)
5722 return NULL_TREE;
5723
5724 new_attrs = tree_cons (NULL_TREE, new_attrs, NULL_TREE);
5725 new_attrs = tree_cons (name, new_attrs, NULL_TREE);
5726 }
5727
5728 /* Recursively call self to "replace" the documented/external form
5729 of the attribute with the condensed internal form. */
5730 decl_attributes (node, new_attrs, flags | ATTR_FLAG_INTERNAL);
5731 return NULL_TREE;
5732 }
5733
5734 /* Extract attribute "arg spec" from each FNDECL argument that has it,
5735 build a single attribute access corresponding to all the arguments,
5736 and return the result. SKIP_VOIDPTR set to ignore void* parameters
5737 (used for user-defined functions for which, unlike in for built-ins,
5738 void* cannot be relied on to determine anything about the access
5739 through it or whether it even takes place).
5740
5741 For example, the parameters in the declaration:
5742
5743 void f (int x, int y, char [x][1][y][3], char [y][2][y][5]);
5744
5745 result in the following attribute access:
5746
5747 value: "+^2[*],$0$1^3[*],$1$1"
5748 list: < <0, x> <1, y> >
5749
5750 where the list has a single value which itself is a list, each
5751 of whose <node>s corresponds to one VLA bound for each of the two
5752 parameters. */
5753
5754 tree
5755 build_attr_access_from_parms (tree parms, bool skip_voidptr)
5756 {
5757 /* Maps each named integral argument DECL seen so far to its position
5758 in the argument list; used to associate VLA sizes with arguments. */
5759 hash_map<tree, unsigned> arg2pos;
5760
5761 /* The string representation of the access specification for all
5762 arguments. */
5763 std::string spec;
5764 unsigned argpos = 0;
5765
5766 /* A TREE_LIST of VLA bounds. */
5767 tree vblist = NULL_TREE;
5768
5769 for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos)
5770 {
5771 if (!DECL_P (arg))
5772 continue;
5773
5774 tree argtype = TREE_TYPE (arg);
5775 if (DECL_NAME (arg) && INTEGRAL_TYPE_P (argtype))
5776 arg2pos.put (arg, argpos);
5777 }
5778
5779 tree nnlist = NULL_TREE;
5780 argpos = 0;
5781 for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos)
5782 {
5783 if (!DECL_P (arg))
5784 continue;
5785
5786 tree argtype = TREE_TYPE (arg);
5787
5788 tree argspec = DECL_ATTRIBUTES (arg);
5789 if (!argspec)
5790 continue;
5791
5792 if (POINTER_TYPE_P (argtype))
5793 {
5794 /* void* arguments in user-defined functions could point to
5795 anything; skip them. */
5796 tree reftype = TREE_TYPE (argtype);
5797 if (skip_voidptr && VOID_TYPE_P (reftype))
5798 continue;
5799 }
5800
5801 /* Each parameter should have at most one "arg spec" attribute. */
5802 argspec = lookup_attribute ("arg spec", argspec);
5803 if (!argspec)
5804 continue;
5805
5806 /* Attribute arg spec should have one or two arguments. */
5807 argspec = TREE_VALUE (argspec);
5808
5809 /* The attribute arg spec string. */
5810 tree str = TREE_VALUE (argspec);
5811 const char *s = TREE_STRING_POINTER (str);
5812
5813 /* Collect the list of nonnull arguments which use "[static ..]". */
5814 if (s != NULL && s[0] == '[' && s[1] == 's')
5815 nnlist = tree_cons (NULL_TREE, build_int_cst (integer_type_node,
5816 argpos + 1), nnlist);
5817
5818 /* Create the attribute access string from the arg spec string,
5819 optionally followed by position of the VLA bound argument if
5820 it is one. */
5821 {
5822 size_t specend = spec.length ();
5823 if (!specend)
5824 {
5825 spec = '+';
5826 specend = 1;
5827 }
5828
5829 /* Format the access string in place. */
5830 int len = snprintf (NULL, 0, "%c%u%s",
5831 attr_access::mode_chars[access_deferred],
5832 argpos, s);
5833 spec.resize (specend + len + 1);
5834 sprintf (&spec[specend], "%c%u%s",
5835 attr_access::mode_chars[access_deferred],
5836 argpos, s);
5837 /* Trim the trailing NUL. */
5838 spec.resize (specend + len);
5839 }
5840
5841 /* The (optional) list of expressions denoting the VLA bounds
5842 N in ARGTYPE <arg>[Ni]...[Nj]...[Nk]. */
5843 tree argvbs = TREE_CHAIN (argspec);
5844 if (argvbs)
5845 {
5846 spec += ',';
5847 /* Add ARGVBS to the list. Their presence is indicated by
5848 appending a comma followed by the dollar sign and, when
5849 it corresponds to a function parameter, the position of
5850 each bound Ni, so it can be distinguished from
5851 an unspecified bound (as in T[*]). The list is in reverse
5852 order of arguments and needs to be reversed to access in
5853 order. */
5854 vblist = tree_cons (NULL_TREE, argvbs, vblist);
5855
5856 unsigned nelts = 0;
5857 for (tree vb = argvbs; vb; vb = TREE_CHAIN (vb), ++nelts)
5858 {
5859 tree bound = TREE_VALUE (vb);
5860 if (const unsigned *psizpos = arg2pos.get (bound))
5861 {
5862 /* BOUND previously seen in the parameter list. */
5863 TREE_PURPOSE (vb) = size_int (*psizpos);
5864 /* Format the position string in place. */
5865 int len = snprintf (NULL, 0, "$%u", *psizpos);
5866 size_t specend = spec.length ();
5867 spec.resize (specend + len + 1);
5868 sprintf (&spec[specend], "$%u", *psizpos);
5869 /* Trim the trailing NUL. */
5870 spec.resize (specend + len);
5871 }
5872 else
5873 {
5874 /* BOUND doesn't name a parameter (it could be a global
5875 variable or an expression such as a function call). */
5876 spec += '$';
5877 }
5878 }
5879 }
5880 }
5881
5882 if (!spec.length ())
5883 return NULL_TREE;
5884
5885 /* If we have nonnull arguments, synthesize an attribute. */
5886 if (nnlist != NULL_TREE)
5887 nnlist = build_tree_list (get_identifier ("nonnull"), nnlist);
5888
5889 /* Attribute access takes a two or three arguments. Wrap VBLIST in
5890 another list in case it has more nodes than would otherwise fit. */
5891 vblist = build_tree_list (NULL_TREE, vblist);
5892
5893 /* Build a single attribute access with the string describing all
5894 array arguments and an optional list of any non-parameter VLA
5895 bounds in order. */
5896 tree str = build_string (spec.length (), spec.c_str ());
5897 tree attrargs = tree_cons (NULL_TREE, str, vblist);
5898 tree name = get_identifier ("access");
5899 return tree_cons (name, attrargs, nnlist);
5900 }
5901
5902 /* Handle a "nothrow" attribute; arguments as in
5903 struct attribute_spec.handler. */
5904
5905 static tree
5906 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5907 int ARG_UNUSED (flags), bool *no_add_attrs)
5908 {
5909 if (TREE_CODE (*node) == FUNCTION_DECL)
5910 TREE_NOTHROW (*node) = 1;
5911 /* ??? TODO: Support types. */
5912 else
5913 {
5914 warning (OPT_Wattributes, "%qE attribute ignored", name);
5915 *no_add_attrs = true;
5916 }
5917
5918 return NULL_TREE;
5919 }
5920
5921 /* Handle a "nothrow" attribute; arguments as in
5922 struct attribute_spec.handler. */
5923
5924 static tree
5925 handle_expected_throw_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5926 int ARG_UNUSED (flags), bool *no_add_attrs)
5927 {
5928 if (TREE_CODE (*node) == FUNCTION_DECL)
5929 /* No flag to set here. */;
5930 /* ??? TODO: Support types. */
5931 else
5932 {
5933 warning (OPT_Wattributes, "%qE attribute ignored", name);
5934 *no_add_attrs = true;
5935 }
5936
5937 return NULL_TREE;
5938 }
5939
5940 /* Handle a "cleanup" attribute; arguments as in
5941 struct attribute_spec.handler. */
5942
5943 static tree
5944 handle_cleanup_attribute (tree *node, tree name, tree args,
5945 int ARG_UNUSED (flags), bool *no_add_attrs)
5946 {
5947 tree decl = *node;
5948 tree cleanup_id, cleanup_decl;
5949
5950 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
5951 for global destructors in C++. This requires infrastructure that
5952 we don't have generically at the moment. It's also not a feature
5953 we'd be missing too much, since we do have attribute constructor. */
5954 if (!VAR_P (decl) || TREE_STATIC (decl))
5955 {
5956 warning (OPT_Wattributes, "%qE attribute ignored", name);
5957 *no_add_attrs = true;
5958 return NULL_TREE;
5959 }
5960
5961 /* Verify that the argument is a function in scope. */
5962 /* ??? We could support pointers to functions here as well, if
5963 that was considered desirable. */
5964 cleanup_id = TREE_VALUE (args);
5965 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
5966 {
5967 error ("cleanup argument not an identifier");
5968 *no_add_attrs = true;
5969 return NULL_TREE;
5970 }
5971 cleanup_decl = lookup_name (cleanup_id);
5972 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
5973 {
5974 error ("cleanup argument not a function");
5975 *no_add_attrs = true;
5976 return NULL_TREE;
5977 }
5978
5979 /* That the function has proper type is checked with the
5980 eventual call to build_function_call. */
5981
5982 return NULL_TREE;
5983 }
5984
5985 /* Handle a "warn_unused_result" attribute. No special handling. */
5986
5987 static tree
5988 handle_warn_unused_result_attribute (tree *node, tree name,
5989 tree ARG_UNUSED (args),
5990 int ARG_UNUSED (flags), bool *no_add_attrs)
5991 {
5992 /* Ignore the attribute for functions not returning any value. */
5993 if (VOID_TYPE_P (TREE_TYPE (*node)))
5994 {
5995 warning (OPT_Wattributes, "%qE attribute ignored", name);
5996 *no_add_attrs = true;
5997 }
5998
5999 return NULL_TREE;
6000 }
6001
6002 /* Handle a "sentinel" attribute. */
6003
6004 static tree
6005 handle_sentinel_attribute (tree *node, tree name, tree args,
6006 int ARG_UNUSED (flags), bool *no_add_attrs)
6007 {
6008 if (!prototype_p (*node))
6009 {
6010 warning (OPT_Wattributes,
6011 "%qE attribute requires prototypes with named arguments", name);
6012 *no_add_attrs = true;
6013 }
6014 else
6015 {
6016 if (!stdarg_p (*node))
6017 {
6018 warning (OPT_Wattributes,
6019 "%qE attribute only applies to variadic functions", name);
6020 *no_add_attrs = true;
6021 }
6022 }
6023
6024 if (args)
6025 {
6026 tree position = TREE_VALUE (args);
6027 if (position && TREE_CODE (position) != IDENTIFIER_NODE
6028 && TREE_CODE (position) != FUNCTION_DECL)
6029 position = default_conversion (position);
6030
6031 if (TREE_CODE (position) != INTEGER_CST
6032 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
6033 {
6034 warning (OPT_Wattributes,
6035 "requested position is not an integer constant");
6036 *no_add_attrs = true;
6037 }
6038 else
6039 {
6040 if (tree_int_cst_lt (position, integer_zero_node))
6041 {
6042 warning (OPT_Wattributes,
6043 "requested position is less than zero");
6044 *no_add_attrs = true;
6045 }
6046 }
6047 }
6048
6049 return NULL_TREE;
6050 }
6051
6052 /* Handle a "type_generic" attribute. */
6053
6054 static tree
6055 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
6056 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
6057 bool * ARG_UNUSED (no_add_attrs))
6058 {
6059 /* Ensure we have a function type. */
6060 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
6061
6062 /* Ensure we have a variadic function. */
6063 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
6064
6065 return NULL_TREE;
6066 }
6067
6068 /* Handle a "target" attribute. */
6069
6070 static tree
6071 handle_target_attribute (tree *node, tree name, tree args, int flags,
6072 bool *no_add_attrs)
6073 {
6074 /* Ensure we have a function declaration. */
6075 if (TREE_CODE (*node) != FUNCTION_DECL)
6076 {
6077 warning (OPT_Wattributes, "%qE attribute ignored", name);
6078 *no_add_attrs = true;
6079 }
6080 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
6081 flags))
6082 *no_add_attrs = true;
6083
6084 /* Check that there's no empty string in values of the attribute. */
6085 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
6086 {
6087 tree value = TREE_VALUE (t);
6088 if (TREE_CODE (value) == STRING_CST
6089 && TREE_STRING_LENGTH (value) == 1
6090 && TREE_STRING_POINTER (value)[0] == '\0')
6091 {
6092 warning (OPT_Wattributes, "empty string in attribute %<target%>");
6093 *no_add_attrs = true;
6094 }
6095 }
6096
6097 return NULL_TREE;
6098 }
6099
6100 /* Handle a "target_version" attribute. */
6101
6102 static tree
6103 handle_target_version_attribute (tree *node, tree name, tree args, int flags,
6104 bool *no_add_attrs)
6105 {
6106 /* Ensure we have a function declaration. */
6107 if (TREE_CODE (*node) != FUNCTION_DECL)
6108 {
6109 warning (OPT_Wattributes, "%qE attribute ignored", name);
6110 *no_add_attrs = true;
6111 }
6112 else if (!targetm.target_option.valid_version_attribute_p (*node, name, args,
6113 flags))
6114 *no_add_attrs = true;
6115
6116 return NULL_TREE;
6117 }
6118
6119 /* Handle a "target_clones" attribute. */
6120
6121 static tree
6122 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6123 int ARG_UNUSED (flags), bool *no_add_attrs)
6124 {
6125 /* Ensure we have a function declaration. */
6126 if (TREE_CODE (*node) == FUNCTION_DECL)
6127 {
6128 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
6129 {
6130 tree value = TREE_VALUE (t);
6131 if (TREE_CODE (value) != STRING_CST)
6132 {
6133 error ("%qE attribute argument not a string constant", name);
6134 *no_add_attrs = true;
6135 return NULL_TREE;
6136 }
6137 }
6138
6139 if (get_target_clone_attr_len (args) == -1)
6140 {
6141 warning (OPT_Wattributes,
6142 "single %<target_clones%> attribute is ignored");
6143 *no_add_attrs = true;
6144 }
6145 else
6146 /* Do not inline functions with multiple clone targets. */
6147 DECL_UNINLINABLE (*node) = 1;
6148 }
6149 else
6150 {
6151 warning (OPT_Wattributes, "%qE attribute ignored", name);
6152 *no_add_attrs = true;
6153 }
6154 return NULL_TREE;
6155 }
6156
6157 /* For handling "optimize" attribute. arguments as in
6158 struct attribute_spec.handler. */
6159
6160 static tree
6161 handle_optimize_attribute (tree *node, tree name, tree args,
6162 int ARG_UNUSED (flags), bool *no_add_attrs)
6163 {
6164 /* Ensure we have a function type. */
6165 if (TREE_CODE (*node) != FUNCTION_DECL)
6166 {
6167 warning (OPT_Wattributes, "%qE attribute ignored", name);
6168 *no_add_attrs = true;
6169 }
6170 else
6171 {
6172 struct cl_optimization cur_opts;
6173 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
6174
6175 /* Save current options. */
6176 cl_optimization_save (&cur_opts, &global_options, &global_options_set);
6177 tree prev_target_node = build_target_option_node (&global_options,
6178 &global_options_set);
6179
6180 /* If we previously had some optimization options, use them as the
6181 default. */
6182 gcc_options *saved_global_options = NULL;
6183
6184 /* When #pragma GCC optimize pragma is used, it modifies global_options
6185 without calling targetm.override_options_after_change. That can leave
6186 target flags inconsistent for comparison. */
6187 if (flag_checking && optimization_current_node == optimization_default_node)
6188 {
6189 saved_global_options = XNEW (gcc_options);
6190 *saved_global_options = global_options;
6191 }
6192
6193 if (old_opts)
6194 cl_optimization_restore (&global_options, &global_options_set,
6195 TREE_OPTIMIZATION (old_opts));
6196
6197 /* Parse options, and update the vector. */
6198 parse_optimize_options (args, true);
6199 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
6200 = build_optimization_node (&global_options, &global_options_set);
6201 tree target_node = build_target_option_node (&global_options,
6202 &global_options_set);
6203 if (prev_target_node != target_node)
6204 DECL_FUNCTION_SPECIFIC_TARGET (*node) = target_node;
6205
6206 /* Also update the cgraph_node, if it's already built. */
6207 if (cgraph_node *cn = cgraph_node::get (*node))
6208 cn->semantic_interposition = flag_semantic_interposition;
6209
6210 /* Restore current options. */
6211 cl_optimization_restore (&global_options, &global_options_set,
6212 &cur_opts);
6213 cl_target_option_restore (&global_options, &global_options_set,
6214 TREE_TARGET_OPTION (prev_target_node));
6215
6216 if (saved_global_options != NULL)
6217 {
6218 if (!seen_error ())
6219 cl_optimization_compare (saved_global_options, &global_options);
6220 free (saved_global_options);
6221 }
6222 }
6223
6224 return NULL_TREE;
6225 }
6226
6227 /* Handle a "no_split_stack" attribute. */
6228
6229 static tree
6230 handle_no_split_stack_attribute (tree *node, tree name,
6231 tree ARG_UNUSED (args),
6232 int ARG_UNUSED (flags),
6233 bool *no_add_attrs)
6234 {
6235 tree decl = *node;
6236
6237 if (TREE_CODE (decl) != FUNCTION_DECL)
6238 {
6239 error_at (DECL_SOURCE_LOCATION (decl),
6240 "%qE attribute applies only to functions", name);
6241 *no_add_attrs = true;
6242 }
6243 else if (DECL_INITIAL (decl))
6244 {
6245 error_at (DECL_SOURCE_LOCATION (decl),
6246 "cannot set %qE attribute after definition", name);
6247 *no_add_attrs = true;
6248 }
6249
6250 return NULL_TREE;
6251 }
6252
6253 /* Handle a "zero_call_used_regs" attribute; arguments as in
6254 struct attribute_spec.handler. */
6255
6256 static tree
6257 handle_zero_call_used_regs_attribute (tree *node, tree name, tree args,
6258 int ARG_UNUSED (flags),
6259 bool *no_add_attrs)
6260 {
6261 tree decl = *node;
6262 tree id = TREE_VALUE (args);
6263
6264 if (TREE_CODE (decl) != FUNCTION_DECL)
6265 {
6266 error_at (DECL_SOURCE_LOCATION (decl),
6267 "%qE attribute applies only to functions", name);
6268 *no_add_attrs = true;
6269 return NULL_TREE;
6270 }
6271
6272 if (TREE_CODE (id) != STRING_CST)
6273 {
6274 error_at (DECL_SOURCE_LOCATION (decl),
6275 "%qE argument not a string", name);
6276 *no_add_attrs = true;
6277 return NULL_TREE;
6278 }
6279
6280 bool found = false;
6281 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
6282 if (strcmp (TREE_STRING_POINTER (id),
6283 zero_call_used_regs_opts[i].name) == 0)
6284 {
6285 found = true;
6286 break;
6287 }
6288
6289 if (!found)
6290 {
6291 error_at (DECL_SOURCE_LOCATION (decl),
6292 "unrecognized %qE attribute argument %qs",
6293 name, TREE_STRING_POINTER (id));
6294 *no_add_attrs = true;
6295 }
6296
6297 return NULL_TREE;
6298 }
6299
6300 /* Handle a "returns_nonnull" attribute; arguments as in
6301 struct attribute_spec.handler. */
6302
6303 static tree
6304 handle_returns_nonnull_attribute (tree *node, tree name, tree, int,
6305 bool *no_add_attrs)
6306 {
6307 // Even without a prototype we still have a return type we can check.
6308 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
6309 {
6310 error ("%qE attribute on a function not returning a pointer", name);
6311 *no_add_attrs = true;
6312 }
6313 return NULL_TREE;
6314 }
6315
6316 /* Handle a "designated_init" attribute; arguments as in
6317 struct attribute_spec.handler. */
6318
6319 static tree
6320 handle_designated_init_attribute (tree *node, tree name, tree, int,
6321 bool *no_add_attrs)
6322 {
6323 if (TREE_CODE (*node) != RECORD_TYPE)
6324 {
6325 error ("%qE attribute is only valid on %<struct%> type", name);
6326 *no_add_attrs = true;
6327 }
6328 return NULL_TREE;
6329 }
6330
6331
6332 /* Handle a "fallthrough" attribute; arguments as in struct
6333 attribute_spec.handler. */
6334
6335 tree
6336 handle_fallthrough_attribute (tree *, tree name, tree, int,
6337 bool *no_add_attrs)
6338 {
6339 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", name);
6340 *no_add_attrs = true;
6341 return NULL_TREE;
6342 }
6343
6344 /* Handle a "assume" attribute; arguments as in struct
6345 attribute_spec.handler. */
6346
6347 tree
6348 handle_assume_attribute (tree *, tree name, tree, int,
6349 bool *no_add_attrs)
6350 {
6351 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", name);
6352 *no_add_attrs = true;
6353 return NULL_TREE;
6354 }
6355
6356 /* Handle a "patchable_function_entry" attributes; arguments as in
6357 struct attribute_spec.handler. */
6358
6359 static tree
6360 handle_patchable_function_entry_attribute (tree *, tree name, tree args,
6361 int, bool *no_add_attrs)
6362 {
6363 for (; args; args = TREE_CHAIN (args))
6364 {
6365 tree val = TREE_VALUE (args);
6366 if (val && TREE_CODE (val) != IDENTIFIER_NODE
6367 && TREE_CODE (val) != FUNCTION_DECL)
6368 val = default_conversion (val);
6369
6370 if (!tree_fits_uhwi_p (val))
6371 {
6372 warning (OPT_Wattributes,
6373 "%qE attribute argument %qE is not an integer constant",
6374 name, val);
6375 *no_add_attrs = true;
6376 return NULL_TREE;
6377 }
6378
6379 if (tree_to_uhwi (val) > USHRT_MAX)
6380 {
6381 warning (OPT_Wattributes,
6382 "%qE attribute argument %qE exceeds %u",
6383 name, val, USHRT_MAX);
6384 *no_add_attrs = true;
6385 return NULL_TREE;
6386 }
6387 }
6388 return NULL_TREE;
6389 }
6390
6391 /* Handle a "NSObject" attributes; arguments as in
6392 struct attribute_spec.handler. */
6393
6394 static tree
6395 handle_nsobject_attribute (tree *node, tree name, tree args,
6396 int /*flags*/, bool *no_add_attrs)
6397 {
6398 *no_add_attrs = true;
6399
6400 /* This attribute only applies to typedefs (or field decls for properties),
6401 we drop it otherwise - but warn about this if enabled. */
6402 if (TREE_CODE (*node) != TYPE_DECL && TREE_CODE (*node) != FIELD_DECL)
6403 {
6404 warning (OPT_WNSObject_attribute, "%qE attribute may be put on a"
6405 " typedef only; attribute is ignored", name);
6406 return NULL_TREE;
6407 }
6408
6409 /* The original implementation only allowed pointers to records, however
6410 recent implementations also allow void *. */
6411 tree type = TREE_TYPE (*node);
6412 if (!type || !POINTER_TYPE_P (type)
6413 || (TREE_CODE (TREE_TYPE (type)) != RECORD_TYPE
6414 && !VOID_TYPE_P (TREE_TYPE (type))))
6415 {
6416 error ("%qE attribute is for pointer types only", name);
6417 return NULL_TREE;
6418 }
6419
6420 tree t = tree_cons (name, args, TYPE_ATTRIBUTES (type));
6421 TREE_TYPE (*node) = build_type_attribute_variant (type, t);
6422
6423 return NULL_TREE;
6424 }
6425
6426 /* Handle a "objc_root_class" attributes; arguments as in
6427 struct attribute_spec.handler. */
6428
6429 static tree
6430 handle_objc_root_class_attribute (tree */*node*/, tree name, tree /*args*/,
6431 int /*flags*/, bool *no_add_attrs)
6432 {
6433 /* This has no meaning outside Objective-C. */
6434 if (!c_dialect_objc())
6435 warning (OPT_Wattributes, "%qE is only applicable to Objective-C"
6436 " class interfaces, attribute ignored", name);
6437
6438 *no_add_attrs = true;
6439 return NULL_TREE;
6440 }
6441
6442 /* Handle an "objc_nullability" attribute; arguments as in
6443 struct attribute_spec.handler. */
6444
6445 static tree
6446 handle_objc_nullability_attribute (tree *node, tree name, tree args,
6447 int /*flags*/,
6448 bool *no_add_attrs)
6449 {
6450 *no_add_attrs = true;
6451
6452 tree type = TREE_TYPE (*node);
6453 if (TREE_CODE (*node) == FUNCTION_DECL)
6454 type = TREE_TYPE (type);
6455
6456 if (type && !POINTER_TYPE_P (type))
6457 {
6458 error ("%qE cannot be applied to non-pointer type %qT", name, type);
6459 return NULL_TREE;
6460 }
6461
6462 /* We accept objc_nullability() with a single argument.
6463 string: "unspecified", "nullable", "nonnull" or "resettable"
6464 integer: 0 and 3 where the values have the same meaning as
6465 the strings. */
6466 tree val = TREE_VALUE (args);
6467 if (TREE_CODE (val) == INTEGER_CST)
6468 {
6469 val = default_conversion (val);
6470 if (!tree_fits_uhwi_p (val) || tree_to_uhwi (val) > 3)
6471 error ("%qE attribute argument %qE is not an integer constant"
6472 " between 0 and 3", name, val);
6473 else
6474 *no_add_attrs = false; /* OK */
6475 }
6476 else if (TREE_CODE (val) == STRING_CST
6477 && (strcmp (TREE_STRING_POINTER (val), "nullable") == 0
6478 || strcmp (TREE_STRING_POINTER (val), "nonnull") == 0
6479 || strcmp (TREE_STRING_POINTER (val), "unspecified") == 0
6480 || strcmp (TREE_STRING_POINTER (val), "resettable") == 0))
6481 *no_add_attrs = false; /* OK */
6482 else if (val != error_mark_node)
6483 error ("%qE attribute argument %qE is not recognized", name, val);
6484
6485 return NULL_TREE;
6486 }
6487
6488 /* Handle a "tainted_args" attribute; arguments as in
6489 struct attribute_spec.handler. */
6490
6491 static tree
6492 handle_tainted_args_attribute (tree *node, tree name, tree, int,
6493 bool *no_add_attrs)
6494 {
6495 if (TREE_CODE (*node) != FUNCTION_DECL
6496 && TREE_CODE (*node) != FIELD_DECL)
6497 {
6498 warning (OPT_Wattributes, "%qE attribute ignored; valid only "
6499 "for functions and function pointer fields",
6500 name);
6501 *no_add_attrs = true;
6502 return NULL_TREE;
6503 }
6504
6505 if (TREE_CODE (*node) == FIELD_DECL
6506 && !(TREE_CODE (TREE_TYPE (*node)) == POINTER_TYPE
6507 && TREE_CODE (TREE_TYPE (TREE_TYPE (*node))) == FUNCTION_TYPE))
6508 {
6509 warning (OPT_Wattributes, "%qE attribute ignored;"
6510 " field must be a function pointer",
6511 name);
6512 *no_add_attrs = true;
6513 return NULL_TREE;
6514 }
6515
6516 *no_add_attrs = false; /* OK */
6517
6518 return NULL_TREE;
6519 }
6520
6521 /* Attempt to partially validate a single attribute ATTR as if
6522 it were to be applied to an entity OPER. */
6523
6524 static bool
6525 validate_attribute (location_t atloc, tree oper, tree attr)
6526 {
6527 /* Determine whether the name of the attribute is valid
6528 and fail with an error if not. */
6529 tree atname = get_attribute_name (attr);
6530 if (!lookup_attribute_spec (atname))
6531 {
6532 if (atloc != UNKNOWN_LOCATION)
6533 error_at (atloc, "unknown attribute %qE", atname);
6534 return false;
6535 }
6536
6537 tree args = TREE_VALUE (attr);
6538 if (!args)
6539 return true;
6540
6541 /* FIXME: Do some validation. */
6542 const char *atstr = IDENTIFIER_POINTER (atname);
6543 if (!strcmp (atstr, "format"))
6544 return true;
6545
6546 /* Only when attribute arguments have been provided try to validate
6547 the whole thing. decl_attributes doesn't return an indication of
6548 success or failure so proceed regardless. */
6549 const char tmpname[] = "__builtin_has_attribute_tmp.";
6550 tree tmpid = get_identifier (tmpname);
6551 tree tmpdecl;
6552 if (!strcmp (atstr, "vector_size"))
6553 {
6554 tree type = TYPE_P (oper) ? oper : TREE_TYPE (oper);
6555 /* Check for function type here since type_for_vector_size
6556 strips it while looking for a function's return type. */
6557 if (FUNC_OR_METHOD_TYPE_P (type))
6558 {
6559 warning_at (atloc, OPT_Wattributes,
6560 "invalid operand type %qT for %qs", type, atstr);
6561 return false;
6562 }
6563
6564 type = type_for_vector_size (type);
6565 if (VECTOR_TYPE_P (type))
6566 type = TREE_TYPE (type);
6567 /* Avoid trying to apply attribute vector_size to OPER since
6568 it's overly restrictive. Simply make sure it has the right
6569 type. */
6570 return type_valid_for_vector_size (type, atname, args, NULL);
6571 }
6572
6573 if (TYPE_P (oper))
6574 tmpdecl = build_decl (atloc, TYPE_DECL, tmpid, oper);
6575 else if (DECL_P (oper))
6576 tmpdecl = build_decl (atloc, TREE_CODE (oper), tmpid, TREE_TYPE (oper));
6577 else if (EXPR_P (oper))
6578 tmpdecl = build_decl (atloc, TYPE_DECL, tmpid, TREE_TYPE (oper));
6579 else
6580 return false;
6581
6582 /* Temporarily clear CURRENT_FUNCTION_DECL to make decl_attributes
6583 believe the DECL declared above is at file scope. (See bug 87526.) */
6584 tree save_curfunc = current_function_decl;
6585 current_function_decl = NULL_TREE;
6586 if (DECL_P (tmpdecl))
6587 {
6588 if (DECL_P (oper))
6589 /* An alias cannot be a definition so declare the symbol extern. */
6590 DECL_EXTERNAL (tmpdecl) = true;
6591 /* Attribute visibility only applies to symbols visible from other
6592 translation units so make it "public." */
6593 TREE_PUBLIC (tmpdecl) = TREE_PUBLIC (oper);
6594 }
6595 decl_attributes (&tmpdecl, attr, 0);
6596 current_function_decl = save_curfunc;
6597
6598 /* FIXME: Change decl_attributes to indicate success or failure (and
6599 parameterize it to avoid failing with errors). */
6600 return true;
6601 }
6602
6603 /* Return true if the DECL, EXPR, or TYPE t has been declared with
6604 attribute ATTR. For DECL, consider also its type. For EXPR,
6605 consider just its type. */
6606
6607 bool
6608 has_attribute (location_t atloc, tree t, tree attr, tree (*convert)(tree))
6609 {
6610 if (!attr || !t || t == error_mark_node)
6611 return false;
6612
6613 if (!validate_attribute (atloc, t, attr))
6614 return false;
6615
6616 tree type = NULL_TREE;
6617 tree expr = NULL_TREE;
6618 if (TYPE_P (t))
6619 type = t;
6620 else
6621 {
6622 do
6623 {
6624 /* Determine the array element/member declaration from
6625 a COMPONENT_REF and an INDIRECT_REF involving a refeence. */
6626 STRIP_NOPS (t);
6627 tree_code code = TREE_CODE (t);
6628 if (code == INDIRECT_REF)
6629 {
6630 tree op0 = TREE_OPERAND (t, 0);
6631 if (TREE_CODE (TREE_TYPE (op0)) == REFERENCE_TYPE)
6632 t = op0;
6633 else
6634 break;
6635 }
6636 else if (code == COMPONENT_REF)
6637 t = TREE_OPERAND (t, 1);
6638 else
6639 break;
6640 } while (true);
6641 expr = t;
6642 }
6643
6644 /* Set to true when an attribute is found in the referenced entity
6645 that matches the specified attribute. */
6646 bool found_match = false;
6647
6648 tree atname = get_attribute_name (attr);
6649 const char *namestr = IDENTIFIER_POINTER (atname);
6650
6651 /* Iterate once for a type and twice for a function or variable
6652 declaration: once for the DECL and the second time for its
6653 TYPE. */
6654 for (bool done = false; !found_match && !done; )
6655 {
6656 tree atlist;
6657 if (type)
6658 {
6659 if (type == error_mark_node)
6660 {
6661 /* This could be a label. FIXME: add support for labels. */
6662 warning_at (atloc, OPT_Wattributes,
6663 (TYPE_P (t)
6664 ? G_("%qs attribute not supported for %qT "
6665 "in %<__builtin_has_attribute%>")
6666 : G_("%qs attribute not supported for %qE "
6667 "in %<__builtin_has_attribute%>")),
6668 namestr, t);
6669 return false;
6670 }
6671
6672 /* Clear EXPR to prevent considering it again below. */
6673 atlist = TYPE_ATTRIBUTES (type);
6674 expr = NULL_TREE;
6675 done = true;
6676 }
6677 else if (DECL_P (expr))
6678 {
6679 /* Set TYPE to the DECL's type to process it on the next
6680 iteration. */
6681 atlist = DECL_ATTRIBUTES (expr);
6682 type = TREE_TYPE (expr);
6683 }
6684 else
6685 {
6686 type = TREE_TYPE (expr);
6687 atlist = TYPE_ATTRIBUTES (type);
6688 done = true;
6689 }
6690
6691 /* True when an attribute with the sought name (though not necessarily
6692 with the sought attributes) has been found on the attribute chain. */
6693 bool found_attr = false;
6694
6695 /* When clear, the first mismatched attribute argument results
6696 in failure. Otherwise, the first matched attribute argument
6697 results in success. */
6698 bool attr_nonnull = !strcmp ("nonnull", namestr);
6699 bool ignore_mismatches = attr_nonnull;
6700
6701 /* Iterate over the instances of the sought attribute on the DECL or
6702 TYPE (there may be multiple instances with different arguments). */
6703 for (; (atlist = lookup_attribute (namestr, atlist));
6704 found_attr = true, atlist = TREE_CHAIN (atlist))
6705 {
6706 /* If there are no arguments to match the result is true except
6707 for nonnull where the attribute with no arguments must match. */
6708 if (!TREE_VALUE (attr))
6709 return attr_nonnull ? !TREE_VALUE (atlist) : true;
6710
6711 /* Attribute nonnull with no arguments subsumes all values of
6712 the attribute. FIXME: This is overly broad since it only
6713 applies to pointer arguments, but querying non-pointer
6714 arguments is diagnosed. */
6715 if (!TREE_VALUE (atlist) && attr_nonnull)
6716 return true;
6717
6718 /* Iterate over the DECL or TYPE attribute argument's values. */
6719 for (tree val = TREE_VALUE (atlist); val; val = TREE_CHAIN (val))
6720 {
6721 /* Iterate over the arguments in the sought attribute comparing
6722 their values to those specified for the DECL or TYPE. */
6723 for (tree arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
6724 {
6725 tree v1 = TREE_VALUE (val);
6726 tree v2 = TREE_VALUE (arg);
6727 if (v1 == v2)
6728 return true;
6729
6730 if (!v1 || !v2)
6731 break;
6732
6733 if (TREE_CODE (v1) == IDENTIFIER_NODE
6734 || TREE_CODE (v2) == IDENTIFIER_NODE)
6735 /* Two identifiers are the same if their values are
6736 equal (that's handled above). Otherwise ther are
6737 either not the same or oneis not an identifier. */
6738 return false;
6739
6740 /* Convert to make them equality-comparable. */
6741 v1 = convert (v1);
6742 v2 = convert (v2);
6743
6744 /* A positive value indicates equality, negative means
6745 "don't know." */
6746 if (simple_cst_equal (v1, v2) == 1)
6747 return true;
6748
6749 if (!ignore_mismatches)
6750 break;
6751 }
6752 }
6753 }
6754
6755 if (!found_attr)
6756 {
6757 /* Some attributes are encoded directly in the tree node. */
6758 if (!strcmp ("aligned", namestr))
6759 {
6760 if (tree arg = TREE_VALUE (attr))
6761 {
6762 arg = convert (TREE_VALUE (arg));
6763 if (!tree_fits_uhwi_p (arg))
6764 /* Invalid argument. */;
6765 else if (expr && DECL_P (expr)
6766 && DECL_USER_ALIGN (expr))
6767 found_match = DECL_ALIGN_UNIT (expr) == tree_to_uhwi (arg);
6768 else if (type && TYPE_USER_ALIGN (type))
6769 found_match = TYPE_ALIGN_UNIT (type) == tree_to_uhwi (arg);
6770 }
6771 else if (expr && DECL_P (expr))
6772 found_match = DECL_USER_ALIGN (expr);
6773 else if (type)
6774 found_match = TYPE_USER_ALIGN (type);
6775 }
6776 else if (!strcmp ("const", namestr))
6777 {
6778 if (expr && DECL_P (expr))
6779 found_match = TREE_READONLY (expr);
6780 }
6781 else if (!strcmp ("noreturn", namestr))
6782 {
6783 /* C11 _Noreturn sets the volatile bit without attaching
6784 an attribute to the decl. */
6785 if (expr
6786 && DECL_P (expr)
6787 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr)))
6788 found_match = TREE_THIS_VOLATILE (expr);
6789 }
6790 else if (!strcmp ("pure", namestr))
6791 {
6792 if (expr && DECL_P (expr))
6793 found_match = DECL_PURE_P (expr);
6794 }
6795 else if (!strcmp ("deprecated", namestr))
6796 {
6797 found_match = TREE_DEPRECATED (expr ? expr : type);
6798 if (found_match)
6799 return true;
6800 }
6801 else if (!strcmp ("vector_size", namestr))
6802 {
6803 if (!type || !VECTOR_TYPE_P (type))
6804 return false;
6805
6806 if (tree arg = TREE_VALUE (attr))
6807 {
6808 /* Compare the vector size argument for equality. */
6809 arg = convert (TREE_VALUE (arg));
6810 return tree_int_cst_equal (arg, TYPE_SIZE_UNIT (type)) == 1;
6811 }
6812 else
6813 return true;
6814 }
6815 else if (!strcmp ("warn_if_not_aligned", namestr))
6816 {
6817 if (tree arg = TREE_VALUE (attr))
6818 {
6819 arg = convert (TREE_VALUE (arg));
6820 if (expr && DECL_P (expr))
6821 found_match = (DECL_WARN_IF_NOT_ALIGN (expr)
6822 == tree_to_uhwi (arg) * BITS_PER_UNIT);
6823 else if (type)
6824 found_match = (TYPE_WARN_IF_NOT_ALIGN (type)
6825 == tree_to_uhwi (arg) * BITS_PER_UNIT);
6826 }
6827 else if (expr && DECL_P (expr))
6828 found_match = DECL_WARN_IF_NOT_ALIGN (expr);
6829 else if (type)
6830 found_match = TYPE_WARN_IF_NOT_ALIGN (type);
6831 }
6832 else if (!strcmp ("transparent_union", namestr))
6833 {
6834 if (type)
6835 found_match = TYPE_TRANSPARENT_AGGR (type) != 0;
6836 }
6837 else if (!strcmp ("mode", namestr))
6838 {
6839 /* Finally issue a warning for attributes that cannot
6840 be supported in this context. Attribute mode is not
6841 added to a symbol and cannot be determined from it. */
6842 warning_at (atloc, OPT_Wattributes,
6843 "%qs attribute not supported in "
6844 "%<__builtin_has_attribute%>", namestr);
6845 break;
6846 }
6847 }
6848 }
6849 return found_match;
6850 }