2017-03-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-attribs.c
blobf2a88e147ba5f0ce3d88bff105f341bd10f0fcac
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_address_attribute (tree *, tree, tree,
55 int, bool *);
56 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
57 int, bool *);
58 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
59 bool *);
60 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
61 bool *);
62 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
63 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
64 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
65 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
66 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_always_inline_attribute (tree *, tree, tree, int,
68 bool *);
69 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
73 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
74 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
75 bool *);
76 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
77 bool *);
78 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_transparent_union_attribute (tree *, tree, tree,
80 int, bool *);
81 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
82 int, bool *);
83 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
86 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
87 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
88 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
89 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
90 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
91 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
94 static tree handle_visibility_attribute (tree *, tree, tree, int,
95 bool *);
96 static tree handle_tls_model_attribute (tree *, tree, tree, int,
97 bool *);
98 static tree handle_no_instrument_function_attribute (tree *, tree,
99 tree, int, bool *);
100 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
101 tree, int, bool *);
102 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
103 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
104 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
105 bool *);
106 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
107 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
108 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
109 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_deprecated_attribute (tree *, tree, tree, int,
111 bool *);
112 static tree handle_vector_size_attribute (tree *, tree, tree, int,
113 bool *);
114 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
118 bool *);
119 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
120 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
121 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
126 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
127 static tree ignore_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
133 bool *);
134 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
135 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
136 bool *);
137 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
140 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
141 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
143 /* Table of machine-independent attributes common to all C-like languages.
145 All attributes referencing arguments should be additionally processed
146 in chkp_copy_function_type_adding_bounds for correct instrumentation
147 by Pointer Bounds Checker.
148 Current list of processed common attributes: nonnull. */
149 const struct attribute_spec c_common_attribute_table[] =
151 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
152 affects_type_identity } */
153 { "packed", 0, 0, false, false, false,
154 handle_packed_attribute , false},
155 { "nocommon", 0, 0, true, false, false,
156 handle_nocommon_attribute, false},
157 { "common", 0, 0, true, false, false,
158 handle_common_attribute, false },
159 /* FIXME: logically, noreturn attributes should be listed as
160 "false, true, true" and apply to function types. But implementing this
161 would require all the places in the compiler that use TREE_THIS_VOLATILE
162 on a decl to identify non-returning functions to be located and fixed
163 to check the function type instead. */
164 { "noreturn", 0, 0, true, false, false,
165 handle_noreturn_attribute, false },
166 { "volatile", 0, 0, true, false, false,
167 handle_noreturn_attribute, false },
168 { "stack_protect", 0, 0, true, false, false,
169 handle_stack_protect_attribute, false },
170 { "noinline", 0, 0, true, false, false,
171 handle_noinline_attribute, false },
172 { "noclone", 0, 0, true, false, false,
173 handle_noclone_attribute, false },
174 { "no_icf", 0, 0, true, false, false,
175 handle_noicf_attribute, false },
176 { "leaf", 0, 0, true, false, false,
177 handle_leaf_attribute, false },
178 { "always_inline", 0, 0, true, false, false,
179 handle_always_inline_attribute, false },
180 { "gnu_inline", 0, 0, true, false, false,
181 handle_gnu_inline_attribute, false },
182 { "artificial", 0, 0, true, false, false,
183 handle_artificial_attribute, false },
184 { "flatten", 0, 0, true, false, false,
185 handle_flatten_attribute, false },
186 { "used", 0, 0, true, false, false,
187 handle_used_attribute, false },
188 { "unused", 0, 0, false, false, false,
189 handle_unused_attribute, false },
190 { "externally_visible", 0, 0, true, false, false,
191 handle_externally_visible_attribute, false },
192 { "no_reorder", 0, 0, true, false, false,
193 handle_no_reorder_attribute, false },
194 /* The same comments as for noreturn attributes apply to const ones. */
195 { "const", 0, 0, true, false, false,
196 handle_const_attribute, false },
197 { "scalar_storage_order", 1, 1, false, false, false,
198 handle_scalar_storage_order_attribute, false },
199 { "transparent_union", 0, 0, false, false, false,
200 handle_transparent_union_attribute, false },
201 { "constructor", 0, 1, true, false, false,
202 handle_constructor_attribute, false },
203 { "destructor", 0, 1, true, false, false,
204 handle_destructor_attribute, false },
205 { "mode", 1, 1, false, true, false,
206 handle_mode_attribute, false },
207 { "section", 1, 1, true, false, false,
208 handle_section_attribute, false },
209 { "aligned", 0, 1, false, false, false,
210 handle_aligned_attribute, false },
211 { "weak", 0, 0, true, false, false,
212 handle_weak_attribute, false },
213 { "noplt", 0, 0, true, false, false,
214 handle_noplt_attribute, false },
215 { "ifunc", 1, 1, true, false, false,
216 handle_ifunc_attribute, false },
217 { "alias", 1, 1, true, false, false,
218 handle_alias_attribute, false },
219 { "weakref", 0, 1, true, false, false,
220 handle_weakref_attribute, false },
221 { "no_instrument_function", 0, 0, true, false, false,
222 handle_no_instrument_function_attribute,
223 false },
224 { "no_profile_instrument_function", 0, 0, true, false, false,
225 handle_no_profile_instrument_function_attribute,
226 false },
227 { "malloc", 0, 0, true, false, false,
228 handle_malloc_attribute, false },
229 { "returns_twice", 0, 0, true, false, false,
230 handle_returns_twice_attribute, false },
231 { "no_stack_limit", 0, 0, true, false, false,
232 handle_no_limit_stack_attribute, false },
233 { "pure", 0, 0, true, false, false,
234 handle_pure_attribute, false },
235 { "transaction_callable", 0, 0, false, true, false,
236 handle_tm_attribute, false },
237 { "transaction_unsafe", 0, 0, false, true, false,
238 handle_tm_attribute, true },
239 { "transaction_safe", 0, 0, false, true, false,
240 handle_tm_attribute, true },
241 { "transaction_safe_dynamic", 0, 0, true, false, false,
242 handle_tm_attribute, false },
243 { "transaction_may_cancel_outer", 0, 0, false, true, false,
244 handle_tm_attribute, false },
245 /* ??? These two attributes didn't make the transition from the
246 Intel language document to the multi-vendor language document. */
247 { "transaction_pure", 0, 0, false, true, false,
248 handle_tm_attribute, false },
249 { "transaction_wrap", 1, 1, true, false, false,
250 handle_tm_wrap_attribute, false },
251 /* For internal use (marking of builtins) only. The name contains space
252 to prevent its usage in source code. */
253 { "no vops", 0, 0, true, false, false,
254 handle_novops_attribute, false },
255 { "deprecated", 0, 1, false, false, false,
256 handle_deprecated_attribute, false },
257 { "vector_size", 1, 1, false, true, false,
258 handle_vector_size_attribute, true },
259 { "visibility", 1, 1, false, false, false,
260 handle_visibility_attribute, false },
261 { "tls_model", 1, 1, true, false, false,
262 handle_tls_model_attribute, false },
263 { "nonnull", 0, -1, false, true, true,
264 handle_nonnull_attribute, false },
265 { "nothrow", 0, 0, true, false, false,
266 handle_nothrow_attribute, false },
267 { "may_alias", 0, 0, false, true, false, NULL, false },
268 { "cleanup", 1, 1, true, false, false,
269 handle_cleanup_attribute, false },
270 { "warn_unused_result", 0, 0, false, true, true,
271 handle_warn_unused_result_attribute, false },
272 { "sentinel", 0, 1, false, true, true,
273 handle_sentinel_attribute, false },
274 /* For internal use (marking of builtins) only. The name contains space
275 to prevent its usage in source code. */
276 { "type generic", 0, 0, false, true, true,
277 handle_type_generic_attribute, false },
278 { "alloc_size", 1, 2, false, true, true,
279 handle_alloc_size_attribute, false },
280 { "cold", 0, 0, true, false, false,
281 handle_cold_attribute, false },
282 { "hot", 0, 0, true, false, false,
283 handle_hot_attribute, false },
284 { "no_address_safety_analysis",
285 0, 0, true, false, false,
286 handle_no_address_safety_analysis_attribute,
287 false },
288 { "no_sanitize_address", 0, 0, true, false, false,
289 handle_no_sanitize_address_attribute,
290 false },
291 { "no_sanitize_thread", 0, 0, true, false, false,
292 handle_no_sanitize_address_attribute,
293 false },
294 { "no_sanitize_undefined", 0, 0, true, false, false,
295 handle_no_sanitize_undefined_attribute,
296 false },
297 { "asan odr indicator", 0, 0, true, false, false,
298 handle_asan_odr_indicator_attribute,
299 false },
300 { "warning", 1, 1, true, false, false,
301 handle_error_attribute, false },
302 { "error", 1, 1, true, false, false,
303 handle_error_attribute, false },
304 { "target", 1, -1, true, false, false,
305 handle_target_attribute, false },
306 { "target_clones", 1, -1, true, false, false,
307 handle_target_clones_attribute, false },
308 { "optimize", 1, -1, true, false, false,
309 handle_optimize_attribute, false },
310 /* For internal use only. The leading '*' both prevents its usage in
311 source code and signals that it may be overridden by machine tables. */
312 { "*tm regparm", 0, 0, false, true, true,
313 ignore_attribute, false },
314 { "no_split_stack", 0, 0, true, false, false,
315 handle_no_split_stack_attribute, false },
316 /* For internal use (marking of builtins and runtime functions) only.
317 The name contains space to prevent its usage in source code. */
318 { "fn spec", 1, 1, false, true, true,
319 handle_fnspec_attribute, false },
320 { "warn_unused", 0, 0, false, false, false,
321 handle_warn_unused_attribute, false },
322 { "returns_nonnull", 0, 0, false, true, true,
323 handle_returns_nonnull_attribute, false },
324 { "omp declare simd", 0, -1, true, false, false,
325 handle_omp_declare_simd_attribute, false },
326 { "cilk simd function", 0, -1, true, false, false,
327 handle_omp_declare_simd_attribute, false },
328 { "simd", 0, 1, true, false, false,
329 handle_simd_attribute, false },
330 { "omp declare target", 0, 0, true, false, false,
331 handle_omp_declare_target_attribute, false },
332 { "omp declare target link", 0, 0, true, false, false,
333 handle_omp_declare_target_attribute, false },
334 { "alloc_align", 1, 1, false, true, true,
335 handle_alloc_align_attribute, false },
336 { "assume_aligned", 1, 2, false, true, true,
337 handle_assume_aligned_attribute, false },
338 { "designated_init", 0, 0, false, true, false,
339 handle_designated_init_attribute, false },
340 { "bnd_variable_size", 0, 0, true, false, false,
341 handle_bnd_variable_size_attribute, false },
342 { "bnd_legacy", 0, 0, true, false, false,
343 handle_bnd_legacy, false },
344 { "bnd_instrument", 0, 0, true, false, false,
345 handle_bnd_instrument, false },
346 { "fallthrough", 0, 0, false, false, false,
347 handle_fallthrough_attribute, false },
348 { NULL, 0, 0, false, false, false, NULL, false }
351 /* Give the specifications for the format attributes, used by C and all
352 descendants.
354 All attributes referencing arguments should be additionally processed
355 in chkp_copy_function_type_adding_bounds for correct instrumentation
356 by Pointer Bounds Checker.
357 Current list of processed format attributes: format, format_arg. */
358 const struct attribute_spec c_common_format_attribute_table[] =
360 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
361 affects_type_identity } */
362 { "format", 3, 3, false, true, true,
363 handle_format_attribute, false },
364 { "format_arg", 1, 1, false, true, true,
365 handle_format_arg_attribute, false },
366 { NULL, 0, 0, false, false, false, NULL, false }
369 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
370 identifier as an argument, so the front end shouldn't look it up. */
372 bool
373 attribute_takes_identifier_p (const_tree attr_id)
375 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
376 if (spec == NULL)
377 /* Unknown attribute that we'll end up ignoring, return true so we
378 don't complain about an identifier argument. */
379 return true;
380 else if (!strcmp ("mode", spec->name)
381 || !strcmp ("format", spec->name)
382 || !strcmp ("cleanup", spec->name))
383 return true;
384 else
385 return targetm.attribute_takes_identifier_p (attr_id);
388 /* Attribute handlers common to C front ends. */
390 /* Handle a "packed" attribute; arguments as in
391 struct attribute_spec.handler. */
393 static tree
394 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
395 int flags, bool *no_add_attrs)
397 if (TYPE_P (*node))
399 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
400 *node = build_variant_type_copy (*node);
401 TYPE_PACKED (*node) = 1;
403 else if (TREE_CODE (*node) == FIELD_DECL)
405 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
406 /* Still pack bitfields. */
407 && ! DECL_INITIAL (*node))
408 warning (OPT_Wattributes,
409 "%qE attribute ignored for field of type %qT",
410 name, TREE_TYPE (*node));
411 else
412 DECL_PACKED (*node) = 1;
414 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
415 used for DECL_REGISTER. It wouldn't mean anything anyway.
416 We can't set DECL_PACKED on the type of a TYPE_DECL, because
417 that changes what the typedef is typing. */
418 else
420 warning (OPT_Wattributes, "%qE attribute ignored", name);
421 *no_add_attrs = true;
424 return NULL_TREE;
427 /* Handle a "nocommon" attribute; arguments as in
428 struct attribute_spec.handler. */
430 static tree
431 handle_nocommon_attribute (tree *node, tree name,
432 tree ARG_UNUSED (args),
433 int ARG_UNUSED (flags), bool *no_add_attrs)
435 if (VAR_P (*node))
436 DECL_COMMON (*node) = 0;
437 else
439 warning (OPT_Wattributes, "%qE attribute ignored", name);
440 *no_add_attrs = true;
443 return NULL_TREE;
446 /* Handle a "common" attribute; arguments as in
447 struct attribute_spec.handler. */
449 static tree
450 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
451 int ARG_UNUSED (flags), bool *no_add_attrs)
453 if (VAR_P (*node))
454 DECL_COMMON (*node) = 1;
455 else
457 warning (OPT_Wattributes, "%qE attribute ignored", name);
458 *no_add_attrs = true;
461 return NULL_TREE;
464 /* Handle a "noreturn" attribute; arguments as in
465 struct attribute_spec.handler. */
467 static tree
468 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
469 int ARG_UNUSED (flags), bool *no_add_attrs)
471 tree type = TREE_TYPE (*node);
473 /* See FIXME comment in c_common_attribute_table. */
474 if (TREE_CODE (*node) == FUNCTION_DECL
475 || objc_method_decl (TREE_CODE (*node)))
476 TREE_THIS_VOLATILE (*node) = 1;
477 else if (TREE_CODE (type) == POINTER_TYPE
478 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
479 TREE_TYPE (*node)
480 = (build_qualified_type
481 (build_pointer_type
482 (build_type_variant (TREE_TYPE (type),
483 TYPE_READONLY (TREE_TYPE (type)), 1)),
484 TYPE_QUALS (type)));
485 else
487 warning (OPT_Wattributes, "%qE attribute ignored", name);
488 *no_add_attrs = true;
491 return NULL_TREE;
494 /* Handle a "hot" and attribute; arguments as in
495 struct attribute_spec.handler. */
497 static tree
498 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
499 int ARG_UNUSED (flags), bool *no_add_attrs)
501 if (TREE_CODE (*node) == FUNCTION_DECL
502 || TREE_CODE (*node) == LABEL_DECL)
504 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
506 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
507 "with attribute %qs", name, "cold");
508 *no_add_attrs = true;
510 /* Most of the rest of the hot processing is done later with
511 lookup_attribute. */
513 else
515 warning (OPT_Wattributes, "%qE attribute ignored", name);
516 *no_add_attrs = true;
519 return NULL_TREE;
522 /* Handle a "cold" and attribute; arguments as in
523 struct attribute_spec.handler. */
525 static tree
526 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
527 int ARG_UNUSED (flags), bool *no_add_attrs)
529 if (TREE_CODE (*node) == FUNCTION_DECL
530 || TREE_CODE (*node) == LABEL_DECL)
532 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
534 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
535 "with attribute %qs", name, "hot");
536 *no_add_attrs = true;
538 /* Most of the rest of the cold processing is done later with
539 lookup_attribute. */
541 else
543 warning (OPT_Wattributes, "%qE attribute ignored", name);
544 *no_add_attrs = true;
547 return NULL_TREE;
550 /* Handle a "no_sanitize_address" attribute; arguments as in
551 struct attribute_spec.handler. */
553 static tree
554 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
555 bool *no_add_attrs)
557 if (TREE_CODE (*node) != FUNCTION_DECL)
559 warning (OPT_Wattributes, "%qE attribute ignored", name);
560 *no_add_attrs = true;
563 return NULL_TREE;
566 /* Handle a "no_address_safety_analysis" attribute; arguments as in
567 struct attribute_spec.handler. */
569 static tree
570 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
571 bool *no_add_attrs)
573 if (TREE_CODE (*node) != FUNCTION_DECL)
574 warning (OPT_Wattributes, "%qE attribute ignored", name);
575 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
576 DECL_ATTRIBUTES (*node)
577 = tree_cons (get_identifier ("no_sanitize_address"),
578 NULL_TREE, DECL_ATTRIBUTES (*node));
579 *no_add_attrs = true;
580 return NULL_TREE;
583 /* Handle a "no_sanitize_undefined" attribute; arguments as in
584 struct attribute_spec.handler. */
586 static tree
587 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
588 bool *no_add_attrs)
590 if (TREE_CODE (*node) != FUNCTION_DECL)
592 warning (OPT_Wattributes, "%qE attribute ignored", name);
593 *no_add_attrs = true;
596 return NULL_TREE;
599 /* Handle an "asan odr indicator" attribute; arguments as in
600 struct attribute_spec.handler. */
602 static tree
603 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
605 return NULL_TREE;
608 /* Handle a "stack_protect" attribute; arguments as in
609 struct attribute_spec.handler. */
611 static tree
612 handle_stack_protect_attribute (tree *node, tree name, tree, int,
613 bool *no_add_attrs)
615 if (TREE_CODE (*node) != FUNCTION_DECL)
617 warning (OPT_Wattributes, "%qE attribute ignored", name);
618 *no_add_attrs = true;
620 else
621 DECL_ATTRIBUTES (*node)
622 = tree_cons (get_identifier ("stack_protect"),
623 NULL_TREE, DECL_ATTRIBUTES (*node));
625 return NULL_TREE;
628 /* Handle a "noinline" attribute; arguments as in
629 struct attribute_spec.handler. */
631 static tree
632 handle_noinline_attribute (tree *node, tree name,
633 tree ARG_UNUSED (args),
634 int ARG_UNUSED (flags), bool *no_add_attrs)
636 if (TREE_CODE (*node) == FUNCTION_DECL)
638 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
640 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
641 "with attribute %qs", name, "always_inline");
642 *no_add_attrs = true;
644 else
645 DECL_UNINLINABLE (*node) = 1;
647 else
649 warning (OPT_Wattributes, "%qE attribute ignored", name);
650 *no_add_attrs = true;
653 return NULL_TREE;
656 /* Handle a "noclone" attribute; arguments as in
657 struct attribute_spec.handler. */
659 static tree
660 handle_noclone_attribute (tree *node, tree name,
661 tree ARG_UNUSED (args),
662 int ARG_UNUSED (flags), bool *no_add_attrs)
664 if (TREE_CODE (*node) != FUNCTION_DECL)
666 warning (OPT_Wattributes, "%qE attribute ignored", name);
667 *no_add_attrs = true;
670 return NULL_TREE;
673 /* Handle a "no_icf" attribute; arguments as in
674 struct attribute_spec.handler. */
676 static tree
677 handle_noicf_attribute (tree *node, tree name,
678 tree ARG_UNUSED (args),
679 int ARG_UNUSED (flags), bool *no_add_attrs)
681 if (TREE_CODE (*node) != FUNCTION_DECL)
683 warning (OPT_Wattributes, "%qE attribute ignored", name);
684 *no_add_attrs = true;
687 return NULL_TREE;
691 /* Handle a "always_inline" attribute; arguments as in
692 struct attribute_spec.handler. */
694 static tree
695 handle_always_inline_attribute (tree *node, tree name,
696 tree ARG_UNUSED (args),
697 int ARG_UNUSED (flags),
698 bool *no_add_attrs)
700 if (TREE_CODE (*node) == FUNCTION_DECL)
702 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
704 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
705 "with %qs attribute", name, "noinline");
706 *no_add_attrs = true;
708 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
710 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
711 "with %qs attribute", name, "target_clones");
712 *no_add_attrs = true;
714 else
715 /* Set the attribute and mark it for disregarding inline
716 limits. */
717 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
719 else
721 warning (OPT_Wattributes, "%qE attribute ignored", name);
722 *no_add_attrs = true;
725 return NULL_TREE;
728 /* Handle a "gnu_inline" attribute; arguments as in
729 struct attribute_spec.handler. */
731 static tree
732 handle_gnu_inline_attribute (tree *node, tree name,
733 tree ARG_UNUSED (args),
734 int ARG_UNUSED (flags),
735 bool *no_add_attrs)
737 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
739 /* Do nothing else, just set the attribute. We'll get at
740 it later with lookup_attribute. */
742 else
744 warning (OPT_Wattributes, "%qE attribute ignored", name);
745 *no_add_attrs = true;
748 return NULL_TREE;
751 /* Handle a "leaf" attribute; arguments as in
752 struct attribute_spec.handler. */
754 static tree
755 handle_leaf_attribute (tree *node, tree name,
756 tree ARG_UNUSED (args),
757 int ARG_UNUSED (flags), bool *no_add_attrs)
759 if (TREE_CODE (*node) != FUNCTION_DECL)
761 warning (OPT_Wattributes, "%qE attribute ignored", name);
762 *no_add_attrs = true;
764 if (!TREE_PUBLIC (*node))
766 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
767 "functions", name);
768 *no_add_attrs = true;
771 return NULL_TREE;
774 /* Handle an "artificial" attribute; arguments as in
775 struct attribute_spec.handler. */
777 static tree
778 handle_artificial_attribute (tree *node, tree name,
779 tree ARG_UNUSED (args),
780 int ARG_UNUSED (flags),
781 bool *no_add_attrs)
783 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
785 /* Do nothing else, just set the attribute. We'll get at
786 it later with lookup_attribute. */
788 else
790 warning (OPT_Wattributes, "%qE attribute ignored", name);
791 *no_add_attrs = true;
794 return NULL_TREE;
797 /* Handle a "flatten" attribute; arguments as in
798 struct attribute_spec.handler. */
800 static tree
801 handle_flatten_attribute (tree *node, tree name,
802 tree args ATTRIBUTE_UNUSED,
803 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
805 if (TREE_CODE (*node) == FUNCTION_DECL)
806 /* Do nothing else, just set the attribute. We'll get at
807 it later with lookup_attribute. */
809 else
811 warning (OPT_Wattributes, "%qE attribute ignored", name);
812 *no_add_attrs = true;
815 return NULL_TREE;
818 /* Handle a "warning" or "error" attribute; arguments as in
819 struct attribute_spec.handler. */
821 static tree
822 handle_error_attribute (tree *node, tree name, tree args,
823 int ARG_UNUSED (flags), bool *no_add_attrs)
825 if (TREE_CODE (*node) == FUNCTION_DECL
826 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
827 /* Do nothing else, just set the attribute. We'll get at
828 it later with lookup_attribute. */
830 else
832 warning (OPT_Wattributes, "%qE attribute ignored", name);
833 *no_add_attrs = true;
836 return NULL_TREE;
839 /* Handle a "used" attribute; arguments as in
840 struct attribute_spec.handler. */
842 static tree
843 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
844 int ARG_UNUSED (flags), bool *no_add_attrs)
846 tree node = *pnode;
848 if (TREE_CODE (node) == FUNCTION_DECL
849 || (VAR_P (node) && TREE_STATIC (node))
850 || (TREE_CODE (node) == TYPE_DECL))
852 TREE_USED (node) = 1;
853 DECL_PRESERVE_P (node) = 1;
854 if (VAR_P (node))
855 DECL_READ_P (node) = 1;
857 else
859 warning (OPT_Wattributes, "%qE attribute ignored", name);
860 *no_add_attrs = true;
863 return NULL_TREE;
866 /* Handle a "unused" attribute; arguments as in
867 struct attribute_spec.handler. */
869 tree
870 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
871 int flags, bool *no_add_attrs)
873 if (DECL_P (*node))
875 tree decl = *node;
877 if (TREE_CODE (decl) == PARM_DECL
878 || VAR_OR_FUNCTION_DECL_P (decl)
879 || TREE_CODE (decl) == LABEL_DECL
880 || TREE_CODE (decl) == CONST_DECL
881 || TREE_CODE (decl) == TYPE_DECL)
883 TREE_USED (decl) = 1;
884 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
885 DECL_READ_P (decl) = 1;
887 else
889 warning (OPT_Wattributes, "%qE attribute ignored", name);
890 *no_add_attrs = true;
893 else
895 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
896 *node = build_variant_type_copy (*node);
897 TREE_USED (*node) = 1;
900 return NULL_TREE;
903 /* Handle a "externally_visible" attribute; arguments as in
904 struct attribute_spec.handler. */
906 static tree
907 handle_externally_visible_attribute (tree *pnode, tree name,
908 tree ARG_UNUSED (args),
909 int ARG_UNUSED (flags),
910 bool *no_add_attrs)
912 tree node = *pnode;
914 if (VAR_OR_FUNCTION_DECL_P (node))
916 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
917 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
919 warning (OPT_Wattributes,
920 "%qE attribute have effect only on public objects", name);
921 *no_add_attrs = true;
924 else
926 warning (OPT_Wattributes, "%qE attribute ignored", name);
927 *no_add_attrs = true;
930 return NULL_TREE;
933 /* Handle the "no_reorder" attribute. Arguments as in
934 struct attribute_spec.handler. */
936 static tree
937 handle_no_reorder_attribute (tree *pnode,
938 tree name,
939 tree,
940 int,
941 bool *no_add_attrs)
943 tree node = *pnode;
945 if (!VAR_OR_FUNCTION_DECL_P (node)
946 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
948 warning (OPT_Wattributes,
949 "%qE attribute only affects top level objects",
950 name);
951 *no_add_attrs = true;
954 return NULL_TREE;
957 /* Handle a "const" attribute; arguments as in
958 struct attribute_spec.handler. */
960 static tree
961 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
962 int ARG_UNUSED (flags), bool *no_add_attrs)
964 tree type = TREE_TYPE (*node);
966 /* See FIXME comment on noreturn in c_common_attribute_table. */
967 if (TREE_CODE (*node) == FUNCTION_DECL)
968 TREE_READONLY (*node) = 1;
969 else if (TREE_CODE (type) == POINTER_TYPE
970 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
971 TREE_TYPE (*node)
972 = (build_qualified_type
973 (build_pointer_type
974 (build_type_variant (TREE_TYPE (type), 1,
975 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
976 TYPE_QUALS (type)));
977 else
979 warning (OPT_Wattributes, "%qE attribute ignored", name);
980 *no_add_attrs = true;
983 return NULL_TREE;
986 /* Handle a "scalar_storage_order" attribute; arguments as in
987 struct attribute_spec.handler. */
989 static tree
990 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
991 int flags, bool *no_add_attrs)
993 tree id = TREE_VALUE (args);
994 tree type;
996 if (TREE_CODE (*node) == TYPE_DECL
997 && ! (flags & ATTR_FLAG_CXX11))
998 node = &TREE_TYPE (*node);
999 type = *node;
1001 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1003 error ("scalar_storage_order is not supported because endianness "
1004 "is not uniform");
1005 return NULL_TREE;
1008 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1010 bool reverse = false;
1012 if (TREE_CODE (id) == STRING_CST
1013 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1014 reverse = !BYTES_BIG_ENDIAN;
1015 else if (TREE_CODE (id) == STRING_CST
1016 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1017 reverse = BYTES_BIG_ENDIAN;
1018 else
1020 error ("scalar_storage_order argument must be one of \"big-endian\""
1021 " or \"little-endian\"");
1022 return NULL_TREE;
1025 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1027 if (reverse)
1028 /* A type variant isn't good enough, since we don't want a cast
1029 to such a type to be removed as a no-op. */
1030 *node = type = build_duplicate_type (type);
1033 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1034 return NULL_TREE;
1037 warning (OPT_Wattributes, "%qE attribute ignored", name);
1038 *no_add_attrs = true;
1039 return NULL_TREE;
1042 /* Handle a "transparent_union" attribute; arguments as in
1043 struct attribute_spec.handler. */
1045 static tree
1046 handle_transparent_union_attribute (tree *node, tree name,
1047 tree ARG_UNUSED (args), int flags,
1048 bool *no_add_attrs)
1050 tree type;
1052 *no_add_attrs = true;
1054 if (TREE_CODE (*node) == TYPE_DECL
1055 && ! (flags & ATTR_FLAG_CXX11))
1056 node = &TREE_TYPE (*node);
1057 type = *node;
1059 if (TREE_CODE (type) == UNION_TYPE)
1061 /* Make sure that the first field will work for a transparent union.
1062 If the type isn't complete yet, leave the check to the code in
1063 finish_struct. */
1064 if (TYPE_SIZE (type))
1066 tree first = first_field (type);
1067 if (first == NULL_TREE
1068 || DECL_ARTIFICIAL (first)
1069 || TYPE_MODE (type) != DECL_MODE (first))
1070 goto ignored;
1073 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1075 /* If the type isn't complete yet, setting the flag
1076 on a variant wouldn't ever be checked. */
1077 if (!TYPE_SIZE (type))
1078 goto ignored;
1080 /* build_duplicate_type doesn't work for C++. */
1081 if (c_dialect_cxx ())
1082 goto ignored;
1084 /* A type variant isn't good enough, since we don't want a cast
1085 to such a type to be removed as a no-op. */
1086 *node = type = build_duplicate_type (type);
1089 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1090 TYPE_TRANSPARENT_AGGR (t) = 1;
1091 return NULL_TREE;
1094 ignored:
1095 warning (OPT_Wattributes, "%qE attribute ignored", name);
1096 return NULL_TREE;
1099 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1100 get the requested priority for a constructor or destructor,
1101 possibly issuing diagnostics for invalid or reserved
1102 priorities. */
1104 static priority_type
1105 get_priority (tree args, bool is_destructor)
1107 HOST_WIDE_INT pri;
1108 tree arg;
1110 if (!args)
1111 return DEFAULT_INIT_PRIORITY;
1113 if (!SUPPORTS_INIT_PRIORITY)
1115 if (is_destructor)
1116 error ("destructor priorities are not supported");
1117 else
1118 error ("constructor priorities are not supported");
1119 return DEFAULT_INIT_PRIORITY;
1122 arg = TREE_VALUE (args);
1123 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1124 goto invalid;
1125 if (arg == error_mark_node)
1126 return DEFAULT_INIT_PRIORITY;
1127 arg = default_conversion (arg);
1128 if (!tree_fits_shwi_p (arg)
1129 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1130 goto invalid;
1132 pri = tree_to_shwi (arg);
1133 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1134 goto invalid;
1136 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1138 if (is_destructor)
1139 warning (0,
1140 "destructor priorities from 0 to %d are reserved "
1141 "for the implementation",
1142 MAX_RESERVED_INIT_PRIORITY);
1143 else
1144 warning (0,
1145 "constructor priorities from 0 to %d are reserved "
1146 "for the implementation",
1147 MAX_RESERVED_INIT_PRIORITY);
1149 return pri;
1151 invalid:
1152 if (is_destructor)
1153 error ("destructor priorities must be integers from 0 to %d inclusive",
1154 MAX_INIT_PRIORITY);
1155 else
1156 error ("constructor priorities must be integers from 0 to %d inclusive",
1157 MAX_INIT_PRIORITY);
1158 return DEFAULT_INIT_PRIORITY;
1161 /* Handle a "constructor" attribute; arguments as in
1162 struct attribute_spec.handler. */
1164 static tree
1165 handle_constructor_attribute (tree *node, tree name, tree args,
1166 int ARG_UNUSED (flags),
1167 bool *no_add_attrs)
1169 tree decl = *node;
1170 tree type = TREE_TYPE (decl);
1172 if (TREE_CODE (decl) == FUNCTION_DECL
1173 && TREE_CODE (type) == FUNCTION_TYPE
1174 && decl_function_context (decl) == 0)
1176 priority_type priority;
1177 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1178 priority = get_priority (args, /*is_destructor=*/false);
1179 SET_DECL_INIT_PRIORITY (decl, priority);
1180 TREE_USED (decl) = 1;
1182 else
1184 warning (OPT_Wattributes, "%qE attribute ignored", name);
1185 *no_add_attrs = true;
1188 return NULL_TREE;
1191 /* Handle a "destructor" attribute; arguments as in
1192 struct attribute_spec.handler. */
1194 static tree
1195 handle_destructor_attribute (tree *node, tree name, tree args,
1196 int ARG_UNUSED (flags),
1197 bool *no_add_attrs)
1199 tree decl = *node;
1200 tree type = TREE_TYPE (decl);
1202 if (TREE_CODE (decl) == FUNCTION_DECL
1203 && TREE_CODE (type) == FUNCTION_TYPE
1204 && decl_function_context (decl) == 0)
1206 priority_type priority;
1207 DECL_STATIC_DESTRUCTOR (decl) = 1;
1208 priority = get_priority (args, /*is_destructor=*/true);
1209 SET_DECL_FINI_PRIORITY (decl, priority);
1210 TREE_USED (decl) = 1;
1212 else
1214 warning (OPT_Wattributes, "%qE attribute ignored", name);
1215 *no_add_attrs = true;
1218 return NULL_TREE;
1221 /* Nonzero if the mode is a valid vector mode for this architecture.
1222 This returns nonzero even if there is no hardware support for the
1223 vector mode, but we can emulate with narrower modes. */
1225 static bool
1226 vector_mode_valid_p (machine_mode mode)
1228 enum mode_class mclass = GET_MODE_CLASS (mode);
1229 machine_mode innermode;
1231 /* Doh! What's going on? */
1232 if (mclass != MODE_VECTOR_INT
1233 && mclass != MODE_VECTOR_FLOAT
1234 && mclass != MODE_VECTOR_FRACT
1235 && mclass != MODE_VECTOR_UFRACT
1236 && mclass != MODE_VECTOR_ACCUM
1237 && mclass != MODE_VECTOR_UACCUM)
1238 return false;
1240 /* Hardware support. Woo hoo! */
1241 if (targetm.vector_mode_supported_p (mode))
1242 return true;
1244 innermode = GET_MODE_INNER (mode);
1246 /* We should probably return 1 if requesting V4DI and we have no DI,
1247 but we have V2DI, but this is probably very unlikely. */
1249 /* If we have support for the inner mode, we can safely emulate it.
1250 We may not have V2DI, but me can emulate with a pair of DIs. */
1251 return targetm.scalar_mode_supported_p (innermode);
1255 /* Handle a "mode" attribute; arguments as in
1256 struct attribute_spec.handler. */
1258 static tree
1259 handle_mode_attribute (tree *node, tree name, tree args,
1260 int ARG_UNUSED (flags), bool *no_add_attrs)
1262 tree type = *node;
1263 tree ident = TREE_VALUE (args);
1265 *no_add_attrs = true;
1267 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1268 warning (OPT_Wattributes, "%qE attribute ignored", name);
1269 else
1271 int j;
1272 const char *p = IDENTIFIER_POINTER (ident);
1273 int len = strlen (p);
1274 machine_mode mode = VOIDmode;
1275 tree typefm;
1276 bool valid_mode;
1278 if (len > 4 && p[0] == '_' && p[1] == '_'
1279 && p[len - 1] == '_' && p[len - 2] == '_')
1281 char *newp = (char *) alloca (len - 1);
1283 strcpy (newp, &p[2]);
1284 newp[len - 4] = '\0';
1285 p = newp;
1288 /* Change this type to have a type with the specified mode.
1289 First check for the special modes. */
1290 if (!strcmp (p, "byte"))
1291 mode = byte_mode;
1292 else if (!strcmp (p, "word"))
1293 mode = word_mode;
1294 else if (!strcmp (p, "pointer"))
1295 mode = ptr_mode;
1296 else if (!strcmp (p, "libgcc_cmp_return"))
1297 mode = targetm.libgcc_cmp_return_mode ();
1298 else if (!strcmp (p, "libgcc_shift_count"))
1299 mode = targetm.libgcc_shift_count_mode ();
1300 else if (!strcmp (p, "unwind_word"))
1301 mode = targetm.unwind_word_mode ();
1302 else
1303 for (j = 0; j < NUM_MACHINE_MODES; j++)
1304 if (!strcmp (p, GET_MODE_NAME (j)))
1306 mode = (machine_mode) j;
1307 break;
1310 if (mode == VOIDmode)
1312 error ("unknown machine mode %qE", ident);
1313 return NULL_TREE;
1316 valid_mode = false;
1317 switch (GET_MODE_CLASS (mode))
1319 case MODE_INT:
1320 case MODE_PARTIAL_INT:
1321 case MODE_FLOAT:
1322 case MODE_DECIMAL_FLOAT:
1323 case MODE_FRACT:
1324 case MODE_UFRACT:
1325 case MODE_ACCUM:
1326 case MODE_UACCUM:
1327 valid_mode = targetm.scalar_mode_supported_p (mode);
1328 break;
1330 case MODE_COMPLEX_INT:
1331 case MODE_COMPLEX_FLOAT:
1332 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1333 break;
1335 case MODE_VECTOR_INT:
1336 case MODE_VECTOR_FLOAT:
1337 case MODE_VECTOR_FRACT:
1338 case MODE_VECTOR_UFRACT:
1339 case MODE_VECTOR_ACCUM:
1340 case MODE_VECTOR_UACCUM:
1341 warning (OPT_Wattributes, "specifying vector types with "
1342 "__attribute__ ((mode)) is deprecated");
1343 warning (OPT_Wattributes,
1344 "use __attribute__ ((vector_size)) instead");
1345 valid_mode = vector_mode_valid_p (mode);
1346 break;
1348 default:
1349 break;
1351 if (!valid_mode)
1353 error ("unable to emulate %qs", p);
1354 return NULL_TREE;
1357 if (POINTER_TYPE_P (type))
1359 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1360 tree (*fn)(tree, machine_mode, bool);
1362 if (!targetm.addr_space.valid_pointer_mode (mode, as))
1364 error ("invalid pointer mode %qs", p);
1365 return NULL_TREE;
1368 if (TREE_CODE (type) == POINTER_TYPE)
1369 fn = build_pointer_type_for_mode;
1370 else
1371 fn = build_reference_type_for_mode;
1372 typefm = fn (TREE_TYPE (type), mode, false);
1374 else
1376 /* For fixed-point modes, we need to test if the signness of type
1377 and the machine mode are consistent. */
1378 if (ALL_FIXED_POINT_MODE_P (mode)
1379 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1381 error ("signedness of type and machine mode %qs don%'t match", p);
1382 return NULL_TREE;
1384 /* For fixed-point modes, we need to pass saturating info. */
1385 typefm = lang_hooks.types.type_for_mode (mode,
1386 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1387 : TYPE_UNSIGNED (type));
1390 if (typefm == NULL_TREE)
1392 error ("no data type for mode %qs", p);
1393 return NULL_TREE;
1395 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1397 /* For enumeral types, copy the precision from the integer
1398 type returned above. If not an INTEGER_TYPE, we can't use
1399 this mode for this type. */
1400 if (TREE_CODE (typefm) != INTEGER_TYPE)
1402 error ("cannot use mode %qs for enumeral types", p);
1403 return NULL_TREE;
1406 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1408 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1409 typefm = type;
1411 else
1413 /* We cannot build a type variant, as there's code that assumes
1414 that TYPE_MAIN_VARIANT has the same mode. This includes the
1415 debug generators. Instead, create a subrange type. This
1416 results in all of the enumeral values being emitted only once
1417 in the original, and the subtype gets them by reference. */
1418 if (TYPE_UNSIGNED (type))
1419 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1420 else
1421 typefm = make_signed_type (TYPE_PRECISION (typefm));
1422 TREE_TYPE (typefm) = type;
1425 else if (VECTOR_MODE_P (mode)
1426 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1427 : TREE_CODE (type) != TREE_CODE (typefm))
1429 error ("mode %qs applied to inappropriate type", p);
1430 return NULL_TREE;
1433 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1436 return NULL_TREE;
1439 /* Handle a "section" attribute; arguments as in
1440 struct attribute_spec.handler. */
1442 static tree
1443 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1444 int ARG_UNUSED (flags), bool *no_add_attrs)
1446 tree decl = *node;
1448 if (!targetm_common.have_named_sections)
1450 error_at (DECL_SOURCE_LOCATION (*node),
1451 "section attributes are not supported for this target");
1452 goto fail;
1455 if (!VAR_OR_FUNCTION_DECL_P (decl))
1457 error ("section attribute not allowed for %q+D", *node);
1458 goto fail;
1461 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1463 error ("section attribute argument not a string constant");
1464 goto fail;
1467 if (VAR_P (decl)
1468 && current_function_decl != NULL_TREE
1469 && !TREE_STATIC (decl))
1471 error_at (DECL_SOURCE_LOCATION (decl),
1472 "section attribute cannot be specified for local variables");
1473 goto fail;
1476 /* The decl may have already been given a section attribute
1477 from a previous declaration. Ensure they match. */
1478 if (DECL_SECTION_NAME (decl) != NULL
1479 && strcmp (DECL_SECTION_NAME (decl),
1480 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1482 error ("section of %q+D conflicts with previous declaration", *node);
1483 goto fail;
1486 if (VAR_P (decl)
1487 && !targetm.have_tls && targetm.emutls.tmpl_section
1488 && DECL_THREAD_LOCAL_P (decl))
1490 error ("section of %q+D cannot be overridden", *node);
1491 goto fail;
1494 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1495 return NULL_TREE;
1497 fail:
1498 *no_add_attrs = true;
1499 return NULL_TREE;
1502 /* If in c++-11, check if the c++-11 alignment constraint with respect
1503 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1504 c++-11 mode, does nothing.
1506 [dcl.align]2/ says:
1508 [* if the constant expression evaluates to a fundamental alignment,
1509 the alignment requirement of the declared entity shall be the
1510 specified fundamental alignment.
1512 * if the constant expression evaluates to an extended alignment
1513 and the implementation supports that alignment in the context
1514 of the declaration, the alignment of the declared entity shall
1515 be that alignment
1517 * if the constant expression evaluates to an extended alignment
1518 and the implementation does not support that alignment in the
1519 context of the declaration, the program is ill-formed]. */
1521 static bool
1522 check_cxx_fundamental_alignment_constraints (tree node,
1523 unsigned align_log,
1524 int flags)
1526 bool alignment_too_large_p = false;
1527 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1528 unsigned max_align = 0;
1530 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1531 || (node == NULL_TREE || node == error_mark_node))
1532 return true;
1534 if (cxx_fundamental_alignment_p (requested_alignment))
1535 return true;
1537 if (VAR_P (node))
1539 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1540 /* For file scope variables and static members, the target supports
1541 alignments that are at most MAX_OFILE_ALIGNMENT. */
1542 max_align = MAX_OFILE_ALIGNMENT;
1543 else
1544 /* For stack variables, the target supports at most
1545 MAX_STACK_ALIGNMENT. */
1546 max_align = MAX_STACK_ALIGNMENT;
1547 if (requested_alignment > max_align)
1548 alignment_too_large_p = true;
1550 /* Let's be liberal for types and fields; don't limit their alignment any
1551 more than check_user_alignment already did. */
1553 if (alignment_too_large_p)
1554 pedwarn (input_location, OPT_Wattributes,
1555 "requested alignment %d is larger than %d",
1556 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1558 return !alignment_too_large_p;
1561 /* Handle a "aligned" attribute; arguments as in
1562 struct attribute_spec.handler. */
1564 static tree
1565 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1566 int flags, bool *no_add_attrs)
1568 tree decl = NULL_TREE;
1569 tree *type = NULL;
1570 int is_type = 0;
1571 tree align_expr;
1572 int i;
1574 if (args)
1576 align_expr = TREE_VALUE (args);
1577 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1578 && TREE_CODE (align_expr) != FUNCTION_DECL)
1579 align_expr = default_conversion (align_expr);
1581 else
1582 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1584 if (DECL_P (*node))
1586 decl = *node;
1587 type = &TREE_TYPE (decl);
1588 is_type = TREE_CODE (*node) == TYPE_DECL;
1590 else if (TYPE_P (*node))
1591 type = node, is_type = 1;
1593 if ((i = check_user_alignment (align_expr, true)) == -1
1594 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
1595 *no_add_attrs = true;
1596 else if (is_type)
1598 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1599 /* OK, modify the type in place. */;
1600 /* If we have a TYPE_DECL, then copy the type, so that we
1601 don't accidentally modify a builtin type. See pushdecl. */
1602 else if (decl && TREE_TYPE (decl) != error_mark_node
1603 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1605 tree tt = TREE_TYPE (decl);
1606 *type = build_variant_type_copy (*type);
1607 DECL_ORIGINAL_TYPE (decl) = tt;
1608 TYPE_NAME (*type) = decl;
1609 TREE_USED (*type) = TREE_USED (decl);
1610 TREE_TYPE (decl) = *type;
1612 else
1613 *type = build_variant_type_copy (*type);
1615 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
1616 TYPE_USER_ALIGN (*type) = 1;
1618 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1619 && TREE_CODE (decl) != FIELD_DECL)
1621 error ("alignment may not be specified for %q+D", decl);
1622 *no_add_attrs = true;
1624 else if (DECL_USER_ALIGN (decl)
1625 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1626 /* C++-11 [dcl.align/4]:
1628 When multiple alignment-specifiers are specified for an
1629 entity, the alignment requirement shall be set to the
1630 strictest specified alignment.
1632 This formally comes from the c++11 specification but we are
1633 doing it for the GNU attribute syntax as well. */
1634 *no_add_attrs = true;
1635 else if (TREE_CODE (decl) == FUNCTION_DECL
1636 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
1638 if (DECL_USER_ALIGN (decl))
1639 error ("alignment for %q+D was previously specified as %d "
1640 "and may not be decreased", decl,
1641 DECL_ALIGN (decl) / BITS_PER_UNIT);
1642 else
1643 error ("alignment for %q+D must be at least %d", decl,
1644 DECL_ALIGN (decl) / BITS_PER_UNIT);
1645 *no_add_attrs = true;
1647 else
1649 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
1650 DECL_USER_ALIGN (decl) = 1;
1653 return NULL_TREE;
1656 /* Handle a "weak" attribute; arguments as in
1657 struct attribute_spec.handler. */
1659 static tree
1660 handle_weak_attribute (tree *node, tree name,
1661 tree ARG_UNUSED (args),
1662 int ARG_UNUSED (flags),
1663 bool * ARG_UNUSED (no_add_attrs))
1665 if (TREE_CODE (*node) == FUNCTION_DECL
1666 && DECL_DECLARED_INLINE_P (*node))
1668 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1669 *no_add_attrs = true;
1671 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1673 error ("indirect function %q+D cannot be declared weak", *node);
1674 *no_add_attrs = true;
1675 return NULL_TREE;
1677 else if (VAR_OR_FUNCTION_DECL_P (*node))
1678 declare_weak (*node);
1679 else
1680 warning (OPT_Wattributes, "%qE attribute ignored", name);
1682 return NULL_TREE;
1685 /* Handle a "noplt" attribute; arguments as in
1686 struct attribute_spec.handler. */
1688 static tree
1689 handle_noplt_attribute (tree *node, tree name,
1690 tree ARG_UNUSED (args),
1691 int ARG_UNUSED (flags),
1692 bool * ARG_UNUSED (no_add_attrs))
1694 if (TREE_CODE (*node) != FUNCTION_DECL)
1696 warning (OPT_Wattributes,
1697 "%qE attribute is only applicable on functions", name);
1698 *no_add_attrs = true;
1699 return NULL_TREE;
1701 return NULL_TREE;
1704 /* Handle an "alias" or "ifunc" attribute; arguments as in
1705 struct attribute_spec.handler, except that IS_ALIAS tells us
1706 whether this is an alias as opposed to ifunc attribute. */
1708 static tree
1709 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
1710 bool *no_add_attrs)
1712 tree decl = *node;
1714 if (TREE_CODE (decl) != FUNCTION_DECL
1715 && (!is_alias || !VAR_P (decl)))
1717 warning (OPT_Wattributes, "%qE attribute ignored", name);
1718 *no_add_attrs = true;
1720 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1721 || (TREE_CODE (decl) != FUNCTION_DECL
1722 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
1723 /* A static variable declaration is always a tentative definition,
1724 but the alias is a non-tentative definition which overrides. */
1725 || (TREE_CODE (decl) != FUNCTION_DECL
1726 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
1728 error ("%q+D defined both normally and as %qE attribute", decl, name);
1729 *no_add_attrs = true;
1730 return NULL_TREE;
1732 else if (!is_alias
1733 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
1734 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
1736 error ("weak %q+D cannot be defined %qE", decl, name);
1737 *no_add_attrs = true;
1738 return NULL_TREE;
1741 /* Note that the very first time we process a nested declaration,
1742 decl_function_context will not be set. Indeed, *would* never
1743 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
1744 we do below. After such frobbery, pushdecl would set the context.
1745 In any case, this is never what we want. */
1746 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
1748 tree id;
1750 id = TREE_VALUE (args);
1751 if (TREE_CODE (id) != STRING_CST)
1753 error ("attribute %qE argument not a string", name);
1754 *no_add_attrs = true;
1755 return NULL_TREE;
1757 id = get_identifier (TREE_STRING_POINTER (id));
1758 /* This counts as a use of the object pointed to. */
1759 TREE_USED (id) = 1;
1761 if (TREE_CODE (decl) == FUNCTION_DECL)
1762 DECL_INITIAL (decl) = error_mark_node;
1763 else
1764 TREE_STATIC (decl) = 1;
1766 if (!is_alias)
1767 /* ifuncs are also aliases, so set that attribute too. */
1768 DECL_ATTRIBUTES (decl)
1769 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
1771 else
1773 warning (OPT_Wattributes, "%qE attribute ignored", name);
1774 *no_add_attrs = true;
1777 if (decl_in_symtab_p (*node))
1779 struct symtab_node *n = symtab_node::get (decl);
1780 if (n && n->refuse_visibility_changes)
1782 if (is_alias)
1783 error ("%+D declared alias after being used", decl);
1784 else
1785 error ("%+D declared ifunc after being used", decl);
1790 return NULL_TREE;
1793 /* Handle an "alias" or "ifunc" attribute; arguments as in
1794 struct attribute_spec.handler. */
1796 static tree
1797 handle_ifunc_attribute (tree *node, tree name, tree args,
1798 int ARG_UNUSED (flags), bool *no_add_attrs)
1800 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
1803 /* Handle an "alias" or "ifunc" attribute; arguments as in
1804 struct attribute_spec.handler. */
1806 static tree
1807 handle_alias_attribute (tree *node, tree name, tree args,
1808 int ARG_UNUSED (flags), bool *no_add_attrs)
1810 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
1813 /* Handle a "weakref" attribute; arguments as in struct
1814 attribute_spec.handler. */
1816 static tree
1817 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1818 int flags, bool *no_add_attrs)
1820 tree attr = NULL_TREE;
1822 /* We must ignore the attribute when it is associated with
1823 local-scoped decls, since attribute alias is ignored and many
1824 such symbols do not even have a DECL_WEAK field. */
1825 if (decl_function_context (*node)
1826 || current_function_decl
1827 || !VAR_OR_FUNCTION_DECL_P (*node))
1829 warning (OPT_Wattributes, "%qE attribute ignored", name);
1830 *no_add_attrs = true;
1831 return NULL_TREE;
1834 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1836 error ("indirect function %q+D cannot be declared weakref", *node);
1837 *no_add_attrs = true;
1838 return NULL_TREE;
1841 /* The idea here is that `weakref("name")' mutates into `weakref,
1842 alias("name")', and weakref without arguments, in turn,
1843 implicitly adds weak. */
1845 if (args)
1847 attr = tree_cons (get_identifier ("alias"), args, attr);
1848 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
1850 *no_add_attrs = true;
1852 decl_attributes (node, attr, flags);
1854 else
1856 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
1857 error_at (DECL_SOURCE_LOCATION (*node),
1858 "weakref attribute must appear before alias attribute");
1860 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
1861 and that isn't supported; and because it wants to add it to
1862 the list of weak decls, which isn't helpful. */
1863 DECL_WEAK (*node) = 1;
1866 if (decl_in_symtab_p (*node))
1868 struct symtab_node *n = symtab_node::get (*node);
1869 if (n && n->refuse_visibility_changes)
1870 error ("%+D declared weakref after being used", *node);
1873 return NULL_TREE;
1876 /* Handle an "visibility" attribute; arguments as in
1877 struct attribute_spec.handler. */
1879 static tree
1880 handle_visibility_attribute (tree *node, tree name, tree args,
1881 int ARG_UNUSED (flags),
1882 bool *ARG_UNUSED (no_add_attrs))
1884 tree decl = *node;
1885 tree id = TREE_VALUE (args);
1886 enum symbol_visibility vis;
1888 if (TYPE_P (*node))
1890 if (TREE_CODE (*node) == ENUMERAL_TYPE)
1891 /* OK */;
1892 else if (!RECORD_OR_UNION_TYPE_P (*node))
1894 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
1895 name);
1896 return NULL_TREE;
1898 else if (TYPE_FIELDS (*node))
1900 error ("%qE attribute ignored because %qT is already defined",
1901 name, *node);
1902 return NULL_TREE;
1905 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
1907 warning (OPT_Wattributes, "%qE attribute ignored", name);
1908 return NULL_TREE;
1911 if (TREE_CODE (id) != STRING_CST)
1913 error ("visibility argument not a string");
1914 return NULL_TREE;
1917 /* If this is a type, set the visibility on the type decl. */
1918 if (TYPE_P (decl))
1920 decl = TYPE_NAME (decl);
1921 if (!decl)
1922 return NULL_TREE;
1923 if (TREE_CODE (decl) == IDENTIFIER_NODE)
1925 warning (OPT_Wattributes, "%qE attribute ignored on types",
1926 name);
1927 return NULL_TREE;
1931 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
1932 vis = VISIBILITY_DEFAULT;
1933 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
1934 vis = VISIBILITY_INTERNAL;
1935 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
1936 vis = VISIBILITY_HIDDEN;
1937 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
1938 vis = VISIBILITY_PROTECTED;
1939 else
1941 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
1942 vis = VISIBILITY_DEFAULT;
1945 if (DECL_VISIBILITY_SPECIFIED (decl)
1946 && vis != DECL_VISIBILITY (decl))
1948 tree attributes = (TYPE_P (*node)
1949 ? TYPE_ATTRIBUTES (*node)
1950 : DECL_ATTRIBUTES (decl));
1951 if (lookup_attribute ("visibility", attributes))
1952 error ("%qD redeclared with different visibility", decl);
1953 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1954 && lookup_attribute ("dllimport", attributes))
1955 error ("%qD was declared %qs which implies default visibility",
1956 decl, "dllimport");
1957 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1958 && lookup_attribute ("dllexport", attributes))
1959 error ("%qD was declared %qs which implies default visibility",
1960 decl, "dllexport");
1963 DECL_VISIBILITY (decl) = vis;
1964 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1966 /* Go ahead and attach the attribute to the node as well. This is needed
1967 so we can determine whether we have VISIBILITY_DEFAULT because the
1968 visibility was not specified, or because it was explicitly overridden
1969 from the containing scope. */
1971 return NULL_TREE;
1974 /* Handle an "tls_model" attribute; arguments as in
1975 struct attribute_spec.handler. */
1977 static tree
1978 handle_tls_model_attribute (tree *node, tree name, tree args,
1979 int ARG_UNUSED (flags), bool *no_add_attrs)
1981 tree id;
1982 tree decl = *node;
1983 enum tls_model kind;
1985 *no_add_attrs = true;
1987 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
1989 warning (OPT_Wattributes, "%qE attribute ignored", name);
1990 return NULL_TREE;
1993 kind = DECL_TLS_MODEL (decl);
1994 id = TREE_VALUE (args);
1995 if (TREE_CODE (id) != STRING_CST)
1997 error ("tls_model argument not a string");
1998 return NULL_TREE;
2001 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2002 kind = TLS_MODEL_LOCAL_EXEC;
2003 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2004 kind = TLS_MODEL_INITIAL_EXEC;
2005 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2006 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2007 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2008 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2009 else
2010 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2012 set_decl_tls_model (decl, kind);
2013 return NULL_TREE;
2016 /* Handle a "no_instrument_function" attribute; arguments as in
2017 struct attribute_spec.handler. */
2019 static tree
2020 handle_no_instrument_function_attribute (tree *node, tree name,
2021 tree ARG_UNUSED (args),
2022 int ARG_UNUSED (flags),
2023 bool *no_add_attrs)
2025 tree decl = *node;
2027 if (TREE_CODE (decl) != FUNCTION_DECL)
2029 error_at (DECL_SOURCE_LOCATION (decl),
2030 "%qE attribute applies only to functions", name);
2031 *no_add_attrs = true;
2033 else
2034 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2036 return NULL_TREE;
2039 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2040 struct attribute_spec.handler. */
2042 static tree
2043 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2044 int, bool *no_add_attrs)
2046 if (TREE_CODE (*node) != FUNCTION_DECL)
2048 warning (OPT_Wattributes, "%qE attribute ignored", name);
2049 *no_add_attrs = true;
2052 return NULL_TREE;
2055 /* Handle a "malloc" attribute; arguments as in
2056 struct attribute_spec.handler. */
2058 static tree
2059 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2060 int ARG_UNUSED (flags), bool *no_add_attrs)
2062 if (TREE_CODE (*node) == FUNCTION_DECL
2063 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2064 DECL_IS_MALLOC (*node) = 1;
2065 else
2067 warning (OPT_Wattributes, "%qE attribute ignored", name);
2068 *no_add_attrs = true;
2071 return NULL_TREE;
2074 /* Handle a "alloc_size" attribute; arguments as in
2075 struct attribute_spec.handler. */
2077 static tree
2078 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2079 int ARG_UNUSED (flags), bool *no_add_attrs)
2081 unsigned arg_count = type_num_arguments (*node);
2082 for (; args; args = TREE_CHAIN (args))
2084 tree position = TREE_VALUE (args);
2085 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2086 && TREE_CODE (position) != FUNCTION_DECL)
2087 position = default_conversion (position);
2089 if (!tree_fits_uhwi_p (position)
2090 || !arg_count
2091 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2093 warning (OPT_Wattributes,
2094 "alloc_size parameter outside range");
2095 *no_add_attrs = true;
2096 return NULL_TREE;
2099 return NULL_TREE;
2102 /* Handle a "alloc_align" attribute; arguments as in
2103 struct attribute_spec.handler. */
2105 static tree
2106 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2107 bool *no_add_attrs)
2109 unsigned arg_count = type_num_arguments (*node);
2110 tree position = TREE_VALUE (args);
2111 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2112 && TREE_CODE (position) != FUNCTION_DECL)
2113 position = default_conversion (position);
2115 if (!tree_fits_uhwi_p (position)
2116 || !arg_count
2117 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2119 warning (OPT_Wattributes,
2120 "alloc_align parameter outside range");
2121 *no_add_attrs = true;
2122 return NULL_TREE;
2124 return NULL_TREE;
2127 /* Handle a "assume_aligned" attribute; arguments as in
2128 struct attribute_spec.handler. */
2130 static tree
2131 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2132 bool *no_add_attrs)
2134 for (; args; args = TREE_CHAIN (args))
2136 tree position = TREE_VALUE (args);
2137 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2138 && TREE_CODE (position) != FUNCTION_DECL)
2139 position = default_conversion (position);
2141 if (TREE_CODE (position) != INTEGER_CST)
2143 warning (OPT_Wattributes,
2144 "assume_aligned parameter not integer constant");
2145 *no_add_attrs = true;
2146 return NULL_TREE;
2149 return NULL_TREE;
2152 /* Handle a "fn spec" attribute; arguments as in
2153 struct attribute_spec.handler. */
2155 static tree
2156 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2157 tree args, int ARG_UNUSED (flags),
2158 bool *no_add_attrs ATTRIBUTE_UNUSED)
2160 gcc_assert (args
2161 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2162 && !TREE_CHAIN (args));
2163 return NULL_TREE;
2166 /* Handle a "bnd_variable_size" attribute; arguments as in
2167 struct attribute_spec.handler. */
2169 static tree
2170 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2171 int ARG_UNUSED (flags), bool *no_add_attrs)
2173 if (TREE_CODE (*node) != FIELD_DECL)
2175 warning (OPT_Wattributes, "%qE attribute ignored", name);
2176 *no_add_attrs = true;
2179 return NULL_TREE;
2182 /* Handle a "bnd_legacy" attribute; arguments as in
2183 struct attribute_spec.handler. */
2185 static tree
2186 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
2187 int ARG_UNUSED (flags), bool *no_add_attrs)
2189 if (TREE_CODE (*node) != FUNCTION_DECL)
2191 warning (OPT_Wattributes, "%qE attribute ignored", name);
2192 *no_add_attrs = true;
2195 return NULL_TREE;
2198 /* Handle a "bnd_instrument" attribute; arguments as in
2199 struct attribute_spec.handler. */
2201 static tree
2202 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
2203 int ARG_UNUSED (flags), bool *no_add_attrs)
2205 if (TREE_CODE (*node) != FUNCTION_DECL)
2207 warning (OPT_Wattributes, "%qE attribute ignored", name);
2208 *no_add_attrs = true;
2211 return NULL_TREE;
2214 /* Handle a "warn_unused" attribute; arguments as in
2215 struct attribute_spec.handler. */
2217 static tree
2218 handle_warn_unused_attribute (tree *node, tree name,
2219 tree args ATTRIBUTE_UNUSED,
2220 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2222 if (TYPE_P (*node))
2223 /* Do nothing else, just set the attribute. We'll get at
2224 it later with lookup_attribute. */
2226 else
2228 warning (OPT_Wattributes, "%qE attribute ignored", name);
2229 *no_add_attrs = true;
2232 return NULL_TREE;
2235 /* Handle an "omp declare simd" attribute; arguments as in
2236 struct attribute_spec.handler. */
2238 static tree
2239 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2241 return NULL_TREE;
2244 /* Handle a "simd" attribute. */
2246 static tree
2247 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2249 if (TREE_CODE (*node) == FUNCTION_DECL)
2251 if (lookup_attribute ("cilk simd function",
2252 DECL_ATTRIBUTES (*node)) != NULL)
2254 error_at (DECL_SOURCE_LOCATION (*node),
2255 "%<__simd__%> attribute cannot be used in the same "
2256 "function marked as a Cilk Plus SIMD-enabled function");
2257 *no_add_attrs = true;
2259 else
2261 tree t = get_identifier ("omp declare simd");
2262 tree attr = NULL_TREE;
2263 if (args)
2265 tree id = TREE_VALUE (args);
2267 if (TREE_CODE (id) != STRING_CST)
2269 error ("attribute %qE argument not a string", name);
2270 *no_add_attrs = true;
2271 return NULL_TREE;
2274 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2275 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2276 OMP_CLAUSE_NOTINBRANCH);
2277 else
2278 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2279 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2280 OMP_CLAUSE_INBRANCH);
2281 else
2283 error ("only %<inbranch%> and %<notinbranch%> flags are "
2284 "allowed for %<__simd__%> attribute");
2285 *no_add_attrs = true;
2286 return NULL_TREE;
2290 DECL_ATTRIBUTES (*node) = tree_cons (t,
2291 build_tree_list (NULL_TREE,
2292 attr),
2293 DECL_ATTRIBUTES (*node));
2296 else
2298 warning (OPT_Wattributes, "%qE attribute ignored", name);
2299 *no_add_attrs = true;
2302 return NULL_TREE;
2305 /* Handle an "omp declare target" attribute; arguments as in
2306 struct attribute_spec.handler. */
2308 static tree
2309 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2311 return NULL_TREE;
2314 /* Handle a "returns_twice" attribute; arguments as in
2315 struct attribute_spec.handler. */
2317 static tree
2318 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2319 int ARG_UNUSED (flags), bool *no_add_attrs)
2321 if (TREE_CODE (*node) == FUNCTION_DECL)
2322 DECL_IS_RETURNS_TWICE (*node) = 1;
2323 else
2325 warning (OPT_Wattributes, "%qE attribute ignored", name);
2326 *no_add_attrs = true;
2329 return NULL_TREE;
2332 /* Handle a "no_limit_stack" attribute; arguments as in
2333 struct attribute_spec.handler. */
2335 static tree
2336 handle_no_limit_stack_attribute (tree *node, tree name,
2337 tree ARG_UNUSED (args),
2338 int ARG_UNUSED (flags),
2339 bool *no_add_attrs)
2341 tree decl = *node;
2343 if (TREE_CODE (decl) != FUNCTION_DECL)
2345 error_at (DECL_SOURCE_LOCATION (decl),
2346 "%qE attribute applies only to functions", name);
2347 *no_add_attrs = true;
2349 else if (DECL_INITIAL (decl))
2351 error_at (DECL_SOURCE_LOCATION (decl),
2352 "can%'t set %qE attribute after definition", name);
2353 *no_add_attrs = true;
2355 else
2356 DECL_NO_LIMIT_STACK (decl) = 1;
2358 return NULL_TREE;
2361 /* Handle a "pure" attribute; arguments as in
2362 struct attribute_spec.handler. */
2364 static tree
2365 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2366 int ARG_UNUSED (flags), bool *no_add_attrs)
2368 if (TREE_CODE (*node) == FUNCTION_DECL)
2369 DECL_PURE_P (*node) = 1;
2370 /* ??? TODO: Support types. */
2371 else
2373 warning (OPT_Wattributes, "%qE attribute ignored", name);
2374 *no_add_attrs = true;
2377 return NULL_TREE;
2380 /* Digest an attribute list destined for a transactional memory statement.
2381 ALLOWED is the set of attributes that are allowed for this statement;
2382 return the attribute we parsed. Multiple attributes are never allowed. */
2385 parse_tm_stmt_attr (tree attrs, int allowed)
2387 tree a_seen = NULL;
2388 int m_seen = 0;
2390 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2392 tree a = TREE_PURPOSE (attrs);
2393 int m = 0;
2395 if (is_attribute_p ("outer", a))
2396 m = TM_STMT_ATTR_OUTER;
2398 if ((m & allowed) == 0)
2400 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2401 continue;
2404 if (m_seen == 0)
2406 a_seen = a;
2407 m_seen = m;
2409 else if (m_seen == m)
2410 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2411 else
2412 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2415 return m_seen;
2418 /* Transform a TM attribute name into a maskable integer and back.
2419 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2420 to how the lack of an attribute is treated. */
2423 tm_attr_to_mask (tree attr)
2425 if (attr == NULL)
2426 return 0;
2427 if (is_attribute_p ("transaction_safe", attr))
2428 return TM_ATTR_SAFE;
2429 if (is_attribute_p ("transaction_callable", attr))
2430 return TM_ATTR_CALLABLE;
2431 if (is_attribute_p ("transaction_pure", attr))
2432 return TM_ATTR_PURE;
2433 if (is_attribute_p ("transaction_unsafe", attr))
2434 return TM_ATTR_IRREVOCABLE;
2435 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2436 return TM_ATTR_MAY_CANCEL_OUTER;
2437 return 0;
2440 tree
2441 tm_mask_to_attr (int mask)
2443 const char *str;
2444 switch (mask)
2446 case TM_ATTR_SAFE:
2447 str = "transaction_safe";
2448 break;
2449 case TM_ATTR_CALLABLE:
2450 str = "transaction_callable";
2451 break;
2452 case TM_ATTR_PURE:
2453 str = "transaction_pure";
2454 break;
2455 case TM_ATTR_IRREVOCABLE:
2456 str = "transaction_unsafe";
2457 break;
2458 case TM_ATTR_MAY_CANCEL_OUTER:
2459 str = "transaction_may_cancel_outer";
2460 break;
2461 default:
2462 gcc_unreachable ();
2464 return get_identifier (str);
2467 /* Return the first TM attribute seen in LIST. */
2469 tree
2470 find_tm_attribute (tree list)
2472 for (; list ; list = TREE_CHAIN (list))
2474 tree name = TREE_PURPOSE (list);
2475 if (tm_attr_to_mask (name) != 0)
2476 return name;
2478 return NULL_TREE;
2481 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2482 Here we accept only function types, and verify that none of the other
2483 function TM attributes are also applied. */
2484 /* ??? We need to accept class types for C++, but not C. This greatly
2485 complicates this function, since we can no longer rely on the extra
2486 processing given by function_type_required. */
2488 static tree
2489 handle_tm_attribute (tree *node, tree name, tree args,
2490 int flags, bool *no_add_attrs)
2492 /* Only one path adds the attribute; others don't. */
2493 *no_add_attrs = true;
2495 switch (TREE_CODE (*node))
2497 case RECORD_TYPE:
2498 case UNION_TYPE:
2499 /* Only tm_callable and tm_safe apply to classes. */
2500 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2501 goto ignored;
2502 /* FALLTHRU */
2504 case FUNCTION_TYPE:
2505 case METHOD_TYPE:
2507 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2508 if (old_name == name)
2510 else if (old_name != NULL_TREE)
2511 error ("type was previously declared %qE", old_name);
2512 else
2513 *no_add_attrs = false;
2515 break;
2517 case FUNCTION_DECL:
2519 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2520 want to set transaction_safe on the type. */
2521 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2522 if (!TYPE_P (DECL_CONTEXT (*node)))
2523 error_at (DECL_SOURCE_LOCATION (*node),
2524 "%<transaction_safe_dynamic%> may only be specified for "
2525 "a virtual function");
2526 *no_add_attrs = false;
2527 decl_attributes (&TREE_TYPE (*node),
2528 build_tree_list (get_identifier ("transaction_safe"),
2529 NULL_TREE),
2531 break;
2534 case POINTER_TYPE:
2536 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2537 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2539 tree fn_tmp = TREE_TYPE (*node);
2540 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2541 *node = build_pointer_type (fn_tmp);
2542 break;
2545 /* FALLTHRU */
2547 default:
2548 /* If a function is next, pass it on to be tried next. */
2549 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2550 return tree_cons (name, args, NULL);
2552 ignored:
2553 warning (OPT_Wattributes, "%qE attribute ignored", name);
2554 break;
2557 return NULL_TREE;
2560 /* Handle the TM_WRAP attribute; arguments as in
2561 struct attribute_spec.handler. */
2563 static tree
2564 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2565 int ARG_UNUSED (flags), bool *no_add_attrs)
2567 tree decl = *node;
2569 /* We don't need the attribute even on success, since we
2570 record the entry in an external table. */
2571 *no_add_attrs = true;
2573 if (TREE_CODE (decl) != FUNCTION_DECL)
2574 warning (OPT_Wattributes, "%qE attribute ignored", name);
2575 else
2577 tree wrap_decl = TREE_VALUE (args);
2578 if (error_operand_p (wrap_decl))
2580 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2581 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2582 error ("%qE argument not an identifier", name);
2583 else
2585 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2586 wrap_decl = lookup_name (wrap_decl);
2587 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2589 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2590 TREE_TYPE (wrap_decl)))
2591 record_tm_replacement (wrap_decl, decl);
2592 else
2593 error ("%qD is not compatible with %qD", wrap_decl, decl);
2595 else
2596 error ("%qE argument is not a function", name);
2600 return NULL_TREE;
2603 /* Ignore the given attribute. Used when this attribute may be usefully
2604 overridden by the target, but is not used generically. */
2606 static tree
2607 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2608 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2609 bool *no_add_attrs)
2611 *no_add_attrs = true;
2612 return NULL_TREE;
2615 /* Handle a "no vops" attribute; arguments as in
2616 struct attribute_spec.handler. */
2618 static tree
2619 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2620 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2621 bool *ARG_UNUSED (no_add_attrs))
2623 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2624 DECL_IS_NOVOPS (*node) = 1;
2625 return NULL_TREE;
2628 /* Handle a "deprecated" attribute; arguments as in
2629 struct attribute_spec.handler. */
2631 static tree
2632 handle_deprecated_attribute (tree *node, tree name,
2633 tree args, int flags,
2634 bool *no_add_attrs)
2636 tree type = NULL_TREE;
2637 int warn = 0;
2638 tree what = NULL_TREE;
2640 if (!args)
2641 *no_add_attrs = true;
2642 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2644 error ("deprecated message is not a string");
2645 *no_add_attrs = true;
2648 if (DECL_P (*node))
2650 tree decl = *node;
2651 type = TREE_TYPE (decl);
2653 if (TREE_CODE (decl) == TYPE_DECL
2654 || TREE_CODE (decl) == PARM_DECL
2655 || VAR_OR_FUNCTION_DECL_P (decl)
2656 || TREE_CODE (decl) == FIELD_DECL
2657 || TREE_CODE (decl) == CONST_DECL
2658 || objc_method_decl (TREE_CODE (decl)))
2659 TREE_DEPRECATED (decl) = 1;
2660 else
2661 warn = 1;
2663 else if (TYPE_P (*node))
2665 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2666 *node = build_variant_type_copy (*node);
2667 TREE_DEPRECATED (*node) = 1;
2668 type = *node;
2670 else
2671 warn = 1;
2673 if (warn)
2675 *no_add_attrs = true;
2676 if (type && TYPE_NAME (type))
2678 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2679 what = TYPE_NAME (*node);
2680 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2681 && DECL_NAME (TYPE_NAME (type)))
2682 what = DECL_NAME (TYPE_NAME (type));
2684 if (what)
2685 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
2686 else
2687 warning (OPT_Wattributes, "%qE attribute ignored", name);
2690 return NULL_TREE;
2693 /* Handle a "vector_size" attribute; arguments as in
2694 struct attribute_spec.handler. */
2696 static tree
2697 handle_vector_size_attribute (tree *node, tree name, tree args,
2698 int ARG_UNUSED (flags),
2699 bool *no_add_attrs)
2701 unsigned HOST_WIDE_INT vecsize, nunits;
2702 machine_mode orig_mode;
2703 tree type = *node, new_type, size;
2705 *no_add_attrs = true;
2707 size = TREE_VALUE (args);
2708 if (size && TREE_CODE (size) != IDENTIFIER_NODE
2709 && TREE_CODE (size) != FUNCTION_DECL)
2710 size = default_conversion (size);
2712 if (!tree_fits_uhwi_p (size))
2714 warning (OPT_Wattributes, "%qE attribute ignored", name);
2715 return NULL_TREE;
2718 /* Get the vector size (in bytes). */
2719 vecsize = tree_to_uhwi (size);
2721 /* We need to provide for vector pointers, vector arrays, and
2722 functions returning vectors. For example:
2724 __attribute__((vector_size(16))) short *foo;
2726 In this case, the mode is SI, but the type being modified is
2727 HI, so we need to look further. */
2729 while (POINTER_TYPE_P (type)
2730 || TREE_CODE (type) == FUNCTION_TYPE
2731 || TREE_CODE (type) == METHOD_TYPE
2732 || TREE_CODE (type) == ARRAY_TYPE
2733 || TREE_CODE (type) == OFFSET_TYPE)
2734 type = TREE_TYPE (type);
2736 /* Get the mode of the type being modified. */
2737 orig_mode = TYPE_MODE (type);
2739 if ((!INTEGRAL_TYPE_P (type)
2740 && !SCALAR_FLOAT_TYPE_P (type)
2741 && !FIXED_POINT_TYPE_P (type))
2742 || (!SCALAR_FLOAT_MODE_P (orig_mode)
2743 && GET_MODE_CLASS (orig_mode) != MODE_INT
2744 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
2745 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
2746 || TREE_CODE (type) == BOOLEAN_TYPE)
2748 error ("invalid vector type for attribute %qE", name);
2749 return NULL_TREE;
2752 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2754 error ("vector size not an integral multiple of component size");
2755 return NULL;
2758 if (vecsize == 0)
2760 error ("zero vector size");
2761 return NULL;
2764 /* Calculate how many units fit in the vector. */
2765 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
2766 if (nunits & (nunits - 1))
2768 error ("number of components of the vector not a power of two");
2769 return NULL_TREE;
2772 new_type = build_vector_type (type, nunits);
2774 /* Build back pointers if needed. */
2775 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
2777 return NULL_TREE;
2780 /* Handle the "nonnull" attribute. */
2782 static tree
2783 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
2784 tree args, int ARG_UNUSED (flags),
2785 bool *no_add_attrs)
2787 tree type = *node;
2788 unsigned HOST_WIDE_INT attr_arg_num;
2790 /* If no arguments are specified, all pointer arguments should be
2791 non-null. Verify a full prototype is given so that the arguments
2792 will have the correct types when we actually check them later.
2793 Avoid diagnosing type-generic built-ins since those have no
2794 prototype. */
2795 if (!args)
2797 if (!prototype_p (type)
2798 && (!TYPE_ATTRIBUTES (type)
2799 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
2801 error ("nonnull attribute without arguments on a non-prototype");
2802 *no_add_attrs = true;
2804 return NULL_TREE;
2807 /* Argument list specified. Verify that each argument number references
2808 a pointer argument. */
2809 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
2811 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
2813 tree arg = TREE_VALUE (args);
2814 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
2815 && TREE_CODE (arg) != FUNCTION_DECL)
2816 TREE_VALUE (args) = arg = default_conversion (arg);
2818 if (!get_nonnull_operand (arg, &arg_num))
2820 error ("nonnull argument has invalid operand number (argument %lu)",
2821 (unsigned long) attr_arg_num);
2822 *no_add_attrs = true;
2823 return NULL_TREE;
2826 if (prototype_p (type))
2828 function_args_iterator iter;
2829 tree argument;
2831 function_args_iter_init (&iter, type);
2832 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
2834 argument = function_args_iter_cond (&iter);
2835 if (argument == NULL_TREE || ck_num == arg_num)
2836 break;
2839 if (!argument
2840 || TREE_CODE (argument) == VOID_TYPE)
2842 error ("nonnull argument with out-of-range operand number "
2843 "(argument %lu, operand %lu)",
2844 (unsigned long) attr_arg_num, (unsigned long) arg_num);
2845 *no_add_attrs = true;
2846 return NULL_TREE;
2849 if (TREE_CODE (argument) != POINTER_TYPE)
2851 error ("nonnull argument references non-pointer operand "
2852 "(argument %lu, operand %lu)",
2853 (unsigned long) attr_arg_num, (unsigned long) arg_num);
2854 *no_add_attrs = true;
2855 return NULL_TREE;
2860 return NULL_TREE;
2863 /* Handle a "nothrow" attribute; arguments as in
2864 struct attribute_spec.handler. */
2866 static tree
2867 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2868 int ARG_UNUSED (flags), bool *no_add_attrs)
2870 if (TREE_CODE (*node) == FUNCTION_DECL)
2871 TREE_NOTHROW (*node) = 1;
2872 /* ??? TODO: Support types. */
2873 else
2875 warning (OPT_Wattributes, "%qE attribute ignored", name);
2876 *no_add_attrs = true;
2879 return NULL_TREE;
2882 /* Handle a "cleanup" attribute; arguments as in
2883 struct attribute_spec.handler. */
2885 static tree
2886 handle_cleanup_attribute (tree *node, tree name, tree args,
2887 int ARG_UNUSED (flags), bool *no_add_attrs)
2889 tree decl = *node;
2890 tree cleanup_id, cleanup_decl;
2892 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
2893 for global destructors in C++. This requires infrastructure that
2894 we don't have generically at the moment. It's also not a feature
2895 we'd be missing too much, since we do have attribute constructor. */
2896 if (!VAR_P (decl) || TREE_STATIC (decl))
2898 warning (OPT_Wattributes, "%qE attribute ignored", name);
2899 *no_add_attrs = true;
2900 return NULL_TREE;
2903 /* Verify that the argument is a function in scope. */
2904 /* ??? We could support pointers to functions here as well, if
2905 that was considered desirable. */
2906 cleanup_id = TREE_VALUE (args);
2907 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
2909 error ("cleanup argument not an identifier");
2910 *no_add_attrs = true;
2911 return NULL_TREE;
2913 cleanup_decl = lookup_name (cleanup_id);
2914 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
2916 error ("cleanup argument not a function");
2917 *no_add_attrs = true;
2918 return NULL_TREE;
2921 /* That the function has proper type is checked with the
2922 eventual call to build_function_call. */
2924 return NULL_TREE;
2927 /* Handle a "warn_unused_result" attribute. No special handling. */
2929 static tree
2930 handle_warn_unused_result_attribute (tree *node, tree name,
2931 tree ARG_UNUSED (args),
2932 int ARG_UNUSED (flags), bool *no_add_attrs)
2934 /* Ignore the attribute for functions not returning any value. */
2935 if (VOID_TYPE_P (TREE_TYPE (*node)))
2937 warning (OPT_Wattributes, "%qE attribute ignored", name);
2938 *no_add_attrs = true;
2941 return NULL_TREE;
2944 /* Handle a "sentinel" attribute. */
2946 static tree
2947 handle_sentinel_attribute (tree *node, tree name, tree args,
2948 int ARG_UNUSED (flags), bool *no_add_attrs)
2950 if (!prototype_p (*node))
2952 warning (OPT_Wattributes,
2953 "%qE attribute requires prototypes with named arguments", name);
2954 *no_add_attrs = true;
2956 else
2958 if (!stdarg_p (*node))
2960 warning (OPT_Wattributes,
2961 "%qE attribute only applies to variadic functions", name);
2962 *no_add_attrs = true;
2966 if (args)
2968 tree position = TREE_VALUE (args);
2969 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2970 && TREE_CODE (position) != FUNCTION_DECL)
2971 position = default_conversion (position);
2973 if (TREE_CODE (position) != INTEGER_CST
2974 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
2976 warning (OPT_Wattributes,
2977 "requested position is not an integer constant");
2978 *no_add_attrs = true;
2980 else
2982 if (tree_int_cst_lt (position, integer_zero_node))
2984 warning (OPT_Wattributes,
2985 "requested position is less than zero");
2986 *no_add_attrs = true;
2991 return NULL_TREE;
2994 /* Handle a "type_generic" attribute. */
2996 static tree
2997 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
2998 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2999 bool * ARG_UNUSED (no_add_attrs))
3001 /* Ensure we have a function type. */
3002 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3004 /* Ensure we have a variadic function. */
3005 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3007 return NULL_TREE;
3010 /* Handle a "target" attribute. */
3012 static tree
3013 handle_target_attribute (tree *node, tree name, tree args, int flags,
3014 bool *no_add_attrs)
3016 /* Ensure we have a function type. */
3017 if (TREE_CODE (*node) != FUNCTION_DECL)
3019 warning (OPT_Wattributes, "%qE attribute ignored", name);
3020 *no_add_attrs = true;
3022 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3024 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3025 "with %qs attribute", name, "target_clones");
3026 *no_add_attrs = true;
3028 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3029 flags))
3030 *no_add_attrs = true;
3032 return NULL_TREE;
3035 /* Handle a "target_clones" attribute. */
3037 static tree
3038 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3039 int ARG_UNUSED (flags), bool *no_add_attrs)
3041 /* Ensure we have a function type. */
3042 if (TREE_CODE (*node) == FUNCTION_DECL)
3044 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3046 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3047 "with %qs attribute", name, "always_inline");
3048 *no_add_attrs = true;
3050 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3052 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3053 "with %qs attribute", name, "target");
3054 *no_add_attrs = true;
3056 else
3057 /* Do not inline functions with multiple clone targets. */
3058 DECL_UNINLINABLE (*node) = 1;
3060 else
3062 warning (OPT_Wattributes, "%qE attribute ignored", name);
3063 *no_add_attrs = true;
3065 return NULL_TREE;
3068 /* For handling "optimize" attribute. arguments as in
3069 struct attribute_spec.handler. */
3071 static tree
3072 handle_optimize_attribute (tree *node, tree name, tree args,
3073 int ARG_UNUSED (flags), bool *no_add_attrs)
3075 /* Ensure we have a function type. */
3076 if (TREE_CODE (*node) != FUNCTION_DECL)
3078 warning (OPT_Wattributes, "%qE attribute ignored", name);
3079 *no_add_attrs = true;
3081 else
3083 struct cl_optimization cur_opts;
3084 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3086 /* Save current options. */
3087 cl_optimization_save (&cur_opts, &global_options);
3089 /* If we previously had some optimization options, use them as the
3090 default. */
3091 if (old_opts)
3092 cl_optimization_restore (&global_options,
3093 TREE_OPTIMIZATION (old_opts));
3095 /* Parse options, and update the vector. */
3096 parse_optimize_options (args, true);
3097 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3098 = build_optimization_node (&global_options);
3100 /* Restore current options. */
3101 cl_optimization_restore (&global_options, &cur_opts);
3104 return NULL_TREE;
3107 /* Handle a "no_split_stack" attribute. */
3109 static tree
3110 handle_no_split_stack_attribute (tree *node, tree name,
3111 tree ARG_UNUSED (args),
3112 int ARG_UNUSED (flags),
3113 bool *no_add_attrs)
3115 tree decl = *node;
3117 if (TREE_CODE (decl) != FUNCTION_DECL)
3119 error_at (DECL_SOURCE_LOCATION (decl),
3120 "%qE attribute applies only to functions", name);
3121 *no_add_attrs = true;
3123 else if (DECL_INITIAL (decl))
3125 error_at (DECL_SOURCE_LOCATION (decl),
3126 "can%'t set %qE attribute after definition", name);
3127 *no_add_attrs = true;
3130 return NULL_TREE;
3133 /* Handle a "returns_nonnull" attribute; arguments as in
3134 struct attribute_spec.handler. */
3136 static tree
3137 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3138 bool *no_add_attrs)
3140 // Even without a prototype we still have a return type we can check.
3141 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3143 error ("returns_nonnull attribute on a function not returning a pointer");
3144 *no_add_attrs = true;
3146 return NULL_TREE;
3149 /* Handle a "designated_init" attribute; arguments as in
3150 struct attribute_spec.handler. */
3152 static tree
3153 handle_designated_init_attribute (tree *node, tree name, tree, int,
3154 bool *no_add_attrs)
3156 if (TREE_CODE (*node) != RECORD_TYPE)
3158 error ("%qE attribute is only valid on %<struct%> type", name);
3159 *no_add_attrs = true;
3161 return NULL_TREE;
3165 /* Handle a "fallthrough" attribute; arguments as in struct
3166 attribute_spec.handler. */
3168 static tree
3169 handle_fallthrough_attribute (tree *, tree name, tree, int,
3170 bool *no_add_attrs)
3172 warning (OPT_Wattributes, "%qE attribute ignored", name);
3173 *no_add_attrs = true;
3174 return NULL_TREE;