]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-family/c-attribs.c
PR c/88546 - Copy attribute unusable for weakrefs
[thirdparty/gcc.git] / gcc / c-family / c-attribs.c
1 /* C-family attributes handling.
2 Copyright (C) 1992-2019 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "tree.h"
26 #include "memmodel.h"
27 #include "c-common.h"
28 #include "gimple-expr.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "intl.h"
34 #include "stor-layout.h"
35 #include "calls.h"
36 #include "attribs.h"
37 #include "varasm.h"
38 #include "trans-mem.h"
39 #include "c-objc.h"
40 #include "common/common-target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
43 #include "toplev.h"
44 #include "tree-iterator.h"
45 #include "opts.h"
46 #include "gimplify.h"
47 #include "tree-pretty-print.h"
48
49 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
57 int, bool *);
58 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
59 int, bool *);
60 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
61 int, bool *);
62 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
63 bool *);
64 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
65 bool *);
66 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
73 static tree handle_always_inline_attribute (tree *, tree, tree, int,
74 bool *);
75 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
80 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
81 bool *);
82 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
83 bool *);
84 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_transparent_union_attribute (tree *, tree, tree,
86 int, bool *);
87 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
88 int, bool *);
89 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
94 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
95 int, bool *);
96 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
97 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
98 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
99 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
101 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
102 static tree handle_visibility_attribute (tree *, tree, tree, int,
103 bool *);
104 static tree handle_tls_model_attribute (tree *, tree, tree, int,
105 bool *);
106 static tree handle_no_instrument_function_attribute (tree *, tree,
107 tree, int, bool *);
108 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
109 tree, int, bool *);
110 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
111 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
112 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
113 bool *);
114 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
118 static tree handle_deprecated_attribute (tree *, tree, tree, int,
119 bool *);
120 static tree handle_vector_size_attribute (tree *, tree, tree, int,
121 bool *);
122 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
126 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
127 bool *);
128 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
135 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
136 static tree ignore_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
140 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
141 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
142 bool *);
143 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
144 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
145 bool *);
146 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
147 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
148 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
149 int, bool *);
150 static tree handle_copy_attribute (tree *, tree, tree, int, bool *);
151
152 /* Helper to define attribute exclusions. */
153 #define ATTR_EXCL(name, function, type, variable) \
154 { name, function, type, variable }
155
156 /* Define attributes that are mutually exclusive with one another. */
157 static const struct attribute_spec::exclusions attr_aligned_exclusions[] =
158 {
159 /* Attribute name exclusion applies to:
160 function, type, variable */
161 ATTR_EXCL ("aligned", true, false, false),
162 ATTR_EXCL ("packed", true, false, false),
163 ATTR_EXCL (NULL, false, false, false)
164 };
165
166 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
167 {
168 ATTR_EXCL ("cold", true, true, true),
169 ATTR_EXCL ("hot", true, true, true),
170 ATTR_EXCL (NULL, false, false, false)
171 };
172
173 static const struct attribute_spec::exclusions attr_common_exclusions[] =
174 {
175 ATTR_EXCL ("common", true, true, true),
176 ATTR_EXCL ("nocommon", true, true, true),
177 ATTR_EXCL (NULL, false, false, false),
178 };
179
180 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
181 {
182 ATTR_EXCL ("noinline", true, true, true),
183 ATTR_EXCL (NULL, false, false, false),
184 };
185
186 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
187 {
188 ATTR_EXCL ("always_inline", true, true, true),
189 ATTR_EXCL ("gnu_inline", true, true, true),
190 ATTR_EXCL (NULL, false, false, false),
191 };
192
193 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
194 {
195 ATTR_EXCL ("alloc_align", true, true, true),
196 ATTR_EXCL ("alloc_size", true, true, true),
197 ATTR_EXCL ("const", true, true, true),
198 ATTR_EXCL ("malloc", true, true, true),
199 ATTR_EXCL ("pure", true, true, true),
200 ATTR_EXCL ("returns_twice", true, true, true),
201 ATTR_EXCL ("warn_unused_result", true, true, true),
202 ATTR_EXCL (NULL, false, false, false),
203 };
204
205 static const struct attribute_spec::exclusions
206 attr_warn_unused_result_exclusions[] =
207 {
208 ATTR_EXCL ("noreturn", true, true, true),
209 ATTR_EXCL ("warn_unused_result", true, true, true),
210 ATTR_EXCL (NULL, false, false, false),
211 };
212
213 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
214 {
215 ATTR_EXCL ("noreturn", true, true, true),
216 ATTR_EXCL (NULL, false, false, false),
217 };
218
219 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
220 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
221 {
222 ATTR_EXCL ("const", true, true, true),
223 ATTR_EXCL ("noreturn", true, true, true),
224 ATTR_EXCL ("pure", true, true, true),
225 ATTR_EXCL (NULL, false, false, false),
226 };
227
228 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
229 {
230 ATTR_EXCL ("const", true, true, true),
231 ATTR_EXCL ("alloc_align", true, true, true),
232 ATTR_EXCL ("alloc_size", true, true, true),
233 ATTR_EXCL ("malloc", true, true, true),
234 ATTR_EXCL ("noreturn", true, true, true),
235 ATTR_EXCL ("pure", true, true, true),
236 ATTR_EXCL (NULL, false, false, false)
237 };
238
239 /* Table of machine-independent attributes common to all C-like languages.
240
241 Current list of processed common attributes: nonnull. */
242 const struct attribute_spec c_common_attribute_table[] =
243 {
244 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
245 affects_type_identity, handler, exclude } */
246 { "packed", 0, 0, false, false, false, false,
247 handle_packed_attribute,
248 attr_aligned_exclusions },
249 { "nocommon", 0, 0, true, false, false, false,
250 handle_nocommon_attribute,
251 attr_common_exclusions },
252 { "common", 0, 0, true, false, false, false,
253 handle_common_attribute,
254 attr_common_exclusions },
255 /* FIXME: logically, noreturn attributes should be listed as
256 "false, true, true" and apply to function types. But implementing this
257 would require all the places in the compiler that use TREE_THIS_VOLATILE
258 on a decl to identify non-returning functions to be located and fixed
259 to check the function type instead. */
260 { "noreturn", 0, 0, true, false, false, false,
261 handle_noreturn_attribute,
262 attr_noreturn_exclusions },
263 { "volatile", 0, 0, true, false, false, false,
264 handle_noreturn_attribute, NULL },
265 { "stack_protect", 0, 0, true, false, false, false,
266 handle_stack_protect_attribute, NULL },
267 { "noinline", 0, 0, true, false, false, false,
268 handle_noinline_attribute,
269 attr_noinline_exclusions },
270 { "noclone", 0, 0, true, false, false, false,
271 handle_noclone_attribute, NULL },
272 { "no_icf", 0, 0, true, false, false, false,
273 handle_noicf_attribute, NULL },
274 { "noipa", 0, 0, true, false, false, false,
275 handle_noipa_attribute, NULL },
276 { "leaf", 0, 0, true, false, false, false,
277 handle_leaf_attribute, NULL },
278 { "always_inline", 0, 0, true, false, false, false,
279 handle_always_inline_attribute,
280 attr_inline_exclusions },
281 { "gnu_inline", 0, 0, true, false, false, false,
282 handle_gnu_inline_attribute,
283 attr_inline_exclusions },
284 { "artificial", 0, 0, true, false, false, false,
285 handle_artificial_attribute, NULL },
286 { "flatten", 0, 0, true, false, false, false,
287 handle_flatten_attribute, NULL },
288 { "used", 0, 0, true, false, false, false,
289 handle_used_attribute, NULL },
290 { "unused", 0, 0, false, false, false, false,
291 handle_unused_attribute, NULL },
292 { "externally_visible", 0, 0, true, false, false, false,
293 handle_externally_visible_attribute, NULL },
294 { "no_reorder", 0, 0, true, false, false, false,
295 handle_no_reorder_attribute, NULL },
296 /* The same comments as for noreturn attributes apply to const ones. */
297 { "const", 0, 0, true, false, false, false,
298 handle_const_attribute,
299 attr_const_pure_exclusions },
300 { "scalar_storage_order", 1, 1, false, false, false, false,
301 handle_scalar_storage_order_attribute, NULL },
302 { "transparent_union", 0, 0, false, false, false, false,
303 handle_transparent_union_attribute, NULL },
304 { "constructor", 0, 1, true, false, false, false,
305 handle_constructor_attribute, NULL },
306 { "destructor", 0, 1, true, false, false, false,
307 handle_destructor_attribute, NULL },
308 { "mode", 1, 1, false, true, false, false,
309 handle_mode_attribute, NULL },
310 { "section", 1, 1, true, false, false, false,
311 handle_section_attribute, NULL },
312 { "aligned", 0, 1, false, false, false, false,
313 handle_aligned_attribute,
314 attr_aligned_exclusions },
315 { "warn_if_not_aligned", 0, 1, false, false, false, false,
316 handle_warn_if_not_aligned_attribute, NULL },
317 { "weak", 0, 0, true, false, false, false,
318 handle_weak_attribute, NULL },
319 { "noplt", 0, 0, true, false, false, false,
320 handle_noplt_attribute, NULL },
321 { "ifunc", 1, 1, true, false, false, false,
322 handle_ifunc_attribute, NULL },
323 { "alias", 1, 1, true, false, false, false,
324 handle_alias_attribute, NULL },
325 { "weakref", 0, 1, true, false, false, false,
326 handle_weakref_attribute, NULL },
327 { "no_instrument_function", 0, 0, true, false, false, false,
328 handle_no_instrument_function_attribute,
329 NULL },
330 { "no_profile_instrument_function", 0, 0, true, false, false, false,
331 handle_no_profile_instrument_function_attribute,
332 NULL },
333 { "malloc", 0, 0, true, false, false, false,
334 handle_malloc_attribute, attr_alloc_exclusions },
335 { "returns_twice", 0, 0, true, false, false, false,
336 handle_returns_twice_attribute,
337 attr_returns_twice_exclusions },
338 { "no_stack_limit", 0, 0, true, false, false, false,
339 handle_no_limit_stack_attribute, NULL },
340 { "pure", 0, 0, true, false, false, false,
341 handle_pure_attribute,
342 attr_const_pure_exclusions },
343 { "transaction_callable", 0, 0, false, true, false, false,
344 handle_tm_attribute, NULL },
345 { "transaction_unsafe", 0, 0, false, true, false, true,
346 handle_tm_attribute, NULL },
347 { "transaction_safe", 0, 0, false, true, false, true,
348 handle_tm_attribute, NULL },
349 { "transaction_safe_dynamic", 0, 0, true, false, false, false,
350 handle_tm_attribute, NULL },
351 { "transaction_may_cancel_outer", 0, 0, false, true, false, false,
352 handle_tm_attribute, NULL },
353 /* ??? These two attributes didn't make the transition from the
354 Intel language document to the multi-vendor language document. */
355 { "transaction_pure", 0, 0, false, true, false, false,
356 handle_tm_attribute, NULL },
357 { "transaction_wrap", 1, 1, true, false, false, false,
358 handle_tm_wrap_attribute, NULL },
359 /* For internal use (marking of builtins) only. The name contains space
360 to prevent its usage in source code. */
361 { "no vops", 0, 0, true, false, false, false,
362 handle_novops_attribute, NULL },
363 { "deprecated", 0, 1, false, false, false, false,
364 handle_deprecated_attribute, NULL },
365 { "vector_size", 1, 1, false, true, false, true,
366 handle_vector_size_attribute, NULL },
367 { "visibility", 1, 1, false, false, false, false,
368 handle_visibility_attribute, NULL },
369 { "tls_model", 1, 1, true, false, false, false,
370 handle_tls_model_attribute, NULL },
371 { "nonnull", 0, -1, false, true, true, false,
372 handle_nonnull_attribute, NULL },
373 { "nonstring", 0, 0, true, false, false, false,
374 handle_nonstring_attribute, NULL },
375 { "nothrow", 0, 0, true, false, false, false,
376 handle_nothrow_attribute, NULL },
377 { "may_alias", 0, 0, false, true, false, false, NULL, NULL },
378 { "cleanup", 1, 1, true, false, false, false,
379 handle_cleanup_attribute, NULL },
380 { "warn_unused_result", 0, 0, false, true, true, false,
381 handle_warn_unused_result_attribute,
382 attr_warn_unused_result_exclusions },
383 { "sentinel", 0, 1, false, true, true, false,
384 handle_sentinel_attribute, NULL },
385 /* For internal use (marking of builtins) only. The name contains space
386 to prevent its usage in source code. */
387 { "type generic", 0, 0, false, true, true, false,
388 handle_type_generic_attribute, NULL },
389 { "alloc_size", 1, 2, false, true, true, false,
390 handle_alloc_size_attribute,
391 attr_alloc_exclusions },
392 { "cold", 0, 0, true, false, false, false,
393 handle_cold_attribute,
394 attr_cold_hot_exclusions },
395 { "hot", 0, 0, true, false, false, false,
396 handle_hot_attribute,
397 attr_cold_hot_exclusions },
398 { "no_address_safety_analysis",
399 0, 0, true, false, false, false,
400 handle_no_address_safety_analysis_attribute,
401 NULL },
402 { "no_sanitize", 1, -1, true, false, false, false,
403 handle_no_sanitize_attribute, NULL },
404 { "no_sanitize_address", 0, 0, true, false, false, false,
405 handle_no_sanitize_address_attribute, NULL },
406 { "no_sanitize_thread", 0, 0, true, false, false, false,
407 handle_no_sanitize_thread_attribute, NULL },
408 { "no_sanitize_undefined", 0, 0, true, false, false, false,
409 handle_no_sanitize_undefined_attribute, NULL },
410 { "asan odr indicator", 0, 0, true, false, false, false,
411 handle_asan_odr_indicator_attribute, NULL },
412 { "warning", 1, 1, true, false, false, false,
413 handle_error_attribute, NULL },
414 { "error", 1, 1, true, false, false, false,
415 handle_error_attribute, NULL },
416 { "target", 1, -1, true, false, false, false,
417 handle_target_attribute, NULL },
418 { "target_clones", 1, -1, true, false, false, false,
419 handle_target_clones_attribute, NULL },
420 { "optimize", 1, -1, true, false, false, false,
421 handle_optimize_attribute, NULL },
422 /* For internal use only. The leading '*' both prevents its usage in
423 source code and signals that it may be overridden by machine tables. */
424 { "*tm regparm", 0, 0, false, true, true, false,
425 ignore_attribute, NULL },
426 { "no_split_stack", 0, 0, true, false, false, false,
427 handle_no_split_stack_attribute, NULL },
428 /* For internal use (marking of builtins and runtime functions) only.
429 The name contains space to prevent its usage in source code. */
430 { "fn spec", 1, 1, false, true, true, false,
431 handle_fnspec_attribute, NULL },
432 { "warn_unused", 0, 0, false, false, false, false,
433 handle_warn_unused_attribute, NULL },
434 { "returns_nonnull", 0, 0, false, true, true, false,
435 handle_returns_nonnull_attribute, NULL },
436 { "omp declare simd", 0, -1, true, false, false, false,
437 handle_omp_declare_simd_attribute, NULL },
438 { "simd", 0, 1, true, false, false, false,
439 handle_simd_attribute, NULL },
440 { "omp declare target", 0, 0, true, false, false, false,
441 handle_omp_declare_target_attribute, NULL },
442 { "omp declare target link", 0, 0, true, false, false, false,
443 handle_omp_declare_target_attribute, NULL },
444 { "omp declare target implicit", 0, 0, true, false, false, false,
445 handle_omp_declare_target_attribute, NULL },
446 { "alloc_align", 1, 1, false, true, true, false,
447 handle_alloc_align_attribute,
448 attr_alloc_exclusions },
449 { "assume_aligned", 1, 2, false, true, true, false,
450 handle_assume_aligned_attribute, NULL },
451 { "designated_init", 0, 0, false, true, false, false,
452 handle_designated_init_attribute, NULL },
453 { "fallthrough", 0, 0, false, false, false, false,
454 handle_fallthrough_attribute, NULL },
455 { "patchable_function_entry", 1, 2, true, false, false, false,
456 handle_patchable_function_entry_attribute,
457 NULL },
458 { "nocf_check", 0, 0, false, true, true, true,
459 handle_nocf_check_attribute, NULL },
460 { "copy", 1, 1, false, false, false, false,
461 handle_copy_attribute, NULL },
462 { NULL, 0, 0, false, false, false, false, NULL, NULL }
463 };
464
465 /* Give the specifications for the format attributes, used by C and all
466 descendants.
467
468 Current list of processed format attributes: format, format_arg. */
469 const struct attribute_spec c_common_format_attribute_table[] =
470 {
471 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
472 affects_type_identity, handler, exclude } */
473 { "format", 3, 3, false, true, true, false,
474 handle_format_attribute, NULL },
475 { "format_arg", 1, 1, false, true, true, false,
476 handle_format_arg_attribute, NULL },
477 { NULL, 0, 0, false, false, false, false, NULL, NULL }
478 };
479
480 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
481 identifier as an argument, so the front end shouldn't look it up. */
482
483 bool
484 attribute_takes_identifier_p (const_tree attr_id)
485 {
486 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
487 if (spec == NULL)
488 /* Unknown attribute that we'll end up ignoring, return true so we
489 don't complain about an identifier argument. */
490 return true;
491 else if (!strcmp ("mode", spec->name)
492 || !strcmp ("format", spec->name)
493 || !strcmp ("cleanup", spec->name))
494 return true;
495 else
496 return targetm.attribute_takes_identifier_p (attr_id);
497 }
498
499 /* Verify that argument value POS at position ARGNO to attribute NAME
500 applied to function TYPE refers to a function parameter at position
501 POS and the expected type CODE. Treat CODE == INTEGER_TYPE as
502 matching all C integral types except bool. If successful, return
503 POS after default conversions, if any. Otherwise, issue appropriate
504 warnings and return null. A non-zero 1-based ARGNO should be passed
505 in by callers only for attributes with more than one argument. */
506
507 tree
508 positional_argument (const_tree fntype, const_tree atname, tree pos,
509 tree_code code, int argno /* = 0 */,
510 int flags /* = posargflags () */)
511 {
512 if (pos && TREE_CODE (pos) != IDENTIFIER_NODE
513 && TREE_CODE (pos) != FUNCTION_DECL)
514 pos = default_conversion (pos);
515
516 tree postype = TREE_TYPE (pos);
517 if (pos == error_mark_node || !postype)
518 {
519 /* Only mention the positional argument number when it's non-zero. */
520 if (argno < 1)
521 warning (OPT_Wattributes,
522 "%qE attribute argument is invalid", atname);
523 else
524 warning (OPT_Wattributes,
525 "%qE attribute argument %i is invalid", atname, argno);
526
527 return NULL_TREE;
528 }
529
530 if (!INTEGRAL_TYPE_P (postype))
531 {
532 /* Handle this case specially to avoid mentioning the value
533 of pointer constants in diagnostics. Only mention
534 the positional argument number when it's non-zero. */
535 if (argno < 1)
536 warning (OPT_Wattributes,
537 "%qE attribute argument has type %qT",
538 atname, postype);
539 else
540 warning (OPT_Wattributes,
541 "%qE attribute argument %i has type %qT",
542 atname, argno, postype);
543
544 return NULL_TREE;
545 }
546
547 if (TREE_CODE (pos) != INTEGER_CST)
548 {
549 /* Only mention the argument number when it's non-zero. */
550 if (argno < 1)
551 warning (OPT_Wattributes,
552 "%qE attribute argument value %qE is not an integer "
553 "constant",
554 atname, pos);
555 else
556 warning (OPT_Wattributes,
557 "%qE attribute argument %i value %qE is not an integer "
558 "constant",
559 atname, argno, pos);
560
561 return NULL_TREE;
562 }
563
564 /* Argument positions are 1-based. */
565 if (integer_zerop (pos))
566 {
567 if (flags & POSARG_ZERO)
568 /* Zero is explicitly allowed. */
569 return pos;
570
571 if (argno < 1)
572 warning (OPT_Wattributes,
573 "%qE attribute argument value %qE does not refer to "
574 "a function parameter",
575 atname, pos);
576 else
577 warning (OPT_Wattributes,
578 "%qE attribute argument %i value %qE does not refer to "
579 "a function parameter",
580 atname, argno, pos);
581
582 return NULL_TREE;
583 }
584
585 if (!prototype_p (fntype))
586 return pos;
587
588 /* Verify that the argument position does not exceed the number
589 of formal arguments to the function. When POSARG_ELLIPSIS
590 is set, ARGNO may be beyond the last argument of a vararg
591 function. */
592 unsigned nargs = type_num_arguments (fntype);
593 if (!nargs
594 || !tree_fits_uhwi_p (pos)
595 || ((flags & POSARG_ELLIPSIS) == 0
596 && !IN_RANGE (tree_to_uhwi (pos), 1, nargs)))
597 {
598
599 if (argno < 1)
600 warning (OPT_Wattributes,
601 "%qE attribute argument value %qE exceeds the number "
602 "of function parameters %u",
603 atname, pos, nargs);
604 else
605 warning (OPT_Wattributes,
606 "%qE attribute argument %i value %qE exceeds the number "
607 "of function parameters %u",
608 atname, argno, pos, nargs);
609 return NULL_TREE;
610 }
611
612 /* Verify that the type of the referenced formal argument matches
613 the expected type. */
614 unsigned HOST_WIDE_INT ipos = tree_to_uhwi (pos);
615
616 /* Zero was handled above. */
617 gcc_assert (ipos != 0);
618
619 if (tree argtype = type_argument_type (fntype, ipos))
620 {
621 if (flags & POSARG_ELLIPSIS)
622 {
623 if (argno < 1)
624 error ("%qE attribute argument value %qE does not refer to "
625 "a variable argument list",
626 atname, pos);
627 else
628 error ("%qE attribute argument %i value %qE does not refer to "
629 "a variable argument list",
630 atname, argno, pos);
631 return NULL_TREE;
632 }
633
634 bool type_match;
635 if (code == STRING_CST && POINTER_TYPE_P (argtype))
636 {
637 /* Where the expected code is STRING_CST accept any pointer
638 to a narrow character type, qualified or otherwise. */
639 tree type = TREE_TYPE (argtype);
640 type = TYPE_MAIN_VARIANT (type);
641 type_match = (type == char_type_node
642 || type == signed_char_type_node
643 || type == unsigned_char_type_node);
644 }
645 else if (code == INTEGER_TYPE)
646 /* For integers, accept enums, wide characters and other types
647 that match INTEGRAL_TYPE_P except for bool. */
648 type_match = (INTEGRAL_TYPE_P (argtype)
649 && TREE_CODE (argtype) != BOOLEAN_TYPE);
650 else
651 type_match = TREE_CODE (argtype) == code;
652
653 if (!type_match)
654 {
655 if (argno < 1)
656 warning (OPT_Wattributes,
657 "%qE attribute argument value %qE refers to "
658 "parameter type %qT",
659 atname, pos, argtype);
660 else
661 warning (OPT_Wattributes,
662 "%qE attribute argument %i value %qE refers to "
663 "parameter type %qT",
664 atname, argno, pos, argtype);
665 return NULL_TREE;
666 }
667 }
668 else if (!(flags & POSARG_ELLIPSIS))
669 {
670 if (argno < 1)
671 warning (OPT_Wattributes,
672 "%qE attribute argument value %qE refers to "
673 "a variadic function parameter of unknown type",
674 atname, pos);
675 else
676 warning (OPT_Wattributes,
677 "%qE attribute argument %i value %qE refers to "
678 "a variadic function parameter of unknown type",
679 atname, argno, pos);
680 return NULL_TREE;
681 }
682
683 return pos;
684 }
685
686
687 /* Attribute handlers common to C front ends. */
688
689 /* Handle a "packed" attribute; arguments as in
690 struct attribute_spec.handler. */
691
692 static tree
693 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
694 int flags, bool *no_add_attrs)
695 {
696 if (TYPE_P (*node))
697 {
698 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
699 {
700 warning (OPT_Wattributes,
701 "%qE attribute ignored for type %qT", name, *node);
702 *no_add_attrs = true;
703 }
704 else
705 TYPE_PACKED (*node) = 1;
706 }
707 else if (TREE_CODE (*node) == FIELD_DECL)
708 {
709 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
710 /* Still pack bitfields. */
711 && ! DECL_C_BIT_FIELD (*node))
712 warning (OPT_Wattributes,
713 "%qE attribute ignored for field of type %qT",
714 name, TREE_TYPE (*node));
715 else
716 DECL_PACKED (*node) = 1;
717 }
718 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
719 used for DECL_REGISTER. It wouldn't mean anything anyway.
720 We can't set DECL_PACKED on the type of a TYPE_DECL, because
721 that changes what the typedef is typing. */
722 else
723 {
724 warning (OPT_Wattributes, "%qE attribute ignored", name);
725 *no_add_attrs = true;
726 }
727
728 return NULL_TREE;
729 }
730
731 /* Handle a "nocommon" attribute; arguments as in
732 struct attribute_spec.handler. */
733
734 static tree
735 handle_nocommon_attribute (tree *node, tree name,
736 tree ARG_UNUSED (args),
737 int ARG_UNUSED (flags), bool *no_add_attrs)
738 {
739 if (VAR_P (*node))
740 DECL_COMMON (*node) = 0;
741 else
742 {
743 warning (OPT_Wattributes, "%qE attribute ignored", name);
744 *no_add_attrs = true;
745 }
746
747 return NULL_TREE;
748 }
749
750 /* Handle a "common" attribute; arguments as in
751 struct attribute_spec.handler. */
752
753 static tree
754 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
755 int ARG_UNUSED (flags), bool *no_add_attrs)
756 {
757 if (VAR_P (*node))
758 DECL_COMMON (*node) = 1;
759 else
760 {
761 warning (OPT_Wattributes, "%qE attribute ignored", name);
762 *no_add_attrs = true;
763 }
764
765 return NULL_TREE;
766 }
767
768 /* Handle a "noreturn" attribute; arguments as in
769 struct attribute_spec.handler. */
770
771 static tree
772 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
773 int ARG_UNUSED (flags), bool *no_add_attrs)
774 {
775 tree type = TREE_TYPE (*node);
776
777 /* See FIXME comment in c_common_attribute_table. */
778 if (TREE_CODE (*node) == FUNCTION_DECL
779 || objc_method_decl (TREE_CODE (*node)))
780 TREE_THIS_VOLATILE (*node) = 1;
781 else if (TREE_CODE (type) == POINTER_TYPE
782 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
783 TREE_TYPE (*node)
784 = (build_qualified_type
785 (build_pointer_type
786 (build_type_variant (TREE_TYPE (type),
787 TYPE_READONLY (TREE_TYPE (type)), 1)),
788 TYPE_QUALS (type)));
789 else
790 {
791 warning (OPT_Wattributes, "%qE attribute ignored", name);
792 *no_add_attrs = true;
793 }
794
795 return NULL_TREE;
796 }
797
798 /* Handle a "hot" and attribute; arguments as in
799 struct attribute_spec.handler. */
800
801 static tree
802 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
803 int ARG_UNUSED (flags), bool *no_add_attrs)
804 {
805 if (TREE_CODE (*node) == FUNCTION_DECL
806 || TREE_CODE (*node) == LABEL_DECL)
807 {
808 /* Attribute hot processing is done later with lookup_attribute. */
809 }
810 else
811 {
812 warning (OPT_Wattributes, "%qE attribute ignored", name);
813 *no_add_attrs = true;
814 }
815
816 return NULL_TREE;
817 }
818
819 /* Handle a "cold" and attribute; arguments as in
820 struct attribute_spec.handler. */
821
822 static tree
823 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
824 int ARG_UNUSED (flags), bool *no_add_attrs)
825 {
826 if (TREE_CODE (*node) == FUNCTION_DECL
827 || TREE_CODE (*node) == LABEL_DECL)
828 {
829 /* Attribute cold processing is done later with lookup_attribute. */
830 }
831 else
832 {
833 warning (OPT_Wattributes, "%qE attribute ignored", name);
834 *no_add_attrs = true;
835 }
836
837 return NULL_TREE;
838 }
839
840 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
841
842 void
843 add_no_sanitize_value (tree node, unsigned int flags)
844 {
845 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
846 if (attr)
847 {
848 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
849 flags |= old_value;
850
851 if (flags == old_value)
852 return;
853
854 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
855 }
856 else
857 DECL_ATTRIBUTES (node)
858 = tree_cons (get_identifier ("no_sanitize"),
859 build_int_cst (unsigned_type_node, flags),
860 DECL_ATTRIBUTES (node));
861 }
862
863 /* Handle a "no_sanitize" attribute; arguments as in
864 struct attribute_spec.handler. */
865
866 static tree
867 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
868 bool *no_add_attrs)
869 {
870 unsigned int flags = 0;
871 *no_add_attrs = true;
872 if (TREE_CODE (*node) != FUNCTION_DECL)
873 {
874 warning (OPT_Wattributes, "%qE attribute ignored", name);
875 return NULL_TREE;
876 }
877
878 for (; args; args = TREE_CHAIN (args))
879 {
880 tree id = TREE_VALUE (args);
881 if (TREE_CODE (id) != STRING_CST)
882 {
883 error ("no_sanitize argument not a string");
884 return NULL_TREE;
885 }
886
887 char *string = ASTRDUP (TREE_STRING_POINTER (id));
888 flags |= parse_no_sanitize_attribute (string);
889 }
890
891 add_no_sanitize_value (*node, flags);
892
893 return NULL_TREE;
894 }
895
896 /* Handle a "no_sanitize_address" attribute; arguments as in
897 struct attribute_spec.handler. */
898
899 static tree
900 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
901 bool *no_add_attrs)
902 {
903 *no_add_attrs = true;
904 if (TREE_CODE (*node) != FUNCTION_DECL)
905 warning (OPT_Wattributes, "%qE attribute ignored", name);
906 else
907 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
908
909 return NULL_TREE;
910 }
911
912 /* Handle a "no_sanitize_thread" attribute; arguments as in
913 struct attribute_spec.handler. */
914
915 static tree
916 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
917 bool *no_add_attrs)
918 {
919 *no_add_attrs = true;
920 if (TREE_CODE (*node) != FUNCTION_DECL)
921 warning (OPT_Wattributes, "%qE attribute ignored", name);
922 else
923 add_no_sanitize_value (*node, SANITIZE_THREAD);
924
925 return NULL_TREE;
926 }
927
928
929 /* Handle a "no_address_safety_analysis" attribute; arguments as in
930 struct attribute_spec.handler. */
931
932 static tree
933 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
934 bool *no_add_attrs)
935 {
936 *no_add_attrs = true;
937 if (TREE_CODE (*node) != FUNCTION_DECL)
938 warning (OPT_Wattributes, "%qE attribute ignored", name);
939 else
940 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
941
942 return NULL_TREE;
943 }
944
945 /* Handle a "no_sanitize_undefined" attribute; arguments as in
946 struct attribute_spec.handler. */
947
948 static tree
949 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
950 bool *no_add_attrs)
951 {
952 *no_add_attrs = true;
953 if (TREE_CODE (*node) != FUNCTION_DECL)
954 warning (OPT_Wattributes, "%qE attribute ignored", name);
955 else
956 add_no_sanitize_value (*node,
957 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
958
959 return NULL_TREE;
960 }
961
962 /* Handle an "asan odr indicator" attribute; arguments as in
963 struct attribute_spec.handler. */
964
965 static tree
966 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
967 {
968 return NULL_TREE;
969 }
970
971 /* Handle a "stack_protect" attribute; arguments as in
972 struct attribute_spec.handler. */
973
974 static tree
975 handle_stack_protect_attribute (tree *node, tree name, tree, int,
976 bool *no_add_attrs)
977 {
978 if (TREE_CODE (*node) != FUNCTION_DECL)
979 {
980 warning (OPT_Wattributes, "%qE attribute ignored", name);
981 *no_add_attrs = true;
982 }
983
984 return NULL_TREE;
985 }
986
987 /* Handle a "noipa" attribute; arguments as in
988 struct attribute_spec.handler. */
989
990 static tree
991 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
992 {
993 if (TREE_CODE (*node) != FUNCTION_DECL)
994 {
995 warning (OPT_Wattributes, "%qE attribute ignored", name);
996 *no_add_attrs = true;
997 }
998
999 return NULL_TREE;
1000 }
1001
1002 /* Handle a "noinline" attribute; arguments as in
1003 struct attribute_spec.handler. */
1004
1005 static tree
1006 handle_noinline_attribute (tree *node, tree name,
1007 tree ARG_UNUSED (args),
1008 int ARG_UNUSED (flags), bool *no_add_attrs)
1009 {
1010 if (TREE_CODE (*node) == FUNCTION_DECL)
1011 {
1012 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
1013 {
1014 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1015 "with attribute %qs", name, "always_inline");
1016 *no_add_attrs = true;
1017 }
1018 else
1019 DECL_UNINLINABLE (*node) = 1;
1020 }
1021 else
1022 {
1023 warning (OPT_Wattributes, "%qE attribute ignored", name);
1024 *no_add_attrs = true;
1025 }
1026
1027 return NULL_TREE;
1028 }
1029
1030 /* Handle a "noclone" attribute; arguments as in
1031 struct attribute_spec.handler. */
1032
1033 static tree
1034 handle_noclone_attribute (tree *node, tree name,
1035 tree ARG_UNUSED (args),
1036 int ARG_UNUSED (flags), bool *no_add_attrs)
1037 {
1038 if (TREE_CODE (*node) != FUNCTION_DECL)
1039 {
1040 warning (OPT_Wattributes, "%qE attribute ignored", name);
1041 *no_add_attrs = true;
1042 }
1043
1044 return NULL_TREE;
1045 }
1046
1047 /* Handle a "nocf_check" attribute; arguments as in
1048 struct attribute_spec.handler. */
1049
1050 static tree
1051 handle_nocf_check_attribute (tree *node, tree name,
1052 tree ARG_UNUSED (args),
1053 int ARG_UNUSED (flags), bool *no_add_attrs)
1054 {
1055 if (TREE_CODE (*node) != FUNCTION_TYPE
1056 && TREE_CODE (*node) != METHOD_TYPE)
1057 {
1058 warning (OPT_Wattributes, "%qE attribute ignored", name);
1059 *no_add_attrs = true;
1060 }
1061 else if (!(flag_cf_protection & CF_BRANCH))
1062 {
1063 warning (OPT_Wattributes, "%qE attribute ignored. Use "
1064 "-fcf-protection option to enable it", name);
1065 *no_add_attrs = true;
1066 }
1067
1068 return NULL_TREE;
1069 }
1070
1071 /* Handle a "no_icf" attribute; arguments as in
1072 struct attribute_spec.handler. */
1073
1074 static tree
1075 handle_noicf_attribute (tree *node, tree name,
1076 tree ARG_UNUSED (args),
1077 int ARG_UNUSED (flags), bool *no_add_attrs)
1078 {
1079 if (TREE_CODE (*node) != FUNCTION_DECL)
1080 {
1081 warning (OPT_Wattributes, "%qE attribute ignored", name);
1082 *no_add_attrs = true;
1083 }
1084
1085 return NULL_TREE;
1086 }
1087
1088
1089 /* Handle a "always_inline" attribute; arguments as in
1090 struct attribute_spec.handler. */
1091
1092 static tree
1093 handle_always_inline_attribute (tree *node, tree name,
1094 tree ARG_UNUSED (args),
1095 int ARG_UNUSED (flags),
1096 bool *no_add_attrs)
1097 {
1098 if (TREE_CODE (*node) == FUNCTION_DECL)
1099 {
1100 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
1101 {
1102 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1103 "with %qs attribute", name, "noinline");
1104 *no_add_attrs = true;
1105 }
1106 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
1107 {
1108 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1109 "with %qs attribute", name, "target_clones");
1110 *no_add_attrs = true;
1111 }
1112 else
1113 /* Set the attribute and mark it for disregarding inline
1114 limits. */
1115 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
1116 }
1117 else
1118 {
1119 warning (OPT_Wattributes, "%qE attribute ignored", name);
1120 *no_add_attrs = true;
1121 }
1122
1123 return NULL_TREE;
1124 }
1125
1126 /* Handle a "gnu_inline" attribute; arguments as in
1127 struct attribute_spec.handler. */
1128
1129 static tree
1130 handle_gnu_inline_attribute (tree *node, tree name,
1131 tree ARG_UNUSED (args),
1132 int ARG_UNUSED (flags),
1133 bool *no_add_attrs)
1134 {
1135 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1136 {
1137 /* Do nothing else, just set the attribute. We'll get at
1138 it later with lookup_attribute. */
1139 }
1140 else
1141 {
1142 warning (OPT_Wattributes, "%qE attribute ignored", name);
1143 *no_add_attrs = true;
1144 }
1145
1146 return NULL_TREE;
1147 }
1148
1149 /* Handle a "leaf" attribute; arguments as in
1150 struct attribute_spec.handler. */
1151
1152 static tree
1153 handle_leaf_attribute (tree *node, tree name,
1154 tree ARG_UNUSED (args),
1155 int ARG_UNUSED (flags), bool *no_add_attrs)
1156 {
1157 if (TREE_CODE (*node) != FUNCTION_DECL)
1158 {
1159 warning (OPT_Wattributes, "%qE attribute ignored", name);
1160 *no_add_attrs = true;
1161 }
1162 if (!TREE_PUBLIC (*node))
1163 {
1164 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
1165 "functions", name);
1166 *no_add_attrs = true;
1167 }
1168
1169 return NULL_TREE;
1170 }
1171
1172 /* Handle an "artificial" attribute; arguments as in
1173 struct attribute_spec.handler. */
1174
1175 static tree
1176 handle_artificial_attribute (tree *node, tree name,
1177 tree ARG_UNUSED (args),
1178 int ARG_UNUSED (flags),
1179 bool *no_add_attrs)
1180 {
1181 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1182 {
1183 /* Do nothing else, just set the attribute. We'll get at
1184 it later with lookup_attribute. */
1185 }
1186 else
1187 {
1188 warning (OPT_Wattributes, "%qE attribute ignored", name);
1189 *no_add_attrs = true;
1190 }
1191
1192 return NULL_TREE;
1193 }
1194
1195 /* Handle a "flatten" attribute; arguments as in
1196 struct attribute_spec.handler. */
1197
1198 static tree
1199 handle_flatten_attribute (tree *node, tree name,
1200 tree args ATTRIBUTE_UNUSED,
1201 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1202 {
1203 if (TREE_CODE (*node) == FUNCTION_DECL)
1204 /* Do nothing else, just set the attribute. We'll get at
1205 it later with lookup_attribute. */
1206 ;
1207 else
1208 {
1209 warning (OPT_Wattributes, "%qE attribute ignored", name);
1210 *no_add_attrs = true;
1211 }
1212
1213 return NULL_TREE;
1214 }
1215
1216 /* Handle a "warning" or "error" attribute; arguments as in
1217 struct attribute_spec.handler. */
1218
1219 static tree
1220 handle_error_attribute (tree *node, tree name, tree args,
1221 int ARG_UNUSED (flags), bool *no_add_attrs)
1222 {
1223 if (TREE_CODE (*node) == FUNCTION_DECL
1224 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
1225 /* Do nothing else, just set the attribute. We'll get at
1226 it later with lookup_attribute. */
1227 ;
1228 else
1229 {
1230 warning (OPT_Wattributes, "%qE attribute ignored", name);
1231 *no_add_attrs = true;
1232 }
1233
1234 return NULL_TREE;
1235 }
1236
1237 /* Handle a "used" attribute; arguments as in
1238 struct attribute_spec.handler. */
1239
1240 static tree
1241 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1242 int ARG_UNUSED (flags), bool *no_add_attrs)
1243 {
1244 tree node = *pnode;
1245
1246 if (TREE_CODE (node) == FUNCTION_DECL
1247 || (VAR_P (node) && TREE_STATIC (node))
1248 || (TREE_CODE (node) == TYPE_DECL))
1249 {
1250 TREE_USED (node) = 1;
1251 DECL_PRESERVE_P (node) = 1;
1252 if (VAR_P (node))
1253 DECL_READ_P (node) = 1;
1254 }
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 "unused" attribute; arguments as in
1265 struct attribute_spec.handler. */
1266
1267 tree
1268 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1269 int flags, bool *no_add_attrs)
1270 {
1271 if (DECL_P (*node))
1272 {
1273 tree decl = *node;
1274
1275 if (TREE_CODE (decl) == PARM_DECL
1276 || VAR_OR_FUNCTION_DECL_P (decl)
1277 || TREE_CODE (decl) == LABEL_DECL
1278 || TREE_CODE (decl) == CONST_DECL
1279 || TREE_CODE (decl) == TYPE_DECL)
1280 {
1281 TREE_USED (decl) = 1;
1282 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1283 DECL_READ_P (decl) = 1;
1284 }
1285 else
1286 {
1287 warning (OPT_Wattributes, "%qE attribute ignored", name);
1288 *no_add_attrs = true;
1289 }
1290 }
1291 else
1292 {
1293 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1294 *node = build_variant_type_copy (*node);
1295 TREE_USED (*node) = 1;
1296 }
1297
1298 return NULL_TREE;
1299 }
1300
1301 /* Handle a "externally_visible" attribute; arguments as in
1302 struct attribute_spec.handler. */
1303
1304 static tree
1305 handle_externally_visible_attribute (tree *pnode, tree name,
1306 tree ARG_UNUSED (args),
1307 int ARG_UNUSED (flags),
1308 bool *no_add_attrs)
1309 {
1310 tree node = *pnode;
1311
1312 if (VAR_OR_FUNCTION_DECL_P (node))
1313 {
1314 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1315 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1316 {
1317 warning (OPT_Wattributes,
1318 "%qE attribute have effect only on public objects", name);
1319 *no_add_attrs = true;
1320 }
1321 }
1322 else
1323 {
1324 warning (OPT_Wattributes, "%qE attribute ignored", name);
1325 *no_add_attrs = true;
1326 }
1327
1328 return NULL_TREE;
1329 }
1330
1331 /* Handle the "no_reorder" attribute. Arguments as in
1332 struct attribute_spec.handler. */
1333
1334 static tree
1335 handle_no_reorder_attribute (tree *pnode,
1336 tree name,
1337 tree,
1338 int,
1339 bool *no_add_attrs)
1340 {
1341 tree node = *pnode;
1342
1343 if (!VAR_OR_FUNCTION_DECL_P (node)
1344 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1345 {
1346 warning (OPT_Wattributes,
1347 "%qE attribute only affects top level objects",
1348 name);
1349 *no_add_attrs = true;
1350 }
1351
1352 return NULL_TREE;
1353 }
1354
1355 /* Handle a "const" attribute; arguments as in
1356 struct attribute_spec.handler. */
1357
1358 static tree
1359 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1360 int flags, bool *no_add_attrs)
1361 {
1362 tree type = TREE_TYPE (*node);
1363
1364 /* See FIXME comment on noreturn in c_common_attribute_table. */
1365 if (TREE_CODE (*node) == FUNCTION_DECL)
1366 TREE_READONLY (*node) = 1;
1367 else if (TREE_CODE (type) == POINTER_TYPE
1368 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1369 TREE_TYPE (*node)
1370 = (build_qualified_type
1371 (build_pointer_type
1372 (build_type_variant (TREE_TYPE (type), 1,
1373 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1374 TYPE_QUALS (type)));
1375 else
1376 {
1377 warning (OPT_Wattributes, "%qE attribute ignored", name);
1378 *no_add_attrs = true;
1379 }
1380
1381 /* void __builtin_unreachable(void) is const. Accept other such
1382 built-ins but warn on user-defined functions that return void. */
1383 if (!(flags & ATTR_FLAG_BUILT_IN)
1384 && TREE_CODE (*node) == FUNCTION_DECL
1385 && VOID_TYPE_P (TREE_TYPE (type)))
1386 warning (OPT_Wattributes, "%qE attribute on function "
1387 "returning %<void%>", name);
1388
1389 return NULL_TREE;
1390 }
1391
1392 /* Handle a "scalar_storage_order" attribute; arguments as in
1393 struct attribute_spec.handler. */
1394
1395 static tree
1396 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1397 int flags, bool *no_add_attrs)
1398 {
1399 tree id = TREE_VALUE (args);
1400 tree type;
1401
1402 if (TREE_CODE (*node) == TYPE_DECL
1403 && ! (flags & ATTR_FLAG_CXX11))
1404 node = &TREE_TYPE (*node);
1405 type = *node;
1406
1407 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1408 {
1409 error ("scalar_storage_order is not supported because endianness "
1410 "is not uniform");
1411 return NULL_TREE;
1412 }
1413
1414 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1415 {
1416 bool reverse = false;
1417
1418 if (TREE_CODE (id) == STRING_CST
1419 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1420 reverse = !BYTES_BIG_ENDIAN;
1421 else if (TREE_CODE (id) == STRING_CST
1422 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1423 reverse = BYTES_BIG_ENDIAN;
1424 else
1425 {
1426 error ("scalar_storage_order argument must be one of \"big-endian\""
1427 " or \"little-endian\"");
1428 return NULL_TREE;
1429 }
1430
1431 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1432 {
1433 if (reverse)
1434 /* A type variant isn't good enough, since we don't want a cast
1435 to such a type to be removed as a no-op. */
1436 *node = type = build_duplicate_type (type);
1437 }
1438
1439 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1440 return NULL_TREE;
1441 }
1442
1443 warning (OPT_Wattributes, "%qE attribute ignored", name);
1444 *no_add_attrs = true;
1445 return NULL_TREE;
1446 }
1447
1448 /* Handle a "transparent_union" attribute; arguments as in
1449 struct attribute_spec.handler. */
1450
1451 static tree
1452 handle_transparent_union_attribute (tree *node, tree name,
1453 tree ARG_UNUSED (args), int flags,
1454 bool *no_add_attrs)
1455 {
1456 tree type;
1457
1458 *no_add_attrs = true;
1459
1460 if (TREE_CODE (*node) == TYPE_DECL
1461 && ! (flags & ATTR_FLAG_CXX11))
1462 node = &TREE_TYPE (*node);
1463 type = *node;
1464
1465 if (TREE_CODE (type) == UNION_TYPE)
1466 {
1467 /* Make sure that the first field will work for a transparent union.
1468 If the type isn't complete yet, leave the check to the code in
1469 finish_struct. */
1470 if (TYPE_SIZE (type))
1471 {
1472 tree first = first_field (type);
1473 if (first == NULL_TREE
1474 || DECL_ARTIFICIAL (first)
1475 || TYPE_MODE (type) != DECL_MODE (first))
1476 goto ignored;
1477 }
1478
1479 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1480 {
1481 /* If the type isn't complete yet, setting the flag
1482 on a variant wouldn't ever be checked. */
1483 if (!TYPE_SIZE (type))
1484 goto ignored;
1485
1486 /* build_duplicate_type doesn't work for C++. */
1487 if (c_dialect_cxx ())
1488 goto ignored;
1489
1490 /* A type variant isn't good enough, since we don't want a cast
1491 to such a type to be removed as a no-op. */
1492 *node = type = build_duplicate_type (type);
1493 }
1494
1495 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1496 TYPE_TRANSPARENT_AGGR (t) = 1;
1497 return NULL_TREE;
1498 }
1499
1500 ignored:
1501 warning (OPT_Wattributes, "%qE attribute ignored", name);
1502 return NULL_TREE;
1503 }
1504
1505 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1506 get the requested priority for a constructor or destructor,
1507 possibly issuing diagnostics for invalid or reserved
1508 priorities. */
1509
1510 static priority_type
1511 get_priority (tree args, bool is_destructor)
1512 {
1513 HOST_WIDE_INT pri;
1514 tree arg;
1515
1516 if (!args)
1517 return DEFAULT_INIT_PRIORITY;
1518
1519 if (!SUPPORTS_INIT_PRIORITY)
1520 {
1521 if (is_destructor)
1522 error ("destructor priorities are not supported");
1523 else
1524 error ("constructor priorities are not supported");
1525 return DEFAULT_INIT_PRIORITY;
1526 }
1527
1528 arg = TREE_VALUE (args);
1529 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1530 goto invalid;
1531 if (arg == error_mark_node)
1532 return DEFAULT_INIT_PRIORITY;
1533 arg = default_conversion (arg);
1534 if (!tree_fits_shwi_p (arg)
1535 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1536 goto invalid;
1537
1538 pri = tree_to_shwi (arg);
1539 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1540 goto invalid;
1541
1542 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1543 {
1544 if (is_destructor)
1545 warning (OPT_Wprio_ctor_dtor,
1546 "destructor priorities from 0 to %d are reserved "
1547 "for the implementation",
1548 MAX_RESERVED_INIT_PRIORITY);
1549 else
1550 warning (OPT_Wprio_ctor_dtor,
1551 "constructor priorities from 0 to %d are reserved "
1552 "for the implementation",
1553 MAX_RESERVED_INIT_PRIORITY);
1554 }
1555 return pri;
1556
1557 invalid:
1558 if (is_destructor)
1559 error ("destructor priorities must be integers from 0 to %d inclusive",
1560 MAX_INIT_PRIORITY);
1561 else
1562 error ("constructor priorities must be integers from 0 to %d inclusive",
1563 MAX_INIT_PRIORITY);
1564 return DEFAULT_INIT_PRIORITY;
1565 }
1566
1567 /* Handle a "constructor" attribute; arguments as in
1568 struct attribute_spec.handler. */
1569
1570 static tree
1571 handle_constructor_attribute (tree *node, tree name, tree args,
1572 int ARG_UNUSED (flags),
1573 bool *no_add_attrs)
1574 {
1575 tree decl = *node;
1576 tree type = TREE_TYPE (decl);
1577
1578 if (TREE_CODE (decl) == FUNCTION_DECL
1579 && TREE_CODE (type) == FUNCTION_TYPE
1580 && decl_function_context (decl) == 0)
1581 {
1582 priority_type priority;
1583 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1584 priority = get_priority (args, /*is_destructor=*/false);
1585 SET_DECL_INIT_PRIORITY (decl, priority);
1586 TREE_USED (decl) = 1;
1587 }
1588 else
1589 {
1590 warning (OPT_Wattributes, "%qE attribute ignored", name);
1591 *no_add_attrs = true;
1592 }
1593
1594 return NULL_TREE;
1595 }
1596
1597 /* Handle a "destructor" attribute; arguments as in
1598 struct attribute_spec.handler. */
1599
1600 static tree
1601 handle_destructor_attribute (tree *node, tree name, tree args,
1602 int ARG_UNUSED (flags),
1603 bool *no_add_attrs)
1604 {
1605 tree decl = *node;
1606 tree type = TREE_TYPE (decl);
1607
1608 if (TREE_CODE (decl) == FUNCTION_DECL
1609 && TREE_CODE (type) == FUNCTION_TYPE
1610 && decl_function_context (decl) == 0)
1611 {
1612 priority_type priority;
1613 DECL_STATIC_DESTRUCTOR (decl) = 1;
1614 priority = get_priority (args, /*is_destructor=*/true);
1615 SET_DECL_FINI_PRIORITY (decl, priority);
1616 TREE_USED (decl) = 1;
1617 }
1618 else
1619 {
1620 warning (OPT_Wattributes, "%qE attribute ignored", name);
1621 *no_add_attrs = true;
1622 }
1623
1624 return NULL_TREE;
1625 }
1626
1627 /* Nonzero if the mode is a valid vector mode for this architecture.
1628 This returns nonzero even if there is no hardware support for the
1629 vector mode, but we can emulate with narrower modes. */
1630
1631 static bool
1632 vector_mode_valid_p (machine_mode mode)
1633 {
1634 enum mode_class mclass = GET_MODE_CLASS (mode);
1635
1636 /* Doh! What's going on? */
1637 if (mclass != MODE_VECTOR_INT
1638 && mclass != MODE_VECTOR_FLOAT
1639 && mclass != MODE_VECTOR_FRACT
1640 && mclass != MODE_VECTOR_UFRACT
1641 && mclass != MODE_VECTOR_ACCUM
1642 && mclass != MODE_VECTOR_UACCUM)
1643 return false;
1644
1645 /* Hardware support. Woo hoo! */
1646 if (targetm.vector_mode_supported_p (mode))
1647 return true;
1648
1649 /* We should probably return 1 if requesting V4DI and we have no DI,
1650 but we have V2DI, but this is probably very unlikely. */
1651
1652 /* If we have support for the inner mode, we can safely emulate it.
1653 We may not have V2DI, but me can emulate with a pair of DIs. */
1654 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1655 }
1656
1657
1658 /* Handle a "mode" attribute; arguments as in
1659 struct attribute_spec.handler. */
1660
1661 static tree
1662 handle_mode_attribute (tree *node, tree name, tree args,
1663 int ARG_UNUSED (flags), bool *no_add_attrs)
1664 {
1665 tree type = *node;
1666 tree ident = TREE_VALUE (args);
1667
1668 *no_add_attrs = true;
1669
1670 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1671 warning (OPT_Wattributes, "%qE attribute ignored", name);
1672 else
1673 {
1674 int j;
1675 const char *p = IDENTIFIER_POINTER (ident);
1676 int len = strlen (p);
1677 machine_mode mode = VOIDmode;
1678 tree typefm;
1679 bool valid_mode;
1680
1681 if (len > 4 && p[0] == '_' && p[1] == '_'
1682 && p[len - 1] == '_' && p[len - 2] == '_')
1683 {
1684 char *newp = (char *) alloca (len - 1);
1685
1686 strcpy (newp, &p[2]);
1687 newp[len - 4] = '\0';
1688 p = newp;
1689 }
1690
1691 /* Change this type to have a type with the specified mode.
1692 First check for the special modes. */
1693 if (!strcmp (p, "byte"))
1694 mode = byte_mode;
1695 else if (!strcmp (p, "word"))
1696 mode = word_mode;
1697 else if (!strcmp (p, "pointer"))
1698 mode = ptr_mode;
1699 else if (!strcmp (p, "libgcc_cmp_return"))
1700 mode = targetm.libgcc_cmp_return_mode ();
1701 else if (!strcmp (p, "libgcc_shift_count"))
1702 mode = targetm.libgcc_shift_count_mode ();
1703 else if (!strcmp (p, "unwind_word"))
1704 mode = targetm.unwind_word_mode ();
1705 else
1706 for (j = 0; j < NUM_MACHINE_MODES; j++)
1707 if (!strcmp (p, GET_MODE_NAME (j)))
1708 {
1709 mode = (machine_mode) j;
1710 break;
1711 }
1712
1713 if (mode == VOIDmode)
1714 {
1715 error ("unknown machine mode %qE", ident);
1716 return NULL_TREE;
1717 }
1718
1719 /* Allow the target a chance to translate MODE into something supported.
1720 See PR86324. */
1721 mode = targetm.translate_mode_attribute (mode);
1722
1723 valid_mode = false;
1724 switch (GET_MODE_CLASS (mode))
1725 {
1726 case MODE_INT:
1727 case MODE_PARTIAL_INT:
1728 case MODE_FLOAT:
1729 case MODE_DECIMAL_FLOAT:
1730 case MODE_FRACT:
1731 case MODE_UFRACT:
1732 case MODE_ACCUM:
1733 case MODE_UACCUM:
1734 valid_mode
1735 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
1736 break;
1737
1738 case MODE_COMPLEX_INT:
1739 case MODE_COMPLEX_FLOAT:
1740 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1741 break;
1742
1743 case MODE_VECTOR_INT:
1744 case MODE_VECTOR_FLOAT:
1745 case MODE_VECTOR_FRACT:
1746 case MODE_VECTOR_UFRACT:
1747 case MODE_VECTOR_ACCUM:
1748 case MODE_VECTOR_UACCUM:
1749 warning (OPT_Wattributes, "specifying vector types with "
1750 "__attribute__ ((mode)) is deprecated");
1751 warning (OPT_Wattributes,
1752 "use __attribute__ ((vector_size)) instead");
1753 valid_mode = vector_mode_valid_p (mode);
1754 break;
1755
1756 default:
1757 break;
1758 }
1759 if (!valid_mode)
1760 {
1761 error ("unable to emulate %qs", p);
1762 return NULL_TREE;
1763 }
1764
1765 if (POINTER_TYPE_P (type))
1766 {
1767 scalar_int_mode addr_mode;
1768 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1769 tree (*fn)(tree, machine_mode, bool);
1770
1771 if (!is_a <scalar_int_mode> (mode, &addr_mode)
1772 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
1773 {
1774 error ("invalid pointer mode %qs", p);
1775 return NULL_TREE;
1776 }
1777
1778 if (TREE_CODE (type) == POINTER_TYPE)
1779 fn = build_pointer_type_for_mode;
1780 else
1781 fn = build_reference_type_for_mode;
1782 typefm = fn (TREE_TYPE (type), addr_mode, false);
1783 }
1784 else
1785 {
1786 /* For fixed-point modes, we need to test if the signness of type
1787 and the machine mode are consistent. */
1788 if (ALL_FIXED_POINT_MODE_P (mode)
1789 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1790 {
1791 error ("signedness of type and machine mode %qs don%'t match", p);
1792 return NULL_TREE;
1793 }
1794 /* For fixed-point modes, we need to pass saturating info. */
1795 typefm = lang_hooks.types.type_for_mode (mode,
1796 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1797 : TYPE_UNSIGNED (type));
1798 }
1799
1800 if (typefm == NULL_TREE)
1801 {
1802 error ("no data type for mode %qs", p);
1803 return NULL_TREE;
1804 }
1805 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1806 {
1807 /* For enumeral types, copy the precision from the integer
1808 type returned above. If not an INTEGER_TYPE, we can't use
1809 this mode for this type. */
1810 if (TREE_CODE (typefm) != INTEGER_TYPE)
1811 {
1812 error ("cannot use mode %qs for enumeral types", p);
1813 return NULL_TREE;
1814 }
1815
1816 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1817 {
1818 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1819 typefm = type;
1820 }
1821 else
1822 {
1823 /* We cannot build a type variant, as there's code that assumes
1824 that TYPE_MAIN_VARIANT has the same mode. This includes the
1825 debug generators. Instead, create a subrange type. This
1826 results in all of the enumeral values being emitted only once
1827 in the original, and the subtype gets them by reference. */
1828 if (TYPE_UNSIGNED (type))
1829 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1830 else
1831 typefm = make_signed_type (TYPE_PRECISION (typefm));
1832 TREE_TYPE (typefm) = type;
1833 }
1834 }
1835 else if (VECTOR_MODE_P (mode)
1836 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1837 : TREE_CODE (type) != TREE_CODE (typefm))
1838 {
1839 error ("mode %qs applied to inappropriate type", p);
1840 return NULL_TREE;
1841 }
1842
1843 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1844 }
1845
1846 return NULL_TREE;
1847 }
1848
1849 /* Handle a "section" attribute; arguments as in
1850 struct attribute_spec.handler. */
1851
1852 static tree
1853 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1854 int ARG_UNUSED (flags), bool *no_add_attrs)
1855 {
1856 tree decl = *node;
1857
1858 if (!targetm_common.have_named_sections)
1859 {
1860 error_at (DECL_SOURCE_LOCATION (*node),
1861 "section attributes are not supported for this target");
1862 goto fail;
1863 }
1864
1865 if (!VAR_OR_FUNCTION_DECL_P (decl))
1866 {
1867 error ("section attribute not allowed for %q+D", *node);
1868 goto fail;
1869 }
1870
1871 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1872 {
1873 error ("section attribute argument not a string constant");
1874 goto fail;
1875 }
1876
1877 if (VAR_P (decl)
1878 && current_function_decl != NULL_TREE
1879 && !TREE_STATIC (decl))
1880 {
1881 error_at (DECL_SOURCE_LOCATION (decl),
1882 "section attribute cannot be specified for local variables");
1883 goto fail;
1884 }
1885
1886 /* The decl may have already been given a section attribute
1887 from a previous declaration. Ensure they match. */
1888 if (DECL_SECTION_NAME (decl) != NULL
1889 && strcmp (DECL_SECTION_NAME (decl),
1890 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1891 {
1892 error ("section of %q+D conflicts with previous declaration", *node);
1893 goto fail;
1894 }
1895
1896 if (VAR_P (decl)
1897 && !targetm.have_tls && targetm.emutls.tmpl_section
1898 && DECL_THREAD_LOCAL_P (decl))
1899 {
1900 error ("section of %q+D cannot be overridden", *node);
1901 goto fail;
1902 }
1903
1904 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1905 return NULL_TREE;
1906
1907 fail:
1908 *no_add_attrs = true;
1909 return NULL_TREE;
1910 }
1911
1912 /* If in c++-11, check if the c++-11 alignment constraint with respect
1913 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1914 c++-11 mode, does nothing.
1915
1916 [dcl.align]2/ says:
1917
1918 [* if the constant expression evaluates to a fundamental alignment,
1919 the alignment requirement of the declared entity shall be the
1920 specified fundamental alignment.
1921
1922 * if the constant expression evaluates to an extended alignment
1923 and the implementation supports that alignment in the context
1924 of the declaration, the alignment of the declared entity shall
1925 be that alignment
1926
1927 * if the constant expression evaluates to an extended alignment
1928 and the implementation does not support that alignment in the
1929 context of the declaration, the program is ill-formed]. */
1930
1931 static bool
1932 check_cxx_fundamental_alignment_constraints (tree node,
1933 unsigned align_log,
1934 int flags)
1935 {
1936 bool alignment_too_large_p = false;
1937 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1938 unsigned max_align = 0;
1939
1940 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1941 || (node == NULL_TREE || node == error_mark_node))
1942 return true;
1943
1944 if (cxx_fundamental_alignment_p (requested_alignment))
1945 return true;
1946
1947 if (VAR_P (node))
1948 {
1949 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1950 /* For file scope variables and static members, the target supports
1951 alignments that are at most MAX_OFILE_ALIGNMENT. */
1952 max_align = MAX_OFILE_ALIGNMENT;
1953 else
1954 /* For stack variables, the target supports at most
1955 MAX_STACK_ALIGNMENT. */
1956 max_align = MAX_STACK_ALIGNMENT;
1957 if (requested_alignment > max_align)
1958 alignment_too_large_p = true;
1959 }
1960 /* Let's be liberal for types and fields; don't limit their alignment any
1961 more than check_user_alignment already did. */
1962
1963 if (alignment_too_large_p)
1964 pedwarn (input_location, OPT_Wattributes,
1965 "requested alignment %d is larger than %d",
1966 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1967
1968 return !alignment_too_large_p;
1969 }
1970
1971 /* Common codes shared by handle_warn_if_not_aligned_attribute and
1972 handle_aligned_attribute. */
1973
1974 static tree
1975 common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
1976 bool *no_add_attrs,
1977 bool warn_if_not_aligned_p)
1978 {
1979 tree decl = NULL_TREE;
1980 tree *type = NULL;
1981 bool is_type = false;
1982 tree align_expr;
1983
1984 /* The last (already pushed) declaration with all validated attributes
1985 merged in or the current about-to-be-pushed one if one hasn't been
1986 yet. */
1987 tree last_decl = node[1] ? node[1] : *node;
1988
1989 if (args)
1990 {
1991 align_expr = TREE_VALUE (args);
1992 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1993 && TREE_CODE (align_expr) != FUNCTION_DECL)
1994 align_expr = default_conversion (align_expr);
1995 }
1996 else
1997 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1998
1999 if (DECL_P (*node))
2000 {
2001 decl = *node;
2002 type = &TREE_TYPE (decl);
2003 is_type = TREE_CODE (*node) == TYPE_DECL;
2004 }
2005 else if (TYPE_P (*node))
2006 type = node, is_type = true;
2007
2008 /* True to consider invalid alignments greater than MAX_OFILE_ALIGNMENT. */
2009 bool objfile = (TREE_CODE (*node) == FUNCTION_DECL
2010 || (VAR_P (*node) && TREE_STATIC (*node)));
2011 /* Log2 of specified alignment. */
2012 int pow2align = check_user_alignment (align_expr, objfile,
2013 /* warn_zero = */ true);
2014 if (pow2align == -1
2015 || !check_cxx_fundamental_alignment_constraints (*node, pow2align, flags))
2016 {
2017 *no_add_attrs = true;
2018 return NULL_TREE;
2019 }
2020
2021 /* The alignment in bits corresponding to the specified alignment. */
2022 unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
2023
2024 /* The alignment of the current declaration and that of the last
2025 pushed declaration, determined on demand below. */
2026 unsigned curalign = 0;
2027 unsigned lastalign = 0;
2028
2029 /* True when SET_DECL_ALIGN() should be called for the decl when
2030 *NO_ADD_ATTRS is false. */
2031 bool set_align = true;
2032 if (is_type)
2033 {
2034 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2035 /* OK, modify the type in place. */;
2036 /* If we have a TYPE_DECL, then copy the type, so that we
2037 don't accidentally modify a builtin type. See pushdecl. */
2038 else if (decl && TREE_TYPE (decl) != error_mark_node
2039 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
2040 {
2041 tree tt = TREE_TYPE (decl);
2042 *type = build_variant_type_copy (*type);
2043 DECL_ORIGINAL_TYPE (decl) = tt;
2044 TYPE_NAME (*type) = decl;
2045 TREE_USED (*type) = TREE_USED (decl);
2046 TREE_TYPE (decl) = *type;
2047 }
2048 else
2049 *type = build_variant_type_copy (*type);
2050
2051 if (warn_if_not_aligned_p)
2052 {
2053 SET_TYPE_WARN_IF_NOT_ALIGN (*type, bitalign);
2054 warn_if_not_aligned_p = false;
2055 }
2056 else
2057 {
2058 SET_TYPE_ALIGN (*type, bitalign);
2059 TYPE_USER_ALIGN (*type) = 1;
2060 }
2061 }
2062 else if (! VAR_OR_FUNCTION_DECL_P (decl)
2063 && TREE_CODE (decl) != FIELD_DECL)
2064 {
2065 error ("alignment may not be specified for %q+D", decl);
2066 *no_add_attrs = true;
2067 }
2068 else if (TREE_CODE (decl) == FUNCTION_DECL
2069 && ((curalign = DECL_ALIGN (decl)) > bitalign
2070 || ((lastalign = DECL_ALIGN (last_decl)) > bitalign)))
2071 {
2072 /* Either a prior attribute on the same declaration or one
2073 on a prior declaration of the same function specifies
2074 stricter alignment than this attribute. */
2075 bool note = lastalign != 0;
2076 if (lastalign)
2077 curalign = lastalign;
2078
2079 curalign /= BITS_PER_UNIT;
2080 unsigned newalign = bitalign / BITS_PER_UNIT;
2081
2082 auto_diagnostic_group d;
2083 if ((DECL_USER_ALIGN (decl)
2084 || DECL_USER_ALIGN (last_decl)))
2085 {
2086 if (warning (OPT_Wattributes,
2087 "ignoring attribute %<%E (%u)%> because it conflicts "
2088 "with attribute %<%E (%u)%>",
2089 name, newalign, name, curalign)
2090 && note)
2091 inform (DECL_SOURCE_LOCATION (last_decl),
2092 "previous declaration here");
2093 /* Only reject attempts to relax/override an alignment
2094 explicitly specified previously and accept declarations
2095 that appear to relax the implicit function alignment for
2096 the target. Both increasing and increasing the alignment
2097 set by -falign-functions setting is permitted. */
2098 *no_add_attrs = true;
2099 }
2100 else if (!warn_if_not_aligned_p)
2101 {
2102 /* Do not fail for attribute warn_if_not_aligned. Otherwise,
2103 silently avoid applying the alignment to the declaration
2104 because it's implicitly satisfied by the target. Apply
2105 the attribute nevertheless so it can be retrieved by
2106 __builtin_has_attribute. */
2107 set_align = false;
2108 }
2109 }
2110 else if (DECL_USER_ALIGN (decl)
2111 && DECL_ALIGN (decl) > bitalign)
2112 /* C++-11 [dcl.align/4]:
2113
2114 When multiple alignment-specifiers are specified for an
2115 entity, the alignment requirement shall be set to the
2116 strictest specified alignment.
2117
2118 This formally comes from the c++11 specification but we are
2119 doing it for the GNU attribute syntax as well. */
2120 *no_add_attrs = true;
2121 else if (!warn_if_not_aligned_p
2122 && TREE_CODE (decl) == FUNCTION_DECL
2123 && DECL_ALIGN (decl) > bitalign)
2124 {
2125 /* Don't warn for function alignment here if warn_if_not_aligned_p
2126 is true. It will be warned about later. */
2127 if (DECL_USER_ALIGN (decl))
2128 {
2129 /* Only reject attempts to relax/override an alignment
2130 explicitly specified previously and accept declarations
2131 that appear to relax the implicit function alignment for
2132 the target. Both increasing and increasing the alignment
2133 set by -falign-functions setting is permitted. */
2134 error ("alignment for %q+D was previously specified as %d "
2135 "and may not be decreased", decl,
2136 DECL_ALIGN (decl) / BITS_PER_UNIT);
2137 *no_add_attrs = true;
2138 }
2139 }
2140 else if (warn_if_not_aligned_p
2141 && TREE_CODE (decl) == FIELD_DECL
2142 && !DECL_C_BIT_FIELD (decl))
2143 {
2144 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
2145 warn_if_not_aligned_p = false;
2146 set_align = false;
2147 }
2148
2149 if (warn_if_not_aligned_p)
2150 {
2151 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
2152 decl);
2153 *no_add_attrs = true;
2154 }
2155 else if (!is_type && !*no_add_attrs && set_align)
2156 {
2157 SET_DECL_ALIGN (decl, bitalign);
2158 DECL_USER_ALIGN (decl) = 1;
2159 }
2160
2161 return NULL_TREE;
2162 }
2163
2164 /* Handle a "aligned" attribute; arguments as in
2165 struct attribute_spec.handler. */
2166
2167 static tree
2168 handle_aligned_attribute (tree *node, tree name, tree args,
2169 int flags, bool *no_add_attrs)
2170 {
2171 return common_handle_aligned_attribute (node, name, args, flags,
2172 no_add_attrs, false);
2173 }
2174
2175 /* Handle a "warn_if_not_aligned" attribute; arguments as in
2176 struct attribute_spec.handler. */
2177
2178 static tree
2179 handle_warn_if_not_aligned_attribute (tree *node, tree name,
2180 tree args, int flags,
2181 bool *no_add_attrs)
2182 {
2183 return common_handle_aligned_attribute (node, name, args, flags,
2184 no_add_attrs, true);
2185 }
2186
2187 /* Handle a "weak" attribute; arguments as in
2188 struct attribute_spec.handler. */
2189
2190 static tree
2191 handle_weak_attribute (tree *node, tree name,
2192 tree ARG_UNUSED (args),
2193 int ARG_UNUSED (flags),
2194 bool * ARG_UNUSED (no_add_attrs))
2195 {
2196 if (TREE_CODE (*node) == FUNCTION_DECL
2197 && DECL_DECLARED_INLINE_P (*node))
2198 {
2199 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
2200 *no_add_attrs = true;
2201 }
2202 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2203 {
2204 error ("indirect function %q+D cannot be declared weak", *node);
2205 *no_add_attrs = true;
2206 return NULL_TREE;
2207 }
2208 else if (VAR_OR_FUNCTION_DECL_P (*node))
2209 declare_weak (*node);
2210 else
2211 warning (OPT_Wattributes, "%qE attribute ignored", name);
2212
2213 return NULL_TREE;
2214 }
2215
2216 /* Handle a "noplt" attribute; arguments as in
2217 struct attribute_spec.handler. */
2218
2219 static tree
2220 handle_noplt_attribute (tree *node, tree name,
2221 tree ARG_UNUSED (args),
2222 int ARG_UNUSED (flags),
2223 bool * ARG_UNUSED (no_add_attrs))
2224 {
2225 if (TREE_CODE (*node) != FUNCTION_DECL)
2226 {
2227 warning (OPT_Wattributes,
2228 "%qE attribute is only applicable on functions", name);
2229 *no_add_attrs = true;
2230 return NULL_TREE;
2231 }
2232 return NULL_TREE;
2233 }
2234
2235 /* Handle an "alias" or "ifunc" attribute; arguments as in
2236 struct attribute_spec.handler, except that IS_ALIAS tells us
2237 whether this is an alias as opposed to ifunc attribute. */
2238
2239 static tree
2240 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
2241 bool *no_add_attrs)
2242 {
2243 tree decl = *node;
2244
2245 if (TREE_CODE (decl) != FUNCTION_DECL
2246 && (!is_alias || !VAR_P (decl)))
2247 {
2248 warning (OPT_Wattributes, "%qE attribute ignored", name);
2249 *no_add_attrs = true;
2250 }
2251 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
2252 || (TREE_CODE (decl) != FUNCTION_DECL
2253 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
2254 /* A static variable declaration is always a tentative definition,
2255 but the alias is a non-tentative definition which overrides. */
2256 || (TREE_CODE (decl) != FUNCTION_DECL
2257 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
2258 {
2259 error ("%q+D defined both normally and as %qE attribute", decl, name);
2260 *no_add_attrs = true;
2261 return NULL_TREE;
2262 }
2263 else if (!is_alias
2264 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2265 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
2266 {
2267 error ("weak %q+D cannot be defined %qE", decl, name);
2268 *no_add_attrs = true;
2269 return NULL_TREE;
2270 }
2271
2272 /* Note that the very first time we process a nested declaration,
2273 decl_function_context will not be set. Indeed, *would* never
2274 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2275 we do below. After such frobbery, pushdecl would set the context.
2276 In any case, this is never what we want. */
2277 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
2278 {
2279 tree id;
2280
2281 id = TREE_VALUE (args);
2282 if (TREE_CODE (id) != STRING_CST)
2283 {
2284 error ("attribute %qE argument not a string", name);
2285 *no_add_attrs = true;
2286 return NULL_TREE;
2287 }
2288 id = get_identifier (TREE_STRING_POINTER (id));
2289 /* This counts as a use of the object pointed to. */
2290 TREE_USED (id) = 1;
2291
2292 if (TREE_CODE (decl) == FUNCTION_DECL)
2293 DECL_INITIAL (decl) = error_mark_node;
2294 else
2295 TREE_STATIC (decl) = 1;
2296
2297 if (!is_alias)
2298 {
2299 /* ifuncs are also aliases, so set that attribute too. */
2300 DECL_ATTRIBUTES (decl)
2301 = tree_cons (get_identifier ("alias"), args,
2302 DECL_ATTRIBUTES (decl));
2303 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
2304 NULL, DECL_ATTRIBUTES (decl));
2305 }
2306 }
2307 else
2308 {
2309 warning (OPT_Wattributes, "%qE attribute ignored", name);
2310 *no_add_attrs = true;
2311 }
2312
2313 if (decl_in_symtab_p (*node))
2314 {
2315 struct symtab_node *n = symtab_node::get (decl);
2316 if (n && n->refuse_visibility_changes)
2317 {
2318 if (is_alias)
2319 error ("%+qD declared alias after being used", decl);
2320 else
2321 error ("%+qD declared ifunc after being used", decl);
2322 }
2323 }
2324
2325
2326 return NULL_TREE;
2327 }
2328
2329 /* Handle an "alias" or "ifunc" attribute; arguments as in
2330 struct attribute_spec.handler. */
2331
2332 static tree
2333 handle_ifunc_attribute (tree *node, tree name, tree args,
2334 int ARG_UNUSED (flags), bool *no_add_attrs)
2335 {
2336 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
2337 }
2338
2339 /* Handle an "alias" or "ifunc" attribute; arguments as in
2340 struct attribute_spec.handler. */
2341
2342 static tree
2343 handle_alias_attribute (tree *node, tree name, tree args,
2344 int ARG_UNUSED (flags), bool *no_add_attrs)
2345 {
2346 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2347 }
2348
2349 /* Handle the "copy" attribute NAME by copying the set of attributes
2350 from the symbol referenced by ARGS to the declaration of *NODE. */
2351
2352 static tree
2353 handle_copy_attribute (tree *node, tree name, tree args,
2354 int flags, bool *no_add_attrs)
2355 {
2356 /* Do not apply the copy attribute itself. It serves no purpose
2357 other than to copy other attributes. */
2358 *no_add_attrs = true;
2359
2360 tree decl = *node;
2361
2362 tree ref = TREE_VALUE (args);
2363 if (ref == error_mark_node)
2364 return NULL_TREE;
2365
2366 if (TREE_CODE (ref) == STRING_CST)
2367 {
2368 /* Explicitly handle this case since using a string literal
2369 as an argument is a likely mistake. */
2370 error_at (DECL_SOURCE_LOCATION (decl),
2371 "%qE attribute argument cannot be a string",
2372 name);
2373 return NULL_TREE;
2374 }
2375
2376 if (CONSTANT_CLASS_P (ref)
2377 && (INTEGRAL_TYPE_P (TREE_TYPE (ref))
2378 || FLOAT_TYPE_P (TREE_TYPE (ref))))
2379 {
2380 /* Similar to the string case, since some function attributes
2381 accept literal numbers as arguments (e.g., alloc_size or
2382 nonnull) using one here is a likely mistake. */
2383 error_at (DECL_SOURCE_LOCATION (decl),
2384 "%qE attribute argument cannot be a constant arithmetic "
2385 "expression",
2386 name);
2387 return NULL_TREE;
2388 }
2389
2390 if (ref == node[1])
2391 {
2392 /* Another possible mistake (but indirect self-references aren't
2393 and diagnosed and shouldn't be). */
2394 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2395 "%qE attribute ignored on a redeclaration "
2396 "of the referenced symbol",
2397 name))
2398 inform (DECL_SOURCE_LOCATION (node[1]),
2399 "previous declaration here");
2400 return NULL_TREE;
2401 }
2402
2403 /* Consider address-of expressions in the attribute argument
2404 as requests to copy from the referenced entity. For constant
2405 expressions, consider those to be requests to copy from their
2406 type, such as in:
2407 struct __attribute__ (copy ((struct T *)0)) U { ... };
2408 which copies type attributes from struct T to the declaration
2409 of struct U. */
2410 if (TREE_CODE (ref) == ADDR_EXPR)
2411 ref = TREE_OPERAND (ref, 0);
2412 else if (CONSTANT_CLASS_P (ref))
2413 ref = TREE_TYPE (ref);
2414
2415 if (DECL_P (decl))
2416 {
2417 if ((VAR_P (decl)
2418 && (TREE_CODE (ref) == FUNCTION_DECL
2419 || (EXPR_P (ref)
2420 && POINTER_TYPE_P (TREE_TYPE (ref))
2421 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (ref))))))
2422 || (TREE_CODE (decl) == FUNCTION_DECL
2423 && (VAR_P (ref)
2424 || (EXPR_P (ref)
2425 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (ref))))))
2426 {
2427 /* It makes no sense to try to copy function attributes
2428 to a variable, or variable attributes to a function. */
2429 if (warning (OPT_Wattributes,
2430 "%qE attribute ignored on a declaration of "
2431 "a different kind than referenced symbol",
2432 name)
2433 && DECL_P (ref))
2434 inform (DECL_SOURCE_LOCATION (ref),
2435 "symbol %qD referenced by %qD declared here", ref, decl);
2436 return NULL_TREE;
2437 }
2438
2439 tree attrs = NULL_TREE;
2440 if (DECL_P (ref))
2441 attrs = DECL_ATTRIBUTES (ref);
2442 else if (TYPE_P (ref))
2443 attrs = TYPE_ATTRIBUTES (ref);
2444
2445 /* Copy decl attributes from REF to DECL. */
2446 for (tree at = attrs; at; at = TREE_CHAIN (at))
2447 {
2448 /* Avoid copying attributes that affect a symbol linkage,
2449 inlining, or visibility since those in all likelihood
2450 only apply to the target.
2451 FIXME: make it possible to specify which attributes to
2452 copy or not to copy in the copy attribute itself. */
2453 tree atname = get_attribute_name (at);
2454 if (is_attribute_p ("alias", atname)
2455 || is_attribute_p ("always_inline", atname)
2456 || is_attribute_p ("gnu_inline", atname)
2457 || is_attribute_p ("ifunc", atname)
2458 || is_attribute_p ("noinline", atname)
2459 || is_attribute_p ("visibility", atname)
2460 || is_attribute_p ("weak", atname)
2461 || is_attribute_p ("weakref", atname))
2462 continue;
2463
2464 /* Attribute leaf only applies to extern functions.
2465 Avoid copying it to static ones. */
2466 if (!TREE_PUBLIC (decl)
2467 && is_attribute_p ("leaf", atname))
2468 continue;
2469
2470 tree atargs = TREE_VALUE (at);
2471 /* Create a copy of just the one attribute ar AT, including
2472 its argumentsm and add it to DECL. */
2473 tree attr = tree_cons (atname, copy_list (atargs), NULL_TREE);
2474 decl_attributes (node, attr, flags, ref);
2475 }
2476
2477 /* Proceed to copy type attributes below. */
2478 }
2479 else if (!TYPE_P (decl))
2480 {
2481 error_at (DECL_SOURCE_LOCATION (decl),
2482 "%qE attribute must apply to a declaration",
2483 name);
2484 return NULL_TREE;
2485 }
2486
2487 /* A function declared with attribute nothrow has the attribute
2488 attached to it, but a C++ throw() function does not. */
2489 if (TREE_NOTHROW (ref))
2490 TREE_NOTHROW (decl) = true;
2491
2492 /* Similarly, a function declared with attribute noreturn has it
2493 attached on to it, but a C11 _Noreturn function does not. */
2494 tree reftype = ref;
2495 if (DECL_P (ref)
2496 && TREE_THIS_VOLATILE (ref)
2497 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype)))
2498 TREE_THIS_VOLATILE (decl) = true;
2499
2500 if (DECL_P (ref) || EXPR_P (ref))
2501 reftype = TREE_TYPE (ref);
2502
2503 if (POINTER_TYPE_P (reftype))
2504 reftype = TREE_TYPE (reftype);
2505
2506 if (!TYPE_P (reftype))
2507 return NULL_TREE;
2508
2509 tree attrs = TYPE_ATTRIBUTES (reftype);
2510
2511 /* Copy type attributes from REF to DECL. */
2512 for (tree at = attrs; at; at = TREE_CHAIN (at))
2513 decl_attributes (node, at, flags, ref);
2514
2515 return NULL_TREE;
2516 }
2517
2518 /* Handle a "weakref" attribute; arguments as in struct
2519 attribute_spec.handler. */
2520
2521 static tree
2522 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2523 int flags, bool *no_add_attrs)
2524 {
2525 tree attr = NULL_TREE;
2526
2527 /* We must ignore the attribute when it is associated with
2528 local-scoped decls, since attribute alias is ignored and many
2529 such symbols do not even have a DECL_WEAK field. */
2530 if (decl_function_context (*node)
2531 || current_function_decl
2532 || !VAR_OR_FUNCTION_DECL_P (*node))
2533 {
2534 warning (OPT_Wattributes, "%qE attribute ignored", name);
2535 *no_add_attrs = true;
2536 return NULL_TREE;
2537 }
2538
2539 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2540 {
2541 error ("indirect function %q+D cannot be declared weakref", *node);
2542 *no_add_attrs = true;
2543 return NULL_TREE;
2544 }
2545
2546 /* The idea here is that `weakref("name")' mutates into `weakref,
2547 alias("name")', and weakref without arguments, in turn,
2548 implicitly adds weak. */
2549
2550 if (args)
2551 {
2552 attr = tree_cons (get_identifier ("alias"), args, attr);
2553 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2554
2555 *no_add_attrs = true;
2556
2557 decl_attributes (node, attr, flags);
2558 }
2559 else
2560 {
2561 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2562 error_at (DECL_SOURCE_LOCATION (*node),
2563 "weakref attribute must appear before alias attribute");
2564
2565 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2566 and that isn't supported; and because it wants to add it to
2567 the list of weak decls, which isn't helpful. */
2568 DECL_WEAK (*node) = 1;
2569 }
2570
2571 if (decl_in_symtab_p (*node))
2572 {
2573 struct symtab_node *n = symtab_node::get (*node);
2574 if (n && n->refuse_visibility_changes)
2575 error ("%+qD declared weakref after being used", *node);
2576 }
2577
2578 return NULL_TREE;
2579 }
2580
2581 /* Handle an "visibility" attribute; arguments as in
2582 struct attribute_spec.handler. */
2583
2584 static tree
2585 handle_visibility_attribute (tree *node, tree name, tree args,
2586 int ARG_UNUSED (flags),
2587 bool *ARG_UNUSED (no_add_attrs))
2588 {
2589 tree decl = *node;
2590 tree id = TREE_VALUE (args);
2591 enum symbol_visibility vis;
2592
2593 if (TYPE_P (*node))
2594 {
2595 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2596 /* OK */;
2597 else if (!RECORD_OR_UNION_TYPE_P (*node))
2598 {
2599 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2600 name);
2601 return NULL_TREE;
2602 }
2603 else if (TYPE_FIELDS (*node))
2604 {
2605 error ("%qE attribute ignored because %qT is already defined",
2606 name, *node);
2607 return NULL_TREE;
2608 }
2609 }
2610 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2611 {
2612 warning (OPT_Wattributes, "%qE attribute ignored", name);
2613 return NULL_TREE;
2614 }
2615
2616 if (TREE_CODE (id) != STRING_CST)
2617 {
2618 error ("visibility argument not a string");
2619 return NULL_TREE;
2620 }
2621
2622 /* If this is a type, set the visibility on the type decl. */
2623 if (TYPE_P (decl))
2624 {
2625 decl = TYPE_NAME (decl);
2626 if (!decl)
2627 return NULL_TREE;
2628 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2629 {
2630 warning (OPT_Wattributes, "%qE attribute ignored on types",
2631 name);
2632 return NULL_TREE;
2633 }
2634 }
2635
2636 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2637 vis = VISIBILITY_DEFAULT;
2638 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2639 vis = VISIBILITY_INTERNAL;
2640 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2641 vis = VISIBILITY_HIDDEN;
2642 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2643 vis = VISIBILITY_PROTECTED;
2644 else
2645 {
2646 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2647 vis = VISIBILITY_DEFAULT;
2648 }
2649
2650 if (DECL_VISIBILITY_SPECIFIED (decl)
2651 && vis != DECL_VISIBILITY (decl))
2652 {
2653 tree attributes = (TYPE_P (*node)
2654 ? TYPE_ATTRIBUTES (*node)
2655 : DECL_ATTRIBUTES (decl));
2656 if (lookup_attribute ("visibility", attributes))
2657 error ("%qD redeclared with different visibility", decl);
2658 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2659 && lookup_attribute ("dllimport", attributes))
2660 error ("%qD was declared %qs which implies default visibility",
2661 decl, "dllimport");
2662 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2663 && lookup_attribute ("dllexport", attributes))
2664 error ("%qD was declared %qs which implies default visibility",
2665 decl, "dllexport");
2666 }
2667
2668 DECL_VISIBILITY (decl) = vis;
2669 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2670
2671 /* Go ahead and attach the attribute to the node as well. This is needed
2672 so we can determine whether we have VISIBILITY_DEFAULT because the
2673 visibility was not specified, or because it was explicitly overridden
2674 from the containing scope. */
2675
2676 return NULL_TREE;
2677 }
2678
2679 /* Handle an "tls_model" attribute; arguments as in
2680 struct attribute_spec.handler. */
2681
2682 static tree
2683 handle_tls_model_attribute (tree *node, tree name, tree args,
2684 int ARG_UNUSED (flags),
2685 bool *ARG_UNUSED (no_add_attrs))
2686 {
2687 tree id;
2688 tree decl = *node;
2689 enum tls_model kind;
2690
2691 if (!VAR_P (decl))
2692 {
2693 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
2694 "is not a variable",
2695 name, decl);
2696 return NULL_TREE;
2697 }
2698
2699 if (!DECL_THREAD_LOCAL_P (decl))
2700 {
2701 warning (OPT_Wattributes, "%qE attribute ignored because %qD does "
2702 "not have thread storage duration", name, decl);
2703 return NULL_TREE;
2704 }
2705
2706 kind = DECL_TLS_MODEL (decl);
2707 id = TREE_VALUE (args);
2708 if (TREE_CODE (id) != STRING_CST)
2709 {
2710 error ("%qE argument not a string", name);
2711 return NULL_TREE;
2712 }
2713
2714 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2715 kind = TLS_MODEL_LOCAL_EXEC;
2716 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2717 kind = TLS_MODEL_INITIAL_EXEC;
2718 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2719 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2720 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2721 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2722 else
2723 error ("%qE argument must be one of %qs, %qs, %qs, or %qs",
2724 name,
2725 "local-exec", "initial-exec", "local-dynamic", "global-dynamic");
2726
2727 set_decl_tls_model (decl, kind);
2728 return NULL_TREE;
2729 }
2730
2731 /* Handle a "no_instrument_function" attribute; arguments as in
2732 struct attribute_spec.handler. */
2733
2734 static tree
2735 handle_no_instrument_function_attribute (tree *node, tree name,
2736 tree ARG_UNUSED (args),
2737 int ARG_UNUSED (flags),
2738 bool *no_add_attrs)
2739 {
2740 tree decl = *node;
2741
2742 if (TREE_CODE (decl) != FUNCTION_DECL)
2743 {
2744 error_at (DECL_SOURCE_LOCATION (decl),
2745 "%qE attribute applies only to functions", name);
2746 *no_add_attrs = true;
2747 }
2748 else
2749 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2750
2751 return NULL_TREE;
2752 }
2753
2754 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2755 struct attribute_spec.handler. */
2756
2757 static tree
2758 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2759 int, bool *no_add_attrs)
2760 {
2761 if (TREE_CODE (*node) != FUNCTION_DECL)
2762 {
2763 warning (OPT_Wattributes, "%qE attribute ignored", name);
2764 *no_add_attrs = true;
2765 }
2766
2767 return NULL_TREE;
2768 }
2769
2770 /* Handle a "malloc" attribute; arguments as in
2771 struct attribute_spec.handler. */
2772
2773 static tree
2774 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2775 int ARG_UNUSED (flags), bool *no_add_attrs)
2776 {
2777 if (TREE_CODE (*node) == FUNCTION_DECL
2778 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2779 DECL_IS_MALLOC (*node) = 1;
2780 else
2781 {
2782 warning (OPT_Wattributes, "%qE attribute ignored", name);
2783 *no_add_attrs = true;
2784 }
2785
2786 return NULL_TREE;
2787 }
2788
2789 /* Handle a "alloc_size" attribute; arguments as in
2790 struct attribute_spec.handler. */
2791
2792 static tree
2793 handle_alloc_size_attribute (tree *node, tree name, tree args,
2794 int ARG_UNUSED (flags), bool *no_add_attrs)
2795 {
2796 tree decl = *node;
2797 tree rettype = TREE_TYPE (decl);
2798 if (!POINTER_TYPE_P (rettype))
2799 {
2800 warning (OPT_Wattributes,
2801 "%qE attribute ignored on a function returning %qT",
2802 name, rettype);
2803 *no_add_attrs = true;
2804 return NULL_TREE;
2805 }
2806
2807 for (int i = 1; args; ++i)
2808 {
2809 tree pos = TREE_VALUE (args);
2810 /* NEXT is null when the attribute includes just one argument.
2811 That's used to tell positional_argument to avoid mentioning
2812 the argument number in diagnostics (since there's just one
2813 mentioning it is unnecessary and coule be confusing). */
2814 tree next = TREE_CHAIN (args);
2815 if (tree val = positional_argument (decl, name, pos, INTEGER_TYPE,
2816 next || i > 1 ? i : 0))
2817 TREE_VALUE (args) = val;
2818 else
2819 {
2820 *no_add_attrs = true;
2821 break;
2822 }
2823
2824 args = next;
2825 }
2826
2827 return NULL_TREE;
2828 }
2829
2830 /* Handle a "alloc_align" attribute; arguments as in
2831 struct attribute_spec.handler. */
2832
2833 static tree
2834 handle_alloc_align_attribute (tree *node, tree name, tree args, int,
2835 bool *no_add_attrs)
2836 {
2837 tree decl = *node;
2838 tree rettype = TREE_TYPE (decl);
2839 if (!POINTER_TYPE_P (rettype))
2840 {
2841 warning (OPT_Wattributes,
2842 "%qE attribute ignored on a function returning %qT",
2843 name, rettype);
2844 *no_add_attrs = true;
2845 return NULL_TREE;
2846 }
2847
2848 if (!positional_argument (*node, name, TREE_VALUE (args), INTEGER_TYPE))
2849 *no_add_attrs = true;
2850
2851 return NULL_TREE;
2852 }
2853
2854 /* Handle a "assume_aligned" attribute; arguments as in
2855 struct attribute_spec.handler. */
2856
2857 static tree
2858 handle_assume_aligned_attribute (tree *node, tree name, tree args, int,
2859 bool *no_add_attrs)
2860 {
2861 tree decl = *node;
2862 tree rettype = TREE_TYPE (decl);
2863 if (TREE_CODE (rettype) != POINTER_TYPE)
2864 {
2865 warning (OPT_Wattributes,
2866 "%qE attribute ignored on a function returning %qT",
2867 name, rettype);
2868 *no_add_attrs = true;
2869 return NULL_TREE;
2870 }
2871
2872 /* The alignment specified by the first argument. */
2873 tree align = NULL_TREE;
2874
2875 for (; args; args = TREE_CHAIN (args))
2876 {
2877 tree val = TREE_VALUE (args);
2878 if (val && TREE_CODE (val) != IDENTIFIER_NODE
2879 && TREE_CODE (val) != FUNCTION_DECL)
2880 val = default_conversion (val);
2881
2882 if (!tree_fits_shwi_p (val))
2883 {
2884 warning (OPT_Wattributes,
2885 "%qE attribute %E is not an integer constant",
2886 name, val);
2887 *no_add_attrs = true;
2888 return NULL_TREE;
2889 }
2890
2891 if (!align)
2892 {
2893 /* Validate and save the alignment. */
2894 if (!integer_pow2p (val))
2895 {
2896 warning (OPT_Wattributes,
2897 "%qE attribute argument %E is not a power of 2",
2898 name, val);
2899 *no_add_attrs = true;
2900 return NULL_TREE;
2901 }
2902
2903 align = val;
2904 }
2905 else if (tree_int_cst_sgn (val) < 0 || tree_int_cst_le (align, val))
2906 {
2907 /* The misalignment specified by the second argument
2908 must be non-negative and less than the alignment. */
2909 warning (OPT_Wattributes,
2910 "%qE attribute argument %E is not in the range [0, %E)",
2911 name, val, align);
2912 *no_add_attrs = true;
2913 return NULL_TREE;
2914 }
2915 }
2916 return NULL_TREE;
2917 }
2918
2919 /* Handle a "fn spec" attribute; arguments as in
2920 struct attribute_spec.handler. */
2921
2922 static tree
2923 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2924 tree args, int ARG_UNUSED (flags),
2925 bool *no_add_attrs ATTRIBUTE_UNUSED)
2926 {
2927 gcc_assert (args
2928 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2929 && !TREE_CHAIN (args));
2930 return NULL_TREE;
2931 }
2932
2933 /* Handle a "warn_unused" attribute; arguments as in
2934 struct attribute_spec.handler. */
2935
2936 static tree
2937 handle_warn_unused_attribute (tree *node, tree name,
2938 tree args ATTRIBUTE_UNUSED,
2939 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2940 {
2941 if (TYPE_P (*node))
2942 /* Do nothing else, just set the attribute. We'll get at
2943 it later with lookup_attribute. */
2944 ;
2945 else
2946 {
2947 warning (OPT_Wattributes, "%qE attribute ignored", name);
2948 *no_add_attrs = true;
2949 }
2950
2951 return NULL_TREE;
2952 }
2953
2954 /* Handle an "omp declare simd" attribute; arguments as in
2955 struct attribute_spec.handler. */
2956
2957 static tree
2958 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2959 {
2960 return NULL_TREE;
2961 }
2962
2963 /* Handle a "simd" attribute. */
2964
2965 static tree
2966 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2967 {
2968 if (TREE_CODE (*node) == FUNCTION_DECL)
2969 {
2970 tree t = get_identifier ("omp declare simd");
2971 tree attr = NULL_TREE;
2972 if (args)
2973 {
2974 tree id = TREE_VALUE (args);
2975
2976 if (TREE_CODE (id) != STRING_CST)
2977 {
2978 error ("attribute %qE argument not a string", name);
2979 *no_add_attrs = true;
2980 return NULL_TREE;
2981 }
2982
2983 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2984 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2985 OMP_CLAUSE_NOTINBRANCH);
2986 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2987 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2988 OMP_CLAUSE_INBRANCH);
2989 else
2990 {
2991 error ("only %<inbranch%> and %<notinbranch%> flags are "
2992 "allowed for %<__simd__%> attribute");
2993 *no_add_attrs = true;
2994 return NULL_TREE;
2995 }
2996 }
2997
2998 DECL_ATTRIBUTES (*node)
2999 = tree_cons (t, build_tree_list (NULL_TREE, attr),
3000 DECL_ATTRIBUTES (*node));
3001 }
3002 else
3003 {
3004 warning (OPT_Wattributes, "%qE attribute ignored", name);
3005 *no_add_attrs = true;
3006 }
3007
3008 return NULL_TREE;
3009 }
3010
3011 /* Handle an "omp declare target" attribute; arguments as in
3012 struct attribute_spec.handler. */
3013
3014 static tree
3015 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
3016 {
3017 return NULL_TREE;
3018 }
3019
3020 /* Handle a "returns_twice" attribute; arguments as in
3021 struct attribute_spec.handler. */
3022
3023 static tree
3024 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3025 int ARG_UNUSED (flags), bool *no_add_attrs)
3026 {
3027 if (TREE_CODE (*node) == FUNCTION_DECL)
3028 DECL_IS_RETURNS_TWICE (*node) = 1;
3029 else
3030 {
3031 warning (OPT_Wattributes, "%qE attribute ignored", name);
3032 *no_add_attrs = true;
3033 }
3034
3035 return NULL_TREE;
3036 }
3037
3038 /* Handle a "no_limit_stack" attribute; arguments as in
3039 struct attribute_spec.handler. */
3040
3041 static tree
3042 handle_no_limit_stack_attribute (tree *node, tree name,
3043 tree ARG_UNUSED (args),
3044 int ARG_UNUSED (flags),
3045 bool *no_add_attrs)
3046 {
3047 tree decl = *node;
3048
3049 if (TREE_CODE (decl) != FUNCTION_DECL)
3050 {
3051 error_at (DECL_SOURCE_LOCATION (decl),
3052 "%qE attribute applies only to functions", name);
3053 *no_add_attrs = true;
3054 }
3055 else if (DECL_INITIAL (decl))
3056 {
3057 error_at (DECL_SOURCE_LOCATION (decl),
3058 "can%'t set %qE attribute after definition", name);
3059 *no_add_attrs = true;
3060 }
3061 else
3062 DECL_NO_LIMIT_STACK (decl) = 1;
3063
3064 return NULL_TREE;
3065 }
3066
3067 /* Handle a "pure" attribute; arguments as in
3068 struct attribute_spec.handler. */
3069
3070 static tree
3071 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3072 int ARG_UNUSED (flags), bool *no_add_attrs)
3073 {
3074 if (TREE_CODE (*node) == FUNCTION_DECL)
3075 {
3076 tree type = TREE_TYPE (*node);
3077 if (VOID_TYPE_P (TREE_TYPE (type)))
3078 warning (OPT_Wattributes, "%qE attribute on function "
3079 "returning %<void%>", name);
3080
3081 DECL_PURE_P (*node) = 1;
3082 /* ??? TODO: Support types. */
3083 }
3084 else
3085 {
3086 warning (OPT_Wattributes, "%qE attribute ignored", name);
3087 *no_add_attrs = true;
3088 }
3089
3090 return NULL_TREE;
3091 }
3092
3093 /* Digest an attribute list destined for a transactional memory statement.
3094 ALLOWED is the set of attributes that are allowed for this statement;
3095 return the attribute we parsed. Multiple attributes are never allowed. */
3096
3097 int
3098 parse_tm_stmt_attr (tree attrs, int allowed)
3099 {
3100 tree a_seen = NULL;
3101 int m_seen = 0;
3102
3103 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
3104 {
3105 tree a = TREE_PURPOSE (attrs);
3106 int m = 0;
3107
3108 if (is_attribute_p ("outer", a))
3109 m = TM_STMT_ATTR_OUTER;
3110
3111 if ((m & allowed) == 0)
3112 {
3113 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
3114 continue;
3115 }
3116
3117 if (m_seen == 0)
3118 {
3119 a_seen = a;
3120 m_seen = m;
3121 }
3122 else if (m_seen == m)
3123 warning (OPT_Wattributes, "%qE attribute duplicated", a);
3124 else
3125 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
3126 }
3127
3128 return m_seen;
3129 }
3130
3131 /* Transform a TM attribute name into a maskable integer and back.
3132 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
3133 to how the lack of an attribute is treated. */
3134
3135 int
3136 tm_attr_to_mask (tree attr)
3137 {
3138 if (attr == NULL)
3139 return 0;
3140 if (is_attribute_p ("transaction_safe", attr))
3141 return TM_ATTR_SAFE;
3142 if (is_attribute_p ("transaction_callable", attr))
3143 return TM_ATTR_CALLABLE;
3144 if (is_attribute_p ("transaction_pure", attr))
3145 return TM_ATTR_PURE;
3146 if (is_attribute_p ("transaction_unsafe", attr))
3147 return TM_ATTR_IRREVOCABLE;
3148 if (is_attribute_p ("transaction_may_cancel_outer", attr))
3149 return TM_ATTR_MAY_CANCEL_OUTER;
3150 return 0;
3151 }
3152
3153 tree
3154 tm_mask_to_attr (int mask)
3155 {
3156 const char *str;
3157 switch (mask)
3158 {
3159 case TM_ATTR_SAFE:
3160 str = "transaction_safe";
3161 break;
3162 case TM_ATTR_CALLABLE:
3163 str = "transaction_callable";
3164 break;
3165 case TM_ATTR_PURE:
3166 str = "transaction_pure";
3167 break;
3168 case TM_ATTR_IRREVOCABLE:
3169 str = "transaction_unsafe";
3170 break;
3171 case TM_ATTR_MAY_CANCEL_OUTER:
3172 str = "transaction_may_cancel_outer";
3173 break;
3174 default:
3175 gcc_unreachable ();
3176 }
3177 return get_identifier (str);
3178 }
3179
3180 /* Return the first TM attribute seen in LIST. */
3181
3182 tree
3183 find_tm_attribute (tree list)
3184 {
3185 for (; list ; list = TREE_CHAIN (list))
3186 {
3187 tree name = TREE_PURPOSE (list);
3188 if (tm_attr_to_mask (name) != 0)
3189 return name;
3190 }
3191 return NULL_TREE;
3192 }
3193
3194 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
3195 Here we accept only function types, and verify that none of the other
3196 function TM attributes are also applied. */
3197 /* ??? We need to accept class types for C++, but not C. This greatly
3198 complicates this function, since we can no longer rely on the extra
3199 processing given by function_type_required. */
3200
3201 static tree
3202 handle_tm_attribute (tree *node, tree name, tree args,
3203 int flags, bool *no_add_attrs)
3204 {
3205 /* Only one path adds the attribute; others don't. */
3206 *no_add_attrs = true;
3207
3208 switch (TREE_CODE (*node))
3209 {
3210 case RECORD_TYPE:
3211 case UNION_TYPE:
3212 /* Only tm_callable and tm_safe apply to classes. */
3213 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
3214 goto ignored;
3215 /* FALLTHRU */
3216
3217 case FUNCTION_TYPE:
3218 case METHOD_TYPE:
3219 {
3220 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
3221 if (old_name == name)
3222 ;
3223 else if (old_name != NULL_TREE)
3224 error ("type was previously declared %qE", old_name);
3225 else
3226 *no_add_attrs = false;
3227 }
3228 break;
3229
3230 case FUNCTION_DECL:
3231 {
3232 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
3233 want to set transaction_safe on the type. */
3234 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
3235 if (!TYPE_P (DECL_CONTEXT (*node)))
3236 error_at (DECL_SOURCE_LOCATION (*node),
3237 "%<transaction_safe_dynamic%> may only be specified for "
3238 "a virtual function");
3239 *no_add_attrs = false;
3240 decl_attributes (&TREE_TYPE (*node),
3241 build_tree_list (get_identifier ("transaction_safe"),
3242 NULL_TREE),
3243 0);
3244 break;
3245 }
3246
3247 case POINTER_TYPE:
3248 {
3249 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
3250 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
3251 {
3252 tree fn_tmp = TREE_TYPE (*node);
3253 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
3254 *node = build_pointer_type (fn_tmp);
3255 break;
3256 }
3257 }
3258 /* FALLTHRU */
3259
3260 default:
3261 /* If a function is next, pass it on to be tried next. */
3262 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
3263 return tree_cons (name, args, NULL);
3264
3265 ignored:
3266 warning (OPT_Wattributes, "%qE attribute ignored", name);
3267 break;
3268 }
3269
3270 return NULL_TREE;
3271 }
3272
3273 /* Handle the TM_WRAP attribute; arguments as in
3274 struct attribute_spec.handler. */
3275
3276 static tree
3277 handle_tm_wrap_attribute (tree *node, tree name, tree args,
3278 int ARG_UNUSED (flags), bool *no_add_attrs)
3279 {
3280 tree decl = *node;
3281
3282 /* We don't need the attribute even on success, since we
3283 record the entry in an external table. */
3284 *no_add_attrs = true;
3285
3286 if (TREE_CODE (decl) != FUNCTION_DECL)
3287 warning (OPT_Wattributes, "%qE attribute ignored", name);
3288 else
3289 {
3290 tree wrap_decl = TREE_VALUE (args);
3291 if (error_operand_p (wrap_decl))
3292 ;
3293 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
3294 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
3295 error ("%qE argument not an identifier", name);
3296 else
3297 {
3298 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
3299 wrap_decl = lookup_name (wrap_decl);
3300 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
3301 {
3302 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
3303 TREE_TYPE (wrap_decl)))
3304 record_tm_replacement (wrap_decl, decl);
3305 else
3306 error ("%qD is not compatible with %qD", wrap_decl, decl);
3307 }
3308 else
3309 error ("%qE argument is not a function", name);
3310 }
3311 }
3312
3313 return NULL_TREE;
3314 }
3315
3316 /* Ignore the given attribute. Used when this attribute may be usefully
3317 overridden by the target, but is not used generically. */
3318
3319 static tree
3320 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
3321 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3322 bool *no_add_attrs)
3323 {
3324 *no_add_attrs = true;
3325 return NULL_TREE;
3326 }
3327
3328 /* Handle a "no vops" attribute; arguments as in
3329 struct attribute_spec.handler. */
3330
3331 static tree
3332 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
3333 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3334 bool *ARG_UNUSED (no_add_attrs))
3335 {
3336 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
3337 DECL_IS_NOVOPS (*node) = 1;
3338 return NULL_TREE;
3339 }
3340
3341 /* Handle a "deprecated" attribute; arguments as in
3342 struct attribute_spec.handler. */
3343
3344 static tree
3345 handle_deprecated_attribute (tree *node, tree name,
3346 tree args, int flags,
3347 bool *no_add_attrs)
3348 {
3349 tree type = NULL_TREE;
3350 int warn = 0;
3351 tree what = NULL_TREE;
3352
3353 if (!args)
3354 *no_add_attrs = true;
3355 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
3356 {
3357 error ("deprecated message is not a string");
3358 *no_add_attrs = true;
3359 }
3360
3361 if (DECL_P (*node))
3362 {
3363 tree decl = *node;
3364 type = TREE_TYPE (decl);
3365
3366 if (TREE_CODE (decl) == TYPE_DECL
3367 || TREE_CODE (decl) == PARM_DECL
3368 || VAR_OR_FUNCTION_DECL_P (decl)
3369 || TREE_CODE (decl) == FIELD_DECL
3370 || TREE_CODE (decl) == CONST_DECL
3371 || objc_method_decl (TREE_CODE (decl)))
3372 TREE_DEPRECATED (decl) = 1;
3373 else
3374 warn = 1;
3375 }
3376 else if (TYPE_P (*node))
3377 {
3378 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3379 *node = build_variant_type_copy (*node);
3380 TREE_DEPRECATED (*node) = 1;
3381 type = *node;
3382 }
3383 else
3384 warn = 1;
3385
3386 if (warn)
3387 {
3388 *no_add_attrs = true;
3389 if (type && TYPE_NAME (type))
3390 {
3391 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3392 what = TYPE_NAME (*node);
3393 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3394 && DECL_NAME (TYPE_NAME (type)))
3395 what = DECL_NAME (TYPE_NAME (type));
3396 }
3397 if (what)
3398 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
3399 else
3400 warning (OPT_Wattributes, "%qE attribute ignored", name);
3401 }
3402
3403 return NULL_TREE;
3404 }
3405
3406 /* Return the "base" type from TYPE that is suitable to apply attribute
3407 vector_size to by stripping arrays, function types, etc. */
3408 static tree
3409 type_for_vector_size (tree type)
3410 {
3411 /* We need to provide for vector pointers, vector arrays, and
3412 functions returning vectors. For example:
3413
3414 __attribute__((vector_size(16))) short *foo;
3415
3416 In this case, the mode is SI, but the type being modified is
3417 HI, so we need to look further. */
3418
3419 while (POINTER_TYPE_P (type)
3420 || TREE_CODE (type) == FUNCTION_TYPE
3421 || TREE_CODE (type) == METHOD_TYPE
3422 || TREE_CODE (type) == ARRAY_TYPE
3423 || TREE_CODE (type) == OFFSET_TYPE)
3424 type = TREE_TYPE (type);
3425
3426 return type;
3427 }
3428
3429 /* Given TYPE, return the base type to which the vector_size attribute
3430 ATNAME with ARGS, when non-null, can be applied, if one exists.
3431 On success and when both ARGS and PTRNUNITS are non-null, set
3432 *PTRNUNINTS to the number of vector units. When PTRNUNITS is not
3433 null, issue a warning when the attribute argument is not constant
3434 and an error if there is no such type. Otherwise issue a warning
3435 in the latter case and return null. */
3436
3437 static tree
3438 type_valid_for_vector_size (tree type, tree atname, tree args,
3439 unsigned HOST_WIDE_INT *ptrnunits)
3440 {
3441 bool error_p = ptrnunits != NULL;
3442
3443 /* Get the mode of the type being modified. */
3444 machine_mode orig_mode = TYPE_MODE (type);
3445
3446 if ((!INTEGRAL_TYPE_P (type)
3447 && !SCALAR_FLOAT_TYPE_P (type)
3448 && !FIXED_POINT_TYPE_P (type))
3449 || (!SCALAR_FLOAT_MODE_P (orig_mode)
3450 && GET_MODE_CLASS (orig_mode) != MODE_INT
3451 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
3452 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
3453 || TREE_CODE (type) == BOOLEAN_TYPE)
3454 {
3455 if (error_p)
3456 error ("invalid vector type for attribute %qE", atname);
3457 else
3458 warning (OPT_Wattributes, "invalid vector type for attribute %qE",
3459 atname);
3460 return NULL_TREE;
3461 }
3462
3463 /* When no argument has been provided this is just a request to validate
3464 the type above. Return TYPE to indicate success. */
3465 if (!args)
3466 return type;
3467
3468 tree size = TREE_VALUE (args);
3469 if (size && TREE_CODE (size) != IDENTIFIER_NODE
3470 && TREE_CODE (size) != FUNCTION_DECL)
3471 size = default_conversion (size);
3472
3473 if (!tree_fits_uhwi_p (size))
3474 {
3475 /* FIXME: make the error message more informative. */
3476 if (error_p)
3477 warning (OPT_Wattributes, "%qE attribute ignored", atname);
3478 return NULL_TREE;
3479 }
3480
3481 unsigned HOST_WIDE_INT vecsize = tree_to_uhwi (size);
3482 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
3483 {
3484 if (error_p)
3485 error ("vector size not an integral multiple of component size");
3486 return NULL_TREE;
3487 }
3488
3489 if (vecsize == 0)
3490 {
3491 error ("zero vector size");
3492 return NULL;
3493 }
3494
3495 /* Calculate how many units fit in the vector. */
3496 unsigned HOST_WIDE_INT nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
3497 if (nunits & (nunits - 1))
3498 {
3499 if (error_p)
3500 error ("number of components of the vector not a power of two");
3501 else
3502 warning (OPT_Wattributes,
3503 "number of components of the vector not a power of two");
3504 return NULL_TREE;
3505 }
3506
3507 if (ptrnunits)
3508 *ptrnunits = nunits;
3509
3510 return type;
3511 }
3512
3513 /* Handle a "vector_size" attribute; arguments as in
3514 struct attribute_spec.handler. */
3515
3516 static tree
3517 handle_vector_size_attribute (tree *node, tree name, tree args,
3518 int ARG_UNUSED (flags),
3519 bool *no_add_attrs)
3520 {
3521 *no_add_attrs = true;
3522
3523 /* Determine the "base" type to apply the attribute to. */
3524 tree type = type_for_vector_size (*node);
3525
3526 /* Get the vector size (in bytes) and let the function compute
3527 the number of vector units. */
3528 unsigned HOST_WIDE_INT nunits;
3529 type = type_valid_for_vector_size (type, name, args, &nunits);
3530 if (!type)
3531 return NULL_TREE;
3532
3533 tree new_type = build_vector_type (type, nunits);
3534
3535 /* Build back pointers if needed. */
3536 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
3537
3538 return NULL_TREE;
3539 }
3540
3541 /* Handle the "nonnull" attribute. */
3542
3543 static tree
3544 handle_nonnull_attribute (tree *node, tree name,
3545 tree args, int ARG_UNUSED (flags),
3546 bool *no_add_attrs)
3547 {
3548 tree type = *node;
3549
3550 /* If no arguments are specified, all pointer arguments should be
3551 non-null. Verify a full prototype is given so that the arguments
3552 will have the correct types when we actually check them later.
3553 Avoid diagnosing type-generic built-ins since those have no
3554 prototype. */
3555 if (!args)
3556 {
3557 if (!prototype_p (type)
3558 && (!TYPE_ATTRIBUTES (type)
3559 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
3560 {
3561 error ("nonnull attribute without arguments on a non-prototype");
3562 *no_add_attrs = true;
3563 }
3564 return NULL_TREE;
3565 }
3566
3567 for (int i = 1; args; ++i)
3568 {
3569 tree pos = TREE_VALUE (args);
3570 /* NEXT is null when the attribute includes just one argument.
3571 That's used to tell positional_argument to avoid mentioning
3572 the argument number in diagnostics (since there's just one
3573 mentioning it is unnecessary and coule be confusing). */
3574 tree next = TREE_CHAIN (args);
3575 if (tree val = positional_argument (type, name, pos, POINTER_TYPE,
3576 next || i > 1 ? i : 0))
3577 TREE_VALUE (args) = val;
3578 else
3579 {
3580 *no_add_attrs = true;
3581 break;
3582 }
3583 args = next;
3584 }
3585
3586 return NULL_TREE;
3587 }
3588
3589 /* Handle the "nonstring" variable attribute. */
3590
3591 static tree
3592 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3593 int ARG_UNUSED (flags), bool *no_add_attrs)
3594 {
3595 gcc_assert (!args);
3596 tree_code code = TREE_CODE (*node);
3597
3598 if (VAR_P (*node)
3599 || code == FIELD_DECL
3600 || code == PARM_DECL)
3601 {
3602 tree type = TREE_TYPE (*node);
3603
3604 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3605 {
3606 /* Accept the attribute on arrays and pointers to all three
3607 narrow character types. */
3608 tree eltype = TREE_TYPE (type);
3609 eltype = TYPE_MAIN_VARIANT (eltype);
3610 if (eltype == char_type_node
3611 || eltype == signed_char_type_node
3612 || eltype == unsigned_char_type_node)
3613 return NULL_TREE;
3614 }
3615
3616 warning (OPT_Wattributes,
3617 "%qE attribute ignored on objects of type %qT",
3618 name, type);
3619 *no_add_attrs = true;
3620 return NULL_TREE;
3621 }
3622
3623 if (code == FUNCTION_DECL)
3624 warning (OPT_Wattributes,
3625 "%qE attribute does not apply to functions", name);
3626 else if (code == TYPE_DECL)
3627 warning (OPT_Wattributes,
3628 "%qE attribute does not apply to types", name);
3629 else
3630 warning (OPT_Wattributes, "%qE attribute ignored", name);
3631
3632 *no_add_attrs = true;
3633 return NULL_TREE;
3634 }
3635
3636 /* Handle a "nothrow" attribute; arguments as in
3637 struct attribute_spec.handler. */
3638
3639 static tree
3640 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3641 int ARG_UNUSED (flags), bool *no_add_attrs)
3642 {
3643 if (TREE_CODE (*node) == FUNCTION_DECL)
3644 TREE_NOTHROW (*node) = 1;
3645 /* ??? TODO: Support types. */
3646 else
3647 {
3648 warning (OPT_Wattributes, "%qE attribute ignored", name);
3649 *no_add_attrs = true;
3650 }
3651
3652 return NULL_TREE;
3653 }
3654
3655 /* Handle a "cleanup" attribute; arguments as in
3656 struct attribute_spec.handler. */
3657
3658 static tree
3659 handle_cleanup_attribute (tree *node, tree name, tree args,
3660 int ARG_UNUSED (flags), bool *no_add_attrs)
3661 {
3662 tree decl = *node;
3663 tree cleanup_id, cleanup_decl;
3664
3665 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3666 for global destructors in C++. This requires infrastructure that
3667 we don't have generically at the moment. It's also not a feature
3668 we'd be missing too much, since we do have attribute constructor. */
3669 if (!VAR_P (decl) || TREE_STATIC (decl))
3670 {
3671 warning (OPT_Wattributes, "%qE attribute ignored", name);
3672 *no_add_attrs = true;
3673 return NULL_TREE;
3674 }
3675
3676 /* Verify that the argument is a function in scope. */
3677 /* ??? We could support pointers to functions here as well, if
3678 that was considered desirable. */
3679 cleanup_id = TREE_VALUE (args);
3680 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3681 {
3682 error ("cleanup argument not an identifier");
3683 *no_add_attrs = true;
3684 return NULL_TREE;
3685 }
3686 cleanup_decl = lookup_name (cleanup_id);
3687 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3688 {
3689 error ("cleanup argument not a function");
3690 *no_add_attrs = true;
3691 return NULL_TREE;
3692 }
3693
3694 /* That the function has proper type is checked with the
3695 eventual call to build_function_call. */
3696
3697 return NULL_TREE;
3698 }
3699
3700 /* Handle a "warn_unused_result" attribute. No special handling. */
3701
3702 static tree
3703 handle_warn_unused_result_attribute (tree *node, tree name,
3704 tree ARG_UNUSED (args),
3705 int ARG_UNUSED (flags), bool *no_add_attrs)
3706 {
3707 /* Ignore the attribute for functions not returning any value. */
3708 if (VOID_TYPE_P (TREE_TYPE (*node)))
3709 {
3710 warning (OPT_Wattributes, "%qE attribute ignored", name);
3711 *no_add_attrs = true;
3712 }
3713
3714 return NULL_TREE;
3715 }
3716
3717 /* Handle a "sentinel" attribute. */
3718
3719 static tree
3720 handle_sentinel_attribute (tree *node, tree name, tree args,
3721 int ARG_UNUSED (flags), bool *no_add_attrs)
3722 {
3723 if (!prototype_p (*node))
3724 {
3725 warning (OPT_Wattributes,
3726 "%qE attribute requires prototypes with named arguments", name);
3727 *no_add_attrs = true;
3728 }
3729 else
3730 {
3731 if (!stdarg_p (*node))
3732 {
3733 warning (OPT_Wattributes,
3734 "%qE attribute only applies to variadic functions", name);
3735 *no_add_attrs = true;
3736 }
3737 }
3738
3739 if (args)
3740 {
3741 tree position = TREE_VALUE (args);
3742 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3743 && TREE_CODE (position) != FUNCTION_DECL)
3744 position = default_conversion (position);
3745
3746 if (TREE_CODE (position) != INTEGER_CST
3747 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3748 {
3749 warning (OPT_Wattributes,
3750 "requested position is not an integer constant");
3751 *no_add_attrs = true;
3752 }
3753 else
3754 {
3755 if (tree_int_cst_lt (position, integer_zero_node))
3756 {
3757 warning (OPT_Wattributes,
3758 "requested position is less than zero");
3759 *no_add_attrs = true;
3760 }
3761 }
3762 }
3763
3764 return NULL_TREE;
3765 }
3766
3767 /* Handle a "type_generic" attribute. */
3768
3769 static tree
3770 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3771 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3772 bool * ARG_UNUSED (no_add_attrs))
3773 {
3774 /* Ensure we have a function type. */
3775 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3776
3777 /* Ensure we have a variadic function. */
3778 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3779
3780 return NULL_TREE;
3781 }
3782
3783 /* Handle a "target" attribute. */
3784
3785 static tree
3786 handle_target_attribute (tree *node, tree name, tree args, int flags,
3787 bool *no_add_attrs)
3788 {
3789 /* Ensure we have a function type. */
3790 if (TREE_CODE (*node) != FUNCTION_DECL)
3791 {
3792 warning (OPT_Wattributes, "%qE attribute ignored", name);
3793 *no_add_attrs = true;
3794 }
3795 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3796 {
3797 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3798 "with %qs attribute", name, "target_clones");
3799 *no_add_attrs = true;
3800 }
3801 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3802 flags))
3803 *no_add_attrs = true;
3804
3805 /* Check that there's no empty string in values of the attribute. */
3806 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3807 {
3808 tree value = TREE_VALUE (t);
3809 if (TREE_CODE (value) == STRING_CST
3810 && TREE_STRING_LENGTH (value) == 1
3811 && TREE_STRING_POINTER (value)[0] == '\0')
3812 {
3813 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3814 *no_add_attrs = true;
3815 }
3816 }
3817
3818 return NULL_TREE;
3819 }
3820
3821 /* Handle a "target_clones" attribute. */
3822
3823 static tree
3824 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3825 int ARG_UNUSED (flags), bool *no_add_attrs)
3826 {
3827 /* Ensure we have a function type. */
3828 if (TREE_CODE (*node) == FUNCTION_DECL)
3829 {
3830 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3831 {
3832 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3833 "with %qs attribute", name, "always_inline");
3834 *no_add_attrs = true;
3835 }
3836 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3837 {
3838 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3839 "with %qs attribute", name, "target");
3840 *no_add_attrs = true;
3841 }
3842 else
3843 /* Do not inline functions with multiple clone targets. */
3844 DECL_UNINLINABLE (*node) = 1;
3845 }
3846 else
3847 {
3848 warning (OPT_Wattributes, "%qE attribute ignored", name);
3849 *no_add_attrs = true;
3850 }
3851 return NULL_TREE;
3852 }
3853
3854 /* For handling "optimize" attribute. arguments as in
3855 struct attribute_spec.handler. */
3856
3857 static tree
3858 handle_optimize_attribute (tree *node, tree name, tree args,
3859 int ARG_UNUSED (flags), bool *no_add_attrs)
3860 {
3861 /* Ensure we have a function type. */
3862 if (TREE_CODE (*node) != FUNCTION_DECL)
3863 {
3864 warning (OPT_Wattributes, "%qE attribute ignored", name);
3865 *no_add_attrs = true;
3866 }
3867 else
3868 {
3869 struct cl_optimization cur_opts;
3870 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3871
3872 /* Save current options. */
3873 cl_optimization_save (&cur_opts, &global_options);
3874
3875 /* If we previously had some optimization options, use them as the
3876 default. */
3877 if (old_opts)
3878 cl_optimization_restore (&global_options,
3879 TREE_OPTIMIZATION (old_opts));
3880
3881 /* Parse options, and update the vector. */
3882 parse_optimize_options (args, true);
3883 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3884 = build_optimization_node (&global_options);
3885
3886 /* Restore current options. */
3887 cl_optimization_restore (&global_options, &cur_opts);
3888 }
3889
3890 return NULL_TREE;
3891 }
3892
3893 /* Handle a "no_split_stack" attribute. */
3894
3895 static tree
3896 handle_no_split_stack_attribute (tree *node, tree name,
3897 tree ARG_UNUSED (args),
3898 int ARG_UNUSED (flags),
3899 bool *no_add_attrs)
3900 {
3901 tree decl = *node;
3902
3903 if (TREE_CODE (decl) != FUNCTION_DECL)
3904 {
3905 error_at (DECL_SOURCE_LOCATION (decl),
3906 "%qE attribute applies only to functions", name);
3907 *no_add_attrs = true;
3908 }
3909 else if (DECL_INITIAL (decl))
3910 {
3911 error_at (DECL_SOURCE_LOCATION (decl),
3912 "can%'t set %qE attribute after definition", name);
3913 *no_add_attrs = true;
3914 }
3915
3916 return NULL_TREE;
3917 }
3918
3919 /* Handle a "returns_nonnull" attribute; arguments as in
3920 struct attribute_spec.handler. */
3921
3922 static tree
3923 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3924 bool *no_add_attrs)
3925 {
3926 // Even without a prototype we still have a return type we can check.
3927 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3928 {
3929 error ("returns_nonnull attribute on a function not returning a pointer");
3930 *no_add_attrs = true;
3931 }
3932 return NULL_TREE;
3933 }
3934
3935 /* Handle a "designated_init" attribute; arguments as in
3936 struct attribute_spec.handler. */
3937
3938 static tree
3939 handle_designated_init_attribute (tree *node, tree name, tree, int,
3940 bool *no_add_attrs)
3941 {
3942 if (TREE_CODE (*node) != RECORD_TYPE)
3943 {
3944 error ("%qE attribute is only valid on %<struct%> type", name);
3945 *no_add_attrs = true;
3946 }
3947 return NULL_TREE;
3948 }
3949
3950
3951 /* Handle a "fallthrough" attribute; arguments as in struct
3952 attribute_spec.handler. */
3953
3954 static tree
3955 handle_fallthrough_attribute (tree *, tree name, tree, int,
3956 bool *no_add_attrs)
3957 {
3958 warning (OPT_Wattributes, "%qE attribute ignored", name);
3959 *no_add_attrs = true;
3960 return NULL_TREE;
3961 }
3962
3963 static tree
3964 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3965 {
3966 /* Nothing to be done here. */
3967 return NULL_TREE;
3968 }
3969
3970 /* Attempt to partially validate a single attribute ATTR as if
3971 it were to be applied to an entity OPER. */
3972
3973 static bool
3974 validate_attribute (location_t atloc, tree oper, tree attr)
3975 {
3976 /* Determine whether the name of the attribute is valid
3977 and fail with an error if not. */
3978 tree atname = get_attribute_name (attr);
3979 if (!lookup_attribute_spec (atname))
3980 {
3981 if (atloc != UNKNOWN_LOCATION)
3982 error_at (atloc, "unknown attribute %qE", atname);
3983 return false;
3984 }
3985
3986 tree args = TREE_VALUE (attr);
3987 if (!args)
3988 return true;
3989
3990 /* FIXME: Do some validation. */
3991 const char *atstr = IDENTIFIER_POINTER (atname);
3992 if (!strcmp (atstr, "format"))
3993 return true;
3994
3995 /* Only when attribute arguments have been provided try to validate
3996 the whole thing. decl_attributes doesn't return an indication of
3997 success or failure so proceed regardless. */
3998 const char tmpname[] = "__builtin_has_attribute_tmp.";
3999 tree tmpid = get_identifier (tmpname);
4000 tree tmpdecl;
4001 if (!strcmp (atstr, "vector_size"))
4002 {
4003 tree type = TYPE_P (oper) ? oper : TREE_TYPE (oper);
4004 /* Check for function type here since type_for_vector_size
4005 strips it while looking for a function's return type. */
4006 if (FUNC_OR_METHOD_TYPE_P (type))
4007 {
4008 warning_at (atloc, OPT_Wattributes,
4009 "invalid operand type %qT for %qs", type, atstr);
4010 return false;
4011 }
4012
4013 type = type_for_vector_size (type);
4014 if (VECTOR_TYPE_P (type))
4015 type = TREE_TYPE (type);
4016 /* Avoid trying to apply attribute vector_size to OPER since
4017 it's overly restrictive. Simply make sure it has the right
4018 type. */
4019 return type_valid_for_vector_size (type, atname, args, NULL);
4020 }
4021
4022 if (TYPE_P (oper))
4023 tmpdecl = build_decl (atloc, TYPE_DECL, tmpid, oper);
4024 else
4025 tmpdecl = build_decl (atloc, TREE_CODE (oper), tmpid, TREE_TYPE (oper));
4026
4027 /* Temporarily clear CURRENT_FUNCTION_DECL to make decl_attributes
4028 believe the DECL declared above is at file scope. (See bug 87526.) */
4029 tree save_curfunc = current_function_decl;
4030 current_function_decl = NULL_TREE;
4031 if (DECL_P (tmpdecl))
4032 {
4033 if (DECL_P (oper))
4034 /* An alias cannot be a defintion so declare the symbol extern. */
4035 DECL_EXTERNAL (tmpdecl) = true;
4036 /* Attribute visibility only applies to symbols visible from other
4037 translation units so make it "public." */
4038 TREE_PUBLIC (tmpdecl) = TREE_PUBLIC (oper);
4039 }
4040 decl_attributes (&tmpdecl, attr, 0);
4041 current_function_decl = save_curfunc;
4042
4043 /* FIXME: Change decl_attributes to indicate success or failure (and
4044 parameterize it to avoid failing with errors). */
4045 return true;
4046 }
4047
4048 /* Return true if the DECL, EXPR, or TYPE t has been declared with
4049 attribute ATTR. For DECL, consider also its type. For EXPR,
4050 consider just its type. */
4051
4052 bool
4053 has_attribute (location_t atloc, tree t, tree attr, tree (*convert)(tree))
4054 {
4055 if (!attr || !t || t == error_mark_node)
4056 return false;
4057
4058 if (!validate_attribute (atloc, t, attr))
4059 return false;
4060
4061 tree type = NULL_TREE;
4062 tree expr = NULL_TREE;
4063 if (TYPE_P (t))
4064 type = t;
4065 else
4066 {
4067 do
4068 {
4069 /* Determine the array element/member declaration from
4070 an ARRAY/COMPONENT_REF. */
4071 STRIP_NOPS (t);
4072 tree_code code = TREE_CODE (t);
4073 if (code == ARRAY_REF)
4074 t = TREE_OPERAND (t, 0);
4075 else if (code == COMPONENT_REF)
4076 t = TREE_OPERAND (t, 1);
4077 else
4078 break;
4079 } while (true);
4080 expr = t;
4081 }
4082
4083 /* Set to true when an attribute is found in the referenced entity
4084 that matches the specified attribute. */
4085 bool found_match = false;
4086
4087 tree atname = get_attribute_name (attr);
4088 const char *namestr = IDENTIFIER_POINTER (atname);
4089
4090 /* Iterate once for a type and twice for a function or variable
4091 declaration: once for the DECL and the second time for its
4092 TYPE. */
4093 for (bool done = false; !found_match && !done; )
4094 {
4095 tree atlist;
4096 if (type)
4097 {
4098 if (type == error_mark_node)
4099 {
4100 /* This could be a label. FIXME: add support for labels. */
4101 warning_at (atloc, OPT_Wattributes,
4102 (TYPE_P (t)
4103 ? G_("%qs attribute not supported for %qT "
4104 "in %<__builtin_has_attribute%>")
4105 : G_("%qs attribute not supported for %qE "
4106 "in %<__builtin_has_attribute%>")),
4107 namestr, t);
4108 return false;
4109 }
4110
4111 /* Clear EXPR to prevent considering it again below. */
4112 atlist = TYPE_ATTRIBUTES (type);
4113 expr = NULL_TREE;
4114 done = true;
4115 }
4116 else if (DECL_P (expr))
4117 {
4118 /* Set TYPE to the DECL's type to process it on the next
4119 iteration. */
4120 atlist = DECL_ATTRIBUTES (expr);
4121 type = TREE_TYPE (expr);
4122 }
4123 else
4124 {
4125 atlist = TYPE_ATTRIBUTES (TREE_TYPE (expr));
4126 done = true;
4127 }
4128
4129 /* True when an attribute with the sought name (though not necessarily
4130 with the sought attributes) has been found on the attribute chain. */
4131 bool found_attr = false;
4132
4133 /* When clear, the first mismatched attribute argument results
4134 in failure. Otherwise, the first matched attribute argument
4135 results in success. */
4136 bool attr_nonnull = !strcmp ("nonnull", namestr);
4137 bool ignore_mismatches = attr_nonnull;
4138
4139 /* Iterate over the instances of the sought attribute on the DECL or
4140 TYPE (there may be multiple instances with different arguments). */
4141 for (; (atlist = lookup_attribute (namestr, atlist));
4142 found_attr = true, atlist = TREE_CHAIN (atlist))
4143 {
4144 /* If there are no arguments to match the result is true except
4145 for nonnull where the attribute with no arguments must match. */
4146 if (!TREE_VALUE (attr))
4147 return attr_nonnull ? !TREE_VALUE (atlist) : true;
4148
4149 /* Attribute nonnull with no arguments subsumes all values of
4150 the attribute. FIXME: This is overly broad since it only
4151 applies to pointer arguments, but querying non-pointer
4152 arguments is diagnosed. */
4153 if (!TREE_VALUE (atlist) && attr_nonnull)
4154 return true;
4155
4156 /* Iterate over the DECL or TYPE attribute argument's values. */
4157 for (tree val = TREE_VALUE (atlist); val; val = TREE_CHAIN (val))
4158 {
4159 /* Iterate over the arguments in the sought attribute comparing
4160 their values to those specified for the DECL or TYPE. */
4161 for (tree arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
4162 {
4163 tree v1 = TREE_VALUE (val);
4164 tree v2 = TREE_VALUE (arg);
4165 if (v1 == v2)
4166 return true;
4167
4168 if (!v1 || !v2)
4169 break;
4170
4171 if (TREE_CODE (v1) == IDENTIFIER_NODE
4172 || TREE_CODE (v2) == IDENTIFIER_NODE)
4173 /* Two identifiers are the same if their values are
4174 equal (that's handled above). Otherwise ther are
4175 either not the same or oneis not an identifier. */
4176 return false;
4177
4178 /* Convert to make them equality-comparable. */
4179 v1 = convert (v1);
4180 v2 = convert (v2);
4181
4182 /* A positive value indicates equality, negative means
4183 "don't know." */
4184 if (simple_cst_equal (v1, v2) == 1)
4185 return true;
4186
4187 if (!ignore_mismatches)
4188 break;
4189 }
4190 }
4191 }
4192
4193 if (!found_attr)
4194 {
4195 /* Some attributes are encoded directly in the tree node. */
4196 if (!strcmp ("aligned", namestr))
4197 {
4198 if (tree arg = TREE_VALUE (attr))
4199 {
4200 arg = convert (TREE_VALUE (arg));
4201 if (expr && DECL_P (expr)
4202 && DECL_USER_ALIGN (expr)
4203 && tree_fits_uhwi_p (arg))
4204 found_match = DECL_ALIGN_UNIT (expr) == tree_to_uhwi (arg);
4205 else if (type && TYPE_USER_ALIGN (type))
4206 found_match = TYPE_ALIGN_UNIT (type) == tree_to_uhwi (arg);
4207 }
4208 else if (expr && DECL_P (expr))
4209 found_match = DECL_USER_ALIGN (expr);
4210 else if (type)
4211 found_match = TYPE_USER_ALIGN (type);
4212 }
4213 else if (!strcmp ("const", namestr))
4214 {
4215 if (expr && DECL_P (expr))
4216 found_match = TREE_READONLY (expr);
4217 }
4218 else if (!strcmp ("noreturn", namestr))
4219 {
4220 /* C11 _Noreturn sets the volatile bit without attaching
4221 an attribute to the decl. */
4222 if (expr
4223 && DECL_P (expr)
4224 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr)))
4225 found_match = TREE_THIS_VOLATILE (expr);
4226 }
4227 else if (!strcmp ("pure", namestr))
4228 {
4229 if (expr && DECL_P (expr))
4230 found_match = DECL_PURE_P (expr);
4231 }
4232 else if (!strcmp ("deprecated", namestr))
4233 {
4234 found_match = TREE_DEPRECATED (expr ? expr : type);
4235 if (found_match)
4236 return true;
4237 }
4238 else if (!strcmp ("vector_size", namestr))
4239 {
4240 if (!type)
4241 continue;
4242
4243 /* Determine the base type from arrays, pointers, and such.
4244 Fail if the base type is not a vector. */
4245 type = type_for_vector_size (type);
4246 if (!VECTOR_TYPE_P (type))
4247 return false;
4248
4249 if (tree arg = TREE_VALUE (attr))
4250 {
4251 /* Compare the vector size argument for equality. */
4252 arg = convert (TREE_VALUE (arg));
4253 return tree_int_cst_equal (arg, TYPE_SIZE_UNIT (type)) == 1;
4254 }
4255 else
4256 return true;
4257 }
4258 else if (!strcmp ("warn_if_not_aligned", namestr))
4259 {
4260 if (tree arg = TREE_VALUE (attr))
4261 {
4262 arg = convert (TREE_VALUE (arg));
4263 if (expr && DECL_P (expr))
4264 found_match = (DECL_WARN_IF_NOT_ALIGN (expr)
4265 == tree_to_uhwi (arg) * BITS_PER_UNIT);
4266 else if (type)
4267 found_match = (TYPE_WARN_IF_NOT_ALIGN (type)
4268 == tree_to_uhwi (arg) * BITS_PER_UNIT);
4269 }
4270 else if (expr && DECL_P (expr))
4271 found_match = DECL_WARN_IF_NOT_ALIGN (expr);
4272 else if (type)
4273 found_match = TYPE_WARN_IF_NOT_ALIGN (type);
4274 }
4275 else if (!strcmp ("transparent_union", namestr))
4276 {
4277 if (type)
4278 found_match = TYPE_TRANSPARENT_AGGR (type) != 0;
4279 }
4280 else if (!strcmp ("mode", namestr))
4281 {
4282 /* Finally issue a warning for attributes that cannot
4283 be supported in this context. Attribute mode is not
4284 added to a symbol and cannot be determined from it. */
4285 warning_at (atloc, OPT_Wattributes,
4286 "%qs attribute not supported in "
4287 "%<__builtin_has_attribute%>", namestr);
4288 break;
4289 }
4290 }
4291 }
4292 return found_match;
4293 }