Use hard_regno_nregs instead of HARD_REGNO_NREGS
[official-gcc.git] / gcc / c-family / c-attribs.c
blob4f6638fd99e0a816a307b5b92c884d4b418dcc4a
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_noicf_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_always_inline_attribute (tree *, tree, tree, int,
72 bool *);
73 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
74 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
79 bool *);
80 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
81 bool *);
82 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
83 static tree handle_transparent_union_attribute (tree *, tree, tree,
84 int, bool *);
85 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
86 int, bool *);
87 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
88 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
93 int, bool *);
94 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
95 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
96 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
97 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
98 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
100 static tree handle_visibility_attribute (tree *, tree, tree, int,
101 bool *);
102 static tree handle_tls_model_attribute (tree *, tree, tree, int,
103 bool *);
104 static tree handle_no_instrument_function_attribute (tree *, tree,
105 tree, int, bool *);
106 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
107 tree, int, bool *);
108 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
109 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
111 bool *);
112 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
113 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_deprecated_attribute (tree *, tree, tree, int,
117 bool *);
118 static tree handle_vector_size_attribute (tree *, tree, tree, int,
119 bool *);
120 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
121 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
125 bool *);
126 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
127 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
134 static tree ignore_attribute (tree *, tree, tree, int, bool *);
135 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
136 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
140 bool *);
141 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
142 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
143 bool *);
144 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
145 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
147 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
148 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
149 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
150 int, bool *);
152 /* Table of machine-independent attributes common to all C-like languages.
154 All attributes referencing arguments should be additionally processed
155 in chkp_copy_function_type_adding_bounds for correct instrumentation
156 by Pointer Bounds Checker.
157 Current list of processed common attributes: nonnull. */
158 const struct attribute_spec c_common_attribute_table[] =
160 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
161 affects_type_identity } */
162 { "packed", 0, 0, false, false, false,
163 handle_packed_attribute , false},
164 { "nocommon", 0, 0, true, false, false,
165 handle_nocommon_attribute, false},
166 { "common", 0, 0, true, false, false,
167 handle_common_attribute, false },
168 /* FIXME: logically, noreturn attributes should be listed as
169 "false, true, true" and apply to function types. But implementing this
170 would require all the places in the compiler that use TREE_THIS_VOLATILE
171 on a decl to identify non-returning functions to be located and fixed
172 to check the function type instead. */
173 { "noreturn", 0, 0, true, false, false,
174 handle_noreturn_attribute, false },
175 { "volatile", 0, 0, true, false, false,
176 handle_noreturn_attribute, false },
177 { "stack_protect", 0, 0, true, false, false,
178 handle_stack_protect_attribute, false },
179 { "noinline", 0, 0, true, false, false,
180 handle_noinline_attribute, false },
181 { "noclone", 0, 0, true, false, false,
182 handle_noclone_attribute, false },
183 { "no_icf", 0, 0, true, false, false,
184 handle_noicf_attribute, false },
185 { "noipa", 0, 0, true, false, false,
186 handle_noipa_attribute, false },
187 { "leaf", 0, 0, true, false, false,
188 handle_leaf_attribute, false },
189 { "always_inline", 0, 0, true, false, false,
190 handle_always_inline_attribute, false },
191 { "gnu_inline", 0, 0, true, false, false,
192 handle_gnu_inline_attribute, false },
193 { "artificial", 0, 0, true, false, false,
194 handle_artificial_attribute, false },
195 { "flatten", 0, 0, true, false, false,
196 handle_flatten_attribute, false },
197 { "used", 0, 0, true, false, false,
198 handle_used_attribute, false },
199 { "unused", 0, 0, false, false, false,
200 handle_unused_attribute, false },
201 { "externally_visible", 0, 0, true, false, false,
202 handle_externally_visible_attribute, false },
203 { "no_reorder", 0, 0, true, false, false,
204 handle_no_reorder_attribute, false },
205 /* The same comments as for noreturn attributes apply to const ones. */
206 { "const", 0, 0, true, false, false,
207 handle_const_attribute, false },
208 { "scalar_storage_order", 1, 1, false, false, false,
209 handle_scalar_storage_order_attribute, false },
210 { "transparent_union", 0, 0, false, false, false,
211 handle_transparent_union_attribute, false },
212 { "constructor", 0, 1, true, false, false,
213 handle_constructor_attribute, false },
214 { "destructor", 0, 1, true, false, false,
215 handle_destructor_attribute, false },
216 { "mode", 1, 1, false, true, false,
217 handle_mode_attribute, false },
218 { "section", 1, 1, true, false, false,
219 handle_section_attribute, false },
220 { "aligned", 0, 1, false, false, false,
221 handle_aligned_attribute, false },
222 { "warn_if_not_aligned", 0, 1, false, false, false,
223 handle_warn_if_not_aligned_attribute,
224 false },
225 { "weak", 0, 0, true, false, false,
226 handle_weak_attribute, false },
227 { "noplt", 0, 0, true, false, false,
228 handle_noplt_attribute, false },
229 { "ifunc", 1, 1, true, false, false,
230 handle_ifunc_attribute, false },
231 { "alias", 1, 1, true, false, false,
232 handle_alias_attribute, false },
233 { "weakref", 0, 1, true, false, false,
234 handle_weakref_attribute, false },
235 { "no_instrument_function", 0, 0, true, false, false,
236 handle_no_instrument_function_attribute,
237 false },
238 { "no_profile_instrument_function", 0, 0, true, false, false,
239 handle_no_profile_instrument_function_attribute,
240 false },
241 { "malloc", 0, 0, true, false, false,
242 handle_malloc_attribute, false },
243 { "returns_twice", 0, 0, true, false, false,
244 handle_returns_twice_attribute, false },
245 { "no_stack_limit", 0, 0, true, false, false,
246 handle_no_limit_stack_attribute, false },
247 { "pure", 0, 0, true, false, false,
248 handle_pure_attribute, false },
249 { "transaction_callable", 0, 0, false, true, false,
250 handle_tm_attribute, false },
251 { "transaction_unsafe", 0, 0, false, true, false,
252 handle_tm_attribute, true },
253 { "transaction_safe", 0, 0, false, true, false,
254 handle_tm_attribute, true },
255 { "transaction_safe_dynamic", 0, 0, true, false, false,
256 handle_tm_attribute, false },
257 { "transaction_may_cancel_outer", 0, 0, false, true, false,
258 handle_tm_attribute, false },
259 /* ??? These two attributes didn't make the transition from the
260 Intel language document to the multi-vendor language document. */
261 { "transaction_pure", 0, 0, false, true, false,
262 handle_tm_attribute, false },
263 { "transaction_wrap", 1, 1, true, false, false,
264 handle_tm_wrap_attribute, false },
265 /* For internal use (marking of builtins) only. The name contains space
266 to prevent its usage in source code. */
267 { "no vops", 0, 0, true, false, false,
268 handle_novops_attribute, false },
269 { "deprecated", 0, 1, false, false, false,
270 handle_deprecated_attribute, false },
271 { "vector_size", 1, 1, false, true, false,
272 handle_vector_size_attribute, true },
273 { "visibility", 1, 1, false, false, false,
274 handle_visibility_attribute, false },
275 { "tls_model", 1, 1, true, false, false,
276 handle_tls_model_attribute, false },
277 { "nonnull", 0, -1, false, true, true,
278 handle_nonnull_attribute, false },
279 { "nonstring", 0, 0, true, false, false,
280 handle_nonstring_attribute, false },
281 { "nothrow", 0, 0, true, false, false,
282 handle_nothrow_attribute, false },
283 { "may_alias", 0, 0, false, true, false, NULL, false },
284 { "cleanup", 1, 1, true, false, false,
285 handle_cleanup_attribute, false },
286 { "warn_unused_result", 0, 0, false, true, true,
287 handle_warn_unused_result_attribute, false },
288 { "sentinel", 0, 1, false, true, true,
289 handle_sentinel_attribute, false },
290 /* For internal use (marking of builtins) only. The name contains space
291 to prevent its usage in source code. */
292 { "type generic", 0, 0, false, true, true,
293 handle_type_generic_attribute, false },
294 { "alloc_size", 1, 2, false, true, true,
295 handle_alloc_size_attribute, false },
296 { "cold", 0, 0, true, false, false,
297 handle_cold_attribute, false },
298 { "hot", 0, 0, true, false, false,
299 handle_hot_attribute, false },
300 { "no_address_safety_analysis",
301 0, 0, true, false, false,
302 handle_no_address_safety_analysis_attribute,
303 false },
304 { "no_sanitize", 1, 1, true, false, false,
305 handle_no_sanitize_attribute,
306 false },
307 { "no_sanitize_address", 0, 0, true, false, false,
308 handle_no_sanitize_address_attribute,
309 false },
310 { "no_sanitize_thread", 0, 0, true, false, false,
311 handle_no_sanitize_thread_attribute,
312 false },
313 { "no_sanitize_undefined", 0, 0, true, false, false,
314 handle_no_sanitize_undefined_attribute,
315 false },
316 { "asan odr indicator", 0, 0, true, false, false,
317 handle_asan_odr_indicator_attribute,
318 false },
319 { "warning", 1, 1, true, false, false,
320 handle_error_attribute, false },
321 { "error", 1, 1, true, false, false,
322 handle_error_attribute, false },
323 { "target", 1, -1, true, false, false,
324 handle_target_attribute, false },
325 { "target_clones", 1, -1, true, false, false,
326 handle_target_clones_attribute, false },
327 { "optimize", 1, -1, true, false, false,
328 handle_optimize_attribute, false },
329 /* For internal use only. The leading '*' both prevents its usage in
330 source code and signals that it may be overridden by machine tables. */
331 { "*tm regparm", 0, 0, false, true, true,
332 ignore_attribute, false },
333 { "no_split_stack", 0, 0, true, false, false,
334 handle_no_split_stack_attribute, false },
335 /* For internal use (marking of builtins and runtime functions) only.
336 The name contains space to prevent its usage in source code. */
337 { "fn spec", 1, 1, false, true, true,
338 handle_fnspec_attribute, false },
339 { "warn_unused", 0, 0, false, false, false,
340 handle_warn_unused_attribute, false },
341 { "returns_nonnull", 0, 0, false, true, true,
342 handle_returns_nonnull_attribute, false },
343 { "omp declare simd", 0, -1, true, false, false,
344 handle_omp_declare_simd_attribute, false },
345 { "cilk simd function", 0, -1, true, false, false,
346 handle_omp_declare_simd_attribute, false },
347 { "simd", 0, 1, true, false, false,
348 handle_simd_attribute, false },
349 { "omp declare target", 0, 0, true, false, false,
350 handle_omp_declare_target_attribute, false },
351 { "omp declare target link", 0, 0, true, false, false,
352 handle_omp_declare_target_attribute, false },
353 { "alloc_align", 1, 1, false, true, true,
354 handle_alloc_align_attribute, false },
355 { "assume_aligned", 1, 2, false, true, true,
356 handle_assume_aligned_attribute, false },
357 { "designated_init", 0, 0, false, true, false,
358 handle_designated_init_attribute, false },
359 { "bnd_variable_size", 0, 0, true, false, false,
360 handle_bnd_variable_size_attribute, false },
361 { "bnd_legacy", 0, 0, true, false, false,
362 handle_bnd_legacy, false },
363 { "bnd_instrument", 0, 0, true, false, false,
364 handle_bnd_instrument, false },
365 { "fallthrough", 0, 0, false, false, false,
366 handle_fallthrough_attribute, false },
367 { "patchable_function_entry", 1, 2, true, false, false,
368 handle_patchable_function_entry_attribute,
369 false },
370 { NULL, 0, 0, false, false, false, NULL, false }
373 /* Give the specifications for the format attributes, used by C and all
374 descendants.
376 All attributes referencing arguments should be additionally processed
377 in chkp_copy_function_type_adding_bounds for correct instrumentation
378 by Pointer Bounds Checker.
379 Current list of processed format attributes: format, format_arg. */
380 const struct attribute_spec c_common_format_attribute_table[] =
382 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
383 affects_type_identity } */
384 { "format", 3, 3, false, true, true,
385 handle_format_attribute, false },
386 { "format_arg", 1, 1, false, true, true,
387 handle_format_arg_attribute, false },
388 { NULL, 0, 0, false, false, false, NULL, false }
391 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
392 identifier as an argument, so the front end shouldn't look it up. */
394 bool
395 attribute_takes_identifier_p (const_tree attr_id)
397 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
398 if (spec == NULL)
399 /* Unknown attribute that we'll end up ignoring, return true so we
400 don't complain about an identifier argument. */
401 return true;
402 else if (!strcmp ("mode", spec->name)
403 || !strcmp ("format", spec->name)
404 || !strcmp ("cleanup", spec->name))
405 return true;
406 else
407 return targetm.attribute_takes_identifier_p (attr_id);
410 /* Attribute handlers common to C front ends. */
412 /* Handle a "packed" attribute; arguments as in
413 struct attribute_spec.handler. */
415 static tree
416 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
417 int flags, bool *no_add_attrs)
419 if (TYPE_P (*node))
421 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
422 *node = build_variant_type_copy (*node);
423 TYPE_PACKED (*node) = 1;
425 else if (TREE_CODE (*node) == FIELD_DECL)
427 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
428 /* Still pack bitfields. */
429 && ! DECL_INITIAL (*node))
430 warning (OPT_Wattributes,
431 "%qE attribute ignored for field of type %qT",
432 name, TREE_TYPE (*node));
433 else
434 DECL_PACKED (*node) = 1;
436 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
437 used for DECL_REGISTER. It wouldn't mean anything anyway.
438 We can't set DECL_PACKED on the type of a TYPE_DECL, because
439 that changes what the typedef is typing. */
440 else
442 warning (OPT_Wattributes, "%qE attribute ignored", name);
443 *no_add_attrs = true;
446 return NULL_TREE;
449 /* Handle a "nocommon" attribute; arguments as in
450 struct attribute_spec.handler. */
452 static tree
453 handle_nocommon_attribute (tree *node, tree name,
454 tree ARG_UNUSED (args),
455 int ARG_UNUSED (flags), bool *no_add_attrs)
457 if (VAR_P (*node))
458 DECL_COMMON (*node) = 0;
459 else
461 warning (OPT_Wattributes, "%qE attribute ignored", name);
462 *no_add_attrs = true;
465 return NULL_TREE;
468 /* Handle a "common" attribute; arguments as in
469 struct attribute_spec.handler. */
471 static tree
472 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
473 int ARG_UNUSED (flags), bool *no_add_attrs)
475 if (VAR_P (*node))
476 DECL_COMMON (*node) = 1;
477 else
479 warning (OPT_Wattributes, "%qE attribute ignored", name);
480 *no_add_attrs = true;
483 return NULL_TREE;
486 /* Handle a "noreturn" attribute; arguments as in
487 struct attribute_spec.handler. */
489 static tree
490 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
491 int ARG_UNUSED (flags), bool *no_add_attrs)
493 tree type = TREE_TYPE (*node);
495 /* See FIXME comment in c_common_attribute_table. */
496 if (TREE_CODE (*node) == FUNCTION_DECL
497 || objc_method_decl (TREE_CODE (*node)))
498 TREE_THIS_VOLATILE (*node) = 1;
499 else if (TREE_CODE (type) == POINTER_TYPE
500 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
501 TREE_TYPE (*node)
502 = (build_qualified_type
503 (build_pointer_type
504 (build_type_variant (TREE_TYPE (type),
505 TYPE_READONLY (TREE_TYPE (type)), 1)),
506 TYPE_QUALS (type)));
507 else
509 warning (OPT_Wattributes, "%qE attribute ignored", name);
510 *no_add_attrs = true;
513 return NULL_TREE;
516 /* Handle a "hot" and attribute; arguments as in
517 struct attribute_spec.handler. */
519 static tree
520 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
521 int ARG_UNUSED (flags), bool *no_add_attrs)
523 if (TREE_CODE (*node) == FUNCTION_DECL
524 || TREE_CODE (*node) == LABEL_DECL)
526 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
528 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
529 "with attribute %qs", name, "cold");
530 *no_add_attrs = true;
532 /* Most of the rest of the hot processing is done later with
533 lookup_attribute. */
535 else
537 warning (OPT_Wattributes, "%qE attribute ignored", name);
538 *no_add_attrs = true;
541 return NULL_TREE;
544 /* Handle a "cold" and attribute; arguments as in
545 struct attribute_spec.handler. */
547 static tree
548 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
549 int ARG_UNUSED (flags), bool *no_add_attrs)
551 if (TREE_CODE (*node) == FUNCTION_DECL
552 || TREE_CODE (*node) == LABEL_DECL)
554 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
556 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
557 "with attribute %qs", name, "hot");
558 *no_add_attrs = true;
560 /* Most of the rest of the cold processing is done later with
561 lookup_attribute. */
563 else
565 warning (OPT_Wattributes, "%qE attribute ignored", name);
566 *no_add_attrs = true;
569 return NULL_TREE;
572 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
574 void
575 add_no_sanitize_value (tree node, unsigned int flags)
577 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
578 if (attr)
580 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
581 flags |= old_value;
583 if (flags == old_value)
584 return;
586 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
588 else
589 DECL_ATTRIBUTES (node)
590 = tree_cons (get_identifier ("no_sanitize"),
591 build_int_cst (unsigned_type_node, flags),
592 DECL_ATTRIBUTES (node));
595 /* Handle a "no_sanitize" attribute; arguments as in
596 struct attribute_spec.handler. */
598 static tree
599 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
600 bool *no_add_attrs)
602 *no_add_attrs = true;
603 tree id = TREE_VALUE (args);
604 if (TREE_CODE (*node) != FUNCTION_DECL)
606 warning (OPT_Wattributes, "%qE attribute ignored", name);
607 return NULL_TREE;
610 if (TREE_CODE (id) != STRING_CST)
612 error ("no_sanitize argument not a string");
613 return NULL_TREE;
616 char *error_value = NULL;
617 char *string = ASTRDUP (TREE_STRING_POINTER (id));
618 unsigned int flags = parse_no_sanitize_attribute (string, &error_value);
620 if (error_value)
622 error ("wrong argument: \"%s\"", error_value);
623 return NULL_TREE;
626 add_no_sanitize_value (*node, flags);
628 return NULL_TREE;
631 /* Handle a "no_sanitize_address" attribute; arguments as in
632 struct attribute_spec.handler. */
634 static tree
635 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
636 bool *no_add_attrs)
638 *no_add_attrs = true;
639 if (TREE_CODE (*node) != FUNCTION_DECL)
640 warning (OPT_Wattributes, "%qE attribute ignored", name);
641 else
642 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
644 return NULL_TREE;
647 /* Handle a "no_sanitize_thread" attribute; arguments as in
648 struct attribute_spec.handler. */
650 static tree
651 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
652 bool *no_add_attrs)
654 *no_add_attrs = true;
655 if (TREE_CODE (*node) != FUNCTION_DECL)
656 warning (OPT_Wattributes, "%qE attribute ignored", name);
657 else
658 add_no_sanitize_value (*node, SANITIZE_THREAD);
660 return NULL_TREE;
664 /* Handle a "no_address_safety_analysis" attribute; arguments as in
665 struct attribute_spec.handler. */
667 static tree
668 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
669 bool *no_add_attrs)
671 *no_add_attrs = true;
672 if (TREE_CODE (*node) != FUNCTION_DECL)
673 warning (OPT_Wattributes, "%qE attribute ignored", name);
674 else
675 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
677 return NULL_TREE;
680 /* Handle a "no_sanitize_undefined" attribute; arguments as in
681 struct attribute_spec.handler. */
683 static tree
684 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
685 bool *no_add_attrs)
687 *no_add_attrs = true;
688 if (TREE_CODE (*node) != FUNCTION_DECL)
689 warning (OPT_Wattributes, "%qE attribute ignored", name);
690 else
691 add_no_sanitize_value (*node,
692 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
694 return NULL_TREE;
697 /* Handle an "asan odr indicator" attribute; arguments as in
698 struct attribute_spec.handler. */
700 static tree
701 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
703 return NULL_TREE;
706 /* Handle a "stack_protect" attribute; arguments as in
707 struct attribute_spec.handler. */
709 static tree
710 handle_stack_protect_attribute (tree *node, tree name, tree, int,
711 bool *no_add_attrs)
713 if (TREE_CODE (*node) != FUNCTION_DECL)
715 warning (OPT_Wattributes, "%qE attribute ignored", name);
716 *no_add_attrs = true;
719 return NULL_TREE;
722 /* Handle a "noipa" attribute; arguments as in
723 struct attribute_spec.handler. */
725 static tree
726 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
728 if (TREE_CODE (*node) != FUNCTION_DECL)
730 warning (OPT_Wattributes, "%qE attribute ignored", name);
731 *no_add_attrs = true;
733 else
734 DECL_ATTRIBUTES (*node)
735 = tree_cons (get_identifier ("stack_protect"),
736 NULL_TREE, DECL_ATTRIBUTES (*node));
738 return NULL_TREE;
741 /* Handle a "noinline" attribute; arguments as in
742 struct attribute_spec.handler. */
744 static tree
745 handle_noinline_attribute (tree *node, tree name,
746 tree ARG_UNUSED (args),
747 int ARG_UNUSED (flags), bool *no_add_attrs)
749 if (TREE_CODE (*node) == FUNCTION_DECL)
751 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
753 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
754 "with attribute %qs", name, "always_inline");
755 *no_add_attrs = true;
757 else
758 DECL_UNINLINABLE (*node) = 1;
760 else
762 warning (OPT_Wattributes, "%qE attribute ignored", name);
763 *no_add_attrs = true;
766 return NULL_TREE;
769 /* Handle a "noclone" attribute; arguments as in
770 struct attribute_spec.handler. */
772 static tree
773 handle_noclone_attribute (tree *node, tree name,
774 tree ARG_UNUSED (args),
775 int ARG_UNUSED (flags), bool *no_add_attrs)
777 if (TREE_CODE (*node) != FUNCTION_DECL)
779 warning (OPT_Wattributes, "%qE attribute ignored", name);
780 *no_add_attrs = true;
783 return NULL_TREE;
786 /* Handle a "no_icf" attribute; arguments as in
787 struct attribute_spec.handler. */
789 static tree
790 handle_noicf_attribute (tree *node, tree name,
791 tree ARG_UNUSED (args),
792 int ARG_UNUSED (flags), bool *no_add_attrs)
794 if (TREE_CODE (*node) != FUNCTION_DECL)
796 warning (OPT_Wattributes, "%qE attribute ignored", name);
797 *no_add_attrs = true;
800 return NULL_TREE;
804 /* Handle a "always_inline" attribute; arguments as in
805 struct attribute_spec.handler. */
807 static tree
808 handle_always_inline_attribute (tree *node, tree name,
809 tree ARG_UNUSED (args),
810 int ARG_UNUSED (flags),
811 bool *no_add_attrs)
813 if (TREE_CODE (*node) == FUNCTION_DECL)
815 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
817 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
818 "with %qs attribute", name, "noinline");
819 *no_add_attrs = true;
821 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
823 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
824 "with %qs attribute", name, "target_clones");
825 *no_add_attrs = true;
827 else
828 /* Set the attribute and mark it for disregarding inline
829 limits. */
830 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
832 else
834 warning (OPT_Wattributes, "%qE attribute ignored", name);
835 *no_add_attrs = true;
838 return NULL_TREE;
841 /* Handle a "gnu_inline" attribute; arguments as in
842 struct attribute_spec.handler. */
844 static tree
845 handle_gnu_inline_attribute (tree *node, tree name,
846 tree ARG_UNUSED (args),
847 int ARG_UNUSED (flags),
848 bool *no_add_attrs)
850 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
852 /* Do nothing else, just set the attribute. We'll get at
853 it later with lookup_attribute. */
855 else
857 warning (OPT_Wattributes, "%qE attribute ignored", name);
858 *no_add_attrs = true;
861 return NULL_TREE;
864 /* Handle a "leaf" attribute; arguments as in
865 struct attribute_spec.handler. */
867 static tree
868 handle_leaf_attribute (tree *node, tree name,
869 tree ARG_UNUSED (args),
870 int ARG_UNUSED (flags), bool *no_add_attrs)
872 if (TREE_CODE (*node) != FUNCTION_DECL)
874 warning (OPT_Wattributes, "%qE attribute ignored", name);
875 *no_add_attrs = true;
877 if (!TREE_PUBLIC (*node))
879 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
880 "functions", name);
881 *no_add_attrs = true;
884 return NULL_TREE;
887 /* Handle an "artificial" attribute; arguments as in
888 struct attribute_spec.handler. */
890 static tree
891 handle_artificial_attribute (tree *node, tree name,
892 tree ARG_UNUSED (args),
893 int ARG_UNUSED (flags),
894 bool *no_add_attrs)
896 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
898 /* Do nothing else, just set the attribute. We'll get at
899 it later with lookup_attribute. */
901 else
903 warning (OPT_Wattributes, "%qE attribute ignored", name);
904 *no_add_attrs = true;
907 return NULL_TREE;
910 /* Handle a "flatten" attribute; arguments as in
911 struct attribute_spec.handler. */
913 static tree
914 handle_flatten_attribute (tree *node, tree name,
915 tree args ATTRIBUTE_UNUSED,
916 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
918 if (TREE_CODE (*node) == FUNCTION_DECL)
919 /* Do nothing else, just set the attribute. We'll get at
920 it later with lookup_attribute. */
922 else
924 warning (OPT_Wattributes, "%qE attribute ignored", name);
925 *no_add_attrs = true;
928 return NULL_TREE;
931 /* Handle a "warning" or "error" attribute; arguments as in
932 struct attribute_spec.handler. */
934 static tree
935 handle_error_attribute (tree *node, tree name, tree args,
936 int ARG_UNUSED (flags), bool *no_add_attrs)
938 if (TREE_CODE (*node) == FUNCTION_DECL
939 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
940 /* Do nothing else, just set the attribute. We'll get at
941 it later with lookup_attribute. */
943 else
945 warning (OPT_Wattributes, "%qE attribute ignored", name);
946 *no_add_attrs = true;
949 return NULL_TREE;
952 /* Handle a "used" attribute; arguments as in
953 struct attribute_spec.handler. */
955 static tree
956 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
957 int ARG_UNUSED (flags), bool *no_add_attrs)
959 tree node = *pnode;
961 if (TREE_CODE (node) == FUNCTION_DECL
962 || (VAR_P (node) && TREE_STATIC (node))
963 || (TREE_CODE (node) == TYPE_DECL))
965 TREE_USED (node) = 1;
966 DECL_PRESERVE_P (node) = 1;
967 if (VAR_P (node))
968 DECL_READ_P (node) = 1;
970 else
972 warning (OPT_Wattributes, "%qE attribute ignored", name);
973 *no_add_attrs = true;
976 return NULL_TREE;
979 /* Handle a "unused" attribute; arguments as in
980 struct attribute_spec.handler. */
982 tree
983 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
984 int flags, bool *no_add_attrs)
986 if (DECL_P (*node))
988 tree decl = *node;
990 if (TREE_CODE (decl) == PARM_DECL
991 || VAR_OR_FUNCTION_DECL_P (decl)
992 || TREE_CODE (decl) == LABEL_DECL
993 || TREE_CODE (decl) == CONST_DECL
994 || TREE_CODE (decl) == TYPE_DECL)
996 TREE_USED (decl) = 1;
997 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
998 DECL_READ_P (decl) = 1;
1000 else
1002 warning (OPT_Wattributes, "%qE attribute ignored", name);
1003 *no_add_attrs = true;
1006 else
1008 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1009 *node = build_variant_type_copy (*node);
1010 TREE_USED (*node) = 1;
1013 return NULL_TREE;
1016 /* Handle a "externally_visible" attribute; arguments as in
1017 struct attribute_spec.handler. */
1019 static tree
1020 handle_externally_visible_attribute (tree *pnode, tree name,
1021 tree ARG_UNUSED (args),
1022 int ARG_UNUSED (flags),
1023 bool *no_add_attrs)
1025 tree node = *pnode;
1027 if (VAR_OR_FUNCTION_DECL_P (node))
1029 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1030 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1032 warning (OPT_Wattributes,
1033 "%qE attribute have effect only on public objects", name);
1034 *no_add_attrs = true;
1037 else
1039 warning (OPT_Wattributes, "%qE attribute ignored", name);
1040 *no_add_attrs = true;
1043 return NULL_TREE;
1046 /* Handle the "no_reorder" attribute. Arguments as in
1047 struct attribute_spec.handler. */
1049 static tree
1050 handle_no_reorder_attribute (tree *pnode,
1051 tree name,
1052 tree,
1053 int,
1054 bool *no_add_attrs)
1056 tree node = *pnode;
1058 if (!VAR_OR_FUNCTION_DECL_P (node)
1059 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1061 warning (OPT_Wattributes,
1062 "%qE attribute only affects top level objects",
1063 name);
1064 *no_add_attrs = true;
1067 return NULL_TREE;
1070 /* Handle a "const" attribute; arguments as in
1071 struct attribute_spec.handler. */
1073 static tree
1074 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1075 int ARG_UNUSED (flags), bool *no_add_attrs)
1077 tree type = TREE_TYPE (*node);
1079 /* See FIXME comment on noreturn in c_common_attribute_table. */
1080 if (TREE_CODE (*node) == FUNCTION_DECL)
1081 TREE_READONLY (*node) = 1;
1082 else if (TREE_CODE (type) == POINTER_TYPE
1083 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1084 TREE_TYPE (*node)
1085 = (build_qualified_type
1086 (build_pointer_type
1087 (build_type_variant (TREE_TYPE (type), 1,
1088 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1089 TYPE_QUALS (type)));
1090 else
1092 warning (OPT_Wattributes, "%qE attribute ignored", name);
1093 *no_add_attrs = true;
1096 return NULL_TREE;
1099 /* Handle a "scalar_storage_order" attribute; arguments as in
1100 struct attribute_spec.handler. */
1102 static tree
1103 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1104 int flags, bool *no_add_attrs)
1106 tree id = TREE_VALUE (args);
1107 tree type;
1109 if (TREE_CODE (*node) == TYPE_DECL
1110 && ! (flags & ATTR_FLAG_CXX11))
1111 node = &TREE_TYPE (*node);
1112 type = *node;
1114 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1116 error ("scalar_storage_order is not supported because endianness "
1117 "is not uniform");
1118 return NULL_TREE;
1121 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1123 bool reverse = false;
1125 if (TREE_CODE (id) == STRING_CST
1126 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1127 reverse = !BYTES_BIG_ENDIAN;
1128 else if (TREE_CODE (id) == STRING_CST
1129 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1130 reverse = BYTES_BIG_ENDIAN;
1131 else
1133 error ("scalar_storage_order argument must be one of \"big-endian\""
1134 " or \"little-endian\"");
1135 return NULL_TREE;
1138 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1140 if (reverse)
1141 /* A type variant isn't good enough, since we don't want a cast
1142 to such a type to be removed as a no-op. */
1143 *node = type = build_duplicate_type (type);
1146 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1147 return NULL_TREE;
1150 warning (OPT_Wattributes, "%qE attribute ignored", name);
1151 *no_add_attrs = true;
1152 return NULL_TREE;
1155 /* Handle a "transparent_union" attribute; arguments as in
1156 struct attribute_spec.handler. */
1158 static tree
1159 handle_transparent_union_attribute (tree *node, tree name,
1160 tree ARG_UNUSED (args), int flags,
1161 bool *no_add_attrs)
1163 tree type;
1165 *no_add_attrs = true;
1167 if (TREE_CODE (*node) == TYPE_DECL
1168 && ! (flags & ATTR_FLAG_CXX11))
1169 node = &TREE_TYPE (*node);
1170 type = *node;
1172 if (TREE_CODE (type) == UNION_TYPE)
1174 /* Make sure that the first field will work for a transparent union.
1175 If the type isn't complete yet, leave the check to the code in
1176 finish_struct. */
1177 if (TYPE_SIZE (type))
1179 tree first = first_field (type);
1180 if (first == NULL_TREE
1181 || DECL_ARTIFICIAL (first)
1182 || TYPE_MODE (type) != DECL_MODE (first))
1183 goto ignored;
1186 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1188 /* If the type isn't complete yet, setting the flag
1189 on a variant wouldn't ever be checked. */
1190 if (!TYPE_SIZE (type))
1191 goto ignored;
1193 /* build_duplicate_type doesn't work for C++. */
1194 if (c_dialect_cxx ())
1195 goto ignored;
1197 /* A type variant isn't good enough, since we don't want a cast
1198 to such a type to be removed as a no-op. */
1199 *node = type = build_duplicate_type (type);
1202 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1203 TYPE_TRANSPARENT_AGGR (t) = 1;
1204 return NULL_TREE;
1207 ignored:
1208 warning (OPT_Wattributes, "%qE attribute ignored", name);
1209 return NULL_TREE;
1212 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1213 get the requested priority for a constructor or destructor,
1214 possibly issuing diagnostics for invalid or reserved
1215 priorities. */
1217 static priority_type
1218 get_priority (tree args, bool is_destructor)
1220 HOST_WIDE_INT pri;
1221 tree arg;
1223 if (!args)
1224 return DEFAULT_INIT_PRIORITY;
1226 if (!SUPPORTS_INIT_PRIORITY)
1228 if (is_destructor)
1229 error ("destructor priorities are not supported");
1230 else
1231 error ("constructor priorities are not supported");
1232 return DEFAULT_INIT_PRIORITY;
1235 arg = TREE_VALUE (args);
1236 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1237 goto invalid;
1238 if (arg == error_mark_node)
1239 return DEFAULT_INIT_PRIORITY;
1240 arg = default_conversion (arg);
1241 if (!tree_fits_shwi_p (arg)
1242 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1243 goto invalid;
1245 pri = tree_to_shwi (arg);
1246 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1247 goto invalid;
1249 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1251 if (is_destructor)
1252 warning (0,
1253 "destructor priorities from 0 to %d are reserved "
1254 "for the implementation",
1255 MAX_RESERVED_INIT_PRIORITY);
1256 else
1257 warning (0,
1258 "constructor priorities from 0 to %d are reserved "
1259 "for the implementation",
1260 MAX_RESERVED_INIT_PRIORITY);
1262 return pri;
1264 invalid:
1265 if (is_destructor)
1266 error ("destructor priorities must be integers from 0 to %d inclusive",
1267 MAX_INIT_PRIORITY);
1268 else
1269 error ("constructor priorities must be integers from 0 to %d inclusive",
1270 MAX_INIT_PRIORITY);
1271 return DEFAULT_INIT_PRIORITY;
1274 /* Handle a "constructor" attribute; arguments as in
1275 struct attribute_spec.handler. */
1277 static tree
1278 handle_constructor_attribute (tree *node, tree name, tree args,
1279 int ARG_UNUSED (flags),
1280 bool *no_add_attrs)
1282 tree decl = *node;
1283 tree type = TREE_TYPE (decl);
1285 if (TREE_CODE (decl) == FUNCTION_DECL
1286 && TREE_CODE (type) == FUNCTION_TYPE
1287 && decl_function_context (decl) == 0)
1289 priority_type priority;
1290 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1291 priority = get_priority (args, /*is_destructor=*/false);
1292 SET_DECL_INIT_PRIORITY (decl, priority);
1293 TREE_USED (decl) = 1;
1295 else
1297 warning (OPT_Wattributes, "%qE attribute ignored", name);
1298 *no_add_attrs = true;
1301 return NULL_TREE;
1304 /* Handle a "destructor" attribute; arguments as in
1305 struct attribute_spec.handler. */
1307 static tree
1308 handle_destructor_attribute (tree *node, tree name, tree args,
1309 int ARG_UNUSED (flags),
1310 bool *no_add_attrs)
1312 tree decl = *node;
1313 tree type = TREE_TYPE (decl);
1315 if (TREE_CODE (decl) == FUNCTION_DECL
1316 && TREE_CODE (type) == FUNCTION_TYPE
1317 && decl_function_context (decl) == 0)
1319 priority_type priority;
1320 DECL_STATIC_DESTRUCTOR (decl) = 1;
1321 priority = get_priority (args, /*is_destructor=*/true);
1322 SET_DECL_FINI_PRIORITY (decl, priority);
1323 TREE_USED (decl) = 1;
1325 else
1327 warning (OPT_Wattributes, "%qE attribute ignored", name);
1328 *no_add_attrs = true;
1331 return NULL_TREE;
1334 /* Nonzero if the mode is a valid vector mode for this architecture.
1335 This returns nonzero even if there is no hardware support for the
1336 vector mode, but we can emulate with narrower modes. */
1338 static bool
1339 vector_mode_valid_p (machine_mode mode)
1341 enum mode_class mclass = GET_MODE_CLASS (mode);
1343 /* Doh! What's going on? */
1344 if (mclass != MODE_VECTOR_INT
1345 && mclass != MODE_VECTOR_FLOAT
1346 && mclass != MODE_VECTOR_FRACT
1347 && mclass != MODE_VECTOR_UFRACT
1348 && mclass != MODE_VECTOR_ACCUM
1349 && mclass != MODE_VECTOR_UACCUM)
1350 return false;
1352 /* Hardware support. Woo hoo! */
1353 if (targetm.vector_mode_supported_p (mode))
1354 return true;
1356 /* We should probably return 1 if requesting V4DI and we have no DI,
1357 but we have V2DI, but this is probably very unlikely. */
1359 /* If we have support for the inner mode, we can safely emulate it.
1360 We may not have V2DI, but me can emulate with a pair of DIs. */
1361 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1365 /* Handle a "mode" attribute; arguments as in
1366 struct attribute_spec.handler. */
1368 static tree
1369 handle_mode_attribute (tree *node, tree name, tree args,
1370 int ARG_UNUSED (flags), bool *no_add_attrs)
1372 tree type = *node;
1373 tree ident = TREE_VALUE (args);
1375 *no_add_attrs = true;
1377 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1378 warning (OPT_Wattributes, "%qE attribute ignored", name);
1379 else
1381 int j;
1382 const char *p = IDENTIFIER_POINTER (ident);
1383 int len = strlen (p);
1384 machine_mode mode = VOIDmode;
1385 tree typefm;
1386 bool valid_mode;
1388 if (len > 4 && p[0] == '_' && p[1] == '_'
1389 && p[len - 1] == '_' && p[len - 2] == '_')
1391 char *newp = (char *) alloca (len - 1);
1393 strcpy (newp, &p[2]);
1394 newp[len - 4] = '\0';
1395 p = newp;
1398 /* Change this type to have a type with the specified mode.
1399 First check for the special modes. */
1400 if (!strcmp (p, "byte"))
1401 mode = byte_mode;
1402 else if (!strcmp (p, "word"))
1403 mode = word_mode;
1404 else if (!strcmp (p, "pointer"))
1405 mode = ptr_mode;
1406 else if (!strcmp (p, "libgcc_cmp_return"))
1407 mode = targetm.libgcc_cmp_return_mode ();
1408 else if (!strcmp (p, "libgcc_shift_count"))
1409 mode = targetm.libgcc_shift_count_mode ();
1410 else if (!strcmp (p, "unwind_word"))
1411 mode = targetm.unwind_word_mode ();
1412 else
1413 for (j = 0; j < NUM_MACHINE_MODES; j++)
1414 if (!strcmp (p, GET_MODE_NAME (j)))
1416 mode = (machine_mode) j;
1417 break;
1420 if (mode == VOIDmode)
1422 error ("unknown machine mode %qE", ident);
1423 return NULL_TREE;
1426 valid_mode = false;
1427 switch (GET_MODE_CLASS (mode))
1429 case MODE_INT:
1430 case MODE_PARTIAL_INT:
1431 case MODE_FLOAT:
1432 case MODE_DECIMAL_FLOAT:
1433 case MODE_FRACT:
1434 case MODE_UFRACT:
1435 case MODE_ACCUM:
1436 case MODE_UACCUM:
1437 valid_mode
1438 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
1439 break;
1441 case MODE_COMPLEX_INT:
1442 case MODE_COMPLEX_FLOAT:
1443 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1444 break;
1446 case MODE_VECTOR_INT:
1447 case MODE_VECTOR_FLOAT:
1448 case MODE_VECTOR_FRACT:
1449 case MODE_VECTOR_UFRACT:
1450 case MODE_VECTOR_ACCUM:
1451 case MODE_VECTOR_UACCUM:
1452 warning (OPT_Wattributes, "specifying vector types with "
1453 "__attribute__ ((mode)) is deprecated");
1454 warning (OPT_Wattributes,
1455 "use __attribute__ ((vector_size)) instead");
1456 valid_mode = vector_mode_valid_p (mode);
1457 break;
1459 default:
1460 break;
1462 if (!valid_mode)
1464 error ("unable to emulate %qs", p);
1465 return NULL_TREE;
1468 if (POINTER_TYPE_P (type))
1470 scalar_int_mode addr_mode;
1471 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1472 tree (*fn)(tree, machine_mode, bool);
1474 if (!is_a <scalar_int_mode> (mode, &addr_mode)
1475 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
1477 error ("invalid pointer mode %qs", p);
1478 return NULL_TREE;
1481 if (TREE_CODE (type) == POINTER_TYPE)
1482 fn = build_pointer_type_for_mode;
1483 else
1484 fn = build_reference_type_for_mode;
1485 typefm = fn (TREE_TYPE (type), addr_mode, false);
1487 else
1489 /* For fixed-point modes, we need to test if the signness of type
1490 and the machine mode are consistent. */
1491 if (ALL_FIXED_POINT_MODE_P (mode)
1492 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1494 error ("signedness of type and machine mode %qs don%'t match", p);
1495 return NULL_TREE;
1497 /* For fixed-point modes, we need to pass saturating info. */
1498 typefm = lang_hooks.types.type_for_mode (mode,
1499 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1500 : TYPE_UNSIGNED (type));
1503 if (typefm == NULL_TREE)
1505 error ("no data type for mode %qs", p);
1506 return NULL_TREE;
1508 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1510 /* For enumeral types, copy the precision from the integer
1511 type returned above. If not an INTEGER_TYPE, we can't use
1512 this mode for this type. */
1513 if (TREE_CODE (typefm) != INTEGER_TYPE)
1515 error ("cannot use mode %qs for enumeral types", p);
1516 return NULL_TREE;
1519 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1521 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1522 typefm = type;
1524 else
1526 /* We cannot build a type variant, as there's code that assumes
1527 that TYPE_MAIN_VARIANT has the same mode. This includes the
1528 debug generators. Instead, create a subrange type. This
1529 results in all of the enumeral values being emitted only once
1530 in the original, and the subtype gets them by reference. */
1531 if (TYPE_UNSIGNED (type))
1532 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1533 else
1534 typefm = make_signed_type (TYPE_PRECISION (typefm));
1535 TREE_TYPE (typefm) = type;
1538 else if (VECTOR_MODE_P (mode)
1539 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1540 : TREE_CODE (type) != TREE_CODE (typefm))
1542 error ("mode %qs applied to inappropriate type", p);
1543 return NULL_TREE;
1546 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1549 return NULL_TREE;
1552 /* Handle a "section" attribute; arguments as in
1553 struct attribute_spec.handler. */
1555 static tree
1556 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1557 int ARG_UNUSED (flags), bool *no_add_attrs)
1559 tree decl = *node;
1561 if (!targetm_common.have_named_sections)
1563 error_at (DECL_SOURCE_LOCATION (*node),
1564 "section attributes are not supported for this target");
1565 goto fail;
1568 if (!VAR_OR_FUNCTION_DECL_P (decl))
1570 error ("section attribute not allowed for %q+D", *node);
1571 goto fail;
1574 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1576 error ("section attribute argument not a string constant");
1577 goto fail;
1580 if (VAR_P (decl)
1581 && current_function_decl != NULL_TREE
1582 && !TREE_STATIC (decl))
1584 error_at (DECL_SOURCE_LOCATION (decl),
1585 "section attribute cannot be specified for local variables");
1586 goto fail;
1589 /* The decl may have already been given a section attribute
1590 from a previous declaration. Ensure they match. */
1591 if (DECL_SECTION_NAME (decl) != NULL
1592 && strcmp (DECL_SECTION_NAME (decl),
1593 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1595 error ("section of %q+D conflicts with previous declaration", *node);
1596 goto fail;
1599 if (VAR_P (decl)
1600 && !targetm.have_tls && targetm.emutls.tmpl_section
1601 && DECL_THREAD_LOCAL_P (decl))
1603 error ("section of %q+D cannot be overridden", *node);
1604 goto fail;
1607 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1608 return NULL_TREE;
1610 fail:
1611 *no_add_attrs = true;
1612 return NULL_TREE;
1615 /* If in c++-11, check if the c++-11 alignment constraint with respect
1616 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1617 c++-11 mode, does nothing.
1619 [dcl.align]2/ says:
1621 [* if the constant expression evaluates to a fundamental alignment,
1622 the alignment requirement of the declared entity shall be the
1623 specified fundamental alignment.
1625 * if the constant expression evaluates to an extended alignment
1626 and the implementation supports that alignment in the context
1627 of the declaration, the alignment of the declared entity shall
1628 be that alignment
1630 * if the constant expression evaluates to an extended alignment
1631 and the implementation does not support that alignment in the
1632 context of the declaration, the program is ill-formed]. */
1634 static bool
1635 check_cxx_fundamental_alignment_constraints (tree node,
1636 unsigned align_log,
1637 int flags)
1639 bool alignment_too_large_p = false;
1640 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1641 unsigned max_align = 0;
1643 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1644 || (node == NULL_TREE || node == error_mark_node))
1645 return true;
1647 if (cxx_fundamental_alignment_p (requested_alignment))
1648 return true;
1650 if (VAR_P (node))
1652 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1653 /* For file scope variables and static members, the target supports
1654 alignments that are at most MAX_OFILE_ALIGNMENT. */
1655 max_align = MAX_OFILE_ALIGNMENT;
1656 else
1657 /* For stack variables, the target supports at most
1658 MAX_STACK_ALIGNMENT. */
1659 max_align = MAX_STACK_ALIGNMENT;
1660 if (requested_alignment > max_align)
1661 alignment_too_large_p = true;
1663 /* Let's be liberal for types and fields; don't limit their alignment any
1664 more than check_user_alignment already did. */
1666 if (alignment_too_large_p)
1667 pedwarn (input_location, OPT_Wattributes,
1668 "requested alignment %d is larger than %d",
1669 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1671 return !alignment_too_large_p;
1674 /* Common codes shared by handle_warn_if_not_aligned_attribute and
1675 handle_aligned_attribute. */
1677 static tree
1678 common_handle_aligned_attribute (tree *node, tree args, int flags,
1679 bool *no_add_attrs,
1680 bool warn_if_not_aligned_p)
1682 tree decl = NULL_TREE;
1683 tree *type = NULL;
1684 int is_type = 0;
1685 tree align_expr;
1686 int i;
1688 if (args)
1690 align_expr = TREE_VALUE (args);
1691 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1692 && TREE_CODE (align_expr) != FUNCTION_DECL)
1693 align_expr = default_conversion (align_expr);
1695 else
1696 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1698 if (DECL_P (*node))
1700 decl = *node;
1701 type = &TREE_TYPE (decl);
1702 is_type = TREE_CODE (*node) == TYPE_DECL;
1704 else if (TYPE_P (*node))
1705 type = node, is_type = 1;
1707 if ((i = check_user_alignment (align_expr, true)) == -1
1708 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
1709 *no_add_attrs = true;
1710 else if (is_type)
1712 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1713 /* OK, modify the type in place. */;
1714 /* If we have a TYPE_DECL, then copy the type, so that we
1715 don't accidentally modify a builtin type. See pushdecl. */
1716 else if (decl && TREE_TYPE (decl) != error_mark_node
1717 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1719 tree tt = TREE_TYPE (decl);
1720 *type = build_variant_type_copy (*type);
1721 DECL_ORIGINAL_TYPE (decl) = tt;
1722 TYPE_NAME (*type) = decl;
1723 TREE_USED (*type) = TREE_USED (decl);
1724 TREE_TYPE (decl) = *type;
1726 else
1727 *type = build_variant_type_copy (*type);
1729 if (warn_if_not_aligned_p)
1731 SET_TYPE_WARN_IF_NOT_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1732 warn_if_not_aligned_p = false;
1734 else
1736 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1737 TYPE_USER_ALIGN (*type) = 1;
1740 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1741 && TREE_CODE (decl) != FIELD_DECL)
1743 error ("alignment may not be specified for %q+D", decl);
1744 *no_add_attrs = true;
1746 else if (DECL_USER_ALIGN (decl)
1747 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1748 /* C++-11 [dcl.align/4]:
1750 When multiple alignment-specifiers are specified for an
1751 entity, the alignment requirement shall be set to the
1752 strictest specified alignment.
1754 This formally comes from the c++11 specification but we are
1755 doing it for the GNU attribute syntax as well. */
1756 *no_add_attrs = true;
1757 else if (TREE_CODE (decl) == FUNCTION_DECL
1758 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1760 if (DECL_USER_ALIGN (decl))
1761 error ("alignment for %q+D was previously specified as %d "
1762 "and may not be decreased", decl,
1763 DECL_ALIGN (decl) / BITS_PER_UNIT);
1764 else
1765 error ("alignment for %q+D must be at least %d", decl,
1766 DECL_ALIGN (decl) / BITS_PER_UNIT);
1767 *no_add_attrs = true;
1769 else
1771 if (warn_if_not_aligned_p)
1773 if (TREE_CODE (decl) == FIELD_DECL && !DECL_INITIAL (decl))
1775 SET_DECL_WARN_IF_NOT_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1776 warn_if_not_aligned_p = false;
1779 else
1781 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1782 DECL_USER_ALIGN (decl) = 1;
1786 if (warn_if_not_aligned_p)
1788 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
1789 decl);
1790 *no_add_attrs = true;
1793 return NULL_TREE;
1796 /* Handle a "aligned" attribute; arguments as in
1797 struct attribute_spec.handler. */
1799 static tree
1800 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1801 int flags, bool *no_add_attrs)
1803 return common_handle_aligned_attribute (node, args, flags,
1804 no_add_attrs, false);
1807 /* Handle a "warn_if_not_aligned" attribute; arguments as in
1808 struct attribute_spec.handler. */
1810 static tree
1811 handle_warn_if_not_aligned_attribute (tree *node, tree ARG_UNUSED (name),
1812 tree args, int flags,
1813 bool *no_add_attrs)
1815 return common_handle_aligned_attribute (node, args, flags,
1816 no_add_attrs, true);
1819 /* Handle a "weak" attribute; arguments as in
1820 struct attribute_spec.handler. */
1822 static tree
1823 handle_weak_attribute (tree *node, tree name,
1824 tree ARG_UNUSED (args),
1825 int ARG_UNUSED (flags),
1826 bool * ARG_UNUSED (no_add_attrs))
1828 if (TREE_CODE (*node) == FUNCTION_DECL
1829 && DECL_DECLARED_INLINE_P (*node))
1831 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1832 *no_add_attrs = true;
1834 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1836 error ("indirect function %q+D cannot be declared weak", *node);
1837 *no_add_attrs = true;
1838 return NULL_TREE;
1840 else if (VAR_OR_FUNCTION_DECL_P (*node))
1841 declare_weak (*node);
1842 else
1843 warning (OPT_Wattributes, "%qE attribute ignored", name);
1845 return NULL_TREE;
1848 /* Handle a "noplt" attribute; arguments as in
1849 struct attribute_spec.handler. */
1851 static tree
1852 handle_noplt_attribute (tree *node, tree name,
1853 tree ARG_UNUSED (args),
1854 int ARG_UNUSED (flags),
1855 bool * ARG_UNUSED (no_add_attrs))
1857 if (TREE_CODE (*node) != FUNCTION_DECL)
1859 warning (OPT_Wattributes,
1860 "%qE attribute is only applicable on functions", name);
1861 *no_add_attrs = true;
1862 return NULL_TREE;
1864 return NULL_TREE;
1867 /* Handle an "alias" or "ifunc" attribute; arguments as in
1868 struct attribute_spec.handler, except that IS_ALIAS tells us
1869 whether this is an alias as opposed to ifunc attribute. */
1871 static tree
1872 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
1873 bool *no_add_attrs)
1875 tree decl = *node;
1877 if (TREE_CODE (decl) != FUNCTION_DECL
1878 && (!is_alias || !VAR_P (decl)))
1880 warning (OPT_Wattributes, "%qE attribute ignored", name);
1881 *no_add_attrs = true;
1883 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1884 || (TREE_CODE (decl) != FUNCTION_DECL
1885 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
1886 /* A static variable declaration is always a tentative definition,
1887 but the alias is a non-tentative definition which overrides. */
1888 || (TREE_CODE (decl) != FUNCTION_DECL
1889 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
1891 error ("%q+D defined both normally and as %qE attribute", decl, name);
1892 *no_add_attrs = true;
1893 return NULL_TREE;
1895 else if (!is_alias
1896 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
1897 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
1899 error ("weak %q+D cannot be defined %qE", decl, name);
1900 *no_add_attrs = true;
1901 return NULL_TREE;
1904 /* Note that the very first time we process a nested declaration,
1905 decl_function_context will not be set. Indeed, *would* never
1906 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
1907 we do below. After such frobbery, pushdecl would set the context.
1908 In any case, this is never what we want. */
1909 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
1911 tree id;
1913 id = TREE_VALUE (args);
1914 if (TREE_CODE (id) != STRING_CST)
1916 error ("attribute %qE argument not a string", name);
1917 *no_add_attrs = true;
1918 return NULL_TREE;
1920 id = get_identifier (TREE_STRING_POINTER (id));
1921 /* This counts as a use of the object pointed to. */
1922 TREE_USED (id) = 1;
1924 if (TREE_CODE (decl) == FUNCTION_DECL)
1925 DECL_INITIAL (decl) = error_mark_node;
1926 else
1927 TREE_STATIC (decl) = 1;
1929 if (!is_alias)
1931 /* ifuncs are also aliases, so set that attribute too. */
1932 DECL_ATTRIBUTES (decl)
1933 = tree_cons (get_identifier ("alias"), args,
1934 DECL_ATTRIBUTES (decl));
1935 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
1936 NULL, DECL_ATTRIBUTES (decl));
1939 else
1941 warning (OPT_Wattributes, "%qE attribute ignored", name);
1942 *no_add_attrs = true;
1945 if (decl_in_symtab_p (*node))
1947 struct symtab_node *n = symtab_node::get (decl);
1948 if (n && n->refuse_visibility_changes)
1950 if (is_alias)
1951 error ("%+qD declared alias after being used", decl);
1952 else
1953 error ("%+qD declared ifunc after being used", decl);
1958 return NULL_TREE;
1961 /* Handle an "alias" or "ifunc" attribute; arguments as in
1962 struct attribute_spec.handler. */
1964 static tree
1965 handle_ifunc_attribute (tree *node, tree name, tree args,
1966 int ARG_UNUSED (flags), bool *no_add_attrs)
1968 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
1971 /* Handle an "alias" or "ifunc" attribute; arguments as in
1972 struct attribute_spec.handler. */
1974 static tree
1975 handle_alias_attribute (tree *node, tree name, tree args,
1976 int ARG_UNUSED (flags), bool *no_add_attrs)
1978 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
1981 /* Handle a "weakref" attribute; arguments as in struct
1982 attribute_spec.handler. */
1984 static tree
1985 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1986 int flags, bool *no_add_attrs)
1988 tree attr = NULL_TREE;
1990 /* We must ignore the attribute when it is associated with
1991 local-scoped decls, since attribute alias is ignored and many
1992 such symbols do not even have a DECL_WEAK field. */
1993 if (decl_function_context (*node)
1994 || current_function_decl
1995 || !VAR_OR_FUNCTION_DECL_P (*node))
1997 warning (OPT_Wattributes, "%qE attribute ignored", name);
1998 *no_add_attrs = true;
1999 return NULL_TREE;
2002 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2004 error ("indirect function %q+D cannot be declared weakref", *node);
2005 *no_add_attrs = true;
2006 return NULL_TREE;
2009 /* The idea here is that `weakref("name")' mutates into `weakref,
2010 alias("name")', and weakref without arguments, in turn,
2011 implicitly adds weak. */
2013 if (args)
2015 attr = tree_cons (get_identifier ("alias"), args, attr);
2016 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2018 *no_add_attrs = true;
2020 decl_attributes (node, attr, flags);
2022 else
2024 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2025 error_at (DECL_SOURCE_LOCATION (*node),
2026 "weakref attribute must appear before alias attribute");
2028 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2029 and that isn't supported; and because it wants to add it to
2030 the list of weak decls, which isn't helpful. */
2031 DECL_WEAK (*node) = 1;
2034 if (decl_in_symtab_p (*node))
2036 struct symtab_node *n = symtab_node::get (*node);
2037 if (n && n->refuse_visibility_changes)
2038 error ("%+qD declared weakref after being used", *node);
2041 return NULL_TREE;
2044 /* Handle an "visibility" attribute; arguments as in
2045 struct attribute_spec.handler. */
2047 static tree
2048 handle_visibility_attribute (tree *node, tree name, tree args,
2049 int ARG_UNUSED (flags),
2050 bool *ARG_UNUSED (no_add_attrs))
2052 tree decl = *node;
2053 tree id = TREE_VALUE (args);
2054 enum symbol_visibility vis;
2056 if (TYPE_P (*node))
2058 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2059 /* OK */;
2060 else if (!RECORD_OR_UNION_TYPE_P (*node))
2062 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2063 name);
2064 return NULL_TREE;
2066 else if (TYPE_FIELDS (*node))
2068 error ("%qE attribute ignored because %qT is already defined",
2069 name, *node);
2070 return NULL_TREE;
2073 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2075 warning (OPT_Wattributes, "%qE attribute ignored", name);
2076 return NULL_TREE;
2079 if (TREE_CODE (id) != STRING_CST)
2081 error ("visibility argument not a string");
2082 return NULL_TREE;
2085 /* If this is a type, set the visibility on the type decl. */
2086 if (TYPE_P (decl))
2088 decl = TYPE_NAME (decl);
2089 if (!decl)
2090 return NULL_TREE;
2091 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2093 warning (OPT_Wattributes, "%qE attribute ignored on types",
2094 name);
2095 return NULL_TREE;
2099 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2100 vis = VISIBILITY_DEFAULT;
2101 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2102 vis = VISIBILITY_INTERNAL;
2103 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2104 vis = VISIBILITY_HIDDEN;
2105 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2106 vis = VISIBILITY_PROTECTED;
2107 else
2109 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2110 vis = VISIBILITY_DEFAULT;
2113 if (DECL_VISIBILITY_SPECIFIED (decl)
2114 && vis != DECL_VISIBILITY (decl))
2116 tree attributes = (TYPE_P (*node)
2117 ? TYPE_ATTRIBUTES (*node)
2118 : DECL_ATTRIBUTES (decl));
2119 if (lookup_attribute ("visibility", attributes))
2120 error ("%qD redeclared with different visibility", decl);
2121 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2122 && lookup_attribute ("dllimport", attributes))
2123 error ("%qD was declared %qs which implies default visibility",
2124 decl, "dllimport");
2125 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2126 && lookup_attribute ("dllexport", attributes))
2127 error ("%qD was declared %qs which implies default visibility",
2128 decl, "dllexport");
2131 DECL_VISIBILITY (decl) = vis;
2132 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2134 /* Go ahead and attach the attribute to the node as well. This is needed
2135 so we can determine whether we have VISIBILITY_DEFAULT because the
2136 visibility was not specified, or because it was explicitly overridden
2137 from the containing scope. */
2139 return NULL_TREE;
2142 /* Handle an "tls_model" attribute; arguments as in
2143 struct attribute_spec.handler. */
2145 static tree
2146 handle_tls_model_attribute (tree *node, tree name, tree args,
2147 int ARG_UNUSED (flags), bool *no_add_attrs)
2149 tree id;
2150 tree decl = *node;
2151 enum tls_model kind;
2153 *no_add_attrs = true;
2155 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
2157 warning (OPT_Wattributes, "%qE attribute ignored", name);
2158 return NULL_TREE;
2161 kind = DECL_TLS_MODEL (decl);
2162 id = TREE_VALUE (args);
2163 if (TREE_CODE (id) != STRING_CST)
2165 error ("tls_model argument not a string");
2166 return NULL_TREE;
2169 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2170 kind = TLS_MODEL_LOCAL_EXEC;
2171 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2172 kind = TLS_MODEL_INITIAL_EXEC;
2173 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2174 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2175 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2176 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2177 else
2178 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2180 set_decl_tls_model (decl, kind);
2181 return NULL_TREE;
2184 /* Handle a "no_instrument_function" attribute; arguments as in
2185 struct attribute_spec.handler. */
2187 static tree
2188 handle_no_instrument_function_attribute (tree *node, tree name,
2189 tree ARG_UNUSED (args),
2190 int ARG_UNUSED (flags),
2191 bool *no_add_attrs)
2193 tree decl = *node;
2195 if (TREE_CODE (decl) != FUNCTION_DECL)
2197 error_at (DECL_SOURCE_LOCATION (decl),
2198 "%qE attribute applies only to functions", name);
2199 *no_add_attrs = true;
2201 else
2202 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2204 return NULL_TREE;
2207 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2208 struct attribute_spec.handler. */
2210 static tree
2211 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2212 int, bool *no_add_attrs)
2214 if (TREE_CODE (*node) != FUNCTION_DECL)
2216 warning (OPT_Wattributes, "%qE attribute ignored", name);
2217 *no_add_attrs = true;
2220 return NULL_TREE;
2223 /* Handle a "malloc" attribute; arguments as in
2224 struct attribute_spec.handler. */
2226 static tree
2227 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2228 int ARG_UNUSED (flags), bool *no_add_attrs)
2230 if (TREE_CODE (*node) == FUNCTION_DECL
2231 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2232 DECL_IS_MALLOC (*node) = 1;
2233 else
2235 warning (OPT_Wattributes, "%qE attribute ignored", name);
2236 *no_add_attrs = true;
2239 return NULL_TREE;
2242 /* Handle a "alloc_size" attribute; arguments as in
2243 struct attribute_spec.handler. */
2245 static tree
2246 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2247 int ARG_UNUSED (flags), bool *no_add_attrs)
2249 unsigned arg_count = type_num_arguments (*node);
2250 for (; args; args = TREE_CHAIN (args))
2252 tree position = TREE_VALUE (args);
2253 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2254 && TREE_CODE (position) != FUNCTION_DECL)
2255 position = default_conversion (position);
2257 if (!tree_fits_uhwi_p (position)
2258 || !arg_count
2259 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2261 warning (OPT_Wattributes,
2262 "alloc_size parameter outside range");
2263 *no_add_attrs = true;
2264 return NULL_TREE;
2267 return NULL_TREE;
2270 /* Handle a "alloc_align" attribute; arguments as in
2271 struct attribute_spec.handler. */
2273 static tree
2274 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2275 bool *no_add_attrs)
2277 unsigned arg_count = type_num_arguments (*node);
2278 tree position = TREE_VALUE (args);
2279 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2280 && TREE_CODE (position) != FUNCTION_DECL)
2281 position = default_conversion (position);
2283 if (!tree_fits_uhwi_p (position)
2284 || !arg_count
2285 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2287 warning (OPT_Wattributes,
2288 "alloc_align parameter outside range");
2289 *no_add_attrs = true;
2290 return NULL_TREE;
2292 return NULL_TREE;
2295 /* Handle a "assume_aligned" attribute; arguments as in
2296 struct attribute_spec.handler. */
2298 static tree
2299 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2300 bool *no_add_attrs)
2302 for (; args; args = TREE_CHAIN (args))
2304 tree position = TREE_VALUE (args);
2305 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2306 && TREE_CODE (position) != FUNCTION_DECL)
2307 position = default_conversion (position);
2309 if (TREE_CODE (position) != INTEGER_CST)
2311 warning (OPT_Wattributes,
2312 "assume_aligned parameter not integer constant");
2313 *no_add_attrs = true;
2314 return NULL_TREE;
2317 return NULL_TREE;
2320 /* Handle a "fn spec" attribute; arguments as in
2321 struct attribute_spec.handler. */
2323 static tree
2324 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2325 tree args, int ARG_UNUSED (flags),
2326 bool *no_add_attrs ATTRIBUTE_UNUSED)
2328 gcc_assert (args
2329 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2330 && !TREE_CHAIN (args));
2331 return NULL_TREE;
2334 /* Handle a "bnd_variable_size" attribute; arguments as in
2335 struct attribute_spec.handler. */
2337 static tree
2338 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2339 int ARG_UNUSED (flags), bool *no_add_attrs)
2341 if (TREE_CODE (*node) != FIELD_DECL)
2343 warning (OPT_Wattributes, "%qE attribute ignored", name);
2344 *no_add_attrs = true;
2347 return NULL_TREE;
2350 /* Handle a "bnd_legacy" attribute; arguments as in
2351 struct attribute_spec.handler. */
2353 static tree
2354 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
2355 int ARG_UNUSED (flags), bool *no_add_attrs)
2357 if (TREE_CODE (*node) != FUNCTION_DECL)
2359 warning (OPT_Wattributes, "%qE attribute ignored", name);
2360 *no_add_attrs = true;
2363 return NULL_TREE;
2366 /* Handle a "bnd_instrument" attribute; arguments as in
2367 struct attribute_spec.handler. */
2369 static tree
2370 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
2371 int ARG_UNUSED (flags), bool *no_add_attrs)
2373 if (TREE_CODE (*node) != FUNCTION_DECL)
2375 warning (OPT_Wattributes, "%qE attribute ignored", name);
2376 *no_add_attrs = true;
2379 return NULL_TREE;
2382 /* Handle a "warn_unused" attribute; arguments as in
2383 struct attribute_spec.handler. */
2385 static tree
2386 handle_warn_unused_attribute (tree *node, tree name,
2387 tree args ATTRIBUTE_UNUSED,
2388 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2390 if (TYPE_P (*node))
2391 /* Do nothing else, just set the attribute. We'll get at
2392 it later with lookup_attribute. */
2394 else
2396 warning (OPT_Wattributes, "%qE attribute ignored", name);
2397 *no_add_attrs = true;
2400 return NULL_TREE;
2403 /* Handle an "omp declare simd" attribute; arguments as in
2404 struct attribute_spec.handler. */
2406 static tree
2407 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2409 return NULL_TREE;
2412 /* Handle a "simd" attribute. */
2414 static tree
2415 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2417 if (TREE_CODE (*node) == FUNCTION_DECL)
2419 if (lookup_attribute ("cilk simd function",
2420 DECL_ATTRIBUTES (*node)) != NULL)
2422 error_at (DECL_SOURCE_LOCATION (*node),
2423 "%<__simd__%> attribute cannot be used in the same "
2424 "function marked as a Cilk Plus SIMD-enabled function");
2425 *no_add_attrs = true;
2427 else
2429 tree t = get_identifier ("omp declare simd");
2430 tree attr = NULL_TREE;
2431 if (args)
2433 tree id = TREE_VALUE (args);
2435 if (TREE_CODE (id) != STRING_CST)
2437 error ("attribute %qE argument not a string", name);
2438 *no_add_attrs = true;
2439 return NULL_TREE;
2442 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2443 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2444 OMP_CLAUSE_NOTINBRANCH);
2445 else
2446 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2447 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2448 OMP_CLAUSE_INBRANCH);
2449 else
2451 error ("only %<inbranch%> and %<notinbranch%> flags are "
2452 "allowed for %<__simd__%> attribute");
2453 *no_add_attrs = true;
2454 return NULL_TREE;
2458 DECL_ATTRIBUTES (*node) = tree_cons (t,
2459 build_tree_list (NULL_TREE,
2460 attr),
2461 DECL_ATTRIBUTES (*node));
2464 else
2466 warning (OPT_Wattributes, "%qE attribute ignored", name);
2467 *no_add_attrs = true;
2470 return NULL_TREE;
2473 /* Handle an "omp declare target" attribute; arguments as in
2474 struct attribute_spec.handler. */
2476 static tree
2477 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2479 return NULL_TREE;
2482 /* Handle a "returns_twice" attribute; arguments as in
2483 struct attribute_spec.handler. */
2485 static tree
2486 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2487 int ARG_UNUSED (flags), bool *no_add_attrs)
2489 if (TREE_CODE (*node) == FUNCTION_DECL)
2490 DECL_IS_RETURNS_TWICE (*node) = 1;
2491 else
2493 warning (OPT_Wattributes, "%qE attribute ignored", name);
2494 *no_add_attrs = true;
2497 return NULL_TREE;
2500 /* Handle a "no_limit_stack" attribute; arguments as in
2501 struct attribute_spec.handler. */
2503 static tree
2504 handle_no_limit_stack_attribute (tree *node, tree name,
2505 tree ARG_UNUSED (args),
2506 int ARG_UNUSED (flags),
2507 bool *no_add_attrs)
2509 tree decl = *node;
2511 if (TREE_CODE (decl) != FUNCTION_DECL)
2513 error_at (DECL_SOURCE_LOCATION (decl),
2514 "%qE attribute applies only to functions", name);
2515 *no_add_attrs = true;
2517 else if (DECL_INITIAL (decl))
2519 error_at (DECL_SOURCE_LOCATION (decl),
2520 "can%'t set %qE attribute after definition", name);
2521 *no_add_attrs = true;
2523 else
2524 DECL_NO_LIMIT_STACK (decl) = 1;
2526 return NULL_TREE;
2529 /* Handle a "pure" attribute; arguments as in
2530 struct attribute_spec.handler. */
2532 static tree
2533 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2534 int ARG_UNUSED (flags), bool *no_add_attrs)
2536 if (TREE_CODE (*node) == FUNCTION_DECL)
2537 DECL_PURE_P (*node) = 1;
2538 /* ??? TODO: Support types. */
2539 else
2541 warning (OPT_Wattributes, "%qE attribute ignored", name);
2542 *no_add_attrs = true;
2545 return NULL_TREE;
2548 /* Digest an attribute list destined for a transactional memory statement.
2549 ALLOWED is the set of attributes that are allowed for this statement;
2550 return the attribute we parsed. Multiple attributes are never allowed. */
2553 parse_tm_stmt_attr (tree attrs, int allowed)
2555 tree a_seen = NULL;
2556 int m_seen = 0;
2558 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2560 tree a = TREE_PURPOSE (attrs);
2561 int m = 0;
2563 if (is_attribute_p ("outer", a))
2564 m = TM_STMT_ATTR_OUTER;
2566 if ((m & allowed) == 0)
2568 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2569 continue;
2572 if (m_seen == 0)
2574 a_seen = a;
2575 m_seen = m;
2577 else if (m_seen == m)
2578 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2579 else
2580 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2583 return m_seen;
2586 /* Transform a TM attribute name into a maskable integer and back.
2587 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2588 to how the lack of an attribute is treated. */
2591 tm_attr_to_mask (tree attr)
2593 if (attr == NULL)
2594 return 0;
2595 if (is_attribute_p ("transaction_safe", attr))
2596 return TM_ATTR_SAFE;
2597 if (is_attribute_p ("transaction_callable", attr))
2598 return TM_ATTR_CALLABLE;
2599 if (is_attribute_p ("transaction_pure", attr))
2600 return TM_ATTR_PURE;
2601 if (is_attribute_p ("transaction_unsafe", attr))
2602 return TM_ATTR_IRREVOCABLE;
2603 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2604 return TM_ATTR_MAY_CANCEL_OUTER;
2605 return 0;
2608 tree
2609 tm_mask_to_attr (int mask)
2611 const char *str;
2612 switch (mask)
2614 case TM_ATTR_SAFE:
2615 str = "transaction_safe";
2616 break;
2617 case TM_ATTR_CALLABLE:
2618 str = "transaction_callable";
2619 break;
2620 case TM_ATTR_PURE:
2621 str = "transaction_pure";
2622 break;
2623 case TM_ATTR_IRREVOCABLE:
2624 str = "transaction_unsafe";
2625 break;
2626 case TM_ATTR_MAY_CANCEL_OUTER:
2627 str = "transaction_may_cancel_outer";
2628 break;
2629 default:
2630 gcc_unreachable ();
2632 return get_identifier (str);
2635 /* Return the first TM attribute seen in LIST. */
2637 tree
2638 find_tm_attribute (tree list)
2640 for (; list ; list = TREE_CHAIN (list))
2642 tree name = TREE_PURPOSE (list);
2643 if (tm_attr_to_mask (name) != 0)
2644 return name;
2646 return NULL_TREE;
2649 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2650 Here we accept only function types, and verify that none of the other
2651 function TM attributes are also applied. */
2652 /* ??? We need to accept class types for C++, but not C. This greatly
2653 complicates this function, since we can no longer rely on the extra
2654 processing given by function_type_required. */
2656 static tree
2657 handle_tm_attribute (tree *node, tree name, tree args,
2658 int flags, bool *no_add_attrs)
2660 /* Only one path adds the attribute; others don't. */
2661 *no_add_attrs = true;
2663 switch (TREE_CODE (*node))
2665 case RECORD_TYPE:
2666 case UNION_TYPE:
2667 /* Only tm_callable and tm_safe apply to classes. */
2668 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2669 goto ignored;
2670 /* FALLTHRU */
2672 case FUNCTION_TYPE:
2673 case METHOD_TYPE:
2675 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2676 if (old_name == name)
2678 else if (old_name != NULL_TREE)
2679 error ("type was previously declared %qE", old_name);
2680 else
2681 *no_add_attrs = false;
2683 break;
2685 case FUNCTION_DECL:
2687 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2688 want to set transaction_safe on the type. */
2689 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2690 if (!TYPE_P (DECL_CONTEXT (*node)))
2691 error_at (DECL_SOURCE_LOCATION (*node),
2692 "%<transaction_safe_dynamic%> may only be specified for "
2693 "a virtual function");
2694 *no_add_attrs = false;
2695 decl_attributes (&TREE_TYPE (*node),
2696 build_tree_list (get_identifier ("transaction_safe"),
2697 NULL_TREE),
2699 break;
2702 case POINTER_TYPE:
2704 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2705 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2707 tree fn_tmp = TREE_TYPE (*node);
2708 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2709 *node = build_pointer_type (fn_tmp);
2710 break;
2713 /* FALLTHRU */
2715 default:
2716 /* If a function is next, pass it on to be tried next. */
2717 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2718 return tree_cons (name, args, NULL);
2720 ignored:
2721 warning (OPT_Wattributes, "%qE attribute ignored", name);
2722 break;
2725 return NULL_TREE;
2728 /* Handle the TM_WRAP attribute; arguments as in
2729 struct attribute_spec.handler. */
2731 static tree
2732 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2733 int ARG_UNUSED (flags), bool *no_add_attrs)
2735 tree decl = *node;
2737 /* We don't need the attribute even on success, since we
2738 record the entry in an external table. */
2739 *no_add_attrs = true;
2741 if (TREE_CODE (decl) != FUNCTION_DECL)
2742 warning (OPT_Wattributes, "%qE attribute ignored", name);
2743 else
2745 tree wrap_decl = TREE_VALUE (args);
2746 if (error_operand_p (wrap_decl))
2748 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2749 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2750 error ("%qE argument not an identifier", name);
2751 else
2753 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2754 wrap_decl = lookup_name (wrap_decl);
2755 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2757 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2758 TREE_TYPE (wrap_decl)))
2759 record_tm_replacement (wrap_decl, decl);
2760 else
2761 error ("%qD is not compatible with %qD", wrap_decl, decl);
2763 else
2764 error ("%qE argument is not a function", name);
2768 return NULL_TREE;
2771 /* Ignore the given attribute. Used when this attribute may be usefully
2772 overridden by the target, but is not used generically. */
2774 static tree
2775 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2776 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2777 bool *no_add_attrs)
2779 *no_add_attrs = true;
2780 return NULL_TREE;
2783 /* Handle a "no vops" attribute; arguments as in
2784 struct attribute_spec.handler. */
2786 static tree
2787 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2788 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2789 bool *ARG_UNUSED (no_add_attrs))
2791 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2792 DECL_IS_NOVOPS (*node) = 1;
2793 return NULL_TREE;
2796 /* Handle a "deprecated" attribute; arguments as in
2797 struct attribute_spec.handler. */
2799 static tree
2800 handle_deprecated_attribute (tree *node, tree name,
2801 tree args, int flags,
2802 bool *no_add_attrs)
2804 tree type = NULL_TREE;
2805 int warn = 0;
2806 tree what = NULL_TREE;
2808 if (!args)
2809 *no_add_attrs = true;
2810 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2812 error ("deprecated message is not a string");
2813 *no_add_attrs = true;
2816 if (DECL_P (*node))
2818 tree decl = *node;
2819 type = TREE_TYPE (decl);
2821 if (TREE_CODE (decl) == TYPE_DECL
2822 || TREE_CODE (decl) == PARM_DECL
2823 || VAR_OR_FUNCTION_DECL_P (decl)
2824 || TREE_CODE (decl) == FIELD_DECL
2825 || TREE_CODE (decl) == CONST_DECL
2826 || objc_method_decl (TREE_CODE (decl)))
2827 TREE_DEPRECATED (decl) = 1;
2828 else
2829 warn = 1;
2831 else if (TYPE_P (*node))
2833 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2834 *node = build_variant_type_copy (*node);
2835 TREE_DEPRECATED (*node) = 1;
2836 type = *node;
2838 else
2839 warn = 1;
2841 if (warn)
2843 *no_add_attrs = true;
2844 if (type && TYPE_NAME (type))
2846 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2847 what = TYPE_NAME (*node);
2848 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2849 && DECL_NAME (TYPE_NAME (type)))
2850 what = DECL_NAME (TYPE_NAME (type));
2852 if (what)
2853 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2854 else
2855 warning (OPT_Wattributes, "%qE attribute ignored", name);
2858 return NULL_TREE;
2861 /* Handle a "vector_size" attribute; arguments as in
2862 struct attribute_spec.handler. */
2864 static tree
2865 handle_vector_size_attribute (tree *node, tree name, tree args,
2866 int ARG_UNUSED (flags),
2867 bool *no_add_attrs)
2869 unsigned HOST_WIDE_INT vecsize, nunits;
2870 machine_mode orig_mode;
2871 tree type = *node, new_type, size;
2873 *no_add_attrs = true;
2875 size = TREE_VALUE (args);
2876 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2877 && TREE_CODE (size) != FUNCTION_DECL)
2878 size = default_conversion (size);
2880 if (!tree_fits_uhwi_p (size))
2882 warning (OPT_Wattributes, "%qE attribute ignored", name);
2883 return NULL_TREE;
2886 /* Get the vector size (in bytes). */
2887 vecsize = tree_to_uhwi (size);
2889 /* We need to provide for vector pointers, vector arrays, and
2890 functions returning vectors. For example:
2892 __attribute__((vector_size(16))) short *foo;
2894 In this case, the mode is SI, but the type being modified is
2895 HI, so we need to look further. */
2897 while (POINTER_TYPE_P (type)
2898 || TREE_CODE (type) == FUNCTION_TYPE
2899 || TREE_CODE (type) == METHOD_TYPE
2900 || TREE_CODE (type) == ARRAY_TYPE
2901 || TREE_CODE (type) == OFFSET_TYPE)
2902 type = TREE_TYPE (type);
2904 /* Get the mode of the type being modified. */
2905 orig_mode = TYPE_MODE (type);
2907 if ((!INTEGRAL_TYPE_P (type)
2908 && !SCALAR_FLOAT_TYPE_P (type)
2909 && !FIXED_POINT_TYPE_P (type))
2910 || (!SCALAR_FLOAT_MODE_P (orig_mode)
2911 && GET_MODE_CLASS (orig_mode) != MODE_INT
2912 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
2913 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
2914 || TREE_CODE (type) == BOOLEAN_TYPE)
2916 error ("invalid vector type for attribute %qE", name);
2917 return NULL_TREE;
2920 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2922 error ("vector size not an integral multiple of component size");
2923 return NULL;
2926 if (vecsize == 0)
2928 error ("zero vector size");
2929 return NULL;
2932 /* Calculate how many units fit in the vector. */
2933 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
2934 if (nunits & (nunits - 1))
2936 error ("number of components of the vector not a power of two");
2937 return NULL_TREE;
2940 new_type = build_vector_type (type, nunits);
2942 /* Build back pointers if needed. */
2943 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
2945 return NULL_TREE;
2948 /* Handle the "nonnull" attribute. */
2950 static tree
2951 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
2952 tree args, int ARG_UNUSED (flags),
2953 bool *no_add_attrs)
2955 tree type = *node;
2956 unsigned HOST_WIDE_INT attr_arg_num;
2958 /* If no arguments are specified, all pointer arguments should be
2959 non-null. Verify a full prototype is given so that the arguments
2960 will have the correct types when we actually check them later.
2961 Avoid diagnosing type-generic built-ins since those have no
2962 prototype. */
2963 if (!args)
2965 if (!prototype_p (type)
2966 && (!TYPE_ATTRIBUTES (type)
2967 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
2969 error ("nonnull attribute without arguments on a non-prototype");
2970 *no_add_attrs = true;
2972 return NULL_TREE;
2975 /* Argument list specified. Verify that each argument number references
2976 a pointer argument. */
2977 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
2979 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
2981 tree arg = TREE_VALUE (args);
2982 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
2983 && TREE_CODE (arg) != FUNCTION_DECL)
2984 TREE_VALUE (args) = arg = default_conversion (arg);
2986 if (!get_nonnull_operand (arg, &arg_num))
2988 error ("nonnull argument has invalid operand number (argument %lu)",
2989 (unsigned long) attr_arg_num);
2990 *no_add_attrs = true;
2991 return NULL_TREE;
2994 if (prototype_p (type))
2996 function_args_iterator iter;
2997 tree argument;
2999 function_args_iter_init (&iter, type);
3000 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
3002 argument = function_args_iter_cond (&iter);
3003 if (argument == NULL_TREE || ck_num == arg_num)
3004 break;
3007 if (!argument
3008 || TREE_CODE (argument) == VOID_TYPE)
3010 error ("nonnull argument with out-of-range operand number "
3011 "(argument %lu, operand %lu)",
3012 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3013 *no_add_attrs = true;
3014 return NULL_TREE;
3017 if (TREE_CODE (argument) != POINTER_TYPE)
3019 error ("nonnull argument references non-pointer operand "
3020 "(argument %lu, operand %lu)",
3021 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3022 *no_add_attrs = true;
3023 return NULL_TREE;
3028 return NULL_TREE;
3031 /* Handle the "nonstring" variable attribute. */
3033 static tree
3034 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3035 int ARG_UNUSED (flags), bool *no_add_attrs)
3037 gcc_assert (!args);
3038 tree_code code = TREE_CODE (*node);
3040 if (VAR_P (*node)
3041 || code == FIELD_DECL
3042 || code == PARM_DECL)
3044 tree type = TREE_TYPE (*node);
3046 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3048 tree eltype = TREE_TYPE (type);
3049 if (eltype == char_type_node)
3050 return NULL_TREE;
3053 warning (OPT_Wattributes,
3054 "%qE attribute ignored on objects of type %qT",
3055 name, type);
3056 *no_add_attrs = true;
3057 return NULL_TREE;
3060 if (code == FUNCTION_DECL)
3061 warning (OPT_Wattributes,
3062 "%qE attribute does not apply to functions", name);
3063 else if (code == TYPE_DECL)
3064 warning (OPT_Wattributes,
3065 "%qE attribute does not apply to types", name);
3066 else
3067 warning (OPT_Wattributes, "%qE attribute ignored", name);
3069 *no_add_attrs = true;
3070 return NULL_TREE;
3073 /* Handle a "nothrow" attribute; arguments as in
3074 struct attribute_spec.handler. */
3076 static tree
3077 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3078 int ARG_UNUSED (flags), bool *no_add_attrs)
3080 if (TREE_CODE (*node) == FUNCTION_DECL)
3081 TREE_NOTHROW (*node) = 1;
3082 /* ??? TODO: Support types. */
3083 else
3085 warning (OPT_Wattributes, "%qE attribute ignored", name);
3086 *no_add_attrs = true;
3089 return NULL_TREE;
3092 /* Handle a "cleanup" attribute; arguments as in
3093 struct attribute_spec.handler. */
3095 static tree
3096 handle_cleanup_attribute (tree *node, tree name, tree args,
3097 int ARG_UNUSED (flags), bool *no_add_attrs)
3099 tree decl = *node;
3100 tree cleanup_id, cleanup_decl;
3102 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3103 for global destructors in C++. This requires infrastructure that
3104 we don't have generically at the moment. It's also not a feature
3105 we'd be missing too much, since we do have attribute constructor. */
3106 if (!VAR_P (decl) || TREE_STATIC (decl))
3108 warning (OPT_Wattributes, "%qE attribute ignored", name);
3109 *no_add_attrs = true;
3110 return NULL_TREE;
3113 /* Verify that the argument is a function in scope. */
3114 /* ??? We could support pointers to functions here as well, if
3115 that was considered desirable. */
3116 cleanup_id = TREE_VALUE (args);
3117 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3119 error ("cleanup argument not an identifier");
3120 *no_add_attrs = true;
3121 return NULL_TREE;
3123 cleanup_decl = lookup_name (cleanup_id);
3124 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3126 error ("cleanup argument not a function");
3127 *no_add_attrs = true;
3128 return NULL_TREE;
3131 /* That the function has proper type is checked with the
3132 eventual call to build_function_call. */
3134 return NULL_TREE;
3137 /* Handle a "warn_unused_result" attribute. No special handling. */
3139 static tree
3140 handle_warn_unused_result_attribute (tree *node, tree name,
3141 tree ARG_UNUSED (args),
3142 int ARG_UNUSED (flags), bool *no_add_attrs)
3144 /* Ignore the attribute for functions not returning any value. */
3145 if (VOID_TYPE_P (TREE_TYPE (*node)))
3147 warning (OPT_Wattributes, "%qE attribute ignored", name);
3148 *no_add_attrs = true;
3151 return NULL_TREE;
3154 /* Handle a "sentinel" attribute. */
3156 static tree
3157 handle_sentinel_attribute (tree *node, tree name, tree args,
3158 int ARG_UNUSED (flags), bool *no_add_attrs)
3160 if (!prototype_p (*node))
3162 warning (OPT_Wattributes,
3163 "%qE attribute requires prototypes with named arguments", name);
3164 *no_add_attrs = true;
3166 else
3168 if (!stdarg_p (*node))
3170 warning (OPT_Wattributes,
3171 "%qE attribute only applies to variadic functions", name);
3172 *no_add_attrs = true;
3176 if (args)
3178 tree position = TREE_VALUE (args);
3179 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3180 && TREE_CODE (position) != FUNCTION_DECL)
3181 position = default_conversion (position);
3183 if (TREE_CODE (position) != INTEGER_CST
3184 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3186 warning (OPT_Wattributes,
3187 "requested position is not an integer constant");
3188 *no_add_attrs = true;
3190 else
3192 if (tree_int_cst_lt (position, integer_zero_node))
3194 warning (OPT_Wattributes,
3195 "requested position is less than zero");
3196 *no_add_attrs = true;
3201 return NULL_TREE;
3204 /* Handle a "type_generic" attribute. */
3206 static tree
3207 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3208 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3209 bool * ARG_UNUSED (no_add_attrs))
3211 /* Ensure we have a function type. */
3212 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3214 /* Ensure we have a variadic function. */
3215 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3217 return NULL_TREE;
3220 /* Handle a "target" attribute. */
3222 static tree
3223 handle_target_attribute (tree *node, tree name, tree args, int flags,
3224 bool *no_add_attrs)
3226 /* Ensure we have a function type. */
3227 if (TREE_CODE (*node) != FUNCTION_DECL)
3229 warning (OPT_Wattributes, "%qE attribute ignored", name);
3230 *no_add_attrs = true;
3232 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3234 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3235 "with %qs attribute", name, "target_clones");
3236 *no_add_attrs = true;
3238 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3239 flags))
3240 *no_add_attrs = true;
3242 /* Check that there's no empty string in values of the attribute. */
3243 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3245 tree value = TREE_VALUE (t);
3246 if (TREE_CODE (value) == STRING_CST
3247 && TREE_STRING_LENGTH (value) == 1
3248 && TREE_STRING_POINTER (value)[0] == '\0')
3250 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3251 *no_add_attrs = true;
3255 return NULL_TREE;
3258 /* Handle a "target_clones" attribute. */
3260 static tree
3261 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3262 int ARG_UNUSED (flags), bool *no_add_attrs)
3264 /* Ensure we have a function type. */
3265 if (TREE_CODE (*node) == FUNCTION_DECL)
3267 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3269 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3270 "with %qs attribute", name, "always_inline");
3271 *no_add_attrs = true;
3273 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3275 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3276 "with %qs attribute", name, "target");
3277 *no_add_attrs = true;
3279 else
3280 /* Do not inline functions with multiple clone targets. */
3281 DECL_UNINLINABLE (*node) = 1;
3283 else
3285 warning (OPT_Wattributes, "%qE attribute ignored", name);
3286 *no_add_attrs = true;
3288 return NULL_TREE;
3291 /* For handling "optimize" attribute. arguments as in
3292 struct attribute_spec.handler. */
3294 static tree
3295 handle_optimize_attribute (tree *node, tree name, tree args,
3296 int ARG_UNUSED (flags), bool *no_add_attrs)
3298 /* Ensure we have a function type. */
3299 if (TREE_CODE (*node) != FUNCTION_DECL)
3301 warning (OPT_Wattributes, "%qE attribute ignored", name);
3302 *no_add_attrs = true;
3304 else
3306 struct cl_optimization cur_opts;
3307 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3309 /* Save current options. */
3310 cl_optimization_save (&cur_opts, &global_options);
3312 /* If we previously had some optimization options, use them as the
3313 default. */
3314 if (old_opts)
3315 cl_optimization_restore (&global_options,
3316 TREE_OPTIMIZATION (old_opts));
3318 /* Parse options, and update the vector. */
3319 parse_optimize_options (args, true);
3320 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3321 = build_optimization_node (&global_options);
3323 /* Restore current options. */
3324 cl_optimization_restore (&global_options, &cur_opts);
3327 return NULL_TREE;
3330 /* Handle a "no_split_stack" attribute. */
3332 static tree
3333 handle_no_split_stack_attribute (tree *node, tree name,
3334 tree ARG_UNUSED (args),
3335 int ARG_UNUSED (flags),
3336 bool *no_add_attrs)
3338 tree decl = *node;
3340 if (TREE_CODE (decl) != FUNCTION_DECL)
3342 error_at (DECL_SOURCE_LOCATION (decl),
3343 "%qE attribute applies only to functions", name);
3344 *no_add_attrs = true;
3346 else if (DECL_INITIAL (decl))
3348 error_at (DECL_SOURCE_LOCATION (decl),
3349 "can%'t set %qE attribute after definition", name);
3350 *no_add_attrs = true;
3353 return NULL_TREE;
3356 /* Handle a "returns_nonnull" attribute; arguments as in
3357 struct attribute_spec.handler. */
3359 static tree
3360 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3361 bool *no_add_attrs)
3363 // Even without a prototype we still have a return type we can check.
3364 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3366 error ("returns_nonnull attribute on a function not returning a pointer");
3367 *no_add_attrs = true;
3369 return NULL_TREE;
3372 /* Handle a "designated_init" attribute; arguments as in
3373 struct attribute_spec.handler. */
3375 static tree
3376 handle_designated_init_attribute (tree *node, tree name, tree, int,
3377 bool *no_add_attrs)
3379 if (TREE_CODE (*node) != RECORD_TYPE)
3381 error ("%qE attribute is only valid on %<struct%> type", name);
3382 *no_add_attrs = true;
3384 return NULL_TREE;
3388 /* Handle a "fallthrough" attribute; arguments as in struct
3389 attribute_spec.handler. */
3391 static tree
3392 handle_fallthrough_attribute (tree *, tree name, tree, int,
3393 bool *no_add_attrs)
3395 warning (OPT_Wattributes, "%qE attribute ignored", name);
3396 *no_add_attrs = true;
3397 return NULL_TREE;
3400 static tree
3401 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3403 /* Nothing to be done here. */
3404 return NULL_TREE;