* config/i386/i386.c (ix86_expand_prologue): Tighten assert
[official-gcc.git] / gcc / c-family / c-attribs.c
blobbb75cba4c39d178eff51bd2028140ef068db9e2a
1 /* C-family attributes handling.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "tree.h"
26 #include "memmodel.h"
27 #include "c-common.h"
28 #include "gimple-expr.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "intl.h"
34 #include "stor-layout.h"
35 #include "calls.h"
36 #include "attribs.h"
37 #include "varasm.h"
38 #include "trans-mem.h"
39 #include "c-objc.h"
40 #include "common/common-target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
43 #include "toplev.h"
44 #include "tree-iterator.h"
45 #include "opts.h"
46 #include "gimplify.h"
48 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
56 int, bool *);
57 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
58 int, bool *);
59 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
60 int, bool *);
61 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
62 bool *);
63 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
64 bool *);
65 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
66 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_always_inline_attribute (tree *, tree, tree, int,
73 bool *);
74 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
80 bool *);
81 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
82 bool *);
83 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_transparent_union_attribute (tree *, tree, tree,
85 int, bool *);
86 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
87 int, bool *);
88 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
94 int, bool *);
95 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
96 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
97 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
98 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
101 static tree handle_visibility_attribute (tree *, tree, tree, int,
102 bool *);
103 static tree handle_tls_model_attribute (tree *, tree, tree, int,
104 bool *);
105 static tree handle_no_instrument_function_attribute (tree *, tree,
106 tree, int, bool *);
107 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
108 tree, int, bool *);
109 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
111 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
112 bool *);
113 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_deprecated_attribute (tree *, tree, tree, int,
118 bool *);
119 static tree handle_vector_size_attribute (tree *, tree, tree, int,
120 bool *);
121 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
126 bool *);
127 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
135 static tree ignore_attribute (tree *, tree, tree, int, bool *);
136 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
140 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
141 bool *);
142 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
143 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
144 bool *);
145 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
147 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
148 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
149 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
150 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
151 int, bool *);
153 /* Table of machine-independent attributes common to all C-like languages.
155 All attributes referencing arguments should be additionally processed
156 in chkp_copy_function_type_adding_bounds for correct instrumentation
157 by Pointer Bounds Checker.
158 Current list of processed common attributes: nonnull. */
159 const struct attribute_spec c_common_attribute_table[] =
161 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
162 affects_type_identity } */
163 { "packed", 0, 0, false, false, false,
164 handle_packed_attribute , false},
165 { "nocommon", 0, 0, true, false, false,
166 handle_nocommon_attribute, false},
167 { "common", 0, 0, true, false, false,
168 handle_common_attribute, false },
169 /* FIXME: logically, noreturn attributes should be listed as
170 "false, true, true" and apply to function types. But implementing this
171 would require all the places in the compiler that use TREE_THIS_VOLATILE
172 on a decl to identify non-returning functions to be located and fixed
173 to check the function type instead. */
174 { "noreturn", 0, 0, true, false, false,
175 handle_noreturn_attribute, false },
176 { "volatile", 0, 0, true, false, false,
177 handle_noreturn_attribute, false },
178 { "stack_protect", 0, 0, true, false, false,
179 handle_stack_protect_attribute, false },
180 { "noinline", 0, 0, true, false, false,
181 handle_noinline_attribute, false },
182 { "noclone", 0, 0, true, false, false,
183 handle_noclone_attribute, false },
184 { "no_icf", 0, 0, true, false, false,
185 handle_noicf_attribute, false },
186 { "noipa", 0, 0, true, false, false,
187 handle_noipa_attribute, false },
188 { "leaf", 0, 0, true, false, false,
189 handle_leaf_attribute, false },
190 { "always_inline", 0, 0, true, false, false,
191 handle_always_inline_attribute, false },
192 { "gnu_inline", 0, 0, true, false, false,
193 handle_gnu_inline_attribute, false },
194 { "artificial", 0, 0, true, false, false,
195 handle_artificial_attribute, false },
196 { "flatten", 0, 0, true, false, false,
197 handle_flatten_attribute, false },
198 { "used", 0, 0, true, false, false,
199 handle_used_attribute, false },
200 { "unused", 0, 0, false, false, false,
201 handle_unused_attribute, false },
202 { "externally_visible", 0, 0, true, false, false,
203 handle_externally_visible_attribute, false },
204 { "no_reorder", 0, 0, true, false, false,
205 handle_no_reorder_attribute, false },
206 /* The same comments as for noreturn attributes apply to const ones. */
207 { "const", 0, 0, true, false, false,
208 handle_const_attribute, false },
209 { "scalar_storage_order", 1, 1, false, false, false,
210 handle_scalar_storage_order_attribute, false },
211 { "transparent_union", 0, 0, false, false, false,
212 handle_transparent_union_attribute, false },
213 { "constructor", 0, 1, true, false, false,
214 handle_constructor_attribute, false },
215 { "destructor", 0, 1, true, false, false,
216 handle_destructor_attribute, false },
217 { "mode", 1, 1, false, true, false,
218 handle_mode_attribute, false },
219 { "section", 1, 1, true, false, false,
220 handle_section_attribute, false },
221 { "aligned", 0, 1, false, false, false,
222 handle_aligned_attribute, false },
223 { "warn_if_not_aligned", 0, 1, false, false, false,
224 handle_warn_if_not_aligned_attribute,
225 false },
226 { "weak", 0, 0, true, false, false,
227 handle_weak_attribute, false },
228 { "noplt", 0, 0, true, false, false,
229 handle_noplt_attribute, false },
230 { "ifunc", 1, 1, true, false, false,
231 handle_ifunc_attribute, false },
232 { "alias", 1, 1, true, false, false,
233 handle_alias_attribute, false },
234 { "weakref", 0, 1, true, false, false,
235 handle_weakref_attribute, false },
236 { "no_instrument_function", 0, 0, true, false, false,
237 handle_no_instrument_function_attribute,
238 false },
239 { "no_profile_instrument_function", 0, 0, true, false, false,
240 handle_no_profile_instrument_function_attribute,
241 false },
242 { "malloc", 0, 0, true, false, false,
243 handle_malloc_attribute, false },
244 { "returns_twice", 0, 0, true, false, false,
245 handle_returns_twice_attribute, false },
246 { "no_stack_limit", 0, 0, true, false, false,
247 handle_no_limit_stack_attribute, false },
248 { "pure", 0, 0, true, false, false,
249 handle_pure_attribute, false },
250 { "transaction_callable", 0, 0, false, true, false,
251 handle_tm_attribute, false },
252 { "transaction_unsafe", 0, 0, false, true, false,
253 handle_tm_attribute, true },
254 { "transaction_safe", 0, 0, false, true, false,
255 handle_tm_attribute, true },
256 { "transaction_safe_dynamic", 0, 0, true, false, false,
257 handle_tm_attribute, false },
258 { "transaction_may_cancel_outer", 0, 0, false, true, false,
259 handle_tm_attribute, false },
260 /* ??? These two attributes didn't make the transition from the
261 Intel language document to the multi-vendor language document. */
262 { "transaction_pure", 0, 0, false, true, false,
263 handle_tm_attribute, false },
264 { "transaction_wrap", 1, 1, true, false, false,
265 handle_tm_wrap_attribute, false },
266 /* For internal use (marking of builtins) only. The name contains space
267 to prevent its usage in source code. */
268 { "no vops", 0, 0, true, false, false,
269 handle_novops_attribute, false },
270 { "deprecated", 0, 1, false, false, false,
271 handle_deprecated_attribute, false },
272 { "vector_size", 1, 1, false, true, false,
273 handle_vector_size_attribute, true },
274 { "visibility", 1, 1, false, false, false,
275 handle_visibility_attribute, false },
276 { "tls_model", 1, 1, true, false, false,
277 handle_tls_model_attribute, false },
278 { "nonnull", 0, -1, false, true, true,
279 handle_nonnull_attribute, false },
280 { "nonstring", 0, 0, true, false, false,
281 handle_nonstring_attribute, false },
282 { "nothrow", 0, 0, true, false, false,
283 handle_nothrow_attribute, false },
284 { "may_alias", 0, 0, false, true, false, NULL, false },
285 { "cleanup", 1, 1, true, false, false,
286 handle_cleanup_attribute, false },
287 { "warn_unused_result", 0, 0, false, true, true,
288 handle_warn_unused_result_attribute, false },
289 { "sentinel", 0, 1, false, true, true,
290 handle_sentinel_attribute, false },
291 /* For internal use (marking of builtins) only. The name contains space
292 to prevent its usage in source code. */
293 { "type generic", 0, 0, false, true, true,
294 handle_type_generic_attribute, false },
295 { "alloc_size", 1, 2, false, true, true,
296 handle_alloc_size_attribute, false },
297 { "cold", 0, 0, true, false, false,
298 handle_cold_attribute, false },
299 { "hot", 0, 0, true, false, false,
300 handle_hot_attribute, false },
301 { "no_address_safety_analysis",
302 0, 0, true, false, false,
303 handle_no_address_safety_analysis_attribute,
304 false },
305 { "no_sanitize", 1, 1, true, false, false,
306 handle_no_sanitize_attribute,
307 false },
308 { "no_sanitize_address", 0, 0, true, false, false,
309 handle_no_sanitize_address_attribute,
310 false },
311 { "no_sanitize_thread", 0, 0, true, false, false,
312 handle_no_sanitize_thread_attribute,
313 false },
314 { "no_sanitize_undefined", 0, 0, true, false, false,
315 handle_no_sanitize_undefined_attribute,
316 false },
317 { "asan odr indicator", 0, 0, true, false, false,
318 handle_asan_odr_indicator_attribute,
319 false },
320 { "warning", 1, 1, true, false, false,
321 handle_error_attribute, false },
322 { "error", 1, 1, true, false, false,
323 handle_error_attribute, false },
324 { "target", 1, -1, true, false, false,
325 handle_target_attribute, false },
326 { "target_clones", 1, -1, true, false, false,
327 handle_target_clones_attribute, false },
328 { "optimize", 1, -1, true, false, false,
329 handle_optimize_attribute, false },
330 /* For internal use only. The leading '*' both prevents its usage in
331 source code and signals that it may be overridden by machine tables. */
332 { "*tm regparm", 0, 0, false, true, true,
333 ignore_attribute, false },
334 { "no_split_stack", 0, 0, true, false, false,
335 handle_no_split_stack_attribute, false },
336 /* For internal use (marking of builtins and runtime functions) only.
337 The name contains space to prevent its usage in source code. */
338 { "fn spec", 1, 1, false, true, true,
339 handle_fnspec_attribute, false },
340 { "warn_unused", 0, 0, false, false, false,
341 handle_warn_unused_attribute, false },
342 { "returns_nonnull", 0, 0, false, true, true,
343 handle_returns_nonnull_attribute, false },
344 { "omp declare simd", 0, -1, true, false, false,
345 handle_omp_declare_simd_attribute, false },
346 { "cilk simd function", 0, -1, true, false, false,
347 handle_omp_declare_simd_attribute, false },
348 { "simd", 0, 1, true, false, false,
349 handle_simd_attribute, false },
350 { "omp declare target", 0, 0, true, false, false,
351 handle_omp_declare_target_attribute, false },
352 { "omp declare target link", 0, 0, true, false, false,
353 handle_omp_declare_target_attribute, false },
354 { "alloc_align", 1, 1, false, true, true,
355 handle_alloc_align_attribute, false },
356 { "assume_aligned", 1, 2, false, true, true,
357 handle_assume_aligned_attribute, false },
358 { "designated_init", 0, 0, false, true, false,
359 handle_designated_init_attribute, false },
360 { "bnd_variable_size", 0, 0, true, false, false,
361 handle_bnd_variable_size_attribute, false },
362 { "bnd_legacy", 0, 0, true, false, false,
363 handle_bnd_legacy, false },
364 { "bnd_instrument", 0, 0, true, false, false,
365 handle_bnd_instrument, false },
366 { "fallthrough", 0, 0, false, false, false,
367 handle_fallthrough_attribute, false },
368 { "patchable_function_entry", 1, 2, true, false, false,
369 handle_patchable_function_entry_attribute,
370 false },
371 { "nocf_check", 0, 0, false, true, true,
372 handle_nocf_check_attribute, true },
373 { NULL, 0, 0, false, false, false, NULL, false }
376 /* Give the specifications for the format attributes, used by C and all
377 descendants.
379 All attributes referencing arguments should be additionally processed
380 in chkp_copy_function_type_adding_bounds for correct instrumentation
381 by Pointer Bounds Checker.
382 Current list of processed format attributes: format, format_arg. */
383 const struct attribute_spec c_common_format_attribute_table[] =
385 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
386 affects_type_identity } */
387 { "format", 3, 3, false, true, true,
388 handle_format_attribute, false },
389 { "format_arg", 1, 1, false, true, true,
390 handle_format_arg_attribute, false },
391 { NULL, 0, 0, false, false, false, NULL, false }
394 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
395 identifier as an argument, so the front end shouldn't look it up. */
397 bool
398 attribute_takes_identifier_p (const_tree attr_id)
400 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
401 if (spec == NULL)
402 /* Unknown attribute that we'll end up ignoring, return true so we
403 don't complain about an identifier argument. */
404 return true;
405 else if (!strcmp ("mode", spec->name)
406 || !strcmp ("format", spec->name)
407 || !strcmp ("cleanup", spec->name))
408 return true;
409 else
410 return targetm.attribute_takes_identifier_p (attr_id);
413 /* Attribute handlers common to C front ends. */
415 /* Handle a "packed" attribute; arguments as in
416 struct attribute_spec.handler. */
418 static tree
419 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
420 int flags, bool *no_add_attrs)
422 if (TYPE_P (*node))
424 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
425 *node = build_variant_type_copy (*node);
426 TYPE_PACKED (*node) = 1;
428 else if (TREE_CODE (*node) == FIELD_DECL)
430 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
431 /* Still pack bitfields. */
432 && ! DECL_C_BIT_FIELD (*node))
433 warning (OPT_Wattributes,
434 "%qE attribute ignored for field of type %qT",
435 name, TREE_TYPE (*node));
436 else
437 DECL_PACKED (*node) = 1;
439 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
440 used for DECL_REGISTER. It wouldn't mean anything anyway.
441 We can't set DECL_PACKED on the type of a TYPE_DECL, because
442 that changes what the typedef is typing. */
443 else
445 warning (OPT_Wattributes, "%qE attribute ignored", name);
446 *no_add_attrs = true;
449 return NULL_TREE;
452 /* Handle a "nocommon" attribute; arguments as in
453 struct attribute_spec.handler. */
455 static tree
456 handle_nocommon_attribute (tree *node, tree name,
457 tree ARG_UNUSED (args),
458 int ARG_UNUSED (flags), bool *no_add_attrs)
460 if (VAR_P (*node))
461 DECL_COMMON (*node) = 0;
462 else
464 warning (OPT_Wattributes, "%qE attribute ignored", name);
465 *no_add_attrs = true;
468 return NULL_TREE;
471 /* Handle a "common" attribute; arguments as in
472 struct attribute_spec.handler. */
474 static tree
475 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
476 int ARG_UNUSED (flags), bool *no_add_attrs)
478 if (VAR_P (*node))
479 DECL_COMMON (*node) = 1;
480 else
482 warning (OPT_Wattributes, "%qE attribute ignored", name);
483 *no_add_attrs = true;
486 return NULL_TREE;
489 /* Handle a "noreturn" attribute; arguments as in
490 struct attribute_spec.handler. */
492 static tree
493 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
494 int ARG_UNUSED (flags), bool *no_add_attrs)
496 tree type = TREE_TYPE (*node);
498 /* See FIXME comment in c_common_attribute_table. */
499 if (TREE_CODE (*node) == FUNCTION_DECL
500 || objc_method_decl (TREE_CODE (*node)))
501 TREE_THIS_VOLATILE (*node) = 1;
502 else if (TREE_CODE (type) == POINTER_TYPE
503 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
504 TREE_TYPE (*node)
505 = (build_qualified_type
506 (build_pointer_type
507 (build_type_variant (TREE_TYPE (type),
508 TYPE_READONLY (TREE_TYPE (type)), 1)),
509 TYPE_QUALS (type)));
510 else
512 warning (OPT_Wattributes, "%qE attribute ignored", name);
513 *no_add_attrs = true;
516 return NULL_TREE;
519 /* Handle a "hot" and attribute; arguments as in
520 struct attribute_spec.handler. */
522 static tree
523 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
524 int ARG_UNUSED (flags), bool *no_add_attrs)
526 if (TREE_CODE (*node) == FUNCTION_DECL
527 || TREE_CODE (*node) == LABEL_DECL)
529 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
531 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
532 "with attribute %qs", name, "cold");
533 *no_add_attrs = true;
535 /* Most of the rest of the hot processing is done later with
536 lookup_attribute. */
538 else
540 warning (OPT_Wattributes, "%qE attribute ignored", name);
541 *no_add_attrs = true;
544 return NULL_TREE;
547 /* Handle a "cold" and attribute; arguments as in
548 struct attribute_spec.handler. */
550 static tree
551 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
552 int ARG_UNUSED (flags), bool *no_add_attrs)
554 if (TREE_CODE (*node) == FUNCTION_DECL
555 || TREE_CODE (*node) == LABEL_DECL)
557 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
559 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
560 "with attribute %qs", name, "hot");
561 *no_add_attrs = true;
563 /* Most of the rest of the cold processing is done later with
564 lookup_attribute. */
566 else
568 warning (OPT_Wattributes, "%qE attribute ignored", name);
569 *no_add_attrs = true;
572 return NULL_TREE;
575 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
577 void
578 add_no_sanitize_value (tree node, unsigned int flags)
580 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
581 if (attr)
583 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
584 flags |= old_value;
586 if (flags == old_value)
587 return;
589 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
591 else
592 DECL_ATTRIBUTES (node)
593 = tree_cons (get_identifier ("no_sanitize"),
594 build_int_cst (unsigned_type_node, flags),
595 DECL_ATTRIBUTES (node));
598 /* Handle a "no_sanitize" attribute; arguments as in
599 struct attribute_spec.handler. */
601 static tree
602 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
603 bool *no_add_attrs)
605 *no_add_attrs = true;
606 tree id = TREE_VALUE (args);
607 if (TREE_CODE (*node) != FUNCTION_DECL)
609 warning (OPT_Wattributes, "%qE attribute ignored", name);
610 return NULL_TREE;
613 if (TREE_CODE (id) != STRING_CST)
615 error ("no_sanitize argument not a string");
616 return NULL_TREE;
619 char *string = ASTRDUP (TREE_STRING_POINTER (id));
620 unsigned int flags = parse_no_sanitize_attribute (string);
622 add_no_sanitize_value (*node, flags);
624 return NULL_TREE;
627 /* Handle a "no_sanitize_address" attribute; arguments as in
628 struct attribute_spec.handler. */
630 static tree
631 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
632 bool *no_add_attrs)
634 *no_add_attrs = true;
635 if (TREE_CODE (*node) != FUNCTION_DECL)
636 warning (OPT_Wattributes, "%qE attribute ignored", name);
637 else
638 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
640 return NULL_TREE;
643 /* Handle a "no_sanitize_thread" attribute; arguments as in
644 struct attribute_spec.handler. */
646 static tree
647 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
648 bool *no_add_attrs)
650 *no_add_attrs = true;
651 if (TREE_CODE (*node) != FUNCTION_DECL)
652 warning (OPT_Wattributes, "%qE attribute ignored", name);
653 else
654 add_no_sanitize_value (*node, SANITIZE_THREAD);
656 return NULL_TREE;
660 /* Handle a "no_address_safety_analysis" attribute; arguments as in
661 struct attribute_spec.handler. */
663 static tree
664 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
665 bool *no_add_attrs)
667 *no_add_attrs = true;
668 if (TREE_CODE (*node) != FUNCTION_DECL)
669 warning (OPT_Wattributes, "%qE attribute ignored", name);
670 else
671 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
673 return NULL_TREE;
676 /* Handle a "no_sanitize_undefined" attribute; arguments as in
677 struct attribute_spec.handler. */
679 static tree
680 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
681 bool *no_add_attrs)
683 *no_add_attrs = true;
684 if (TREE_CODE (*node) != FUNCTION_DECL)
685 warning (OPT_Wattributes, "%qE attribute ignored", name);
686 else
687 add_no_sanitize_value (*node,
688 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
690 return NULL_TREE;
693 /* Handle an "asan odr indicator" attribute; arguments as in
694 struct attribute_spec.handler. */
696 static tree
697 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
699 return NULL_TREE;
702 /* Handle a "stack_protect" attribute; arguments as in
703 struct attribute_spec.handler. */
705 static tree
706 handle_stack_protect_attribute (tree *node, tree name, tree, int,
707 bool *no_add_attrs)
709 if (TREE_CODE (*node) != FUNCTION_DECL)
711 warning (OPT_Wattributes, "%qE attribute ignored", name);
712 *no_add_attrs = true;
715 return NULL_TREE;
718 /* Handle a "noipa" attribute; arguments as in
719 struct attribute_spec.handler. */
721 static tree
722 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
724 if (TREE_CODE (*node) != FUNCTION_DECL)
726 warning (OPT_Wattributes, "%qE attribute ignored", name);
727 *no_add_attrs = true;
730 return NULL_TREE;
733 /* Handle a "noinline" attribute; arguments as in
734 struct attribute_spec.handler. */
736 static tree
737 handle_noinline_attribute (tree *node, tree name,
738 tree ARG_UNUSED (args),
739 int ARG_UNUSED (flags), bool *no_add_attrs)
741 if (TREE_CODE (*node) == FUNCTION_DECL)
743 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
745 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
746 "with attribute %qs", name, "always_inline");
747 *no_add_attrs = true;
749 else
750 DECL_UNINLINABLE (*node) = 1;
752 else
754 warning (OPT_Wattributes, "%qE attribute ignored", name);
755 *no_add_attrs = true;
758 return NULL_TREE;
761 /* Handle a "noclone" attribute; arguments as in
762 struct attribute_spec.handler. */
764 static tree
765 handle_noclone_attribute (tree *node, tree name,
766 tree ARG_UNUSED (args),
767 int ARG_UNUSED (flags), bool *no_add_attrs)
769 if (TREE_CODE (*node) != FUNCTION_DECL)
771 warning (OPT_Wattributes, "%qE attribute ignored", name);
772 *no_add_attrs = true;
775 return NULL_TREE;
778 /* Handle a "nocf_check" attribute; arguments as in
779 struct attribute_spec.handler. */
781 static tree
782 handle_nocf_check_attribute (tree *node, tree name,
783 tree ARG_UNUSED (args),
784 int ARG_UNUSED (flags), bool *no_add_attrs)
786 if (TREE_CODE (*node) != FUNCTION_TYPE
787 && TREE_CODE (*node) != METHOD_TYPE)
789 warning (OPT_Wattributes, "%qE attribute ignored", name);
790 *no_add_attrs = true;
792 else if (!(flag_cf_protection & CF_BRANCH))
794 warning (OPT_Wattributes, "%qE attribute ignored. Use "
795 "-fcf-protection option to enable it", name);
796 *no_add_attrs = true;
799 return NULL_TREE;
802 /* Handle a "no_icf" attribute; arguments as in
803 struct attribute_spec.handler. */
805 static tree
806 handle_noicf_attribute (tree *node, tree name,
807 tree ARG_UNUSED (args),
808 int ARG_UNUSED (flags), bool *no_add_attrs)
810 if (TREE_CODE (*node) != FUNCTION_DECL)
812 warning (OPT_Wattributes, "%qE attribute ignored", name);
813 *no_add_attrs = true;
816 return NULL_TREE;
820 /* Handle a "always_inline" attribute; arguments as in
821 struct attribute_spec.handler. */
823 static tree
824 handle_always_inline_attribute (tree *node, tree name,
825 tree ARG_UNUSED (args),
826 int ARG_UNUSED (flags),
827 bool *no_add_attrs)
829 if (TREE_CODE (*node) == FUNCTION_DECL)
831 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
833 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
834 "with %qs attribute", name, "noinline");
835 *no_add_attrs = true;
837 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
839 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
840 "with %qs attribute", name, "target_clones");
841 *no_add_attrs = true;
843 else
844 /* Set the attribute and mark it for disregarding inline
845 limits. */
846 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
848 else
850 warning (OPT_Wattributes, "%qE attribute ignored", name);
851 *no_add_attrs = true;
854 return NULL_TREE;
857 /* Handle a "gnu_inline" attribute; arguments as in
858 struct attribute_spec.handler. */
860 static tree
861 handle_gnu_inline_attribute (tree *node, tree name,
862 tree ARG_UNUSED (args),
863 int ARG_UNUSED (flags),
864 bool *no_add_attrs)
866 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
868 /* Do nothing else, just set the attribute. We'll get at
869 it later with lookup_attribute. */
871 else
873 warning (OPT_Wattributes, "%qE attribute ignored", name);
874 *no_add_attrs = true;
877 return NULL_TREE;
880 /* Handle a "leaf" attribute; arguments as in
881 struct attribute_spec.handler. */
883 static tree
884 handle_leaf_attribute (tree *node, tree name,
885 tree ARG_UNUSED (args),
886 int ARG_UNUSED (flags), bool *no_add_attrs)
888 if (TREE_CODE (*node) != FUNCTION_DECL)
890 warning (OPT_Wattributes, "%qE attribute ignored", name);
891 *no_add_attrs = true;
893 if (!TREE_PUBLIC (*node))
895 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
896 "functions", name);
897 *no_add_attrs = true;
900 return NULL_TREE;
903 /* Handle an "artificial" attribute; arguments as in
904 struct attribute_spec.handler. */
906 static tree
907 handle_artificial_attribute (tree *node, tree name,
908 tree ARG_UNUSED (args),
909 int ARG_UNUSED (flags),
910 bool *no_add_attrs)
912 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
914 /* Do nothing else, just set the attribute. We'll get at
915 it later with lookup_attribute. */
917 else
919 warning (OPT_Wattributes, "%qE attribute ignored", name);
920 *no_add_attrs = true;
923 return NULL_TREE;
926 /* Handle a "flatten" attribute; arguments as in
927 struct attribute_spec.handler. */
929 static tree
930 handle_flatten_attribute (tree *node, tree name,
931 tree args ATTRIBUTE_UNUSED,
932 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
934 if (TREE_CODE (*node) == FUNCTION_DECL)
935 /* Do nothing else, just set the attribute. We'll get at
936 it later with lookup_attribute. */
938 else
940 warning (OPT_Wattributes, "%qE attribute ignored", name);
941 *no_add_attrs = true;
944 return NULL_TREE;
947 /* Handle a "warning" or "error" attribute; arguments as in
948 struct attribute_spec.handler. */
950 static tree
951 handle_error_attribute (tree *node, tree name, tree args,
952 int ARG_UNUSED (flags), bool *no_add_attrs)
954 if (TREE_CODE (*node) == FUNCTION_DECL
955 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
956 /* Do nothing else, just set the attribute. We'll get at
957 it later with lookup_attribute. */
959 else
961 warning (OPT_Wattributes, "%qE attribute ignored", name);
962 *no_add_attrs = true;
965 return NULL_TREE;
968 /* Handle a "used" attribute; arguments as in
969 struct attribute_spec.handler. */
971 static tree
972 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
973 int ARG_UNUSED (flags), bool *no_add_attrs)
975 tree node = *pnode;
977 if (TREE_CODE (node) == FUNCTION_DECL
978 || (VAR_P (node) && TREE_STATIC (node))
979 || (TREE_CODE (node) == TYPE_DECL))
981 TREE_USED (node) = 1;
982 DECL_PRESERVE_P (node) = 1;
983 if (VAR_P (node))
984 DECL_READ_P (node) = 1;
986 else
988 warning (OPT_Wattributes, "%qE attribute ignored", name);
989 *no_add_attrs = true;
992 return NULL_TREE;
995 /* Handle a "unused" attribute; arguments as in
996 struct attribute_spec.handler. */
998 tree
999 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1000 int flags, bool *no_add_attrs)
1002 if (DECL_P (*node))
1004 tree decl = *node;
1006 if (TREE_CODE (decl) == PARM_DECL
1007 || VAR_OR_FUNCTION_DECL_P (decl)
1008 || TREE_CODE (decl) == LABEL_DECL
1009 || TREE_CODE (decl) == CONST_DECL
1010 || TREE_CODE (decl) == TYPE_DECL)
1012 TREE_USED (decl) = 1;
1013 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1014 DECL_READ_P (decl) = 1;
1016 else
1018 warning (OPT_Wattributes, "%qE attribute ignored", name);
1019 *no_add_attrs = true;
1022 else
1024 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1025 *node = build_variant_type_copy (*node);
1026 TREE_USED (*node) = 1;
1029 return NULL_TREE;
1032 /* Handle a "externally_visible" attribute; arguments as in
1033 struct attribute_spec.handler. */
1035 static tree
1036 handle_externally_visible_attribute (tree *pnode, tree name,
1037 tree ARG_UNUSED (args),
1038 int ARG_UNUSED (flags),
1039 bool *no_add_attrs)
1041 tree node = *pnode;
1043 if (VAR_OR_FUNCTION_DECL_P (node))
1045 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1046 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1048 warning (OPT_Wattributes,
1049 "%qE attribute have effect only on public objects", name);
1050 *no_add_attrs = true;
1053 else
1055 warning (OPT_Wattributes, "%qE attribute ignored", name);
1056 *no_add_attrs = true;
1059 return NULL_TREE;
1062 /* Handle the "no_reorder" attribute. Arguments as in
1063 struct attribute_spec.handler. */
1065 static tree
1066 handle_no_reorder_attribute (tree *pnode,
1067 tree name,
1068 tree,
1069 int,
1070 bool *no_add_attrs)
1072 tree node = *pnode;
1074 if (!VAR_OR_FUNCTION_DECL_P (node)
1075 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1077 warning (OPT_Wattributes,
1078 "%qE attribute only affects top level objects",
1079 name);
1080 *no_add_attrs = true;
1083 return NULL_TREE;
1086 /* Handle a "const" attribute; arguments as in
1087 struct attribute_spec.handler. */
1089 static tree
1090 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1091 int ARG_UNUSED (flags), bool *no_add_attrs)
1093 tree type = TREE_TYPE (*node);
1095 /* See FIXME comment on noreturn in c_common_attribute_table. */
1096 if (TREE_CODE (*node) == FUNCTION_DECL)
1097 TREE_READONLY (*node) = 1;
1098 else if (TREE_CODE (type) == POINTER_TYPE
1099 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1100 TREE_TYPE (*node)
1101 = (build_qualified_type
1102 (build_pointer_type
1103 (build_type_variant (TREE_TYPE (type), 1,
1104 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1105 TYPE_QUALS (type)));
1106 else
1108 warning (OPT_Wattributes, "%qE attribute ignored", name);
1109 *no_add_attrs = true;
1112 return NULL_TREE;
1115 /* Handle a "scalar_storage_order" attribute; arguments as in
1116 struct attribute_spec.handler. */
1118 static tree
1119 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1120 int flags, bool *no_add_attrs)
1122 tree id = TREE_VALUE (args);
1123 tree type;
1125 if (TREE_CODE (*node) == TYPE_DECL
1126 && ! (flags & ATTR_FLAG_CXX11))
1127 node = &TREE_TYPE (*node);
1128 type = *node;
1130 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1132 error ("scalar_storage_order is not supported because endianness "
1133 "is not uniform");
1134 return NULL_TREE;
1137 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1139 bool reverse = false;
1141 if (TREE_CODE (id) == STRING_CST
1142 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1143 reverse = !BYTES_BIG_ENDIAN;
1144 else if (TREE_CODE (id) == STRING_CST
1145 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1146 reverse = BYTES_BIG_ENDIAN;
1147 else
1149 error ("scalar_storage_order argument must be one of \"big-endian\""
1150 " or \"little-endian\"");
1151 return NULL_TREE;
1154 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1156 if (reverse)
1157 /* A type variant isn't good enough, since we don't want a cast
1158 to such a type to be removed as a no-op. */
1159 *node = type = build_duplicate_type (type);
1162 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1163 return NULL_TREE;
1166 warning (OPT_Wattributes, "%qE attribute ignored", name);
1167 *no_add_attrs = true;
1168 return NULL_TREE;
1171 /* Handle a "transparent_union" attribute; arguments as in
1172 struct attribute_spec.handler. */
1174 static tree
1175 handle_transparent_union_attribute (tree *node, tree name,
1176 tree ARG_UNUSED (args), int flags,
1177 bool *no_add_attrs)
1179 tree type;
1181 *no_add_attrs = true;
1183 if (TREE_CODE (*node) == TYPE_DECL
1184 && ! (flags & ATTR_FLAG_CXX11))
1185 node = &TREE_TYPE (*node);
1186 type = *node;
1188 if (TREE_CODE (type) == UNION_TYPE)
1190 /* Make sure that the first field will work for a transparent union.
1191 If the type isn't complete yet, leave the check to the code in
1192 finish_struct. */
1193 if (TYPE_SIZE (type))
1195 tree first = first_field (type);
1196 if (first == NULL_TREE
1197 || DECL_ARTIFICIAL (first)
1198 || TYPE_MODE (type) != DECL_MODE (first))
1199 goto ignored;
1202 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1204 /* If the type isn't complete yet, setting the flag
1205 on a variant wouldn't ever be checked. */
1206 if (!TYPE_SIZE (type))
1207 goto ignored;
1209 /* build_duplicate_type doesn't work for C++. */
1210 if (c_dialect_cxx ())
1211 goto ignored;
1213 /* A type variant isn't good enough, since we don't want a cast
1214 to such a type to be removed as a no-op. */
1215 *node = type = build_duplicate_type (type);
1218 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1219 TYPE_TRANSPARENT_AGGR (t) = 1;
1220 return NULL_TREE;
1223 ignored:
1224 warning (OPT_Wattributes, "%qE attribute ignored", name);
1225 return NULL_TREE;
1228 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1229 get the requested priority for a constructor or destructor,
1230 possibly issuing diagnostics for invalid or reserved
1231 priorities. */
1233 static priority_type
1234 get_priority (tree args, bool is_destructor)
1236 HOST_WIDE_INT pri;
1237 tree arg;
1239 if (!args)
1240 return DEFAULT_INIT_PRIORITY;
1242 if (!SUPPORTS_INIT_PRIORITY)
1244 if (is_destructor)
1245 error ("destructor priorities are not supported");
1246 else
1247 error ("constructor priorities are not supported");
1248 return DEFAULT_INIT_PRIORITY;
1251 arg = TREE_VALUE (args);
1252 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1253 goto invalid;
1254 if (arg == error_mark_node)
1255 return DEFAULT_INIT_PRIORITY;
1256 arg = default_conversion (arg);
1257 if (!tree_fits_shwi_p (arg)
1258 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1259 goto invalid;
1261 pri = tree_to_shwi (arg);
1262 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1263 goto invalid;
1265 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1267 if (is_destructor)
1268 warning (0,
1269 "destructor priorities from 0 to %d are reserved "
1270 "for the implementation",
1271 MAX_RESERVED_INIT_PRIORITY);
1272 else
1273 warning (0,
1274 "constructor priorities from 0 to %d are reserved "
1275 "for the implementation",
1276 MAX_RESERVED_INIT_PRIORITY);
1278 return pri;
1280 invalid:
1281 if (is_destructor)
1282 error ("destructor priorities must be integers from 0 to %d inclusive",
1283 MAX_INIT_PRIORITY);
1284 else
1285 error ("constructor priorities must be integers from 0 to %d inclusive",
1286 MAX_INIT_PRIORITY);
1287 return DEFAULT_INIT_PRIORITY;
1290 /* Handle a "constructor" attribute; arguments as in
1291 struct attribute_spec.handler. */
1293 static tree
1294 handle_constructor_attribute (tree *node, tree name, tree args,
1295 int ARG_UNUSED (flags),
1296 bool *no_add_attrs)
1298 tree decl = *node;
1299 tree type = TREE_TYPE (decl);
1301 if (TREE_CODE (decl) == FUNCTION_DECL
1302 && TREE_CODE (type) == FUNCTION_TYPE
1303 && decl_function_context (decl) == 0)
1305 priority_type priority;
1306 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1307 priority = get_priority (args, /*is_destructor=*/false);
1308 SET_DECL_INIT_PRIORITY (decl, priority);
1309 TREE_USED (decl) = 1;
1311 else
1313 warning (OPT_Wattributes, "%qE attribute ignored", name);
1314 *no_add_attrs = true;
1317 return NULL_TREE;
1320 /* Handle a "destructor" attribute; arguments as in
1321 struct attribute_spec.handler. */
1323 static tree
1324 handle_destructor_attribute (tree *node, tree name, tree args,
1325 int ARG_UNUSED (flags),
1326 bool *no_add_attrs)
1328 tree decl = *node;
1329 tree type = TREE_TYPE (decl);
1331 if (TREE_CODE (decl) == FUNCTION_DECL
1332 && TREE_CODE (type) == FUNCTION_TYPE
1333 && decl_function_context (decl) == 0)
1335 priority_type priority;
1336 DECL_STATIC_DESTRUCTOR (decl) = 1;
1337 priority = get_priority (args, /*is_destructor=*/true);
1338 SET_DECL_FINI_PRIORITY (decl, priority);
1339 TREE_USED (decl) = 1;
1341 else
1343 warning (OPT_Wattributes, "%qE attribute ignored", name);
1344 *no_add_attrs = true;
1347 return NULL_TREE;
1350 /* Nonzero if the mode is a valid vector mode for this architecture.
1351 This returns nonzero even if there is no hardware support for the
1352 vector mode, but we can emulate with narrower modes. */
1354 static bool
1355 vector_mode_valid_p (machine_mode mode)
1357 enum mode_class mclass = GET_MODE_CLASS (mode);
1359 /* Doh! What's going on? */
1360 if (mclass != MODE_VECTOR_INT
1361 && mclass != MODE_VECTOR_FLOAT
1362 && mclass != MODE_VECTOR_FRACT
1363 && mclass != MODE_VECTOR_UFRACT
1364 && mclass != MODE_VECTOR_ACCUM
1365 && mclass != MODE_VECTOR_UACCUM)
1366 return false;
1368 /* Hardware support. Woo hoo! */
1369 if (targetm.vector_mode_supported_p (mode))
1370 return true;
1372 /* We should probably return 1 if requesting V4DI and we have no DI,
1373 but we have V2DI, but this is probably very unlikely. */
1375 /* If we have support for the inner mode, we can safely emulate it.
1376 We may not have V2DI, but me can emulate with a pair of DIs. */
1377 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1381 /* Handle a "mode" attribute; arguments as in
1382 struct attribute_spec.handler. */
1384 static tree
1385 handle_mode_attribute (tree *node, tree name, tree args,
1386 int ARG_UNUSED (flags), bool *no_add_attrs)
1388 tree type = *node;
1389 tree ident = TREE_VALUE (args);
1391 *no_add_attrs = true;
1393 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1394 warning (OPT_Wattributes, "%qE attribute ignored", name);
1395 else
1397 int j;
1398 const char *p = IDENTIFIER_POINTER (ident);
1399 int len = strlen (p);
1400 machine_mode mode = VOIDmode;
1401 tree typefm;
1402 bool valid_mode;
1404 if (len > 4 && p[0] == '_' && p[1] == '_'
1405 && p[len - 1] == '_' && p[len - 2] == '_')
1407 char *newp = (char *) alloca (len - 1);
1409 strcpy (newp, &p[2]);
1410 newp[len - 4] = '\0';
1411 p = newp;
1414 /* Change this type to have a type with the specified mode.
1415 First check for the special modes. */
1416 if (!strcmp (p, "byte"))
1417 mode = byte_mode;
1418 else if (!strcmp (p, "word"))
1419 mode = word_mode;
1420 else if (!strcmp (p, "pointer"))
1421 mode = ptr_mode;
1422 else if (!strcmp (p, "libgcc_cmp_return"))
1423 mode = targetm.libgcc_cmp_return_mode ();
1424 else if (!strcmp (p, "libgcc_shift_count"))
1425 mode = targetm.libgcc_shift_count_mode ();
1426 else if (!strcmp (p, "unwind_word"))
1427 mode = targetm.unwind_word_mode ();
1428 else
1429 for (j = 0; j < NUM_MACHINE_MODES; j++)
1430 if (!strcmp (p, GET_MODE_NAME (j)))
1432 mode = (machine_mode) j;
1433 break;
1436 if (mode == VOIDmode)
1438 error ("unknown machine mode %qE", ident);
1439 return NULL_TREE;
1442 valid_mode = false;
1443 switch (GET_MODE_CLASS (mode))
1445 case MODE_INT:
1446 case MODE_PARTIAL_INT:
1447 case MODE_FLOAT:
1448 case MODE_DECIMAL_FLOAT:
1449 case MODE_FRACT:
1450 case MODE_UFRACT:
1451 case MODE_ACCUM:
1452 case MODE_UACCUM:
1453 valid_mode
1454 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
1455 break;
1457 case MODE_COMPLEX_INT:
1458 case MODE_COMPLEX_FLOAT:
1459 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1460 break;
1462 case MODE_VECTOR_INT:
1463 case MODE_VECTOR_FLOAT:
1464 case MODE_VECTOR_FRACT:
1465 case MODE_VECTOR_UFRACT:
1466 case MODE_VECTOR_ACCUM:
1467 case MODE_VECTOR_UACCUM:
1468 warning (OPT_Wattributes, "specifying vector types with "
1469 "__attribute__ ((mode)) is deprecated");
1470 warning (OPT_Wattributes,
1471 "use __attribute__ ((vector_size)) instead");
1472 valid_mode = vector_mode_valid_p (mode);
1473 break;
1475 default:
1476 break;
1478 if (!valid_mode)
1480 error ("unable to emulate %qs", p);
1481 return NULL_TREE;
1484 if (POINTER_TYPE_P (type))
1486 scalar_int_mode addr_mode;
1487 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1488 tree (*fn)(tree, machine_mode, bool);
1490 if (!is_a <scalar_int_mode> (mode, &addr_mode)
1491 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
1493 error ("invalid pointer mode %qs", p);
1494 return NULL_TREE;
1497 if (TREE_CODE (type) == POINTER_TYPE)
1498 fn = build_pointer_type_for_mode;
1499 else
1500 fn = build_reference_type_for_mode;
1501 typefm = fn (TREE_TYPE (type), addr_mode, false);
1503 else
1505 /* For fixed-point modes, we need to test if the signness of type
1506 and the machine mode are consistent. */
1507 if (ALL_FIXED_POINT_MODE_P (mode)
1508 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1510 error ("signedness of type and machine mode %qs don%'t match", p);
1511 return NULL_TREE;
1513 /* For fixed-point modes, we need to pass saturating info. */
1514 typefm = lang_hooks.types.type_for_mode (mode,
1515 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1516 : TYPE_UNSIGNED (type));
1519 if (typefm == NULL_TREE)
1521 error ("no data type for mode %qs", p);
1522 return NULL_TREE;
1524 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1526 /* For enumeral types, copy the precision from the integer
1527 type returned above. If not an INTEGER_TYPE, we can't use
1528 this mode for this type. */
1529 if (TREE_CODE (typefm) != INTEGER_TYPE)
1531 error ("cannot use mode %qs for enumeral types", p);
1532 return NULL_TREE;
1535 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1537 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1538 typefm = type;
1540 else
1542 /* We cannot build a type variant, as there's code that assumes
1543 that TYPE_MAIN_VARIANT has the same mode. This includes the
1544 debug generators. Instead, create a subrange type. This
1545 results in all of the enumeral values being emitted only once
1546 in the original, and the subtype gets them by reference. */
1547 if (TYPE_UNSIGNED (type))
1548 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1549 else
1550 typefm = make_signed_type (TYPE_PRECISION (typefm));
1551 TREE_TYPE (typefm) = type;
1554 else if (VECTOR_MODE_P (mode)
1555 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1556 : TREE_CODE (type) != TREE_CODE (typefm))
1558 error ("mode %qs applied to inappropriate type", p);
1559 return NULL_TREE;
1562 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1565 return NULL_TREE;
1568 /* Handle a "section" attribute; arguments as in
1569 struct attribute_spec.handler. */
1571 static tree
1572 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1573 int ARG_UNUSED (flags), bool *no_add_attrs)
1575 tree decl = *node;
1577 if (!targetm_common.have_named_sections)
1579 error_at (DECL_SOURCE_LOCATION (*node),
1580 "section attributes are not supported for this target");
1581 goto fail;
1584 if (!VAR_OR_FUNCTION_DECL_P (decl))
1586 error ("section attribute not allowed for %q+D", *node);
1587 goto fail;
1590 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1592 error ("section attribute argument not a string constant");
1593 goto fail;
1596 if (VAR_P (decl)
1597 && current_function_decl != NULL_TREE
1598 && !TREE_STATIC (decl))
1600 error_at (DECL_SOURCE_LOCATION (decl),
1601 "section attribute cannot be specified for local variables");
1602 goto fail;
1605 /* The decl may have already been given a section attribute
1606 from a previous declaration. Ensure they match. */
1607 if (DECL_SECTION_NAME (decl) != NULL
1608 && strcmp (DECL_SECTION_NAME (decl),
1609 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1611 error ("section of %q+D conflicts with previous declaration", *node);
1612 goto fail;
1615 if (VAR_P (decl)
1616 && !targetm.have_tls && targetm.emutls.tmpl_section
1617 && DECL_THREAD_LOCAL_P (decl))
1619 error ("section of %q+D cannot be overridden", *node);
1620 goto fail;
1623 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1624 return NULL_TREE;
1626 fail:
1627 *no_add_attrs = true;
1628 return NULL_TREE;
1631 /* If in c++-11, check if the c++-11 alignment constraint with respect
1632 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1633 c++-11 mode, does nothing.
1635 [dcl.align]2/ says:
1637 [* if the constant expression evaluates to a fundamental alignment,
1638 the alignment requirement of the declared entity shall be the
1639 specified fundamental alignment.
1641 * if the constant expression evaluates to an extended alignment
1642 and the implementation supports that alignment in the context
1643 of the declaration, the alignment of the declared entity shall
1644 be that alignment
1646 * if the constant expression evaluates to an extended alignment
1647 and the implementation does not support that alignment in the
1648 context of the declaration, the program is ill-formed]. */
1650 static bool
1651 check_cxx_fundamental_alignment_constraints (tree node,
1652 unsigned align_log,
1653 int flags)
1655 bool alignment_too_large_p = false;
1656 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1657 unsigned max_align = 0;
1659 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1660 || (node == NULL_TREE || node == error_mark_node))
1661 return true;
1663 if (cxx_fundamental_alignment_p (requested_alignment))
1664 return true;
1666 if (VAR_P (node))
1668 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1669 /* For file scope variables and static members, the target supports
1670 alignments that are at most MAX_OFILE_ALIGNMENT. */
1671 max_align = MAX_OFILE_ALIGNMENT;
1672 else
1673 /* For stack variables, the target supports at most
1674 MAX_STACK_ALIGNMENT. */
1675 max_align = MAX_STACK_ALIGNMENT;
1676 if (requested_alignment > max_align)
1677 alignment_too_large_p = true;
1679 /* Let's be liberal for types and fields; don't limit their alignment any
1680 more than check_user_alignment already did. */
1682 if (alignment_too_large_p)
1683 pedwarn (input_location, OPT_Wattributes,
1684 "requested alignment %d is larger than %d",
1685 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1687 return !alignment_too_large_p;
1690 /* Common codes shared by handle_warn_if_not_aligned_attribute and
1691 handle_aligned_attribute. */
1693 static tree
1694 common_handle_aligned_attribute (tree *node, tree args, int flags,
1695 bool *no_add_attrs,
1696 bool warn_if_not_aligned_p)
1698 tree decl = NULL_TREE;
1699 tree *type = NULL;
1700 int is_type = 0;
1701 tree align_expr;
1702 int i;
1704 if (args)
1706 align_expr = TREE_VALUE (args);
1707 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1708 && TREE_CODE (align_expr) != FUNCTION_DECL)
1709 align_expr = default_conversion (align_expr);
1711 else
1712 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1714 if (DECL_P (*node))
1716 decl = *node;
1717 type = &TREE_TYPE (decl);
1718 is_type = TREE_CODE (*node) == TYPE_DECL;
1720 else if (TYPE_P (*node))
1721 type = node, is_type = 1;
1723 if ((i = check_user_alignment (align_expr, true)) == -1
1724 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
1725 *no_add_attrs = true;
1726 else if (is_type)
1728 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1729 /* OK, modify the type in place. */;
1730 /* If we have a TYPE_DECL, then copy the type, so that we
1731 don't accidentally modify a builtin type. See pushdecl. */
1732 else if (decl && TREE_TYPE (decl) != error_mark_node
1733 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1735 tree tt = TREE_TYPE (decl);
1736 *type = build_variant_type_copy (*type);
1737 DECL_ORIGINAL_TYPE (decl) = tt;
1738 TYPE_NAME (*type) = decl;
1739 TREE_USED (*type) = TREE_USED (decl);
1740 TREE_TYPE (decl) = *type;
1742 else
1743 *type = build_variant_type_copy (*type);
1745 if (warn_if_not_aligned_p)
1747 SET_TYPE_WARN_IF_NOT_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1748 warn_if_not_aligned_p = false;
1750 else
1752 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1753 TYPE_USER_ALIGN (*type) = 1;
1756 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1757 && TREE_CODE (decl) != FIELD_DECL)
1759 error ("alignment may not be specified for %q+D", decl);
1760 *no_add_attrs = true;
1762 else if (DECL_USER_ALIGN (decl)
1763 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1764 /* C++-11 [dcl.align/4]:
1766 When multiple alignment-specifiers are specified for an
1767 entity, the alignment requirement shall be set to the
1768 strictest specified alignment.
1770 This formally comes from the c++11 specification but we are
1771 doing it for the GNU attribute syntax as well. */
1772 *no_add_attrs = true;
1773 else if (!warn_if_not_aligned_p
1774 && TREE_CODE (decl) == FUNCTION_DECL
1775 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1777 /* Don't warn function alignment here if warn_if_not_aligned_p is
1778 true. It will be warned later. */
1779 if (DECL_USER_ALIGN (decl))
1780 error ("alignment for %q+D was previously specified as %d "
1781 "and may not be decreased", decl,
1782 DECL_ALIGN (decl) / BITS_PER_UNIT);
1783 else
1784 error ("alignment for %q+D must be at least %d", decl,
1785 DECL_ALIGN (decl) / BITS_PER_UNIT);
1786 *no_add_attrs = true;
1788 else
1790 if (warn_if_not_aligned_p)
1792 if (TREE_CODE (decl) == FIELD_DECL && !DECL_C_BIT_FIELD (decl))
1794 SET_DECL_WARN_IF_NOT_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1795 warn_if_not_aligned_p = false;
1798 else
1800 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1801 DECL_USER_ALIGN (decl) = 1;
1805 if (warn_if_not_aligned_p)
1807 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
1808 decl);
1809 *no_add_attrs = true;
1812 return NULL_TREE;
1815 /* Handle a "aligned" attribute; arguments as in
1816 struct attribute_spec.handler. */
1818 static tree
1819 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1820 int flags, bool *no_add_attrs)
1822 return common_handle_aligned_attribute (node, args, flags,
1823 no_add_attrs, false);
1826 /* Handle a "warn_if_not_aligned" attribute; arguments as in
1827 struct attribute_spec.handler. */
1829 static tree
1830 handle_warn_if_not_aligned_attribute (tree *node, tree ARG_UNUSED (name),
1831 tree args, int flags,
1832 bool *no_add_attrs)
1834 return common_handle_aligned_attribute (node, args, flags,
1835 no_add_attrs, true);
1838 /* Handle a "weak" attribute; arguments as in
1839 struct attribute_spec.handler. */
1841 static tree
1842 handle_weak_attribute (tree *node, tree name,
1843 tree ARG_UNUSED (args),
1844 int ARG_UNUSED (flags),
1845 bool * ARG_UNUSED (no_add_attrs))
1847 if (TREE_CODE (*node) == FUNCTION_DECL
1848 && DECL_DECLARED_INLINE_P (*node))
1850 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1851 *no_add_attrs = true;
1853 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1855 error ("indirect function %q+D cannot be declared weak", *node);
1856 *no_add_attrs = true;
1857 return NULL_TREE;
1859 else if (VAR_OR_FUNCTION_DECL_P (*node))
1860 declare_weak (*node);
1861 else
1862 warning (OPT_Wattributes, "%qE attribute ignored", name);
1864 return NULL_TREE;
1867 /* Handle a "noplt" attribute; arguments as in
1868 struct attribute_spec.handler. */
1870 static tree
1871 handle_noplt_attribute (tree *node, tree name,
1872 tree ARG_UNUSED (args),
1873 int ARG_UNUSED (flags),
1874 bool * ARG_UNUSED (no_add_attrs))
1876 if (TREE_CODE (*node) != FUNCTION_DECL)
1878 warning (OPT_Wattributes,
1879 "%qE attribute is only applicable on functions", name);
1880 *no_add_attrs = true;
1881 return NULL_TREE;
1883 return NULL_TREE;
1886 /* Handle an "alias" or "ifunc" attribute; arguments as in
1887 struct attribute_spec.handler, except that IS_ALIAS tells us
1888 whether this is an alias as opposed to ifunc attribute. */
1890 static tree
1891 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
1892 bool *no_add_attrs)
1894 tree decl = *node;
1896 if (TREE_CODE (decl) != FUNCTION_DECL
1897 && (!is_alias || !VAR_P (decl)))
1899 warning (OPT_Wattributes, "%qE attribute ignored", name);
1900 *no_add_attrs = true;
1902 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1903 || (TREE_CODE (decl) != FUNCTION_DECL
1904 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
1905 /* A static variable declaration is always a tentative definition,
1906 but the alias is a non-tentative definition which overrides. */
1907 || (TREE_CODE (decl) != FUNCTION_DECL
1908 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
1910 error ("%q+D defined both normally and as %qE attribute", decl, name);
1911 *no_add_attrs = true;
1912 return NULL_TREE;
1914 else if (!is_alias
1915 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
1916 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
1918 error ("weak %q+D cannot be defined %qE", decl, name);
1919 *no_add_attrs = true;
1920 return NULL_TREE;
1923 /* Note that the very first time we process a nested declaration,
1924 decl_function_context will not be set. Indeed, *would* never
1925 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
1926 we do below. After such frobbery, pushdecl would set the context.
1927 In any case, this is never what we want. */
1928 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
1930 tree id;
1932 id = TREE_VALUE (args);
1933 if (TREE_CODE (id) != STRING_CST)
1935 error ("attribute %qE argument not a string", name);
1936 *no_add_attrs = true;
1937 return NULL_TREE;
1939 id = get_identifier (TREE_STRING_POINTER (id));
1940 /* This counts as a use of the object pointed to. */
1941 TREE_USED (id) = 1;
1943 if (TREE_CODE (decl) == FUNCTION_DECL)
1944 DECL_INITIAL (decl) = error_mark_node;
1945 else
1946 TREE_STATIC (decl) = 1;
1948 if (!is_alias)
1950 /* ifuncs are also aliases, so set that attribute too. */
1951 DECL_ATTRIBUTES (decl)
1952 = tree_cons (get_identifier ("alias"), args,
1953 DECL_ATTRIBUTES (decl));
1954 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
1955 NULL, DECL_ATTRIBUTES (decl));
1958 else
1960 warning (OPT_Wattributes, "%qE attribute ignored", name);
1961 *no_add_attrs = true;
1964 if (decl_in_symtab_p (*node))
1966 struct symtab_node *n = symtab_node::get (decl);
1967 if (n && n->refuse_visibility_changes)
1969 if (is_alias)
1970 error ("%+qD declared alias after being used", decl);
1971 else
1972 error ("%+qD declared ifunc after being used", decl);
1977 return NULL_TREE;
1980 /* Handle an "alias" or "ifunc" attribute; arguments as in
1981 struct attribute_spec.handler. */
1983 static tree
1984 handle_ifunc_attribute (tree *node, tree name, tree args,
1985 int ARG_UNUSED (flags), bool *no_add_attrs)
1987 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
1990 /* Handle an "alias" or "ifunc" attribute; arguments as in
1991 struct attribute_spec.handler. */
1993 static tree
1994 handle_alias_attribute (tree *node, tree name, tree args,
1995 int ARG_UNUSED (flags), bool *no_add_attrs)
1997 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2000 /* Handle a "weakref" attribute; arguments as in struct
2001 attribute_spec.handler. */
2003 static tree
2004 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2005 int flags, bool *no_add_attrs)
2007 tree attr = NULL_TREE;
2009 /* We must ignore the attribute when it is associated with
2010 local-scoped decls, since attribute alias is ignored and many
2011 such symbols do not even have a DECL_WEAK field. */
2012 if (decl_function_context (*node)
2013 || current_function_decl
2014 || !VAR_OR_FUNCTION_DECL_P (*node))
2016 warning (OPT_Wattributes, "%qE attribute ignored", name);
2017 *no_add_attrs = true;
2018 return NULL_TREE;
2021 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2023 error ("indirect function %q+D cannot be declared weakref", *node);
2024 *no_add_attrs = true;
2025 return NULL_TREE;
2028 /* The idea here is that `weakref("name")' mutates into `weakref,
2029 alias("name")', and weakref without arguments, in turn,
2030 implicitly adds weak. */
2032 if (args)
2034 attr = tree_cons (get_identifier ("alias"), args, attr);
2035 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2037 *no_add_attrs = true;
2039 decl_attributes (node, attr, flags);
2041 else
2043 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2044 error_at (DECL_SOURCE_LOCATION (*node),
2045 "weakref attribute must appear before alias attribute");
2047 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2048 and that isn't supported; and because it wants to add it to
2049 the list of weak decls, which isn't helpful. */
2050 DECL_WEAK (*node) = 1;
2053 if (decl_in_symtab_p (*node))
2055 struct symtab_node *n = symtab_node::get (*node);
2056 if (n && n->refuse_visibility_changes)
2057 error ("%+qD declared weakref after being used", *node);
2060 return NULL_TREE;
2063 /* Handle an "visibility" attribute; arguments as in
2064 struct attribute_spec.handler. */
2066 static tree
2067 handle_visibility_attribute (tree *node, tree name, tree args,
2068 int ARG_UNUSED (flags),
2069 bool *ARG_UNUSED (no_add_attrs))
2071 tree decl = *node;
2072 tree id = TREE_VALUE (args);
2073 enum symbol_visibility vis;
2075 if (TYPE_P (*node))
2077 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2078 /* OK */;
2079 else if (!RECORD_OR_UNION_TYPE_P (*node))
2081 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2082 name);
2083 return NULL_TREE;
2085 else if (TYPE_FIELDS (*node))
2087 error ("%qE attribute ignored because %qT is already defined",
2088 name, *node);
2089 return NULL_TREE;
2092 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2094 warning (OPT_Wattributes, "%qE attribute ignored", name);
2095 return NULL_TREE;
2098 if (TREE_CODE (id) != STRING_CST)
2100 error ("visibility argument not a string");
2101 return NULL_TREE;
2104 /* If this is a type, set the visibility on the type decl. */
2105 if (TYPE_P (decl))
2107 decl = TYPE_NAME (decl);
2108 if (!decl)
2109 return NULL_TREE;
2110 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2112 warning (OPT_Wattributes, "%qE attribute ignored on types",
2113 name);
2114 return NULL_TREE;
2118 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2119 vis = VISIBILITY_DEFAULT;
2120 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2121 vis = VISIBILITY_INTERNAL;
2122 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2123 vis = VISIBILITY_HIDDEN;
2124 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2125 vis = VISIBILITY_PROTECTED;
2126 else
2128 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2129 vis = VISIBILITY_DEFAULT;
2132 if (DECL_VISIBILITY_SPECIFIED (decl)
2133 && vis != DECL_VISIBILITY (decl))
2135 tree attributes = (TYPE_P (*node)
2136 ? TYPE_ATTRIBUTES (*node)
2137 : DECL_ATTRIBUTES (decl));
2138 if (lookup_attribute ("visibility", attributes))
2139 error ("%qD redeclared with different visibility", decl);
2140 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2141 && lookup_attribute ("dllimport", attributes))
2142 error ("%qD was declared %qs which implies default visibility",
2143 decl, "dllimport");
2144 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2145 && lookup_attribute ("dllexport", attributes))
2146 error ("%qD was declared %qs which implies default visibility",
2147 decl, "dllexport");
2150 DECL_VISIBILITY (decl) = vis;
2151 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2153 /* Go ahead and attach the attribute to the node as well. This is needed
2154 so we can determine whether we have VISIBILITY_DEFAULT because the
2155 visibility was not specified, or because it was explicitly overridden
2156 from the containing scope. */
2158 return NULL_TREE;
2161 /* Handle an "tls_model" attribute; arguments as in
2162 struct attribute_spec.handler. */
2164 static tree
2165 handle_tls_model_attribute (tree *node, tree name, tree args,
2166 int ARG_UNUSED (flags), bool *no_add_attrs)
2168 tree id;
2169 tree decl = *node;
2170 enum tls_model kind;
2172 *no_add_attrs = true;
2174 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
2176 warning (OPT_Wattributes, "%qE attribute ignored", name);
2177 return NULL_TREE;
2180 kind = DECL_TLS_MODEL (decl);
2181 id = TREE_VALUE (args);
2182 if (TREE_CODE (id) != STRING_CST)
2184 error ("tls_model argument not a string");
2185 return NULL_TREE;
2188 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2189 kind = TLS_MODEL_LOCAL_EXEC;
2190 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2191 kind = TLS_MODEL_INITIAL_EXEC;
2192 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2193 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2194 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2195 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2196 else
2197 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2199 set_decl_tls_model (decl, kind);
2200 return NULL_TREE;
2203 /* Handle a "no_instrument_function" attribute; arguments as in
2204 struct attribute_spec.handler. */
2206 static tree
2207 handle_no_instrument_function_attribute (tree *node, tree name,
2208 tree ARG_UNUSED (args),
2209 int ARG_UNUSED (flags),
2210 bool *no_add_attrs)
2212 tree decl = *node;
2214 if (TREE_CODE (decl) != FUNCTION_DECL)
2216 error_at (DECL_SOURCE_LOCATION (decl),
2217 "%qE attribute applies only to functions", name);
2218 *no_add_attrs = true;
2220 else
2221 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2223 return NULL_TREE;
2226 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2227 struct attribute_spec.handler. */
2229 static tree
2230 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2231 int, bool *no_add_attrs)
2233 if (TREE_CODE (*node) != FUNCTION_DECL)
2235 warning (OPT_Wattributes, "%qE attribute ignored", name);
2236 *no_add_attrs = true;
2239 return NULL_TREE;
2242 /* Handle a "malloc" attribute; arguments as in
2243 struct attribute_spec.handler. */
2245 static tree
2246 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2247 int ARG_UNUSED (flags), bool *no_add_attrs)
2249 if (TREE_CODE (*node) == FUNCTION_DECL
2250 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2251 DECL_IS_MALLOC (*node) = 1;
2252 else
2254 warning (OPT_Wattributes, "%qE attribute ignored", name);
2255 *no_add_attrs = true;
2258 return NULL_TREE;
2261 /* Handle a "alloc_size" attribute; arguments as in
2262 struct attribute_spec.handler. */
2264 static tree
2265 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2266 int ARG_UNUSED (flags), bool *no_add_attrs)
2268 unsigned arg_count = type_num_arguments (*node);
2269 for (; args; args = TREE_CHAIN (args))
2271 tree position = TREE_VALUE (args);
2272 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2273 && TREE_CODE (position) != FUNCTION_DECL)
2274 position = default_conversion (position);
2276 if (!tree_fits_uhwi_p (position)
2277 || !arg_count
2278 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2280 warning (OPT_Wattributes,
2281 "alloc_size parameter outside range");
2282 *no_add_attrs = true;
2283 return NULL_TREE;
2286 return NULL_TREE;
2289 /* Handle a "alloc_align" attribute; arguments as in
2290 struct attribute_spec.handler. */
2292 static tree
2293 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2294 bool *no_add_attrs)
2296 unsigned arg_count = type_num_arguments (*node);
2297 tree position = TREE_VALUE (args);
2298 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2299 && TREE_CODE (position) != FUNCTION_DECL)
2300 position = default_conversion (position);
2302 if (!tree_fits_uhwi_p (position)
2303 || !arg_count
2304 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2306 warning (OPT_Wattributes,
2307 "alloc_align parameter outside range");
2308 *no_add_attrs = true;
2309 return NULL_TREE;
2311 return NULL_TREE;
2314 /* Handle a "assume_aligned" attribute; arguments as in
2315 struct attribute_spec.handler. */
2317 static tree
2318 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2319 bool *no_add_attrs)
2321 for (; args; args = TREE_CHAIN (args))
2323 tree position = TREE_VALUE (args);
2324 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2325 && TREE_CODE (position) != FUNCTION_DECL)
2326 position = default_conversion (position);
2328 if (TREE_CODE (position) != INTEGER_CST)
2330 warning (OPT_Wattributes,
2331 "assume_aligned parameter not integer constant");
2332 *no_add_attrs = true;
2333 return NULL_TREE;
2336 return NULL_TREE;
2339 /* Handle a "fn spec" attribute; arguments as in
2340 struct attribute_spec.handler. */
2342 static tree
2343 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2344 tree args, int ARG_UNUSED (flags),
2345 bool *no_add_attrs ATTRIBUTE_UNUSED)
2347 gcc_assert (args
2348 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2349 && !TREE_CHAIN (args));
2350 return NULL_TREE;
2353 /* Handle a "bnd_variable_size" attribute; arguments as in
2354 struct attribute_spec.handler. */
2356 static tree
2357 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2358 int ARG_UNUSED (flags), bool *no_add_attrs)
2360 if (TREE_CODE (*node) != FIELD_DECL)
2362 warning (OPT_Wattributes, "%qE attribute ignored", name);
2363 *no_add_attrs = true;
2366 return NULL_TREE;
2369 /* Handle a "bnd_legacy" attribute; arguments as in
2370 struct attribute_spec.handler. */
2372 static tree
2373 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
2374 int ARG_UNUSED (flags), bool *no_add_attrs)
2376 if (TREE_CODE (*node) != FUNCTION_DECL)
2378 warning (OPT_Wattributes, "%qE attribute ignored", name);
2379 *no_add_attrs = true;
2382 return NULL_TREE;
2385 /* Handle a "bnd_instrument" attribute; arguments as in
2386 struct attribute_spec.handler. */
2388 static tree
2389 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
2390 int ARG_UNUSED (flags), bool *no_add_attrs)
2392 if (TREE_CODE (*node) != FUNCTION_DECL)
2394 warning (OPT_Wattributes, "%qE attribute ignored", name);
2395 *no_add_attrs = true;
2398 return NULL_TREE;
2401 /* Handle a "warn_unused" attribute; arguments as in
2402 struct attribute_spec.handler. */
2404 static tree
2405 handle_warn_unused_attribute (tree *node, tree name,
2406 tree args ATTRIBUTE_UNUSED,
2407 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2409 if (TYPE_P (*node))
2410 /* Do nothing else, just set the attribute. We'll get at
2411 it later with lookup_attribute. */
2413 else
2415 warning (OPT_Wattributes, "%qE attribute ignored", name);
2416 *no_add_attrs = true;
2419 return NULL_TREE;
2422 /* Handle an "omp declare simd" attribute; arguments as in
2423 struct attribute_spec.handler. */
2425 static tree
2426 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2428 return NULL_TREE;
2431 /* Handle a "simd" attribute. */
2433 static tree
2434 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2436 if (TREE_CODE (*node) == FUNCTION_DECL)
2438 if (lookup_attribute ("cilk simd function",
2439 DECL_ATTRIBUTES (*node)) != NULL)
2441 error_at (DECL_SOURCE_LOCATION (*node),
2442 "%<__simd__%> attribute cannot be used in the same "
2443 "function marked as a Cilk Plus SIMD-enabled function");
2444 *no_add_attrs = true;
2446 else
2448 tree t = get_identifier ("omp declare simd");
2449 tree attr = NULL_TREE;
2450 if (args)
2452 tree id = TREE_VALUE (args);
2454 if (TREE_CODE (id) != STRING_CST)
2456 error ("attribute %qE argument not a string", name);
2457 *no_add_attrs = true;
2458 return NULL_TREE;
2461 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2462 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2463 OMP_CLAUSE_NOTINBRANCH);
2464 else
2465 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2466 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2467 OMP_CLAUSE_INBRANCH);
2468 else
2470 error ("only %<inbranch%> and %<notinbranch%> flags are "
2471 "allowed for %<__simd__%> attribute");
2472 *no_add_attrs = true;
2473 return NULL_TREE;
2477 DECL_ATTRIBUTES (*node) = tree_cons (t,
2478 build_tree_list (NULL_TREE,
2479 attr),
2480 DECL_ATTRIBUTES (*node));
2483 else
2485 warning (OPT_Wattributes, "%qE attribute ignored", name);
2486 *no_add_attrs = true;
2489 return NULL_TREE;
2492 /* Handle an "omp declare target" attribute; arguments as in
2493 struct attribute_spec.handler. */
2495 static tree
2496 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2498 return NULL_TREE;
2501 /* Handle a "returns_twice" attribute; arguments as in
2502 struct attribute_spec.handler. */
2504 static tree
2505 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2506 int ARG_UNUSED (flags), bool *no_add_attrs)
2508 if (TREE_CODE (*node) == FUNCTION_DECL)
2509 DECL_IS_RETURNS_TWICE (*node) = 1;
2510 else
2512 warning (OPT_Wattributes, "%qE attribute ignored", name);
2513 *no_add_attrs = true;
2516 return NULL_TREE;
2519 /* Handle a "no_limit_stack" attribute; arguments as in
2520 struct attribute_spec.handler. */
2522 static tree
2523 handle_no_limit_stack_attribute (tree *node, tree name,
2524 tree ARG_UNUSED (args),
2525 int ARG_UNUSED (flags),
2526 bool *no_add_attrs)
2528 tree decl = *node;
2530 if (TREE_CODE (decl) != FUNCTION_DECL)
2532 error_at (DECL_SOURCE_LOCATION (decl),
2533 "%qE attribute applies only to functions", name);
2534 *no_add_attrs = true;
2536 else if (DECL_INITIAL (decl))
2538 error_at (DECL_SOURCE_LOCATION (decl),
2539 "can%'t set %qE attribute after definition", name);
2540 *no_add_attrs = true;
2542 else
2543 DECL_NO_LIMIT_STACK (decl) = 1;
2545 return NULL_TREE;
2548 /* Handle a "pure" attribute; arguments as in
2549 struct attribute_spec.handler. */
2551 static tree
2552 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2553 int ARG_UNUSED (flags), bool *no_add_attrs)
2555 if (TREE_CODE (*node) == FUNCTION_DECL)
2556 DECL_PURE_P (*node) = 1;
2557 /* ??? TODO: Support types. */
2558 else
2560 warning (OPT_Wattributes, "%qE attribute ignored", name);
2561 *no_add_attrs = true;
2564 return NULL_TREE;
2567 /* Digest an attribute list destined for a transactional memory statement.
2568 ALLOWED is the set of attributes that are allowed for this statement;
2569 return the attribute we parsed. Multiple attributes are never allowed. */
2572 parse_tm_stmt_attr (tree attrs, int allowed)
2574 tree a_seen = NULL;
2575 int m_seen = 0;
2577 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2579 tree a = TREE_PURPOSE (attrs);
2580 int m = 0;
2582 if (is_attribute_p ("outer", a))
2583 m = TM_STMT_ATTR_OUTER;
2585 if ((m & allowed) == 0)
2587 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2588 continue;
2591 if (m_seen == 0)
2593 a_seen = a;
2594 m_seen = m;
2596 else if (m_seen == m)
2597 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2598 else
2599 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2602 return m_seen;
2605 /* Transform a TM attribute name into a maskable integer and back.
2606 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2607 to how the lack of an attribute is treated. */
2610 tm_attr_to_mask (tree attr)
2612 if (attr == NULL)
2613 return 0;
2614 if (is_attribute_p ("transaction_safe", attr))
2615 return TM_ATTR_SAFE;
2616 if (is_attribute_p ("transaction_callable", attr))
2617 return TM_ATTR_CALLABLE;
2618 if (is_attribute_p ("transaction_pure", attr))
2619 return TM_ATTR_PURE;
2620 if (is_attribute_p ("transaction_unsafe", attr))
2621 return TM_ATTR_IRREVOCABLE;
2622 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2623 return TM_ATTR_MAY_CANCEL_OUTER;
2624 return 0;
2627 tree
2628 tm_mask_to_attr (int mask)
2630 const char *str;
2631 switch (mask)
2633 case TM_ATTR_SAFE:
2634 str = "transaction_safe";
2635 break;
2636 case TM_ATTR_CALLABLE:
2637 str = "transaction_callable";
2638 break;
2639 case TM_ATTR_PURE:
2640 str = "transaction_pure";
2641 break;
2642 case TM_ATTR_IRREVOCABLE:
2643 str = "transaction_unsafe";
2644 break;
2645 case TM_ATTR_MAY_CANCEL_OUTER:
2646 str = "transaction_may_cancel_outer";
2647 break;
2648 default:
2649 gcc_unreachable ();
2651 return get_identifier (str);
2654 /* Return the first TM attribute seen in LIST. */
2656 tree
2657 find_tm_attribute (tree list)
2659 for (; list ; list = TREE_CHAIN (list))
2661 tree name = TREE_PURPOSE (list);
2662 if (tm_attr_to_mask (name) != 0)
2663 return name;
2665 return NULL_TREE;
2668 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2669 Here we accept only function types, and verify that none of the other
2670 function TM attributes are also applied. */
2671 /* ??? We need to accept class types for C++, but not C. This greatly
2672 complicates this function, since we can no longer rely on the extra
2673 processing given by function_type_required. */
2675 static tree
2676 handle_tm_attribute (tree *node, tree name, tree args,
2677 int flags, bool *no_add_attrs)
2679 /* Only one path adds the attribute; others don't. */
2680 *no_add_attrs = true;
2682 switch (TREE_CODE (*node))
2684 case RECORD_TYPE:
2685 case UNION_TYPE:
2686 /* Only tm_callable and tm_safe apply to classes. */
2687 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2688 goto ignored;
2689 /* FALLTHRU */
2691 case FUNCTION_TYPE:
2692 case METHOD_TYPE:
2694 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2695 if (old_name == name)
2697 else if (old_name != NULL_TREE)
2698 error ("type was previously declared %qE", old_name);
2699 else
2700 *no_add_attrs = false;
2702 break;
2704 case FUNCTION_DECL:
2706 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2707 want to set transaction_safe on the type. */
2708 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2709 if (!TYPE_P (DECL_CONTEXT (*node)))
2710 error_at (DECL_SOURCE_LOCATION (*node),
2711 "%<transaction_safe_dynamic%> may only be specified for "
2712 "a virtual function");
2713 *no_add_attrs = false;
2714 decl_attributes (&TREE_TYPE (*node),
2715 build_tree_list (get_identifier ("transaction_safe"),
2716 NULL_TREE),
2718 break;
2721 case POINTER_TYPE:
2723 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2724 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2726 tree fn_tmp = TREE_TYPE (*node);
2727 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2728 *node = build_pointer_type (fn_tmp);
2729 break;
2732 /* FALLTHRU */
2734 default:
2735 /* If a function is next, pass it on to be tried next. */
2736 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2737 return tree_cons (name, args, NULL);
2739 ignored:
2740 warning (OPT_Wattributes, "%qE attribute ignored", name);
2741 break;
2744 return NULL_TREE;
2747 /* Handle the TM_WRAP attribute; arguments as in
2748 struct attribute_spec.handler. */
2750 static tree
2751 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2752 int ARG_UNUSED (flags), bool *no_add_attrs)
2754 tree decl = *node;
2756 /* We don't need the attribute even on success, since we
2757 record the entry in an external table. */
2758 *no_add_attrs = true;
2760 if (TREE_CODE (decl) != FUNCTION_DECL)
2761 warning (OPT_Wattributes, "%qE attribute ignored", name);
2762 else
2764 tree wrap_decl = TREE_VALUE (args);
2765 if (error_operand_p (wrap_decl))
2767 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2768 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2769 error ("%qE argument not an identifier", name);
2770 else
2772 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2773 wrap_decl = lookup_name (wrap_decl);
2774 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2776 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2777 TREE_TYPE (wrap_decl)))
2778 record_tm_replacement (wrap_decl, decl);
2779 else
2780 error ("%qD is not compatible with %qD", wrap_decl, decl);
2782 else
2783 error ("%qE argument is not a function", name);
2787 return NULL_TREE;
2790 /* Ignore the given attribute. Used when this attribute may be usefully
2791 overridden by the target, but is not used generically. */
2793 static tree
2794 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2795 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2796 bool *no_add_attrs)
2798 *no_add_attrs = true;
2799 return NULL_TREE;
2802 /* Handle a "no vops" attribute; arguments as in
2803 struct attribute_spec.handler. */
2805 static tree
2806 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2807 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2808 bool *ARG_UNUSED (no_add_attrs))
2810 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2811 DECL_IS_NOVOPS (*node) = 1;
2812 return NULL_TREE;
2815 /* Handle a "deprecated" attribute; arguments as in
2816 struct attribute_spec.handler. */
2818 static tree
2819 handle_deprecated_attribute (tree *node, tree name,
2820 tree args, int flags,
2821 bool *no_add_attrs)
2823 tree type = NULL_TREE;
2824 int warn = 0;
2825 tree what = NULL_TREE;
2827 if (!args)
2828 *no_add_attrs = true;
2829 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2831 error ("deprecated message is not a string");
2832 *no_add_attrs = true;
2835 if (DECL_P (*node))
2837 tree decl = *node;
2838 type = TREE_TYPE (decl);
2840 if (TREE_CODE (decl) == TYPE_DECL
2841 || TREE_CODE (decl) == PARM_DECL
2842 || VAR_OR_FUNCTION_DECL_P (decl)
2843 || TREE_CODE (decl) == FIELD_DECL
2844 || TREE_CODE (decl) == CONST_DECL
2845 || objc_method_decl (TREE_CODE (decl)))
2846 TREE_DEPRECATED (decl) = 1;
2847 else
2848 warn = 1;
2850 else if (TYPE_P (*node))
2852 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2853 *node = build_variant_type_copy (*node);
2854 TREE_DEPRECATED (*node) = 1;
2855 type = *node;
2857 else
2858 warn = 1;
2860 if (warn)
2862 *no_add_attrs = true;
2863 if (type && TYPE_NAME (type))
2865 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2866 what = TYPE_NAME (*node);
2867 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2868 && DECL_NAME (TYPE_NAME (type)))
2869 what = DECL_NAME (TYPE_NAME (type));
2871 if (what)
2872 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2873 else
2874 warning (OPT_Wattributes, "%qE attribute ignored", name);
2877 return NULL_TREE;
2880 /* Handle a "vector_size" attribute; arguments as in
2881 struct attribute_spec.handler. */
2883 static tree
2884 handle_vector_size_attribute (tree *node, tree name, tree args,
2885 int ARG_UNUSED (flags),
2886 bool *no_add_attrs)
2888 unsigned HOST_WIDE_INT vecsize, nunits;
2889 machine_mode orig_mode;
2890 tree type = *node, new_type, size;
2892 *no_add_attrs = true;
2894 size = TREE_VALUE (args);
2895 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2896 && TREE_CODE (size) != FUNCTION_DECL)
2897 size = default_conversion (size);
2899 if (!tree_fits_uhwi_p (size))
2901 warning (OPT_Wattributes, "%qE attribute ignored", name);
2902 return NULL_TREE;
2905 /* Get the vector size (in bytes). */
2906 vecsize = tree_to_uhwi (size);
2908 /* We need to provide for vector pointers, vector arrays, and
2909 functions returning vectors. For example:
2911 __attribute__((vector_size(16))) short *foo;
2913 In this case, the mode is SI, but the type being modified is
2914 HI, so we need to look further. */
2916 while (POINTER_TYPE_P (type)
2917 || TREE_CODE (type) == FUNCTION_TYPE
2918 || TREE_CODE (type) == METHOD_TYPE
2919 || TREE_CODE (type) == ARRAY_TYPE
2920 || TREE_CODE (type) == OFFSET_TYPE)
2921 type = TREE_TYPE (type);
2923 /* Get the mode of the type being modified. */
2924 orig_mode = TYPE_MODE (type);
2926 if ((!INTEGRAL_TYPE_P (type)
2927 && !SCALAR_FLOAT_TYPE_P (type)
2928 && !FIXED_POINT_TYPE_P (type))
2929 || (!SCALAR_FLOAT_MODE_P (orig_mode)
2930 && GET_MODE_CLASS (orig_mode) != MODE_INT
2931 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
2932 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
2933 || TREE_CODE (type) == BOOLEAN_TYPE)
2935 error ("invalid vector type for attribute %qE", name);
2936 return NULL_TREE;
2939 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2941 error ("vector size not an integral multiple of component size");
2942 return NULL;
2945 if (vecsize == 0)
2947 error ("zero vector size");
2948 return NULL;
2951 /* Calculate how many units fit in the vector. */
2952 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
2953 if (nunits & (nunits - 1))
2955 error ("number of components of the vector not a power of two");
2956 return NULL_TREE;
2959 new_type = build_vector_type (type, nunits);
2961 /* Build back pointers if needed. */
2962 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
2964 return NULL_TREE;
2967 /* Handle the "nonnull" attribute. */
2969 static tree
2970 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
2971 tree args, int ARG_UNUSED (flags),
2972 bool *no_add_attrs)
2974 tree type = *node;
2975 unsigned HOST_WIDE_INT attr_arg_num;
2977 /* If no arguments are specified, all pointer arguments should be
2978 non-null. Verify a full prototype is given so that the arguments
2979 will have the correct types when we actually check them later.
2980 Avoid diagnosing type-generic built-ins since those have no
2981 prototype. */
2982 if (!args)
2984 if (!prototype_p (type)
2985 && (!TYPE_ATTRIBUTES (type)
2986 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
2988 error ("nonnull attribute without arguments on a non-prototype");
2989 *no_add_attrs = true;
2991 return NULL_TREE;
2994 /* Argument list specified. Verify that each argument number references
2995 a pointer argument. */
2996 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
2998 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
3000 tree arg = TREE_VALUE (args);
3001 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
3002 && TREE_CODE (arg) != FUNCTION_DECL)
3003 TREE_VALUE (args) = arg = default_conversion (arg);
3005 if (!get_nonnull_operand (arg, &arg_num))
3007 error ("nonnull argument has invalid operand number (argument %lu)",
3008 (unsigned long) attr_arg_num);
3009 *no_add_attrs = true;
3010 return NULL_TREE;
3013 if (prototype_p (type))
3015 function_args_iterator iter;
3016 tree argument;
3018 function_args_iter_init (&iter, type);
3019 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
3021 argument = function_args_iter_cond (&iter);
3022 if (argument == NULL_TREE || ck_num == arg_num)
3023 break;
3026 if (!argument
3027 || TREE_CODE (argument) == VOID_TYPE)
3029 error ("nonnull argument with out-of-range operand number "
3030 "(argument %lu, operand %lu)",
3031 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3032 *no_add_attrs = true;
3033 return NULL_TREE;
3036 if (TREE_CODE (argument) != POINTER_TYPE)
3038 error ("nonnull argument references non-pointer operand "
3039 "(argument %lu, operand %lu)",
3040 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3041 *no_add_attrs = true;
3042 return NULL_TREE;
3047 return NULL_TREE;
3050 /* Handle the "nonstring" variable attribute. */
3052 static tree
3053 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3054 int ARG_UNUSED (flags), bool *no_add_attrs)
3056 gcc_assert (!args);
3057 tree_code code = TREE_CODE (*node);
3059 if (VAR_P (*node)
3060 || code == FIELD_DECL
3061 || code == PARM_DECL)
3063 tree type = TREE_TYPE (*node);
3065 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3067 tree eltype = TREE_TYPE (type);
3068 if (eltype == char_type_node)
3069 return NULL_TREE;
3072 warning (OPT_Wattributes,
3073 "%qE attribute ignored on objects of type %qT",
3074 name, type);
3075 *no_add_attrs = true;
3076 return NULL_TREE;
3079 if (code == FUNCTION_DECL)
3080 warning (OPT_Wattributes,
3081 "%qE attribute does not apply to functions", name);
3082 else if (code == TYPE_DECL)
3083 warning (OPT_Wattributes,
3084 "%qE attribute does not apply to types", name);
3085 else
3086 warning (OPT_Wattributes, "%qE attribute ignored", name);
3088 *no_add_attrs = true;
3089 return NULL_TREE;
3092 /* Handle a "nothrow" attribute; arguments as in
3093 struct attribute_spec.handler. */
3095 static tree
3096 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3097 int ARG_UNUSED (flags), bool *no_add_attrs)
3099 if (TREE_CODE (*node) == FUNCTION_DECL)
3100 TREE_NOTHROW (*node) = 1;
3101 /* ??? TODO: Support types. */
3102 else
3104 warning (OPT_Wattributes, "%qE attribute ignored", name);
3105 *no_add_attrs = true;
3108 return NULL_TREE;
3111 /* Handle a "cleanup" attribute; arguments as in
3112 struct attribute_spec.handler. */
3114 static tree
3115 handle_cleanup_attribute (tree *node, tree name, tree args,
3116 int ARG_UNUSED (flags), bool *no_add_attrs)
3118 tree decl = *node;
3119 tree cleanup_id, cleanup_decl;
3121 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3122 for global destructors in C++. This requires infrastructure that
3123 we don't have generically at the moment. It's also not a feature
3124 we'd be missing too much, since we do have attribute constructor. */
3125 if (!VAR_P (decl) || TREE_STATIC (decl))
3127 warning (OPT_Wattributes, "%qE attribute ignored", name);
3128 *no_add_attrs = true;
3129 return NULL_TREE;
3132 /* Verify that the argument is a function in scope. */
3133 /* ??? We could support pointers to functions here as well, if
3134 that was considered desirable. */
3135 cleanup_id = TREE_VALUE (args);
3136 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3138 error ("cleanup argument not an identifier");
3139 *no_add_attrs = true;
3140 return NULL_TREE;
3142 cleanup_decl = lookup_name (cleanup_id);
3143 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3145 error ("cleanup argument not a function");
3146 *no_add_attrs = true;
3147 return NULL_TREE;
3150 /* That the function has proper type is checked with the
3151 eventual call to build_function_call. */
3153 return NULL_TREE;
3156 /* Handle a "warn_unused_result" attribute. No special handling. */
3158 static tree
3159 handle_warn_unused_result_attribute (tree *node, tree name,
3160 tree ARG_UNUSED (args),
3161 int ARG_UNUSED (flags), bool *no_add_attrs)
3163 /* Ignore the attribute for functions not returning any value. */
3164 if (VOID_TYPE_P (TREE_TYPE (*node)))
3166 warning (OPT_Wattributes, "%qE attribute ignored", name);
3167 *no_add_attrs = true;
3170 return NULL_TREE;
3173 /* Handle a "sentinel" attribute. */
3175 static tree
3176 handle_sentinel_attribute (tree *node, tree name, tree args,
3177 int ARG_UNUSED (flags), bool *no_add_attrs)
3179 if (!prototype_p (*node))
3181 warning (OPT_Wattributes,
3182 "%qE attribute requires prototypes with named arguments", name);
3183 *no_add_attrs = true;
3185 else
3187 if (!stdarg_p (*node))
3189 warning (OPT_Wattributes,
3190 "%qE attribute only applies to variadic functions", name);
3191 *no_add_attrs = true;
3195 if (args)
3197 tree position = TREE_VALUE (args);
3198 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3199 && TREE_CODE (position) != FUNCTION_DECL)
3200 position = default_conversion (position);
3202 if (TREE_CODE (position) != INTEGER_CST
3203 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3205 warning (OPT_Wattributes,
3206 "requested position is not an integer constant");
3207 *no_add_attrs = true;
3209 else
3211 if (tree_int_cst_lt (position, integer_zero_node))
3213 warning (OPT_Wattributes,
3214 "requested position is less than zero");
3215 *no_add_attrs = true;
3220 return NULL_TREE;
3223 /* Handle a "type_generic" attribute. */
3225 static tree
3226 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3227 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3228 bool * ARG_UNUSED (no_add_attrs))
3230 /* Ensure we have a function type. */
3231 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3233 /* Ensure we have a variadic function. */
3234 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3236 return NULL_TREE;
3239 /* Handle a "target" attribute. */
3241 static tree
3242 handle_target_attribute (tree *node, tree name, tree args, int flags,
3243 bool *no_add_attrs)
3245 /* Ensure we have a function type. */
3246 if (TREE_CODE (*node) != FUNCTION_DECL)
3248 warning (OPT_Wattributes, "%qE attribute ignored", name);
3249 *no_add_attrs = true;
3251 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3253 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3254 "with %qs attribute", name, "target_clones");
3255 *no_add_attrs = true;
3257 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3258 flags))
3259 *no_add_attrs = true;
3261 /* Check that there's no empty string in values of the attribute. */
3262 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3264 tree value = TREE_VALUE (t);
3265 if (TREE_CODE (value) == STRING_CST
3266 && TREE_STRING_LENGTH (value) == 1
3267 && TREE_STRING_POINTER (value)[0] == '\0')
3269 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3270 *no_add_attrs = true;
3274 return NULL_TREE;
3277 /* Handle a "target_clones" attribute. */
3279 static tree
3280 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3281 int ARG_UNUSED (flags), bool *no_add_attrs)
3283 /* Ensure we have a function type. */
3284 if (TREE_CODE (*node) == FUNCTION_DECL)
3286 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3288 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3289 "with %qs attribute", name, "always_inline");
3290 *no_add_attrs = true;
3292 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3294 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3295 "with %qs attribute", name, "target");
3296 *no_add_attrs = true;
3298 else
3299 /* Do not inline functions with multiple clone targets. */
3300 DECL_UNINLINABLE (*node) = 1;
3302 else
3304 warning (OPT_Wattributes, "%qE attribute ignored", name);
3305 *no_add_attrs = true;
3307 return NULL_TREE;
3310 /* For handling "optimize" attribute. arguments as in
3311 struct attribute_spec.handler. */
3313 static tree
3314 handle_optimize_attribute (tree *node, tree name, tree args,
3315 int ARG_UNUSED (flags), bool *no_add_attrs)
3317 /* Ensure we have a function type. */
3318 if (TREE_CODE (*node) != FUNCTION_DECL)
3320 warning (OPT_Wattributes, "%qE attribute ignored", name);
3321 *no_add_attrs = true;
3323 else
3325 struct cl_optimization cur_opts;
3326 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3328 /* Save current options. */
3329 cl_optimization_save (&cur_opts, &global_options);
3331 /* If we previously had some optimization options, use them as the
3332 default. */
3333 if (old_opts)
3334 cl_optimization_restore (&global_options,
3335 TREE_OPTIMIZATION (old_opts));
3337 /* Parse options, and update the vector. */
3338 parse_optimize_options (args, true);
3339 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3340 = build_optimization_node (&global_options);
3342 /* Restore current options. */
3343 cl_optimization_restore (&global_options, &cur_opts);
3346 return NULL_TREE;
3349 /* Handle a "no_split_stack" attribute. */
3351 static tree
3352 handle_no_split_stack_attribute (tree *node, tree name,
3353 tree ARG_UNUSED (args),
3354 int ARG_UNUSED (flags),
3355 bool *no_add_attrs)
3357 tree decl = *node;
3359 if (TREE_CODE (decl) != FUNCTION_DECL)
3361 error_at (DECL_SOURCE_LOCATION (decl),
3362 "%qE attribute applies only to functions", name);
3363 *no_add_attrs = true;
3365 else if (DECL_INITIAL (decl))
3367 error_at (DECL_SOURCE_LOCATION (decl),
3368 "can%'t set %qE attribute after definition", name);
3369 *no_add_attrs = true;
3372 return NULL_TREE;
3375 /* Handle a "returns_nonnull" attribute; arguments as in
3376 struct attribute_spec.handler. */
3378 static tree
3379 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3380 bool *no_add_attrs)
3382 // Even without a prototype we still have a return type we can check.
3383 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3385 error ("returns_nonnull attribute on a function not returning a pointer");
3386 *no_add_attrs = true;
3388 return NULL_TREE;
3391 /* Handle a "designated_init" attribute; arguments as in
3392 struct attribute_spec.handler. */
3394 static tree
3395 handle_designated_init_attribute (tree *node, tree name, tree, int,
3396 bool *no_add_attrs)
3398 if (TREE_CODE (*node) != RECORD_TYPE)
3400 error ("%qE attribute is only valid on %<struct%> type", name);
3401 *no_add_attrs = true;
3403 return NULL_TREE;
3407 /* Handle a "fallthrough" attribute; arguments as in struct
3408 attribute_spec.handler. */
3410 static tree
3411 handle_fallthrough_attribute (tree *, tree name, tree, int,
3412 bool *no_add_attrs)
3414 warning (OPT_Wattributes, "%qE attribute ignored", name);
3415 *no_add_attrs = true;
3416 return NULL_TREE;
3419 static tree
3420 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3422 /* Nothing to be done here. */
3423 return NULL_TREE;