Line data Source code
1 : /* Build expressions with type checking for C compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 :
21 : /* This file is part of the C front end.
22 : It contains routines to build C expressions given their operands,
23 : including computing the types of the result, C-specific error checks,
24 : and some optimization. */
25 :
26 : #include "config.h"
27 : #include "system.h"
28 : #include "coretypes.h"
29 : #include "memmodel.h"
30 : #include "target.h"
31 : #include "function.h"
32 : #include "bitmap.h"
33 : #include "c-tree.h"
34 : #include "gimple-expr.h"
35 : #include "predict.h"
36 : #include "stor-layout.h"
37 : #include "trans-mem.h"
38 : #include "varasm.h"
39 : #include "stmt.h"
40 : #include "langhooks.h"
41 : #include "c-lang.h"
42 : #include "intl.h"
43 : #include "tree-iterator.h"
44 : #include "gimplify.h"
45 : #include "tree-inline.h"
46 : #include "omp-general.h"
47 : #include "c-family/c-objc.h"
48 : #include "c-family/c-ubsan.h"
49 : #include "gomp-constants.h"
50 : #include "spellcheck-tree.h"
51 : #include "c-family/c-type-mismatch.h"
52 : #include "stringpool.h"
53 : #include "attribs.h"
54 : #include "asan.h"
55 : #include "realmpfr.h"
56 : #include "tree-pretty-print-markup.h"
57 : #include "gcc-urlifier.h"
58 :
59 : /* Possible cases of implicit conversions. Used to select diagnostic messages
60 : and control folding initializers in convert_for_assignment. */
61 : enum impl_conv {
62 : ic_argpass,
63 : ic_assign,
64 : ic_init,
65 : ic_init_const,
66 : ic_return
67 : };
68 :
69 : /* The level of nesting inside "__alignof__". */
70 : int in_alignof;
71 :
72 : /* The level of nesting inside "sizeof". */
73 : int in_sizeof;
74 :
75 : /* The level of nesting inside "countof". */
76 : int in_countof;
77 :
78 : /* The level of nesting inside "typeof". */
79 : int in_typeof;
80 :
81 : /* The level of nesting inside "_Generic". */
82 : int in_generic;
83 :
84 : /* True when parsing OpenMP loop expressions. */
85 : bool c_in_omp_for;
86 :
87 : /* True when parsing OpenMP map clause. */
88 : bool c_omp_array_section_p;
89 :
90 : /* The argument of last parsed sizeof expression, only to be tested
91 : if expr.original_code == SIZEOF_EXPR. */
92 : tree c_last_sizeof_arg;
93 : location_t c_last_sizeof_loc;
94 :
95 : /* Nonzero if we might need to print a "missing braces around
96 : initializer" message within this initializer. */
97 : static int found_missing_braces;
98 :
99 : static bool require_constant_value;
100 : static bool require_constant_elements;
101 : static bool require_constexpr_value;
102 :
103 : static tree qualify_type (tree, tree);
104 : struct comptypes_data;
105 : static bool tagged_types_tu_compatible_p (const_tree, const_tree,
106 : struct comptypes_data *);
107 : static bool comp_target_types (location_t, tree, tree);
108 : static bool function_types_compatible_p (const_tree, const_tree,
109 : struct comptypes_data *);
110 : static bool type_lists_compatible_p (const_tree, const_tree,
111 : struct comptypes_data *);
112 : static int convert_arguments (location_t, vec<location_t>, tree,
113 : vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 : tree);
115 : static tree pointer_diff (location_t, tree, tree, tree *);
116 : static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
117 : enum impl_conv, bool, tree, tree, int,
118 : int = 0);
119 : static tree valid_compound_expr_initializer (tree, tree);
120 : static void push_string (const char *);
121 : static void push_member_name (tree);
122 : static int spelling_length (void);
123 : static char *print_spelling (char *);
124 : static void warning_init (location_t, int, const char *);
125 : static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
126 : bool, bool, bool);
127 : static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
128 : bool, struct obstack *);
129 : static void output_pending_init_elements (int, struct obstack *);
130 : static bool set_designator (location_t, bool, struct obstack *);
131 : static void push_range_stack (tree, struct obstack *);
132 : static void add_pending_init (location_t, tree, tree, tree, bool,
133 : struct obstack *);
134 : static void set_nonincremental_init (struct obstack *);
135 : static void set_nonincremental_init_from_string (tree, struct obstack *);
136 : static tree find_init_member (tree, struct obstack *);
137 : static void readonly_warning (tree, enum lvalue_use);
138 : static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
139 : static void record_maybe_used_decl (tree, bool address);
140 : static bool comptypes_internal (const_tree, const_tree,
141 : struct comptypes_data *data);
142 : static bool comptypes_check_for_composite (tree t1, tree t2);
143 : static bool handle_counted_by_p (tree);
144 :
145 : /* Return true if EXP is a null pointer constant, false otherwise. */
146 :
147 : bool
148 188505874 : null_pointer_constant_p (const_tree expr)
149 : {
150 : /* This should really operate on c_expr structures, but they aren't
151 : yet available everywhere required. */
152 188505874 : tree type = TREE_TYPE (expr);
153 :
154 : /* An integer constant expression with the value 0, such an expression
155 : cast to type void*, or the predefined constant nullptr, are a null
156 : pointer constant. */
157 188505874 : if (expr == nullptr_node)
158 : return true;
159 :
160 188505191 : return (TREE_CODE (expr) == INTEGER_CST
161 19091692 : && !TREE_OVERFLOW (expr)
162 19091595 : && integer_zerop (expr)
163 192435560 : && (INTEGRAL_TYPE_P (type)
164 334150 : || (TREE_CODE (type) == POINTER_TYPE
165 334148 : && VOID_TYPE_P (TREE_TYPE (type))
166 258862 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
167 : }
168 :
169 : /* EXPR may appear in an unevaluated part of an integer constant
170 : expression, but not in an evaluated part. Wrap it in a
171 : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
172 : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
173 :
174 : static tree
175 62357 : note_integer_operands (tree expr)
176 : {
177 62357 : tree ret;
178 62357 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
179 : {
180 22 : ret = copy_node (expr);
181 22 : TREE_OVERFLOW (ret) = 1;
182 : }
183 : else
184 : {
185 62335 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
186 62335 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
187 : }
188 62357 : return ret;
189 : }
190 :
191 : /* Having checked whether EXPR may appear in an unevaluated part of an
192 : integer constant expression and found that it may, remove any
193 : C_MAYBE_CONST_EXPR noting this fact and return the resulting
194 : expression. */
195 :
196 : static inline tree
197 33729352 : remove_c_maybe_const_expr (tree expr)
198 : {
199 33729352 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
200 61507 : return C_MAYBE_CONST_EXPR_EXPR (expr);
201 : else
202 : return expr;
203 : }
204 :
205 : /* This is a cache to hold if two types are seen. */
206 :
207 : struct tagged_tu_seen_cache {
208 : const struct tagged_tu_seen_cache * next;
209 : const_tree t1;
210 : const_tree t2;
211 : };
212 :
213 : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
214 : does not have an incomplete type. (That includes void types.)
215 : LOC is the location of the use. */
216 :
217 : tree
218 772551936 : require_complete_type (location_t loc, tree value)
219 : {
220 772551936 : tree type = TREE_TYPE (value);
221 :
222 772551936 : if (error_operand_p (value))
223 8867 : return error_mark_node;
224 :
225 : /* First, detect a valid value with a complete type. */
226 772543069 : if (COMPLETE_TYPE_P (type))
227 : return value;
228 :
229 164 : c_incomplete_type_error (loc, value, type);
230 164 : return error_mark_node;
231 : }
232 :
233 : /* Print an error message for invalid use of an incomplete type.
234 : VALUE is the expression that was used (or 0 if that isn't known)
235 : and TYPE is the type that was invalid. LOC is the location for
236 : the error. */
237 :
238 : void
239 224 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
240 : {
241 : /* Avoid duplicate error message. */
242 224 : if (TREE_CODE (type) == ERROR_MARK)
243 : return;
244 :
245 224 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
246 98 : error_at (loc, "%qD has an incomplete type %qT", value, type);
247 : else
248 : {
249 126 : retry:
250 : /* We must print an error message. Be clever about what it says. */
251 :
252 126 : switch (TREE_CODE (type))
253 : {
254 82 : case RECORD_TYPE:
255 82 : case UNION_TYPE:
256 82 : case ENUMERAL_TYPE:
257 82 : break;
258 :
259 38 : case VOID_TYPE:
260 38 : error_at (loc, "invalid use of void expression");
261 38 : return;
262 :
263 6 : case ARRAY_TYPE:
264 6 : if (TYPE_DOMAIN (type))
265 : {
266 3 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
267 : {
268 3 : error_at (loc, "invalid use of flexible array member");
269 3 : return;
270 : }
271 0 : type = TREE_TYPE (type);
272 0 : goto retry;
273 : }
274 3 : error_at (loc, "invalid use of array with unspecified bounds");
275 3 : return;
276 :
277 0 : default:
278 0 : gcc_unreachable ();
279 : }
280 :
281 82 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
282 80 : error_at (loc, "invalid use of undefined type %qT", type);
283 : else
284 : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
285 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
286 : }
287 : }
288 :
289 : /* Given a type, apply default promotions wrt unnamed function
290 : arguments and return the new type. */
291 :
292 : tree
293 124461572 : c_type_promotes_to (tree type)
294 : {
295 124461572 : tree ret = NULL_TREE;
296 :
297 124461572 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
298 1504314 : ret = double_type_node;
299 122957258 : else if (c_promoting_integer_type_p (type))
300 : {
301 : /* Preserve unsignedness if not really getting any wider. */
302 18646670 : if (TYPE_UNSIGNED (type)
303 18646670 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
304 0 : ret = unsigned_type_node;
305 : else
306 18646670 : ret = integer_type_node;
307 : }
308 :
309 20150984 : if (ret != NULL_TREE)
310 20150984 : return (TYPE_ATOMIC (type)
311 20150984 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
312 : : ret);
313 :
314 : return type;
315 : }
316 :
317 : /* Return true if between two named address spaces, whether there is a superset
318 : named address space that encompasses both address spaces. If there is a
319 : superset, return which address space is the superset. */
320 :
321 : static bool
322 7355165 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
323 : {
324 7355165 : if (as1 == as2)
325 : {
326 7355165 : *common = as1;
327 7355165 : return true;
328 : }
329 0 : else if (targetm.addr_space.subset_p (as1, as2))
330 : {
331 0 : *common = as2;
332 0 : return true;
333 : }
334 0 : else if (targetm.addr_space.subset_p (as2, as1))
335 : {
336 0 : *common = as1;
337 0 : return true;
338 : }
339 : else
340 : return false;
341 : }
342 :
343 : /* Return a variant of TYPE which has all the type qualifiers of LIKE
344 : as well as those of TYPE. */
345 :
346 : static tree
347 2598408 : qualify_type (tree type, tree like)
348 : {
349 2598408 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
350 2598408 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
351 2598408 : addr_space_t as_common;
352 :
353 : /* If the two named address spaces are different, determine the common
354 : superset address space. If there isn't one, raise an error. */
355 2598408 : if (!addr_space_superset (as_type, as_like, &as_common))
356 : {
357 0 : as_common = as_type;
358 0 : error ("%qT and %qT are in disjoint named address spaces",
359 : type, like);
360 : }
361 :
362 5196816 : return c_build_qualified_type (type,
363 2598408 : TYPE_QUALS_NO_ADDR_SPACE (type)
364 2598408 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
365 2598408 : | ENCODE_QUAL_ADDR_SPACE (as_common));
366 : }
367 :
368 :
369 : /* Check consistency of type TYPE. For derived types, we test that
370 : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
371 : the requirements of the base type. We also check that arrays with a
372 : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
373 : inconsistency is detected false is returned and true otherwise. */
374 :
375 : static bool
376 140367808 : c_verify_type (tree type)
377 : {
378 140367808 : switch (TREE_CODE (type))
379 : {
380 67115155 : case POINTER_TYPE:
381 67115155 : case FUNCTION_TYPE:
382 67115155 : case ARRAY_TYPE:
383 : /* Pointer, array, functions are variably modified if and only if the
384 : target, element, return type is variably modified. */
385 67115155 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
386 67115155 : && (C_TYPE_VARIABLY_MODIFIED (type)
387 66932657 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
388 : return false;
389 : /* If the target type is structural equality, the type should also. */
390 67115155 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
391 67115155 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
392 : return false;
393 :
394 140367808 : default:
395 140367808 : break;
396 : }
397 :
398 140367808 : switch (TREE_CODE (type))
399 : {
400 66203856 : case POINTER_TYPE:
401 66203856 : case FUNCTION_TYPE:
402 : /* Pointer and functions can not have variable size. */
403 66203856 : if (C_TYPE_VARIABLE_SIZE (type))
404 0 : return false;
405 : break;
406 911299 : case ARRAY_TYPE:
407 : /* An array has variable size if and only if it has a non-constant
408 : dimensions or its element type has variable size. */
409 911299 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
410 911299 : || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
411 885607 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
412 : != INTEGER_CST))
413 911299 : != C_TYPE_VARIABLE_SIZE (type))
414 : return false;
415 : /* If the element type or the array has variable size, then the
416 : array has variable size and is variably modified. */
417 911299 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
418 911299 : || C_TYPE_VARIABLE_SIZE (type))
419 : {
420 28698 : if (!C_TYPE_VARIABLE_SIZE (type))
421 : return false;
422 28698 : if (!C_TYPE_VARIABLY_MODIFIED (type))
423 0 : return false;
424 : }
425 : default:
426 : break;
427 : }
428 : return true;
429 : }
430 :
431 : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
432 : from a base type to a newly built derived or qualified type. */
433 :
434 : static tree
435 133382302 : c_set_type_bits (tree new_type, tree old_type)
436 : {
437 133382302 : gcc_checking_assert (c_verify_type (old_type));
438 :
439 133382302 : if (c_type_variably_modified_p (old_type))
440 28316 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
441 :
442 133382302 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
443 : {
444 16804 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
445 16804 : C_TYPE_VARIABLE_SIZE (new_type) = true;
446 : }
447 133382302 : return new_type;
448 : }
449 :
450 : /* Build a pointer type using the default pointer mode. */
451 :
452 : tree
453 71482974 : c_build_pointer_type (tree to_type)
454 : {
455 71482974 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
456 71482974 : : TYPE_ADDR_SPACE (to_type);
457 71482974 : machine_mode pointer_mode;
458 :
459 71482974 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
460 71482974 : pointer_mode = targetm.addr_space.pointer_mode (as);
461 : else
462 : pointer_mode = c_default_pointer_mode;
463 :
464 71482974 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
465 : }
466 :
467 : /* Build a pointer type using the given mode. */
468 :
469 : tree
470 71731137 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
471 : {
472 71731137 : tree ret = build_pointer_type_for_mode (type, mode, m);
473 71731137 : return c_set_type_bits (ret, type);
474 : }
475 :
476 : /* Build a function type. */
477 :
478 : tree
479 53267132 : c_build_function_type (tree type, tree args, bool no)
480 : {
481 53267132 : tree ret = build_function_type (type, args, no);
482 53267132 : return c_set_type_bits (ret, type);
483 : }
484 :
485 : /* Build an array type. This sets typeless storage as required
486 : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
487 : based on the element type and domain. */
488 :
489 : tree
490 1171103 : c_build_array_type (tree type, tree domain)
491 : {
492 1171103 : int type_quals = TYPE_QUALS (type);
493 :
494 : /* Identify typeless storage as introduced in C2Y
495 : and supported also in earlier language modes. */
496 1748045 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
497 1171103 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
498 :
499 1171103 : tree ret = build_array_type (type, domain, typeless);
500 :
501 1085420 : if (domain && TYPE_MAX_VALUE (domain)
502 2167232 : && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
503 : {
504 24259 : C_TYPE_VARIABLE_SIZE (ret) = 1;
505 24259 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
506 : }
507 :
508 1171103 : return c_set_type_bits (ret, type);
509 : }
510 :
511 :
512 : /* Build an array type of unspecified size. */
513 : tree
514 150 : c_build_array_type_unspecified (tree type)
515 : {
516 150 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
517 : integer_zero_node, size_zero_node);
518 150 : return c_build_array_type (type, build_index_type (upper));
519 : }
520 :
521 :
522 : tree
523 7212930 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
524 : {
525 7212930 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
526 7212930 : return c_set_type_bits (ret, type);
527 : }
528 :
529 : tree
530 7023880 : c_build_type_attribute_variant (tree type, tree attrs)
531 : {
532 7023880 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
533 : }
534 :
535 : /* Reconstruct a complex derived type. This is used to re-construct types
536 : with the vector attribute. It is called via a langhook. */
537 :
538 : tree
539 839324 : c_reconstruct_complex_type (tree type, tree bottom)
540 : {
541 839324 : tree inner, outer;
542 :
543 839324 : if (TREE_CODE (type) == POINTER_TYPE)
544 : {
545 174826 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
546 174826 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
547 174826 : TYPE_REF_CAN_ALIAS_ALL (type));
548 : }
549 : else if (TREE_CODE (type) == ARRAY_TYPE)
550 : {
551 7273 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
552 7273 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
553 :
554 7273 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
555 : == C_TYPE_VARIABLE_SIZE (outer));
556 : }
557 : else if (TREE_CODE (type) == FUNCTION_TYPE)
558 : {
559 585 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
560 585 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
561 585 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
562 : }
563 : else if (TREE_CODE (type) == REFERENCE_TYPE
564 : || TREE_CODE (type) == OFFSET_TYPE)
565 0 : gcc_unreachable ();
566 : else
567 : return bottom;
568 :
569 182684 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
570 365368 : TYPE_QUALS (type));
571 : }
572 :
573 :
574 : /* Helper function for c_canonical_type. Check whether FIELD
575 : contains a pointer to a structure or union with tag,
576 : possibly nested in other type derivations. */
577 : static bool
578 3440675 : ptr_to_tagged_member (tree field)
579 : {
580 3440675 : gcc_assert (FIELD_DECL == TREE_CODE (field));
581 3440675 : tree type = TREE_TYPE (field);
582 3440675 : bool ptr_seen = false;
583 3440675 : while (TREE_CODE (type) == ARRAY_TYPE
584 : || TREE_CODE (type) == FUNCTION_TYPE
585 4727233 : || TREE_CODE (type) == POINTER_TYPE)
586 : {
587 1286558 : if (TREE_CODE (type) == POINTER_TYPE)
588 698691 : ptr_seen = true;
589 1286558 : type = TREE_TYPE (type);
590 : }
591 :
592 3440675 : if (ptr_seen
593 677642 : && RECORD_OR_UNION_TYPE_P (type)
594 3689396 : && NULL_TREE != c_type_tag (type))
595 : return true;
596 :
597 : return false;
598 : }
599 :
600 : /* For a record or union type, make necessary adaptations so that the
601 : type can be used as TYPE_CANONICAL.
602 :
603 : If the TYPE contains a pointer (possibly nested in other type
604 : derivations) to a structure or union as a member, create a copy
605 : and change such pointers to void pointers. Otherwise, the middle-end
606 : gets confused when recording component aliases in the case where we
607 : have formed equivalency classes that include types for which these
608 : member pointers end up pointing to other structure or unions types
609 : which have the same tag but are not compatible. */
610 : tree
611 883883 : c_type_canonical (tree type)
612 : {
613 883883 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
614 :
615 883883 : bool needs_mod = false;
616 3632078 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
617 : {
618 2829821 : if (!ptr_to_tagged_member (x))
619 2748195 : continue;
620 : needs_mod = true;
621 : break;
622 : }
623 883883 : if (!needs_mod)
624 : return type;
625 :
626 : /* Construct new version with such members replaced. */
627 81626 : tree n = build_distinct_type_copy (type);
628 81626 : tree *fields = &TYPE_FIELDS (n);
629 692480 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
630 : {
631 610854 : tree f = copy_node (x);
632 610854 : if (ptr_to_tagged_member (x))
633 161896 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
634 : ptr_type_node);
635 610854 : *fields = f;
636 610854 : fields = &DECL_CHAIN (f);
637 : }
638 81626 : TYPE_CANONICAL (n) = n;
639 81626 : return n;
640 : }
641 :
642 :
643 : /* If NTYPE is a type of a non-variadic function with a prototype
644 : and OTYPE is a type of a function without a prototype and ATTRS
645 : contains attribute format, diagnosess and removes it from ATTRS.
646 : Returns the result of build_type_attribute_variant of NTYPE and
647 : the (possibly) modified ATTRS. */
648 :
649 : static tree
650 9835 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
651 : {
652 9835 : if (!prototype_p (otype)
653 9823 : && prototype_p (ntype)
654 19628 : && lookup_attribute ("format", attrs))
655 : {
656 14 : warning_at (input_location, OPT_Wattributes,
657 : "%qs attribute can only be applied to variadic functions",
658 : "format");
659 14 : attrs = remove_attribute ("format", attrs);
660 : }
661 9835 : return c_build_type_attribute_variant (ntype, attrs);
662 :
663 : }
664 :
665 : /* Given a type which could be a typedef name, make sure to return the
666 : original type. See set_underlying_type. */
667 : static const_tree
668 62293967 : c_type_original (const_tree t)
669 : {
670 : /* It may even be a typedef of a typedef...
671 : In the case of compiler-created builtin structs the TYPE_DECL
672 : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
673 62293967 : while (TYPE_NAME (t)
674 32080713 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
675 62342279 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
676 24154 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
677 62293967 : return t;
678 : }
679 :
680 : /* Return the tag for a tagged type. */
681 : tree
682 62293967 : c_type_tag (const_tree t)
683 : {
684 62293967 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
685 62293967 : const_tree orig = c_type_original (t);
686 62293967 : tree name = TYPE_NAME (orig);
687 62293967 : if (!name)
688 : return NULL_TREE;
689 32056559 : if (TREE_CODE (name) == TYPE_DECL)
690 : {
691 4 : if (!DECL_NAME (name))
692 : return NULL_TREE;
693 2 : name = DECL_NAME (name);
694 : }
695 32056557 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
696 : return name;
697 : }
698 :
699 : /* Remove qualifiers but not atomic. For arrays remove qualifiers
700 : on the element type but also do not remove atomic. */
701 : static tree
702 62330977 : remove_qualifiers (tree t)
703 : {
704 62330977 : if (!t || t == error_mark_node)
705 : return t;
706 62330952 : return TYPE_ATOMIC (strip_array_types (t))
707 62330952 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
708 62315377 : : TYPE_MAIN_VARIANT (t);
709 : }
710 :
711 :
712 : /* Helper function for composite_type_internal. Find a compatible type
713 : in a (transparent) union U compatible to T. If found, return the
714 : type of the corresponding member. Otherwise, return the union type U. */
715 : static tree
716 8208912 : transparent_union_replacement (tree u, tree t)
717 : {
718 8208907 : if (u == error_mark_node || t == error_mark_node
719 8208902 : || TREE_CODE (u) != UNION_TYPE
720 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
721 8208976 : || comptypes (u, t))
722 8208848 : return u;
723 :
724 80 : for (tree memb = TYPE_FIELDS (u); memb; memb = DECL_CHAIN (memb))
725 : {
726 80 : tree m = remove_qualifiers (TREE_TYPE (memb));
727 80 : if (comptypes (m, t))
728 : {
729 64 : pedwarn (input_location, OPT_Wpedantic,
730 : "function types not truly compatible in ISO C");
731 64 : return m;
732 : }
733 : }
734 :
735 : return u;
736 : }
737 :
738 :
739 :
740 : /* Return the composite type of two compatible types.
741 :
742 : We assume that comptypes has already been done and returned
743 : nonzero; if that isn't so, this may crash. In particular, we
744 : assume that qualifiers match. */
745 :
746 : struct composite_cache {
747 : tree t1;
748 : tree t2;
749 : tree composite;
750 : struct composite_cache* next;
751 : };
752 :
753 : tree
754 13015960 : composite_type_internal (tree t1, tree t2, tree cond,
755 : struct composite_cache* cache)
756 : {
757 13015960 : enum tree_code code1;
758 13015960 : enum tree_code code2;
759 13015960 : tree attributes;
760 :
761 : /* Save time if the two types are the same. */
762 :
763 13015960 : if (t1 == t2) return t1;
764 :
765 : /* If one type is nonsense, use the other. */
766 2632174 : if (t1 == error_mark_node)
767 : return t2;
768 2632170 : if (t2 == error_mark_node)
769 : return t1;
770 :
771 2632168 : code1 = TREE_CODE (t1);
772 2632168 : code2 = TREE_CODE (t2);
773 :
774 : /* Merge the attributes. */
775 2632168 : attributes = targetm.merge_type_attributes (t1, t2);
776 :
777 : /* If one is an enumerated type and the other is the compatible
778 : integer type, the composite type might be either of the two
779 : (DR#013 question 3). For consistency, use the enumerated type as
780 : the composite type. */
781 :
782 2632168 : if (code1 == ENUMERAL_TYPE
783 193 : && (code2 == INTEGER_TYPE
784 193 : || code2 == BOOLEAN_TYPE))
785 : return t1;
786 2631975 : if (code2 == ENUMERAL_TYPE
787 69 : && (code1 == INTEGER_TYPE
788 69 : || code1 == BOOLEAN_TYPE))
789 : return t2;
790 :
791 2631906 : gcc_assert (code1 == code2);
792 :
793 2631906 : switch (code1)
794 : {
795 6366 : case POINTER_TYPE:
796 : /* For two pointers, do this recursively on the target type. */
797 6366 : {
798 6366 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
799 6366 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
800 : cond, cache);
801 6366 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
802 12732 : return c_build_type_attribute_qual_variant (n, attributes,
803 6366 : TYPE_QUALS (t2));
804 : }
805 :
806 10994 : case ARRAY_TYPE:
807 10994 : {
808 10994 : tree d1 = TYPE_DOMAIN (t1);
809 10994 : tree d2 = TYPE_DOMAIN (t2);
810 :
811 : /* We should not have any type quals on arrays at all. */
812 10994 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
813 : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
814 :
815 10994 : bool t1_complete = COMPLETE_TYPE_P (t1);
816 10994 : bool t2_complete = COMPLETE_TYPE_P (t2);
817 :
818 10994 : bool d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
819 10994 : bool d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
820 :
821 10994 : bool d1_variable, d2_variable;
822 :
823 21988 : d1_variable = (!d1_zero
824 10994 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
825 10399 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
826 21988 : d2_variable = (!d2_zero
827 10994 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
828 10652 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
829 :
830 10994 : bool use1 = d1 && (d2_variable || d2_zero || !d1_variable);
831 10994 : bool use2 = d2 && (d1_variable || d1_zero || !d2_variable);
832 :
833 : /* If the first is an unspecified size pick the other one. */
834 9012 : if (d2_variable && c_type_unspecified_p (t1))
835 : {
836 28 : gcc_assert (use1 && use2);
837 : use1 = false;
838 : }
839 :
840 : /* If both are VLAs but not unspecified and we are in the
841 : conditional operator, we create a conditional to select
842 : the size of the active branch. */
843 232 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
844 11223 : && d2_variable && !c_type_unspecified_p (t2);
845 :
846 10994 : tree td;
847 10994 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
848 : cond, cache);
849 :
850 10994 : if (!use0)
851 : {
852 : /* Save space: see if the result is identical to one of the args. */
853 10877 : if (elt == TREE_TYPE (t1) && use1)
854 10087 : return c_build_type_attribute_variant (t1, attributes);
855 790 : if (elt == TREE_TYPE (t2) && use2)
856 672 : return c_build_type_attribute_variant (t2, attributes);
857 :
858 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
859 10 : return c_build_type_attribute_variant (t1, attributes);
860 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
861 2 : return c_build_type_attribute_variant (t2, attributes);
862 :
863 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
864 : }
865 : else
866 : {
867 : /* Not used in C. */
868 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
869 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
870 :
871 117 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
872 117 : cond, TYPE_MAX_VALUE (d1),
873 117 : TYPE_MAX_VALUE (d2));
874 :
875 117 : td = build_index_type (d);
876 : }
877 :
878 : /* Merge the element types, and have a size if either arg has
879 : one. We may have qualifiers on the element types. To set
880 : up TYPE_MAIN_VARIANT correctly, we need to form the
881 : composite of the unqualified types and add the qualifiers
882 : back at the end. */
883 223 : int quals = TYPE_QUALS (strip_array_types (elt));
884 223 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
885 :
886 223 : t1 = c_build_array_type (unqual_elt, td);
887 :
888 : /* Check that a type which has a varying outermost dimension
889 : got marked has having a variable size. */
890 223 : bool varsize = (d1_variable && d2_variable)
891 97 : || (d1_variable && !t2_complete)
892 309 : || (d2_variable && !t1_complete);
893 157 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
894 :
895 : /* Ensure a composite type involving a zero-length array type
896 : is a zero-length type not an incomplete type. */
897 223 : if (d1_zero && d2_zero
898 16 : && (t1_complete || t2_complete)
899 224 : && !COMPLETE_TYPE_P (t1))
900 : {
901 1 : TYPE_SIZE (t1) = bitsize_zero_node;
902 1 : TYPE_SIZE_UNIT (t1) = size_zero_node;
903 : }
904 223 : t1 = c_build_qualified_type (t1, quals);
905 223 : return c_build_type_attribute_variant (t1, attributes);
906 : }
907 :
908 253 : case RECORD_TYPE:
909 253 : case UNION_TYPE:
910 253 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
911 : {
912 : /* Go to the original type to get the right tag. */
913 150 : tree tag = c_type_tag (t1);
914 :
915 150 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
916 150 : gcc_checking_assert (!tag || comptypes (t1, t2));
917 :
918 : /* If a composite type for these two types is already under
919 : construction, return it. */
920 :
921 376 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
922 246 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
923 20 : return c->composite;
924 :
925 : /* Otherwise, create a new type node and link it into the cache. */
926 :
927 130 : tree n = make_node (code1);
928 130 : SET_TYPE_STRUCTURAL_EQUALITY (n);
929 130 : TYPE_NAME (n) = tag;
930 :
931 130 : struct composite_cache cache2 = { t1, t2, n, cache };
932 130 : cache = &cache2;
933 :
934 130 : tree f1 = TYPE_FIELDS (t1);
935 130 : tree f2 = TYPE_FIELDS (t2);
936 130 : tree fields = NULL_TREE;
937 :
938 321 : for (tree a = f1, b = f2; a && b;
939 191 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
940 : {
941 191 : tree ta = TREE_TYPE (a);
942 191 : tree tb = TREE_TYPE (b);
943 :
944 191 : if (DECL_C_BIT_FIELD (a))
945 : {
946 19 : ta = DECL_BIT_FIELD_TYPE (a);
947 19 : tb = DECL_BIT_FIELD_TYPE (b);
948 : }
949 :
950 191 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
951 191 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
952 :
953 191 : tree t = composite_type_internal (ta, tb, cond, cache);
954 191 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
955 :
956 191 : DECL_PACKED (f) = DECL_PACKED (a);
957 191 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
958 191 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
959 191 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
960 :
961 191 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
962 :
963 191 : finish_decl (f, input_location, NULL, NULL, NULL);
964 :
965 191 : if (DECL_C_BIT_FIELD (a))
966 : {
967 : /* This will be processed by finish_struct. */
968 19 : SET_DECL_C_BIT_FIELD (f);
969 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
970 19 : tree_to_uhwi (DECL_SIZE (a)));
971 19 : DECL_NONADDRESSABLE_P (f) = true;
972 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
973 : }
974 :
975 191 : DECL_CHAIN (f) = fields;
976 191 : fields = f;
977 : }
978 :
979 130 : fields = nreverse (fields);
980 :
981 : /* Setup the struct/union type. Because we inherit all variably
982 : modified components, we can ignore the size expression. */
983 130 : tree expr = NULL_TREE;
984 :
985 : /* Set TYPE_STUB_DECL for debugging symbols. */
986 130 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
987 : NULL_TREE, n));
988 :
989 130 : n = finish_struct (input_location, n, fields, attributes, NULL,
990 : &expr);
991 :
992 130 : return qualify_type (n, t1);
993 : }
994 : /* FALLTHRU */
995 103 : case ENUMERAL_TYPE:
996 103 : if (attributes != NULL)
997 : {
998 : /* Try harder not to create a new aggregate type. */
999 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1000 : return t1;
1001 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1002 : return t2;
1003 : }
1004 99 : return c_build_type_attribute_variant (t1, attributes);
1005 :
1006 2573358 : case FUNCTION_TYPE:
1007 : /* Function types: prefer the one that specified arg types.
1008 : If both do, merge the arg types. Also merge the return types. */
1009 2573358 : {
1010 2573358 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1011 : cond, cache);
1012 2573358 : tree p1 = TYPE_ARG_TYPES (t1);
1013 2573358 : tree p2 = TYPE_ARG_TYPES (t2);
1014 :
1015 : /* Save space: see if the result is identical to one of the args. */
1016 2573358 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1017 9845 : return c_build_functype_attribute_variant (t1, t2, attributes);
1018 2564040 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
1019 517 : return c_build_functype_attribute_variant (t2, t1, attributes);
1020 :
1021 : /* Simple way if one arg fails to specify argument types. */
1022 2563523 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
1023 : {
1024 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
1025 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
1026 9 : t1 = c_build_type_attribute_variant (t1, attributes);
1027 9 : return qualify_type (t1, t2);
1028 : }
1029 2563514 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
1030 : {
1031 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
1032 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
1033 1 : t1 = c_build_type_attribute_variant (t1, attributes);
1034 1 : return qualify_type (t1, t2);
1035 : }
1036 :
1037 : /* If both args specify argument types, we must merge the two
1038 : lists, argument by argument. */
1039 2563513 : tree newargs = NULL_TREE;
1040 2563513 : tree *endp = &newargs;
1041 :
1042 6667969 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1043 : {
1044 6500602 : if (p1 == void_list_node)
1045 : {
1046 2396146 : *endp = void_list_node;
1047 2396146 : break;
1048 : }
1049 4104456 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1050 4104456 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1051 :
1052 4104456 : gcc_assert (mv1);
1053 4104456 : gcc_assert (mv2);
1054 :
1055 4104456 : mv1 = transparent_union_replacement (mv1, mv2);
1056 4104456 : mv2 = transparent_union_replacement (mv2, mv1);
1057 :
1058 4104456 : *endp = tree_cons (NULL_TREE,
1059 : composite_type_internal (mv1, mv2, cond, cache),
1060 : NULL_TREE);
1061 :
1062 4104456 : endp = &TREE_CHAIN (*endp);
1063 : }
1064 :
1065 2563513 : t1 = c_build_function_type (valtype, newargs);
1066 2563513 : t1 = qualify_type (t1, t2);
1067 : }
1068 : /* FALLTHRU */
1069 :
1070 2604448 : default:
1071 2604448 : return c_build_type_attribute_variant (t1, attributes);
1072 : }
1073 : }
1074 :
1075 : tree
1076 6320595 : composite_type_cond (tree t1, tree t2, tree cond)
1077 : {
1078 6320595 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1079 :
1080 6320595 : struct composite_cache cache = { };
1081 6320595 : tree n = composite_type_internal (t1, t2, cond, &cache);
1082 :
1083 6320595 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1084 6320595 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1085 6320595 : return n;
1086 : }
1087 :
1088 :
1089 : tree
1090 6316125 : composite_type (tree t1, tree t2)
1091 : {
1092 6316125 : return composite_type_cond (t1, t2, NULL_TREE);
1093 : }
1094 :
1095 : /* Return the type of a conditional expression between pointers to
1096 : possibly differently qualified versions of compatible types.
1097 :
1098 : We assume that comp_target_types has already been done and returned
1099 : true; if that isn't so, this may crash. */
1100 :
1101 : static tree
1102 114901 : common_pointer_type (tree t1, tree t2, tree cond)
1103 : {
1104 114901 : tree attributes;
1105 114901 : unsigned target_quals;
1106 114901 : addr_space_t as1, as2, as_common;
1107 114901 : int quals1, quals2;
1108 :
1109 : /* Save time if the two types are the same. */
1110 :
1111 114901 : if (t1 == t2) return t1;
1112 :
1113 : /* If one type is nonsense, use the other. */
1114 4470 : if (t1 == error_mark_node)
1115 : return t2;
1116 4470 : if (t2 == error_mark_node)
1117 : return t1;
1118 :
1119 4470 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1120 : && TREE_CODE (t2) == POINTER_TYPE);
1121 :
1122 : /* Merge the attributes. */
1123 4470 : attributes = targetm.merge_type_attributes (t1, t2);
1124 :
1125 : /* Find the composite type of the target types, and combine the
1126 : qualifiers of the two types' targets. */
1127 4470 : tree pointed_to_1 = TREE_TYPE (t1);
1128 4470 : tree pointed_to_2 = TREE_TYPE (t2);
1129 4470 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1130 4470 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1131 :
1132 : /* Strip array types to get correct qualifier for pointers to arrays */
1133 4470 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1134 4470 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1135 :
1136 : /* For function types do not merge const qualifiers, but drop them
1137 : if used inconsistently. The middle-end uses these to mark const
1138 : and noreturn functions. */
1139 4470 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1140 2129 : target_quals = (quals1 & quals2);
1141 : else
1142 2341 : target_quals = (quals1 | quals2);
1143 :
1144 : /* If the two named address spaces are different, determine the common
1145 : superset address space. This is guaranteed to exist due to the
1146 : assumption that comp_target_type returned true. */
1147 4470 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1148 4470 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1149 4470 : if (!addr_space_superset (as1, as2, &as_common))
1150 0 : gcc_unreachable ();
1151 :
1152 4470 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1153 :
1154 4470 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1155 4470 : return c_build_type_attribute_variant (t1, attributes);
1156 : }
1157 :
1158 : /* Return the common type for two arithmetic types under the usual
1159 : arithmetic conversions. The default conversions have already been
1160 : applied, and enumerated types converted to their compatible integer
1161 : types. The resulting type is unqualified and has no attributes.
1162 :
1163 : This is the type for the result of most arithmetic operations
1164 : if the operands have the given two types. */
1165 :
1166 : static tree
1167 13942545 : c_common_type (tree t1, tree t2)
1168 : {
1169 13942545 : enum tree_code code1;
1170 13942545 : enum tree_code code2;
1171 :
1172 : /* If one type is nonsense, use the other. */
1173 13942545 : if (t1 == error_mark_node)
1174 : return t2;
1175 13942545 : if (t2 == error_mark_node)
1176 : return t1;
1177 :
1178 13942545 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1179 37924 : t1 = TYPE_MAIN_VARIANT (t1);
1180 :
1181 13942545 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1182 49381 : t2 = TYPE_MAIN_VARIANT (t2);
1183 :
1184 13942545 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1185 : {
1186 188232 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1187 188232 : t1 = c_build_type_attribute_variant (t1, attrs);
1188 : }
1189 :
1190 13942545 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1191 : {
1192 187710 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1193 187710 : t2 = c_build_type_attribute_variant (t2, attrs);
1194 : }
1195 :
1196 : /* Save time if the two types are the same. */
1197 :
1198 13942545 : if (t1 == t2) return t1;
1199 :
1200 2321252 : code1 = TREE_CODE (t1);
1201 2321252 : code2 = TREE_CODE (t2);
1202 :
1203 2321252 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1204 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1205 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1206 2321252 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1207 : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1208 : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1209 :
1210 : /* When one operand is a decimal float type, the other operand cannot be
1211 : a generic float type or a complex type. We also disallow vector types
1212 : here. */
1213 2321252 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1214 2323675 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1215 : {
1216 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1217 : {
1218 3 : error ("cannot mix operands of decimal floating and vector types");
1219 3 : return error_mark_node;
1220 : }
1221 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1222 : {
1223 5 : error ("cannot mix operands of decimal floating and complex types");
1224 5 : return error_mark_node;
1225 : }
1226 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1227 : {
1228 16 : error ("cannot mix operands of decimal floating "
1229 : "and other floating types");
1230 16 : return error_mark_node;
1231 : }
1232 : }
1233 :
1234 : /* If one type is a vector type, return that type. (How the usual
1235 : arithmetic conversions apply to the vector types extension is not
1236 : precisely specified.) */
1237 2321228 : if (code1 == VECTOR_TYPE)
1238 : return t1;
1239 :
1240 2320941 : if (code2 == VECTOR_TYPE)
1241 : return t2;
1242 :
1243 : /* If one type is complex, form the common type of the non-complex
1244 : components, then make that complex. Use T1 or T2 if it is the
1245 : required type. */
1246 2320938 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1247 : {
1248 89322 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1249 89322 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1250 89322 : tree subtype = c_common_type (subtype1, subtype2);
1251 :
1252 89322 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1253 : return t1;
1254 59207 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1255 : return t2;
1256 3241 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1257 : {
1258 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1259 4 : TYPE_PRECISION (subtype));
1260 6 : return code1 == COMPLEX_TYPE ? t1 : t2;
1261 : }
1262 : else
1263 3237 : return build_complex_type (subtype);
1264 : }
1265 :
1266 : /* If only one is real, use it as the result. */
1267 :
1268 2231616 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1269 : return t1;
1270 :
1271 2104737 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1272 : return t2;
1273 :
1274 : /* If both are real and either are decimal floating point types, use
1275 : the decimal floating point type with the greater precision. */
1276 :
1277 2074365 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1278 : {
1279 107214 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1280 107214 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1281 : return dfloat128_type_node;
1282 : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1283 : implementation the same mode. */
1284 106665 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1285 106665 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1286 : return dfloat64x_type_node;
1287 106661 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1288 106661 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1289 : return dfloat64_type_node;
1290 106216 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1291 106216 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1292 : return dfloat32_type_node;
1293 : }
1294 :
1295 : /* Deal with fixed-point types. */
1296 2073022 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1297 : {
1298 0 : unsigned int unsignedp = 0, satp = 0;
1299 0 : scalar_mode m1, m2;
1300 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1301 :
1302 0 : m1 = SCALAR_TYPE_MODE (t1);
1303 0 : m2 = SCALAR_TYPE_MODE (t2);
1304 :
1305 : /* If one input type is saturating, the result type is saturating. */
1306 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1307 : satp = 1;
1308 :
1309 : /* If both fixed-point types are unsigned, the result type is unsigned.
1310 : When mixing fixed-point and integer types, follow the sign of the
1311 : fixed-point type.
1312 : Otherwise, the result type is signed. */
1313 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1314 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1315 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1316 0 : && TYPE_UNSIGNED (t1))
1317 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1318 0 : && TYPE_UNSIGNED (t2)))
1319 : unsignedp = 1;
1320 :
1321 : /* The result type is signed. */
1322 0 : if (unsignedp == 0)
1323 : {
1324 : /* If the input type is unsigned, we need to convert to the
1325 : signed type. */
1326 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1327 : {
1328 0 : enum mode_class mclass = (enum mode_class) 0;
1329 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1330 : mclass = MODE_FRACT;
1331 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1332 : mclass = MODE_ACCUM;
1333 : else
1334 0 : gcc_unreachable ();
1335 0 : m1 = as_a <scalar_mode>
1336 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1337 : }
1338 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1339 : {
1340 0 : enum mode_class mclass = (enum mode_class) 0;
1341 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1342 : mclass = MODE_FRACT;
1343 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1344 : mclass = MODE_ACCUM;
1345 : else
1346 0 : gcc_unreachable ();
1347 0 : m2 = as_a <scalar_mode>
1348 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1349 : }
1350 : }
1351 :
1352 0 : if (code1 == FIXED_POINT_TYPE)
1353 : {
1354 0 : fbit1 = GET_MODE_FBIT (m1);
1355 0 : ibit1 = GET_MODE_IBIT (m1);
1356 : }
1357 : else
1358 : {
1359 0 : fbit1 = 0;
1360 : /* Signed integers need to subtract one sign bit. */
1361 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1362 : }
1363 :
1364 0 : if (code2 == FIXED_POINT_TYPE)
1365 : {
1366 0 : fbit2 = GET_MODE_FBIT (m2);
1367 0 : ibit2 = GET_MODE_IBIT (m2);
1368 : }
1369 : else
1370 : {
1371 0 : fbit2 = 0;
1372 : /* Signed integers need to subtract one sign bit. */
1373 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1374 : }
1375 :
1376 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1377 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1378 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1379 : satp);
1380 : }
1381 :
1382 : /* Both real or both integers; use the one with greater precision. */
1383 :
1384 2073022 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1385 : return t1;
1386 1102048 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1387 : return t2;
1388 :
1389 : /* Same precision. Prefer long longs to longs to ints when the
1390 : same precision, following the C99 rules on integer type rank
1391 : (which are equivalent to the C90 rules for C90 types). */
1392 :
1393 737775 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1394 737775 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1395 : return long_long_unsigned_type_node;
1396 :
1397 648532 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1398 648532 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1399 : {
1400 15315 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1401 361 : return long_long_unsigned_type_node;
1402 : else
1403 : return long_long_integer_type_node;
1404 : }
1405 :
1406 633217 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1407 633217 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1408 : return long_unsigned_type_node;
1409 :
1410 534080 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1411 534080 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1412 : {
1413 : /* But preserve unsignedness from the other type,
1414 : since long cannot hold all the values of an unsigned int. */
1415 26403 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1416 131 : return long_unsigned_type_node;
1417 : else
1418 : return long_integer_type_node;
1419 : }
1420 :
1421 : /* For floating types of the same TYPE_PRECISION (which we here
1422 : assume means either the same set of values, or sets of values
1423 : neither a subset of the other, with behavior being undefined in
1424 : the latter case), follow the rules from TS 18661-3: prefer
1425 : interchange types _FloatN, then standard types long double,
1426 : double, float, then extended types _FloatNx. For extended types,
1427 : check them starting with _Float128x as that seems most consistent
1428 : in spirit with preferring long double to double; for interchange
1429 : types, also check in that order for consistency although it's not
1430 : possible for more than one of them to have the same
1431 : precision. */
1432 507677 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1433 507677 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1434 :
1435 2537819 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1436 2030305 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1437 : return FLOATN_TYPE_NODE (i);
1438 :
1439 : /* Likewise, prefer long double to double even if same size. */
1440 507514 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1441 : return long_double_type_node;
1442 :
1443 : /* Likewise, prefer double to float even if same size.
1444 : We got a couple of embedded targets with 32 bit doubles, and the
1445 : pdp11 might have 64 bit floats. */
1446 507272 : if (mv1 == double_type_node || mv2 == double_type_node)
1447 : return double_type_node;
1448 :
1449 506700 : if (mv1 == float_type_node || mv2 == float_type_node)
1450 : return float_type_node;
1451 :
1452 2018124 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1453 1513593 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1454 : return FLOATNX_TYPE_NODE (i);
1455 :
1456 504531 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1457 : {
1458 : /* Prefer any other integral types over bit-precise integer types. */
1459 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1460 6 : return code1 == BITINT_TYPE ? t2 : t1;
1461 : /* If BITINT_TYPE is unsigned and the other type is signed
1462 : non-BITINT_TYPE with the same precision, the latter has higher rank.
1463 : In that case:
1464 : Otherwise, both operands are converted to the unsigned integer type
1465 : corresponding to the type of the operand with signed integer type. */
1466 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1467 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1468 : }
1469 :
1470 : /* Otherwise prefer the unsigned one. */
1471 :
1472 504523 : if (TYPE_UNSIGNED (t1))
1473 : return t1;
1474 : else
1475 : return t2;
1476 : }
1477 :
1478 : /* Wrapper around c_common_type that is used by c-common.cc and other
1479 : front end optimizations that remove promotions. ENUMERAL_TYPEs
1480 : are allowed here and are converted to their compatible integer types.
1481 : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1482 : preferably a non-Boolean type as the common type. */
1483 : tree
1484 102837 : common_type (tree t1, tree t2)
1485 : {
1486 102837 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1487 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1488 102837 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1489 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1490 :
1491 : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1492 102837 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1493 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1494 584 : return boolean_type_node;
1495 :
1496 : /* If either type is BOOLEAN_TYPE, then return the other. */
1497 102253 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1498 : return t2;
1499 102253 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1500 : return t1;
1501 :
1502 102253 : return c_common_type (t1, t2);
1503 : }
1504 :
1505 :
1506 :
1507 : /* Helper function for comptypes. For two compatible types, return 1
1508 : if they pass consistency checks. In particular we test that
1509 : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1510 :
1511 : static bool
1512 44388473 : comptypes_verify (tree type1, tree type2)
1513 : {
1514 44388473 : if (type1 == type2 || !type1 || !type2
1515 3492754 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1516 : return true;
1517 :
1518 3492753 : gcc_checking_assert (c_verify_type (type1));
1519 3492753 : gcc_checking_assert (c_verify_type (type2));
1520 :
1521 3492753 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1522 386005 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1523 3876967 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1524 : {
1525 : /* FIXME: check other types. */
1526 383850 : if (RECORD_OR_UNION_TYPE_P (type1)
1527 383850 : || TREE_CODE (type1) == ENUMERAL_TYPE
1528 383850 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1529 0 : return false;
1530 : }
1531 : return true;
1532 : }
1533 :
1534 : struct comptypes_data {
1535 :
1536 : /* output */
1537 : bool enum_and_int_p;
1538 : bool different_types_p;
1539 : bool warning_needed;
1540 :
1541 : /* context */
1542 : bool anon_field;
1543 : bool pointedto;
1544 :
1545 : /* configuration */
1546 : bool equiv;
1547 : bool ignore_promoting_args;
1548 :
1549 : const struct tagged_tu_seen_cache* cache;
1550 : };
1551 :
1552 :
1553 : /* Helper function for composite_type. This function ignores when the
1554 : function type of an old-style declaration is incompatible with a type
1555 : of a declaration with prototype because some are arguments are not
1556 : self-promoting. This is ignored only for function types but not
1557 : ignored in a nested context. */
1558 :
1559 : static bool
1560 18961785 : comptypes_check_for_composite (tree t1, tree t2)
1561 : {
1562 18961785 : struct comptypes_data data = { };
1563 18961785 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1564 18961785 : return comptypes_internal (t1, t2, &data);
1565 : }
1566 :
1567 :
1568 : /* C implementation of compatible_types_for_indirection_note_p. */
1569 :
1570 : bool
1571 762 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1572 : {
1573 762 : return comptypes (type1, type2) == 1;
1574 : }
1575 :
1576 : /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1577 : or various other operations. Return 2 if they are compatible
1578 : but a warning may be needed if you use them together. */
1579 :
1580 : int
1581 56353415 : comptypes (tree type1, tree type2)
1582 : {
1583 56353415 : struct comptypes_data data = { };
1584 56353415 : bool ret = comptypes_internal (type1, type2, &data);
1585 :
1586 56353415 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1587 :
1588 37843219 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1589 : }
1590 :
1591 :
1592 : /* Like comptypes, but it returns non-zero only for identical
1593 : types. */
1594 :
1595 : bool
1596 374 : comptypes_same_p (tree type1, tree type2)
1597 : {
1598 374 : struct comptypes_data data = { };
1599 374 : bool ret = comptypes_internal (type1, type2, &data);
1600 :
1601 374 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1602 :
1603 374 : if (data.different_types_p)
1604 152 : return false;
1605 :
1606 : return ret;
1607 : }
1608 :
1609 :
1610 : /* Like comptypes, but if it returns non-zero because enum and int are
1611 : compatible, it sets *ENUM_AND_INT_P to true. */
1612 :
1613 : int
1614 6744773 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1615 : {
1616 6744773 : struct comptypes_data data = { };
1617 6744773 : bool ret = comptypes_internal (type1, type2, &data);
1618 6744773 : *enum_and_int_p = data.enum_and_int_p;
1619 :
1620 6744773 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1621 :
1622 6499089 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1623 : }
1624 :
1625 : /* Like comptypes, but if it returns nonzero for different types, it
1626 : sets *DIFFERENT_TYPES_P to true. */
1627 :
1628 : int
1629 45831 : comptypes_check_different_types (tree type1, tree type2,
1630 : bool *different_types_p)
1631 : {
1632 45831 : struct comptypes_data data = { };
1633 45831 : bool ret = comptypes_internal (type1, type2, &data);
1634 45831 : *different_types_p = data.different_types_p;
1635 :
1636 45831 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1637 :
1638 45831 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1639 : }
1640 :
1641 :
1642 : /* Like comptypes, but if it returns true for struct and union types
1643 : considered equivalent for aliasing purposes, i.e. for setting
1644 : TYPE_CANONICAL after completing a struct or union.
1645 :
1646 : This function must return false only for types which are not
1647 : compatible according to C language semantics (cf. comptypes),
1648 : otherwise the middle-end would make incorrect aliasing decisions.
1649 : It may return true for some similar types that are not compatible
1650 : according to those stricter rules.
1651 :
1652 : In particular, we ignore size expression in arrays so that the
1653 : following structs are in the same equivalence class:
1654 :
1655 : struct foo { char (*buf)[]; };
1656 : struct foo { char (*buf)[3]; };
1657 : struct foo { char (*buf)[4]; };
1658 :
1659 : We also treat unions / structs with members which are pointers to
1660 : structures or unions with the same tag as equivalent (if they are not
1661 : incompatible for other reasons). Although incomplete structure
1662 : or union types are not compatible to any other type, they may become
1663 : compatible to different types when completed. To avoid having to update
1664 : TYPE_CANONICAL at this point, we only consider the tag when forming
1665 : the equivalence classes. For example, the following types with tag
1666 : 'foo' are all considered equivalent:
1667 :
1668 : struct bar;
1669 : struct foo { struct bar *x };
1670 : struct foo { struct bar { int a; } *x };
1671 : struct foo { struct bar { char b; } *x }; */
1672 :
1673 : bool
1674 14643587 : comptypes_equiv_p (tree type1, tree type2)
1675 : {
1676 14643587 : struct comptypes_data data = { };
1677 14643587 : data.equiv = true;
1678 14643587 : bool ret = comptypes_internal (type1, type2, &data);
1679 :
1680 : /* check that different equivance classes are assigned only
1681 : to types that are not compatible. */
1682 14643587 : gcc_checking_assert (ret || !comptypes (type1, type2));
1683 :
1684 14643587 : return ret;
1685 : }
1686 :
1687 :
1688 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1689 : or various other operations. If they are compatible but a warning may
1690 : be needed if you use them together, 'warning_needed' in DATA is set.
1691 : If one type is an enum and the other a compatible integer type, then
1692 : this sets 'enum_and_int_p' in DATA to true (it is never set to
1693 : false). If the types are compatible but different enough not to be
1694 : permitted in C11 typedef redeclarations, then this sets
1695 : 'different_types_p' in DATA to true; it is never set to
1696 : false, but may or may not be set if the types are incompatible.
1697 : If two functions types are not compatible only because one is
1698 : an old-style definition that does not have self-promoting arguments,
1699 : then this can be ignored by setting 'ignore_promoting_args_p'.
1700 : For 'equiv' we can compute equivalency classes (see above).
1701 : This differs from comptypes, in that we don't free the seen
1702 : types. */
1703 :
1704 : static bool
1705 132653187 : comptypes_internal (const_tree type1, const_tree type2,
1706 : struct comptypes_data *data)
1707 : {
1708 132936147 : const_tree t1 = type1;
1709 132936147 : const_tree t2 = type2;
1710 :
1711 : /* Suppress errors caused by previously reported errors. */
1712 :
1713 132936147 : if (t1 == t2 || !t1 || !t2
1714 45074843 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1715 : return true;
1716 :
1717 : /* Qualifiers must match. C99 6.7.3p9 */
1718 :
1719 45074840 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1720 : return false;
1721 :
1722 : /* Enumerated types are compatible with integer types, but this is
1723 : not transitive: two enumerated types in the same translation unit
1724 : are compatible with each other only if they are the same type. */
1725 :
1726 45034780 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1727 1016 : && COMPLETE_TYPE_P (t1)
1728 45035752 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1729 : {
1730 856 : t1 = ENUM_UNDERLYING_TYPE (t1);
1731 856 : if (TREE_CODE (t2) != VOID_TYPE)
1732 : {
1733 852 : data->enum_and_int_p = true;
1734 852 : data->different_types_p = true;
1735 : }
1736 : }
1737 45033924 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1738 4711 : && COMPLETE_TYPE_P (t2)
1739 45038629 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1740 : {
1741 4589 : t2 = ENUM_UNDERLYING_TYPE (t2);
1742 4589 : if (TREE_CODE (t1) != VOID_TYPE)
1743 : {
1744 3690 : data->enum_and_int_p = true;
1745 3690 : data->different_types_p = true;
1746 : }
1747 : }
1748 :
1749 45034780 : if (t1 == t2)
1750 : return true;
1751 :
1752 : /* Different classes of types can't be compatible. */
1753 :
1754 45033412 : if (TREE_CODE (t1) != TREE_CODE (t2))
1755 : return false;
1756 :
1757 : /* Allow for two different type nodes which have essentially the same
1758 : definition. Note that we already checked for equality of the type
1759 : qualifiers (just above). */
1760 :
1761 37021378 : if (TREE_CODE (t1) != ARRAY_TYPE
1762 37021378 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1763 : return true;
1764 :
1765 36686201 : int attrval;
1766 :
1767 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1768 36686201 : if (!(attrval = comp_type_attributes (t1, t2)))
1769 : return false;
1770 :
1771 36685808 : if (2 == attrval)
1772 336 : data->warning_needed = true;
1773 :
1774 36685808 : switch (TREE_CODE (t1))
1775 : {
1776 3359646 : case INTEGER_TYPE:
1777 3359646 : case FIXED_POINT_TYPE:
1778 3359646 : case REAL_TYPE:
1779 3359646 : case BITINT_TYPE:
1780 : /* With these nodes, we can't determine type equivalence by
1781 : looking at what is stored in the nodes themselves, because
1782 : two nodes might have different TYPE_MAIN_VARIANTs but still
1783 : represent the same type. For example, wchar_t and int could
1784 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1785 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1786 : and are distinct types. On the other hand, int and the
1787 : following typedef
1788 :
1789 : typedef int INT __attribute((may_alias));
1790 :
1791 : have identical properties, different TYPE_MAIN_VARIANTs, but
1792 : represent the same type. The canonical type system keeps
1793 : track of equivalence in this case, so we fall back on it. */
1794 3359646 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1795 :
1796 282960 : case POINTER_TYPE:
1797 : /* Do not remove mode information. */
1798 282960 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1799 : return false;
1800 282960 : data->pointedto = true;
1801 282960 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1802 :
1803 8894436 : case FUNCTION_TYPE:
1804 8894436 : return function_types_compatible_p (t1, t2, data);
1805 :
1806 154346 : case ARRAY_TYPE:
1807 154346 : {
1808 154346 : tree d1 = TYPE_DOMAIN (t1);
1809 154346 : tree d2 = TYPE_DOMAIN (t2);
1810 154346 : bool d1_variable, d2_variable;
1811 154346 : bool d1_zero, d2_zero;
1812 :
1813 : /* Target types must match incl. qualifiers. */
1814 154346 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1815 : return false;
1816 :
1817 119359 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1818 6289 : data->different_types_p = true;
1819 : /* Ignore size mismatches when forming equivalence classes. */
1820 119359 : if (data->equiv)
1821 : return true;
1822 : /* Sizes must match unless one is missing or variable. */
1823 50354 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1824 : return true;
1825 :
1826 34141 : d1_zero = !TYPE_MAX_VALUE (d1);
1827 34141 : d2_zero = !TYPE_MAX_VALUE (d2);
1828 :
1829 68282 : d1_variable = (!d1_zero
1830 34141 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1831 34109 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1832 68282 : d2_variable = (!d2_zero
1833 34141 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1834 34013 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1835 :
1836 34141 : if (d1_variable != d2_variable)
1837 943 : data->different_types_p = true;
1838 34141 : if (d1_variable || d2_variable)
1839 : return true;
1840 4047 : if (d1_zero && d2_zero)
1841 : return true;
1842 4047 : if (d1_zero || d2_zero
1843 3951 : || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1844 7998 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1845 4047 : return false;
1846 :
1847 : return true;
1848 : }
1849 :
1850 23456845 : case ENUMERAL_TYPE:
1851 23456845 : case RECORD_TYPE:
1852 23456845 : case UNION_TYPE:
1853 :
1854 23456845 : if (!flag_isoc23)
1855 : return false;
1856 :
1857 23456798 : return tagged_types_tu_compatible_p (t1, t2, data);
1858 :
1859 536770 : case VECTOR_TYPE:
1860 1073540 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1861 536770 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1862 :
1863 : default:
1864 : return false;
1865 : }
1866 : gcc_unreachable ();
1867 : }
1868 :
1869 : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1870 : their qualifiers, except for named address spaces. If the pointers point to
1871 : different named addresses, then we must determine if one address space is a
1872 : subset of the other. */
1873 :
1874 : static bool
1875 1763761 : comp_target_types (location_t location, tree ttl, tree ttr)
1876 : {
1877 1763761 : int val;
1878 1763761 : int val_ped;
1879 1763761 : tree mvl = TREE_TYPE (ttl);
1880 1763761 : tree mvr = TREE_TYPE (ttr);
1881 1763761 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1882 1763761 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1883 1763761 : addr_space_t as_common;
1884 1763761 : bool enum_and_int_p;
1885 :
1886 : /* Fail if pointers point to incompatible address spaces. */
1887 1763761 : if (!addr_space_superset (asl, asr, &as_common))
1888 : return 0;
1889 :
1890 : /* For pedantic record result of comptypes on arrays before losing
1891 : qualifiers on the element type below. */
1892 1763761 : val_ped = 1;
1893 :
1894 1763761 : if (TREE_CODE (mvl) == ARRAY_TYPE
1895 1991 : && TREE_CODE (mvr) == ARRAY_TYPE)
1896 1963 : val_ped = comptypes (mvl, mvr);
1897 :
1898 : /* Qualifiers on element types of array types that are
1899 : pointer targets are also removed. */
1900 1763761 : mvl = remove_qualifiers (mvl);
1901 1763761 : mvr = remove_qualifiers (mvr);
1902 :
1903 1763761 : enum_and_int_p = false;
1904 1763761 : val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1905 :
1906 1763761 : if (val == 1 && val_ped != 1)
1907 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1908 : "in ISO C before C23");
1909 :
1910 1763761 : if (val == 2)
1911 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1912 :
1913 1763761 : if (val == 1 && enum_and_int_p && warn_cxx_compat)
1914 2 : warning_at (location, OPT_Wc___compat,
1915 : "pointer target types incompatible in C++");
1916 :
1917 1763761 : return val;
1918 : }
1919 :
1920 : /* Subroutines of `comptypes'. */
1921 :
1922 : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1923 : compatible. The two types are not the same (which has been
1924 : checked earlier in comptypes_internal). */
1925 :
1926 : static bool
1927 23456798 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1928 : struct comptypes_data *data)
1929 : {
1930 23456798 : tree s1, s2;
1931 :
1932 23456798 : if (c_type_tag (t1) != c_type_tag (t2))
1933 : return false;
1934 :
1935 : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1936 : structs with the same tag as equivalent, but only when they are targets
1937 : of pointers inside other structs. */
1938 19387571 : if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1939 : return true;
1940 :
1941 : /* Different types without tag are incompatible except as an anonymous
1942 : field or when forming equivalence classes for TYPE_CANONICAL. */
1943 19387510 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1944 : return false;
1945 :
1946 14255536 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1947 14254495 : data->different_types_p = true;
1948 :
1949 : /* Incomplete types are incompatible inside a TU. */
1950 14255536 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1951 : return false;
1952 :
1953 14255526 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1954 14255526 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1955 14255471 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1956 : return false;
1957 :
1958 14255516 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1959 144624 : data->different_types_p = true;
1960 :
1961 : /* For types already being looked at in some active
1962 : invocation of this function, assume compatibility.
1963 : The cache is built as a linked list on the stack
1964 : with the head of the list passed downwards. */
1965 14255516 : for (const struct tagged_tu_seen_cache *t = data->cache;
1966 14265192 : t != NULL; t = t->next)
1967 9970 : if (t->t1 == t1 && t->t2 == t2)
1968 : return true;
1969 :
1970 14255222 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1971 :
1972 14255222 : switch (TREE_CODE (t1))
1973 : {
1974 55 : case ENUMERAL_TYPE:
1975 55 : {
1976 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1977 : return false;
1978 :
1979 : /* Speed up the case where the type values are in the same order. */
1980 51 : tree tv1 = TYPE_VALUES (t1);
1981 51 : tree tv2 = TYPE_VALUES (t2);
1982 :
1983 51 : if (tv1 == tv2)
1984 : return true;
1985 :
1986 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1987 : {
1988 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1989 : break;
1990 :
1991 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1992 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1993 : break;
1994 : }
1995 :
1996 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1997 : return true;
1998 :
1999 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
2000 : return false;
2001 :
2002 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
2003 : return false;
2004 :
2005 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
2006 : {
2007 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
2008 :
2009 16 : if (s2 == NULL
2010 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
2011 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
2012 8 : return false;
2013 : }
2014 :
2015 : return true;
2016 : }
2017 :
2018 14255167 : case UNION_TYPE:
2019 14255167 : case RECORD_TYPE:
2020 :
2021 14255167 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2022 : return false;
2023 :
2024 12322429 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2025 13845990 : s1 && s2;
2026 1523561 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2027 : {
2028 13780581 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2029 13780581 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2030 :
2031 13780581 : tree ft1 = TREE_TYPE (s1);
2032 13780581 : tree ft2 = TREE_TYPE (s2);
2033 :
2034 13780581 : if (DECL_NAME (s1) != DECL_NAME (s2))
2035 : return false;
2036 :
2037 12134760 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2038 : return false;
2039 :
2040 3233508 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2041 : return false;
2042 :
2043 3233488 : if (DECL_C_BIT_FIELD (s1))
2044 : {
2045 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2046 : return false;
2047 :
2048 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2049 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2050 : }
2051 :
2052 3233334 : if (!ft1 || !ft2)
2053 : return false;
2054 :
2055 3233334 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2056 : return false;
2057 :
2058 3233333 : data->anon_field = !DECL_NAME (s1);
2059 3233333 : data->pointedto = false;
2060 :
2061 3233333 : const struct tagged_tu_seen_cache *cache = data->cache;
2062 3233333 : data->cache = &entry;
2063 3233333 : bool ret = comptypes_internal (ft1, ft2, data);
2064 3233333 : data->cache = cache;
2065 3233333 : if (!ret)
2066 : return false;
2067 :
2068 1592454 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2069 1592454 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2070 :
2071 1592454 : if (data->equiv
2072 998172 : && st1 && TREE_CODE (st1) == INTEGER_CST
2073 997690 : && st2 && TREE_CODE (st2) == INTEGER_CST
2074 2590138 : && !tree_int_cst_equal (st1, st2))
2075 : return false;
2076 :
2077 1523595 : tree counted_by1 = lookup_attribute ("counted_by",
2078 1523595 : DECL_ATTRIBUTES (s1));
2079 1523595 : tree counted_by2 = lookup_attribute ("counted_by",
2080 1523595 : DECL_ATTRIBUTES (s2));
2081 : /* If there is no counted_by attribute for both fields. */
2082 1523595 : if (!counted_by1 && !counted_by2)
2083 1523531 : continue;
2084 :
2085 : /* If only one field has counted_by attribute. */
2086 64 : if ((counted_by1 && !counted_by2)
2087 64 : || (!counted_by1 && counted_by2))
2088 : return false;
2089 :
2090 : /* Now both s1 and s2 have counted_by attributes, check
2091 : whether they are the same. */
2092 :
2093 52 : tree counted_by_field1
2094 52 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2095 52 : tree counted_by_field2
2096 52 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2097 :
2098 52 : gcc_assert (counted_by_field1 && counted_by_field2);
2099 :
2100 94 : while (TREE_CHAIN (counted_by_field1))
2101 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2102 86 : while (TREE_CHAIN (counted_by_field2))
2103 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2104 :
2105 52 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2106 52 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2107 : return false;
2108 : }
2109 : return true;
2110 :
2111 0 : default:
2112 0 : gcc_unreachable ();
2113 : }
2114 : }
2115 :
2116 : /* Return true if two function types F1 and F2 are compatible.
2117 : If either type specifies no argument types,
2118 : the other must specify a fixed number of self-promoting arg types.
2119 : Otherwise, if one type specifies only the number of arguments,
2120 : the other must specify that number of self-promoting arg types.
2121 : Otherwise, the argument types must match. */
2122 :
2123 : static bool
2124 8894436 : function_types_compatible_p (const_tree f1, const_tree f2,
2125 : struct comptypes_data *data)
2126 : {
2127 8894436 : tree ret1 = TREE_TYPE (f1);
2128 8894436 : tree ret2 = TREE_TYPE (f2);
2129 :
2130 : /* 'volatile' qualifiers on a function's return type used to mean
2131 : the function is noreturn. */
2132 8894436 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2133 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2134 8894436 : if (TYPE_VOLATILE (ret1))
2135 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2136 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2137 8894436 : if (TYPE_VOLATILE (ret2))
2138 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2139 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2140 :
2141 8894436 : bool ignore_pargs = data->ignore_promoting_args;
2142 8894436 : data->ignore_promoting_args = false;
2143 :
2144 8894436 : if (!comptypes_internal (ret1, ret2, data))
2145 : return false;
2146 :
2147 8891841 : data->ignore_promoting_args = ignore_pargs;
2148 :
2149 8891841 : tree args1 = TYPE_ARG_TYPES (f1);
2150 8891841 : tree args2 = TYPE_ARG_TYPES (f2);
2151 :
2152 8891841 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2153 41532 : data->different_types_p = true;
2154 :
2155 : /* An unspecified parmlist matches any specified parmlist
2156 : whose argument types don't need default promotions. */
2157 :
2158 8891841 : if (args1 == NULL_TREE)
2159 : {
2160 21909 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2161 : return false;
2162 21906 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2163 : return false;
2164 21434 : data->ignore_promoting_args = false;
2165 : /* If one of these types comes from a non-prototype fn definition,
2166 : compare that with the other type's arglist.
2167 : If they don't match, ask for a warning (but no error). */
2168 21434 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2169 21434 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2170 12 : data->warning_needed = true;
2171 21434 : return true;
2172 : }
2173 8869932 : if (args2 == NULL_TREE)
2174 : {
2175 19705 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2176 : return false;
2177 19703 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2178 : return false;
2179 19642 : data->ignore_promoting_args = false;
2180 19642 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2181 19642 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2182 1 : data->warning_needed = true;
2183 19642 : return true;
2184 : }
2185 :
2186 : /* Both types have argument lists: compare them and propagate results. */
2187 8850227 : return type_lists_compatible_p (args1, args2, data);
2188 : }
2189 :
2190 : /* Check two lists of types for compatibility, returning false for
2191 : incompatible, true for compatible. */
2192 :
2193 : static bool
2194 8850303 : type_lists_compatible_p (const_tree args1, const_tree args2,
2195 : struct comptypes_data *data)
2196 : {
2197 55121359 : while (1)
2198 : {
2199 31985831 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2200 : return true;
2201 : /* If one list is shorter than the other,
2202 : they fail to match. */
2203 23279990 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2204 : return false;
2205 23279983 : tree a1 = TREE_VALUE (args1);
2206 23279983 : tree a2 = TREE_VALUE (args2);
2207 23279983 : tree mv1 = remove_qualifiers (a1);
2208 23279983 : tree mv2 = remove_qualifiers (a2);
2209 :
2210 23279983 : gcc_assert (mv2);
2211 23279983 : gcc_assert (mv2);
2212 :
2213 : /* If one of the lists has an error marker, ignore this arg. */
2214 23279983 : if (TREE_CODE (a1) == ERROR_MARK
2215 23279979 : || TREE_CODE (a2) == ERROR_MARK)
2216 : ;
2217 23279963 : else if (!comptypes_internal (mv1, mv2, data))
2218 : {
2219 144686 : data->different_types_p = true;
2220 : /* Allow wait (union {union wait *u; int *i} *)
2221 : and wait (union wait *) to be compatible. */
2222 144686 : if (TREE_CODE (a1) == UNION_TYPE
2223 130 : && (TYPE_NAME (a1) == NULL_TREE
2224 98 : || TYPE_TRANSPARENT_AGGR (a1))
2225 130 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2226 144816 : && tree_int_cst_equal (TYPE_SIZE (a1),
2227 130 : TYPE_SIZE (a2)))
2228 : {
2229 130 : tree memb;
2230 130 : for (memb = TYPE_FIELDS (a1);
2231 166 : memb; memb = DECL_CHAIN (memb))
2232 : {
2233 164 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2234 164 : if (comptypes_internal (mv3, mv2, data))
2235 : break;
2236 : }
2237 : if (memb == NULL_TREE)
2238 : return false;
2239 : }
2240 144556 : else if (TREE_CODE (a2) == UNION_TYPE
2241 105 : && (TYPE_NAME (a2) == NULL_TREE
2242 74 : || TYPE_TRANSPARENT_AGGR (a2))
2243 105 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2244 144661 : && tree_int_cst_equal (TYPE_SIZE (a2),
2245 105 : TYPE_SIZE (a1)))
2246 : {
2247 105 : tree memb;
2248 105 : for (memb = TYPE_FIELDS (a2);
2249 133 : memb; memb = DECL_CHAIN (memb))
2250 : {
2251 131 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2252 131 : if (comptypes_internal (mv3, mv1, data))
2253 : break;
2254 : }
2255 : if (memb == NULL_TREE)
2256 : return false;
2257 : }
2258 : else
2259 144451 : return false;
2260 : }
2261 :
2262 23135528 : args1 = TREE_CHAIN (args1);
2263 23135528 : args2 = TREE_CHAIN (args2);
2264 23135528 : }
2265 : }
2266 :
2267 : /* Compute the size to increment a pointer by. When a function type or void
2268 : type or incomplete type is passed, size_one_node is returned.
2269 : This function does not emit any diagnostics; the caller is responsible
2270 : for that. */
2271 :
2272 : static tree
2273 233801 : c_size_in_bytes (const_tree type)
2274 : {
2275 233801 : enum tree_code code = TREE_CODE (type);
2276 :
2277 233801 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2278 233801 : || !COMPLETE_TYPE_P (type))
2279 191 : return size_one_node;
2280 :
2281 : /* Convert in case a char is more than one unit. */
2282 233610 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2283 233610 : size_int (TYPE_PRECISION (char_type_node)
2284 : / BITS_PER_UNIT));
2285 : }
2286 :
2287 : /* Return either DECL or its known constant value (if it has one). */
2288 :
2289 : tree
2290 14326854 : decl_constant_value_1 (tree decl, bool in_init)
2291 : {
2292 14326854 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2293 14326854 : TREE_CODE (decl) != PARM_DECL
2294 14326854 : && !TREE_THIS_VOLATILE (decl)
2295 13982548 : && TREE_READONLY (decl)
2296 36928 : && DECL_INITIAL (decl) != NULL_TREE
2297 35932 : && !error_operand_p (DECL_INITIAL (decl))
2298 : /* This is invalid if initial value is not constant.
2299 : If it has either a function call, a memory reference,
2300 : or a variable, then re-evaluating it could give different results. */
2301 35923 : && TREE_CONSTANT (DECL_INITIAL (decl))
2302 : /* Check for cases where this is sub-optimal, even though valid. */
2303 14349714 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2304 17645 : return DECL_INITIAL (decl);
2305 : return decl;
2306 : }
2307 :
2308 : /* Return either DECL or its known constant value (if it has one).
2309 : Like the above, but always return decl outside of functions. */
2310 :
2311 : tree
2312 14326606 : decl_constant_value (tree decl)
2313 : {
2314 : /* Don't change a variable array bound or initial value to a constant
2315 : in a place where a variable is invalid. */
2316 14326606 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2317 : }
2318 :
2319 : /* Convert the array expression EXP to a pointer. */
2320 : static tree
2321 820496 : array_to_pointer_conversion (location_t loc, tree exp)
2322 : {
2323 820496 : tree orig_exp = exp;
2324 820496 : tree type = TREE_TYPE (exp);
2325 820496 : tree adr;
2326 820496 : tree restype = TREE_TYPE (type);
2327 820496 : tree ptrtype;
2328 :
2329 820496 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2330 :
2331 820496 : STRIP_TYPE_NOPS (exp);
2332 :
2333 820496 : copy_warning (exp, orig_exp);
2334 :
2335 820496 : ptrtype = c_build_pointer_type (restype);
2336 :
2337 820496 : if (INDIRECT_REF_P (exp))
2338 2906 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2339 :
2340 : /* In C++ array compound literals are temporary objects unless they are
2341 : const or appear in namespace scope, so they are destroyed too soon
2342 : to use them for much of anything (c++/53220). */
2343 817590 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2344 : {
2345 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2346 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2347 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2348 : "converting an array compound literal to a pointer "
2349 : "leads to a dangling pointer in C++");
2350 : }
2351 :
2352 817590 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2353 817590 : return convert (ptrtype, adr);
2354 : }
2355 :
2356 : /* Convert the function expression EXP to a pointer. */
2357 : static tree
2358 50562266 : function_to_pointer_conversion (location_t loc, tree exp)
2359 : {
2360 50562266 : tree orig_exp = exp;
2361 :
2362 50562266 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2363 :
2364 50562266 : STRIP_TYPE_NOPS (exp);
2365 :
2366 50562266 : copy_warning (exp, orig_exp);
2367 :
2368 50562266 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2369 :
2370 : /* If the function is defined and known to not to require a non-local
2371 : context, make sure no trampoline is generated. */
2372 50562266 : if (TREE_CODE (exp) == FUNCTION_DECL
2373 50562266 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2374 16057111 : TREE_NO_TRAMPOLINE (exp2) = 1;
2375 :
2376 50562266 : return exp2;
2377 : }
2378 :
2379 : /* Mark EXP as read, not just set, for set but not used -Wunused
2380 : warning purposes. */
2381 :
2382 : void
2383 489234331 : mark_exp_read (tree exp)
2384 : {
2385 615528765 : switch (TREE_CODE (exp))
2386 : {
2387 203448639 : case VAR_DECL:
2388 203448639 : case PARM_DECL:
2389 203448639 : DECL_READ_P (exp) = 1;
2390 203448639 : break;
2391 19701789 : CASE_CONVERT:
2392 19701789 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2393 3800 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2394 : {
2395 : case PREINCREMENT_EXPR:
2396 : case PREDECREMENT_EXPR:
2397 : case POSTINCREMENT_EXPR:
2398 : case POSTDECREMENT_EXPR:
2399 : return;
2400 : default:
2401 : break;
2402 : }
2403 : /* FALLTHRU */
2404 123550674 : case ARRAY_REF:
2405 123550674 : case COMPONENT_REF:
2406 123550674 : case MODIFY_EXPR:
2407 123550674 : case REALPART_EXPR:
2408 123550674 : case IMAGPART_EXPR:
2409 123550674 : case ADDR_EXPR:
2410 123550674 : case VIEW_CONVERT_EXPR:
2411 123550674 : case PREINCREMENT_EXPR:
2412 123550674 : case PREDECREMENT_EXPR:
2413 123550674 : case POSTINCREMENT_EXPR:
2414 123550674 : case POSTDECREMENT_EXPR:
2415 123550674 : mark_exp_read (TREE_OPERAND (exp, 0));
2416 123550674 : break;
2417 236310 : case COMPOUND_EXPR:
2418 : /* Pattern match what build_atomic_assign produces with modifycode
2419 : NOP_EXPR. */
2420 236310 : if (VAR_P (TREE_OPERAND (exp, 1))
2421 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2422 263453 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2423 : {
2424 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2425 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2426 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2427 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2428 4410 : && TREE_CODE (t2) == CALL_EXPR)
2429 : {
2430 2200 : tree fndecl = get_callee_fndecl (t2);
2431 2200 : tree arg = NULL_TREE;
2432 2200 : if (fndecl
2433 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2434 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2435 4400 : && call_expr_nargs (t2) >= 2)
2436 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2437 : {
2438 130 : case BUILT_IN_ATOMIC_STORE:
2439 130 : arg = CALL_EXPR_ARG (t2, 1);
2440 130 : break;
2441 2070 : case BUILT_IN_ATOMIC_STORE_1:
2442 2070 : case BUILT_IN_ATOMIC_STORE_2:
2443 2070 : case BUILT_IN_ATOMIC_STORE_4:
2444 2070 : case BUILT_IN_ATOMIC_STORE_8:
2445 2070 : case BUILT_IN_ATOMIC_STORE_16:
2446 2070 : arg = CALL_EXPR_ARG (t2, 0);
2447 2070 : break;
2448 : default:
2449 : break;
2450 : }
2451 2200 : if (arg)
2452 : {
2453 2200 : STRIP_NOPS (arg);
2454 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2455 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2456 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2457 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2458 : }
2459 : }
2460 : }
2461 : /* FALLTHRU */
2462 2743217 : case C_MAYBE_CONST_EXPR:
2463 2743217 : mark_exp_read (TREE_OPERAND (exp, 1));
2464 2743217 : break;
2465 578 : case OMP_ARRAY_SECTION:
2466 578 : mark_exp_read (TREE_OPERAND (exp, 0));
2467 578 : if (TREE_OPERAND (exp, 1))
2468 384 : mark_exp_read (TREE_OPERAND (exp, 1));
2469 578 : if (TREE_OPERAND (exp, 2))
2470 543 : mark_exp_read (TREE_OPERAND (exp, 2));
2471 : break;
2472 : default:
2473 : break;
2474 : }
2475 : }
2476 :
2477 : /* Perform the default conversion of arrays and functions to pointers.
2478 : Return the result of converting EXP. For any other expression, just
2479 : return EXP.
2480 :
2481 : LOC is the location of the expression. */
2482 :
2483 : struct c_expr
2484 361926530 : default_function_array_conversion (location_t loc, struct c_expr exp)
2485 : {
2486 361926530 : tree orig_exp = exp.value;
2487 361926530 : tree type = TREE_TYPE (exp.value);
2488 361926530 : enum tree_code code = TREE_CODE (type);
2489 :
2490 361926530 : switch (code)
2491 : {
2492 : case ARRAY_TYPE:
2493 : {
2494 : bool not_lvalue = false;
2495 : bool lvalue_array_p;
2496 :
2497 773596 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2498 773596 : || CONVERT_EXPR_P (exp.value))
2499 773596 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2500 : {
2501 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2502 0 : not_lvalue = true;
2503 0 : exp.value = TREE_OPERAND (exp.value, 0);
2504 : }
2505 :
2506 773596 : copy_warning (exp.value, orig_exp);
2507 :
2508 773596 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2509 773596 : if (!flag_isoc99 && !lvalue_array_p)
2510 : {
2511 : /* Before C99, non-lvalue arrays do not decay to pointers.
2512 : Normally, using such an array would be invalid; but it can
2513 : be used correctly inside sizeof or as a statement expression.
2514 : Thus, do not give an error here; an error will result later. */
2515 109 : return exp;
2516 : }
2517 :
2518 773487 : exp.value = array_to_pointer_conversion (loc, exp.value);
2519 : }
2520 773487 : break;
2521 762126 : case FUNCTION_TYPE:
2522 762126 : exp.value = function_to_pointer_conversion (loc, exp.value);
2523 762126 : break;
2524 : default:
2525 : break;
2526 : }
2527 :
2528 361926421 : return exp;
2529 : }
2530 :
2531 : struct c_expr
2532 947439 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2533 : {
2534 947439 : mark_exp_read (exp.value);
2535 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2536 947439 : if (TREE_CODE (exp.value) == COMPONENT_REF
2537 947439 : && handle_counted_by_p (exp.value))
2538 66620 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2539 947439 : return default_function_array_conversion (loc, exp);
2540 : }
2541 :
2542 : /* Return whether EXPR should be treated as an atomic lvalue for the
2543 : purposes of load and store handling. */
2544 :
2545 : static bool
2546 363022244 : really_atomic_lvalue (tree expr)
2547 : {
2548 363022244 : if (error_operand_p (expr))
2549 : return false;
2550 363014766 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2551 : return false;
2552 57393 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2553 : return false;
2554 57390 : if (!lvalue_p (expr))
2555 : return false;
2556 :
2557 : /* Ignore _Atomic on register variables, since their addresses can't
2558 : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2559 : sequences wouldn't work. Ignore _Atomic on structures containing
2560 : bit-fields, since accessing elements of atomic structures or
2561 : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2562 : it's undefined at translation time or execution time, and the
2563 : normal atomic sequences again wouldn't work. */
2564 57532 : while (handled_component_p (expr))
2565 : {
2566 144 : if (TREE_CODE (expr) == COMPONENT_REF
2567 144 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2568 : return false;
2569 144 : expr = TREE_OPERAND (expr, 0);
2570 : }
2571 57388 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2572 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2573 112572 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2574 : return false;
2575 : return true;
2576 : }
2577 :
2578 : /* If EXPR is a named constant (C23) derived from a constexpr variable
2579 : - that is, a reference to such a variable, or a member extracted by
2580 : a sequence of structure and union (but not array) member accesses
2581 : (where union member accesses must access the same member as
2582 : initialized) - then return the corresponding initializer;
2583 : otherwise, return NULL_TREE. */
2584 :
2585 : static tree
2586 359834470 : maybe_get_constexpr_init (tree expr)
2587 : {
2588 359834470 : tree decl = NULL_TREE;
2589 359834470 : if (TREE_CODE (expr) == VAR_DECL)
2590 : decl = expr;
2591 348128966 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2592 821630 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2593 821630 : if (decl
2594 12527134 : && C_DECL_DECLARED_CONSTEXPR (decl)
2595 370 : && DECL_INITIAL (decl) != NULL_TREE
2596 822000 : && !error_operand_p (DECL_INITIAL (decl)))
2597 370 : return DECL_INITIAL (decl);
2598 359834100 : if (TREE_CODE (expr) != COMPONENT_REF)
2599 : return NULL_TREE;
2600 872236 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2601 872236 : if (inner == NULL_TREE)
2602 : return NULL_TREE;
2603 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2604 47 : && !error_operand_p (inner)
2605 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2606 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2607 47 : inner = TREE_OPERAND (inner, 0);
2608 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2609 : return NULL_TREE;
2610 126 : tree field = TREE_OPERAND (expr, 1);
2611 126 : unsigned HOST_WIDE_INT cidx;
2612 126 : tree cfield, cvalue;
2613 126 : bool have_other_init = false;
2614 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2615 : {
2616 250 : if (cfield == field)
2617 : return cvalue;
2618 140 : have_other_init = true;
2619 : }
2620 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2621 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2622 : return NULL_TREE;
2623 : /* Return a default initializer. */
2624 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2625 5 : return build_constructor (TREE_TYPE (expr), NULL);
2626 8 : return build_zero_cst (TREE_TYPE (expr));
2627 : }
2628 :
2629 : /* Helper function for convert_lvalue_to_rvalue called via
2630 : walk_tree_without_duplicates. Find DATA inside of the expression. */
2631 :
2632 : static tree
2633 230295 : c_find_var_r (tree *tp, int *walk_subtrees, void *data)
2634 : {
2635 230295 : if (TYPE_P (*tp))
2636 0 : *walk_subtrees = 0;
2637 230295 : else if (*tp == (tree) data)
2638 : return *tp;
2639 : return NULL_TREE;
2640 : }
2641 :
2642 : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2643 : including converting functions and arrays to pointers if CONVERT_P.
2644 : If READ_P, also mark the expression as having been read. If
2645 : FOR_INIT, constexpr expressions of structure and union type should
2646 : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2647 : constexpr scalars (including elements of structures and unions) are
2648 : replaced by their initializers. */
2649 :
2650 : struct c_expr
2651 359149883 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2652 : bool convert_p, bool read_p, bool for_init)
2653 : {
2654 359149883 : bool force_non_npc = false;
2655 359149883 : if (read_p)
2656 315141637 : mark_exp_read (exp.value);
2657 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2658 315141637 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2659 781080 : && handle_counted_by_p (exp.value))
2660 781051 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2661 :
2662 359149883 : if (convert_p)
2663 359136653 : exp = default_function_array_conversion (loc, exp);
2664 359149883 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2665 359149883 : || (flag_isoc2y
2666 1359 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2667 356669970 : exp.value = require_complete_type (loc, exp.value);
2668 359149883 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2669 : {
2670 358962234 : tree init = maybe_get_constexpr_init (exp.value);
2671 358962234 : if (init != NULL_TREE)
2672 : {
2673 : /* A named constant of pointer type or type nullptr_t is not
2674 : a null pointer constant even if the initializer is
2675 : one. */
2676 367 : if (TREE_CODE (init) == INTEGER_CST
2677 232 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2678 393 : && integer_zerop (init))
2679 : force_non_npc = true;
2680 : exp.value = init;
2681 : }
2682 : }
2683 359149883 : if (really_atomic_lvalue (exp.value))
2684 : {
2685 26938 : vec<tree, va_gc> *params;
2686 26938 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2687 26938 : tree expr_type = TREE_TYPE (exp.value);
2688 26938 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2689 26938 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2690 :
2691 26938 : gcc_assert (TYPE_ATOMIC (expr_type));
2692 :
2693 : /* Expansion of a generic atomic load may require an addition
2694 : element, so allocate enough to prevent a resize. */
2695 26938 : vec_alloc (params, 4);
2696 :
2697 : /* Remove the qualifiers for the rest of the expressions and
2698 : create the VAL temp variable to hold the RHS. */
2699 26938 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2700 26938 : tmp = create_tmp_var_raw (nonatomic_type);
2701 26938 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2702 26938 : TREE_ADDRESSABLE (tmp) = 1;
2703 : /* Do not disable warnings for TMP even though it's artificial.
2704 : -Winvalid-memory-model depends on it. */
2705 :
2706 : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2707 26938 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2708 26938 : params->quick_push (expr_addr);
2709 26938 : params->quick_push (tmp_addr);
2710 26938 : params->quick_push (seq_cst);
2711 26938 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2712 :
2713 : /* EXPR is always read. */
2714 26938 : mark_exp_read (exp.value);
2715 :
2716 : /* Optimize the common case where c_build_function_call_vec
2717 : immediately folds __atomic_load (&expr, &tmp, SEQ_CST); into
2718 : tmp = __atomic_load_<N> (&expr, SEQ_CST);
2719 : In that case tmp is not addressable and can be initialized
2720 : fully by the rhs of the MODIFY_EXPR. */
2721 26938 : tree tem = func_call;
2722 26938 : if (CONVERT_EXPR_P (tem) && VOID_TYPE_P (TREE_TYPE (tem)))
2723 : {
2724 25683 : tem = TREE_OPERAND (tem, 0);
2725 25683 : if (TREE_CODE (tem) == MODIFY_EXPR
2726 25683 : && TREE_OPERAND (tem, 0) == tmp
2727 51366 : && !walk_tree_without_duplicates (&TREE_OPERAND (tem, 1),
2728 : c_find_var_r, tmp))
2729 : {
2730 25683 : TREE_ADDRESSABLE (tmp) = 0;
2731 25683 : func_call = TREE_OPERAND (tem, 1);
2732 : }
2733 : }
2734 :
2735 : /* Return tmp which contains the value loaded. */
2736 26938 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2737 : NULL_TREE, NULL_TREE);
2738 : }
2739 359136653 : if (convert_p && !error_operand_p (exp.value)
2740 718279102 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2741 359129110 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2742 359149883 : if (force_non_npc)
2743 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2744 :
2745 359149883 : {
2746 359149883 : tree false_value, true_value;
2747 359136653 : if (convert_p && !error_operand_p (exp.value)
2748 718279102 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2749 : &false_value, &true_value))
2750 : {
2751 9721 : tree t = save_expr (exp.value);
2752 :
2753 9721 : mark_exp_read (exp.value);
2754 :
2755 9721 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2756 9721 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2757 9721 : expr = build_compound_expr (loc, expr, boolean_true_node);
2758 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2759 : fold_build2_loc (loc, NE_EXPR,
2760 : boolean_type_node,
2761 : t, true_value),
2762 : expr, boolean_true_node);
2763 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2764 : fold_build2_loc (loc, NE_EXPR,
2765 : boolean_type_node,
2766 : t, false_value),
2767 : expr, boolean_false_node);
2768 :
2769 9721 : exp.value = expr;
2770 : }
2771 : }
2772 :
2773 359149883 : return exp;
2774 : }
2775 :
2776 : /* Wrapper for the overload above, same arguments but for tree rather than
2777 : c_expr. This is important for hardbools to decay to bools. */
2778 :
2779 : static inline tree
2780 574650 : convert_lvalue_to_rvalue (location_t loc, tree val,
2781 : bool convert_p, bool read_p, bool for_init = false)
2782 : {
2783 574650 : struct c_expr expr;
2784 574650 : memset (&expr, 0, sizeof (expr));
2785 574650 : expr.value = val;
2786 574650 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2787 574650 : return expr.value;
2788 : }
2789 :
2790 : /* EXP is an expression of integer type. Apply the integer promotions
2791 : to it and return the promoted value. */
2792 :
2793 : tree
2794 76377239 : perform_integral_promotions (tree exp)
2795 : {
2796 76377239 : tree type = TREE_TYPE (exp);
2797 76377239 : enum tree_code code = TREE_CODE (type);
2798 :
2799 76377239 : gcc_assert (INTEGRAL_TYPE_P (type));
2800 :
2801 : /* Convert enums to the result of applying the integer promotions to
2802 : their underlying type. */
2803 76377239 : if (code == ENUMERAL_TYPE)
2804 : {
2805 649936 : type = ENUM_UNDERLYING_TYPE (type);
2806 649936 : if (c_promoting_integer_type_p (type))
2807 : {
2808 94 : if (TYPE_UNSIGNED (type)
2809 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2810 0 : type = unsigned_type_node;
2811 : else
2812 94 : type = integer_type_node;
2813 : }
2814 :
2815 649936 : return convert (type, exp);
2816 : }
2817 :
2818 : /* ??? This should no longer be needed now bit-fields have their
2819 : proper types. */
2820 75727303 : if (TREE_CODE (exp) == COMPONENT_REF
2821 75727303 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2822 : {
2823 62179 : if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2824 : == BITINT_TYPE)
2825 454 : return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2826 : /* If it's thinner than an int, promote it like a
2827 : c_promoting_integer_type_p, otherwise leave it alone. */
2828 61725 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2829 61725 : TYPE_PRECISION (integer_type_node)) < 0)
2830 53463 : return convert (integer_type_node, exp);
2831 : }
2832 :
2833 75673386 : if (c_promoting_integer_type_p (type))
2834 : {
2835 : /* Preserve unsignedness if not really getting any wider. */
2836 643478 : if (TYPE_UNSIGNED (type)
2837 643478 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2838 0 : return convert (unsigned_type_node, exp);
2839 :
2840 643478 : return convert (integer_type_node, exp);
2841 : }
2842 :
2843 : return exp;
2844 : }
2845 :
2846 :
2847 : /* Perform default promotions for C data used in expressions.
2848 : Enumeral types or short or char are converted to int.
2849 : In addition, manifest constants symbols are replaced by their values. */
2850 :
2851 : tree
2852 85897811 : default_conversion (tree exp)
2853 : {
2854 85897811 : tree orig_exp;
2855 85897811 : tree type = TREE_TYPE (exp);
2856 85897811 : enum tree_code code = TREE_CODE (type);
2857 85897811 : tree promoted_type;
2858 :
2859 85897811 : mark_exp_read (exp);
2860 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2861 85897811 : if (TREE_CODE (exp) == COMPONENT_REF
2862 85897811 : && handle_counted_by_p (exp))
2863 598354 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2864 :
2865 : /* Functions and arrays have been converted during parsing. */
2866 85897811 : gcc_assert (code != FUNCTION_TYPE);
2867 85897811 : if (code == ARRAY_TYPE)
2868 : return exp;
2869 :
2870 : /* Constants can be used directly unless they're not loadable. */
2871 85897652 : if (TREE_CODE (exp) == CONST_DECL)
2872 0 : exp = DECL_INITIAL (exp);
2873 :
2874 : /* Strip no-op conversions. */
2875 85897652 : orig_exp = exp;
2876 86269049 : STRIP_TYPE_NOPS (exp);
2877 :
2878 85897652 : copy_warning (exp, orig_exp);
2879 :
2880 85897652 : if (code == VOID_TYPE)
2881 : {
2882 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2883 : "void value not ignored as it ought to be");
2884 2 : return error_mark_node;
2885 : }
2886 :
2887 85897650 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2888 85897650 : if (exp == error_mark_node)
2889 : return error_mark_node;
2890 :
2891 85896747 : promoted_type = targetm.promoted_type (type);
2892 85896747 : if (promoted_type)
2893 0 : return convert (promoted_type, exp);
2894 :
2895 85896747 : if (INTEGRAL_TYPE_P (type))
2896 75380070 : return perform_integral_promotions (exp);
2897 :
2898 : return exp;
2899 : }
2900 :
2901 : /* Look up COMPONENT in a structure or union TYPE.
2902 :
2903 : If the component name is not found, returns NULL_TREE. Otherwise,
2904 : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2905 : stepping down the chain to the component, which is in the last
2906 : TREE_VALUE of the list. Normally the list is of length one, but if
2907 : the component is embedded within (nested) anonymous structures or
2908 : unions, the list steps down the chain to the component. */
2909 :
2910 : tree
2911 2314988 : lookup_field (const_tree type, tree component)
2912 : {
2913 2314988 : tree field;
2914 :
2915 : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2916 : to the field elements. Use a binary search on this array to quickly
2917 : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2918 : will always be set for structures which have many elements.
2919 :
2920 : Duplicate field checking replaces duplicates with NULL_TREE so
2921 : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2922 : case just iterate using DECL_CHAIN. */
2923 :
2924 2614916 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2925 2614916 : && !seen_error ())
2926 : {
2927 299907 : int bot, top, half;
2928 299907 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2929 :
2930 299907 : field = TYPE_FIELDS (type);
2931 299907 : bot = 0;
2932 299907 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2933 1378291 : while (top - bot > 1)
2934 : {
2935 1349682 : half = (top - bot + 1) >> 1;
2936 1349682 : field = field_array[bot+half];
2937 :
2938 1349682 : if (DECL_NAME (field) == NULL_TREE)
2939 : {
2940 : /* Step through all anon unions in linear fashion. */
2941 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2942 : {
2943 0 : field = field_array[bot++];
2944 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2945 : {
2946 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2947 :
2948 0 : if (anon)
2949 0 : return tree_cons (NULL_TREE, field, anon);
2950 :
2951 : /* The Plan 9 compiler permits referring
2952 : directly to an anonymous struct/union field
2953 : using a typedef name. */
2954 0 : if (flag_plan9_extensions
2955 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2956 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2957 : == TYPE_DECL)
2958 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2959 : == component))
2960 : break;
2961 : }
2962 : }
2963 :
2964 : /* Entire record is only anon unions. */
2965 0 : if (bot > top)
2966 : return NULL_TREE;
2967 :
2968 : /* Restart the binary search, with new lower bound. */
2969 0 : continue;
2970 0 : }
2971 :
2972 1349682 : if (DECL_NAME (field) == component)
2973 : break;
2974 1078384 : if (DECL_NAME (field) < component)
2975 : bot += half;
2976 : else
2977 892665 : top = bot + half;
2978 : }
2979 :
2980 299907 : if (DECL_NAME (field_array[bot]) == component)
2981 : field = field_array[bot];
2982 271298 : else if (DECL_NAME (field) != component)
2983 : return NULL_TREE;
2984 : }
2985 : else
2986 : {
2987 5248457 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2988 : {
2989 5239164 : if (DECL_NAME (field) == NULL_TREE
2990 5239164 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2991 : {
2992 12196 : tree anon = lookup_field (TREE_TYPE (field), component);
2993 :
2994 12196 : if (anon)
2995 2998 : return tree_cons (NULL_TREE, field, anon);
2996 :
2997 : /* The Plan 9 compiler permits referring directly to an
2998 : anonymous struct/union field using a typedef
2999 : name. */
3000 9198 : if (flag_plan9_extensions
3001 174 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3002 172 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
3003 9260 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3004 : == component))
3005 : break;
3006 : }
3007 :
3008 5236162 : if (DECL_NAME (field) == component)
3009 : break;
3010 : }
3011 :
3012 2012083 : if (field == NULL_TREE)
3013 : return NULL_TREE;
3014 : }
3015 :
3016 2302697 : return tree_cons (NULL_TREE, field, NULL_TREE);
3017 : }
3018 :
3019 : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
3020 :
3021 : static void
3022 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
3023 : vec<tree> *candidates)
3024 : {
3025 101 : tree field;
3026 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3027 : {
3028 157 : if (DECL_NAME (field) == NULL_TREE
3029 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3030 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
3031 : candidates);
3032 :
3033 157 : if (DECL_NAME (field))
3034 111 : candidates->safe_push (DECL_NAME (field));
3035 : }
3036 101 : }
3037 :
3038 : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
3039 : rather than returning a TREE_LIST for an exact match. */
3040 :
3041 : static tree
3042 71 : lookup_field_fuzzy (tree type, tree component)
3043 : {
3044 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
3045 :
3046 : /* First, gather a list of candidates. */
3047 71 : auto_vec <tree> candidates;
3048 :
3049 71 : lookup_field_fuzzy_find_candidates (type, component,
3050 : &candidates);
3051 :
3052 71 : return find_closest_identifier (component, &candidates);
3053 71 : }
3054 :
3055 : /* Support function for build_component_ref's error-handling.
3056 :
3057 : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
3058 : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
3059 :
3060 : static bool
3061 72 : should_suggest_deref_p (tree datum_type)
3062 : {
3063 : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
3064 : allows "." for ptrs; we could be handling a failed attempt
3065 : to access a property. */
3066 72 : if (c_dialect_objc ())
3067 : return false;
3068 :
3069 : /* Only suggest it for pointers... */
3070 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3071 : return false;
3072 :
3073 : /* ...to structs/unions. */
3074 8 : tree underlying_type = TREE_TYPE (datum_type);
3075 8 : enum tree_code code = TREE_CODE (underlying_type);
3076 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3077 : return true;
3078 : else
3079 1 : return false;
3080 : }
3081 :
3082 : /* Give a component ref REF, decide whether we should handle its counted_by
3083 : attribute based on its context:
3084 : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3085 :
3086 : static bool
3087 2271065 : handle_counted_by_p (tree ref)
3088 : {
3089 2271065 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3090 2271065 : tree datum = TREE_OPERAND (ref, 0);
3091 : /* If the component_ref is build for a offsetof, i.e., the datum
3092 : of the component_ref is a indirect_ref of null_pointer_node,
3093 : we should not generate call to .ACCESS_WITH_SIZE. */
3094 2271065 : if (TREE_CODE (datum) == INDIRECT_REF
3095 2271065 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3096 : return false;
3097 2270735 : if (in_typeof || in_alignof)
3098 8786 : return false;
3099 : return true;
3100 : }
3101 :
3102 : /* Given a component ref REF, if there is a counted_by attribute attached,
3103 : issue error when the element_type is a structure or union including a
3104 : flexible array member. */
3105 :
3106 : static void
3107 2271045 : check_counted_by_attribute (location_t loc, tree ref)
3108 : {
3109 2271045 : tree subdatum = TREE_OPERAND (ref, 1);
3110 2271045 : tree sub_type = TREE_TYPE (subdatum);
3111 :
3112 2271045 : if (!c_flexible_array_member_type_p (sub_type)
3113 2271045 : && TREE_CODE (sub_type) != POINTER_TYPE)
3114 : return;
3115 :
3116 387005 : tree element_type = TREE_TYPE (sub_type);
3117 :
3118 387005 : tree attr_counted_by = lookup_attribute ("counted_by",
3119 387005 : DECL_ATTRIBUTES (subdatum));
3120 387005 : if (attr_counted_by)
3121 : {
3122 : /* Issue error when the element_type is a structure or
3123 : union including a flexible array member. */
3124 859 : if (RECORD_OR_UNION_TYPE_P (element_type)
3125 859 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3126 : {
3127 2 : error_at (loc,
3128 : "%<counted_by%> attribute is not allowed for a pointer to"
3129 : " structure or union with flexible array member");
3130 2 : return;
3131 : }
3132 : }
3133 : }
3134 :
3135 : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3136 : to the object that represents its counted_by per the attribute
3137 : counted_by attached to this field if it's a flexible array member
3138 : or a pointer field, otherwise return NULL_TREE.
3139 : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3140 : For example, if:
3141 :
3142 : struct P {
3143 : int k;
3144 : int x[] __attribute__ ((counted_by (k)));
3145 : } *p;
3146 :
3147 : for:
3148 : p->x
3149 :
3150 : the ref to the object that represents its element count will be:
3151 :
3152 : &(p->k)
3153 : */
3154 :
3155 : static tree
3156 500078 : build_counted_by_ref (tree datum, tree subdatum,
3157 : tree *counted_by_type)
3158 : {
3159 500078 : tree sub_type = TREE_TYPE (subdatum);
3160 500078 : if (!c_flexible_array_member_type_p (sub_type)
3161 500078 : && TREE_CODE (sub_type) != POINTER_TYPE)
3162 : return NULL_TREE;
3163 :
3164 500078 : tree attr_counted_by = lookup_attribute ("counted_by",
3165 500078 : DECL_ATTRIBUTES (subdatum));
3166 500078 : if (!attr_counted_by)
3167 : return NULL_TREE;
3168 :
3169 632 : tree counted_by_ref = NULL_TREE;
3170 632 : *counted_by_type = NULL_TREE;
3171 :
3172 632 : tree type = TREE_TYPE (datum);
3173 :
3174 : /* If the type of the containing structure is an anonymous struct/union,
3175 : and this anonymous struct/union is not a root type, get the first
3176 : outer named structure/union type. */
3177 632 : while (TREE_CODE (datum) == COMPONENT_REF
3178 35 : && c_type_tag (type) == NULL_TREE
3179 702 : && DECL_NAME (TREE_OPERAND (datum, 1)) == NULL_TREE)
3180 : {
3181 35 : datum = TREE_OPERAND (datum, 0);
3182 35 : type = TREE_TYPE (datum);
3183 : }
3184 :
3185 632 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3186 632 : tree counted_by_field = lookup_field (type, field_id);
3187 632 : gcc_assert (counted_by_field);
3188 :
3189 1044 : tree counted_by_subdatum;
3190 1044 : do
3191 : {
3192 1044 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3193 : /* Get the TYPE of the counted_by field. */
3194 1044 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3195 :
3196 1044 : counted_by_ref
3197 1044 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3198 : datum, counted_by_subdatum, NULL_TREE);
3199 :
3200 1044 : datum = counted_by_ref;
3201 1044 : counted_by_field = TREE_CHAIN (counted_by_field);
3202 : }
3203 1044 : while (counted_by_field);
3204 :
3205 632 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3206 632 : return counted_by_ref;
3207 : }
3208 :
3209 : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3210 : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3211 : call to the internal function .ACCESS_WITH_SIZE.
3212 :
3213 : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3214 : the internal function .ACCESS_WITH_SIZE.
3215 :
3216 : REF
3217 :
3218 : to:
3219 :
3220 : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3221 : TYPE_SIZE_UNIT for element)
3222 :
3223 : NOTE: The return type of this function is the POINTER type pointing
3224 : to the original flexible array type. Then the type of the INDIRECT_REF
3225 : is the original flexible array type.
3226 : The type of the first argument of this function is a POINTER type
3227 : to the original flexible array type.
3228 :
3229 : B: For pointers with counted_by, generate a call to the internal function
3230 : .ACCESS_WITH_SIZE.
3231 :
3232 : REF
3233 :
3234 : to:
3235 :
3236 : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3237 : TYPE_SIZE_UNIT for element)
3238 :
3239 : NOTE: The return type of this function is the original pointer type.
3240 : The type of the first argument of this function is the original
3241 : pointer type.
3242 :
3243 : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3244 : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3245 :
3246 : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3247 : of the array.
3248 :
3249 : */
3250 : static tree
3251 632 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3252 : tree counted_by_ref,
3253 : tree counted_by_type)
3254 : {
3255 632 : gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))
3256 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3257 :
3258 632 : bool is_fam = c_flexible_array_member_type_p (TREE_TYPE (ref));
3259 :
3260 : /* The result type of the call is a pointer to the flexible array type;
3261 : or is the original ponter type to the pointer field with counted_by. */
3262 632 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3263 431 : : TREE_TYPE (ref);
3264 :
3265 632 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3266 632 : tree element_size = VOID_TYPE_P (element_type)
3267 632 : ? build_one_cst (size_type_node)
3268 624 : : TYPE_SIZE_UNIT (element_type);
3269 :
3270 632 : tree first_param = is_fam
3271 632 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3272 : false, NULL)
3273 431 : : c_fully_fold (ref, false, NULL);
3274 632 : tree second_param
3275 632 : = c_fully_fold (counted_by_ref, false, NULL);
3276 632 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3277 :
3278 632 : tree call
3279 632 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3280 : result_type, 4,
3281 : first_param,
3282 : second_param,
3283 : third_param,
3284 : element_size);
3285 :
3286 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3287 632 : if (is_fam)
3288 201 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3289 632 : SET_EXPR_LOCATION (call, loc);
3290 632 : return call;
3291 : }
3292 :
3293 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3294 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3295 : function .ACCESS_WITH_SIZE.
3296 : Otherwise, return the ref itself. */
3297 :
3298 : tree
3299 2262029 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3300 : {
3301 2262029 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3302 2262029 : tree datum = TREE_OPERAND (ref, 0);
3303 2262029 : tree subdatum = TREE_OPERAND (ref, 1);
3304 2262029 : tree counted_by_type = NULL_TREE;
3305 :
3306 2262029 : if (!(c_flexible_array_member_type_p (TREE_TYPE (ref))
3307 2191575 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3308 : return ref;
3309 :
3310 500078 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3311 : &counted_by_type);
3312 500078 : if (counted_by_ref)
3313 632 : ref = build_access_with_size_for_counted_by (loc, ref,
3314 : counted_by_ref,
3315 : counted_by_type);
3316 : return ref;
3317 : }
3318 :
3319 : /* Make an expression to refer to the COMPONENT field of structure or
3320 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3321 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3322 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3323 : it is from -> operator.
3324 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3325 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3326 :
3327 : tree
3328 2268935 : build_component_ref (location_t loc, tree datum, tree component,
3329 : location_t component_loc, location_t arrow_loc)
3330 : {
3331 2268935 : tree type = TREE_TYPE (datum);
3332 2268935 : enum tree_code code = TREE_CODE (type);
3333 2268935 : tree field = NULL;
3334 2268935 : tree ref;
3335 2268935 : bool datum_lvalue = lvalue_p (datum);
3336 :
3337 2268935 : if (!objc_is_public (datum, component))
3338 0 : return error_mark_node;
3339 :
3340 : /* Detect Objective-C property syntax object.property. */
3341 2268935 : if (c_dialect_objc ()
3342 2268935 : && (ref = objc_maybe_build_component_ref (datum, component)))
3343 : return ref;
3344 :
3345 : /* See if there is a field or component with name COMPONENT. */
3346 :
3347 2268935 : if (code == RECORD_TYPE || code == UNION_TYPE)
3348 : {
3349 2268863 : if (!COMPLETE_TYPE_P (type))
3350 : {
3351 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3352 19 : return error_mark_node;
3353 : }
3354 :
3355 2268844 : field = lookup_field (type, component);
3356 :
3357 2268844 : if (!field)
3358 : {
3359 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3360 63 : if (guessed_id)
3361 : {
3362 : /* Attempt to provide a fixit replacement hint, if
3363 : we have a valid range for the component. */
3364 0 : location_t reported_loc
3365 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3366 26 : gcc_rich_location rich_loc (reported_loc);
3367 26 : if (component_loc != UNKNOWN_LOCATION)
3368 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3369 26 : error_at (&rich_loc,
3370 : "%qT has no member named %qE; did you mean %qE?",
3371 : type, component, guessed_id);
3372 26 : }
3373 : else
3374 37 : error_at (loc, "%qT has no member named %qE", type, component);
3375 63 : return error_mark_node;
3376 : }
3377 :
3378 : /* Accessing elements of atomic structures or unions is undefined
3379 : behavior (C11 6.5.2.3#5). */
3380 2268781 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3381 : {
3382 18 : if (code == RECORD_TYPE)
3383 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3384 : "structure %qE", component, datum);
3385 : else
3386 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3387 : "union %qE", component, datum);
3388 : }
3389 :
3390 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3391 : This might be better solved in future the way the C++ front
3392 : end does it - by giving the anonymous entities each a
3393 : separate name and type, and then have build_component_ref
3394 : recursively call itself. We can't do that here. */
3395 2271045 : do
3396 : {
3397 2271045 : tree subdatum = TREE_VALUE (field);
3398 2271045 : int quals;
3399 2271045 : tree subtype;
3400 2271045 : bool use_datum_quals;
3401 :
3402 2271045 : if (TREE_TYPE (subdatum) == error_mark_node)
3403 : return error_mark_node;
3404 :
3405 : /* If this is an rvalue, it does not have qualifiers in C
3406 : standard terms and we must avoid propagating such
3407 : qualifiers down to a non-lvalue array that is then
3408 : converted to a pointer. */
3409 4542090 : use_datum_quals = (datum_lvalue
3410 2271045 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3411 :
3412 2271045 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3413 2271045 : if (use_datum_quals)
3414 2270783 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3415 2271045 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3416 :
3417 2271045 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3418 : NULL_TREE);
3419 2271045 : SET_EXPR_LOCATION (ref, loc);
3420 :
3421 2271045 : check_counted_by_attribute (loc, ref);
3422 :
3423 2271045 : if (TREE_READONLY (subdatum)
3424 2271045 : || (use_datum_quals && TREE_READONLY (datum)))
3425 30592 : TREE_READONLY (ref) = 1;
3426 2271045 : if (TREE_THIS_VOLATILE (subdatum)
3427 2270051 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3428 2428 : TREE_THIS_VOLATILE (ref) = 1;
3429 :
3430 2271045 : if (TREE_UNAVAILABLE (subdatum))
3431 10 : error_unavailable_use (subdatum, NULL_TREE);
3432 2271035 : else if (TREE_DEPRECATED (subdatum))
3433 16 : warn_deprecated_use (subdatum, NULL_TREE);
3434 :
3435 2271045 : datum = ref;
3436 :
3437 2271045 : field = TREE_CHAIN (field);
3438 : }
3439 2271045 : while (field);
3440 :
3441 : return ref;
3442 : }
3443 72 : else if (should_suggest_deref_p (type))
3444 : {
3445 : /* Special-case the error message for "ptr.field" for the case
3446 : where the user has confused "." vs "->". */
3447 7 : rich_location richloc (line_table, loc);
3448 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3449 : {
3450 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3451 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3452 2 : error_at (&richloc,
3453 : "%qE is a pointer to pointer; did you mean to dereference "
3454 : "it before applying %<->%> to it?",
3455 2 : TREE_OPERAND (datum, 0));
3456 : }
3457 : else
3458 : {
3459 : /* "loc" should be the "." token. */
3460 5 : richloc.add_fixit_replace ("->");
3461 5 : error_at (&richloc,
3462 : "%qE is a pointer; did you mean to use %<->%>?",
3463 : datum);
3464 : }
3465 7 : return error_mark_node;
3466 7 : }
3467 65 : else if (code != ERROR_MARK)
3468 1 : error_at (loc,
3469 : "request for member %qE in something not a structure or union",
3470 : component);
3471 :
3472 65 : return error_mark_node;
3473 : }
3474 :
3475 : /* Given an expression PTR for a pointer, return an expression
3476 : for the value pointed to.
3477 : ERRORSTRING is the name of the operator to appear in error messages.
3478 :
3479 : LOC is the location to use for the generated tree. */
3480 :
3481 : tree
3482 2746591 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3483 : {
3484 2746591 : tree pointer = default_conversion (ptr);
3485 2746591 : tree type = TREE_TYPE (pointer);
3486 2746591 : tree ref;
3487 :
3488 2746591 : if (TREE_CODE (type) == POINTER_TYPE)
3489 : {
3490 2746238 : if (CONVERT_EXPR_P (pointer)
3491 1966095 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3492 : {
3493 : /* If a warning is issued, mark it to avoid duplicates from
3494 : the backend. This only needs to be done at
3495 : warn_strict_aliasing > 2. */
3496 780143 : if (warn_strict_aliasing > 2)
3497 334865 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3498 334865 : type, TREE_OPERAND (pointer, 0)))
3499 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3500 : }
3501 :
3502 2746238 : if (TREE_CODE (pointer) == ADDR_EXPR
3503 2746238 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3504 67430 : == TREE_TYPE (type)))
3505 : {
3506 67421 : ref = TREE_OPERAND (pointer, 0);
3507 67421 : protected_set_expr_location (ref, loc);
3508 67421 : return ref;
3509 : }
3510 : else
3511 : {
3512 2678817 : tree t = TREE_TYPE (type);
3513 :
3514 2678817 : ref = build1 (INDIRECT_REF, t, pointer);
3515 :
3516 2678817 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3517 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3518 :
3519 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3520 : so that we get the proper error message if the result is used
3521 : to assign to. Also, &* is supposed to be a no-op.
3522 : And ANSI C seems to specify that the type of the result
3523 : should be the const type. */
3524 : /* A de-reference of a pointer to const is not a const. It is valid
3525 : to change it via some other pointer. */
3526 2678817 : TREE_READONLY (ref) = TYPE_READONLY (t);
3527 2678817 : TREE_SIDE_EFFECTS (ref)
3528 2678817 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3529 2678817 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3530 2678817 : protected_set_expr_location (ref, loc);
3531 2678817 : return ref;
3532 : }
3533 : }
3534 353 : else if (TREE_CODE (pointer) != ERROR_MARK)
3535 278 : invalid_indirection_error (loc, type, errstring);
3536 :
3537 353 : return error_mark_node;
3538 : }
3539 :
3540 : /* This handles expressions of the form "a[i]", which denotes
3541 : an array reference.
3542 :
3543 : This is logically equivalent in C to *(a+i), but we may do it differently.
3544 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3545 : This avoids forcing the array out of registers, and can work on
3546 : arrays that are not lvalues (for example, members of structures returned
3547 : by functions).
3548 :
3549 : For vector types, allow vector[i] but not i[vector], and create
3550 : *(((type*)&vectortype) + i) for the expression.
3551 :
3552 : LOC is the location to use for the returned expression. */
3553 :
3554 : tree
3555 3467542 : build_array_ref (location_t loc, tree array, tree index)
3556 : {
3557 3467542 : tree ret;
3558 3467542 : bool swapped = false;
3559 3467542 : if (TREE_TYPE (array) == error_mark_node
3560 3467542 : || TREE_TYPE (index) == error_mark_node)
3561 : return error_mark_node;
3562 :
3563 3467434 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3564 1993494 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3565 : /* Allow vector[index] but not index[vector]. */
3566 4489525 : && !gnu_vector_type_p (TREE_TYPE (array)))
3567 : {
3568 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3569 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3570 : {
3571 2 : error_at (loc,
3572 : "subscripted value is neither array nor pointer nor vector");
3573 :
3574 2 : return error_mark_node;
3575 : }
3576 192 : std::swap (array, index);
3577 192 : swapped = true;
3578 : }
3579 :
3580 3467432 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3581 : {
3582 2 : error_at (loc, "array subscript is not an integer");
3583 2 : return error_mark_node;
3584 : }
3585 :
3586 3467430 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3587 : {
3588 10 : error_at (loc, "subscripted value is pointer to function");
3589 10 : return error_mark_node;
3590 : }
3591 :
3592 : /* ??? Existing practice has been to warn only when the char
3593 : index is syntactically the index, not for char[array]. */
3594 3467420 : if (!swapped)
3595 3467234 : warn_array_subscript_with_type_char (loc, index);
3596 :
3597 : /* Apply default promotions *after* noticing character types. */
3598 3467420 : index = default_conversion (index);
3599 3467420 : if (index == error_mark_node)
3600 : return error_mark_node;
3601 :
3602 3467419 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3603 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3604 :
3605 3467419 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3606 3467419 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3607 :
3608 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3609 3467419 : if (TREE_CODE (array) == COMPONENT_REF
3610 3467419 : && handle_counted_by_p (array))
3611 815924 : array = handle_counted_by_for_component_ref (loc, array);
3612 :
3613 3467419 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3614 : {
3615 2495844 : tree rval, type;
3616 :
3617 : /* An array that is indexed by a non-constant
3618 : cannot be stored in a register; we must be able to do
3619 : address arithmetic on its address.
3620 : Likewise an array of elements of variable size. */
3621 2495844 : if (TREE_CODE (index) != INTEGER_CST
3622 2495844 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3623 2022308 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3624 : {
3625 474276 : if (!c_mark_addressable (array, true, true))
3626 1 : return error_mark_node;
3627 : }
3628 : /* An array that is indexed by a constant value which is not within
3629 : the array bounds cannot be stored in a register either; because we
3630 : would get a crash in store_bit_field/extract_bit_field when trying
3631 : to access a non-existent part of the register. */
3632 2495843 : if (TREE_CODE (index) == INTEGER_CST
3633 2022308 : && TYPE_DOMAIN (TREE_TYPE (array))
3634 4514792 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3635 : {
3636 60650 : if (!c_mark_addressable (array, false, true))
3637 0 : return error_mark_node;
3638 : /* ISO C2Y disallows a negative integer constant expression index
3639 : when array subscripting has an operand of array type. */
3640 60650 : if (flag_isoc2y
3641 26 : && !TREE_OVERFLOW (index)
3642 60673 : && tree_int_cst_sgn (index) < 0)
3643 10 : error_at (loc, "array subscript is negative");
3644 : }
3645 :
3646 2495843 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3647 2472506 : && ! was_vector)
3648 : {
3649 1473226 : tree foo = array;
3650 2308245 : while (TREE_CODE (foo) == COMPONENT_REF)
3651 835019 : foo = TREE_OPERAND (foo, 0);
3652 974065 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3653 2447225 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3654 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3655 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3656 : "ISO C forbids subscripting %<register%> array "
3657 : "before C2Y");
3658 1473148 : else if (!lvalue_p (foo))
3659 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3660 : "ISO C90 forbids subscripting non-lvalue "
3661 : "array");
3662 : }
3663 :
3664 2495843 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3665 2495843 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3666 3 : index = fold_convert (sizetype, index);
3667 :
3668 2495843 : type = TREE_TYPE (TREE_TYPE (array));
3669 2495843 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3670 : /* Array ref is const/volatile if the array elements are
3671 : or if the array is. */
3672 7487529 : TREE_READONLY (rval)
3673 2495843 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3674 2495843 : | TREE_READONLY (array));
3675 7487529 : TREE_SIDE_EFFECTS (rval)
3676 2495843 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3677 2495843 : | TREE_SIDE_EFFECTS (array));
3678 4991686 : TREE_THIS_VOLATILE (rval)
3679 2495843 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3680 : /* This was added by rms on 16 Nov 91.
3681 : It fixes vol struct foo *a; a->elts[1]
3682 : in an inline function.
3683 : Hope it doesn't break something else. */
3684 2495843 : | TREE_THIS_VOLATILE (array));
3685 2495843 : ret = require_complete_type (loc, rval);
3686 2495843 : protected_set_expr_location (ret, loc);
3687 2495843 : if (non_lvalue)
3688 105090 : ret = non_lvalue_loc (loc, ret);
3689 2495843 : return ret;
3690 : }
3691 : else
3692 : {
3693 971575 : tree ar = default_conversion (array);
3694 :
3695 971575 : if (ar == error_mark_node)
3696 : return ar;
3697 :
3698 971575 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3699 971575 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3700 :
3701 971575 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3702 : index, false),
3703 : RO_ARRAY_INDEXING);
3704 971575 : if (non_lvalue)
3705 0 : ret = non_lvalue_loc (loc, ret);
3706 971575 : return ret;
3707 : }
3708 : }
3709 :
3710 : /* Build an OpenMP array section reference, creating an exact type for the
3711 : resulting expression based on the element type and bounds if possible. If
3712 : we have variable bounds, create an incomplete array type for the result
3713 : instead. */
3714 :
3715 : tree
3716 3950 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3717 : {
3718 3950 : tree type = TREE_TYPE (array);
3719 3950 : gcc_assert (type);
3720 :
3721 3950 : tree sectype, eltype = TREE_TYPE (type);
3722 :
3723 : /* It's not an array or pointer type. Just reuse the type of the original
3724 : expression as the type of the array section (an error will be raised
3725 : anyway, later). */
3726 3950 : if (eltype == NULL_TREE || error_operand_p (eltype))
3727 12 : sectype = TREE_TYPE (array);
3728 : else
3729 : {
3730 3938 : tree idxtype = NULL_TREE;
3731 :
3732 3938 : if (index != NULL_TREE
3733 3938 : && length != NULL_TREE
3734 3011 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3735 6944 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3736 : {
3737 2999 : tree low = fold_convert (sizetype, index);
3738 2999 : tree high = fold_convert (sizetype, length);
3739 2999 : high = size_binop (PLUS_EXPR, low, high);
3740 2999 : high = size_binop (MINUS_EXPR, high, size_one_node);
3741 2999 : idxtype = build_range_type (sizetype, low, high);
3742 : }
3743 135 : else if ((index == NULL_TREE || integer_zerop (index))
3744 829 : && length != NULL_TREE
3745 1616 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3746 660 : idxtype = build_index_type (length);
3747 :
3748 3938 : gcc_assert (!error_operand_p (idxtype));
3749 :
3750 3938 : sectype = c_build_array_type (eltype, idxtype);
3751 : }
3752 :
3753 3950 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3754 : }
3755 :
3756 :
3757 : /* Record that REF is used. This is relevant for undeclared static
3758 : function and declarations referenced from a non-local context.
3759 : ADDRESS indicates whether the address is taken. */
3760 :
3761 : void
3762 472674702 : mark_decl_used (tree ref, bool address)
3763 : {
3764 472674702 : if (!ref || !DECL_P (ref) || in_alignof)
3765 : return;
3766 :
3767 : /* Non-file-scope and non-local reference in nested function. */
3768 347926669 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3769 121977877 : && DECL_CONTEXT (current_function_decl)
3770 472640945 : && DECL_CONTEXT (ref) != current_function_decl;
3771 :
3772 : /* An undeclared static function. */
3773 472630894 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3774 101844125 : && DECL_INITIAL (ref) == NULL_TREE
3775 69666441 : && DECL_EXTERNAL (ref)
3776 542294737 : && !TREE_PUBLIC (ref);
3777 :
3778 472496828 : if (!static_p && !nonloc_p)
3779 : return;
3780 :
3781 : /* If we may be in an unevaluated context, delay the decision. */
3782 136672 : if (in_sizeof || in_typeof || in_countof || in_generic)
3783 270 : return record_maybe_used_decl (ref, address);
3784 :
3785 136402 : if (static_p)
3786 133810 : C_DECL_USED (ref) = 1;
3787 :
3788 136402 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3789 : || TREE_CODE (ref) == PARM_DECL))
3790 2048 : DECL_NONLOCAL (ref) = 1;
3791 :
3792 : /* Nothing to do anymore. */
3793 2592 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3794 135211 : return;
3795 :
3796 : /* Filter out the cases where referencing a non-local variable does not
3797 : require a non-local context passed via the static chain. */
3798 1191 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3799 1135 : switch (TREE_CODE (ref))
3800 : {
3801 215 : case FUNCTION_DECL:
3802 : /* Use of another local function that requires no context is ok. */
3803 215 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3804 : return;
3805 : break;
3806 438 : case VAR_DECL:
3807 : /* Static variables and constexpr are ok, but for the later only
3808 : when the address is not taken. */
3809 438 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3810 : return;
3811 : break;
3812 : case TYPE_DECL:
3813 : /* A typedef is ok when not for a variably-modified type. */
3814 : return;
3815 : case CONST_DECL:
3816 : /* An enumeration constant is ok. */
3817 : return;
3818 : case PARM_DECL:
3819 : break;
3820 : case LABEL_DECL:
3821 : break;
3822 0 : default:
3823 0 : gcc_unreachable ();
3824 : }
3825 :
3826 : /* Mark all parent functions up to the nesting level of the variable as
3827 : as needing the non-local context. */
3828 1987 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3829 : {
3830 1987 : if (cont == DECL_CONTEXT (ref))
3831 : break;
3832 :
3833 : /* There should not be any other type of context used for function
3834 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3835 999 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3836 :
3837 999 : if (TREE_CODE (cont) == FUNCTION_DECL)
3838 999 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3839 : }
3840 : }
3841 :
3842 :
3843 : /* Build an external reference to identifier ID. FUN indicates
3844 : whether this will be used for a function call. LOC is the source
3845 : location of the identifier. This sets *TYPE to the type of the
3846 : identifier, which is not the same as the type of the returned value
3847 : for CONST_DECLs defined as enum constants. If the type of the
3848 : identifier is not available, *TYPE is set to NULL. */
3849 : tree
3850 174822513 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3851 : {
3852 174822513 : tree ref;
3853 174822513 : tree decl = lookup_name (id);
3854 :
3855 : /* In Objective-C, an instance variable (ivar) may be preferred to
3856 : whatever lookup_name() found. */
3857 174822513 : decl = objc_lookup_ivar (decl, id);
3858 :
3859 174822513 : *type = NULL;
3860 174822513 : if (decl && decl != error_mark_node)
3861 : {
3862 174816362 : ref = decl;
3863 174816362 : *type = TREE_TYPE (ref);
3864 174816362 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3865 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3866 : decl);
3867 : }
3868 6151 : else if (fun)
3869 : /* Implicit function declaration. */
3870 4729 : ref = implicitly_declare (loc, id);
3871 1422 : else if (decl == error_mark_node)
3872 : /* Don't complain about something that's already been
3873 : complained about. */
3874 : return error_mark_node;
3875 : else
3876 : {
3877 1188 : undeclared_variable (loc, id);
3878 1188 : return error_mark_node;
3879 : }
3880 :
3881 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3882 : unmappable types if we return the decl with an error_mark_node type,
3883 : rather than returning error_mark_node for the decl itself. */
3884 174821091 : if (TREE_TYPE (ref) == error_mark_node
3885 174821091 : && !c_omp_array_section_p)
3886 : return error_mark_node;
3887 :
3888 174821074 : if (TREE_UNAVAILABLE (ref))
3889 14 : error_unavailable_use (ref, NULL_TREE);
3890 174821060 : else if (TREE_DEPRECATED (ref))
3891 100 : warn_deprecated_use (ref, NULL_TREE);
3892 :
3893 : /* Recursive call does not count as usage. */
3894 174821074 : if (ref != current_function_decl)
3895 : {
3896 174818433 : TREE_USED (ref) = 1;
3897 : }
3898 :
3899 174821074 : mark_decl_used (ref, false);
3900 :
3901 174821074 : if (TREE_CODE (ref) == CONST_DECL)
3902 : {
3903 417544 : used_types_insert (TREE_TYPE (ref));
3904 :
3905 417544 : if (warn_cxx_compat
3906 377 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3907 417919 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3908 : {
3909 1 : warning_at (loc, OPT_Wc___compat,
3910 : "enum constant defined in struct or union "
3911 : "is not visible in C++");
3912 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3913 : }
3914 :
3915 417544 : ref = DECL_INITIAL (ref);
3916 417544 : TREE_CONSTANT (ref) = 1;
3917 : }
3918 : /* C99 6.7.4p3: An inline definition of a function with external
3919 : linkage ... shall not contain a reference to an identifier with
3920 : internal linkage. */
3921 174403530 : else if (current_function_decl != NULL_TREE
3922 172892288 : && DECL_DECLARED_INLINE_P (current_function_decl)
3923 159261035 : && DECL_EXTERNAL (current_function_decl)
3924 158489449 : && VAR_OR_FUNCTION_DECL_P (ref)
3925 56479681 : && (!VAR_P (ref) || TREE_STATIC (ref))
3926 47337362 : && ! TREE_PUBLIC (ref)
3927 174403564 : && DECL_CONTEXT (ref) != current_function_decl)
3928 25 : record_inline_static (loc, current_function_decl, ref,
3929 : csi_internal);
3930 :
3931 : return ref;
3932 : }
3933 :
3934 : static struct maybe_used_decl *maybe_used_decls;
3935 :
3936 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3937 : _Generic, might be used if the operand of sizeof is a VLA type or the
3938 : operand of typeof is a variably modified type or the operand of _Countof has
3939 : a variable number of elements or the operand of _Generic is the one selected
3940 : as the result. */
3941 :
3942 : static void
3943 270 : record_maybe_used_decl (tree decl, bool address)
3944 : {
3945 270 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3946 270 : t->decl = decl;
3947 270 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3948 270 : t->address = address;
3949 270 : t->next = maybe_used_decls;
3950 270 : maybe_used_decls = t;
3951 270 : }
3952 :
3953 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3954 : _Generic. If USED is false, just discard them. If it is true, mark them
3955 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3956 : them to the next level up (if still inside sizeof or typeof or _Countof or
3957 : _Generic). */
3958 :
3959 : void
3960 1466205 : pop_maybe_used (bool used)
3961 : {
3962 1466205 : struct maybe_used_decl *p = maybe_used_decls;
3963 1466205 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3964 1466499 : while (p && p->level > cur_level)
3965 : {
3966 294 : if (used)
3967 : {
3968 74 : if (cur_level == 0)
3969 50 : mark_decl_used (p->decl, p->address);
3970 : else
3971 24 : p->level = cur_level;
3972 : }
3973 294 : p = p->next;
3974 : }
3975 1466205 : if (!used || cur_level == 0)
3976 1466125 : maybe_used_decls = p;
3977 1466205 : }
3978 :
3979 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3980 : _Generic, without acting on them, and return the pointer to the previous top
3981 : of the stack. This for use at the end of a default generic association when
3982 : it is not yet known whether the expression is used. If it later turns out
3983 : the expression is used (or treated as used before C23), restore_maybe_used
3984 : should be called on the return value followed by pop_maybe_used (true);
3985 : otherwise, the return value can be discarded. */
3986 :
3987 : struct maybe_used_decl *
3988 514 : save_maybe_used ()
3989 : {
3990 514 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
3991 514 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3992 546 : while (p && p->level > cur_level)
3993 32 : p = p->next;
3994 514 : maybe_used_decls = p;
3995 514 : return orig;
3996 : }
3997 :
3998 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
3999 : or _Generic returned by save_maybe_used. It is required that the stack is
4000 : at exactly the point where it was left by save_maybe_used. */
4001 :
4002 : void
4003 72 : restore_maybe_used (struct maybe_used_decl *stack)
4004 : {
4005 72 : maybe_used_decls = stack;
4006 72 : }
4007 :
4008 : /* Return the result of sizeof applied to EXPR. */
4009 :
4010 : struct c_expr
4011 312785 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4012 : {
4013 312785 : struct c_expr ret;
4014 312785 : if (expr.value == error_mark_node)
4015 : {
4016 113 : ret.value = error_mark_node;
4017 113 : ret.original_code = ERROR_MARK;
4018 113 : ret.original_type = NULL;
4019 113 : ret.m_decimal = 0;
4020 113 : pop_maybe_used (false);
4021 : }
4022 : else
4023 : {
4024 312672 : bool expr_const_operands = true;
4025 :
4026 312672 : if (TREE_CODE (expr.value) == PARM_DECL
4027 312672 : && C_ARRAY_PARAMETER (expr.value))
4028 : {
4029 55 : auto_diagnostic_group d;
4030 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4031 : "%<sizeof%> on array function parameter %qE will "
4032 : "return size of %qT", expr.value,
4033 55 : TREE_TYPE (expr.value)))
4034 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4035 55 : }
4036 312672 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4037 : &expr_const_operands);
4038 312672 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4039 312672 : c_last_sizeof_arg = expr.value;
4040 312672 : c_last_sizeof_loc = loc;
4041 312672 : ret.original_code = SIZEOF_EXPR;
4042 312672 : ret.original_type = NULL;
4043 312672 : ret.m_decimal = 0;
4044 312672 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4045 : {
4046 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4047 288 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4048 : folded_expr, ret.value);
4049 288 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4050 288 : SET_EXPR_LOCATION (ret.value, loc);
4051 : }
4052 312672 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4053 : }
4054 312785 : return ret;
4055 : }
4056 :
4057 : /* Return the result of sizeof applied to T, a structure for the type
4058 : name passed to sizeof (rather than the type itself). LOC is the
4059 : location of the original expression. */
4060 :
4061 : struct c_expr
4062 331101 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4063 : {
4064 331101 : tree type;
4065 331101 : struct c_expr ret;
4066 331101 : tree type_expr = NULL_TREE;
4067 331101 : bool type_expr_const = true;
4068 331101 : type = groktypename (t, &type_expr, &type_expr_const);
4069 331101 : ret.value = c_sizeof (loc, type);
4070 331101 : c_last_sizeof_arg = type;
4071 331101 : c_last_sizeof_loc = loc;
4072 331101 : ret.original_code = SIZEOF_EXPR;
4073 331101 : ret.original_type = NULL;
4074 331101 : ret.m_decimal = 0;
4075 331101 : if (type == error_mark_node)
4076 : {
4077 5 : ret.value = error_mark_node;
4078 5 : ret.original_code = ERROR_MARK;
4079 : }
4080 : else
4081 331054 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4082 662065 : && C_TYPE_VARIABLE_SIZE (type))
4083 : {
4084 : /* If the type is a [*] array, it is a VLA but is represented as
4085 : having a size of zero. In such a case we must ensure that
4086 : the result of sizeof does not get folded to a constant by
4087 : c_fully_fold, because if the size is evaluated the result is
4088 : not constant and so constraints on zero or negative size
4089 : arrays must not be applied when this sizeof call is inside
4090 : another array declarator. */
4091 41 : if (!type_expr)
4092 16 : type_expr = integer_zero_node;
4093 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4094 : type_expr, ret.value);
4095 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4096 : }
4097 331101 : pop_maybe_used (type != error_mark_node
4098 331101 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4099 331101 : return ret;
4100 : }
4101 :
4102 : static bool
4103 138 : is_top_array_vla (tree type)
4104 : {
4105 138 : bool zero, var;
4106 138 : tree d;
4107 :
4108 138 : if (TREE_CODE (type) != ARRAY_TYPE)
4109 : return false;
4110 120 : if (!COMPLETE_TYPE_P (type))
4111 : return false;
4112 :
4113 116 : d = TYPE_DOMAIN (type);
4114 116 : zero = !TYPE_MAX_VALUE (d);
4115 116 : if (zero)
4116 : return false;
4117 :
4118 100 : var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST
4119 100 : || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST);
4120 : return var;
4121 : }
4122 :
4123 : /* Return the result of countof applied to EXPR. */
4124 :
4125 : struct c_expr
4126 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4127 : {
4128 54 : struct c_expr ret;
4129 54 : if (expr.value == error_mark_node)
4130 : {
4131 1 : ret.value = error_mark_node;
4132 1 : ret.original_code = ERROR_MARK;
4133 1 : ret.original_type = NULL;
4134 1 : ret.m_decimal = 0;
4135 1 : pop_maybe_used (false);
4136 : }
4137 : else
4138 : {
4139 53 : bool expr_const_operands = true;
4140 :
4141 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4142 : &expr_const_operands);
4143 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4144 53 : c_last_sizeof_arg = expr.value;
4145 53 : c_last_sizeof_loc = loc;
4146 53 : ret.original_code = COUNTOF_EXPR;
4147 53 : ret.original_type = NULL;
4148 53 : ret.m_decimal = 0;
4149 53 : if (is_top_array_vla (TREE_TYPE (folded_expr)))
4150 : {
4151 : /* countof is evaluated when given a vla. */
4152 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4153 : folded_expr, ret.value);
4154 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4155 11 : SET_EXPR_LOCATION (ret.value, loc);
4156 : }
4157 53 : pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
4158 : }
4159 54 : return ret;
4160 : }
4161 :
4162 : /* Return the result of countof applied to T, a structure for the type
4163 : name passed to countof (rather than the type itself). LOC is the
4164 : location of the original expression. */
4165 :
4166 : struct c_expr
4167 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4168 : {
4169 18 : tree type;
4170 18 : struct c_expr ret;
4171 18 : tree type_expr = NULL_TREE;
4172 18 : bool type_expr_const = true;
4173 18 : type = groktypename (t, &type_expr, &type_expr_const);
4174 18 : ret.value = c_countof_type (loc, type);
4175 18 : c_last_sizeof_arg = type;
4176 18 : c_last_sizeof_loc = loc;
4177 18 : ret.original_code = COUNTOF_EXPR;
4178 18 : ret.original_type = NULL;
4179 18 : ret.m_decimal = 0;
4180 18 : if (type == error_mark_node)
4181 : {
4182 0 : ret.value = error_mark_node;
4183 0 : ret.original_code = ERROR_MARK;
4184 : }
4185 : else
4186 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4187 22 : && is_top_array_vla (type))
4188 : {
4189 : /* If the type is a [*] array, it is a VLA but is represented as
4190 : having a size of zero. In such a case we must ensure that
4191 : the result of countof does not get folded to a constant by
4192 : c_fully_fold, because if the number of elements is evaluated
4193 : the result is not constant and so
4194 : constraints on zero or negative size arrays must not be applied
4195 : when this countof call is inside another array declarator. */
4196 6 : if (!type_expr)
4197 0 : type_expr = integer_zero_node;
4198 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4199 : type_expr, ret.value);
4200 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4201 : }
4202 18 : pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false);
4203 18 : return ret;
4204 : }
4205 :
4206 : /* Return the result of maxof applied to T, a structure for the type
4207 : name passed to maxof (rather than the type itself). LOC is the
4208 : location of the original expression. */
4209 :
4210 : struct c_expr
4211 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4212 : {
4213 46 : tree type;
4214 46 : struct c_expr ret;
4215 46 : tree type_expr = NULL_TREE;
4216 46 : bool type_expr_const = true;
4217 46 : type = groktypename (t, &type_expr, &type_expr_const);
4218 46 : ret.value = c_maxof_type (loc, type);
4219 46 : c_last_sizeof_arg = type;
4220 46 : c_last_sizeof_loc = loc;
4221 46 : ret.original_code = MAXOF_EXPR;
4222 46 : ret.original_type = NULL;
4223 46 : ret.m_decimal = 0;
4224 46 : if (type == error_mark_node)
4225 : {
4226 0 : ret.value = error_mark_node;
4227 0 : ret.original_code = ERROR_MARK;
4228 : }
4229 46 : pop_maybe_used (type != error_mark_node);
4230 46 : return ret;
4231 : }
4232 :
4233 : /* Return the result of minof applied to T, a structure for the type
4234 : name passed to minof (rather than the type itself). LOC is the
4235 : location of the original expression. */
4236 :
4237 : struct c_expr
4238 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4239 : {
4240 46 : tree type;
4241 46 : struct c_expr ret;
4242 46 : tree type_expr = NULL_TREE;
4243 46 : bool type_expr_const = true;
4244 46 : type = groktypename (t, &type_expr, &type_expr_const);
4245 46 : ret.value = c_minof_type (loc, type);
4246 46 : c_last_sizeof_arg = type;
4247 46 : c_last_sizeof_loc = loc;
4248 46 : ret.original_code = MINOF_EXPR;
4249 46 : ret.original_type = NULL;
4250 46 : ret.m_decimal = 0;
4251 46 : if (type == error_mark_node)
4252 : {
4253 0 : ret.value = error_mark_node;
4254 0 : ret.original_code = ERROR_MARK;
4255 : }
4256 46 : pop_maybe_used (type != error_mark_node);
4257 46 : return ret;
4258 : }
4259 :
4260 : /* Build a function call to function FUNCTION with parameters PARAMS.
4261 : The function call is at LOC.
4262 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4263 : TREE_VALUE of each node is a parameter-expression.
4264 : FUNCTION's data type may be a function type or a pointer-to-function. */
4265 :
4266 : tree
4267 0 : build_function_call (location_t loc, tree function, tree params)
4268 : {
4269 0 : vec<tree, va_gc> *v;
4270 0 : tree ret;
4271 :
4272 0 : vec_alloc (v, list_length (params));
4273 0 : for (; params; params = TREE_CHAIN (params))
4274 0 : v->quick_push (TREE_VALUE (params));
4275 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4276 0 : vec_free (v);
4277 0 : return ret;
4278 : }
4279 :
4280 : /* Give a note about the location of the declaration of DECL,
4281 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4282 :
4283 : static void
4284 299 : inform_declaration (tree decl, tree function_expr)
4285 : {
4286 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4287 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4288 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4289 92 : else if (function_expr)
4290 92 : switch (TREE_CODE (function_expr))
4291 : {
4292 : default:
4293 : break;
4294 13 : case COMPONENT_REF:
4295 : /* Show the decl of the pertinent field (e.g. for callback
4296 : fields in a struct. */
4297 13 : {
4298 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4299 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4300 13 : inform (loc, "declared here");
4301 : }
4302 : break;
4303 : }
4304 299 : }
4305 :
4306 : /* C implementation of callback for use when checking param types. */
4307 :
4308 : static bool
4309 2 : comp_parm_types (tree wanted_type, tree actual_type)
4310 : {
4311 2 : return comptypes (wanted_type, actual_type);
4312 : }
4313 :
4314 : /* Build a function call to function FUNCTION with parameters PARAMS.
4315 : If FUNCTION is the result of resolving an overloaded target built-in,
4316 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4317 : ORIGTYPES, if not NULL, is a vector of types; each element is
4318 : either NULL or the original type of the corresponding element in
4319 : PARAMS. The original type may differ from TREE_TYPE of the
4320 : parameter for enums. FUNCTION's data type may be a function type
4321 : or pointer-to-function. This function changes the elements of
4322 : PARAMS. */
4323 :
4324 : tree
4325 49863085 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4326 : tree function, vec<tree, va_gc> *params,
4327 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4328 : {
4329 49863085 : tree fntype, fundecl = NULL_TREE;
4330 49863085 : tree name = NULL_TREE, result;
4331 49863085 : tree tem;
4332 49863085 : int nargs;
4333 49863085 : tree *argarray;
4334 :
4335 :
4336 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4337 49863085 : STRIP_TYPE_NOPS (function);
4338 :
4339 : /* Convert anything with function type to a pointer-to-function. */
4340 49863085 : if (TREE_CODE (function) == FUNCTION_DECL)
4341 : {
4342 49785660 : name = DECL_NAME (function);
4343 :
4344 49785660 : if (flag_tm)
4345 421 : tm_malloc_replacement (function);
4346 49785660 : fundecl = function;
4347 49785660 : if (!orig_fundecl)
4348 49785660 : orig_fundecl = fundecl;
4349 : /* Atomic functions have type checking/casting already done. They are
4350 : often rewritten and don't match the original parameter list. */
4351 99571320 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4352 : origtypes = NULL;
4353 : }
4354 49863085 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4355 49800140 : function = function_to_pointer_conversion (loc, function);
4356 :
4357 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4358 : expressions, like those used for ObjC messenger dispatches. */
4359 49863085 : if (params && !params->is_empty ())
4360 38155241 : function = objc_rewrite_function_call (function, (*params)[0]);
4361 :
4362 49863085 : function = c_fully_fold (function, false, NULL);
4363 :
4364 49863085 : fntype = TREE_TYPE (function);
4365 :
4366 49863085 : if (TREE_CODE (fntype) == ERROR_MARK)
4367 5 : return error_mark_node;
4368 :
4369 49863080 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4370 49863046 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4371 : {
4372 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4373 33 : error_at (loc,
4374 : "called object %qE is not a function or function pointer",
4375 : function);
4376 4 : else if (DECL_P (function))
4377 : {
4378 1 : auto_diagnostic_group d;
4379 1 : error_at (loc,
4380 : "called object %qD is not a function or function pointer",
4381 : function);
4382 1 : inform_declaration (function, NULL_TREE);
4383 1 : }
4384 : else
4385 3 : error_at (loc,
4386 : "called object is not a function or function pointer");
4387 37 : return error_mark_node;
4388 : }
4389 :
4390 49863043 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4391 374529 : current_function_returns_abnormally = 1;
4392 :
4393 : /* fntype now gets the type of function pointed to. */
4394 49863043 : fntype = TREE_TYPE (fntype);
4395 49863043 : tree return_type = TREE_TYPE (fntype);
4396 :
4397 : /* Convert the parameters to the types declared in the
4398 : function prototype, or apply default promotions. */
4399 :
4400 49863043 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4401 : origtypes, function, fundecl);
4402 49863043 : if (nargs < 0)
4403 178 : return error_mark_node;
4404 :
4405 : /* Check that the function is called through a compatible prototype.
4406 : If it is not, warn. */
4407 49861958 : if (CONVERT_EXPR_P (function)
4408 923 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4409 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4410 49863050 : && !comptypes (fntype, TREE_TYPE (tem)))
4411 : {
4412 : /* This situation leads to run-time undefined behavior. We can't,
4413 : therefore, simply error unless we can prove that all possible
4414 : executions of the program must execute the code. */
4415 20 : warning_at (loc, 0, "function called through a non-compatible type");
4416 :
4417 20 : if (VOID_TYPE_P (return_type)
4418 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4419 1 : pedwarn (loc, 0,
4420 : "function with qualified void return type called");
4421 : }
4422 :
4423 49862865 : argarray = vec_safe_address (params);
4424 :
4425 : /* Check that arguments to builtin functions match the expectations. */
4426 49862865 : if (fundecl
4427 49785497 : && fndecl_built_in_p (fundecl)
4428 83088361 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4429 : orig_fundecl, nargs, argarray))
4430 298 : return error_mark_node;
4431 :
4432 : /* Check that the arguments to the function are valid. */
4433 49862567 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4434 : nargs, argarray, &arg_loc,
4435 : comp_parm_types);
4436 :
4437 49862567 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4438 49862567 : && !VOID_TYPE_P (return_type))
4439 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4440 49862567 : if (name != NULL_TREE
4441 99647766 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4442 : {
4443 32505307 : if (require_constant_value)
4444 3487 : result
4445 3487 : = fold_build_call_array_initializer_loc (loc, return_type,
4446 : function, nargs, argarray);
4447 : else
4448 32501820 : result = fold_build_call_array_loc (loc, return_type,
4449 : function, nargs, argarray);
4450 32505307 : if (TREE_CODE (result) == NOP_EXPR
4451 32505307 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4452 26410 : STRIP_TYPE_NOPS (result);
4453 : }
4454 : else
4455 17357260 : result = build_call_array_loc (loc, return_type,
4456 : function, nargs, argarray);
4457 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4458 : later. */
4459 49862567 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4460 215 : suppress_warning (result, OPT_Wnonnull);
4461 :
4462 : /* In this improbable scenario, a nested function returns a VM type.
4463 : Create a TARGET_EXPR so that the call always has a LHS, much as
4464 : what the C++ FE does for functions returning non-PODs. */
4465 49862567 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4466 : {
4467 81 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4468 81 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4469 : NULL_TREE, NULL_TREE);
4470 : }
4471 :
4472 49862567 : if (VOID_TYPE_P (TREE_TYPE (result)))
4473 : {
4474 2390093 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4475 2 : pedwarn (loc, 0,
4476 : "function with qualified void return type called");
4477 2390093 : return result;
4478 : }
4479 47472474 : return require_complete_type (loc, result);
4480 : }
4481 :
4482 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4483 :
4484 : tree
4485 49862642 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4486 : tree function, vec<tree, va_gc> *params,
4487 : vec<tree, va_gc> *origtypes)
4488 : {
4489 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4490 49862653 : STRIP_TYPE_NOPS (function);
4491 :
4492 : /* Convert anything with function type to a pointer-to-function. */
4493 49862642 : if (TREE_CODE (function) == FUNCTION_DECL)
4494 : {
4495 : /* Implement type-directed function overloading for builtins.
4496 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4497 : handle all the type checking. The result is a complete expression
4498 : that implements this function call. */
4499 49785217 : tree tem = resolve_overloaded_builtin (loc, function, params);
4500 49785217 : if (tem)
4501 : return tem;
4502 : }
4503 49776725 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4504 : }
4505 :
4506 : /* Helper for convert_arguments called to convert the VALue of argument
4507 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4508 : PARMNUM and TYPE.
4509 : PLOC is the location where the conversion is being performed.
4510 : FUNCTION and FUNDECL are the same as in convert_arguments.
4511 : VALTYPE is the original type of VAL before the conversion and,
4512 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4513 : NPC is true if VAL represents the null pointer constant (VAL itself
4514 : will have been folded to an integer constant).
4515 : RNAME is the same as FUNCTION except in Objective C when it's
4516 : the function selector.
4517 : EXCESS_PRECISION is true when VAL was originally represented
4518 : as EXCESS_PRECISION_EXPR.
4519 : WARNOPT is the same as in convert_for_assignment. */
4520 :
4521 : static tree
4522 125525697 : convert_argument (location_t ploc, tree function, tree fundecl,
4523 : tree type, tree origtype, tree val, tree valtype,
4524 : bool npc, tree rname, int parmnum, int argnum,
4525 : bool excess_precision, int warnopt)
4526 : {
4527 : /* Formal parm type is specified by a function prototype. */
4528 :
4529 125525697 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4530 : {
4531 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4532 : parmnum + 1);
4533 3 : return error_mark_node;
4534 : }
4535 :
4536 : /* Optionally warn about conversions that differ from the default
4537 : conversions. */
4538 125525694 : if (warn_traditional_conversion || warn_traditional)
4539 : {
4540 193 : if (INTEGRAL_TYPE_P (type)
4541 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4542 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4543 : "passing argument %d of %qE as integer rather "
4544 : "than floating due to prototype",
4545 : argnum, rname);
4546 193 : if (INTEGRAL_TYPE_P (type)
4547 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4548 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4549 : "passing argument %d of %qE as integer rather "
4550 : "than complex due to prototype",
4551 : argnum, rname);
4552 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4553 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4554 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4555 : "passing argument %d of %qE as complex rather "
4556 : "than floating due to prototype",
4557 : argnum, rname);
4558 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4559 71 : && INTEGRAL_TYPE_P (valtype))
4560 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4561 : "passing argument %d of %qE as floating rather "
4562 : "than integer due to prototype",
4563 : argnum, rname);
4564 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4565 7 : && INTEGRAL_TYPE_P (valtype))
4566 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4567 : "passing argument %d of %qE as complex rather "
4568 : "than integer due to prototype",
4569 : argnum, rname);
4570 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4571 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4572 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4573 : "passing argument %d of %qE as floating rather "
4574 : "than complex due to prototype",
4575 : argnum, rname);
4576 : /* ??? At some point, messages should be written about
4577 : conversions between complex types, but that's too messy
4578 : to do now. */
4579 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4580 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4581 : {
4582 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4583 :
4584 : /* Warn if any argument is passed as `float',
4585 : since without a prototype it would be `double'. */
4586 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4587 45 : && type != dfloat32_type_node)
4588 7 : warning_at (ploc, 0,
4589 : "passing argument %d of %qE as %<float%> "
4590 : "rather than %<double%> due to prototype",
4591 : argnum, rname);
4592 :
4593 : /* Warn if mismatch between argument and prototype
4594 : for decimal float types. Warn of conversions with
4595 : binary float types and of precision narrowing due to
4596 : prototype. */
4597 38 : else if (type != valtype
4598 32 : && (type == dfloat32_type_node
4599 22 : || type == dfloat64_type_node
4600 12 : || type == dfloat128_type_node
4601 2 : || valtype == dfloat32_type_node
4602 2 : || valtype == dfloat64_type_node
4603 2 : || valtype == dfloat128_type_node)
4604 38 : && (formal_prec
4605 30 : <= TYPE_PRECISION (valtype)
4606 14 : || (type == dfloat128_type_node
4607 10 : && (valtype
4608 10 : != dfloat64_type_node
4609 8 : && (valtype
4610 : != dfloat32_type_node)))
4611 8 : || (type == dfloat64_type_node
4612 4 : && (valtype
4613 : != dfloat32_type_node))))
4614 24 : warning_at (ploc, 0,
4615 : "passing argument %d of %qE as %qT "
4616 : "rather than %qT due to prototype",
4617 : argnum, rname, type, valtype);
4618 :
4619 : }
4620 : /* Detect integer changing in width or signedness.
4621 : These warnings are only activated with
4622 : -Wtraditional-conversion, not with -Wtraditional. */
4623 105 : else if (warn_traditional_conversion
4624 105 : && INTEGRAL_TYPE_P (type)
4625 102 : && INTEGRAL_TYPE_P (valtype))
4626 : {
4627 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4628 83 : tree would_have_been = default_conversion (val);
4629 83 : tree type1 = TREE_TYPE (would_have_been);
4630 :
4631 83 : if (val == error_mark_node)
4632 : /* VAL could have been of incomplete type. */;
4633 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4634 83 : && (TYPE_MAIN_VARIANT (type)
4635 2 : == TYPE_MAIN_VARIANT (valtype)))
4636 : /* No warning if function asks for enum
4637 : and the actual arg is that enum type. */
4638 : ;
4639 81 : else if (formal_prec != TYPE_PRECISION (type1))
4640 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4641 : "passing argument %d of %qE "
4642 : "with different width due to prototype",
4643 : argnum, rname);
4644 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4645 : ;
4646 : /* Don't complain if the formal parameter type
4647 : is an enum, because we can't tell now whether
4648 : the value was an enum--even the same enum. */
4649 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4650 : ;
4651 32 : else if (TREE_CODE (val) == INTEGER_CST
4652 5 : && int_fits_type_p (val, type))
4653 : /* Change in signedness doesn't matter
4654 : if a constant value is unaffected. */
4655 : ;
4656 : /* If the value is extended from a narrower
4657 : unsigned type, it doesn't matter whether we
4658 : pass it as signed or unsigned; the value
4659 : certainly is the same either way. */
4660 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4661 32 : && TYPE_UNSIGNED (valtype))
4662 : ;
4663 32 : else if (TYPE_UNSIGNED (type))
4664 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4665 : "passing argument %d of %qE "
4666 : "as unsigned due to prototype",
4667 : argnum, rname);
4668 : else
4669 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4670 : "passing argument %d of %qE "
4671 : "as signed due to prototype",
4672 : argnum, rname);
4673 : }
4674 : }
4675 :
4676 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4677 : sake of better warnings from convert_and_check. */
4678 125525694 : if (excess_precision)
4679 370 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4680 :
4681 125525694 : tree parmval = convert_for_assignment (ploc, ploc, type,
4682 : val, origtype, ic_argpass,
4683 : npc, fundecl, function,
4684 : parmnum + 1, warnopt);
4685 125525694 : return parmval;
4686 : }
4687 :
4688 : /* Convert the argument expressions in the vector VALUES
4689 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4690 :
4691 : If the list is exhausted, or when an element has NULL as its type,
4692 : perform the default conversions.
4693 :
4694 : ORIGTYPES is the original types of the expressions in VALUES. This
4695 : holds the type of enum values which have been converted to integral
4696 : types. It may be NULL.
4697 :
4698 : FUNCTION is a tree for the called function. It is used only for
4699 : error messages, where it is formatted with %qE.
4700 :
4701 : This is also where warnings about wrong number of args are generated.
4702 :
4703 : ARG_LOC are locations of function arguments (if any).
4704 :
4705 : Returns the actual number of arguments processed (which may be less
4706 : than the length of VALUES in some error situations), or -1 on
4707 : failure. */
4708 :
4709 : static int
4710 49863043 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4711 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4712 : tree function, tree fundecl)
4713 : {
4714 49863043 : tree typelist = TYPE_ARG_TYPES (fntype);
4715 49863043 : unsigned int parmnum;
4716 49863043 : bool error_args = false;
4717 49863043 : const bool type_generic = fundecl
4718 49863043 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4719 49863043 : bool type_generic_remove_excess_precision = false;
4720 49863043 : bool type_generic_overflow_p = false;
4721 49863043 : bool type_generic_bit_query = false;
4722 49863043 : tree selector;
4723 :
4724 : /* Change pointer to function to the function itself for
4725 : diagnostics. */
4726 49863043 : if (TREE_CODE (function) == ADDR_EXPR
4727 49863043 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4728 49787758 : function = TREE_OPERAND (function, 0);
4729 :
4730 : /* Handle an ObjC selector specially for diagnostics. */
4731 49863043 : selector = objc_message_selector ();
4732 :
4733 : /* For a call to a built-in function declared without a prototype,
4734 : set to the built-in function's argument list. */
4735 49863043 : tree builtin_typelist = NULL_TREE;
4736 :
4737 : /* For type-generic built-in functions, determine whether excess
4738 : precision should be removed (classification) or not
4739 : (comparison). */
4740 49863043 : if (fundecl
4741 49863043 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4742 : {
4743 1396571 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4744 1396571 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4745 : {
4746 : /* For a call to a built-in function declared without a prototype
4747 : use the types of the parameters of the internal built-in to
4748 : match those of the arguments to. */
4749 689636 : if (tree bdecl = builtin_decl_explicit (code))
4750 689636 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4751 : }
4752 :
4753 : /* For type-generic built-in functions, determine whether excess
4754 : precision should be removed (classification) or not
4755 : (comparison). */
4756 1396571 : if (type_generic)
4757 60878 : switch (code)
4758 : {
4759 5380 : case BUILT_IN_ISFINITE:
4760 5380 : case BUILT_IN_ISINF:
4761 5380 : case BUILT_IN_ISINF_SIGN:
4762 5380 : case BUILT_IN_ISNAN:
4763 5380 : case BUILT_IN_ISNORMAL:
4764 5380 : case BUILT_IN_ISSIGNALING:
4765 5380 : case BUILT_IN_FPCLASSIFY:
4766 5380 : type_generic_remove_excess_precision = true;
4767 5380 : break;
4768 :
4769 21026 : case BUILT_IN_ADD_OVERFLOW_P:
4770 21026 : case BUILT_IN_SUB_OVERFLOW_P:
4771 21026 : case BUILT_IN_MUL_OVERFLOW_P:
4772 : /* The last argument of these type-generic builtins
4773 : should not be promoted. */
4774 21026 : type_generic_overflow_p = true;
4775 21026 : break;
4776 :
4777 1294 : case BUILT_IN_CLZG:
4778 1294 : case BUILT_IN_CTZG:
4779 1294 : case BUILT_IN_CLRSBG:
4780 1294 : case BUILT_IN_FFSG:
4781 1294 : case BUILT_IN_PARITYG:
4782 1294 : case BUILT_IN_POPCOUNTG:
4783 : /* The first argument of these type-generic builtins
4784 : should not be promoted. */
4785 1294 : type_generic_bit_query = true;
4786 1294 : break;
4787 :
4788 : default:
4789 : break;
4790 : }
4791 : }
4792 :
4793 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4794 : individual converted arguments. */
4795 :
4796 49863043 : tree typetail, builtin_typetail, val;
4797 49863043 : for (typetail = typelist,
4798 49863043 : builtin_typetail = builtin_typelist,
4799 49863043 : parmnum = 0;
4800 176323006 : values && values->iterate (parmnum, &val);
4801 : ++parmnum)
4802 : {
4803 : /* The type of the function parameter (if it was declared with one). */
4804 251983810 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4805 : /* The type of the built-in function parameter (if the function
4806 : is a built-in). Used to detect type incompatibilities in
4807 : calls to built-ins declared without a prototype. */
4808 126459999 : tree builtin_type = (builtin_typetail
4809 127495731 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4810 : /* The original type of the argument being passed to the function. */
4811 126459999 : tree valtype = TREE_TYPE (val);
4812 : /* The called function (or function selector in Objective C). */
4813 126459999 : tree rname = function;
4814 126459999 : int argnum = parmnum + 1;
4815 126459999 : const char *invalid_func_diag;
4816 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4817 126459999 : bool excess_precision = false;
4818 : /* The value of the argument after conversion to the type
4819 : of the function parameter it is passed to. */
4820 126459999 : tree parmval;
4821 : /* Some __atomic_* builtins have additional hidden argument at
4822 : position 0. */
4823 126459999 : location_t ploc
4824 126191256 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4825 126190656 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4826 126459999 : : input_location;
4827 :
4828 126459999 : if (type == void_type_node)
4829 : {
4830 33 : auto_diagnostic_group d;
4831 33 : int num_expected = parmnum;
4832 33 : int num_actual = values->length ();
4833 33 : gcc_rich_location rich_loc (loc);
4834 33 : if (ploc != input_location)
4835 32 : rich_loc.add_range (ploc);
4836 33 : if (selector)
4837 0 : error_at (&rich_loc,
4838 : "too many arguments to method %qE; expected %i, have %i",
4839 : selector, num_expected, num_actual);
4840 : else
4841 33 : error_at (&rich_loc,
4842 : "too many arguments to function %qE; expected %i, have %i",
4843 : function, num_expected, num_actual);
4844 33 : inform_declaration (fundecl, function);
4845 65 : return error_args ? -1 : (int) parmnum;
4846 33 : }
4847 :
4848 126459966 : if (builtin_type == void_type_node)
4849 : {
4850 12 : auto_diagnostic_group d;
4851 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4852 : "too many arguments to built-in function %qE "
4853 : "expecting %d", function, parmnum))
4854 12 : inform_declaration (fundecl, function);
4855 12 : builtin_typetail = NULL_TREE;
4856 12 : }
4857 :
4858 90366 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4859 126550215 : && !(fundecl && fndecl_built_in_p (fundecl)))
4860 : {
4861 6963 : auto_diagnostic_group d;
4862 6963 : bool warned;
4863 6963 : if (selector)
4864 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4865 : "ISO C23 does not allow arguments"
4866 : " for method %qE declared without parameters",
4867 : function);
4868 : else
4869 6963 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4870 : "ISO C23 does not allow arguments"
4871 : " for function %qE declared without parameters",
4872 : function);
4873 6963 : if (warned)
4874 4 : inform_declaration (fundecl, function);
4875 6963 : }
4876 :
4877 126459966 : if (selector && argnum > 2)
4878 : {
4879 0 : rname = selector;
4880 0 : argnum -= 2;
4881 : }
4882 :
4883 : /* Determine if VAL is a null pointer constant before folding it. */
4884 126459966 : bool npc = null_pointer_constant_p (val);
4885 :
4886 : /* If there is excess precision and a prototype, convert once to
4887 : the required type rather than converting via the semantic
4888 : type. Likewise without a prototype a float value represented
4889 : as long double should be converted once to double. But for
4890 : type-generic classification functions excess precision must
4891 : be removed here. */
4892 126459966 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4893 423 : && (type || !type_generic || !type_generic_remove_excess_precision))
4894 : {
4895 403 : val = TREE_OPERAND (val, 0);
4896 403 : excess_precision = true;
4897 : }
4898 126459966 : val = c_fully_fold (val, false, NULL);
4899 252919933 : STRIP_TYPE_NOPS (val);
4900 :
4901 126459966 : val = require_complete_type (ploc, val);
4902 :
4903 : /* Some floating-point arguments must be promoted to double when
4904 : no type is specified by a prototype. This applies to
4905 : arguments of type float, and to architecture-specific types
4906 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4907 126459966 : bool promote_float_arg = false;
4908 126459966 : if (type == NULL_TREE
4909 936188 : && TREE_CODE (valtype) == REAL_TYPE
4910 269701 : && (TYPE_PRECISION (valtype)
4911 269701 : <= TYPE_PRECISION (double_type_node))
4912 262419 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4913 131418 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4914 126591384 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4915 : {
4916 : /* Promote this argument, unless it has a _FloatN or
4917 : _FloatNx type. */
4918 1027846 : promote_float_arg = true;
4919 1027846 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4920 900438 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4921 : {
4922 : promote_float_arg = false;
4923 : break;
4924 : }
4925 : /* Don't promote __bf16 either. */
4926 130685 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4927 126329351 : promote_float_arg = false;
4928 : }
4929 :
4930 126459966 : if (type != NULL_TREE)
4931 : {
4932 125523778 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4933 125523778 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4934 : val, valtype, npc, rname, parmnum, argnum,
4935 : excess_precision, 0);
4936 : }
4937 : /* A NULLPTR type is just a nullptr always. */
4938 936188 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4939 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4940 936179 : else if (promote_float_arg)
4941 : {
4942 127338 : if (type_generic)
4943 : parmval = val;
4944 : else
4945 : {
4946 : /* Convert `float' to `double'. */
4947 124446 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4948 6 : warning_at (ploc, OPT_Wdouble_promotion,
4949 : "implicit conversion from %qT to %qT when passing "
4950 : "argument to function",
4951 : valtype, double_type_node);
4952 124446 : parmval = convert (double_type_node, val);
4953 : }
4954 : }
4955 808841 : else if ((excess_precision && !type_generic)
4956 808839 : || (type_generic_overflow_p && parmnum == 2)
4957 787817 : || (type_generic_bit_query && parmnum == 0))
4958 : /* A "double" argument with excess precision being passed
4959 : without a prototype or in variable arguments.
4960 : The last argument of __builtin_*_overflow_p should not be
4961 : promoted, similarly the first argument of
4962 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4963 22312 : parmval = convert (valtype, val);
4964 1573058 : else if ((invalid_func_diag =
4965 786529 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4966 : {
4967 0 : error (invalid_func_diag);
4968 0 : return -1;
4969 : }
4970 786529 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4971 : {
4972 : return -1;
4973 : }
4974 : else
4975 : /* Convert `short' and `char' to full-size `int'. */
4976 786526 : parmval = default_conversion (val);
4977 :
4978 126459963 : (*values)[parmnum] = parmval;
4979 126459963 : if (parmval == error_mark_node)
4980 105 : error_args = true;
4981 :
4982 126459963 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4983 : {
4984 : /* For a call to a built-in function declared without a prototype,
4985 : perform the conversions from the argument to the expected type
4986 : but issue warnings rather than errors for any mismatches.
4987 : Ignore the converted argument and use the PARMVAL obtained
4988 : above by applying default conversions instead. */
4989 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4990 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4991 : val, valtype, npc, rname, parmnum, argnum,
4992 : excess_precision,
4993 : OPT_Wbuiltin_declaration_mismatch);
4994 : }
4995 :
4996 126459963 : if (typetail)
4997 125523778 : typetail = TREE_CHAIN (typetail);
4998 :
4999 126459963 : if (builtin_typetail)
5000 1035715 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5001 : }
5002 :
5003 88018201 : gcc_assert (parmnum == vec_safe_length (values));
5004 :
5005 99396213 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5006 : {
5007 : /* Not enough args.
5008 : Determine minimum number of arguments required. */
5009 : int min_expected_num = 0;
5010 231 : bool at_least_p = false;
5011 : tree iter = typelist;
5012 386 : while (true)
5013 : {
5014 231 : if (!iter)
5015 : {
5016 : /* Variadic arguments; stop iterating. */
5017 : at_least_p = true;
5018 : break;
5019 : }
5020 222 : if (iter == void_list_node)
5021 : /* End of arguments; stop iterating. */
5022 : break;
5023 155 : ++min_expected_num;
5024 155 : iter = TREE_CHAIN (iter);
5025 : }
5026 76 : auto_diagnostic_group d;
5027 76 : int actual_num = vec_safe_length (values);
5028 143 : error_at (loc,
5029 : at_least_p
5030 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5031 : : G_("too few arguments to function %qE; expected %i, have %i"),
5032 : function, min_expected_num, actual_num);
5033 76 : inform_declaration (fundecl, function);
5034 76 : return -1;
5035 76 : }
5036 :
5037 50516363 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5038 : {
5039 : unsigned nargs = parmnum;
5040 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5041 428 : ++nargs;
5042 :
5043 188 : auto_diagnostic_group d;
5044 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5045 : "too few arguments to built-in function %qE "
5046 : "expecting %u", function, nargs - 1))
5047 173 : inform_declaration (fundecl, function);
5048 188 : }
5049 :
5050 49862931 : return error_args ? -1 : (int) parmnum;
5051 : }
5052 :
5053 : /* This is the entry point used by the parser to build unary operators
5054 : in the input. CODE, a tree_code, specifies the unary operator, and
5055 : ARG is the operand. For unary plus, the C parser currently uses
5056 : CONVERT_EXPR for code.
5057 :
5058 : LOC is the location to use for the tree generated.
5059 : */
5060 :
5061 : struct c_expr
5062 8155145 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5063 : {
5064 8155145 : struct c_expr result;
5065 :
5066 8155145 : result.original_code = code;
5067 8155145 : result.original_type = NULL;
5068 8155145 : result.m_decimal = 0;
5069 :
5070 8155145 : if (reject_gcc_builtin (arg.value))
5071 : {
5072 2 : result.value = error_mark_node;
5073 : }
5074 : else
5075 : {
5076 8155143 : result.value = build_unary_op (loc, code, arg.value, false);
5077 :
5078 8155143 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5079 5 : overflow_warning (loc, result.value, arg.value);
5080 : }
5081 :
5082 : /* We are typically called when parsing a prefix token at LOC acting on
5083 : ARG. Reflect this by updating the source range of the result to
5084 : start at LOC and end at the end of ARG. */
5085 8155145 : set_c_expr_source_range (&result,
5086 : loc, arg.get_finish ());
5087 :
5088 8155145 : return result;
5089 : }
5090 :
5091 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5092 :
5093 : bool
5094 1281681 : char_type_p (tree type)
5095 : {
5096 1281681 : return (type == char_type_node
5097 991254 : || type == unsigned_char_type_node
5098 956117 : || type == signed_char_type_node
5099 955269 : || type == char16_type_node
5100 2017615 : || type == char32_type_node);
5101 : }
5102 :
5103 : /* This is the entry point used by the parser to build binary operators
5104 : in the input. CODE, a tree_code, specifies the binary operator, and
5105 : ARG1 and ARG2 are the operands. In addition to constructing the
5106 : expression, we check for operands that were written with other binary
5107 : operators in a way that is likely to confuse the user.
5108 :
5109 : LOCATION is the location of the binary operator. */
5110 :
5111 : struct c_expr
5112 9318481 : parser_build_binary_op (location_t location, enum tree_code code,
5113 : struct c_expr arg1, struct c_expr arg2)
5114 : {
5115 9318481 : struct c_expr result;
5116 9318481 : result.m_decimal = 0;
5117 :
5118 9318481 : enum tree_code code1 = arg1.original_code;
5119 9318481 : enum tree_code code2 = arg2.original_code;
5120 9318481 : tree type1 = (arg1.original_type
5121 9318481 : ? arg1.original_type
5122 5979994 : : TREE_TYPE (arg1.value));
5123 9318481 : tree type2 = (arg2.original_type
5124 9318481 : ? arg2.original_type
5125 6874547 : : TREE_TYPE (arg2.value));
5126 :
5127 9318481 : result.value = build_binary_op (location, code,
5128 : arg1.value, arg2.value, true);
5129 9318480 : result.original_code = code;
5130 9318480 : result.original_type = NULL;
5131 9318480 : result.m_decimal = 0;
5132 :
5133 9318480 : if (TREE_CODE (result.value) == ERROR_MARK)
5134 : {
5135 1297 : set_c_expr_source_range (&result,
5136 : arg1.get_start (),
5137 : arg2.get_finish ());
5138 1297 : return result;
5139 : }
5140 :
5141 9317183 : if (location != UNKNOWN_LOCATION)
5142 9317183 : protected_set_expr_location (result.value, location);
5143 :
5144 9317183 : set_c_expr_source_range (&result,
5145 : arg1.get_start (),
5146 : arg2.get_finish ());
5147 :
5148 : /* Check for cases such as x+y<<z which users are likely
5149 : to misinterpret. */
5150 9317183 : if (warn_parentheses)
5151 2003936 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5152 : arg2.value);
5153 :
5154 9317183 : if (warn_logical_op)
5155 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5156 : code1, arg1.value, code2, arg2.value);
5157 :
5158 9317183 : if (warn_tautological_compare)
5159 : {
5160 2002449 : tree lhs = arg1.value;
5161 2002449 : tree rhs = arg2.value;
5162 2002449 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5163 : {
5164 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5165 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5166 : lhs = NULL_TREE;
5167 : else
5168 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5169 : }
5170 2002449 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5171 : {
5172 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5173 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5174 : rhs = NULL_TREE;
5175 : else
5176 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5177 : }
5178 2002449 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5179 2002449 : warn_tautological_cmp (location, code, lhs, rhs);
5180 : }
5181 :
5182 9317183 : if (warn_logical_not_paren
5183 2002532 : && TREE_CODE_CLASS (code) == tcc_comparison
5184 405816 : && code1 == TRUTH_NOT_EXPR
5185 405816 : && code2 != TRUTH_NOT_EXPR
5186 : /* Avoid warning for !!x == y. */
5187 9317309 : && (TREE_CODE (arg1.value) != NE_EXPR
5188 19 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5189 : {
5190 : /* Avoid warning for !b == y where b has _Bool type. */
5191 107 : tree t = integer_zero_node;
5192 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5193 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5194 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5195 : {
5196 90 : t = TREE_OPERAND (arg1.value, 0);
5197 199 : do
5198 : {
5199 199 : if (TREE_TYPE (t) != integer_type_node)
5200 : break;
5201 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5202 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5203 90 : else if (CONVERT_EXPR_P (t))
5204 19 : t = TREE_OPERAND (t, 0);
5205 : else
5206 : break;
5207 : }
5208 : while (1);
5209 : }
5210 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5211 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5212 : }
5213 :
5214 : /* Warn about comparisons against string literals, with the exception
5215 : of testing for equality or inequality of a string literal with NULL. */
5216 9317183 : if (code == EQ_EXPR || code == NE_EXPR)
5217 : {
5218 1165287 : if ((code1 == STRING_CST
5219 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5220 1165298 : || (code2 == STRING_CST
5221 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5222 36 : warning_at (location, OPT_Waddress,
5223 : "comparison with string literal results in unspecified behavior");
5224 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5225 1165287 : if (POINTER_TYPE_P (type1)
5226 90645 : && null_pointer_constant_p (arg2.value)
5227 1186378 : && char_type_p (type2))
5228 : {
5229 18 : auto_diagnostic_group d;
5230 18 : if (warning_at (location, OPT_Wpointer_compare,
5231 : "comparison between pointer and zero character "
5232 : "constant"))
5233 10 : inform (arg1.get_start (),
5234 : "did you mean to dereference the pointer?");
5235 18 : }
5236 1165269 : else if (POINTER_TYPE_P (type2)
5237 87948 : && null_pointer_constant_p (arg1.value)
5238 1165535 : && char_type_p (type1))
5239 : {
5240 10 : auto_diagnostic_group d;
5241 10 : if (warning_at (location, OPT_Wpointer_compare,
5242 : "comparison between pointer and zero character "
5243 : "constant"))
5244 10 : inform (arg2.get_start (),
5245 : "did you mean to dereference the pointer?");
5246 10 : }
5247 : }
5248 8151896 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5249 996183 : && (code1 == STRING_CST || code2 == STRING_CST))
5250 0 : warning_at (location, OPT_Waddress,
5251 : "comparison with string literal results in unspecified "
5252 : "behavior");
5253 :
5254 9317183 : if (warn_zero_as_null_pointer_constant
5255 24 : && c_inhibit_evaluation_warnings == 0
5256 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5257 : {
5258 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5259 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5260 9 : && INTEGRAL_TYPE_P (type2)
5261 33 : && null_pointer_constant_p (arg2.value))
5262 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5263 : "zero as null pointer constant");
5264 :
5265 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5266 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5267 11 : && INTEGRAL_TYPE_P (type1)
5268 35 : && null_pointer_constant_p (arg1.value))
5269 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5270 : "zero as null pointer constant");
5271 : }
5272 :
5273 9317183 : if (warn_array_compare
5274 2002287 : && TREE_CODE_CLASS (code) == tcc_comparison
5275 405601 : && TREE_CODE (type1) == ARRAY_TYPE
5276 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5277 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5278 :
5279 2179593 : if (TREE_OVERFLOW_P (result.value)
5280 299 : && !TREE_OVERFLOW_P (arg1.value)
5281 9317475 : && !TREE_OVERFLOW_P (arg2.value))
5282 249 : overflow_warning (location, result.value);
5283 :
5284 : /* Warn about comparisons of different enum types. */
5285 9317182 : if (warn_enum_compare
5286 2022225 : && TREE_CODE_CLASS (code) == tcc_comparison
5287 410026 : && TREE_CODE (type1) == ENUMERAL_TYPE
5288 7038 : && TREE_CODE (type2) == ENUMERAL_TYPE
5289 9324158 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5290 2 : warning_at (location, OPT_Wenum_compare,
5291 : "comparison between %qT and %qT",
5292 : type1, type2);
5293 :
5294 9317182 : if (warn_xor_used_as_pow
5295 9317182 : && code == BIT_XOR_EXPR
5296 77025 : && arg1.m_decimal
5297 722 : && arg2.m_decimal)
5298 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5299 : location,
5300 : arg2.get_location (), arg2.value);
5301 :
5302 : return result;
5303 : }
5304 :
5305 : /* Return a tree for the difference of pointers OP0 and OP1.
5306 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5307 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5308 :
5309 : static tree
5310 3681 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5311 : {
5312 3681 : tree restype = ptrdiff_type_node;
5313 3681 : tree result, inttype;
5314 :
5315 3681 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5316 3681 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5317 3681 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5318 3681 : tree orig_op0 = op0;
5319 3681 : tree orig_op1 = op1;
5320 :
5321 : /* If the operands point into different address spaces, we need to
5322 : explicitly convert them to pointers into the common address space
5323 : before we can subtract the numerical address values. */
5324 3681 : if (as0 != as1)
5325 : {
5326 0 : addr_space_t as_common;
5327 0 : tree common_type;
5328 :
5329 : /* Determine the common superset address space. This is guaranteed
5330 : to exist because the caller verified that comp_target_types
5331 : returned non-zero. */
5332 0 : if (!addr_space_superset (as0, as1, &as_common))
5333 0 : gcc_unreachable ();
5334 :
5335 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5336 0 : op0 = convert (common_type, op0);
5337 0 : op1 = convert (common_type, op1);
5338 : }
5339 :
5340 : /* Determine integer type result of the subtraction. This will usually
5341 : be the same as the result type (ptrdiff_t), but may need to be a wider
5342 : type if pointers for the address space are wider than ptrdiff_t. */
5343 3681 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5344 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5345 : else
5346 : inttype = restype;
5347 :
5348 3681 : if (VOID_TYPE_P (target_type))
5349 146 : pedwarn (loc, OPT_Wpointer_arith,
5350 : "pointer of type %<void *%> used in subtraction");
5351 3681 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5352 4 : pedwarn (loc, OPT_Wpointer_arith,
5353 : "pointer to a function used in subtraction");
5354 :
5355 3681 : if (current_function_decl != NULL_TREE
5356 3681 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5357 : {
5358 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5359 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5360 :
5361 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5362 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5363 : }
5364 :
5365 : /* First do the subtraction, then build the divide operator
5366 : and only convert at the very end.
5367 : Do not do default conversions in case restype is a short type. */
5368 :
5369 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5370 : pointers. If some platform cannot provide that, or has a larger
5371 : ptrdiff_type to support differences larger than half the address
5372 : space, cast the pointers to some larger integer type and do the
5373 : computations in that type. */
5374 3681 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5375 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5376 : convert (inttype, op1), false);
5377 : else
5378 : {
5379 : /* Cast away qualifiers. */
5380 3681 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5381 3681 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5382 3681 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5383 : }
5384 :
5385 : /* This generates an error if op1 is pointer to incomplete type. */
5386 3681 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5387 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5388 3677 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5389 3677 : TREE_TYPE (TREE_TYPE (orig_op0))))
5390 7354 : verify_type_context (loc, TCTX_POINTER_ARITH,
5391 3677 : TREE_TYPE (TREE_TYPE (orig_op1)));
5392 :
5393 3681 : op1 = c_size_in_bytes (target_type);
5394 :
5395 3681 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5396 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5397 :
5398 : /* Divide by the size, in easiest possible way. */
5399 3681 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5400 : op0, convert (inttype, op1));
5401 :
5402 : /* Convert to final result type if necessary. */
5403 3681 : return convert (restype, result);
5404 : }
5405 :
5406 : /* Expand atomic compound assignments into an appropriate sequence as
5407 : specified by the C11 standard section 6.5.16.2.
5408 :
5409 : _Atomic T1 E1
5410 : T2 E2
5411 : E1 op= E2
5412 :
5413 : This sequence is used for all types for which these operations are
5414 : supported.
5415 :
5416 : In addition, built-in versions of the 'fe' prefixed routines may
5417 : need to be invoked for floating point (real, complex or vector) when
5418 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5419 :
5420 : T1 newval;
5421 : T1 old;
5422 : T1 *addr
5423 : T2 val
5424 : fenv_t fenv
5425 :
5426 : addr = &E1;
5427 : val = (E2);
5428 : __atomic_load (addr, &old, SEQ_CST);
5429 : feholdexcept (&fenv);
5430 : loop:
5431 : newval = old op val;
5432 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5433 : SEQ_CST))
5434 : goto done;
5435 : feclearexcept (FE_ALL_EXCEPT);
5436 : goto loop:
5437 : done:
5438 : feupdateenv (&fenv);
5439 :
5440 : The compiler will issue the __atomic_fetch_* built-in when possible,
5441 : otherwise it will generate the generic form of the atomic operations.
5442 : This requires temp(s) and has their address taken. The atomic processing
5443 : is smart enough to figure out when the size of an object can utilize
5444 : a lock-free version, and convert the built-in call to the appropriate
5445 : lock-free routine. The optimizers will then dispose of any temps that
5446 : are no longer required, and lock-free implementations are utilized as
5447 : long as there is target support for the required size.
5448 :
5449 : If the operator is NOP_EXPR, then this is a simple assignment, and
5450 : an __atomic_store is issued to perform the assignment rather than
5451 : the above loop. */
5452 :
5453 : /* Build an atomic assignment at LOC, expanding into the proper
5454 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5455 : the result of the operation, unless RETURN_OLD_P, in which case
5456 : return the old value of LHS (this is only for postincrement and
5457 : postdecrement). */
5458 :
5459 : static tree
5460 30433 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5461 : tree rhs, bool return_old_p)
5462 : {
5463 30433 : tree fndecl, func_call;
5464 30433 : vec<tree, va_gc> *params;
5465 30433 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5466 30433 : tree old, old_addr;
5467 30433 : tree compound_stmt = NULL_TREE;
5468 30433 : tree stmt, goto_stmt;
5469 30433 : tree loop_label, loop_decl, done_label, done_decl;
5470 :
5471 30433 : tree lhs_type = TREE_TYPE (lhs);
5472 30433 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5473 30433 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5474 30433 : tree rhs_semantic_type = TREE_TYPE (rhs);
5475 30433 : tree nonatomic_rhs_semantic_type;
5476 30433 : tree rhs_type;
5477 :
5478 30433 : gcc_assert (TYPE_ATOMIC (lhs_type));
5479 :
5480 30433 : if (return_old_p)
5481 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5482 :
5483 : /* Allocate enough vector items for a compare_exchange. */
5484 30433 : vec_alloc (params, 6);
5485 :
5486 : /* Create a compound statement to hold the sequence of statements
5487 : with a loop. */
5488 30433 : if (modifycode != NOP_EXPR)
5489 : {
5490 19954 : compound_stmt = c_begin_compound_stmt (false);
5491 :
5492 : /* For consistency with build_modify_expr on non-_Atomic,
5493 : mark the lhs as read. Also, it would be very hard to match
5494 : such expressions in mark_exp_read. */
5495 19954 : mark_exp_read (lhs);
5496 : }
5497 :
5498 : /* Remove any excess precision (which is only present here in the
5499 : case of compound assignments). */
5500 30433 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5501 : {
5502 0 : gcc_assert (modifycode != NOP_EXPR);
5503 0 : rhs = TREE_OPERAND (rhs, 0);
5504 : }
5505 30433 : rhs_type = TREE_TYPE (rhs);
5506 :
5507 : /* Fold the RHS if it hasn't already been folded. */
5508 30433 : if (modifycode != NOP_EXPR)
5509 19954 : rhs = c_fully_fold (rhs, false, NULL);
5510 :
5511 : /* Remove the qualifiers for the rest of the expressions and create
5512 : the VAL temp variable to hold the RHS. */
5513 30433 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5514 30433 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5515 30433 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5516 : TYPE_UNQUALIFIED);
5517 30433 : val = create_tmp_var_raw (nonatomic_rhs_type);
5518 30433 : TREE_ADDRESSABLE (val) = 1;
5519 30433 : suppress_warning (val);
5520 30433 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5521 : NULL_TREE);
5522 30433 : TREE_SIDE_EFFECTS (rhs) = 1;
5523 30433 : SET_EXPR_LOCATION (rhs, loc);
5524 30433 : if (modifycode != NOP_EXPR)
5525 19954 : add_stmt (rhs);
5526 :
5527 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5528 : an atomic_store. */
5529 30433 : if (modifycode == NOP_EXPR)
5530 : {
5531 10479 : compound_stmt = rhs;
5532 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5533 10479 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5534 10479 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5535 10479 : params->quick_push (lhs_addr);
5536 10479 : params->quick_push (rhs);
5537 10479 : params->quick_push (seq_cst);
5538 10479 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5539 :
5540 10479 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5541 : compound_stmt, func_call);
5542 :
5543 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5544 : the statement and that value. */
5545 10479 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5546 : }
5547 :
5548 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5549 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5550 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5551 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5552 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5553 : {
5554 11713 : built_in_function fncode;
5555 11713 : switch (modifycode)
5556 : {
5557 3075 : case PLUS_EXPR:
5558 3075 : case POINTER_PLUS_EXPR:
5559 2124 : fncode = (return_old_p
5560 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5561 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5562 : break;
5563 2889 : case MINUS_EXPR:
5564 2094 : fncode = (return_old_p
5565 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5566 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5567 : break;
5568 609 : case BIT_AND_EXPR:
5569 609 : fncode = (return_old_p
5570 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5571 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5572 : break;
5573 609 : case BIT_IOR_EXPR:
5574 609 : fncode = (return_old_p
5575 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5576 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5577 : break;
5578 609 : case BIT_XOR_EXPR:
5579 609 : fncode = (return_old_p
5580 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5581 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5582 : break;
5583 3922 : default:
5584 3922 : goto cas_loop;
5585 : }
5586 :
5587 : /* We can only use "_1" through "_16" variants of the atomic fetch
5588 : built-ins. */
5589 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5590 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5591 0 : goto cas_loop;
5592 :
5593 : /* If this is a pointer type, we need to multiply by the size of
5594 : the pointer target type. */
5595 7791 : if (POINTER_TYPE_P (lhs_type))
5596 : {
5597 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5598 : /* ??? This would introduce -Wdiscarded-qualifiers
5599 : warning: __atomic_fetch_* expect volatile void *
5600 : type as the first argument. (Assignments between
5601 : atomic and non-atomic objects are OK.) */
5602 1018 : || TYPE_RESTRICT (lhs_type))
5603 17 : goto cas_loop;
5604 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5605 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5606 : convert (ptrdiff_type_node, rhs),
5607 : convert (ptrdiff_type_node, sz));
5608 : }
5609 :
5610 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5611 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5612 7774 : fndecl = builtin_decl_explicit (fncode);
5613 7774 : params->quick_push (lhs_addr);
5614 7774 : params->quick_push (rhs);
5615 7774 : params->quick_push (seq_cst);
5616 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5617 :
5618 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5619 7774 : TREE_ADDRESSABLE (newval) = 1;
5620 7774 : suppress_warning (newval);
5621 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5622 : NULL_TREE, NULL_TREE);
5623 7774 : SET_EXPR_LOCATION (rhs, loc);
5624 7774 : add_stmt (rhs);
5625 :
5626 : /* Finish the compound statement. */
5627 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5628 :
5629 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5630 : the statement and that value. */
5631 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5632 : }
5633 :
5634 8241 : cas_loop:
5635 : /* Create the variables and labels required for the op= form. */
5636 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5637 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5638 12180 : TREE_ADDRESSABLE (old) = 1;
5639 12180 : suppress_warning (old);
5640 :
5641 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5642 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5643 12180 : TREE_ADDRESSABLE (newval) = 1;
5644 12180 : suppress_warning (newval);
5645 :
5646 12180 : loop_decl = create_artificial_label (loc);
5647 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5648 :
5649 12180 : done_decl = create_artificial_label (loc);
5650 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5651 :
5652 : /* __atomic_load (addr, &old, SEQ_CST). */
5653 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5654 12180 : params->quick_push (lhs_addr);
5655 12180 : params->quick_push (old_addr);
5656 12180 : params->quick_push (seq_cst);
5657 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5658 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5659 : NULL_TREE);
5660 12180 : add_stmt (old);
5661 12180 : params->truncate (0);
5662 :
5663 : /* Create the expressions for floating-point environment
5664 : manipulation, if required. */
5665 12180 : bool need_fenv = (flag_trapping_math
5666 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5667 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5668 12180 : if (need_fenv)
5669 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5670 :
5671 12180 : if (hold_call)
5672 7054 : add_stmt (hold_call);
5673 :
5674 : /* loop: */
5675 12180 : add_stmt (loop_label);
5676 :
5677 : /* newval = old + val; */
5678 12180 : if (rhs_type != rhs_semantic_type)
5679 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5680 12180 : rhs = build_binary_op (loc, modifycode,
5681 : convert_lvalue_to_rvalue (loc, old, true, true),
5682 : val, true);
5683 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5684 : {
5685 0 : tree eptype = TREE_TYPE (rhs);
5686 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5687 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5688 : }
5689 : else
5690 12180 : rhs = c_fully_fold (rhs, false, NULL);
5691 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5692 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5693 : NULL_TREE, 0);
5694 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5695 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5696 : subsequent ICEs in nested function processing after an error, ensure such
5697 : a TARGET_EXPR is built even after an error. */
5698 12180 : if (rhs == error_mark_node)
5699 10 : rhs = old;
5700 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5701 : NULL_TREE);
5702 12180 : SET_EXPR_LOCATION (rhs, loc);
5703 12180 : add_stmt (rhs);
5704 :
5705 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5706 : goto done; */
5707 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5708 12180 : params->quick_push (lhs_addr);
5709 12180 : params->quick_push (old_addr);
5710 12180 : params->quick_push (newval_addr);
5711 12180 : params->quick_push (integer_zero_node);
5712 12180 : params->quick_push (seq_cst);
5713 12180 : params->quick_push (seq_cst);
5714 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5715 :
5716 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5717 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5718 :
5719 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5720 12180 : SET_EXPR_LOCATION (stmt, loc);
5721 12180 : add_stmt (stmt);
5722 :
5723 12180 : if (clear_call)
5724 7054 : add_stmt (clear_call);
5725 :
5726 : /* goto loop; */
5727 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5728 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5729 12180 : add_stmt (goto_stmt);
5730 :
5731 : /* done: */
5732 12180 : add_stmt (done_label);
5733 :
5734 12180 : if (update_call)
5735 7054 : add_stmt (update_call);
5736 :
5737 : /* Finish the compound statement. */
5738 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5739 :
5740 : /* NEWVAL is the value that was successfully stored, return a
5741 : COMPOUND_EXPR of the statement and the appropriate value. */
5742 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5743 12180 : return_old_p ? old : newval);
5744 : }
5745 :
5746 : /* Construct and perhaps optimize a tree representation
5747 : for a unary operation. CODE, a tree_code, specifies the operation
5748 : and XARG is the operand.
5749 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5750 : promotions (such as from short to int).
5751 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5752 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5753 : to pointers in C99.
5754 :
5755 : LOCATION is the location of the operator. */
5756 :
5757 : tree
5758 60476241 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5759 : bool noconvert)
5760 : {
5761 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5762 60476241 : tree arg = xarg;
5763 60476241 : tree argtype = NULL_TREE;
5764 60476241 : enum tree_code typecode;
5765 60476241 : tree val;
5766 60476241 : tree ret = error_mark_node;
5767 60476241 : tree eptype = NULL_TREE;
5768 60476241 : const char *invalid_op_diag;
5769 60476241 : bool int_operands;
5770 :
5771 60476241 : int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5772 6456284 : if (int_operands)
5773 6456284 : arg = remove_c_maybe_const_expr (arg);
5774 :
5775 60476241 : if (code != ADDR_EXPR)
5776 8528502 : arg = require_complete_type (location, arg);
5777 :
5778 60476241 : typecode = TREE_CODE (TREE_TYPE (arg));
5779 60476241 : if (typecode == ERROR_MARK)
5780 74 : return error_mark_node;
5781 60476167 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5782 32183 : typecode = INTEGER_TYPE;
5783 :
5784 120952334 : if ((invalid_op_diag
5785 60476167 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5786 : {
5787 0 : error_at (location, invalid_op_diag);
5788 0 : return error_mark_node;
5789 : }
5790 :
5791 60476167 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5792 : {
5793 237 : eptype = TREE_TYPE (arg);
5794 237 : arg = TREE_OPERAND (arg, 0);
5795 : }
5796 :
5797 60476167 : switch (code)
5798 : {
5799 28755 : case CONVERT_EXPR:
5800 : /* This is used for unary plus, because a CONVERT_EXPR
5801 : is enough to prevent anybody from looking inside for
5802 : associativity, but won't generate any code. */
5803 28760 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5804 8 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5805 5 : || typecode == BITINT_TYPE
5806 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5807 : {
5808 1 : error_at (location, "wrong type argument to unary plus");
5809 1 : return error_mark_node;
5810 : }
5811 28754 : else if (!noconvert)
5812 28754 : arg = default_conversion (arg);
5813 28754 : arg = non_lvalue_loc (location, arg);
5814 28754 : break;
5815 :
5816 6692754 : case NEGATE_EXPR:
5817 7139882 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5818 483639 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5819 452372 : || typecode == BITINT_TYPE
5820 447128 : || gnu_vector_type_p (TREE_TYPE (arg))))
5821 : {
5822 1 : error_at (location, "wrong type argument to unary minus");
5823 1 : return error_mark_node;
5824 : }
5825 6692753 : else if (!noconvert)
5826 6692747 : arg = default_conversion (arg);
5827 : break;
5828 :
5829 295033 : case BIT_NOT_EXPR:
5830 : /* ~ works on integer types and non float vectors. */
5831 295033 : if (typecode == INTEGER_TYPE
5832 295033 : || typecode == BITINT_TYPE
5833 295033 : || (gnu_vector_type_p (TREE_TYPE (arg))
5834 2057 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5835 : {
5836 : tree e = arg;
5837 :
5838 : /* Warn if the expression has boolean value. */
5839 294434 : while (TREE_CODE (e) == COMPOUND_EXPR)
5840 11 : e = TREE_OPERAND (e, 1);
5841 :
5842 588832 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5843 588832 : || truth_value_p (TREE_CODE (e))))
5844 : {
5845 146 : auto_diagnostic_group d;
5846 146 : if (warning_at (location, OPT_Wbool_operation,
5847 : "%<~%> on a boolean expression"))
5848 : {
5849 12 : gcc_rich_location richloc (location);
5850 12 : richloc.add_fixit_insert_before (location, "!");
5851 12 : inform (&richloc, "did you mean to use logical not?");
5852 12 : }
5853 146 : }
5854 294423 : if (!noconvert)
5855 294420 : arg = default_conversion (arg);
5856 : }
5857 610 : else if (typecode == COMPLEX_TYPE)
5858 : {
5859 605 : code = CONJ_EXPR;
5860 605 : pedwarn (location, OPT_Wpedantic,
5861 : "ISO C does not support %<~%> for complex conjugation");
5862 605 : if (!noconvert)
5863 605 : arg = default_conversion (arg);
5864 : }
5865 : else
5866 : {
5867 5 : error_at (location, "wrong type argument to bit-complement");
5868 5 : return error_mark_node;
5869 : }
5870 : break;
5871 :
5872 3 : case ABS_EXPR:
5873 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5874 : {
5875 0 : error_at (location, "wrong type argument to abs");
5876 0 : return error_mark_node;
5877 : }
5878 3 : else if (!noconvert)
5879 0 : arg = default_conversion (arg);
5880 : break;
5881 :
5882 7 : case ABSU_EXPR:
5883 7 : if (!(typecode == INTEGER_TYPE))
5884 : {
5885 0 : error_at (location, "wrong type argument to absu");
5886 0 : return error_mark_node;
5887 : }
5888 7 : else if (!noconvert)
5889 0 : arg = default_conversion (arg);
5890 : break;
5891 :
5892 0 : case CONJ_EXPR:
5893 : /* Conjugating a real value is a no-op, but allow it anyway. */
5894 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5895 : || typecode == COMPLEX_TYPE))
5896 : {
5897 0 : error_at (location, "wrong type argument to conjugation");
5898 0 : return error_mark_node;
5899 : }
5900 0 : else if (!noconvert)
5901 0 : arg = default_conversion (arg);
5902 : break;
5903 :
5904 375879 : case TRUTH_NOT_EXPR:
5905 375879 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5906 7131 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5907 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5908 18 : && typecode != BITINT_TYPE)
5909 : {
5910 10 : error_at (location,
5911 : "wrong type argument to unary exclamation mark");
5912 10 : return error_mark_node;
5913 : }
5914 375869 : if (int_operands)
5915 : {
5916 139857 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5917 139857 : arg = remove_c_maybe_const_expr (arg);
5918 : }
5919 : else
5920 236012 : arg = c_objc_common_truthvalue_conversion (location, arg);
5921 375869 : ret = invert_truthvalue_loc (location, arg);
5922 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5923 375869 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5924 235617 : location = EXPR_LOCATION (ret);
5925 375869 : goto return_build_unary_op;
5926 :
5927 201216 : case REALPART_EXPR:
5928 201216 : case IMAGPART_EXPR:
5929 201216 : ret = build_real_imag_expr (location, code, arg);
5930 201216 : if (ret == error_mark_node)
5931 : return error_mark_node;
5932 201208 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5933 2 : eptype = TREE_TYPE (eptype);
5934 201208 : goto return_build_unary_op;
5935 :
5936 934812 : case PREINCREMENT_EXPR:
5937 934812 : case POSTINCREMENT_EXPR:
5938 934812 : case PREDECREMENT_EXPR:
5939 934812 : case POSTDECREMENT_EXPR:
5940 :
5941 934812 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5942 : {
5943 2 : tree inner = build_unary_op (location, code,
5944 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
5945 : noconvert);
5946 2 : if (inner == error_mark_node)
5947 : return error_mark_node;
5948 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5949 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5950 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5951 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5952 2 : goto return_build_unary_op;
5953 : }
5954 :
5955 : /* Complain about anything that is not a true lvalue. In
5956 : Objective-C, skip this check for property_refs. */
5957 934810 : if (!objc_is_property_ref (arg)
5958 1869620 : && !lvalue_or_else (location,
5959 934810 : arg, ((code == PREINCREMENT_EXPR
5960 934810 : || code == POSTINCREMENT_EXPR)
5961 : ? lv_increment
5962 : : lv_decrement)))
5963 22 : return error_mark_node;
5964 :
5965 934788 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5966 : {
5967 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5968 2 : warning_at (location, OPT_Wc___compat,
5969 : "increment of enumeration value is invalid in C++");
5970 : else
5971 2 : warning_at (location, OPT_Wc___compat,
5972 : "decrement of enumeration value is invalid in C++");
5973 : }
5974 :
5975 934788 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5976 : {
5977 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5978 328 : warning_at (location, OPT_Wbool_operation,
5979 : "increment of a boolean expression");
5980 : else
5981 336 : warning_at (location, OPT_Wbool_operation,
5982 : "decrement of a boolean expression");
5983 : }
5984 :
5985 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5986 934788 : arg = c_fully_fold (arg, false, NULL, true);
5987 :
5988 934788 : bool atomic_op;
5989 934788 : atomic_op = really_atomic_lvalue (arg);
5990 :
5991 : /* Increment or decrement the real part of the value,
5992 : and don't change the imaginary part. */
5993 934788 : if (typecode == COMPLEX_TYPE)
5994 : {
5995 57 : tree real, imag;
5996 :
5997 57 : pedwarn_c23 (location, OPT_Wpedantic,
5998 : "ISO C does not support %<++%> and %<--%> on complex "
5999 : "types before C2Y");
6000 :
6001 57 : if (!atomic_op)
6002 : {
6003 53 : arg = stabilize_reference (arg);
6004 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6005 : true);
6006 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6007 : true);
6008 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6009 53 : if (real == error_mark_node || imag == error_mark_node)
6010 : return error_mark_node;
6011 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6012 : real, imag);
6013 53 : goto return_build_unary_op;
6014 : }
6015 : }
6016 :
6017 : /* Report invalid types. */
6018 :
6019 934735 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6020 704615 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6021 159 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6022 934791 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6023 : {
6024 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6025 3 : error_at (location, "wrong type argument to increment");
6026 : else
6027 2 : error_at (location, "wrong type argument to decrement");
6028 :
6029 5 : return error_mark_node;
6030 : }
6031 :
6032 934730 : {
6033 934730 : tree inc;
6034 :
6035 934730 : argtype = TREE_TYPE (arg);
6036 :
6037 : /* Compute the increment. */
6038 :
6039 934730 : if (typecode == POINTER_TYPE)
6040 : {
6041 : /* If pointer target is an incomplete type,
6042 : we just cannot know how to do the arithmetic. */
6043 230120 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6044 : {
6045 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6046 17 : error_at (location,
6047 : "increment of pointer to an incomplete type %qT",
6048 17 : TREE_TYPE (argtype));
6049 : else
6050 10 : error_at (location,
6051 : "decrement of pointer to an incomplete type %qT",
6052 10 : TREE_TYPE (argtype));
6053 : }
6054 230093 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6055 230093 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6056 : {
6057 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6058 6 : pedwarn (location, OPT_Wpointer_arith,
6059 : "wrong type argument to increment");
6060 : else
6061 4 : pedwarn (location, OPT_Wpointer_arith,
6062 : "wrong type argument to decrement");
6063 : }
6064 : else
6065 460166 : verify_type_context (location, TCTX_POINTER_ARITH,
6066 230083 : TREE_TYPE (argtype));
6067 :
6068 230120 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6069 230120 : inc = convert_to_ptrofftype_loc (location, inc);
6070 : }
6071 704610 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6072 : {
6073 : /* For signed fract types, we invert ++ to -- or
6074 : -- to ++, and change inc from 1 to -1, because
6075 : it is not possible to represent 1 in signed fract constants.
6076 : For unsigned fract types, the result always overflows and
6077 : we get an undefined (original) or the maximum value. */
6078 0 : if (code == PREINCREMENT_EXPR)
6079 : code = PREDECREMENT_EXPR;
6080 : else if (code == PREDECREMENT_EXPR)
6081 : code = PREINCREMENT_EXPR;
6082 : else if (code == POSTINCREMENT_EXPR)
6083 : code = POSTDECREMENT_EXPR;
6084 : else /* code == POSTDECREMENT_EXPR */
6085 0 : code = POSTINCREMENT_EXPR;
6086 :
6087 0 : inc = integer_minus_one_node;
6088 0 : inc = convert (argtype, inc);
6089 : }
6090 : else
6091 : {
6092 704559 : inc = VECTOR_TYPE_P (argtype)
6093 704610 : ? build_one_cst (argtype)
6094 : : integer_one_node;
6095 704610 : inc = convert (argtype, inc);
6096 : }
6097 :
6098 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6099 : need to ask Objective-C to build the increment or decrement
6100 : expression for it. */
6101 934730 : if (objc_is_property_ref (arg))
6102 0 : return objc_build_incr_expr_for_property_ref (location, code,
6103 20 : arg, inc);
6104 :
6105 : /* Report a read-only lvalue. */
6106 934730 : if (TYPE_READONLY (argtype))
6107 : {
6108 20 : readonly_error (location, arg,
6109 20 : ((code == PREINCREMENT_EXPR
6110 20 : || code == POSTINCREMENT_EXPR)
6111 : ? lv_increment : lv_decrement));
6112 20 : return error_mark_node;
6113 : }
6114 934710 : else if (TREE_READONLY (arg))
6115 4 : readonly_warning (arg,
6116 4 : ((code == PREINCREMENT_EXPR
6117 4 : || code == POSTINCREMENT_EXPR)
6118 : ? lv_increment : lv_decrement));
6119 :
6120 : /* If the argument is atomic, use the special code sequences for
6121 : atomic compound assignment. */
6122 934710 : if (atomic_op)
6123 : {
6124 4469 : arg = stabilize_reference (arg);
6125 8938 : ret = build_atomic_assign (location, arg,
6126 4469 : ((code == PREINCREMENT_EXPR
6127 4469 : || code == POSTINCREMENT_EXPR)
6128 : ? PLUS_EXPR
6129 : : MINUS_EXPR),
6130 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6131 : ? inc
6132 : : integer_one_node),
6133 : (code == POSTINCREMENT_EXPR
6134 4469 : || code == POSTDECREMENT_EXPR));
6135 934710 : goto return_build_unary_op;
6136 : }
6137 :
6138 930241 : tree true_res;
6139 930241 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6140 : {
6141 364 : tree larg = stabilize_reference (arg);
6142 364 : tree sarg = save_expr (larg);
6143 364 : switch (code)
6144 : {
6145 91 : case PREINCREMENT_EXPR:
6146 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6147 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6148 91 : break;
6149 91 : case POSTINCREMENT_EXPR:
6150 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6151 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6152 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6153 91 : break;
6154 91 : case PREDECREMENT_EXPR:
6155 91 : {
6156 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6157 : true, true);
6158 91 : rarg = invert_truthvalue_loc (location, rarg);
6159 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6160 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6161 : }
6162 91 : break;
6163 91 : case POSTDECREMENT_EXPR:
6164 91 : {
6165 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6166 : true, true);
6167 91 : rarg = invert_truthvalue_loc (location, rarg);
6168 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6169 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6170 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6171 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6172 : }
6173 91 : break;
6174 : default:
6175 : gcc_unreachable ();
6176 : }
6177 364 : TREE_SIDE_EFFECTS (val) = 1;
6178 : }
6179 929877 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6180 64 : val = boolean_increment (code, arg);
6181 : else
6182 929813 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6183 930241 : TREE_SIDE_EFFECTS (val) = 1;
6184 930241 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6185 3939 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6186 : TYPE_UNQUALIFIED);
6187 930241 : ret = val;
6188 930241 : goto return_build_unary_op;
6189 : }
6190 :
6191 51947698 : case ADDR_EXPR:
6192 : /* Note that this operation never does default_conversion. */
6193 :
6194 : /* The operand of unary '&' must be an lvalue (which excludes
6195 : expressions of type void), or, in C99, the result of a [] or
6196 : unary '*' operator. */
6197 51947698 : if (VOID_TYPE_P (TREE_TYPE (arg))
6198 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6199 51947717 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6200 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6201 :
6202 : /* Let &* cancel out to simplify resulting code. */
6203 51947698 : if (INDIRECT_REF_P (arg))
6204 : {
6205 : /* Don't let this be an lvalue. */
6206 22311 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6207 16792 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6208 5519 : ret = TREE_OPERAND (arg, 0);
6209 5519 : goto return_build_unary_op;
6210 : }
6211 :
6212 : /* Anything not already handled and not a true memory reference
6213 : or a non-lvalue array is an error. */
6214 51925387 : if (typecode != FUNCTION_TYPE && !noconvert
6215 51925387 : && !lvalue_or_else (location, arg, lv_addressof))
6216 17 : return error_mark_node;
6217 :
6218 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6219 : folding later. */
6220 51925370 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6221 : {
6222 14 : tree inner = build_unary_op (location, code,
6223 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6224 : noconvert);
6225 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6226 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6227 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6228 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6229 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6230 14 : goto return_build_unary_op;
6231 : }
6232 :
6233 : /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
6234 : .ACCESS_WITH_SIZE. */
6235 51925356 : if (is_access_with_size_p (arg))
6236 0 : arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
6237 :
6238 51925356 : argtype = TREE_TYPE (arg);
6239 :
6240 : /* If the lvalue is const or volatile, merge that into the type
6241 : to which the address will point. This is only needed
6242 : for function types. */
6243 51925356 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6244 51438608 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6245 81997752 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6246 : {
6247 29992418 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6248 29992418 : int quals = orig_quals;
6249 :
6250 29992418 : if (TREE_READONLY (arg))
6251 29618589 : quals |= TYPE_QUAL_CONST;
6252 29992418 : if (TREE_THIS_VOLATILE (arg))
6253 374716 : quals |= TYPE_QUAL_VOLATILE;
6254 :
6255 29992418 : argtype = c_build_qualified_type (argtype, quals);
6256 : }
6257 :
6258 51925356 : switch (TREE_CODE (arg))
6259 : {
6260 65550 : case COMPONENT_REF:
6261 65550 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6262 : {
6263 8 : error_at (location, "cannot take address of bit-field %qD",
6264 8 : TREE_OPERAND (arg, 1));
6265 8 : return error_mark_node;
6266 : }
6267 :
6268 : /* fall through */
6269 :
6270 139686 : case ARRAY_REF:
6271 139686 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6272 : {
6273 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6274 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6275 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6276 : {
6277 6 : error_at (location, "cannot take address of scalar with "
6278 : "reverse storage order");
6279 6 : return error_mark_node;
6280 : }
6281 :
6282 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6283 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6284 58 : warning_at (location, OPT_Wscalar_storage_order,
6285 : "address of array with reverse scalar storage "
6286 : "order requested");
6287 : }
6288 :
6289 51925342 : default:
6290 51925342 : break;
6291 : }
6292 :
6293 51925342 : if (!c_mark_addressable (arg))
6294 20 : return error_mark_node;
6295 :
6296 51925322 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6297 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6298 :
6299 51925322 : argtype = c_build_pointer_type (argtype);
6300 :
6301 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6302 : when we have proper support for integer constant expressions. */
6303 51925322 : val = get_base_address (arg);
6304 51925322 : if (val && INDIRECT_REF_P (val)
6305 51949315 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6306 : {
6307 540 : ret = fold_offsetof (arg, argtype);
6308 540 : goto return_build_unary_op;
6309 : }
6310 :
6311 51924782 : val = build1 (ADDR_EXPR, argtype, arg);
6312 :
6313 51924782 : ret = val;
6314 51924782 : goto return_build_unary_op;
6315 :
6316 10 : case PAREN_EXPR:
6317 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6318 10 : goto return_build_unary_op;
6319 :
6320 0 : default:
6321 0 : gcc_unreachable ();
6322 : }
6323 :
6324 7016545 : if (argtype == NULL_TREE)
6325 7016545 : argtype = TREE_TYPE (arg);
6326 7016545 : if (TREE_CODE (arg) == INTEGER_CST)
6327 6312468 : ret = (require_constant_value
6328 6312468 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6329 6293887 : : fold_build1_loc (location, code, argtype, arg));
6330 : else
6331 704077 : ret = build1 (code, argtype, arg);
6332 60459252 : return_build_unary_op:
6333 60459252 : gcc_assert (ret != error_mark_node);
6334 6456760 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6335 66916003 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6336 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6337 60458690 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6338 74 : ret = note_integer_operands (ret);
6339 60459252 : if (eptype)
6340 237 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6341 60459252 : protected_set_expr_location (ret, location);
6342 60459252 : return ret;
6343 : }
6344 :
6345 : /* Return nonzero if REF is an lvalue valid for this language.
6346 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6347 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6348 :
6349 : bool
6350 178132083 : lvalue_p (const_tree ref)
6351 : {
6352 179625950 : const enum tree_code code = TREE_CODE (ref);
6353 :
6354 179625950 : switch (code)
6355 : {
6356 1469074 : case REALPART_EXPR:
6357 1469074 : case IMAGPART_EXPR:
6358 1469074 : case COMPONENT_REF:
6359 1469074 : return lvalue_p (TREE_OPERAND (ref, 0));
6360 :
6361 24793 : case C_MAYBE_CONST_EXPR:
6362 24793 : return lvalue_p (TREE_OPERAND (ref, 1));
6363 :
6364 : case COMPOUND_LITERAL_EXPR:
6365 : case STRING_CST:
6366 : return true;
6367 :
6368 25461621 : case MEM_REF:
6369 25461621 : case TARGET_MEM_REF:
6370 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6371 : here as well. */
6372 25461621 : case INDIRECT_REF:
6373 25461621 : case ARRAY_REF:
6374 25461621 : case VAR_DECL:
6375 25461621 : case PARM_DECL:
6376 25461621 : case RESULT_DECL:
6377 25461621 : case ERROR_MARK:
6378 25461621 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6379 25461621 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6380 :
6381 6 : case BIND_EXPR:
6382 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6383 :
6384 7073231 : case CALL_EXPR:
6385 7073231 : return is_access_with_size_p (ref);
6386 :
6387 : default:
6388 : return false;
6389 : }
6390 : }
6391 :
6392 : /* Give a warning for storing in something that is read-only in GCC
6393 : terms but not const in ISO C terms. */
6394 :
6395 : static void
6396 5 : readonly_warning (tree arg, enum lvalue_use use)
6397 : {
6398 5 : switch (use)
6399 : {
6400 1 : case lv_assign:
6401 1 : warning (0, "assignment of read-only location %qE", arg);
6402 1 : break;
6403 2 : case lv_increment:
6404 2 : warning (0, "increment of read-only location %qE", arg);
6405 2 : break;
6406 2 : case lv_decrement:
6407 2 : warning (0, "decrement of read-only location %qE", arg);
6408 2 : break;
6409 0 : default:
6410 0 : gcc_unreachable ();
6411 : }
6412 5 : return;
6413 : }
6414 :
6415 :
6416 : /* Return nonzero if REF is an lvalue valid for this language;
6417 : otherwise, print an error message and return zero. USE says
6418 : how the lvalue is being used and so selects the error message.
6419 : LOCATION is the location at which any error should be reported. */
6420 :
6421 : static int
6422 4714142 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6423 : {
6424 4714142 : int win = lvalue_p (ref);
6425 :
6426 4714142 : if (!win)
6427 75 : lvalue_error (loc, use);
6428 :
6429 4714142 : return win;
6430 : }
6431 :
6432 : /* Mark EXP saying that we need to be able to take the
6433 : address of it; it should not be allocated in a register.
6434 : Returns true if successful. ARRAY_REF_P is true if this
6435 : is for ARRAY_REF construction - in that case we don't want
6436 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6437 : it is fine to use ARRAY_REFs for vector subscripts on vector
6438 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6439 : than producing an error for taking the address of a register. */
6440 :
6441 : bool
6442 52470020 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6443 : {
6444 52470020 : tree x = exp;
6445 :
6446 53050531 : while (1)
6447 53050531 : switch (TREE_CODE (x))
6448 : {
6449 10029 : case VIEW_CONVERT_EXPR:
6450 10029 : if (array_ref_p
6451 9919 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6452 19948 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6453 : return true;
6454 110 : x = TREE_OPERAND (x, 0);
6455 110 : break;
6456 :
6457 396311 : case COMPONENT_REF:
6458 396311 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6459 : {
6460 6 : error ("cannot take address of bit-field %qD",
6461 3 : TREE_OPERAND (x, 1));
6462 3 : return false;
6463 : }
6464 : /* FALLTHRU */
6465 580401 : case ADDR_EXPR:
6466 580401 : case ARRAY_REF:
6467 580401 : case REALPART_EXPR:
6468 580401 : case IMAGPART_EXPR:
6469 580401 : x = TREE_OPERAND (x, 0);
6470 580401 : break;
6471 :
6472 587 : case COMPOUND_LITERAL_EXPR:
6473 587 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6474 : {
6475 16 : if (override_register)
6476 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6477 : else
6478 : {
6479 4 : error ("address of register compound literal requested");
6480 4 : return false;
6481 : }
6482 : }
6483 583 : TREE_ADDRESSABLE (x) = 1;
6484 583 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6485 583 : return true;
6486 :
6487 0 : case CONSTRUCTOR:
6488 0 : TREE_ADDRESSABLE (x) = 1;
6489 0 : return true;
6490 :
6491 1320692 : case VAR_DECL:
6492 1320692 : case CONST_DECL:
6493 1320692 : case PARM_DECL:
6494 1320692 : case RESULT_DECL:
6495 1320692 : if (C_DECL_REGISTER (x)
6496 1320692 : && DECL_NONLOCAL (x))
6497 : {
6498 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6499 : {
6500 0 : error
6501 0 : ("global register variable %qD used in nested function", x);
6502 0 : return false;
6503 : }
6504 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6505 : }
6506 1320692 : else if (C_DECL_REGISTER (x))
6507 : {
6508 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6509 4 : error ("address of global register variable %qD requested", x);
6510 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6511 : {
6512 54 : DECL_REGISTER (x) = 0;
6513 54 : TREE_ADDRESSABLE (x) = 1;
6514 54 : mark_decl_used (x, true);
6515 54 : return true;
6516 : }
6517 : else
6518 15 : error ("address of register variable %qD requested", x);
6519 19 : return false;
6520 : }
6521 :
6522 : /* FALLTHRU */
6523 51897555 : case FUNCTION_DECL:
6524 51897555 : TREE_ADDRESSABLE (x) = 1;
6525 51897555 : mark_decl_used (x, true);
6526 : /* FALLTHRU */
6527 : default:
6528 : return true;
6529 : }
6530 : }
6531 :
6532 : /* Convert EXPR to TYPE, warning about conversion problems with
6533 : constants. SEMANTIC_TYPE is the type this conversion would use
6534 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6535 : is equivalent to convert_and_check. This function is a wrapper that
6536 : handles conversions that may be different than
6537 : the usual ones because of excess precision. */
6538 :
6539 : static tree
6540 23594570 : ep_convert_and_check (location_t loc, tree type, tree expr,
6541 : tree semantic_type)
6542 : {
6543 23594570 : if (TREE_TYPE (expr) == type)
6544 : return expr;
6545 :
6546 : /* For C11, integer conversions may have results with excess
6547 : precision. */
6548 2058017 : if (flag_isoc11 || !semantic_type)
6549 2058013 : return convert_and_check (loc, type, expr);
6550 :
6551 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6552 4 : && TREE_TYPE (expr) != semantic_type)
6553 : {
6554 : /* For integers, we need to check the real conversion, not
6555 : the conversion to the excess precision type. */
6556 4 : expr = convert_and_check (loc, semantic_type, expr);
6557 : }
6558 : /* Result type is the excess precision type, which should be
6559 : large enough, so do not check. */
6560 4 : return convert (type, expr);
6561 : }
6562 :
6563 : /* If EXPR refers to a built-in declared without a prototype returns
6564 : the actual type of the built-in and, if non-null, set *BLTIN to
6565 : a pointer to the built-in. Otherwise return the type of EXPR
6566 : and clear *BLTIN if non-null. */
6567 :
6568 : static tree
6569 2834196 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6570 : {
6571 2834196 : tree dummy;
6572 2834196 : if (!bltin)
6573 0 : bltin = &dummy;
6574 :
6575 2834196 : *bltin = NULL_TREE;
6576 :
6577 2834196 : tree type = TREE_TYPE (expr);
6578 2834196 : if (TREE_CODE (expr) != ADDR_EXPR)
6579 : return type;
6580 :
6581 208541 : tree oper = TREE_OPERAND (expr, 0);
6582 208541 : if (!DECL_P (oper)
6583 170042 : || TREE_CODE (oper) != FUNCTION_DECL
6584 244957 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6585 : return type;
6586 :
6587 2085 : built_in_function code = DECL_FUNCTION_CODE (oper);
6588 2085 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6589 : return type;
6590 :
6591 1977 : if ((*bltin = builtin_decl_implicit (code)))
6592 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6593 :
6594 : return type;
6595 : }
6596 :
6597 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6598 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6599 : if folded to an integer constant then the unselected half may
6600 : contain arbitrary operations not normally permitted in constant
6601 : expressions. Set the location of the expression to LOC. */
6602 :
6603 : tree
6604 408661 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6605 : tree op1, tree op1_original_type, location_t op1_loc,
6606 : tree op2, tree op2_original_type, location_t op2_loc)
6607 : {
6608 408661 : tree type1;
6609 408661 : tree type2;
6610 408661 : tree result_type = NULL;
6611 408661 : tree semantic_result_type = NULL;
6612 408661 : tree orig_op1 = op1, orig_op2 = op2;
6613 408661 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6614 408661 : bool ifexp_int_operands;
6615 408661 : tree ret;
6616 :
6617 408661 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6618 115958 : if (op1_int_operands)
6619 115958 : op1 = remove_c_maybe_const_expr (op1);
6620 408661 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6621 151117 : if (op2_int_operands)
6622 151117 : op2 = remove_c_maybe_const_expr (op2);
6623 408661 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6624 242143 : if (ifexp_int_operands)
6625 242143 : ifexp = remove_c_maybe_const_expr (ifexp);
6626 :
6627 : /* Promote both alternatives. */
6628 :
6629 408661 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6630 404744 : op1 = default_conversion (op1);
6631 408661 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6632 378821 : op2 = default_conversion (op2);
6633 :
6634 408661 : if (TREE_CODE (ifexp) == ERROR_MARK
6635 408592 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6636 817239 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6637 83 : return error_mark_node;
6638 :
6639 408578 : tree bltin1 = NULL_TREE;
6640 408578 : tree bltin2 = NULL_TREE;
6641 408578 : type1 = type_or_builtin_type (op1, &bltin1);
6642 408578 : const enum tree_code code1 = TREE_CODE (type1);
6643 408578 : type2 = type_or_builtin_type (op2, &bltin2);
6644 408578 : const enum tree_code code2 = TREE_CODE (type2);
6645 :
6646 408578 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6647 1 : return error_mark_node;
6648 :
6649 408577 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6650 1 : return error_mark_node;
6651 :
6652 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6653 : In C99 they will be pointers by now. */
6654 408576 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6655 : {
6656 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6657 1 : return error_mark_node;
6658 : }
6659 :
6660 408575 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6661 408569 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6662 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6663 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6664 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6665 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6666 : {
6667 7 : semantic_result_type = c_common_type (type1, type2);
6668 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6669 : {
6670 6 : op1 = TREE_OPERAND (op1, 0);
6671 6 : type1 = TREE_TYPE (op1);
6672 6 : gcc_assert (TREE_CODE (type1) == code1);
6673 : }
6674 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6675 : {
6676 1 : op2 = TREE_OPERAND (op2, 0);
6677 1 : type2 = TREE_TYPE (op2);
6678 1 : gcc_assert (TREE_CODE (type2) == code2);
6679 : }
6680 : }
6681 :
6682 408575 : if (warn_cxx_compat)
6683 : {
6684 418 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6685 418 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6686 :
6687 418 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6688 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6689 424 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6690 2 : warning_at (colon_loc, OPT_Wc___compat,
6691 : "different enum types in conditional is "
6692 : "invalid in C++: %qT vs %qT", t1, t2);
6693 : }
6694 :
6695 408575 : if (warn_zero_as_null_pointer_constant
6696 20 : && c_inhibit_evaluation_warnings == 0)
6697 : {
6698 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6699 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6700 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6701 : "zero as null pointer constant");
6702 :
6703 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6704 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6705 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6706 : "zero as null pointer constant");
6707 : }
6708 :
6709 : /* Quickly detect the usual case where op1 and op2 have the same type
6710 : after promotion. */
6711 408575 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6712 : {
6713 273976 : if (type1 == type2)
6714 : result_type = type1;
6715 : else
6716 6369 : result_type = TYPE_MAIN_VARIANT (type1);
6717 : }
6718 134599 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6719 79980 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6720 54667 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6721 26152 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6722 : {
6723 : /* In C11, a conditional expression between a floating-point
6724 : type and an integer type should convert the integer type to
6725 : the evaluation format of the floating-point type, with
6726 : possible excess precision. */
6727 28615 : tree eptype1 = type1;
6728 28615 : tree eptype2 = type2;
6729 28615 : if (flag_isoc11)
6730 : {
6731 28302 : tree eptype;
6732 11640 : if (ANY_INTEGRAL_TYPE_P (type1)
6733 28302 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6734 : {
6735 2 : eptype2 = eptype;
6736 2 : if (!semantic_result_type)
6737 2 : semantic_result_type = c_common_type (type1, type2);
6738 : }
6739 501 : else if (ANY_INTEGRAL_TYPE_P (type2)
6740 28300 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6741 : {
6742 4626 : eptype1 = eptype;
6743 4626 : if (!semantic_result_type)
6744 4626 : semantic_result_type = c_common_type (type1, type2);
6745 : }
6746 : }
6747 28615 : result_type = c_common_type (eptype1, eptype2);
6748 28615 : if (result_type == error_mark_node)
6749 : return error_mark_node;
6750 28613 : do_warn_double_promotion (result_type, type1, type2,
6751 : "implicit conversion from %qT to %qT to "
6752 : "match other result of conditional",
6753 : colon_loc);
6754 :
6755 : /* If -Wsign-compare, warn here if type1 and type2 have
6756 : different signedness. We'll promote the signed to unsigned
6757 : and later code won't know it used to be different.
6758 : Do this check on the original types, so that explicit casts
6759 : will be considered, but default promotions won't. */
6760 28613 : if (c_inhibit_evaluation_warnings == 0)
6761 : {
6762 28422 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6763 28422 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6764 :
6765 28422 : if (unsigned_op1 ^ unsigned_op2)
6766 : {
6767 13011 : bool ovf;
6768 :
6769 : /* Do not warn if the result type is signed, since the
6770 : signed type will only be chosen if it can represent
6771 : all the values of the unsigned type. */
6772 13011 : if (!TYPE_UNSIGNED (result_type))
6773 : /* OK */;
6774 : else
6775 : {
6776 12907 : bool op1_maybe_const = true;
6777 12907 : bool op2_maybe_const = true;
6778 :
6779 : /* Do not warn if the signed quantity is an
6780 : unsuffixed integer literal (or some static
6781 : constant expression involving such literals) and
6782 : it is non-negative. This warning requires the
6783 : operands to be folded for best results, so do
6784 : that folding in this case even without
6785 : warn_sign_compare to avoid warning options
6786 : possibly affecting code generation. */
6787 12907 : c_inhibit_evaluation_warnings
6788 12907 : += (ifexp == truthvalue_false_node);
6789 12907 : op1 = c_fully_fold (op1, require_constant_value,
6790 : &op1_maybe_const);
6791 12907 : c_inhibit_evaluation_warnings
6792 12907 : -= (ifexp == truthvalue_false_node);
6793 :
6794 12907 : c_inhibit_evaluation_warnings
6795 12907 : += (ifexp == truthvalue_true_node);
6796 12907 : op2 = c_fully_fold (op2, require_constant_value,
6797 : &op2_maybe_const);
6798 12907 : c_inhibit_evaluation_warnings
6799 12907 : -= (ifexp == truthvalue_true_node);
6800 :
6801 12907 : if (warn_sign_compare)
6802 : {
6803 1245 : if ((unsigned_op2
6804 685 : && tree_expr_nonnegative_warnv_p (op1, &ovf))
6805 1248 : || (unsigned_op1
6806 560 : && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6807 : /* OK */;
6808 10 : else if (unsigned_op2)
6809 3 : warning_at (op1_loc, OPT_Wsign_compare,
6810 : "operand of %<?:%> changes signedness from "
6811 : "%qT to %qT due to unsignedness of other "
6812 3 : "operand", TREE_TYPE (orig_op1),
6813 3 : TREE_TYPE (orig_op2));
6814 : else
6815 7 : warning_at (op2_loc, OPT_Wsign_compare,
6816 : "operand of %<?:%> changes signedness from "
6817 : "%qT to %qT due to unsignedness of other "
6818 7 : "operand", TREE_TYPE (orig_op2),
6819 7 : TREE_TYPE (orig_op1));
6820 : }
6821 12907 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6822 7562 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6823 12907 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6824 4391 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6825 : }
6826 : }
6827 : }
6828 : }
6829 105984 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6830 : {
6831 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6832 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6833 : "ISO C forbids conditional expr with only one void side");
6834 25995 : result_type = void_type_node;
6835 : }
6836 79989 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6837 : {
6838 79592 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6839 79592 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6840 79592 : addr_space_t as_common;
6841 :
6842 79592 : if (comp_target_types (colon_loc, type1, type2))
6843 : {
6844 2619 : ifexp = save_expr (ifexp);
6845 2619 : result_type = common_pointer_type (type1, type2, ifexp);
6846 : }
6847 76973 : else if (null_pointer_constant_p (orig_op1))
6848 : result_type = type2;
6849 72811 : else if (null_pointer_constant_p (orig_op2))
6850 : result_type = type1;
6851 34803 : else if (!addr_space_superset (as1, as2, &as_common))
6852 : {
6853 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6854 : "used in conditional expression");
6855 0 : return error_mark_node;
6856 : }
6857 34803 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6858 330 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6859 34805 : || (VOID_TYPE_P (TREE_TYPE (type2))
6860 34428 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6861 : {
6862 34755 : tree t1 = TREE_TYPE (type1);
6863 34755 : tree t2 = TREE_TYPE (type2);
6864 34755 : if (!(VOID_TYPE_P (t1)
6865 329 : && !TYPE_ATOMIC (t1)))
6866 : {
6867 : /* roles are swapped */
6868 34427 : t1 = t2;
6869 34427 : t2 = TREE_TYPE (type1);
6870 : }
6871 34755 : tree t2_stripped = strip_array_types (t2);
6872 34755 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6873 34755 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6874 : {
6875 18 : if (!flag_isoc23)
6876 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6877 : "pointer to array loses qualifier "
6878 : "in conditional expression");
6879 12 : else if (warn_c11_c23_compat > 0)
6880 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6881 : "pointer to array loses qualifier "
6882 : "in conditional expression in ISO C before C23");
6883 : }
6884 34755 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6885 4 : pedwarn (colon_loc, OPT_Wpedantic,
6886 : "ISO C forbids conditional expr between "
6887 : "%<void *%> and function pointer");
6888 : /* for array, use qualifiers of element type */
6889 34755 : if (flag_isoc23)
6890 11006 : t2 = t2_stripped;
6891 34755 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6892 : }
6893 : /* Objective-C pointer comparisons are a bit more lenient. */
6894 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6895 0 : result_type = objc_common_type (type1, type2);
6896 : else
6897 : {
6898 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6899 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6900 48 : if (!flag_isoc99)
6901 : /* This downgrade to a warning ensures that -std=gnu89
6902 : -pedantic-errors does not flag these mismatches between
6903 : builtins as errors (as diagnostics::kind::permerror would)
6904 : ISO C99 and later do not have implicit function declarations,
6905 : so the mismatch cannot occur naturally there. */
6906 5 : kind = (bltin1 && bltin2
6907 5 : ? diagnostics::kind::warning
6908 : : diagnostics::kind::pedwarn);
6909 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6910 : "pointer type mismatch "
6911 : "in conditional expression"))
6912 : {
6913 46 : inform (op1_loc, "first expression has type %qT", type1);
6914 46 : inform (op2_loc, "second expression has type %qT", type2);
6915 : }
6916 48 : result_type = c_build_pointer_type
6917 48 : (c_build_qualified_type (void_type_node, qual));
6918 : }
6919 : }
6920 397 : else if (code1 == POINTER_TYPE
6921 277 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6922 : {
6923 273 : if (!null_pointer_constant_p (orig_op2))
6924 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6925 : "pointer/integer type mismatch "
6926 : "in conditional expression");
6927 : else
6928 : {
6929 259 : op2 = null_pointer_node;
6930 : }
6931 : result_type = type1;
6932 : }
6933 124 : else if (code2 == POINTER_TYPE
6934 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6935 : {
6936 86 : if (!null_pointer_constant_p (orig_op1))
6937 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6938 : "pointer/integer type mismatch "
6939 : "in conditional expression");
6940 : else
6941 : {
6942 72 : op1 = null_pointer_node;
6943 : }
6944 : result_type = type2;
6945 : }
6946 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6947 : type nullptr_t and the other is a pointer, the result type is the pointer
6948 : type." */
6949 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6950 : result_type = type2;
6951 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6952 : result_type = type1;
6953 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6954 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
6955 8 : TYPE_MAIN_VARIANT (type2)))
6956 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6957 8 : TYPE_MAIN_VARIANT (type2));
6958 :
6959 408551 : if (!result_type)
6960 : {
6961 22 : if (flag_cond_mismatch)
6962 0 : result_type = void_type_node;
6963 : else
6964 : {
6965 22 : error_at (colon_loc, "type mismatch in conditional expression");
6966 22 : return error_mark_node;
6967 : }
6968 : }
6969 :
6970 : /* Merge const and volatile flags of the incoming types. */
6971 408551 : result_type
6972 408551 : = build_type_variant (result_type,
6973 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
6974 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6975 :
6976 408551 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
6977 : semantic_result_type);
6978 408551 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
6979 : semantic_result_type);
6980 :
6981 408551 : if (ifexp_bcp && ifexp == truthvalue_true_node)
6982 : {
6983 10 : op2_int_operands = true;
6984 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
6985 : }
6986 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
6987 : {
6988 49 : op1_int_operands = true;
6989 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
6990 : }
6991 914109 : int_const = int_operands = (ifexp_int_operands
6992 408551 : && op1_int_operands
6993 408551 : && op2_int_operands);
6994 97007 : if (int_operands)
6995 : {
6996 97007 : int_const = ((ifexp == truthvalue_true_node
6997 58720 : && TREE_CODE (orig_op1) == INTEGER_CST
6998 58706 : && !TREE_OVERFLOW (orig_op1))
6999 97021 : || (ifexp == truthvalue_false_node
7000 38241 : && TREE_CODE (orig_op2) == INTEGER_CST
7001 38225 : && !TREE_OVERFLOW (orig_op2)));
7002 : }
7003 :
7004 : /* Need to convert condition operand into a vector mask. */
7005 408551 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7006 : {
7007 0 : tree vectype = TREE_TYPE (ifexp);
7008 0 : tree elem_type = TREE_TYPE (vectype);
7009 0 : tree zero = build_int_cst (elem_type, 0);
7010 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7011 0 : tree cmp_type = truth_type_for (vectype);
7012 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7013 : }
7014 :
7015 408551 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7016 96963 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7017 : else
7018 : {
7019 311588 : if (int_operands)
7020 : {
7021 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7022 : nested inside of the expression. */
7023 76 : op1 = c_fully_fold (op1, false, NULL);
7024 76 : op2 = c_fully_fold (op2, false, NULL);
7025 : }
7026 311588 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7027 311588 : if (int_operands)
7028 76 : ret = note_integer_operands (ret);
7029 : }
7030 408551 : if (semantic_result_type)
7031 4635 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7032 :
7033 408551 : protected_set_expr_location (ret, colon_loc);
7034 :
7035 : /* If the OP1 and OP2 are the same and don't have side-effects,
7036 : warn here, because the COND_EXPR will be turned into OP1. */
7037 408551 : if (warn_duplicated_branches
7038 34 : && TREE_CODE (ret) == COND_EXPR
7039 408585 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7040 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7041 : "this condition has identical branches");
7042 :
7043 : return ret;
7044 : }
7045 :
7046 : /* EXPR is an expression, location LOC, whose result is discarded.
7047 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7048 : whose right-hand operand is such a call, possibly recursively). */
7049 :
7050 : static void
7051 6471941 : maybe_warn_nodiscard (location_t loc, tree expr)
7052 : {
7053 6471941 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7054 : return;
7055 3957008 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7056 : {
7057 49300 : expr = TREE_OPERAND (expr, 1);
7058 49300 : if (EXPR_HAS_LOCATION (expr))
7059 32199 : loc = EXPR_LOCATION (expr);
7060 : }
7061 3907708 : if (TREE_CODE (expr) != CALL_EXPR)
7062 : return;
7063 343590 : tree fn = CALL_EXPR_FN (expr);
7064 343590 : if (!fn)
7065 : return;
7066 343574 : tree attr;
7067 343574 : if (TREE_CODE (fn) == ADDR_EXPR
7068 342909 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7069 686483 : && (attr = lookup_attribute ("nodiscard",
7070 342909 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7071 : {
7072 3 : fn = TREE_OPERAND (fn, 0);
7073 3 : tree args = TREE_VALUE (attr);
7074 3 : if (args)
7075 1 : args = TREE_VALUE (args);
7076 3 : auto_diagnostic_group d;
7077 3 : auto_urlify_attributes sentinel;
7078 3 : int warned;
7079 3 : if (args)
7080 1 : warned = warning_at (loc, OPT_Wunused_result,
7081 : "ignoring return value of %qD, declared with "
7082 : "attribute %<nodiscard%>: %E", fn, args);
7083 : else
7084 2 : warned = warning_at (loc, OPT_Wunused_result,
7085 : "ignoring return value of %qD, declared with "
7086 : "attribute %<nodiscard%>", fn);
7087 3 : if (warned)
7088 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7089 3 : }
7090 : else
7091 : {
7092 343571 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7093 343571 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7094 343571 : if (!attr)
7095 343559 : return;
7096 12 : tree args = TREE_VALUE (attr);
7097 12 : if (args)
7098 5 : args = TREE_VALUE (args);
7099 12 : auto_diagnostic_group d;
7100 12 : auto_urlify_attributes sentinel;
7101 12 : int warned;
7102 12 : if (args)
7103 5 : warned = warning_at (loc, OPT_Wunused_result,
7104 : "ignoring return value of type %qT, declared "
7105 : "with attribute %<nodiscard%>: %E",
7106 : rettype, args);
7107 : else
7108 7 : warned = warning_at (loc, OPT_Wunused_result,
7109 : "ignoring return value of type %qT, declared "
7110 : "with attribute %<nodiscard%>", rettype);
7111 12 : if (warned)
7112 : {
7113 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7114 : {
7115 11 : fn = TREE_OPERAND (fn, 0);
7116 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7117 11 : inform (DECL_SOURCE_LOCATION (fn),
7118 : "in call to %qD, declared here", fn);
7119 : }
7120 : }
7121 12 : }
7122 : }
7123 :
7124 : /* Return a compound expression that performs two expressions and
7125 : returns the value of the second of them.
7126 :
7127 : LOC is the location of the COMPOUND_EXPR. */
7128 :
7129 : tree
7130 111770 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7131 : {
7132 111770 : bool expr1_int_operands, expr2_int_operands;
7133 111770 : tree eptype = NULL_TREE;
7134 111770 : tree ret;
7135 :
7136 111770 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7137 326 : if (expr1_int_operands)
7138 326 : expr1 = remove_c_maybe_const_expr (expr1);
7139 111770 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7140 66105 : if (expr2_int_operands)
7141 66105 : expr2 = remove_c_maybe_const_expr (expr2);
7142 :
7143 111770 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7144 0 : expr1 = TREE_OPERAND (expr1, 0);
7145 111770 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7146 : {
7147 1 : eptype = TREE_TYPE (expr2);
7148 1 : expr2 = TREE_OPERAND (expr2, 0);
7149 : }
7150 :
7151 111770 : if (!TREE_SIDE_EFFECTS (expr1))
7152 : {
7153 : /* The left-hand operand of a comma expression is like an expression
7154 : statement: with -Wunused, we should warn if it doesn't have
7155 : any side-effects, unless it was explicitly cast to (void). */
7156 8406 : if (warn_unused_value)
7157 : {
7158 1257 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7159 1257 : && CONVERT_EXPR_P (expr1))
7160 : ; /* (void) a, b */
7161 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7162 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7163 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7164 : ; /* (void) a, (void) b, c */
7165 : else
7166 11 : warning_at (loc, OPT_Wunused_value,
7167 : "left-hand operand of comma expression has no effect");
7168 : }
7169 : }
7170 103364 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7171 13111 : && warn_unused_value)
7172 : {
7173 : tree r = expr1;
7174 : location_t cloc = loc;
7175 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7176 : {
7177 2417 : if (EXPR_HAS_LOCATION (r))
7178 2417 : cloc = EXPR_LOCATION (r);
7179 2417 : r = TREE_OPERAND (r, 1);
7180 : }
7181 2416 : if (!TREE_SIDE_EFFECTS (r)
7182 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7183 2420 : && !CONVERT_EXPR_P (r))
7184 4 : warning_at (cloc, OPT_Wunused_value,
7185 : "right-hand operand of comma expression has no effect");
7186 : }
7187 :
7188 : /* With -Wunused, we should also warn if the left-hand operand does have
7189 : side-effects, but computes a value which is not used. For example, in
7190 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7191 : so we should issue a warning. */
7192 100948 : else if (warn_unused_value)
7193 35400 : warn_if_unused_value (expr1, loc);
7194 :
7195 111770 : maybe_warn_nodiscard (loc, expr1);
7196 :
7197 111770 : if (expr2 == error_mark_node)
7198 : return error_mark_node;
7199 :
7200 111757 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7201 :
7202 111757 : if (flag_isoc99
7203 : && expr1_int_operands
7204 111301 : && expr2_int_operands)
7205 153 : ret = note_integer_operands (ret);
7206 :
7207 111757 : if (eptype)
7208 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7209 :
7210 111757 : protected_set_expr_location (ret, loc);
7211 111757 : return ret;
7212 : }
7213 :
7214 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7215 : which we are casting. OTYPE is the type of the expression being
7216 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7217 : of the cast. -Wcast-qual appeared on the command line. Named
7218 : address space qualifiers are not handled here, because they result
7219 : in different warnings. */
7220 :
7221 : static void
7222 13405 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7223 : {
7224 13405 : tree in_type = type;
7225 13405 : tree in_otype = otype;
7226 13405 : int added = 0;
7227 13405 : int discarded = 0;
7228 13575 : bool is_const;
7229 :
7230 : /* Check that the qualifiers on IN_TYPE are a superset of the
7231 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7232 : nodes is uninteresting and we stop as soon as we hit a
7233 : non-POINTER_TYPE node on either type. */
7234 13575 : do
7235 : {
7236 13575 : in_otype = TREE_TYPE (in_otype);
7237 13575 : in_type = TREE_TYPE (in_type);
7238 :
7239 : /* GNU C allows cv-qualified function types. 'const' means the
7240 : function is very pure, 'volatile' means it can't return. We
7241 : need to warn when such qualifiers are added, not when they're
7242 : taken away. */
7243 13575 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7244 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7245 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7246 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7247 : else
7248 13557 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7249 13557 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7250 : }
7251 13575 : while (TREE_CODE (in_type) == POINTER_TYPE
7252 13575 : && TREE_CODE (in_otype) == POINTER_TYPE);
7253 :
7254 13405 : if (added)
7255 2 : warning_at (loc, OPT_Wcast_qual,
7256 : "cast adds %q#v qualifier to function type", added);
7257 :
7258 13405 : if (discarded)
7259 : /* There are qualifiers present in IN_OTYPE that are not present
7260 : in IN_TYPE. */
7261 1117 : warning_at (loc, OPT_Wcast_qual,
7262 : "cast discards %qv qualifier from pointer target type",
7263 : discarded);
7264 :
7265 13405 : if (added || discarded)
7266 : return;
7267 :
7268 : /* A cast from **T to const **T is unsafe, because it can cause a
7269 : const value to be changed with no additional warning. We only
7270 : issue this warning if T is the same on both sides, and we only
7271 : issue the warning if there are the same number of pointers on
7272 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7273 : cast is unsafe when a qualifier is added at one level and const
7274 : is not present at all outer levels.
7275 :
7276 : To issue this warning, we check at each level whether the cast
7277 : adds new qualifiers not already seen. We don't need to special
7278 : case function types, as they won't have the same
7279 : TYPE_MAIN_VARIANT. */
7280 :
7281 12286 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7282 : return;
7283 51 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7284 : return;
7285 :
7286 48 : in_type = type;
7287 48 : in_otype = otype;
7288 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7289 130 : do
7290 : {
7291 130 : in_type = TREE_TYPE (in_type);
7292 130 : in_otype = TREE_TYPE (in_otype);
7293 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7294 130 : && !is_const)
7295 : {
7296 27 : warning_at (loc, OPT_Wcast_qual,
7297 : "to be safe all intermediate pointers in cast from "
7298 : "%qT to %qT must be %<const%> qualified",
7299 : otype, type);
7300 27 : break;
7301 : }
7302 103 : if (is_const)
7303 72 : is_const = TYPE_READONLY (in_type);
7304 : }
7305 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7306 : }
7307 :
7308 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7309 :
7310 : static bool
7311 1280 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7312 : {
7313 1280 : if (error_operand_p (t1) || error_operand_p (t2))
7314 : return true;
7315 :
7316 1279 : t1 = TYPE_MAIN_VARIANT (t1);
7317 1279 : t2 = TYPE_MAIN_VARIANT (t2);
7318 :
7319 1279 : if (TREE_CODE (t1) == POINTER_TYPE
7320 213 : && TREE_CODE (t2) == POINTER_TYPE)
7321 : return true;
7322 :
7323 : /* Only the precision of the parameter matters. This check should
7324 : make sure that the callee does not see undefined values in argument
7325 : registers. */
7326 1087 : if (INTEGRAL_TYPE_P (t1)
7327 905 : && INTEGRAL_TYPE_P (t2)
7328 1424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7329 : return true;
7330 :
7331 930 : return comptypes (t1, t2);
7332 : }
7333 :
7334 : /* Check if a type cast between two function types can be considered safe. */
7335 :
7336 : static bool
7337 7469 : c_safe_function_type_cast_p (tree t1, tree t2)
7338 : {
7339 7469 : if (TREE_TYPE (t1) == void_type_node &&
7340 5479 : TYPE_ARG_TYPES (t1) == void_list_node)
7341 : return true;
7342 :
7343 3714 : if (TREE_TYPE (t2) == void_type_node &&
7344 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7345 : return true;
7346 :
7347 884 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7348 : return false;
7349 :
7350 189 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7351 481 : t1 && t2;
7352 292 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7353 396 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7354 : return false;
7355 :
7356 : return true;
7357 : }
7358 :
7359 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7360 : LOC is the location of the cast-- typically the open paren of the cast. */
7361 :
7362 : tree
7363 120176325 : build_c_cast (location_t loc, tree type, tree expr)
7364 : {
7365 120176325 : tree value;
7366 :
7367 120176325 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7368 :
7369 120176325 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7370 66 : expr = TREE_OPERAND (expr, 0);
7371 :
7372 120176325 : value = expr;
7373 120176325 : if (int_operands)
7374 6867771 : value = remove_c_maybe_const_expr (value);
7375 :
7376 120176325 : if (type == error_mark_node || expr == error_mark_node)
7377 : return error_mark_node;
7378 :
7379 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7380 : only in <protocol> qualifications. But when constructing cast expressions,
7381 : the protocols do matter and must be kept around. */
7382 120175303 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7383 0 : return build1 (NOP_EXPR, type, expr);
7384 :
7385 120175303 : type = TYPE_MAIN_VARIANT (type);
7386 :
7387 120175303 : if (TREE_CODE (type) == ARRAY_TYPE)
7388 : {
7389 13 : error_at (loc, "cast specifies array type");
7390 13 : return error_mark_node;
7391 : }
7392 :
7393 120175290 : if (TREE_CODE (type) == FUNCTION_TYPE)
7394 : {
7395 6 : error_at (loc, "cast specifies function type");
7396 6 : return error_mark_node;
7397 : }
7398 :
7399 120175284 : if (!VOID_TYPE_P (type))
7400 : {
7401 120131064 : value = require_complete_type (loc, value);
7402 120131064 : if (value == error_mark_node)
7403 : return error_mark_node;
7404 : }
7405 :
7406 120175283 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7407 : {
7408 18109753 : if (RECORD_OR_UNION_TYPE_P (type)
7409 18109753 : && pedwarn (loc, OPT_Wpedantic,
7410 : "ISO C forbids casting nonscalar to the same type"))
7411 : ;
7412 18109749 : else if (warn_useless_cast)
7413 7 : warning_at (loc, OPT_Wuseless_cast,
7414 : "useless cast to type %qT", type);
7415 :
7416 : /* Convert to remove any qualifiers from VALUE's type. */
7417 18109753 : value = convert (type, value);
7418 : }
7419 102065530 : else if (TREE_CODE (type) == UNION_TYPE)
7420 : {
7421 137 : tree field;
7422 :
7423 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7424 185 : if (TREE_TYPE (field) != error_mark_node
7425 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7426 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7427 : break;
7428 :
7429 137 : if (field)
7430 : {
7431 130 : tree t;
7432 130 : bool maybe_const = true;
7433 :
7434 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7435 130 : t = c_fully_fold (value, false, &maybe_const);
7436 130 : t = build_constructor_single (type, field, t);
7437 130 : if (!maybe_const)
7438 15 : t = c_wrap_maybe_const (t, true);
7439 130 : t = digest_init (loc, field, type, t,
7440 : NULL_TREE, false, false, false, true, false, false);
7441 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7442 130 : return t;
7443 : }
7444 7 : error_at (loc, "cast to union type from type not present in union");
7445 7 : return error_mark_node;
7446 : }
7447 : else
7448 : {
7449 102065393 : tree otype, ovalue;
7450 :
7451 102065393 : if (type == void_type_node)
7452 : {
7453 18250 : tree t = build1 (CONVERT_EXPR, type, value);
7454 18250 : SET_EXPR_LOCATION (t, loc);
7455 18250 : return t;
7456 : }
7457 :
7458 102047143 : otype = TREE_TYPE (value);
7459 :
7460 : /* Optionally warn about potentially worrisome casts. */
7461 102047143 : if (warn_cast_qual
7462 179497 : && TREE_CODE (type) == POINTER_TYPE
7463 26463 : && TREE_CODE (otype) == POINTER_TYPE)
7464 13405 : handle_warn_cast_qual (loc, type, otype);
7465 :
7466 : /* Warn about conversions between pointers to disjoint
7467 : address spaces. */
7468 102047143 : if (TREE_CODE (type) == POINTER_TYPE
7469 3330715 : && TREE_CODE (otype) == POINTER_TYPE
7470 105033131 : && !null_pointer_constant_p (value))
7471 : {
7472 2932315 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7473 2932315 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7474 2932315 : addr_space_t as_common;
7475 :
7476 2932315 : if (!addr_space_superset (as_to, as_from, &as_common))
7477 : {
7478 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7479 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7480 : "from disjoint generic address space pointer",
7481 : c_addr_space_name (as_to));
7482 :
7483 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7484 0 : warning_at (loc, 0, "cast to generic address space pointer "
7485 : "from disjoint %qs address space pointer",
7486 : c_addr_space_name (as_from));
7487 :
7488 : else
7489 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7490 : "from disjoint %qs address space pointer",
7491 : c_addr_space_name (as_to),
7492 : c_addr_space_name (as_from));
7493 : }
7494 :
7495 : /* Warn of new allocations that are not big enough for the target
7496 : type. */
7497 2932315 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7498 805 : if (tree fndecl = get_callee_fndecl (value))
7499 801 : if (DECL_IS_MALLOC (fndecl))
7500 : {
7501 517 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7502 517 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7503 517 : if (alloc_size)
7504 159 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7505 : alloc_size);
7506 : }
7507 : }
7508 :
7509 : /* Warn about possible alignment problems. */
7510 102047143 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7511 7 : && TREE_CODE (type) == POINTER_TYPE
7512 7 : && TREE_CODE (otype) == POINTER_TYPE
7513 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7514 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7515 : /* Don't warn about opaque types, where the actual alignment
7516 : restriction is unknown. */
7517 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7518 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7519 102047157 : && min_align_of_type (TREE_TYPE (type))
7520 7 : > min_align_of_type (TREE_TYPE (otype)))
7521 5 : warning_at (loc, OPT_Wcast_align,
7522 : "cast increases required alignment of target type");
7523 :
7524 102047143 : if ((TREE_CODE (type) == INTEGER_TYPE
7525 102047143 : || TREE_CODE (type) == BITINT_TYPE)
7526 7083546 : && TREE_CODE (otype) == POINTER_TYPE
7527 102072374 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7528 : /* Unlike conversion of integers to pointers, where the
7529 : warning is disabled for converting constants because
7530 : of cases such as SIG_*, warn about converting constant
7531 : pointers to integers. In some cases it may cause unwanted
7532 : sign extension, and a warning is appropriate. */
7533 1450 : warning_at (loc, OPT_Wpointer_to_int_cast,
7534 : "cast from pointer to integer of different size");
7535 :
7536 102047143 : if ((TREE_CODE (value) == CALL_EXPR
7537 34645875 : && !is_access_with_size_p (value))
7538 136693016 : && TREE_CODE (type) != TREE_CODE (otype))
7539 2286 : warning_at (loc, OPT_Wbad_function_cast,
7540 : "cast from function call of type %qT "
7541 : "to non-matching type %qT", otype, type);
7542 :
7543 102047143 : if (TREE_CODE (type) == POINTER_TYPE
7544 3330715 : && (TREE_CODE (otype) == INTEGER_TYPE
7545 3330715 : || TREE_CODE (otype) == BITINT_TYPE)
7546 344641 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7547 : /* Don't warn about converting any constant. */
7548 102354233 : && !TREE_CONSTANT (value))
7549 407 : warning_at (loc,
7550 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7551 : "of different size");
7552 :
7553 102047143 : if (warn_strict_aliasing <= 2)
7554 93515140 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7555 :
7556 : /* If pedantic, warn for conversions between function and object
7557 : pointer types, except for converting a null pointer constant
7558 : to function pointer type. */
7559 102047143 : if (pedantic
7560 238922 : && TREE_CODE (type) == POINTER_TYPE
7561 144180 : && TREE_CODE (otype) == POINTER_TYPE
7562 2782 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7563 102047164 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7564 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7565 : "conversion of function pointer to object pointer type");
7566 :
7567 102047143 : if (pedantic
7568 238922 : && TREE_CODE (type) == POINTER_TYPE
7569 144180 : && TREE_CODE (otype) == POINTER_TYPE
7570 2782 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7571 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7572 102047153 : && !null_pointer_constant_p (value))
7573 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7574 : "conversion of object pointer to function pointer type");
7575 :
7576 102047143 : if (TREE_CODE (type) == POINTER_TYPE
7577 3330715 : && TREE_CODE (otype) == POINTER_TYPE
7578 2985988 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7579 8581 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7580 102054612 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7581 7469 : TREE_TYPE (otype)))
7582 799 : warning_at (loc, OPT_Wcast_function_type,
7583 : "cast between incompatible function types"
7584 : " from %qT to %qT", otype, type);
7585 :
7586 102047143 : ovalue = value;
7587 : /* If converting to boolean a value with integer operands that
7588 : is not itself represented as an INTEGER_CST, the call below
7589 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7590 : build_binary_op as called by c_common_truthvalue_conversion
7591 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7592 : subexpression has been fully folded. To avoid nested
7593 : C_MAYBE_CONST_EXPR, ensure that
7594 : c_objc_common_truthvalue_conversion receives an argument
7595 : properly marked as having integer operands in that case. */
7596 102047143 : if (int_operands
7597 6677505 : && TREE_CODE (value) != INTEGER_CST
7598 102047185 : && (TREE_CODE (type) == BOOLEAN_TYPE
7599 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7600 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7601 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7602 28 : value = note_integer_operands (value);
7603 102047143 : value = convert (type, value);
7604 :
7605 : /* Ignore any integer overflow caused by the cast. */
7606 102047143 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7607 : {
7608 6624009 : if (TREE_OVERFLOW_P (ovalue))
7609 : {
7610 9 : if (!TREE_OVERFLOW (value))
7611 : {
7612 : /* Avoid clobbering a shared constant. */
7613 0 : value = copy_node (value);
7614 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7615 : }
7616 : }
7617 6624000 : else if (TREE_OVERFLOW (value))
7618 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7619 3085 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7620 : }
7621 : }
7622 :
7623 : /* Don't let a cast be an lvalue. */
7624 120156896 : if (lvalue_p (value))
7625 90568 : value = non_lvalue_loc (loc, value);
7626 :
7627 : /* Don't allow the results of casting to floating-point or complex
7628 : types be confused with actual constants, or casts involving
7629 : integer and pointer types other than direct integer-to-integer
7630 : and integer-to-pointer be confused with integer constant
7631 : expressions and null pointer constants. */
7632 120156896 : if (TREE_CODE (value) == REAL_CST
7633 120107010 : || TREE_CODE (value) == COMPLEX_CST
7634 240241283 : || (TREE_CODE (value) == INTEGER_CST
7635 6872830 : && !((TREE_CODE (expr) == INTEGER_CST
7636 6799417 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7637 66010 : || TREE_CODE (expr) == REAL_CST
7638 : || TREE_CODE (expr) == COMPLEX_CST)))
7639 134152 : value = build1 (NOP_EXPR, type, value);
7640 :
7641 : /* If the expression has integer operands and so can occur in an
7642 : unevaluated part of an integer constant expression, ensure the
7643 : return value reflects this. */
7644 120156896 : if (int_operands
7645 6853852 : && INTEGRAL_TYPE_P (type)
7646 6409564 : && value != error_mark_node
7647 126566459 : && !EXPR_INT_CONST_OPERANDS (value))
7648 11 : value = note_integer_operands (value);
7649 :
7650 120156896 : protected_set_expr_location (value, loc);
7651 120156896 : return value;
7652 : }
7653 :
7654 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7655 : location of the open paren of the cast, or the position of the cast
7656 : expr. */
7657 : tree
7658 120175829 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7659 : {
7660 120175829 : tree type;
7661 120175829 : tree type_expr = NULL_TREE;
7662 120175829 : bool type_expr_const = true;
7663 120175829 : tree ret;
7664 120175829 : int saved_wsp = warn_strict_prototypes;
7665 :
7666 : /* This avoids warnings about unprototyped casts on
7667 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7668 120175829 : if (TREE_CODE (expr) == INTEGER_CST)
7669 6921555 : warn_strict_prototypes = 0;
7670 120175829 : type = groktypename (type_name, &type_expr, &type_expr_const);
7671 120175829 : warn_strict_prototypes = saved_wsp;
7672 :
7673 764929 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7674 120940504 : && reject_gcc_builtin (expr))
7675 2 : return error_mark_node;
7676 :
7677 120175827 : ret = build_c_cast (loc, type, expr);
7678 120175827 : if (ret == error_mark_node)
7679 : return error_mark_node;
7680 :
7681 120174665 : if (type_expr)
7682 : {
7683 249 : bool inner_expr_const = true;
7684 249 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7685 249 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7686 498 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7687 145 : && inner_expr_const);
7688 249 : SET_EXPR_LOCATION (ret, loc);
7689 : }
7690 :
7691 120174665 : if (!EXPR_HAS_LOCATION (ret))
7692 6800102 : protected_set_expr_location (ret, loc);
7693 :
7694 : /* C++ does not permits types to be defined in a cast, but it
7695 : allows references to incomplete types. */
7696 120174665 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7697 1 : warning_at (loc, OPT_Wc___compat,
7698 : "defining a type in a cast is invalid in C++");
7699 :
7700 : return ret;
7701 : }
7702 :
7703 : /* Build an assignment expression of lvalue LHS from value RHS.
7704 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7705 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7706 : MODIFYCODE is the code for a binary operator that we use
7707 : to combine the old value of LHS with RHS to get the new value.
7708 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7709 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7710 : which may differ from TREE_TYPE (RHS) for an enum value.
7711 :
7712 : LOCATION is the location of the MODIFYCODE operator.
7713 : RHS_LOC is the location of the RHS. */
7714 :
7715 : tree
7716 2900613 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7717 : enum tree_code modifycode,
7718 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7719 : {
7720 2900613 : tree result;
7721 2900613 : tree newrhs;
7722 2900613 : tree rhseval = NULL_TREE;
7723 2900613 : tree lhstype = TREE_TYPE (lhs);
7724 2900613 : tree olhstype = lhstype;
7725 2900613 : bool npc;
7726 2900613 : bool is_atomic_op;
7727 :
7728 : /* Types that aren't fully specified cannot be used in assignments. */
7729 2900613 : lhs = require_complete_type (location, lhs);
7730 2900613 : rhs = require_complete_type (location, rhs);
7731 :
7732 : /* Avoid duplicate error messages from operands that had errors. */
7733 2900613 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7734 473 : return error_mark_node;
7735 :
7736 : /* Ensure an error for assigning a non-lvalue array to an array in
7737 : C90. */
7738 2900140 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7739 : {
7740 1 : error_at (location, "assignment to expression with array type");
7741 1 : return error_mark_node;
7742 : }
7743 :
7744 : /* For ObjC properties, defer this check. */
7745 2900139 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7746 32 : return error_mark_node;
7747 :
7748 2900107 : is_atomic_op = really_atomic_lvalue (lhs);
7749 :
7750 2900107 : newrhs = rhs;
7751 :
7752 2900107 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7753 : {
7754 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7755 : lhs_origtype, modifycode, rhs_loc, rhs,
7756 : rhs_origtype);
7757 2 : if (inner == error_mark_node)
7758 : return error_mark_node;
7759 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7760 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7761 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7762 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7763 2 : protected_set_expr_location (result, location);
7764 2 : return result;
7765 : }
7766 :
7767 : /* If a binary op has been requested, combine the old LHS value with the RHS
7768 : producing the value we should actually store into the LHS. */
7769 :
7770 2900105 : if (modifycode != NOP_EXPR)
7771 : {
7772 214979 : lhs = c_fully_fold (lhs, false, NULL, true);
7773 214979 : lhs = stabilize_reference (lhs);
7774 :
7775 : /* Construct the RHS for any non-atomic compound assignemnt. */
7776 214979 : if (!is_atomic_op)
7777 : {
7778 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7779 : are preevaluated before the rest of the assignment expression's
7780 : side-effects, because RHS could contain e.g. function calls
7781 : that modify LHS. */
7782 199494 : if (TREE_SIDE_EFFECTS (rhs))
7783 : {
7784 8685 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7785 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7786 : else
7787 8657 : newrhs = save_expr (rhs);
7788 8685 : rhseval = newrhs;
7789 8685 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7790 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7791 : newrhs);
7792 : }
7793 199494 : bool clear_decl_read = false;
7794 88467 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7795 153317 : && !DECL_READ_P (lhs)
7796 251726 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7797 : : warn_unused_but_set_parameter) > 2)
7798 : {
7799 21550 : mark_exp_read (newrhs);
7800 21550 : if (!DECL_READ_P (lhs))
7801 199494 : clear_decl_read = true;
7802 : }
7803 :
7804 199494 : newrhs = build_binary_op (location, modifycode,
7805 : convert_lvalue_to_rvalue (location, lhs,
7806 : true, true),
7807 : newrhs, true);
7808 199494 : if (clear_decl_read)
7809 21550 : DECL_READ_P (lhs) = 0;
7810 :
7811 : /* The original type of the right hand side is no longer
7812 : meaningful. */
7813 : rhs_origtype = NULL_TREE;
7814 : }
7815 : }
7816 :
7817 2900105 : if (c_dialect_objc ())
7818 : {
7819 : /* Check if we are modifying an Objective-C property reference;
7820 : if so, we need to generate setter calls. */
7821 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7822 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7823 : else
7824 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7825 0 : if (result)
7826 0 : goto return_result;
7827 :
7828 : /* Else, do the check that we postponed for Objective-C. */
7829 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7830 0 : return error_mark_node;
7831 : }
7832 :
7833 : /* Give an error for storing in something that is 'const'. */
7834 :
7835 2900105 : if (TYPE_READONLY (lhstype)
7836 2900105 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7837 25761 : && C_TYPE_FIELDS_READONLY (lhstype)))
7838 : {
7839 88 : readonly_error (location, lhs, lv_assign);
7840 88 : return error_mark_node;
7841 : }
7842 2900017 : else if (TREE_READONLY (lhs))
7843 1 : readonly_warning (lhs, lv_assign);
7844 :
7845 : /* If storing into a structure or union member,
7846 : it has probably been given type `int'.
7847 : Compute the type that would go with
7848 : the actual amount of storage the member occupies. */
7849 :
7850 2900017 : if (TREE_CODE (lhs) == COMPONENT_REF
7851 249111 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7852 249111 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7853 109847 : || SCALAR_FLOAT_TYPE_P (lhstype)
7854 86538 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7855 169569 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7856 :
7857 : /* If storing in a field that is in actuality a short or narrower than one,
7858 : we must store in the field in its actual type. */
7859 :
7860 2900017 : if (lhstype != TREE_TYPE (lhs))
7861 : {
7862 0 : lhs = copy_node (lhs);
7863 0 : TREE_TYPE (lhs) = lhstype;
7864 : }
7865 :
7866 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7867 : when LHS does not have its original type. This happens for,
7868 : e.g., an enum bitfield in a struct. */
7869 2900017 : if (warn_cxx_compat
7870 3196 : && lhs_origtype != NULL_TREE
7871 3196 : && lhs_origtype != lhstype
7872 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7873 : {
7874 4 : tree checktype = (rhs_origtype != NULL_TREE
7875 4 : ? rhs_origtype
7876 0 : : TREE_TYPE (rhs));
7877 4 : if (checktype != error_mark_node
7878 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7879 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7880 2 : warning_at (location, OPT_Wc___compat,
7881 : "enum conversion in assignment is invalid in C++");
7882 : }
7883 :
7884 : /* Remove qualifiers. */
7885 2900017 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7886 2900017 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7887 :
7888 : /* Convert new value to destination type. Fold it first, then
7889 : restore any excess precision information, for the sake of
7890 : conversion warnings. */
7891 :
7892 2900017 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7893 : {
7894 2884532 : tree rhs_semantic_type = NULL_TREE;
7895 2884532 : if (!c_in_omp_for)
7896 : {
7897 2868211 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7898 : {
7899 5077 : rhs_semantic_type = TREE_TYPE (newrhs);
7900 5077 : newrhs = TREE_OPERAND (newrhs, 0);
7901 : }
7902 2868211 : npc = null_pointer_constant_p (newrhs);
7903 2868211 : newrhs = c_fully_fold (newrhs, false, NULL);
7904 2868211 : if (rhs_semantic_type)
7905 5077 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7906 : }
7907 : else
7908 16321 : npc = null_pointer_constant_p (newrhs);
7909 2884532 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7910 : rhs_origtype, ic_assign, npc,
7911 : NULL_TREE, NULL_TREE, 0);
7912 2884532 : if (TREE_CODE (newrhs) == ERROR_MARK)
7913 127 : return error_mark_node;
7914 : }
7915 :
7916 : /* Emit ObjC write barrier, if necessary. */
7917 2899890 : if (c_dialect_objc () && flag_objc_gc)
7918 : {
7919 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7920 0 : if (result)
7921 : {
7922 0 : protected_set_expr_location (result, location);
7923 0 : goto return_result;
7924 : }
7925 : }
7926 :
7927 : /* Scan operands. */
7928 :
7929 2899890 : if (is_atomic_op)
7930 25964 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7931 : else
7932 : {
7933 2873926 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7934 2873926 : TREE_SIDE_EFFECTS (result) = 1;
7935 2873926 : protected_set_expr_location (result, location);
7936 : }
7937 :
7938 : /* If we got the LHS in a different type for storing in,
7939 : convert the result back to the nominal type of LHS
7940 : so that the value we return always has the same type
7941 : as the LHS argument. */
7942 :
7943 2899890 : if (olhstype == TREE_TYPE (result))
7944 2899890 : goto return_result;
7945 :
7946 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7947 : rhs_origtype, ic_assign, false, NULL_TREE,
7948 : NULL_TREE, 0);
7949 0 : protected_set_expr_location (result, location);
7950 :
7951 2899890 : return_result:
7952 2899890 : if (rhseval)
7953 8685 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7954 : return result;
7955 : }
7956 :
7957 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7958 : This is used to implement -fplan9-extensions. */
7959 :
7960 : static bool
7961 26 : find_anonymous_field_with_type (tree struct_type, tree type)
7962 : {
7963 26 : tree field;
7964 26 : bool found;
7965 :
7966 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7967 26 : found = false;
7968 26 : for (field = TYPE_FIELDS (struct_type);
7969 68 : field != NULL_TREE;
7970 42 : field = TREE_CHAIN (field))
7971 : {
7972 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7973 42 : if (DECL_NAME (field) == NULL
7974 42 : && comptypes (type, fieldtype))
7975 : {
7976 4 : if (found)
7977 : return false;
7978 : found = true;
7979 : }
7980 38 : else if (DECL_NAME (field) == NULL
7981 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7982 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
7983 : {
7984 0 : if (found)
7985 : return false;
7986 : found = true;
7987 : }
7988 : }
7989 : return found;
7990 : }
7991 :
7992 : /* RHS is an expression whose type is pointer to struct. If there is
7993 : an anonymous field in RHS with type TYPE, then return a pointer to
7994 : that field in RHS. This is used with -fplan9-extensions. This
7995 : returns NULL if no conversion could be found. */
7996 :
7997 : static tree
7998 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
7999 : {
8000 28 : tree rhs_struct_type, lhs_main_type;
8001 28 : tree field, found_field;
8002 28 : bool found_sub_field;
8003 28 : tree ret;
8004 :
8005 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8006 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8007 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8008 :
8009 28 : gcc_assert (POINTER_TYPE_P (type));
8010 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8011 :
8012 28 : found_field = NULL_TREE;
8013 28 : found_sub_field = false;
8014 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8015 172 : field != NULL_TREE;
8016 144 : field = TREE_CHAIN (field))
8017 : {
8018 148 : if (DECL_NAME (field) != NULL_TREE
8019 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8020 96 : continue;
8021 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8022 52 : if (comptypes (lhs_main_type, fieldtype))
8023 : {
8024 28 : if (found_field != NULL_TREE)
8025 : return NULL_TREE;
8026 : found_field = field;
8027 : }
8028 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8029 : lhs_main_type))
8030 : {
8031 4 : if (found_field != NULL_TREE)
8032 : return NULL_TREE;
8033 : found_field = field;
8034 : found_sub_field = true;
8035 : }
8036 : }
8037 :
8038 24 : if (found_field == NULL_TREE)
8039 : return NULL_TREE;
8040 :
8041 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8042 : build_fold_indirect_ref (rhs), found_field,
8043 : NULL_TREE);
8044 24 : ret = build_fold_addr_expr_loc (location, ret);
8045 :
8046 24 : if (found_sub_field)
8047 : {
8048 2 : ret = convert_to_anonymous_field (location, type, ret);
8049 2 : gcc_assert (ret != NULL_TREE);
8050 : }
8051 :
8052 : return ret;
8053 : }
8054 :
8055 : /* Issue an error message for a bad initializer component.
8056 : GMSGID identifies the message.
8057 : The component name is taken from the spelling stack. */
8058 :
8059 : static void ATTRIBUTE_GCC_DIAG (2,0)
8060 944 : error_init (location_t loc, const char *gmsgid, ...)
8061 : {
8062 944 : char *ofwhat;
8063 :
8064 944 : auto_diagnostic_group d;
8065 :
8066 : /* The gmsgid may be a format string with %< and %>. */
8067 944 : va_list ap;
8068 944 : va_start (ap, gmsgid);
8069 944 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8070 944 : loc, -1, gmsgid, &ap);
8071 944 : va_end (ap);
8072 :
8073 944 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8074 944 : if (*ofwhat && warned)
8075 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8076 944 : }
8077 :
8078 : /* Used to implement pedwarn_init and permerror_init. */
8079 :
8080 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8081 2538 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8082 : va_list *ap, enum diagnostics::kind kind)
8083 : {
8084 : /* Use the location where a macro was expanded rather than where
8085 : it was defined to make sure macros defined in system headers
8086 : but used incorrectly elsewhere are diagnosed. */
8087 2538 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8088 2538 : auto_diagnostic_group d;
8089 2538 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8090 2538 : if (warned)
8091 : {
8092 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8093 1815 : if (*ofwhat)
8094 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8095 : }
8096 5076 : return warned;
8097 2538 : }
8098 :
8099 : /* Issue a pedantic warning for a bad initializer component. OPT is
8100 : the option OPT_* (from options.h) controlling this warning or 0 if
8101 : it is unconditionally given. GMSGID identifies the message. The
8102 : component name is taken from the spelling stack. */
8103 :
8104 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8105 2187 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8106 : {
8107 2187 : va_list ap;
8108 2187 : va_start (ap, gmsgid);
8109 2187 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8110 : diagnostics::kind::pedwarn);
8111 2187 : va_end (ap);
8112 2187 : return warned;
8113 : }
8114 :
8115 : /* Like pedwarn_init, but issue a permerror. */
8116 :
8117 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8118 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8119 : {
8120 351 : va_list ap;
8121 351 : va_start (ap, gmsgid);
8122 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8123 : diagnostics::kind::permerror);
8124 351 : va_end (ap);
8125 351 : return warned;
8126 : }
8127 :
8128 : /* Issue a warning for a bad initializer component.
8129 :
8130 : OPT is the OPT_W* value corresponding to the warning option that
8131 : controls this warning. GMSGID identifies the message. The
8132 : component name is taken from the spelling stack. */
8133 :
8134 : static void
8135 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8136 : {
8137 146 : char *ofwhat;
8138 146 : bool warned;
8139 :
8140 146 : auto_diagnostic_group d;
8141 :
8142 : /* Use the location where a macro was expanded rather than where
8143 : it was defined to make sure macros defined in system headers
8144 : but used incorrectly elsewhere are diagnosed. */
8145 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8146 :
8147 : /* The gmsgid may be a format string with %< and %>. */
8148 146 : warned = warning_at (exploc, opt, gmsgid);
8149 146 : if (warned)
8150 : {
8151 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8152 133 : if (*ofwhat)
8153 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8154 : }
8155 146 : }
8156 :
8157 : /* If TYPE is an array type and EXPR is a parenthesized string
8158 : constant, warn if pedantic that EXPR is being used to initialize an
8159 : object of type TYPE. */
8160 :
8161 : void
8162 7276148 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8163 : {
8164 7276148 : if (pedantic
8165 80456 : && TREE_CODE (type) == ARRAY_TYPE
8166 1145 : && TREE_CODE (expr.value) == STRING_CST
8167 472 : && expr.original_code != STRING_CST)
8168 13 : pedwarn_init (loc, OPT_Wpedantic,
8169 : "array initialized from parenthesized string constant");
8170 7276148 : }
8171 :
8172 : /* Attempt to locate the parameter with the given index within FNDECL,
8173 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8174 :
8175 : static location_t
8176 937 : get_fndecl_argument_location (tree fndecl, int argnum)
8177 : {
8178 937 : int i;
8179 937 : tree param;
8180 :
8181 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8182 937 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8183 1016 : i < argnum && param;
8184 79 : i++, param = TREE_CHAIN (param))
8185 : ;
8186 :
8187 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8188 : return DECL_SOURCE_LOCATION (FNDECL). */
8189 937 : if (param == NULL)
8190 653 : return DECL_SOURCE_LOCATION (fndecl);
8191 :
8192 284 : return DECL_SOURCE_LOCATION (param);
8193 : }
8194 :
8195 : /* Issue a note about a mismatching argument for parameter PARMNUM
8196 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8197 : Attempt to issue the note at the pertinent parameter of the decl;
8198 : failing that issue it at the location of FUNDECL; failing that
8199 : issue it at PLOC.
8200 : Use highlight_colors::actual for the ACTUAL_TYPE
8201 : and highlight_colors::expected for EXPECTED_TYPE and the
8202 : parameter of FUNDECL*/
8203 :
8204 : static void
8205 1061 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8206 : tree expected_type, tree actual_type)
8207 : {
8208 1061 : location_t loc;
8209 1061 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8210 937 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8211 : else
8212 : loc = ploc;
8213 :
8214 1061 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8215 :
8216 1061 : pp_markup::element_expected_type elem_expected_type (expected_type);
8217 1061 : pp_markup::element_actual_type elem_actual_type (actual_type);
8218 1061 : inform (&richloc,
8219 : "expected %e but argument is of type %e",
8220 : &elem_expected_type, &elem_actual_type);
8221 1061 : }
8222 :
8223 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8224 : function FUNDECL declared without prototype to parameter PARMNUM of
8225 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8226 :
8227 : static void
8228 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8229 : tree parmtype, tree argtype)
8230 : {
8231 418 : tree_code parmcode = TREE_CODE (parmtype);
8232 418 : tree_code argcode = TREE_CODE (argtype);
8233 418 : tree promoted = c_type_promotes_to (argtype);
8234 :
8235 : /* Avoid warning for enum arguments that promote to an integer type
8236 : of the same size/mode. */
8237 418 : if (parmcode == INTEGER_TYPE
8238 418 : && argcode == ENUMERAL_TYPE
8239 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8240 : return;
8241 :
8242 417 : if ((parmcode == argcode
8243 222 : || (parmcode == INTEGER_TYPE
8244 : && argcode == ENUMERAL_TYPE))
8245 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8246 : return;
8247 :
8248 : /* This diagnoses even signed/unsigned mismatches. Those might be
8249 : safe in many cases but GCC may emit suboptimal code for them so
8250 : warning on those cases drives efficiency improvements. */
8251 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8252 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8253 : ? G_("%qD argument %d type is %qT where %qT is expected "
8254 : "in a call to built-in function declared without "
8255 : "prototype")
8256 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8257 : "in a call to built-in function declared without "
8258 : "prototype"),
8259 : fundecl, parmnum, promoted, parmtype))
8260 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8261 : "built-in %qD declared here",
8262 : fundecl);
8263 : }
8264 :
8265 : /* Print a declaration in quotes, with the given highlight_color.
8266 : Analogous to handler for %qD, but with a specific highlight color. */
8267 :
8268 199 : class pp_element_quoted_decl : public pp_element
8269 : {
8270 : public:
8271 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8272 199 : : m_decl (decl),
8273 199 : m_highlight_color (highlight_color)
8274 : {
8275 : }
8276 :
8277 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8278 : {
8279 199 : ctxt.begin_quote ();
8280 199 : ctxt.begin_highlight_color (m_highlight_color);
8281 :
8282 199 : print_decl (ctxt);
8283 :
8284 199 : ctxt.end_highlight_color ();
8285 199 : ctxt.end_quote ();
8286 199 : }
8287 :
8288 199 : void print_decl (pp_markup::context &ctxt)
8289 : {
8290 199 : pretty_printer *const pp = &ctxt.m_pp;
8291 199 : pp->set_padding (pp_none);
8292 199 : if (DECL_NAME (m_decl))
8293 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8294 : else
8295 0 : pp_string (pp, _("({anonymous})"));
8296 199 : }
8297 :
8298 : private:
8299 : tree m_decl;
8300 : const char *m_highlight_color;
8301 : };
8302 :
8303 : /* If TYPE is from a typedef, issue a note showing the location
8304 : to the user.
8305 : Use HIGHLIGHT_COLOR as the highlight color. */
8306 :
8307 : static void
8308 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8309 : {
8310 1178 : if (!typedef_variant_p (type))
8311 1144 : return;
8312 :
8313 34 : tree typedef_decl = TYPE_NAME (type);
8314 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8315 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8316 34 : nullptr, highlight_color);
8317 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8318 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8319 34 : }
8320 :
8321 : /* Convert value RHS to type TYPE as preparation for an assignment to
8322 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8323 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8324 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8325 : constant before any folding.
8326 : The real work of conversion is done by `convert'.
8327 : The purpose of this function is to generate error messages
8328 : for assignments that are not allowed in C.
8329 : ERRTYPE says whether it is argument passing, assignment,
8330 : initialization or return.
8331 :
8332 : In the following example, '~' denotes where EXPR_LOC and '^' where
8333 : LOCATION point to:
8334 :
8335 : f (var); [ic_argpass]
8336 : ^ ~~~
8337 : x = var; [ic_assign]
8338 : ^ ~~~;
8339 : int x = var; [ic_init]
8340 : ^^^
8341 : return x; [ic_return]
8342 : ^
8343 :
8344 : FUNCTION is a tree for the function being called.
8345 : PARMNUM is the number of the argument, for printing in error messages.
8346 : WARNOPT may be set to a warning option to issue the corresponding warning
8347 : rather than an error for invalid conversions. Used for calls to built-in
8348 : functions declared without a prototype. */
8349 :
8350 : static tree
8351 166943609 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8352 : tree rhs, tree origtype, enum impl_conv errtype,
8353 : bool null_pointer_constant, tree fundecl,
8354 : tree function, int parmnum, int warnopt /* = 0 */)
8355 : {
8356 166943609 : enum tree_code codel = TREE_CODE (type);
8357 166943609 : tree orig_rhs = rhs;
8358 166943609 : tree rhstype;
8359 166943609 : enum tree_code coder;
8360 166943609 : tree rname = NULL_TREE;
8361 166943609 : bool objc_ok = false;
8362 :
8363 : /* Use the expansion point location to handle cases such as user's
8364 : function returning a wrong-type macro defined in a system header. */
8365 166943609 : location = expansion_point_location_if_in_system_header (location);
8366 :
8367 166943609 : if (errtype == ic_argpass)
8368 : {
8369 125525980 : tree selector;
8370 : /* Change pointer to function to the function itself for
8371 : diagnostics. */
8372 125525980 : if (TREE_CODE (function) == ADDR_EXPR
8373 125525980 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8374 0 : function = TREE_OPERAND (function, 0);
8375 :
8376 : /* Handle an ObjC selector specially for diagnostics. */
8377 125525980 : selector = objc_message_selector ();
8378 125525980 : rname = function;
8379 125525980 : if (selector && parmnum > 2)
8380 : {
8381 0 : rname = selector;
8382 0 : parmnum -= 2;
8383 : }
8384 : }
8385 :
8386 : /* This macro is used to emit diagnostics to ensure that all format
8387 : strings are complete sentences, visible to gettext and checked at
8388 : compile time. */
8389 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8390 : do { \
8391 : switch (errtype) \
8392 : { \
8393 : case ic_argpass: \
8394 : { \
8395 : auto_diagnostic_group d; \
8396 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8397 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8398 : } \
8399 : break; \
8400 : case ic_assign: \
8401 : pedwarn (LOCATION, OPT, AS); \
8402 : break; \
8403 : case ic_init: \
8404 : case ic_init_const: \
8405 : pedwarn_init (LOCATION, OPT, IN); \
8406 : break; \
8407 : case ic_return: \
8408 : pedwarn (LOCATION, OPT, RE); \
8409 : break; \
8410 : default: \
8411 : gcc_unreachable (); \
8412 : } \
8413 : } while (0)
8414 :
8415 : /* This macro is used to emit diagnostics to ensure that all format
8416 : strings are complete sentences, visible to gettext and checked at
8417 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8418 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8419 : do { \
8420 : switch (errtype) \
8421 : { \
8422 : case ic_argpass: \
8423 : { \
8424 : auto_diagnostic_group d; \
8425 : if (PEDWARN) { \
8426 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8427 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8428 : } else { \
8429 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8430 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8431 : } \
8432 : } \
8433 : break; \
8434 : case ic_assign: \
8435 : if (PEDWARN) \
8436 : pedwarn (LOCATION, OPT, AS, QUALS); \
8437 : else \
8438 : warning_at (LOCATION, OPT, AS, QUALS); \
8439 : break; \
8440 : case ic_init: \
8441 : case ic_init_const: \
8442 : if (PEDWARN) \
8443 : pedwarn (LOCATION, OPT, IN, QUALS); \
8444 : else \
8445 : warning_at (LOCATION, OPT, IN, QUALS); \
8446 : break; \
8447 : case ic_return: \
8448 : if (PEDWARN) \
8449 : pedwarn (LOCATION, OPT, RE, QUALS); \
8450 : else \
8451 : warning_at (LOCATION, OPT, RE, QUALS); \
8452 : break; \
8453 : default: \
8454 : gcc_unreachable (); \
8455 : } \
8456 : } while (0)
8457 :
8458 : /* This macro is used to emit diagnostics to ensure that all format
8459 : strings are complete sentences, visible to gettext and checked at
8460 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8461 : extra parameter to enumerate qualifiers. */
8462 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8463 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8464 :
8465 :
8466 166943609 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8467 5975 : rhs = TREE_OPERAND (rhs, 0);
8468 :
8469 166943609 : rhstype = TREE_TYPE (rhs);
8470 166943609 : coder = TREE_CODE (rhstype);
8471 :
8472 166943609 : if (coder == ERROR_MARK)
8473 346 : return error_mark_node;
8474 :
8475 166943263 : if (c_dialect_objc ())
8476 : {
8477 0 : int parmno;
8478 :
8479 0 : switch (errtype)
8480 : {
8481 : case ic_return:
8482 : parmno = 0;
8483 : break;
8484 :
8485 : case ic_assign:
8486 : parmno = -1;
8487 : break;
8488 :
8489 : case ic_init:
8490 : case ic_init_const:
8491 : parmno = -2;
8492 : break;
8493 :
8494 : default:
8495 0 : parmno = parmnum;
8496 : break;
8497 : }
8498 :
8499 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8500 : }
8501 :
8502 166943263 : if (warn_cxx_compat)
8503 : {
8504 28859 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8505 28859 : if (checktype != error_mark_node
8506 28859 : && TREE_CODE (type) == ENUMERAL_TYPE
8507 28965 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8508 45 : switch (errtype)
8509 : {
8510 8 : case ic_argpass:
8511 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8512 : "passing argument %d of %qE is invalid in C++",
8513 : parmnum, rname))
8514 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8515 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8516 : "expected %qT but argument is of type %qT",
8517 : type, rhstype);
8518 : break;
8519 10 : case ic_assign:
8520 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8521 : "%qT in assignment is invalid in C++", rhstype, type);
8522 10 : break;
8523 18 : case ic_init:
8524 18 : case ic_init_const:
8525 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8526 : "%qT to %qT in initialization is invalid in C++",
8527 : rhstype, type);
8528 18 : break;
8529 9 : case ic_return:
8530 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8531 : "%qT in return is invalid in C++", rhstype, type);
8532 9 : break;
8533 0 : default:
8534 0 : gcc_unreachable ();
8535 : }
8536 : }
8537 :
8538 166943263 : if (warn_enum_conversion)
8539 : {
8540 14586588 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8541 14586588 : if (checktype != error_mark_node
8542 14586588 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8543 12709 : && TREE_CODE (type) == ENUMERAL_TYPE
8544 14594784 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8545 : {
8546 7 : gcc_rich_location loc (location);
8547 7 : warning_at (&loc, OPT_Wenum_conversion,
8548 : "implicit conversion from %qT to %qT",
8549 : checktype, type);
8550 7 : }
8551 : }
8552 :
8553 166943263 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8554 : {
8555 147856133 : warn_for_address_of_packed_member (type, orig_rhs);
8556 147856133 : if (type != rhstype)
8557 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8558 36838430 : rhs = convert (type, rhs);
8559 147856133 : return rhs;
8560 : }
8561 :
8562 19087130 : if (coder == VOID_TYPE)
8563 : {
8564 : /* Except for passing an argument to an unprototyped function,
8565 : this is a constraint violation. When passing an argument to
8566 : an unprototyped function, it is compile-time undefined;
8567 : making it a constraint in that case was rejected in
8568 : DR#252. */
8569 8 : const char msg[] = "void value not ignored as it ought to be";
8570 8 : if (warnopt)
8571 0 : warning_at (location, warnopt, msg);
8572 : else
8573 8 : error_at (location, msg);
8574 8 : return error_mark_node;
8575 : }
8576 19087122 : rhs = require_complete_type (location, rhs);
8577 19087122 : if (rhs == error_mark_node)
8578 : return error_mark_node;
8579 :
8580 19087122 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8581 6 : return error_mark_node;
8582 :
8583 : /* A non-reference type can convert to a reference. This handles
8584 : va_start, va_copy and possibly port built-ins. */
8585 19087116 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8586 : {
8587 286 : if (!lvalue_p (rhs))
8588 : {
8589 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8590 0 : if (warnopt)
8591 0 : warning_at (location, warnopt, msg);
8592 : else
8593 0 : error_at (location, msg);
8594 0 : return error_mark_node;
8595 : }
8596 286 : if (!c_mark_addressable (rhs))
8597 0 : return error_mark_node;
8598 286 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8599 286 : SET_EXPR_LOCATION (rhs, location);
8600 :
8601 286 : rhs = convert_for_assignment (location, expr_loc,
8602 286 : c_build_pointer_type (TREE_TYPE (type)),
8603 : rhs, origtype, errtype,
8604 : null_pointer_constant, fundecl, function,
8605 : parmnum, warnopt);
8606 286 : if (rhs == error_mark_node)
8607 : return error_mark_node;
8608 :
8609 286 : rhs = build1 (NOP_EXPR, type, rhs);
8610 286 : SET_EXPR_LOCATION (rhs, location);
8611 286 : return rhs;
8612 : }
8613 : /* Some types can interconvert without explicit casts. */
8614 19086830 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8615 19086830 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8616 9281227 : return convert (type, rhs);
8617 : /* Arithmetic types all interconvert, and enum is treated like int. */
8618 9805603 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8619 2273621 : || codel == FIXED_POINT_TYPE
8620 2273621 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8621 2187302 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8622 7710140 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8623 82821 : || coder == FIXED_POINT_TYPE
8624 82821 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8625 39792 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8626 : {
8627 7709021 : if (warnopt && errtype == ic_argpass)
8628 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8629 : rhstype);
8630 :
8631 7709021 : bool save = in_late_binary_op;
8632 7677990 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8633 15330823 : || (coder == REAL_TYPE
8634 276012 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8635 24196 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8636 93963 : in_late_binary_op = true;
8637 11086851 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8638 : ? expr_loc : location, type, orig_rhs,
8639 : errtype == ic_init_const);
8640 7709021 : in_late_binary_op = save;
8641 7709021 : return ret;
8642 : }
8643 :
8644 : /* Aggregates in different TUs might need conversion. */
8645 2096582 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8646 104 : && codel == coder
8647 2096604 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8648 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8649 9 : ? expr_loc : location, type, rhs);
8650 :
8651 : /* Conversion to a transparent union or record from its member types.
8652 : This applies only to function arguments. */
8653 2096573 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8654 95 : && TYPE_TRANSPARENT_AGGR (type))
8655 2096611 : && errtype == ic_argpass)
8656 : {
8657 38 : tree memb, marginal_memb = NULL_TREE;
8658 :
8659 48 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8660 : {
8661 48 : tree memb_type = TREE_TYPE (memb);
8662 :
8663 48 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8664 48 : TYPE_MAIN_VARIANT (rhstype)))
8665 : break;
8666 :
8667 26 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8668 0 : continue;
8669 :
8670 26 : if (coder == POINTER_TYPE)
8671 : {
8672 26 : tree ttl = TREE_TYPE (memb_type);
8673 26 : tree ttr = TREE_TYPE (rhstype);
8674 :
8675 : /* Any non-function converts to a [const][volatile] void *
8676 : and vice versa; otherwise, targets must be the same.
8677 : Meanwhile, the lhs target must have all the qualifiers of
8678 : the rhs. */
8679 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8680 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8681 45 : || comp_target_types (location, memb_type, rhstype))
8682 : {
8683 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8684 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8685 : /* If this type won't generate any warnings, use it. */
8686 16 : if (lquals == rquals
8687 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8688 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8689 0 : ? ((lquals | rquals) == rquals)
8690 16 : : ((lquals | rquals) == lquals)))
8691 : break;
8692 :
8693 : /* Keep looking for a better type, but remember this one. */
8694 0 : if (!marginal_memb)
8695 10 : marginal_memb = memb;
8696 : }
8697 : }
8698 :
8699 : /* Can convert integer zero to any pointer type. */
8700 10 : if (null_pointer_constant)
8701 : {
8702 0 : rhs = null_pointer_node;
8703 0 : break;
8704 : }
8705 : }
8706 :
8707 38 : if (memb || marginal_memb)
8708 : {
8709 38 : if (!memb)
8710 : {
8711 : /* We have only a marginally acceptable member type;
8712 : it needs a warning. */
8713 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8714 0 : tree ttr = TREE_TYPE (rhstype);
8715 :
8716 : /* Const and volatile mean something different for function
8717 : types, so the usual warnings are not appropriate. */
8718 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8719 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8720 : {
8721 : /* Because const and volatile on functions are
8722 : restrictions that say the function will not do
8723 : certain things, it is okay to use a const or volatile
8724 : function where an ordinary one is wanted, but not
8725 : vice-versa. */
8726 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8727 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8728 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8729 : OPT_Wdiscarded_qualifiers,
8730 : G_("passing argument %d of %qE "
8731 : "makes %q#v qualified function "
8732 : "pointer from unqualified"),
8733 : G_("assignment makes %q#v qualified "
8734 : "function pointer from "
8735 : "unqualified"),
8736 : G_("initialization makes %q#v qualified "
8737 : "function pointer from "
8738 : "unqualified"),
8739 : G_("return makes %q#v qualified function "
8740 : "pointer from unqualified"),
8741 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8742 : }
8743 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8744 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8745 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8746 : OPT_Wdiscarded_qualifiers,
8747 : G_("passing argument %d of %qE discards "
8748 : "%qv qualifier from pointer target type"),
8749 : G_("assignment discards %qv qualifier "
8750 : "from pointer target type"),
8751 : G_("initialization discards %qv qualifier "
8752 : "from pointer target type"),
8753 : G_("return discards %qv qualifier from "
8754 : "pointer target type"),
8755 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8756 :
8757 : memb = marginal_memb;
8758 : }
8759 :
8760 38 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8761 30 : pedwarn (location, OPT_Wpedantic,
8762 : "ISO C prohibits argument conversion to union type");
8763 :
8764 38 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8765 38 : return build_constructor_single (type, memb, rhs);
8766 : }
8767 : }
8768 :
8769 : /* Conversions among pointers */
8770 2096535 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8771 2095260 : && (coder == codel))
8772 : {
8773 : /* If RHS refers to a built-in declared without a prototype
8774 : BLTIN is the declaration of the built-in with a prototype
8775 : and RHSTYPE is set to the actual type of the built-in. */
8776 2017040 : tree bltin;
8777 2017040 : rhstype = type_or_builtin_type (rhs, &bltin);
8778 :
8779 2017040 : tree ttl = TREE_TYPE (type);
8780 2017040 : tree ttr = TREE_TYPE (rhstype);
8781 2017040 : bool is_opaque_pointer;
8782 2017040 : bool target_cmp = false; /* Cache comp_target_types () result. */
8783 2017040 : addr_space_t asl;
8784 2017040 : addr_space_t asr;
8785 :
8786 2017040 : tree mvl = remove_qualifiers (ttl);
8787 2017040 : tree mvr = remove_qualifiers (ttr);
8788 :
8789 : /* Opaque pointers are treated like void pointers. */
8790 2017040 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8791 :
8792 : /* The Plan 9 compiler permits a pointer to a struct to be
8793 : automatically converted into a pointer to an anonymous field
8794 : within the struct. */
8795 2017040 : if (flag_plan9_extensions
8796 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8797 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8798 26 : && mvl != mvr)
8799 : {
8800 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8801 26 : if (new_rhs != NULL_TREE)
8802 : {
8803 22 : rhs = new_rhs;
8804 22 : rhstype = TREE_TYPE (rhs);
8805 22 : coder = TREE_CODE (rhstype);
8806 22 : ttr = TREE_TYPE (rhstype);
8807 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8808 : }
8809 : }
8810 :
8811 : /* C++ does not allow the implicit conversion void* -> T*. However,
8812 : for the purpose of reducing the number of false positives, we
8813 : tolerate the special case of
8814 :
8815 : int *p = NULL;
8816 :
8817 : where NULL is typically defined in C to be '(void *) 0'. */
8818 2017040 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8819 26830 : warning_at (errtype == ic_argpass ? expr_loc : location,
8820 14225 : OPT_Wc___compat,
8821 : "request for implicit conversion "
8822 : "from %qT to %qT not permitted in C++", rhstype, type);
8823 :
8824 : /* Warn of new allocations that are not big enough for the target
8825 : type. */
8826 2017040 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8827 5455 : if (tree fndecl = get_callee_fndecl (rhs))
8828 5455 : if (DECL_IS_MALLOC (fndecl))
8829 : {
8830 4601 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8831 4601 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8832 4601 : if (alloc_size)
8833 447 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8834 : }
8835 :
8836 : /* See if the pointers point to incompatible address spaces. */
8837 2017040 : asl = TYPE_ADDR_SPACE (ttl);
8838 2017040 : asr = TYPE_ADDR_SPACE (ttr);
8839 2017040 : if (!null_pointer_constant_p (rhs)
8840 2017040 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8841 : {
8842 3 : auto_diagnostic_group d;
8843 3 : bool diagnosed = true;
8844 3 : switch (errtype)
8845 : {
8846 1 : case ic_argpass:
8847 1 : {
8848 1 : const char msg[] = G_("passing argument %d of %qE from "
8849 : "pointer to non-enclosed address space");
8850 1 : if (warnopt)
8851 0 : diagnosed
8852 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8853 : else
8854 1 : error_at (expr_loc, msg, parmnum, rname);
8855 0 : break;
8856 : }
8857 0 : case ic_assign:
8858 0 : {
8859 0 : const char msg[] = G_("assignment from pointer to "
8860 : "non-enclosed address space");
8861 0 : if (warnopt)
8862 0 : diagnosed = warning_at (location, warnopt, msg);
8863 : else
8864 0 : error_at (location, msg);
8865 0 : break;
8866 : }
8867 0 : case ic_init:
8868 0 : case ic_init_const:
8869 0 : {
8870 0 : const char msg[] = G_("initialization from pointer to "
8871 : "non-enclosed address space");
8872 0 : if (warnopt)
8873 0 : diagnosed = warning_at (location, warnopt, msg);
8874 : else
8875 0 : error_at (location, msg);
8876 0 : break;
8877 : }
8878 2 : case ic_return:
8879 2 : {
8880 2 : const char msg[] = G_("return from pointer to "
8881 : "non-enclosed address space");
8882 2 : if (warnopt)
8883 0 : diagnosed = warning_at (location, warnopt, msg);
8884 : else
8885 2 : error_at (location, msg);
8886 0 : break;
8887 : }
8888 0 : default:
8889 0 : gcc_unreachable ();
8890 : }
8891 3 : if (diagnosed)
8892 : {
8893 3 : if (errtype == ic_argpass)
8894 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8895 : else
8896 2 : inform (location, "expected %qT but pointer is of type %qT",
8897 : type, rhstype);
8898 : }
8899 3 : return error_mark_node;
8900 3 : }
8901 :
8902 : /* Check if the right-hand side has a format attribute but the
8903 : left-hand side doesn't. */
8904 2017037 : if (warn_suggest_attribute_format
8905 2017037 : && check_missing_format_attribute (type, rhstype))
8906 : {
8907 16 : switch (errtype)
8908 : {
8909 4 : case ic_argpass:
8910 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8911 : "argument %d of %qE might be "
8912 : "a candidate for a format attribute",
8913 : parmnum, rname);
8914 4 : break;
8915 4 : case ic_assign:
8916 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8917 : "assignment left-hand side might be "
8918 : "a candidate for a format attribute");
8919 4 : break;
8920 4 : case ic_init:
8921 4 : case ic_init_const:
8922 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8923 : "initialization left-hand side might be "
8924 : "a candidate for a format attribute");
8925 4 : break;
8926 4 : case ic_return:
8927 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8928 : "return type might be "
8929 : "a candidate for a format attribute");
8930 4 : break;
8931 0 : default:
8932 0 : gcc_unreachable ();
8933 : }
8934 : }
8935 :
8936 : /* See if the pointers point to incompatible scalar storage orders. */
8937 2017037 : if (warn_scalar_storage_order
8938 2014121 : && !null_pointer_constant_p (rhs)
8939 5978765 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8940 1980864 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8941 : {
8942 18 : tree t;
8943 :
8944 18 : switch (errtype)
8945 : {
8946 11 : case ic_argpass:
8947 : /* Do not warn for built-in functions, for example memcpy, since we
8948 : control how they behave and they can be useful in this area. */
8949 11 : if (TREE_CODE (rname) != FUNCTION_DECL
8950 11 : || !fndecl_built_in_p (rname))
8951 1 : warning_at (location, OPT_Wscalar_storage_order,
8952 : "passing argument %d of %qE from incompatible "
8953 : "scalar storage order", parmnum, rname);
8954 : break;
8955 3 : case ic_assign:
8956 : /* Do not warn if the RHS is a call to a function that returns a
8957 : pointer that is not an alias. */
8958 3 : if (TREE_CODE (rhs) != CALL_EXPR
8959 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8960 5 : || !DECL_IS_MALLOC (t))
8961 1 : warning_at (location, OPT_Wscalar_storage_order,
8962 : "assignment to %qT from pointer type %qT with "
8963 : "incompatible scalar storage order", type, rhstype);
8964 : break;
8965 3 : case ic_init:
8966 3 : case ic_init_const:
8967 : /* Likewise. */
8968 3 : if (TREE_CODE (rhs) != CALL_EXPR
8969 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8970 5 : || !DECL_IS_MALLOC (t))
8971 1 : warning_at (location, OPT_Wscalar_storage_order,
8972 : "initialization of %qT from pointer type %qT with "
8973 : "incompatible scalar storage order", type, rhstype);
8974 : break;
8975 1 : case ic_return:
8976 1 : warning_at (location, OPT_Wscalar_storage_order,
8977 : "returning %qT from pointer type with incompatible "
8978 : "scalar storage order %qT", rhstype, type);
8979 1 : break;
8980 0 : default:
8981 0 : gcc_unreachable ();
8982 : }
8983 : }
8984 :
8985 : /* Any non-function converts to a [const][volatile] void *
8986 : and vice versa; otherwise, targets must be the same.
8987 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8988 422926 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8989 1594112 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8990 1546767 : || (target_cmp = comp_target_types (location, type, rhstype))
8991 2446 : || is_opaque_pointer
8992 2021929 : || ((c_common_unsigned_type (mvl)
8993 2446 : == c_common_unsigned_type (mvr))
8994 3340 : && (c_common_signed_type (mvl)
8995 1670 : == c_common_signed_type (mvr))
8996 1655 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
8997 : {
8998 : /* Warn about loss of qualifers from pointers to arrays with
8999 : qualifiers on the element type. */
9000 2016238 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9001 : {
9002 2131 : ttr = strip_array_types (ttr);
9003 2131 : ttl = strip_array_types (ttl);
9004 :
9005 2131 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9006 2131 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9007 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9008 : location, expr_loc,
9009 : OPT_Wdiscarded_array_qualifiers,
9010 : G_("passing argument %d of %qE discards "
9011 : "%qv qualifier from pointer target type"),
9012 : G_("assignment discards %qv qualifier "
9013 : "from pointer target type"),
9014 : G_("initialization discards %qv qualifier "
9015 : "from pointer target type"),
9016 : G_("return discards %qv qualifier from "
9017 : "pointer target type"),
9018 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9019 : }
9020 2014107 : else if (pedantic
9021 146051 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9022 146031 : ||
9023 : (VOID_TYPE_P (ttr)
9024 2952 : && !null_pointer_constant
9025 178 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9026 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9027 : G_("ISO C forbids passing argument %d of "
9028 : "%qE between function pointer "
9029 : "and %<void *%>"),
9030 : G_("ISO C forbids assignment between "
9031 : "function pointer and %<void *%>"),
9032 : G_("ISO C forbids initialization between "
9033 : "function pointer and %<void *%>"),
9034 : G_("ISO C forbids return between function "
9035 : "pointer and %<void *%>"));
9036 : /* Const and volatile mean something different for function types,
9037 : so the usual warnings are not appropriate. */
9038 2014073 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9039 1981662 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9040 : {
9041 : /* Assignments between atomic and non-atomic objects are OK. */
9042 1980965 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9043 1980965 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9044 1980965 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9045 1980965 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9046 :
9047 : /* Don't warn about loss of qualifier for conversions from
9048 : qualified void* to pointers to arrays with corresponding
9049 : qualifier on the element type (except for pedantic before C23). */
9050 1980965 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9051 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9052 : OPT_Wdiscarded_qualifiers,
9053 : G_("passing argument %d of %qE discards "
9054 : "%qv qualifier from pointer target type"),
9055 : G_("assignment discards %qv qualifier "
9056 : "from pointer target type"),
9057 : G_("initialization discards %qv qualifier "
9058 : "from pointer target type"),
9059 : G_("return discards %qv qualifier from "
9060 : "pointer target type"),
9061 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9062 11 : else if (warn_quals_ped)
9063 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9064 : "array with qualifier on the element is not qualified before C23");
9065 :
9066 : /* If this is not a case of ignoring a mismatch in signedness,
9067 : no warning. */
9068 1980257 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9069 1541847 : || target_cmp)
9070 : ;
9071 : /* If there is a mismatch, do warn. */
9072 1647 : else if (warn_pointer_sign)
9073 82 : switch (errtype)
9074 : {
9075 33 : case ic_argpass:
9076 33 : {
9077 33 : auto_diagnostic_group d;
9078 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9079 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9080 33 : highlight_colors::actual);
9081 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9082 : "pointer targets in passing argument %d of "
9083 : "%qE differ in signedness", parmnum, rname))
9084 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9085 : rhstype);
9086 33 : }
9087 33 : break;
9088 21 : case ic_assign:
9089 21 : pedwarn (location, OPT_Wpointer_sign,
9090 : "pointer targets in assignment from %qT to %qT "
9091 : "differ in signedness", rhstype, type);
9092 21 : break;
9093 14 : case ic_init:
9094 14 : case ic_init_const:
9095 14 : pedwarn_init (location, OPT_Wpointer_sign,
9096 : "pointer targets in initialization of %qT "
9097 : "from %qT differ in signedness", type,
9098 : rhstype);
9099 14 : break;
9100 14 : case ic_return:
9101 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9102 : "returning %qT from a function with return type "
9103 : "%qT differ in signedness", rhstype, type);
9104 14 : break;
9105 0 : default:
9106 0 : gcc_unreachable ();
9107 : }
9108 : }
9109 33108 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9110 4147 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9111 : {
9112 : /* Because const and volatile on functions are restrictions
9113 : that say the function will not do certain things,
9114 : it is okay to use a const or volatile function
9115 : where an ordinary one is wanted, but not vice-versa. */
9116 3450 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9117 3450 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9118 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9119 : OPT_Wdiscarded_qualifiers,
9120 : G_("passing argument %d of %qE makes "
9121 : "%q#v qualified function pointer "
9122 : "from unqualified"),
9123 : G_("assignment makes %q#v qualified function "
9124 : "pointer from unqualified"),
9125 : G_("initialization makes %q#v qualified "
9126 : "function pointer from unqualified"),
9127 : G_("return makes %q#v qualified function "
9128 : "pointer from unqualified"),
9129 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
9130 : }
9131 : }
9132 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9133 799 : else if (!objc_ok)
9134 : {
9135 799 : auto_diagnostic_group d;
9136 799 : bool warned = false;
9137 799 : pp_markup::element_expected_type e_type (type);
9138 799 : pp_markup::element_actual_type e_rhstype (rhstype);
9139 799 : switch (errtype)
9140 : {
9141 306 : case ic_argpass:
9142 306 : {
9143 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
9144 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
9145 306 : highlight_colors::actual);
9146 306 : warned
9147 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9148 : "passing argument %d of %qE from "
9149 : "incompatible pointer type",
9150 : parmnum, rname);
9151 306 : if (warned)
9152 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9153 306 : }
9154 306 : break;
9155 267 : case ic_assign:
9156 267 : if (bltin)
9157 11 : warned
9158 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9159 : "assignment to %e from pointer to "
9160 : "%qD with incompatible type %e",
9161 : &e_type, bltin, &e_rhstype);
9162 : else
9163 256 : warned
9164 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9165 : "assignment to %e from incompatible "
9166 : "pointer type %e",
9167 : &e_type, &e_rhstype);
9168 : break;
9169 167 : case ic_init:
9170 167 : case ic_init_const:
9171 167 : if (bltin)
9172 13 : warned
9173 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9174 : "initialization of %e from pointer to "
9175 : "%qD with incompatible type %e",
9176 : &e_type, bltin, &e_rhstype);
9177 : else
9178 154 : warned
9179 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9180 : "initialization of %e from incompatible "
9181 : "pointer type %e",
9182 : &e_type, &e_rhstype);
9183 : break;
9184 59 : case ic_return:
9185 59 : if (bltin)
9186 11 : warned
9187 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9188 : "returning pointer to %qD of type %e from "
9189 : "a function with incompatible type %e",
9190 : bltin, &e_rhstype, &e_type);
9191 : else
9192 48 : warned
9193 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9194 : "returning %e from a function with "
9195 : "incompatible return type %e",
9196 : &e_rhstype, &e_type);
9197 : break;
9198 0 : default:
9199 0 : gcc_unreachable ();
9200 : }
9201 799 : if (warned)
9202 : {
9203 : /* If the mismatching function type is a pointer to a function,
9204 : try to show the decl of the function. */
9205 589 : if (TREE_CODE (rhs) == ADDR_EXPR
9206 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9207 : {
9208 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9209 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9210 : {
9211 165 : gcc_rich_location richloc
9212 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9213 165 : highlight_colors::actual);
9214 165 : pp_element_quoted_decl e_rhs_fndecl
9215 165 : (rhs_fndecl, highlight_colors::actual);
9216 165 : inform (&richloc,
9217 : "%e declared here", &e_rhs_fndecl);
9218 165 : }
9219 : }
9220 : /* If either/both of the types are typedefs, show the decl. */
9221 589 : maybe_inform_typedef_location (type,
9222 : highlight_colors::expected);
9223 589 : maybe_inform_typedef_location (rhstype,
9224 : highlight_colors::actual);
9225 : }
9226 799 : }
9227 :
9228 : /* If RHS isn't an address, check pointer or array of packed
9229 : struct or union. */
9230 2017037 : warn_for_address_of_packed_member (type, orig_rhs);
9231 :
9232 2017037 : return convert (type, rhs);
9233 : }
9234 79495 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9235 : {
9236 : /* ??? This should not be an error when inlining calls to
9237 : unprototyped functions. */
9238 4 : const char msg[] = "invalid use of non-lvalue array";
9239 4 : if (warnopt)
9240 0 : warning_at (location, warnopt, msg);
9241 : else
9242 4 : error_at (location, msg);
9243 4 : return error_mark_node;
9244 : }
9245 79491 : else if (codel == POINTER_TYPE
9246 78216 : && (coder == INTEGER_TYPE
9247 78216 : || coder == ENUMERAL_TYPE
9248 615 : || coder == BOOLEAN_TYPE
9249 615 : || coder == NULLPTR_TYPE
9250 27 : || coder == BITINT_TYPE))
9251 : {
9252 78203 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9253 77205 : && coder != NULLPTR_TYPE)
9254 76646 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9255 : "zero as null pointer constant");
9256 : /* A NULLPTR type is just a nullptr always. */
9257 77644 : if (coder == NULLPTR_TYPE)
9258 574 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9259 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9260 : or one that results from arithmetic, even including a cast to
9261 : integer type. */
9262 77629 : else if (!null_pointer_constant)
9263 972 : switch (errtype)
9264 : {
9265 800 : case ic_argpass:
9266 800 : {
9267 800 : auto_diagnostic_group d;
9268 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9269 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9270 800 : highlight_colors::actual);
9271 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9272 : "passing argument %d of %qE makes pointer "
9273 : "from integer without a cast", parmnum, rname))
9274 : {
9275 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9276 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9277 : }
9278 800 : }
9279 800 : break;
9280 93 : case ic_assign:
9281 93 : permerror_opt (location, OPT_Wint_conversion,
9282 : "assignment to %qT from %qT makes pointer from "
9283 : "integer without a cast", type, rhstype);
9284 93 : break;
9285 56 : case ic_init:
9286 56 : case ic_init_const:
9287 56 : permerror_init (location, OPT_Wint_conversion,
9288 : "initialization of %qT from %qT makes pointer "
9289 : "from integer without a cast", type, rhstype);
9290 56 : break;
9291 23 : case ic_return:
9292 23 : permerror_init (location, OPT_Wint_conversion,
9293 : "returning %qT from a function with return type "
9294 : "%qT makes pointer from integer without a cast",
9295 : rhstype, type);
9296 23 : break;
9297 0 : default:
9298 0 : gcc_unreachable ();
9299 : }
9300 :
9301 77629 : return convert (type, rhs);
9302 : }
9303 1288 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9304 540 : && coder == POINTER_TYPE)
9305 : {
9306 514 : switch (errtype)
9307 : {
9308 344 : case ic_argpass:
9309 344 : {
9310 344 : auto_diagnostic_group d;
9311 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9312 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9313 344 : highlight_colors::actual);
9314 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9315 : "passing argument %d of %qE makes integer from "
9316 : "pointer without a cast", parmnum, rname))
9317 : {
9318 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9319 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9320 : }
9321 344 : }
9322 344 : break;
9323 52 : case ic_assign:
9324 52 : permerror_opt (location, OPT_Wint_conversion,
9325 : "assignment to %qT from %qT makes integer from "
9326 : "pointer without a cast", type, rhstype);
9327 52 : break;
9328 83 : case ic_init:
9329 83 : case ic_init_const:
9330 83 : permerror_init (location, OPT_Wint_conversion,
9331 : "initialization of %qT from %qT makes integer "
9332 : "from pointer without a cast", type, rhstype);
9333 83 : break;
9334 35 : case ic_return:
9335 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9336 : "function with return type %qT makes integer from "
9337 : "pointer without a cast", rhstype, type);
9338 35 : break;
9339 0 : default:
9340 0 : gcc_unreachable ();
9341 : }
9342 :
9343 514 : return convert (type, rhs);
9344 : }
9345 626 : else if (C_BOOLEAN_TYPE_P (type)
9346 : /* The type nullptr_t may be converted to bool. The
9347 : result is false. */
9348 776 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9349 : {
9350 150 : tree ret;
9351 150 : bool save = in_late_binary_op;
9352 150 : in_late_binary_op = true;
9353 150 : ret = convert (type, rhs);
9354 150 : in_late_binary_op = save;
9355 150 : return ret;
9356 : }
9357 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9358 6 : return convert (type, rhs);
9359 :
9360 618 : switch (errtype)
9361 : {
9362 35 : case ic_argpass:
9363 35 : {
9364 35 : auto_diagnostic_group d;
9365 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9366 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9367 35 : highlight_colors::actual);
9368 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9369 35 : if (warnopt)
9370 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9371 : else
9372 27 : error_at (&richloc, msg, parmnum, rname);
9373 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9374 35 : }
9375 35 : break;
9376 108 : case ic_assign:
9377 108 : {
9378 108 : const char msg[]
9379 : = G_("incompatible types when assigning to type %qT from type %qT");
9380 108 : if (warnopt)
9381 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9382 : else
9383 108 : error_at (expr_loc, msg, type, rhstype);
9384 108 : break;
9385 : }
9386 460 : case ic_init:
9387 460 : case ic_init_const:
9388 460 : {
9389 460 : const char msg[]
9390 : = G_("incompatible types when initializing type %qT using type %qT");
9391 460 : if (warnopt)
9392 0 : warning_at (location, 0, msg, type, rhstype);
9393 : else
9394 460 : error_at (location, msg, type, rhstype);
9395 460 : break;
9396 : }
9397 15 : case ic_return:
9398 15 : {
9399 15 : const char msg[]
9400 : = G_("incompatible types when returning type %qT but %qT was expected");
9401 15 : if (warnopt)
9402 0 : warning_at (location, 0, msg, rhstype, type);
9403 : else
9404 15 : error_at (location, msg, rhstype, type);
9405 15 : break;
9406 : }
9407 0 : default:
9408 0 : gcc_unreachable ();
9409 : }
9410 :
9411 618 : return error_mark_node;
9412 : }
9413 :
9414 : /* If VALUE is a compound expr all of whose expressions are constant, then
9415 : return its value. Otherwise, return error_mark_node.
9416 :
9417 : This is for handling COMPOUND_EXPRs as initializer elements
9418 : which is allowed with a warning when -pedantic is specified. */
9419 :
9420 : static tree
9421 2 : valid_compound_expr_initializer (tree value, tree endtype)
9422 : {
9423 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9424 : {
9425 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9426 1 : == error_mark_node)
9427 : return error_mark_node;
9428 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9429 0 : endtype);
9430 : }
9431 1 : else if (!initializer_constant_valid_p (value, endtype))
9432 1 : return error_mark_node;
9433 : else
9434 : return value;
9435 : }
9436 :
9437 : /* Perform appropriate conversions on the initial value of a variable,
9438 : store it in the declaration DECL,
9439 : and print any error messages that are appropriate.
9440 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9441 : If the init is invalid, store an ERROR_MARK.
9442 :
9443 : INIT_LOC is the location of the initial value. */
9444 :
9445 : void
9446 7267312 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9447 : {
9448 7267312 : tree value, type;
9449 7267312 : bool npc = false;
9450 7267312 : bool int_const_expr = false;
9451 7267312 : bool arith_const_expr = false;
9452 :
9453 : /* If variable's type was invalidly declared, just ignore it. */
9454 :
9455 7267312 : type = TREE_TYPE (decl);
9456 7267312 : if (TREE_CODE (type) == ERROR_MARK)
9457 : return;
9458 :
9459 : /* Digest the specified initializer into an expression. */
9460 :
9461 7267304 : if (init)
9462 : {
9463 7267301 : npc = null_pointer_constant_p (init);
9464 7267301 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9465 419607 : && !TREE_OVERFLOW (init)
9466 7686872 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9467 : /* Not fully determined before folding. */
9468 : arith_const_expr = true;
9469 : }
9470 7267304 : bool constexpr_p = (VAR_P (decl)
9471 7267304 : && C_DECL_DECLARED_CONSTEXPR (decl));
9472 7267304 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9473 : int_const_expr, arith_const_expr, true,
9474 7267304 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9475 :
9476 : /* Store the expression if valid; else report error. */
9477 :
9478 7267304 : if (!in_system_header_at (input_location)
9479 7267304 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9480 28136 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9481 : "aggregate initialization");
9482 :
9483 7267304 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9484 7267303 : DECL_INITIAL (decl) = value;
9485 :
9486 : /* ANSI wants warnings about out-of-range constant initializers. */
9487 7267315 : STRIP_TYPE_NOPS (value);
9488 7267304 : if (TREE_STATIC (decl))
9489 179869 : constant_expression_warning (value);
9490 :
9491 : /* Check if we need to set array size from compound literal size. */
9492 7267304 : if (TREE_CODE (type) == ARRAY_TYPE
9493 26331 : && TYPE_DOMAIN (type) == NULL_TREE
9494 7279796 : && value != error_mark_node)
9495 : {
9496 : tree inside_init = init;
9497 :
9498 11620 : STRIP_TYPE_NOPS (inside_init);
9499 11620 : inside_init = fold (inside_init);
9500 :
9501 11620 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9502 : {
9503 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9504 :
9505 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9506 : {
9507 : /* For int foo[] = (int [3]){1}; we need to set array size
9508 : now since later on array initializer will be just the
9509 : brace enclosed list of the compound literal. */
9510 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9511 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9512 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9513 13 : layout_type (type);
9514 13 : layout_decl (cldecl, 0);
9515 13 : TREE_TYPE (decl)
9516 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9517 : }
9518 : }
9519 : }
9520 : }
9521 :
9522 : /* Methods for storing and printing names for error messages. */
9523 :
9524 : /* Implement a spelling stack that allows components of a name to be pushed
9525 : and popped. Each element on the stack is this structure. */
9526 :
9527 : struct spelling
9528 : {
9529 : int kind;
9530 : union
9531 : {
9532 : unsigned HOST_WIDE_INT i;
9533 : const char *s;
9534 : } u;
9535 : };
9536 :
9537 : #define SPELLING_STRING 1
9538 : #define SPELLING_MEMBER 2
9539 : #define SPELLING_BOUNDS 3
9540 :
9541 : static struct spelling *spelling; /* Next stack element (unused). */
9542 : static struct spelling *spelling_base; /* Spelling stack base. */
9543 : static int spelling_size; /* Size of the spelling stack. */
9544 :
9545 : /* Macros to save and restore the spelling stack around push_... functions.
9546 : Alternative to SAVE_SPELLING_STACK. */
9547 :
9548 : #define SPELLING_DEPTH() (spelling - spelling_base)
9549 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9550 :
9551 : /* Push an element on the spelling stack with type KIND and assign VALUE
9552 : to MEMBER. */
9553 :
9554 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9555 : { \
9556 : int depth = SPELLING_DEPTH (); \
9557 : \
9558 : if (depth >= spelling_size) \
9559 : { \
9560 : spelling_size += 10; \
9561 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9562 : spelling_size); \
9563 : RESTORE_SPELLING_DEPTH (depth); \
9564 : } \
9565 : \
9566 : spelling->kind = (KIND); \
9567 : spelling->MEMBER = (VALUE); \
9568 : spelling++; \
9569 : }
9570 :
9571 : /* Push STRING on the stack. Printed literally. */
9572 :
9573 : static void
9574 7264540 : push_string (const char *string)
9575 : {
9576 7264540 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9577 7264540 : }
9578 :
9579 : /* Push a member name on the stack. Printed as '.' STRING. */
9580 :
9581 : static void
9582 1227192 : push_member_name (tree decl)
9583 : {
9584 1227192 : const char *const string
9585 1227192 : = (DECL_NAME (decl)
9586 1227192 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9587 208 : : _("<anonymous>"));
9588 1227192 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9589 1227192 : }
9590 :
9591 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9592 :
9593 : static void
9594 2603626 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9595 : {
9596 2603626 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9597 2603626 : }
9598 :
9599 : /* Compute the maximum size in bytes of the printed spelling. */
9600 :
9601 : static int
9602 2892 : spelling_length (void)
9603 : {
9604 2892 : int size = 0;
9605 2892 : struct spelling *p;
9606 :
9607 6416 : for (p = spelling_base; p < spelling; p++)
9608 : {
9609 3524 : if (p->kind == SPELLING_BOUNDS)
9610 1479 : size += 25;
9611 : else
9612 2045 : size += strlen (p->u.s) + 1;
9613 : }
9614 :
9615 2892 : return size;
9616 : }
9617 :
9618 : /* Print the spelling to BUFFER and return it. */
9619 :
9620 : static char *
9621 2892 : print_spelling (char *buffer)
9622 : {
9623 2892 : char *d = buffer;
9624 2892 : struct spelling *p;
9625 :
9626 6416 : for (p = spelling_base; p < spelling; p++)
9627 3524 : if (p->kind == SPELLING_BOUNDS)
9628 : {
9629 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9630 1479 : d += strlen (d);
9631 : }
9632 : else
9633 : {
9634 2045 : const char *s;
9635 2045 : if (p->kind == SPELLING_MEMBER)
9636 198 : *d++ = '.';
9637 16715 : for (s = p->u.s; (*d = *s++); d++)
9638 : ;
9639 : }
9640 2892 : *d++ = '\0';
9641 2892 : return buffer;
9642 : }
9643 :
9644 : /* Check whether INIT, a floating or integer constant, is
9645 : representable in TYPE, a real floating type with the same radix or
9646 : a decimal floating type initialized with a binary floating
9647 : constant. Return true if OK, false if not. */
9648 : static bool
9649 362 : constexpr_init_fits_real_type (tree type, tree init)
9650 : {
9651 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9652 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9653 362 : if (TREE_CODE (init) == REAL_CST
9654 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9655 : {
9656 : /* Same mode, no conversion required except for the case of
9657 : signaling NaNs if the types are incompatible (e.g. double and
9658 : long double with the same mode). */
9659 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9660 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9661 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9662 : return false;
9663 177 : return true;
9664 : }
9665 185 : if (TREE_CODE (init) == INTEGER_CST)
9666 : {
9667 54 : tree converted = build_real_from_int_cst (type, init);
9668 54 : bool fail = false;
9669 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9670 54 : TYPE_PRECISION (TREE_TYPE (init)));
9671 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9672 54 : }
9673 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9674 : return false;
9675 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9676 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9677 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9678 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9679 20 : return true;
9680 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9681 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9682 : {
9683 : /* This is valid if the real number represented by the
9684 : initializer can be exactly represented in the decimal
9685 : type. Compare the values using MPFR. */
9686 8 : REAL_VALUE_TYPE t;
9687 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9688 8 : mpfr_t bin_val, dec_val;
9689 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9690 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9691 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9692 8 : char string[256];
9693 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9694 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9695 8 : && mpfr_equal_p (bin_val, dec_val));
9696 8 : mpfr_clear (bin_val);
9697 8 : mpfr_clear (dec_val);
9698 8 : return res;
9699 : }
9700 : /* exact_real_truncate is not quite right here, since it doesn't
9701 : allow even an exact conversion to subnormal values. */
9702 86 : REAL_VALUE_TYPE t;
9703 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9704 86 : return real_identical (&t, &TREE_REAL_CST (init));
9705 : }
9706 :
9707 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9708 : initializer for type TYPE, and give an error if not. INIT has
9709 : already been folded and verified to be constant. INT_CONST_EXPR
9710 : and ARITH_CONST_EXPR say whether it is an integer constant
9711 : expression or arithmetic constant expression, respectively. If
9712 : TYPE is not a scalar type, this function does nothing. */
9713 :
9714 : static void
9715 912 : check_constexpr_init (location_t loc, tree type, tree init,
9716 : bool int_const_expr, bool arith_const_expr)
9717 : {
9718 912 : if (POINTER_TYPE_P (type))
9719 : {
9720 : /* The initializer must be null. */
9721 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9722 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9723 86 : return;
9724 : }
9725 826 : if (INTEGRAL_TYPE_P (type))
9726 : {
9727 : /* The initializer must be an integer constant expression,
9728 : representable in the target type. */
9729 316 : if (!int_const_expr)
9730 : {
9731 13 : if (TREE_CODE (init) == RAW_DATA_CST
9732 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9733 : {
9734 4 : if (!TYPE_UNSIGNED (type))
9735 137 : for (unsigned int i = 0;
9736 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9737 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9738 : {
9739 1 : error_at (loc, "%<constexpr%> initializer not "
9740 : "representable in type of object");
9741 1 : break;
9742 : }
9743 : }
9744 : else
9745 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9746 : "integer constant expression");
9747 : }
9748 303 : else if (!int_fits_type_p (init, type))
9749 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9750 : "type of object");
9751 316 : return;
9752 : }
9753 : /* We don't apply any extra checks to extension types such as vector
9754 : or fixed-point types. */
9755 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9756 : return;
9757 353 : if (!arith_const_expr)
9758 : {
9759 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9760 : "constant expression");
9761 5 : return;
9762 : }
9763 : /* We don't apply any extra checks to complex integers. */
9764 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9765 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9766 : return;
9767 : /* Following N3082, a real type cannot be initialized from a complex
9768 : type and a binary type cannot be initialized from a decimal type
9769 : (but initializing a decimal type from a binary type is OK).
9770 : Signaling NaN initializers are OK only if the types are
9771 : compatible (not just the same mode); all quiet NaN and infinity
9772 : initializations are considered to preserve the value. */
9773 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9774 348 : && SCALAR_FLOAT_TYPE_P (type))
9775 : {
9776 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9777 : "complex type");
9778 6 : return;
9779 : }
9780 342 : if (SCALAR_FLOAT_TYPE_P (type)
9781 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9782 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9783 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9784 : {
9785 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9786 : "floating-point type is of decimal type");
9787 6 : return;
9788 : }
9789 336 : bool fits;
9790 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9791 : {
9792 42 : switch (TREE_CODE (init))
9793 : {
9794 10 : case INTEGER_CST:
9795 10 : case REAL_CST:
9796 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9797 10 : break;
9798 32 : case COMPLEX_CST:
9799 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9800 32 : TREE_REALPART (init))
9801 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9802 26 : TREE_IMAGPART (init)));
9803 : break;
9804 0 : default:
9805 0 : gcc_unreachable ();
9806 : }
9807 : }
9808 : else
9809 294 : fits = constexpr_init_fits_real_type (type, init);
9810 304 : if (!fits)
9811 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9812 : "type of object");
9813 : }
9814 :
9815 : /* Digest the parser output INIT as an initializer for type TYPE
9816 : initializing DECL.
9817 : Return a C expression of type TYPE to represent the initial value.
9818 :
9819 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9820 :
9821 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9822 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9823 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9824 : constant expression, false if it has already been determined in the
9825 : caller that it is not (but folding may have made the value passed here
9826 : indistinguishable from an arithmetic constant expression).
9827 :
9828 : If INIT is a string constant, STRICT_STRING is true if it is
9829 : unparenthesized or we should not warn here for it being parenthesized.
9830 : For other types of INIT, STRICT_STRING is not used.
9831 :
9832 : INIT_LOC is the location of the INIT.
9833 :
9834 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9835 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9836 : on initializers for 'constexpr' objects apply. */
9837 :
9838 : static tree
9839 16888969 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9840 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9841 : bool arith_const_expr, bool strict_string,
9842 : bool require_constant, bool require_constexpr)
9843 : {
9844 16888969 : enum tree_code code = TREE_CODE (type);
9845 16888969 : tree inside_init = init;
9846 16888969 : tree semantic_type = NULL_TREE;
9847 16888969 : bool maybe_const = true;
9848 :
9849 16888969 : if (type == error_mark_node
9850 16888969 : || !init
9851 33777935 : || error_operand_p (init))
9852 : return error_mark_node;
9853 :
9854 16896436 : STRIP_TYPE_NOPS (inside_init);
9855 :
9856 : /* If require_constant is TRUE, when the initializer is a call to
9857 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9858 : For example:
9859 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9860 : will be converted to
9861 : y = &static_annotated.c. */
9862 :
9863 16887876 : if (require_constant
9864 2894543 : && TREE_CODE (inside_init) == NOP_EXPR
9865 736235 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9866 16887878 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9867 2 : inside_init
9868 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9869 :
9870 16887876 : if (!c_in_omp_for)
9871 : {
9872 16883124 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9873 : {
9874 441 : semantic_type = TREE_TYPE (inside_init);
9875 441 : inside_init = TREE_OPERAND (inside_init, 0);
9876 : }
9877 16883124 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9878 : }
9879 : /* TODO: this may not detect all cases of expressions folding to
9880 : constants that are not arithmetic constant expressions. */
9881 16887876 : if (!maybe_const)
9882 : arith_const_expr = false;
9883 25201562 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9884 5652217 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9885 16819461 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9886 : arith_const_expr = false;
9887 8410644 : else if (TREE_CODE (inside_init) != INTEGER_CST
9888 : && TREE_CODE (inside_init) != REAL_CST
9889 : && TREE_CODE (inside_init) != COMPLEX_CST
9890 : && TREE_CODE (inside_init) != RAW_DATA_CST)
9891 : arith_const_expr = false;
9892 5074271 : else if (TREE_OVERFLOW (inside_init))
9893 11813649 : arith_const_expr = false;
9894 :
9895 : /* Initialization of an array of chars from a string constant
9896 : optionally enclosed in braces. */
9897 :
9898 16887876 : if (code == ARRAY_TYPE && inside_init
9899 188402 : && TREE_CODE (inside_init) == STRING_CST)
9900 : {
9901 11710 : tree typ1
9902 11710 : = (TYPE_ATOMIC (TREE_TYPE (type))
9903 11710 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9904 : TYPE_QUAL_ATOMIC)
9905 11696 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9906 : /* Note that an array could be both an array of character type
9907 : and an array of wchar_t if wchar_t is signed char or unsigned
9908 : char. */
9909 23420 : bool char_array = (typ1 == char_type_node
9910 516 : || typ1 == signed_char_type_node
9911 12191 : || typ1 == unsigned_char_type_node);
9912 11710 : bool wchar_array = !!comptypes (typ1, wchar_type_node);
9913 11710 : bool char16_array = !!comptypes (typ1, char16_type_node);
9914 11710 : bool char32_array = !!comptypes (typ1, char32_type_node);
9915 :
9916 11710 : if (char_array || wchar_array || char16_array || char32_array)
9917 : {
9918 11681 : struct c_expr expr;
9919 11681 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9920 11681 : bool incompat_string_cst = false;
9921 11681 : expr.value = inside_init;
9922 11681 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9923 11681 : expr.original_type = NULL;
9924 11681 : expr.m_decimal = 0;
9925 11681 : maybe_warn_string_init (init_loc, type, expr);
9926 :
9927 11681 : if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9928 91 : pedwarn_init (init_loc, OPT_Wpedantic,
9929 : "initialization of a flexible array member");
9930 :
9931 11681 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9932 11681 : TYPE_MAIN_VARIANT (type)))
9933 : return inside_init;
9934 :
9935 4185 : if (char_array)
9936 : {
9937 4074 : if (typ2 != char_type_node && typ2 != char8_type_node)
9938 : incompat_string_cst = true;
9939 : }
9940 111 : else if (!comptypes (typ1, typ2))
9941 : incompat_string_cst = true;
9942 :
9943 : if (incompat_string_cst)
9944 : {
9945 72 : error_init (init_loc, "cannot initialize array of %qT from "
9946 : "a string literal with type array of %qT",
9947 : typ1, typ2);
9948 72 : return error_mark_node;
9949 : }
9950 :
9951 4113 : if (require_constexpr
9952 4113 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9953 : {
9954 : /* Check if all characters of the string can be
9955 : represented in the type of the constexpr object being
9956 : initialized. */
9957 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9958 24 : const unsigned char *p =
9959 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
9960 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9961 70 : for (unsigned i = 0; i < len; i++)
9962 58 : if (p[i] > 127)
9963 : {
9964 12 : error_init (init_loc, "%<constexpr%> initializer not "
9965 : "representable in type of object");
9966 12 : break;
9967 : }
9968 : }
9969 :
9970 4113 : if (TYPE_DOMAIN (type) != NULL_TREE
9971 4019 : && TYPE_SIZE (type) != NULL_TREE
9972 8073 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9973 : {
9974 3960 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9975 :
9976 3960 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9977 : {
9978 396 : unsigned HOST_WIDE_INT avail
9979 396 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9980 396 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9981 396 : const char *p = TREE_STRING_POINTER (inside_init);
9982 :
9983 : /* Construct truncated string. */
9984 396 : inside_init = build_string (avail, p);
9985 :
9986 : /* Subtract the size of a single (possibly wide) character
9987 : because it may be ok to ignore the terminating NUL char
9988 : that is counted in the length of the constant. */
9989 396 : if (len - unit > avail)
9990 63 : pedwarn_init (init_loc, 0,
9991 : "initializer-string for array of %qT "
9992 : "is too long (%wu chars into %wu "
9993 : "available)", typ1, len, avail);
9994 333 : else if (warn_cxx_compat)
9995 9 : warning_at (init_loc, OPT_Wc___compat,
9996 : "initializer-string for array of %qT "
9997 : "is too long for C++ (%wu chars into %wu "
9998 : "available)", typ1, len, avail);
9999 324 : else if (warn_unterminated_string_initialization
10000 324 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10001 22 : warning_at (init_loc,
10002 22 : OPT_Wunterminated_string_initialization,
10003 : "initializer-string for array of %qT "
10004 : "truncates NUL terminator but destination "
10005 : "lacks %qs attribute (%wu chars into %wu "
10006 : "available)", typ1, "nonstring", len, avail);
10007 : }
10008 : }
10009 :
10010 4113 : TREE_TYPE (inside_init) = type;
10011 4113 : return inside_init;
10012 : }
10013 29 : else if (INTEGRAL_TYPE_P (typ1))
10014 : {
10015 29 : error_init (init_loc, "array of inappropriate type initialized "
10016 : "from string constant");
10017 29 : return error_mark_node;
10018 : }
10019 : }
10020 :
10021 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10022 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10023 : below and handle as a constructor. */
10024 16876166 : if (code == VECTOR_TYPE
10025 6307180 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10026 6307178 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10027 23183306 : && TREE_CONSTANT (inside_init))
10028 : {
10029 632895 : if (TREE_CODE (inside_init) == VECTOR_CST
10030 632992 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10031 97 : TYPE_MAIN_VARIANT (type)))
10032 : return inside_init;
10033 :
10034 632798 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10035 : {
10036 : unsigned HOST_WIDE_INT ix;
10037 : tree value;
10038 4171236 : bool constant_p = true;
10039 :
10040 : /* Iterate through elements and check if all constructor
10041 : elements are *_CSTs. */
10042 4171236 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10043 3538494 : if (!CONSTANT_CLASS_P (value))
10044 : {
10045 : constant_p = false;
10046 : break;
10047 : }
10048 :
10049 632798 : if (constant_p)
10050 632742 : return build_vector_from_ctor (type,
10051 632742 : CONSTRUCTOR_ELTS (inside_init));
10052 : }
10053 : }
10054 :
10055 16243327 : if (warn_sequence_point)
10056 2447846 : verify_sequence_points (inside_init);
10057 :
10058 : /* Any type can be initialized
10059 : from an expression of the same type, optionally with braces. */
10060 :
10061 16243327 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10062 32486654 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10063 16243327 : TYPE_MAIN_VARIANT (type))
10064 3131378 : || (code == ARRAY_TYPE
10065 477 : && comptypes (TREE_TYPE (inside_init), type))
10066 3131378 : || (gnu_vector_type_p (type)
10067 189 : && comptypes (TREE_TYPE (inside_init), type))
10068 3131378 : || (code == POINTER_TYPE
10069 111960 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10070 9290 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10071 9290 : TREE_TYPE (type)))))
10072 : {
10073 13113925 : if (code == POINTER_TYPE)
10074 : {
10075 784602 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10076 : {
10077 1976 : if (TREE_CODE (inside_init) == STRING_CST
10078 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10079 1973 : inside_init = array_to_pointer_conversion
10080 1973 : (init_loc, inside_init);
10081 : else
10082 : {
10083 3 : error_init (init_loc, "invalid use of non-lvalue array");
10084 3 : return error_mark_node;
10085 : }
10086 : }
10087 : }
10088 :
10089 13113922 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10090 : /* Although the types are compatible, we may require a
10091 : conversion. */
10092 5674285 : inside_init = convert (type, inside_init);
10093 :
10094 13113922 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10095 13113922 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10096 : {
10097 0 : error_init (init_loc, "invalid initializer");
10098 0 : return error_mark_node;
10099 : }
10100 :
10101 13113922 : if (require_constant
10102 2298163 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10103 : {
10104 : /* As an extension, allow initializing objects with static storage
10105 : duration with compound literals (which are then treated just as
10106 : the brace enclosed list they contain). Also allow this for
10107 : vectors, as we can only assign them with compound literals. */
10108 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10109 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10110 : "is not constant");
10111 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10112 31 : inside_init = DECL_INITIAL (decl);
10113 : }
10114 :
10115 13113922 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10116 176215 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10117 : {
10118 2 : error_init (init_loc, "array initialized from non-constant array "
10119 : "expression");
10120 2 : return error_mark_node;
10121 : }
10122 :
10123 : /* Compound expressions can only occur here if -Wpedantic or
10124 : -pedantic-errors is specified. In the later case, we always want
10125 : an error. In the former case, we simply want a warning. */
10126 13113920 : if (require_constant && pedantic
10127 20284 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10128 : {
10129 1 : inside_init
10130 1 : = valid_compound_expr_initializer (inside_init,
10131 1 : TREE_TYPE (inside_init));
10132 1 : if (inside_init == error_mark_node)
10133 1 : error_init (init_loc, "initializer element is not constant");
10134 : else
10135 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10136 : "initializer element is not constant");
10137 1 : if (flag_pedantic_errors)
10138 0 : inside_init = error_mark_node;
10139 : }
10140 2298162 : else if (require_constant
10141 2298162 : && !initializer_constant_valid_p (inside_init,
10142 2298162 : TREE_TYPE (inside_init)))
10143 : {
10144 94 : error_init (init_loc, "initializer element is not constant");
10145 94 : inside_init = error_mark_node;
10146 : }
10147 13113825 : else if (require_constant && !maybe_const)
10148 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10149 : "initializer element is not a constant expression");
10150 13113606 : else if (require_constexpr)
10151 584 : check_constexpr_init (init_loc, type, inside_init,
10152 : int_const_expr, arith_const_expr);
10153 :
10154 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10155 13113920 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10156 853491 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10157 : type, inside_init, origtype,
10158 : (require_constant
10159 : ? ic_init_const
10160 : : ic_init), null_pointer_constant,
10161 : NULL_TREE, NULL_TREE, 0);
10162 13113920 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10163 259 : && c_inhibit_evaluation_warnings == 0
10164 259 : && warn_conversion
10165 1 : && !TYPE_UNSIGNED (type)
10166 13113921 : && TYPE_PRECISION (type) == CHAR_BIT)
10167 303 : for (unsigned int i = 0;
10168 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10169 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10170 10 : warning_at (init_loc, OPT_Wconversion,
10171 : "conversion from %qT to %qT changes value from "
10172 : "%qd to %qd",
10173 : integer_type_node, type,
10174 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10175 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10176 13113920 : return inside_init;
10177 : }
10178 :
10179 : /* Handle scalar types, including conversions. */
10180 :
10181 3129402 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10182 128073 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10183 11093 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10184 9030 : || code == BITINT_TYPE)
10185 : {
10186 3128914 : tree unconverted_init = inside_init;
10187 3128914 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10188 3128914 : && (TREE_CODE (init) == STRING_CST
10189 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10190 7315 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10191 3128914 : if (semantic_type)
10192 436 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10193 : inside_init);
10194 3128914 : inside_init
10195 5676229 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10196 : inside_init, origtype,
10197 : require_constant ? ic_init_const : ic_init,
10198 : null_pointer_constant, NULL_TREE, NULL_TREE,
10199 : 0);
10200 :
10201 : /* Check to see if we have already given an error message. */
10202 3128914 : if (inside_init == error_mark_node)
10203 : ;
10204 3128454 : else if (require_constant && !TREE_CONSTANT (inside_init))
10205 : {
10206 36 : error_init (init_loc, "initializer element is not constant");
10207 36 : inside_init = error_mark_node;
10208 : }
10209 3128418 : else if (require_constant
10210 3709589 : && !initializer_constant_valid_p (inside_init,
10211 581171 : TREE_TYPE (inside_init)))
10212 : {
10213 10 : error_init (init_loc, "initializer element is not computable at "
10214 : "load time");
10215 10 : inside_init = error_mark_node;
10216 : }
10217 3128408 : else if (require_constant && !maybe_const)
10218 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10219 : "initializer element is not a constant expression");
10220 3128403 : else if (require_constexpr)
10221 328 : check_constexpr_init (init_loc, type, unconverted_init,
10222 : int_const_expr, arith_const_expr);
10223 :
10224 3128914 : return inside_init;
10225 : }
10226 :
10227 : /* Come here only for records and arrays. */
10228 :
10229 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10230 : {
10231 0 : error_init (init_loc,
10232 : "variable-sized object may not be initialized except "
10233 : "with an empty initializer");
10234 0 : return error_mark_node;
10235 : }
10236 :
10237 488 : error_init (init_loc, "invalid initializer");
10238 488 : return error_mark_node;
10239 : }
10240 :
10241 : /* Handle initializers that use braces. */
10242 :
10243 : /* Type of object we are accumulating a constructor for.
10244 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10245 : static tree constructor_type;
10246 :
10247 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10248 : left to fill. */
10249 : static tree constructor_fields;
10250 :
10251 : /* For an ARRAY_TYPE, this is the specified index
10252 : at which to store the next element we get. */
10253 : static tree constructor_index;
10254 :
10255 : /* For an ARRAY_TYPE, this is the maximum index. */
10256 : static tree constructor_max_index;
10257 :
10258 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10259 : static tree constructor_unfilled_fields;
10260 :
10261 : /* For an ARRAY_TYPE, this is the index of the first element
10262 : not yet written out. */
10263 : static tree constructor_unfilled_index;
10264 :
10265 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10266 : This is so we can generate gaps between fields, when appropriate. */
10267 : static tree constructor_bit_index;
10268 :
10269 : /* If we are saving up the elements rather than allocating them,
10270 : this is the list of elements so far (in reverse order,
10271 : most recent first). */
10272 : static vec<constructor_elt, va_gc> *constructor_elements;
10273 :
10274 : /* 1 if constructor should be incrementally stored into a constructor chain,
10275 : 0 if all the elements should be kept in AVL tree. */
10276 : static int constructor_incremental;
10277 :
10278 : /* 1 if so far this constructor's elements are all compile-time constants. */
10279 : static int constructor_constant;
10280 :
10281 : /* 1 if so far this constructor's elements are all valid address constants. */
10282 : static int constructor_simple;
10283 :
10284 : /* 1 if this constructor has an element that cannot be part of a
10285 : constant expression. */
10286 : static int constructor_nonconst;
10287 :
10288 : /* 1 if this constructor is erroneous so far. */
10289 : static int constructor_erroneous;
10290 :
10291 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10292 : static int constructor_zeroinit;
10293 :
10294 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10295 : static bool constructor_zero_padding_bits;
10296 :
10297 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10298 : of braces are an error). */
10299 : static bool constructor_braced_scalar;
10300 :
10301 : /* Structure for managing pending initializer elements, organized as an
10302 : AVL tree. */
10303 :
10304 : struct init_node
10305 : {
10306 : struct init_node *left, *right;
10307 : struct init_node *parent;
10308 : int balance;
10309 : tree purpose;
10310 : tree value;
10311 : tree origtype;
10312 : };
10313 :
10314 : /* Tree of pending elements at this constructor level.
10315 : These are elements encountered out of order
10316 : which belong at places we haven't reached yet in actually
10317 : writing the output.
10318 : Will never hold tree nodes across GC runs. */
10319 : static struct init_node *constructor_pending_elts;
10320 :
10321 : /* The SPELLING_DEPTH of this constructor. */
10322 : static int constructor_depth;
10323 :
10324 : /* DECL node for which an initializer is being read.
10325 : 0 means we are reading a constructor expression
10326 : such as (struct foo) {...}. */
10327 : static tree constructor_decl;
10328 :
10329 : /* Nonzero if there were any member designators in this initializer. */
10330 : static int constructor_designated;
10331 :
10332 : /* Nesting depth of designator list. */
10333 : static int designator_depth;
10334 :
10335 : /* Nonzero if there were diagnosed errors in this designator list. */
10336 : static int designator_erroneous;
10337 :
10338 :
10339 : /* This stack has a level for each implicit or explicit level of
10340 : structuring in the initializer, including the outermost one. It
10341 : saves the values of most of the variables above. */
10342 :
10343 : struct constructor_range_stack;
10344 :
10345 : struct constructor_stack
10346 : {
10347 : struct constructor_stack *next;
10348 : tree type;
10349 : tree fields;
10350 : tree index;
10351 : tree max_index;
10352 : tree unfilled_index;
10353 : tree unfilled_fields;
10354 : tree bit_index;
10355 : vec<constructor_elt, va_gc> *elements;
10356 : struct init_node *pending_elts;
10357 : int offset;
10358 : int depth;
10359 : /* If value nonzero, this value should replace the entire
10360 : constructor at this level. */
10361 : struct c_expr replacement_value;
10362 : struct constructor_range_stack *range_stack;
10363 : char constant;
10364 : char simple;
10365 : char nonconst;
10366 : char implicit;
10367 : char erroneous;
10368 : char outer;
10369 : char incremental;
10370 : char designated;
10371 : bool zero_padding_bits;
10372 : bool braced_scalar;
10373 : int designator_depth;
10374 : };
10375 :
10376 : static struct constructor_stack *constructor_stack;
10377 :
10378 : /* This stack represents designators from some range designator up to
10379 : the last designator in the list. */
10380 :
10381 : struct constructor_range_stack
10382 : {
10383 : struct constructor_range_stack *next, *prev;
10384 : struct constructor_stack *stack;
10385 : tree range_start;
10386 : tree index;
10387 : tree range_end;
10388 : tree fields;
10389 : };
10390 :
10391 : static struct constructor_range_stack *constructor_range_stack;
10392 :
10393 : /* This stack records separate initializers that are nested.
10394 : Nested initializers can't happen in ANSI C, but GNU C allows them
10395 : in cases like { ... (struct foo) { ... } ... }. */
10396 :
10397 : struct initializer_stack
10398 : {
10399 : struct initializer_stack *next;
10400 : tree decl;
10401 : struct constructor_stack *constructor_stack;
10402 : struct constructor_range_stack *constructor_range_stack;
10403 : vec<constructor_elt, va_gc> *elements;
10404 : struct spelling *spelling;
10405 : struct spelling *spelling_base;
10406 : int spelling_size;
10407 : char require_constant_value;
10408 : char require_constant_elements;
10409 : char require_constexpr_value;
10410 : char designated;
10411 : rich_location *missing_brace_richloc;
10412 : };
10413 :
10414 : static struct initializer_stack *initializer_stack;
10415 :
10416 : /* Prepare to parse and output the initializer for variable DECL. */
10417 :
10418 : void
10419 7264540 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10420 : bool init_require_constant, bool init_require_constexpr,
10421 : rich_location *richloc)
10422 : {
10423 7264540 : const char *locus;
10424 7264540 : struct initializer_stack *p = XNEW (struct initializer_stack);
10425 :
10426 7264540 : p->decl = constructor_decl;
10427 7264540 : p->require_constant_value = require_constant_value;
10428 7264540 : p->require_constant_elements = require_constant_elements;
10429 7264540 : p->require_constexpr_value = require_constexpr_value;
10430 7264540 : p->constructor_stack = constructor_stack;
10431 7264540 : p->constructor_range_stack = constructor_range_stack;
10432 7264540 : p->elements = constructor_elements;
10433 7264540 : p->spelling = spelling;
10434 7264540 : p->spelling_base = spelling_base;
10435 7264540 : p->spelling_size = spelling_size;
10436 7264540 : p->next = initializer_stack;
10437 7264540 : p->missing_brace_richloc = richloc;
10438 7264540 : p->designated = constructor_designated;
10439 7264540 : initializer_stack = p;
10440 :
10441 7264540 : constructor_decl = decl;
10442 7264540 : constructor_designated = 0;
10443 :
10444 7264540 : require_constant_value = init_require_constant;
10445 7264540 : require_constexpr_value = init_require_constexpr;
10446 7264540 : if (decl != NULL_TREE && decl != error_mark_node)
10447 : {
10448 6343383 : require_constant_elements
10449 6166710 : = ((init_require_constant || (pedantic && !flag_isoc99))
10450 : /* For a scalar, you can always use any value to initialize,
10451 : even within braces. */
10452 6355331 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10453 6343383 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10454 : }
10455 : else
10456 : {
10457 921157 : require_constant_elements = false;
10458 921157 : locus = _("(anonymous)");
10459 : }
10460 :
10461 7264540 : constructor_stack = 0;
10462 7264540 : constructor_range_stack = 0;
10463 :
10464 7264540 : found_missing_braces = 0;
10465 :
10466 7264540 : spelling_base = 0;
10467 7264540 : spelling_size = 0;
10468 7264540 : RESTORE_SPELLING_DEPTH (0);
10469 :
10470 7264540 : if (locus)
10471 7264540 : push_string (locus);
10472 7264540 : }
10473 :
10474 : void
10475 7264538 : finish_init (void)
10476 : {
10477 7264538 : struct initializer_stack *p = initializer_stack;
10478 :
10479 : /* Free the whole constructor stack of this initializer. */
10480 7264538 : while (constructor_stack)
10481 : {
10482 0 : struct constructor_stack *q = constructor_stack;
10483 0 : constructor_stack = q->next;
10484 0 : XDELETE (q);
10485 : }
10486 :
10487 7264538 : gcc_assert (!constructor_range_stack);
10488 :
10489 : /* Pop back to the data of the outer initializer (if any). */
10490 7264538 : XDELETE (spelling_base);
10491 :
10492 7264538 : constructor_decl = p->decl;
10493 7264538 : require_constant_value = p->require_constant_value;
10494 7264538 : require_constant_elements = p->require_constant_elements;
10495 7264538 : require_constexpr_value = p->require_constexpr_value;
10496 7264538 : constructor_stack = p->constructor_stack;
10497 7264538 : constructor_designated = p->designated;
10498 7264538 : constructor_range_stack = p->constructor_range_stack;
10499 7264538 : constructor_elements = p->elements;
10500 7264538 : spelling = p->spelling;
10501 7264538 : spelling_base = p->spelling_base;
10502 7264538 : spelling_size = p->spelling_size;
10503 7264538 : initializer_stack = p->next;
10504 7264538 : XDELETE (p);
10505 7264538 : }
10506 :
10507 : /* Call here when we see the initializer is surrounded by braces.
10508 : This is instead of a call to push_init_level;
10509 : it is matched by a call to pop_init_level.
10510 :
10511 : TYPE is the type to initialize, for a constructor expression.
10512 : For an initializer for a decl, TYPE is zero. */
10513 :
10514 : void
10515 1024653 : really_start_incremental_init (tree type)
10516 : {
10517 1024653 : struct constructor_stack *p = XNEW (struct constructor_stack);
10518 :
10519 1024653 : if (type == NULL_TREE)
10520 105460 : type = TREE_TYPE (constructor_decl);
10521 :
10522 1024653 : if (VECTOR_TYPE_P (type)
10523 1024653 : && TYPE_VECTOR_OPAQUE (type))
10524 0 : error ("opaque vector types cannot be initialized");
10525 :
10526 1024653 : p->type = constructor_type;
10527 1024653 : p->fields = constructor_fields;
10528 1024653 : p->index = constructor_index;
10529 1024653 : p->max_index = constructor_max_index;
10530 1024653 : p->unfilled_index = constructor_unfilled_index;
10531 1024653 : p->unfilled_fields = constructor_unfilled_fields;
10532 1024653 : p->bit_index = constructor_bit_index;
10533 1024653 : p->elements = constructor_elements;
10534 1024653 : p->constant = constructor_constant;
10535 1024653 : p->simple = constructor_simple;
10536 1024653 : p->nonconst = constructor_nonconst;
10537 1024653 : p->erroneous = constructor_erroneous;
10538 1024653 : p->pending_elts = constructor_pending_elts;
10539 1024653 : p->depth = constructor_depth;
10540 1024653 : p->replacement_value.value = 0;
10541 1024653 : p->replacement_value.original_code = ERROR_MARK;
10542 1024653 : p->replacement_value.original_type = NULL;
10543 1024653 : p->implicit = 0;
10544 1024653 : p->range_stack = 0;
10545 1024653 : p->outer = 0;
10546 1024653 : p->incremental = constructor_incremental;
10547 1024653 : p->designated = constructor_designated;
10548 1024653 : p->zero_padding_bits = constructor_zero_padding_bits;
10549 1024653 : p->braced_scalar = constructor_braced_scalar;
10550 1024653 : p->designator_depth = designator_depth;
10551 1024653 : p->next = 0;
10552 1024653 : constructor_stack = p;
10553 :
10554 1024653 : constructor_constant = 1;
10555 1024653 : constructor_simple = 1;
10556 1024653 : constructor_nonconst = 0;
10557 1024653 : constructor_depth = SPELLING_DEPTH ();
10558 1024653 : constructor_elements = NULL;
10559 1024653 : constructor_pending_elts = 0;
10560 1024653 : constructor_type = type;
10561 1024653 : constructor_incremental = 1;
10562 1024653 : constructor_designated = 0;
10563 1024653 : constructor_zero_padding_bits = false;
10564 1024653 : constructor_zeroinit = 1;
10565 1024653 : constructor_braced_scalar = false;
10566 1024653 : designator_depth = 0;
10567 1024653 : designator_erroneous = 0;
10568 :
10569 1024653 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10570 : {
10571 81429 : constructor_fields = TYPE_FIELDS (constructor_type);
10572 : /* Skip any nameless bit fields at the beginning. */
10573 81429 : while (constructor_fields != NULL_TREE
10574 81457 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10575 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10576 :
10577 81429 : constructor_unfilled_fields = constructor_fields;
10578 81429 : constructor_bit_index = bitsize_zero_node;
10579 : }
10580 943224 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10581 : {
10582 17866 : if (TYPE_DOMAIN (constructor_type))
10583 : {
10584 9400 : constructor_max_index
10585 9400 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10586 :
10587 : /* Detect non-empty initializations of zero-length arrays. */
10588 9400 : if (constructor_max_index == NULL_TREE
10589 9400 : && TYPE_SIZE (constructor_type))
10590 205 : constructor_max_index = integer_minus_one_node;
10591 :
10592 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10593 : to initialize VLAs with a nonempty initializer will cause a
10594 : proper error; avoid tree checking errors as well by setting a
10595 : safe value. */
10596 9400 : if (constructor_max_index
10597 9400 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10598 73 : constructor_max_index = integer_minus_one_node;
10599 :
10600 9400 : constructor_index
10601 9400 : = convert (bitsizetype,
10602 9400 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10603 : }
10604 : else
10605 : {
10606 8466 : constructor_index = bitsize_zero_node;
10607 8466 : constructor_max_index = NULL_TREE;
10608 : }
10609 :
10610 17866 : constructor_unfilled_index = constructor_index;
10611 : }
10612 925358 : else if (gnu_vector_type_p (constructor_type))
10613 : {
10614 : /* Vectors are like simple fixed-size arrays. */
10615 1849604 : constructor_max_index =
10616 924802 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10617 924802 : constructor_index = bitsize_zero_node;
10618 924802 : constructor_unfilled_index = constructor_index;
10619 : }
10620 : else
10621 : {
10622 : /* Handle the case of int x = {5}; */
10623 556 : constructor_fields = constructor_type;
10624 556 : constructor_unfilled_fields = constructor_type;
10625 556 : constructor_braced_scalar = true;
10626 : }
10627 1024653 : }
10628 :
10629 : extern location_t last_init_list_comma;
10630 :
10631 : /* Called when we see an open brace for a nested initializer. Finish
10632 : off any pending levels with implicit braces. */
10633 : void
10634 340561 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10635 : {
10636 340563 : while (constructor_stack->implicit)
10637 : {
10638 304 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10639 140 : && constructor_fields == NULL_TREE)
10640 0 : process_init_element (input_location,
10641 : pop_init_level (loc, 1, braced_init_obstack,
10642 : last_init_list_comma),
10643 : true, braced_init_obstack);
10644 304 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10645 164 : && constructor_max_index
10646 468 : && tree_int_cst_lt (constructor_max_index,
10647 : constructor_index))
10648 2 : process_init_element (input_location,
10649 : pop_init_level (loc, 1, braced_init_obstack,
10650 : last_init_list_comma),
10651 : true, braced_init_obstack);
10652 : else
10653 : break;
10654 : }
10655 340561 : }
10656 :
10657 : /* Push down into a subobject, for initialization.
10658 : If this is for an explicit set of braces, IMPLICIT is 0.
10659 : If it is because the next element belongs at a lower level,
10660 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10661 :
10662 : void
10663 1043241 : push_init_level (location_t loc, int implicit,
10664 : struct obstack *braced_init_obstack)
10665 : {
10666 1043241 : struct constructor_stack *p;
10667 1043241 : tree value = NULL_TREE;
10668 :
10669 : /* Unless this is an explicit brace, we need to preserve previous
10670 : content if any. */
10671 1043241 : if (implicit)
10672 : {
10673 703622 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10674 798 : value = find_init_member (constructor_fields, braced_init_obstack);
10675 702824 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10676 702824 : value = find_init_member (constructor_index, braced_init_obstack);
10677 : }
10678 :
10679 1043241 : p = XNEW (struct constructor_stack);
10680 1043241 : p->type = constructor_type;
10681 1043241 : p->fields = constructor_fields;
10682 1043241 : p->index = constructor_index;
10683 1043241 : p->max_index = constructor_max_index;
10684 1043241 : p->unfilled_index = constructor_unfilled_index;
10685 1043241 : p->unfilled_fields = constructor_unfilled_fields;
10686 1043241 : p->bit_index = constructor_bit_index;
10687 1043241 : p->elements = constructor_elements;
10688 1043241 : p->constant = constructor_constant;
10689 1043241 : p->simple = constructor_simple;
10690 1043241 : p->nonconst = constructor_nonconst;
10691 1043241 : p->erroneous = constructor_erroneous;
10692 1043241 : p->pending_elts = constructor_pending_elts;
10693 1043241 : p->depth = constructor_depth;
10694 1043241 : p->replacement_value.value = NULL_TREE;
10695 1043241 : p->replacement_value.original_code = ERROR_MARK;
10696 1043241 : p->replacement_value.original_type = NULL;
10697 1043241 : p->implicit = implicit;
10698 1043241 : p->outer = 0;
10699 1043241 : p->incremental = constructor_incremental;
10700 1043241 : p->designated = constructor_designated;
10701 1043241 : p->zero_padding_bits = constructor_zero_padding_bits;
10702 1043241 : p->braced_scalar = constructor_braced_scalar;
10703 1043241 : p->designator_depth = designator_depth;
10704 1043241 : p->next = constructor_stack;
10705 1043241 : p->range_stack = 0;
10706 1043241 : constructor_stack = p;
10707 :
10708 1043241 : constructor_constant = 1;
10709 1043241 : constructor_simple = 1;
10710 1043241 : constructor_nonconst = 0;
10711 1043241 : constructor_depth = SPELLING_DEPTH ();
10712 1043241 : constructor_elements = NULL;
10713 1043241 : constructor_incremental = 1;
10714 : /* If the upper initializer is designated, then mark this as
10715 : designated too to prevent bogus warnings. */
10716 1043241 : constructor_designated = p->designated;
10717 : /* If the upper initializer has padding bits zeroed, that includes
10718 : all nested initializers as well. */
10719 1043241 : constructor_zero_padding_bits = p->zero_padding_bits;
10720 1043241 : constructor_braced_scalar = false;
10721 1043241 : constructor_pending_elts = 0;
10722 1043241 : if (!implicit)
10723 : {
10724 339619 : p->range_stack = constructor_range_stack;
10725 339619 : constructor_range_stack = 0;
10726 339619 : designator_depth = 0;
10727 339619 : designator_erroneous = 0;
10728 : }
10729 :
10730 : /* Don't die if an entire brace-pair level is superfluous
10731 : in the containing level. */
10732 1043241 : if (constructor_type == NULL_TREE)
10733 : ;
10734 1043241 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10735 : {
10736 : /* Don't die if there are extra init elts at the end. */
10737 158857 : if (constructor_fields == NULL_TREE)
10738 51 : constructor_type = NULL_TREE;
10739 : else
10740 : {
10741 158806 : constructor_type = TREE_TYPE (constructor_fields);
10742 158806 : push_member_name (constructor_fields);
10743 158806 : constructor_depth++;
10744 : }
10745 : }
10746 884384 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10747 : {
10748 884333 : constructor_type = TREE_TYPE (constructor_type);
10749 884333 : push_array_bounds (tree_to_uhwi (constructor_index));
10750 884333 : constructor_depth++;
10751 : }
10752 :
10753 1043241 : if (constructor_type == NULL_TREE)
10754 : {
10755 51 : error_init (loc, "extra brace group at end of initializer");
10756 51 : constructor_fields = NULL_TREE;
10757 51 : constructor_unfilled_fields = NULL_TREE;
10758 51 : return;
10759 : }
10760 :
10761 1043190 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10762 : {
10763 360 : constructor_constant = TREE_CONSTANT (value);
10764 360 : constructor_simple = TREE_STATIC (value);
10765 360 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10766 360 : constructor_elements = CONSTRUCTOR_ELTS (value);
10767 360 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10768 360 : if (!vec_safe_is_empty (constructor_elements)
10769 340 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10770 340 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10771 294 : set_nonincremental_init (braced_init_obstack);
10772 : }
10773 :
10774 1043190 : if (implicit == 1)
10775 : {
10776 702680 : found_missing_braces = 1;
10777 702680 : if (initializer_stack->missing_brace_richloc)
10778 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10779 702680 : (loc, "{");
10780 : }
10781 :
10782 1043190 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10783 : {
10784 879733 : constructor_fields = TYPE_FIELDS (constructor_type);
10785 : /* Skip any nameless bit fields at the beginning. */
10786 879733 : while (constructor_fields != NULL_TREE
10787 879797 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10788 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10789 :
10790 879733 : constructor_unfilled_fields = constructor_fields;
10791 879733 : constructor_bit_index = bitsize_zero_node;
10792 : }
10793 163457 : else if (gnu_vector_type_p (constructor_type))
10794 : {
10795 : /* Vectors are like simple fixed-size arrays. */
10796 9602 : constructor_max_index =
10797 4801 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10798 4801 : constructor_index = bitsize_int (0);
10799 4801 : constructor_unfilled_index = constructor_index;
10800 : }
10801 158656 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10802 : {
10803 158576 : if (TYPE_DOMAIN (constructor_type))
10804 : {
10805 158576 : constructor_max_index
10806 158576 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10807 :
10808 : /* Detect non-empty initializations of zero-length arrays. */
10809 158576 : if (constructor_max_index == NULL_TREE
10810 158576 : && TYPE_SIZE (constructor_type))
10811 135 : constructor_max_index = integer_minus_one_node;
10812 :
10813 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10814 : to initialize VLAs will cause a proper error; avoid tree
10815 : checking errors as well by setting a safe value. */
10816 158576 : if (constructor_max_index
10817 158306 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10818 2 : constructor_max_index = integer_minus_one_node;
10819 :
10820 158576 : constructor_index
10821 158576 : = convert (bitsizetype,
10822 158576 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10823 : }
10824 : else
10825 0 : constructor_index = bitsize_zero_node;
10826 :
10827 158576 : constructor_unfilled_index = constructor_index;
10828 158576 : if (value && TREE_CODE (value) == STRING_CST)
10829 : {
10830 : /* We need to split the char/wchar array into individual
10831 : characters, so that we don't have to special case it
10832 : everywhere. */
10833 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10834 : }
10835 : }
10836 : else
10837 : {
10838 80 : if (constructor_type != error_mark_node)
10839 : {
10840 51 : if (p->braced_scalar)
10841 22 : permerror_init (input_location, 0,
10842 : "braces around scalar initializer");
10843 : else
10844 29 : warning_init (input_location, 0,
10845 : "braces around scalar initializer");
10846 51 : constructor_braced_scalar = true;
10847 : }
10848 80 : constructor_fields = constructor_type;
10849 80 : constructor_unfilled_fields = constructor_type;
10850 : }
10851 : }
10852 :
10853 : /* At the end of an implicit or explicit brace level,
10854 : finish up that level of constructor. If a single expression
10855 : with redundant braces initialized that level, return the
10856 : c_expr structure for that expression. Otherwise, the original_code
10857 : element is set to ERROR_MARK.
10858 : If we were outputting the elements as they are read, return 0 as the value
10859 : from inner levels (process_init_element ignores that),
10860 : but return error_mark_node as the value from the outermost level
10861 : (that's what we want to put in DECL_INITIAL).
10862 : Otherwise, return a CONSTRUCTOR expression as the value. */
10863 :
10864 : struct c_expr
10865 2067894 : pop_init_level (location_t loc, int implicit,
10866 : struct obstack *braced_init_obstack,
10867 : location_t insert_before)
10868 : {
10869 2067894 : struct constructor_stack *p;
10870 2067894 : struct c_expr ret;
10871 2067894 : ret.value = NULL_TREE;
10872 2067894 : ret.original_code = ERROR_MARK;
10873 2067894 : ret.original_type = NULL;
10874 2067894 : ret.m_decimal = 0;
10875 :
10876 2067894 : if (implicit == 0)
10877 : {
10878 : /* When we come to an explicit close brace,
10879 : pop any inner levels that didn't have explicit braces. */
10880 1365205 : while (constructor_stack->implicit)
10881 933 : process_init_element (input_location,
10882 : pop_init_level (loc, 1, braced_init_obstack,
10883 : insert_before),
10884 : true, braced_init_obstack);
10885 1364272 : gcc_assert (!constructor_range_stack);
10886 : }
10887 : else
10888 703622 : if (initializer_stack->missing_brace_richloc)
10889 703622 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10890 703622 : (insert_before, "}");
10891 :
10892 : /* Now output all pending elements. */
10893 2067894 : constructor_incremental = 1;
10894 2067894 : output_pending_init_elements (1, braced_init_obstack);
10895 :
10896 2067894 : p = constructor_stack;
10897 :
10898 : /* Error for initializing a flexible array member, or a zero-length
10899 : array member in an inappropriate context. */
10900 2067843 : if (constructor_type && constructor_fields
10901 159387 : && TREE_CODE (constructor_type) == ARRAY_TYPE
10902 152624 : && TYPE_DOMAIN (constructor_type)
10903 2220510 : && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10904 : {
10905 : /* Silently discard empty initializations. The parser will
10906 : already have pedwarned for empty brackets for C17 and earlier. */
10907 300 : if (integer_zerop (constructor_unfilled_index))
10908 84 : constructor_type = NULL_TREE;
10909 300 : if (constructor_type || flag_isoc23)
10910 : {
10911 296 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10912 :
10913 296 : if (constructor_depth <= 2)
10914 249 : pedwarn_init (loc, OPT_Wpedantic,
10915 : "initialization of a flexible array member");
10916 47 : else if (constructor_type)
10917 33 : error_init (loc, "initialization of flexible array member "
10918 : "in a nested context");
10919 : else
10920 14 : pedwarn_init (loc, OPT_Wpedantic,
10921 : "initialization of flexible array member "
10922 : "in a nested context");
10923 :
10924 : /* We have already issued an error message for the existence
10925 : of a flexible array member not at the end of the structure.
10926 : Discard the initializer so that we do not die later. */
10927 296 : if (constructor_type
10928 216 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10929 302 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10930 0 : constructor_type = NULL_TREE;
10931 : }
10932 : }
10933 :
10934 2067894 : switch (vec_safe_length (constructor_elements))
10935 : {
10936 4640 : case 0:
10937 : /* Initialization with { } counts as zeroinit. */
10938 4640 : constructor_zeroinit = 1;
10939 4640 : break;
10940 915178 : case 1:
10941 : /* This might be zeroinit as well. */
10942 915178 : if (integer_zerop ((*constructor_elements)[0].value))
10943 2046 : constructor_zeroinit = 1;
10944 : break;
10945 1148076 : default:
10946 : /* If the constructor has more than one element, it can't be { 0 }. */
10947 1148076 : constructor_zeroinit = 0;
10948 1148076 : break;
10949 : }
10950 :
10951 : /* Warn when some structs are initialized with direct aggregation. */
10952 2067894 : if (!implicit && found_missing_braces && warn_missing_braces
10953 37 : && !constructor_zeroinit)
10954 : {
10955 18 : gcc_assert (initializer_stack->missing_brace_richloc);
10956 18 : warning_at (initializer_stack->missing_brace_richloc,
10957 18 : OPT_Wmissing_braces,
10958 : "missing braces around initializer");
10959 : }
10960 :
10961 : /* Warn when some struct elements are implicitly initialized to zero. */
10962 2067894 : if (warn_missing_field_initializers
10963 425443 : && constructor_type
10964 425415 : && TREE_CODE (constructor_type) == RECORD_TYPE
10965 193395 : && constructor_unfilled_fields)
10966 : {
10967 : /* Do not warn for flexible array members or zero-length arrays. */
10968 43 : while (constructor_unfilled_fields
10969 43 : && (!DECL_SIZE (constructor_unfilled_fields)
10970 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10971 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10972 :
10973 43 : if (constructor_unfilled_fields
10974 : /* Do not warn if this level of the initializer uses member
10975 : designators; it is likely to be deliberate. */
10976 43 : && !constructor_designated
10977 : /* Do not warn about initializing with { 0 } or with { }. */
10978 24 : && !constructor_zeroinit)
10979 : {
10980 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
10981 : "missing initializer for field %qD of %qT",
10982 : constructor_unfilled_fields,
10983 : constructor_type))
10984 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10985 : "%qD declared here", constructor_unfilled_fields);
10986 : }
10987 : }
10988 :
10989 : /* Pad out the end of the structure. */
10990 2067894 : if (p->replacement_value.value)
10991 : /* If this closes a superfluous brace pair,
10992 : just pass out the element between them. */
10993 138 : ret = p->replacement_value;
10994 2067756 : else if (constructor_type == NULL_TREE)
10995 : ;
10996 2067629 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
10997 2067629 : && TREE_CODE (constructor_type) != ARRAY_TYPE
10998 2067629 : && !gnu_vector_type_p (constructor_type))
10999 : {
11000 : /* A nonincremental scalar initializer--just return
11001 : the element, after verifying there is just one.
11002 : Empty scalar initializers are supported in C23. */
11003 636 : if (vec_safe_is_empty (constructor_elements))
11004 : {
11005 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11006 85 : ret.value = error_mark_node;
11007 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11008 : {
11009 2 : error_init (loc, "invalid initializer");
11010 2 : ret.value = error_mark_node;
11011 : }
11012 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11013 : /* Ensure this is a null pointer constant in the case of a
11014 : 'constexpr' object initialized with {}. */
11015 33 : ret.value = build_zero_cst (ptr_type_node);
11016 : else
11017 81 : ret.value = build_zero_cst (constructor_type);
11018 : }
11019 435 : else if (vec_safe_length (constructor_elements) != 1)
11020 : {
11021 0 : error_init (loc, "extra elements in scalar initializer");
11022 0 : ret.value = (*constructor_elements)[0].value;
11023 : }
11024 : else
11025 435 : ret.value = (*constructor_elements)[0].value;
11026 : }
11027 : else
11028 : {
11029 2066993 : if (constructor_erroneous)
11030 272 : ret.value = error_mark_node;
11031 : else
11032 : {
11033 2066721 : ret.value = build_constructor (constructor_type,
11034 : constructor_elements);
11035 2066721 : if (constructor_constant)
11036 1737467 : TREE_CONSTANT (ret.value) = 1;
11037 2066721 : if (constructor_constant && constructor_simple)
11038 1737428 : TREE_STATIC (ret.value) = 1;
11039 2066721 : if (constructor_nonconst)
11040 672 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11041 2066721 : if (constructor_zero_padding_bits)
11042 20 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11043 : }
11044 : }
11045 :
11046 2067894 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11047 : {
11048 1046 : if (constructor_nonconst)
11049 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11050 1031 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11051 0 : ret.original_code = ERROR_MARK;
11052 : }
11053 :
11054 2067894 : constructor_type = p->type;
11055 2067894 : constructor_fields = p->fields;
11056 2067894 : constructor_index = p->index;
11057 2067894 : constructor_max_index = p->max_index;
11058 2067894 : constructor_unfilled_index = p->unfilled_index;
11059 2067894 : constructor_unfilled_fields = p->unfilled_fields;
11060 2067894 : constructor_bit_index = p->bit_index;
11061 2067894 : constructor_elements = p->elements;
11062 2067894 : constructor_constant = p->constant;
11063 2067894 : constructor_simple = p->simple;
11064 2067894 : constructor_nonconst = p->nonconst;
11065 2067894 : constructor_erroneous = p->erroneous;
11066 2067894 : constructor_incremental = p->incremental;
11067 2067894 : constructor_designated = p->designated;
11068 2067894 : constructor_zero_padding_bits = p->zero_padding_bits;
11069 2067894 : constructor_braced_scalar = p->braced_scalar;
11070 2067894 : designator_depth = p->designator_depth;
11071 2067894 : constructor_pending_elts = p->pending_elts;
11072 2067894 : constructor_depth = p->depth;
11073 2067894 : if (!p->implicit)
11074 1364272 : constructor_range_stack = p->range_stack;
11075 2067894 : RESTORE_SPELLING_DEPTH (constructor_depth);
11076 :
11077 2067894 : constructor_stack = p->next;
11078 2067894 : XDELETE (p);
11079 :
11080 2067894 : if (ret.value == NULL_TREE && constructor_stack == 0)
11081 0 : ret.value = error_mark_node;
11082 2067894 : return ret;
11083 : }
11084 :
11085 : /* Common handling for both array range and field name designators.
11086 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11087 :
11088 : static bool
11089 34043 : set_designator (location_t loc, bool array,
11090 : struct obstack *braced_init_obstack)
11091 : {
11092 34043 : tree subtype;
11093 34043 : enum tree_code subcode;
11094 :
11095 : /* Don't die if an entire brace-pair level is superfluous
11096 : in the containing level, or for an erroneous type. */
11097 34043 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11098 : return true;
11099 :
11100 : /* If there were errors in this designator list already, bail out
11101 : silently. */
11102 34034 : if (designator_erroneous)
11103 : return true;
11104 :
11105 : /* Likewise for an initializer for a variable-size type. Those are
11106 : diagnosed in the parser, except for empty initializer braces. */
11107 34034 : if (COMPLETE_TYPE_P (constructor_type)
11108 34034 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11109 : return true;
11110 :
11111 34024 : if (!designator_depth)
11112 : {
11113 33406 : gcc_assert (!constructor_range_stack);
11114 :
11115 : /* Designator list starts at the level of closest explicit
11116 : braces. */
11117 33789 : while (constructor_stack->implicit)
11118 383 : process_init_element (input_location,
11119 : pop_init_level (loc, 1, braced_init_obstack,
11120 : last_init_list_comma),
11121 : true, braced_init_obstack);
11122 33406 : constructor_designated = 1;
11123 33406 : return false;
11124 : }
11125 :
11126 618 : switch (TREE_CODE (constructor_type))
11127 : {
11128 421 : case RECORD_TYPE:
11129 421 : case UNION_TYPE:
11130 421 : subtype = TREE_TYPE (constructor_fields);
11131 421 : if (subtype != error_mark_node)
11132 421 : subtype = TYPE_MAIN_VARIANT (subtype);
11133 : break;
11134 197 : case ARRAY_TYPE:
11135 197 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11136 197 : break;
11137 0 : default:
11138 0 : gcc_unreachable ();
11139 : }
11140 :
11141 618 : subcode = TREE_CODE (subtype);
11142 618 : if (array && subcode != ARRAY_TYPE)
11143 : {
11144 1 : error_init (loc, "array index in non-array initializer");
11145 1 : return true;
11146 : }
11147 617 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11148 : {
11149 0 : error_init (loc, "field name not in record or union initializer");
11150 0 : return true;
11151 : }
11152 :
11153 617 : constructor_designated = 1;
11154 617 : finish_implicit_inits (loc, braced_init_obstack);
11155 617 : push_init_level (loc, 2, braced_init_obstack);
11156 617 : return false;
11157 : }
11158 :
11159 : /* If there are range designators in designator list, push a new designator
11160 : to constructor_range_stack. RANGE_END is end of such stack range or
11161 : NULL_TREE if there is no range designator at this level. */
11162 :
11163 : static void
11164 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11165 : {
11166 390 : struct constructor_range_stack *p;
11167 :
11168 780 : p = (struct constructor_range_stack *)
11169 390 : obstack_alloc (braced_init_obstack,
11170 : sizeof (struct constructor_range_stack));
11171 390 : p->prev = constructor_range_stack;
11172 390 : p->next = 0;
11173 390 : p->fields = constructor_fields;
11174 390 : p->range_start = constructor_index;
11175 390 : p->index = constructor_index;
11176 390 : p->stack = constructor_stack;
11177 390 : p->range_end = range_end;
11178 390 : if (constructor_range_stack)
11179 19 : constructor_range_stack->next = p;
11180 390 : constructor_range_stack = p;
11181 390 : }
11182 :
11183 : /* Within an array initializer, specify the next index to be initialized.
11184 : FIRST is that index. If LAST is nonzero, then initialize a range
11185 : of indices, running from FIRST through LAST. */
11186 :
11187 : void
11188 1149 : set_init_index (location_t loc, tree first, tree last,
11189 : struct obstack *braced_init_obstack)
11190 : {
11191 1149 : if (set_designator (loc, true, braced_init_obstack))
11192 : return;
11193 :
11194 1146 : designator_erroneous = 1;
11195 :
11196 2292 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11197 2281 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11198 : {
11199 12 : error_init (loc, "array index in initializer not of integer type");
11200 12 : return;
11201 : }
11202 :
11203 1134 : if (TREE_CODE (first) != INTEGER_CST)
11204 : {
11205 6 : first = c_fully_fold (first, false, NULL);
11206 6 : if (TREE_CODE (first) == INTEGER_CST)
11207 5 : pedwarn_init (loc, OPT_Wpedantic,
11208 : "array index in initializer is not "
11209 : "an integer constant expression");
11210 : }
11211 :
11212 1134 : if (last && TREE_CODE (last) != INTEGER_CST)
11213 : {
11214 2 : last = c_fully_fold (last, false, NULL);
11215 2 : if (TREE_CODE (last) == INTEGER_CST)
11216 1 : pedwarn_init (loc, OPT_Wpedantic,
11217 : "array index in initializer is not "
11218 : "an integer constant expression");
11219 : }
11220 :
11221 1134 : if (TREE_CODE (first) != INTEGER_CST)
11222 1 : error_init (loc, "nonconstant array index in initializer");
11223 1133 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11224 1 : error_init (loc, "nonconstant array index in initializer");
11225 1132 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11226 9 : error_init (loc, "array index in non-array initializer");
11227 1123 : else if (tree_int_cst_sgn (first) == -1)
11228 15 : error_init (loc, "array index in initializer exceeds array bounds");
11229 1108 : else if (constructor_max_index
11230 1108 : && tree_int_cst_lt (constructor_max_index, first))
11231 7 : error_init (loc, "array index in initializer exceeds array bounds");
11232 : else
11233 : {
11234 1101 : constant_expression_warning (first);
11235 1101 : if (last)
11236 385 : constant_expression_warning (last);
11237 1101 : constructor_index = convert (bitsizetype, first);
11238 1101 : if (tree_int_cst_lt (constructor_index, first))
11239 : {
11240 0 : constructor_index = copy_node (constructor_index);
11241 0 : TREE_OVERFLOW (constructor_index) = 1;
11242 : }
11243 :
11244 1101 : if (last)
11245 : {
11246 385 : if (tree_int_cst_equal (first, last))
11247 : last = NULL_TREE;
11248 384 : else if (tree_int_cst_lt (last, first))
11249 : {
11250 2 : error_init (loc, "empty index range in initializer");
11251 2 : last = NULL_TREE;
11252 : }
11253 : else
11254 : {
11255 382 : last = convert (bitsizetype, last);
11256 382 : if (constructor_max_index != NULL_TREE
11257 382 : && tree_int_cst_lt (constructor_max_index, last))
11258 : {
11259 3 : error_init (loc, "array index range in initializer exceeds "
11260 : "array bounds");
11261 3 : last = NULL_TREE;
11262 : }
11263 : }
11264 : }
11265 :
11266 1101 : designator_depth++;
11267 1101 : designator_erroneous = 0;
11268 1101 : if (constructor_range_stack || last)
11269 379 : push_range_stack (last, braced_init_obstack);
11270 : }
11271 : }
11272 :
11273 : /* Within a struct initializer, specify the next field to be initialized. */
11274 :
11275 : void
11276 32849 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11277 : struct obstack *braced_init_obstack)
11278 : {
11279 32849 : tree field;
11280 :
11281 32849 : if (set_designator (loc, false, braced_init_obstack))
11282 : return;
11283 :
11284 32832 : designator_erroneous = 1;
11285 :
11286 32832 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11287 : {
11288 3 : error_init (loc, "field name not in record or union initializer");
11289 3 : return;
11290 : }
11291 :
11292 32829 : field = lookup_field (constructor_type, fieldname);
11293 :
11294 32829 : if (field == NULL_TREE)
11295 : {
11296 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11297 8 : if (guessed_id)
11298 : {
11299 4 : gcc_rich_location rich_loc (fieldname_loc);
11300 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11301 4 : error_at (&rich_loc,
11302 : "%qT has no member named %qE; did you mean %qE?",
11303 : constructor_type, fieldname, guessed_id);
11304 4 : }
11305 : else
11306 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11307 : constructor_type, fieldname);
11308 : }
11309 : else
11310 32866 : do
11311 : {
11312 32866 : constructor_fields = TREE_VALUE (field);
11313 32866 : designator_depth++;
11314 32866 : designator_erroneous = 0;
11315 32866 : if (constructor_range_stack)
11316 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11317 32866 : field = TREE_CHAIN (field);
11318 32866 : if (field)
11319 : {
11320 45 : if (set_designator (loc, false, braced_init_obstack))
11321 : return;
11322 : }
11323 : }
11324 32866 : while (field != NULL_TREE);
11325 : }
11326 :
11327 : /* Helper function for add_pending_init. Find inorder successor of P
11328 : in AVL tree. */
11329 : static struct init_node *
11330 129 : init_node_successor (struct init_node *p)
11331 : {
11332 129 : struct init_node *r;
11333 129 : if (p->right)
11334 : {
11335 : r = p->right;
11336 58 : while (r->left)
11337 : r = r->left;
11338 : return r;
11339 : }
11340 75 : r = p->parent;
11341 114 : while (r && p == r->right)
11342 : {
11343 39 : p = r;
11344 39 : r = r->parent;
11345 : }
11346 : return r;
11347 : }
11348 :
11349 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11350 : identifies the initializer, either array index or field in a structure.
11351 : VALUE is the value of that index or field. If ORIGTYPE is not
11352 : NULL_TREE, it is the original type of VALUE.
11353 :
11354 : IMPLICIT is true if value comes from pop_init_level (1),
11355 : the new initializer has been merged with the existing one
11356 : and thus no warnings should be emitted about overriding an
11357 : existing initializer. */
11358 :
11359 : static void
11360 3269 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11361 : bool implicit, struct obstack *braced_init_obstack)
11362 : {
11363 3269 : struct init_node *p, **q, *r;
11364 :
11365 3269 : q = &constructor_pending_elts;
11366 3269 : p = 0;
11367 :
11368 3269 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11369 : {
11370 3610 : while (*q != 0)
11371 : {
11372 2412 : p = *q;
11373 2412 : if (tree_int_cst_lt (purpose, p->purpose))
11374 283 : q = &p->left;
11375 2129 : else if (tree_int_cst_lt (p->purpose, purpose))
11376 : {
11377 1960 : if (TREE_CODE (p->value) != RAW_DATA_CST
11378 1960 : || (p->right
11379 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11380 1830 : q = &p->right;
11381 : else
11382 : {
11383 130 : widest_int pp = wi::to_widest (p->purpose);
11384 130 : widest_int pw = wi::to_widest (purpose);
11385 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11386 98 : q = &p->right;
11387 : else
11388 : {
11389 : /* Override which should split the old RAW_DATA_CST
11390 : into 2 or 3 pieces. */
11391 32 : if (!implicit && warn_override_init)
11392 9 : warning_init (loc, OPT_Woverride_init,
11393 : "initialized field overwritten");
11394 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11395 32 : unsigned HOST_WIDE_INT len = 1;
11396 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11397 0 : len = RAW_DATA_LENGTH (value);
11398 32 : unsigned HOST_WIDE_INT end = 0;
11399 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11400 32 : gcc_checking_assert (start < plen && start);
11401 32 : if (plen - start > len)
11402 30 : end = plen - start - len;
11403 32 : tree v = p->value;
11404 32 : tree origtype = p->origtype;
11405 32 : if (start == 1)
11406 2 : p->value = build_int_cst (TREE_TYPE (v),
11407 2 : RAW_DATA_UCHAR_ELT (v, 0));
11408 : else
11409 : {
11410 30 : p->value = v;
11411 30 : if (end > 1)
11412 26 : v = copy_node (v);
11413 30 : RAW_DATA_LENGTH (p->value) = start;
11414 : }
11415 32 : if (end)
11416 : {
11417 30 : tree epurpose
11418 30 : = size_binop (PLUS_EXPR, purpose,
11419 : bitsize_int (len));
11420 30 : if (end > 1)
11421 : {
11422 28 : RAW_DATA_LENGTH (v) -= plen - end;
11423 28 : RAW_DATA_POINTER (v) += plen - end;
11424 : }
11425 : else
11426 2 : v = build_int_cst (TREE_TYPE (v),
11427 2 : RAW_DATA_UCHAR_ELT (v, plen
11428 : - end));
11429 30 : add_pending_init (loc, epurpose, v, origtype,
11430 : implicit, braced_init_obstack);
11431 : }
11432 32 : q = &constructor_pending_elts;
11433 32 : continue;
11434 32 : }
11435 130 : }
11436 : }
11437 : else
11438 : {
11439 169 : if (TREE_CODE (p->value) == RAW_DATA_CST
11440 179 : && (RAW_DATA_LENGTH (p->value)
11441 10 : > (TREE_CODE (value) == RAW_DATA_CST
11442 10 : ? RAW_DATA_LENGTH (value) : 1)))
11443 : {
11444 : /* Override which should split the old RAW_DATA_CST
11445 : into 2 pieces. */
11446 6 : if (!implicit && warn_override_init)
11447 3 : warning_init (loc, OPT_Woverride_init,
11448 : "initialized field overwritten");
11449 6 : unsigned HOST_WIDE_INT len = 1;
11450 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11451 2 : len = RAW_DATA_LENGTH (value);
11452 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11453 : {
11454 6 : RAW_DATA_LENGTH (p->value) -= len;
11455 6 : RAW_DATA_POINTER (p->value) += len;
11456 : }
11457 : else
11458 : {
11459 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11460 0 : p->value
11461 0 : = build_int_cst (TREE_TYPE (p->value),
11462 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11463 : }
11464 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11465 : bitsize_int (len));
11466 6 : continue;
11467 6 : }
11468 163 : if (TREE_CODE (value) == RAW_DATA_CST)
11469 : {
11470 8 : handle_raw_data:
11471 : /* RAW_DATA_CST value might overlap various further
11472 : prior initval entries. Find out how many. */
11473 13 : unsigned cnt = 0;
11474 13 : widest_int w
11475 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11476 13 : struct init_node *r = p, *last = NULL;
11477 13 : bool override_init = warn_override_init;
11478 13 : while ((r = init_node_successor (r))
11479 53 : && wi::to_widest (r->purpose) < w)
11480 : {
11481 40 : ++cnt;
11482 40 : if (TREE_SIDE_EFFECTS (r->value))
11483 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11484 : "initialized field with side-effects "
11485 : "overwritten");
11486 38 : else if (override_init)
11487 : {
11488 6 : warning_init (loc, OPT_Woverride_init,
11489 : "initialized field overwritten");
11490 6 : override_init = false;
11491 : }
11492 : last = r;
11493 : }
11494 13 : if (cnt)
11495 : {
11496 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11497 13 : && (wi::to_widest (last->purpose)
11498 13 : + RAW_DATA_LENGTH (last->value) > w))
11499 : {
11500 : /* The last overlapping prior initval overlaps
11501 : only partially. Shrink it and decrease cnt. */
11502 0 : unsigned int l = (wi::to_widest (last->purpose)
11503 0 : + RAW_DATA_LENGTH (last->value)
11504 0 : - w).to_uhwi ();
11505 0 : --cnt;
11506 0 : RAW_DATA_LENGTH (last->value) -= l;
11507 0 : RAW_DATA_POINTER (last->value) += l;
11508 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11509 0 : last->value
11510 0 : = build_int_cst (TREE_TYPE (last->value),
11511 0 : RAW_DATA_UCHAR_ELT (last->value,
11512 : 0));
11513 0 : last->purpose
11514 0 : = size_binop (PLUS_EXPR, last->purpose,
11515 : bitsize_int (l));
11516 : }
11517 : /* Instead of deleting cnt nodes from the AVL tree
11518 : and rebalancing, peel of last cnt bytes from the
11519 : RAW_DATA_CST. Overriding thousands of previously
11520 : initialized array elements with #embed needs to work,
11521 : but doesn't need to be super efficient. */
11522 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11523 : > cnt);
11524 13 : RAW_DATA_LENGTH (value) -= cnt;
11525 13 : const unsigned char *s
11526 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11527 13 : unsigned int o = RAW_DATA_LENGTH (value);
11528 53 : for (r = p; cnt--; ++o, ++s)
11529 : {
11530 40 : r = init_node_successor (r);
11531 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11532 : bitsize_int (o));
11533 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11534 40 : r->origtype = origtype;
11535 : }
11536 13 : if (RAW_DATA_LENGTH (value) == 1)
11537 0 : value = build_int_cst (TREE_TYPE (value),
11538 0 : RAW_DATA_UCHAR_ELT (value, 0));
11539 : }
11540 : }
11541 168 : if (!implicit)
11542 : {
11543 54 : if (TREE_SIDE_EFFECTS (p->value))
11544 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11545 : "initialized field with side-effects "
11546 : "overwritten");
11547 48 : else if (warn_override_init)
11548 13 : warning_init (loc, OPT_Woverride_init,
11549 : "initialized field overwritten");
11550 : }
11551 168 : p->value = value;
11552 168 : p->origtype = origtype;
11553 168 : return;
11554 : }
11555 : }
11556 1198 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11557 : {
11558 61 : struct init_node *r;
11559 61 : if (q == &p->left)
11560 : r = p;
11561 : else
11562 36 : r = init_node_successor (p);
11563 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11564 131 : + RAW_DATA_LENGTH (value)))
11565 : {
11566 : /* Overlap with at least one prior initval in the range but
11567 : not at the start. */
11568 5 : p = r;
11569 5 : p->purpose = purpose;
11570 5 : goto handle_raw_data;
11571 : }
11572 : }
11573 : }
11574 : else
11575 : {
11576 1908 : tree bitpos;
11577 :
11578 1908 : bitpos = bit_position (purpose);
11579 5430 : while (*q != NULL)
11580 : {
11581 1871 : p = *q;
11582 1871 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11583 43 : q = &p->left;
11584 1828 : else if (p->purpose != purpose)
11585 1571 : q = &p->right;
11586 : else
11587 : {
11588 257 : if (!implicit)
11589 : {
11590 33 : if (TREE_SIDE_EFFECTS (p->value))
11591 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11592 : "initialized field with side-effects "
11593 : "overwritten");
11594 29 : else if (warn_override_init)
11595 6 : warning_init (loc, OPT_Woverride_init,
11596 : "initialized field overwritten");
11597 : }
11598 257 : p->value = value;
11599 257 : p->origtype = origtype;
11600 257 : return;
11601 : }
11602 : }
11603 : }
11604 :
11605 2844 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11606 : sizeof (struct init_node));
11607 2844 : r->purpose = purpose;
11608 2844 : r->value = value;
11609 2844 : r->origtype = origtype;
11610 :
11611 2844 : *q = r;
11612 2844 : r->parent = p;
11613 2844 : r->left = 0;
11614 2844 : r->right = 0;
11615 2844 : r->balance = 0;
11616 :
11617 4878 : while (p)
11618 : {
11619 2826 : struct init_node *s;
11620 :
11621 2826 : if (r == p->left)
11622 : {
11623 237 : if (p->balance == 0)
11624 174 : p->balance = -1;
11625 63 : else if (p->balance < 0)
11626 : {
11627 45 : if (r->balance < 0)
11628 : {
11629 : /* L rotation. */
11630 40 : p->left = r->right;
11631 40 : if (p->left)
11632 2 : p->left->parent = p;
11633 40 : r->right = p;
11634 :
11635 40 : p->balance = 0;
11636 40 : r->balance = 0;
11637 :
11638 40 : s = p->parent;
11639 40 : p->parent = r;
11640 40 : r->parent = s;
11641 40 : if (s)
11642 : {
11643 31 : if (s->left == p)
11644 10 : s->left = r;
11645 : else
11646 21 : s->right = r;
11647 : }
11648 : else
11649 9 : constructor_pending_elts = r;
11650 : }
11651 : else
11652 : {
11653 : /* LR rotation. */
11654 5 : struct init_node *t = r->right;
11655 :
11656 5 : r->right = t->left;
11657 5 : if (r->right)
11658 0 : r->right->parent = r;
11659 5 : t->left = r;
11660 :
11661 5 : p->left = t->right;
11662 5 : if (p->left)
11663 0 : p->left->parent = p;
11664 5 : t->right = p;
11665 :
11666 5 : p->balance = t->balance < 0;
11667 5 : r->balance = -(t->balance > 0);
11668 5 : t->balance = 0;
11669 :
11670 5 : s = p->parent;
11671 5 : p->parent = t;
11672 5 : r->parent = t;
11673 5 : t->parent = s;
11674 5 : if (s)
11675 : {
11676 0 : if (s->left == p)
11677 0 : s->left = t;
11678 : else
11679 0 : s->right = t;
11680 : }
11681 : else
11682 5 : constructor_pending_elts = t;
11683 : }
11684 : break;
11685 : }
11686 : else
11687 : {
11688 : /* p->balance == +1; growth of left side balances the node. */
11689 18 : p->balance = 0;
11690 18 : break;
11691 : }
11692 : }
11693 : else /* r == p->right */
11694 : {
11695 2589 : if (p->balance == 0)
11696 : /* Growth propagation from right side. */
11697 1860 : p->balance++;
11698 729 : else if (p->balance > 0)
11699 : {
11700 700 : if (r->balance > 0)
11701 : {
11702 : /* R rotation. */
11703 698 : p->right = r->left;
11704 698 : if (p->right)
11705 173 : p->right->parent = p;
11706 698 : r->left = p;
11707 :
11708 698 : p->balance = 0;
11709 698 : r->balance = 0;
11710 :
11711 698 : s = p->parent;
11712 698 : p->parent = r;
11713 698 : r->parent = s;
11714 698 : if (s)
11715 : {
11716 378 : if (s->left == p)
11717 2 : s->left = r;
11718 : else
11719 376 : s->right = r;
11720 : }
11721 : else
11722 320 : constructor_pending_elts = r;
11723 : }
11724 : else /* r->balance == -1 */
11725 : {
11726 : /* RL rotation */
11727 2 : struct init_node *t = r->left;
11728 :
11729 2 : r->left = t->right;
11730 2 : if (r->left)
11731 2 : r->left->parent = r;
11732 2 : t->right = r;
11733 :
11734 2 : p->right = t->left;
11735 2 : if (p->right)
11736 2 : p->right->parent = p;
11737 2 : t->left = p;
11738 :
11739 2 : r->balance = (t->balance < 0);
11740 2 : p->balance = -(t->balance > 0);
11741 2 : t->balance = 0;
11742 :
11743 2 : s = p->parent;
11744 2 : p->parent = t;
11745 2 : r->parent = t;
11746 2 : t->parent = s;
11747 2 : if (s)
11748 : {
11749 0 : if (s->left == p)
11750 0 : s->left = t;
11751 : else
11752 0 : s->right = t;
11753 : }
11754 : else
11755 2 : constructor_pending_elts = t;
11756 : }
11757 : break;
11758 : }
11759 : else
11760 : {
11761 : /* p->balance == -1; growth of right side balances the node. */
11762 29 : p->balance = 0;
11763 29 : break;
11764 : }
11765 : }
11766 :
11767 2034 : r = p;
11768 2034 : p = p->parent;
11769 : }
11770 : }
11771 :
11772 : /* Build AVL tree from a sorted chain. */
11773 :
11774 : static void
11775 418 : set_nonincremental_init (struct obstack * braced_init_obstack)
11776 : {
11777 418 : unsigned HOST_WIDE_INT ix;
11778 418 : tree index, value;
11779 :
11780 418 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11781 418 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11782 : return;
11783 :
11784 1601 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11785 1183 : add_pending_init (input_location, index, value, NULL_TREE, true,
11786 : braced_init_obstack);
11787 418 : constructor_elements = NULL;
11788 418 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11789 : {
11790 200 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11791 : /* Skip any nameless bit fields at the beginning. */
11792 200 : while (constructor_unfilled_fields != NULL_TREE
11793 200 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11794 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11795 :
11796 : }
11797 218 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11798 : {
11799 218 : if (TYPE_DOMAIN (constructor_type))
11800 160 : constructor_unfilled_index
11801 160 : = convert (bitsizetype,
11802 160 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11803 : else
11804 58 : constructor_unfilled_index = bitsize_zero_node;
11805 : }
11806 418 : constructor_incremental = 0;
11807 : }
11808 :
11809 : /* Build AVL tree from a string constant. */
11810 :
11811 : static void
11812 7 : set_nonincremental_init_from_string (tree str,
11813 : struct obstack * braced_init_obstack)
11814 : {
11815 7 : tree value, purpose, type;
11816 7 : HOST_WIDE_INT val[2];
11817 7 : const char *p, *end;
11818 7 : int byte, wchar_bytes, charwidth, bitpos;
11819 :
11820 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11821 :
11822 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11823 7 : charwidth = TYPE_PRECISION (char_type_node);
11824 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11825 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11826 7 : type = TREE_TYPE (constructor_type);
11827 7 : p = TREE_STRING_POINTER (str);
11828 7 : end = p + TREE_STRING_LENGTH (str);
11829 :
11830 7 : for (purpose = bitsize_zero_node;
11831 : p < end
11832 52 : && !(constructor_max_index
11833 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11834 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11835 : {
11836 25 : if (wchar_bytes == 1)
11837 : {
11838 9 : val[0] = (unsigned char) *p++;
11839 9 : val[1] = 0;
11840 : }
11841 : else
11842 : {
11843 16 : val[1] = 0;
11844 16 : val[0] = 0;
11845 64 : for (byte = 0; byte < wchar_bytes; byte++)
11846 : {
11847 48 : if (BYTES_BIG_ENDIAN)
11848 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11849 : else
11850 48 : bitpos = byte * charwidth;
11851 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11852 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11853 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11854 : }
11855 : }
11856 :
11857 25 : if (!TYPE_UNSIGNED (type))
11858 : {
11859 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11860 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11861 : {
11862 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11863 : {
11864 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11865 0 : val[1] = -1;
11866 : }
11867 : }
11868 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11869 : {
11870 0 : if (val[0] < 0)
11871 0 : val[1] = -1;
11872 : }
11873 0 : else if (val[1] & (HOST_WIDE_INT_1
11874 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11875 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11876 : }
11877 :
11878 25 : value = wide_int_to_tree (type,
11879 25 : wide_int::from_array (val, 2,
11880 : HOST_BITS_PER_WIDE_INT * 2));
11881 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11882 : braced_init_obstack);
11883 : }
11884 :
11885 7 : constructor_incremental = 0;
11886 7 : }
11887 :
11888 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11889 : not initialized yet. */
11890 :
11891 : static tree
11892 703622 : find_init_member (tree field, struct obstack * braced_init_obstack)
11893 : {
11894 703622 : struct init_node *p;
11895 :
11896 703622 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11897 : {
11898 702824 : if (constructor_incremental
11899 702824 : && tree_int_cst_lt (field, constructor_unfilled_index))
11900 16 : set_nonincremental_init (braced_init_obstack);
11901 :
11902 702824 : p = constructor_pending_elts;
11903 702947 : while (p)
11904 : {
11905 233 : if (tree_int_cst_lt (field, p->purpose))
11906 25 : p = p->left;
11907 208 : else if (tree_int_cst_lt (p->purpose, field))
11908 98 : p = p->right;
11909 : else
11910 110 : return p->value;
11911 : }
11912 : }
11913 798 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11914 : {
11915 675 : tree bitpos = bit_position (field);
11916 :
11917 675 : if (constructor_incremental
11918 675 : && (!constructor_unfilled_fields
11919 472 : || tree_int_cst_lt (bitpos,
11920 472 : bit_position (constructor_unfilled_fields))))
11921 51 : set_nonincremental_init (braced_init_obstack);
11922 :
11923 675 : p = constructor_pending_elts;
11924 815 : while (p)
11925 : {
11926 347 : if (field == p->purpose)
11927 207 : return p->value;
11928 140 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11929 7 : p = p->left;
11930 : else
11931 133 : p = p->right;
11932 : }
11933 : }
11934 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11935 : {
11936 123 : if (!vec_safe_is_empty (constructor_elements)
11937 50 : && (constructor_elements->last ().index == field))
11938 50 : return constructor_elements->last ().value;
11939 : }
11940 : return NULL_TREE;
11941 : }
11942 :
11943 : /* "Output" the next constructor element.
11944 : At top level, really output it to assembler code now.
11945 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11946 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11947 : TYPE is the data type that the containing data type wants here.
11948 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11949 : If VALUE is a string constant, STRICT_STRING is true if it is
11950 : unparenthesized or we should not warn here for it being parenthesized.
11951 : For other types of VALUE, STRICT_STRING is not used.
11952 :
11953 : PENDING if true means output pending elements that belong
11954 : right after this element. (PENDING is normally true;
11955 : it is false while outputting pending elements, to avoid recursion.)
11956 :
11957 : IMPLICIT is true if value comes from pop_init_level (1),
11958 : the new initializer has been merged with the existing one
11959 : and thus no warnings should be emitted about overriding an
11960 : existing initializer. */
11961 :
11962 : static void
11963 9621790 : output_init_element (location_t loc, tree value, tree origtype,
11964 : bool strict_string, tree type, tree field, bool pending,
11965 : bool implicit, struct obstack * braced_init_obstack)
11966 : {
11967 9621790 : tree semantic_type = NULL_TREE;
11968 9621790 : bool maybe_const = true;
11969 9621790 : bool npc, int_const_expr, arith_const_expr;
11970 :
11971 9621790 : if (type == error_mark_node || value == error_mark_node)
11972 : {
11973 244 : constructor_erroneous = 1;
11974 2369 : return;
11975 : }
11976 9621546 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11977 200080 : && (TREE_CODE (value) == STRING_CST
11978 158670 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11979 41418 : && !(TREE_CODE (value) == STRING_CST
11980 41410 : && TREE_CODE (type) == ARRAY_TYPE
11981 3898 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11982 9659066 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11983 37520 : TYPE_MAIN_VARIANT (type)))
11984 37520 : value = array_to_pointer_conversion (input_location, value);
11985 :
11986 9621546 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11987 182 : && require_constant_value && pending)
11988 : {
11989 : /* As an extension, allow initializing objects with static storage
11990 : duration with compound literals (which are then treated just as
11991 : the brace enclosed list they contain). */
11992 114 : if (flag_isoc99)
11993 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
11994 : "constant");
11995 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
11996 114 : value = DECL_INITIAL (decl);
11997 : }
11998 :
11999 9621546 : npc = null_pointer_constant_p (value);
12000 19243092 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12001 4219223 : && !TREE_OVERFLOW (value)
12002 13840768 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12003 : /* Not fully determined before folding. */
12004 9621546 : arith_const_expr = true;
12005 9621546 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12006 : {
12007 16 : semantic_type = TREE_TYPE (value);
12008 16 : value = TREE_OPERAND (value, 0);
12009 : }
12010 9621546 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12011 : /* TODO: this may not detect all cases of expressions folding to
12012 : constants that are not arithmetic constant expressions. */
12013 9621546 : if (!maybe_const)
12014 : arith_const_expr = false;
12015 19240535 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12016 3184761 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12017 11462359 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12018 : arith_const_expr = false;
12019 7793491 : else if (TREE_CODE (value) != INTEGER_CST
12020 : && TREE_CODE (value) != REAL_CST
12021 : && TREE_CODE (value) != COMPLEX_CST
12022 : && TREE_CODE (value) != RAW_DATA_CST)
12023 : arith_const_expr = false;
12024 4608252 : else if (TREE_OVERFLOW (value))
12025 5013295 : arith_const_expr = false;
12026 :
12027 9621546 : if (value == error_mark_node)
12028 0 : constructor_erroneous = 1;
12029 9621546 : else if (!TREE_CONSTANT (value))
12030 3205508 : constructor_constant = 0;
12031 6416038 : else if (!initializer_constant_valid_p (value,
12032 6416038 : TREE_TYPE (value),
12033 6416038 : AGGREGATE_TYPE_P (constructor_type)
12034 6416038 : && TYPE_REVERSE_STORAGE_ORDER
12035 : (constructor_type))
12036 6416038 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12037 1037866 : && DECL_C_BIT_FIELD (field)
12038 8103 : && TREE_CODE (value) != INTEGER_CST))
12039 77 : constructor_simple = 0;
12040 9621546 : if (!maybe_const)
12041 1172 : constructor_nonconst = 1;
12042 :
12043 : /* Digest the initializer and issue any errors about incompatible
12044 : types before issuing errors about non-constant initializers. */
12045 9621546 : tree new_value = value;
12046 9621546 : if (semantic_type)
12047 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12048 : /* In the case of braces around a scalar initializer, the result of
12049 : this initializer processing goes through digest_init again at the
12050 : outer level. In the case of a constexpr initializer for a
12051 : pointer, avoid converting a null pointer constant to something
12052 : that is not a null pointer constant to avoid a spurious error
12053 : from that second processing. */
12054 9621546 : if (!require_constexpr_value
12055 398 : || !npc
12056 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12057 9621535 : new_value = digest_init (loc,
12058 9621535 : RECORD_OR_UNION_TYPE_P (constructor_type)
12059 8551493 : ? field : constructor_fields
12060 8551493 : ? constructor_fields : constructor_decl,
12061 : type, new_value, origtype, npc,
12062 : int_const_expr, arith_const_expr, strict_string,
12063 : require_constant_value, require_constexpr_value);
12064 9621546 : if (new_value == error_mark_node)
12065 : {
12066 76 : constructor_erroneous = 1;
12067 76 : return;
12068 : }
12069 9621470 : if (require_constant_value || require_constant_elements)
12070 2715457 : constant_expression_warning (new_value);
12071 :
12072 : /* Proceed to check the constness of the original initializer. */
12073 9621470 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12074 : {
12075 3205534 : if (require_constant_value)
12076 : {
12077 0 : error_init (loc, "initializer element is not constant");
12078 0 : value = error_mark_node;
12079 : }
12080 3205534 : else if (require_constant_elements)
12081 15 : pedwarn (loc, OPT_Wpedantic,
12082 : "initializer element is not computable at load time");
12083 : }
12084 6415936 : else if (!maybe_const
12085 54 : && (require_constant_value || require_constant_elements))
12086 28 : pedwarn_init (loc, OPT_Wpedantic,
12087 : "initializer element is not a constant expression");
12088 : /* digest_init has already carried out the additional checks
12089 : required for 'constexpr' initializers (using the information
12090 : passed to it about whether the original initializer was certain
12091 : kinds of constant expression), so that check does not need to be
12092 : repeated here. */
12093 :
12094 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12095 : enum type. */
12096 9621470 : if (warn_cxx_compat
12097 6895 : && field != NULL_TREE
12098 6895 : && TREE_CODE (field) == FIELD_DECL
12099 459 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12100 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12101 21 : != TYPE_MAIN_VARIANT (type))
12102 9621491 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12103 : {
12104 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12105 21 : if (checktype != error_mark_node
12106 21 : && (TYPE_MAIN_VARIANT (checktype)
12107 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12108 9 : warning_init (loc, OPT_Wc___compat,
12109 : "enum conversion in initialization is invalid in C++");
12110 : }
12111 :
12112 : /* If this field is empty and does not have side effects (and is not at
12113 : the end of structure), don't do anything other than checking the
12114 : initializer. */
12115 9621470 : if (field
12116 9621470 : && (TREE_TYPE (field) == error_mark_node
12117 9621035 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12118 9620726 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12119 62 : && !TREE_SIDE_EFFECTS (new_value)
12120 33 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12121 33 : || DECL_CHAIN (field)))))
12122 18 : return;
12123 :
12124 : /* Finally, set VALUE to the initializer value digested above. */
12125 9621452 : value = new_value;
12126 :
12127 : /* If this element doesn't come next in sequence,
12128 : put it on constructor_pending_elts. */
12129 9621452 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12130 9621452 : && (!constructor_incremental
12131 1719991 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12132 1719625 : || (TREE_CODE (value) == RAW_DATA_CST
12133 240 : && constructor_pending_elts
12134 66 : && pending)))
12135 : {
12136 627 : if (constructor_incremental
12137 627 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12138 337 : || (TREE_CODE (value) == RAW_DATA_CST
12139 14 : && constructor_pending_elts
12140 14 : && pending)))
12141 48 : set_nonincremental_init (braced_init_obstack);
12142 :
12143 627 : add_pending_init (loc, field, value, origtype, implicit,
12144 : braced_init_obstack);
12145 627 : return;
12146 : }
12147 9620825 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12148 1038921 : && (!constructor_incremental
12149 1038581 : || field != constructor_unfilled_fields))
12150 : {
12151 : /* We do this for records but not for unions. In a union,
12152 : no matter which field is specified, it can be initialized
12153 : right away since it starts at the beginning of the union. */
12154 1404 : if (constructor_incremental)
12155 : {
12156 1064 : if (!constructor_unfilled_fields)
12157 5 : set_nonincremental_init (braced_init_obstack);
12158 : else
12159 : {
12160 1059 : tree bitpos, unfillpos;
12161 :
12162 1059 : bitpos = bit_position (field);
12163 1059 : unfillpos = bit_position (constructor_unfilled_fields);
12164 :
12165 1059 : if (tree_int_cst_lt (bitpos, unfillpos))
12166 4 : set_nonincremental_init (braced_init_obstack);
12167 : }
12168 : }
12169 :
12170 1404 : add_pending_init (loc, field, value, origtype, implicit,
12171 : braced_init_obstack);
12172 1404 : return;
12173 : }
12174 9619421 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12175 9619421 : && !vec_safe_is_empty (constructor_elements))
12176 : {
12177 62 : if (!implicit)
12178 : {
12179 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12180 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12181 : "initialized field with side-effects overwritten");
12182 9 : else if (warn_override_init)
12183 6 : warning_init (loc, OPT_Woverride_init,
12184 : "initialized field overwritten");
12185 : }
12186 :
12187 : /* We can have just one union field set. */
12188 62 : constructor_elements = NULL;
12189 : }
12190 :
12191 : /* Otherwise, output this element either to
12192 : constructor_elements or to the assembler file. */
12193 :
12194 9619421 : constructor_elt celt = {field, value};
12195 9619421 : vec_safe_push (constructor_elements, celt);
12196 :
12197 : /* Advance the variable that indicates sequential elements output. */
12198 9619421 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12199 : {
12200 1719620 : tree inc = bitsize_one_node;
12201 1719620 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12202 235 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12203 1719620 : constructor_unfilled_index
12204 1719620 : = size_binop_loc (input_location, PLUS_EXPR,
12205 : constructor_unfilled_index, inc);
12206 : }
12207 7899801 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12208 : {
12209 1037517 : constructor_unfilled_fields
12210 1037517 : = DECL_CHAIN (constructor_unfilled_fields);
12211 :
12212 : /* Skip any nameless bit fields. */
12213 1037517 : while (constructor_unfilled_fields != NULL_TREE
12214 1037618 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12215 101 : constructor_unfilled_fields
12216 101 : = DECL_CHAIN (constructor_unfilled_fields);
12217 : }
12218 6862284 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12219 31089 : constructor_unfilled_fields = NULL_TREE;
12220 :
12221 : /* Now output any pending elements which have become next. */
12222 9619421 : if (pending)
12223 9616556 : output_pending_init_elements (0, braced_init_obstack);
12224 : }
12225 :
12226 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12227 : comes before field2, 1 if field1 comes after field2 and
12228 : 0 if field1 == field2. */
12229 :
12230 : static int
12231 4029 : init_field_decl_cmp (tree field1, tree field2)
12232 : {
12233 4029 : if (field1 == field2)
12234 : return 0;
12235 :
12236 2154 : tree bitpos1 = bit_position (field1);
12237 2154 : tree bitpos2 = bit_position (field2);
12238 2154 : if (tree_int_cst_equal (bitpos1, bitpos2))
12239 : {
12240 : /* If one of the fields has non-zero bitsize, then that
12241 : field must be the last one in a sequence of zero
12242 : sized fields, fields after it will have bigger
12243 : bit_position. */
12244 22 : if (TREE_TYPE (field1) != error_mark_node
12245 22 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12246 44 : && integer_nonzerop (TREE_TYPE (field1)))
12247 : return 1;
12248 22 : if (TREE_TYPE (field2) != error_mark_node
12249 22 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12250 37 : && integer_nonzerop (TREE_TYPE (field2)))
12251 : return -1;
12252 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12253 : which field comes earlier. Walk chains of both
12254 : fields, so that if field1 and field2 are close to each
12255 : other in either order, it is found soon even for large
12256 : sequences of zero sized fields. */
12257 : tree f1 = field1, f2 = field2;
12258 22 : while (1)
12259 : {
12260 22 : f1 = DECL_CHAIN (f1);
12261 22 : f2 = DECL_CHAIN (f2);
12262 22 : if (f1 == NULL_TREE)
12263 : {
12264 7 : gcc_assert (f2);
12265 : return 1;
12266 : }
12267 15 : if (f2 == NULL_TREE)
12268 : return -1;
12269 1 : if (f1 == field2)
12270 : return -1;
12271 0 : if (f2 == field1)
12272 : return 1;
12273 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12274 : return 1;
12275 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12276 : return -1;
12277 : }
12278 : }
12279 2132 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12280 : return -1;
12281 : else
12282 : return 1;
12283 : }
12284 :
12285 : /* Output any pending elements which have become next.
12286 : As we output elements, constructor_unfilled_{fields,index}
12287 : advances, which may cause other elements to become next;
12288 : if so, they too are output.
12289 :
12290 : If ALL is 0, we return when there are
12291 : no more pending elements to output now.
12292 :
12293 : If ALL is 1, we output space as necessary so that
12294 : we can output all the pending elements. */
12295 : static void
12296 11684450 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12297 : {
12298 11684450 : struct init_node *elt = constructor_pending_elts;
12299 11685450 : tree next;
12300 :
12301 11685450 : retry:
12302 :
12303 : /* Look through the whole pending tree.
12304 : If we find an element that should be output now,
12305 : output it. Otherwise, set NEXT to the element
12306 : that comes first among those still pending. */
12307 :
12308 11685450 : next = NULL_TREE;
12309 11691100 : while (elt)
12310 : {
12311 7598 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12312 : {
12313 3004 : if (tree_int_cst_equal (elt->purpose,
12314 : constructor_unfilled_index))
12315 1197 : output_init_element (input_location, elt->value, elt->origtype,
12316 1197 : true, TREE_TYPE (constructor_type),
12317 : constructor_unfilled_index, false, false,
12318 : braced_init_obstack);
12319 1807 : else if (tree_int_cst_lt (constructor_unfilled_index,
12320 1807 : elt->purpose))
12321 : {
12322 : /* Advance to the next smaller node. */
12323 589 : if (elt->left)
12324 : elt = elt->left;
12325 : else
12326 : {
12327 : /* We have reached the smallest node bigger than the
12328 : current unfilled index. Fill the space first. */
12329 209 : next = elt->purpose;
12330 209 : break;
12331 : }
12332 : }
12333 : else
12334 : {
12335 : /* Advance to the next bigger node. */
12336 1218 : if (elt->right)
12337 : elt = elt->right;
12338 : else
12339 : {
12340 : /* We have reached the biggest node in a subtree. Find
12341 : the parent of it, which is the next bigger node. */
12342 1218 : while (elt->parent && elt->parent->right == elt)
12343 : elt = elt->parent;
12344 774 : elt = elt->parent;
12345 1149 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12346 375 : elt->purpose))
12347 : {
12348 22 : next = elt->purpose;
12349 22 : break;
12350 : }
12351 : }
12352 : }
12353 : }
12354 4594 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12355 : {
12356 : /* If the current record is complete we are done. */
12357 4594 : if (constructor_unfilled_fields == NULL_TREE)
12358 : break;
12359 :
12360 3755 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12361 : elt->purpose);
12362 3755 : if (cmp == 0)
12363 1668 : output_init_element (input_location, elt->value, elt->origtype,
12364 1668 : true, TREE_TYPE (elt->purpose),
12365 : elt->purpose, false, false,
12366 : braced_init_obstack);
12367 2087 : else if (cmp < 0)
12368 : {
12369 : /* Advance to the next smaller node. */
12370 1092 : if (elt->left)
12371 : elt = elt->left;
12372 : else
12373 : {
12374 : /* We have reached the smallest node bigger than the
12375 : current unfilled field. Fill the space first. */
12376 811 : next = elt->purpose;
12377 811 : break;
12378 : }
12379 : }
12380 : else
12381 : {
12382 : /* Advance to the next bigger node. */
12383 995 : if (elt->right)
12384 : elt = elt->right;
12385 : else
12386 : {
12387 : /* We have reached the biggest node in a subtree. Find
12388 : the parent of it, which is the next bigger node. */
12389 735 : while (elt->parent && elt->parent->right == elt)
12390 : elt = elt->parent;
12391 519 : elt = elt->parent;
12392 519 : if (elt
12393 519 : && init_field_decl_cmp (constructor_unfilled_fields,
12394 : elt->purpose) < 0)
12395 : {
12396 67 : next = elt->purpose;
12397 67 : break;
12398 : }
12399 : }
12400 : }
12401 : }
12402 : }
12403 :
12404 : /* Ordinarily return, but not if we want to output all
12405 : and there are elements left. */
12406 11685450 : if (!(all && next != NULL_TREE))
12407 11684450 : return;
12408 :
12409 : /* If it's not incremental, just skip over the gap, so that after
12410 : jumping to retry we will output the next successive element. */
12411 1000 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12412 808 : constructor_unfilled_fields = next;
12413 192 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12414 192 : constructor_unfilled_index = next;
12415 :
12416 : /* ELT now points to the node in the pending tree with the next
12417 : initializer to output. */
12418 1000 : goto retry;
12419 : }
12420 :
12421 : /* Expression VALUE coincides with the start of type TYPE in a braced
12422 : initializer. Return true if we should treat VALUE as initializing
12423 : the first element of TYPE, false if we should treat it as initializing
12424 : TYPE as a whole.
12425 :
12426 : If the initializer is clearly invalid, the question becomes:
12427 : which choice gives the best error message? */
12428 :
12429 : static bool
12430 3486652 : initialize_elementwise_p (tree type, tree value)
12431 : {
12432 3486652 : if (type == error_mark_node || value == error_mark_node)
12433 : return false;
12434 :
12435 3486429 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12436 :
12437 3486429 : tree value_type = TREE_TYPE (value);
12438 3486429 : if (value_type == error_mark_node)
12439 : return false;
12440 :
12441 : /* GNU vectors can be initialized elementwise. However, treat any
12442 : kind of vector value as initializing the vector type as a whole,
12443 : regardless of whether the value is a GNU vector. Such initializers
12444 : are valid if and only if they would have been valid in a non-braced
12445 : initializer like:
12446 :
12447 : TYPE foo = VALUE;
12448 :
12449 : so recursing into the vector type would be at best confusing or at
12450 : worst wrong. For example, when -flax-vector-conversions is in effect,
12451 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12452 : have different element types and different numbers of elements. */
12453 3486429 : if (gnu_vector_type_p (type))
12454 19523 : return !VECTOR_TYPE_P (value_type);
12455 :
12456 3466906 : if (AGGREGATE_TYPE_P (type))
12457 1741351 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12458 :
12459 : return false;
12460 : }
12461 :
12462 : /* Helper function for process_init_element. Split first element of
12463 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12464 :
12465 : static inline tree
12466 9618800 : maybe_split_raw_data (tree value, tree *raw_data)
12467 : {
12468 9618800 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12469 : return value;
12470 211 : *raw_data = value;
12471 211 : value = build_int_cst (integer_type_node,
12472 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12473 211 : ++RAW_DATA_POINTER (*raw_data);
12474 211 : --RAW_DATA_LENGTH (*raw_data);
12475 211 : return value;
12476 : }
12477 :
12478 : /* Return non-zero if c_parser_initval should attempt to optimize
12479 : large initializers into RAW_DATA_CST. In that case return how
12480 : many elements to optimize at most. */
12481 :
12482 : unsigned
12483 2991671 : c_maybe_optimize_large_byte_initializer (void)
12484 : {
12485 2991671 : if (!constructor_type
12486 2991601 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12487 210983 : || constructor_stack->implicit)
12488 : return 0;
12489 208218 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12490 208218 : if (TREE_CODE (elttype) != INTEGER_TYPE
12491 208218 : && TREE_CODE (elttype) != BITINT_TYPE)
12492 : return 0;
12493 185605 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12494 27989 : || constructor_stack->replacement_value.value
12495 27989 : || (COMPLETE_TYPE_P (constructor_type)
12496 26352 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12497 213594 : || constructor_range_stack)
12498 : return 0;
12499 27989 : if (constructor_max_index == NULL_TREE)
12500 : return INT_MAX;
12501 26352 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12502 26352 : || integer_all_onesp (constructor_max_index))
12503 4773 : return 0;
12504 21579 : widest_int w = wi::to_widest (constructor_max_index);
12505 21579 : w -= wi::to_widest (constructor_index);
12506 21579 : w += 1;
12507 21579 : if (w < 64)
12508 : return 0;
12509 6895 : if (w > INT_MAX)
12510 : return INT_MAX;
12511 6895 : return w.to_uhwi ();
12512 21579 : }
12513 :
12514 : /* Add one non-braced element to the current constructor level.
12515 : This adjusts the current position within the constructor's type.
12516 : This may also start or terminate implicit levels
12517 : to handle a partly-braced initializer.
12518 :
12519 : Once this has found the correct level for the new element,
12520 : it calls output_init_element.
12521 :
12522 : IMPLICIT is true if value comes from pop_init_level (1),
12523 : the new initializer has been merged with the existing one
12524 : and thus no warnings should be emitted about overriding an
12525 : existing initializer. */
12526 :
12527 : void
12528 9609099 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12529 : struct obstack * braced_init_obstack)
12530 : {
12531 9609099 : tree orig_value = value.value;
12532 19218198 : int string_flag
12533 9609099 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12534 9609099 : bool strict_string = value.original_code == STRING_CST;
12535 9609099 : bool was_designated = designator_depth != 0;
12536 9609099 : tree raw_data = NULL_TREE;
12537 :
12538 9609311 : retry:
12539 9609311 : designator_depth = 0;
12540 9609311 : designator_erroneous = 0;
12541 :
12542 9609311 : if (!implicit && value.value && !integer_zerop (value.value))
12543 7587064 : constructor_zeroinit = 0;
12544 :
12545 : /* Handle superfluous braces around string cst as in
12546 : char x[] = {"foo"}; */
12547 9609311 : if (constructor_type
12548 9609183 : && !was_designated
12549 9575215 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12550 1707237 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12551 10347370 : && integer_zerop (constructor_unfilled_index))
12552 : {
12553 163069 : if (constructor_stack->replacement_value.value)
12554 : {
12555 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12556 409 : return;
12557 : }
12558 163061 : else if (string_flag)
12559 : {
12560 138 : constructor_stack->replacement_value = value;
12561 138 : return;
12562 : }
12563 : }
12564 :
12565 9609165 : if (constructor_stack->replacement_value.value != NULL_TREE)
12566 : {
12567 0 : error_init (loc, "excess elements in struct initializer");
12568 0 : return;
12569 : }
12570 :
12571 : /* Ignore elements of a brace group if it is entirely superfluous
12572 : and has already been diagnosed, or if the type is erroneous. */
12573 9609165 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12574 : return;
12575 :
12576 : /* Ignore elements of an initializer for a variable-size type.
12577 : Those are diagnosed in the parser (empty initializer braces are OK). */
12578 9608968 : if (COMPLETE_TYPE_P (constructor_type)
12579 9608968 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12580 : return;
12581 :
12582 8905288 : if (!implicit && warn_designated_init && !was_designated
12583 8871937 : && TREE_CODE (constructor_type) == RECORD_TYPE
12584 10643754 : && lookup_attribute ("designated_init",
12585 1034844 : TYPE_ATTRIBUTES (constructor_type)))
12586 50 : warning_init (loc,
12587 : OPT_Wdesignated_init,
12588 : "positional initialization of field "
12589 : "in %<struct%> declared with %<designated_init%> attribute");
12590 :
12591 : /* If we've exhausted any levels that didn't have braces,
12592 : pop them now. */
12593 10310889 : while (constructor_stack->implicit)
12594 : {
12595 706849 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12596 702792 : && constructor_fields == NULL_TREE)
12597 701738 : process_init_element (loc,
12598 : pop_init_level (loc, 1, braced_init_obstack,
12599 : last_init_list_comma),
12600 : true, braced_init_obstack);
12601 5111 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12602 1698 : || gnu_vector_type_p (constructor_type))
12603 4057 : && constructor_max_index
12604 9141 : && tree_int_cst_lt (constructor_max_index,
12605 : constructor_index))
12606 241 : process_init_element (loc,
12607 : pop_init_level (loc, 1, braced_init_obstack,
12608 : last_init_list_comma),
12609 : true, braced_init_obstack);
12610 : else
12611 : break;
12612 : }
12613 :
12614 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12615 9608910 : if (constructor_range_stack)
12616 : {
12617 : /* If value is a compound literal and we'll be just using its
12618 : content, don't put it into a SAVE_EXPR. */
12619 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12620 3 : || !require_constant_value)
12621 : {
12622 368 : tree semantic_type = NULL_TREE;
12623 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12624 : {
12625 0 : semantic_type = TREE_TYPE (value.value);
12626 0 : value.value = TREE_OPERAND (value.value, 0);
12627 : }
12628 368 : value.value = save_expr (value.value);
12629 368 : if (semantic_type)
12630 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12631 : value.value);
12632 : }
12633 : }
12634 :
12635 10323154 : while (1)
12636 : {
12637 10323154 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12638 : {
12639 1038993 : tree fieldtype;
12640 1038993 : enum tree_code fieldcode;
12641 :
12642 1038993 : if (constructor_fields == NULL_TREE)
12643 : {
12644 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12645 1280 : break;
12646 : }
12647 :
12648 1037713 : fieldtype = TREE_TYPE (constructor_fields);
12649 1037713 : if (fieldtype != error_mark_node)
12650 1037712 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12651 1037713 : fieldcode = TREE_CODE (fieldtype);
12652 :
12653 : /* Error for non-static initialization of a flexible array member. */
12654 1037713 : if (fieldcode == ARRAY_TYPE
12655 155277 : && !require_constant_value
12656 1127 : && TYPE_SIZE (fieldtype) == NULL_TREE
12657 1037723 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12658 : {
12659 10 : error_init (loc, "non-static initialization of a flexible "
12660 : "array member");
12661 10 : break;
12662 : }
12663 :
12664 : /* Error for initialization of a flexible array member with
12665 : a string constant if the structure is in an array. E.g.:
12666 : struct S { int x; char y[]; };
12667 : struct S s[] = { { 1, "foo" } };
12668 : is invalid. */
12669 1037703 : if (string_flag
12670 1037703 : && fieldcode == ARRAY_TYPE
12671 3353 : && constructor_depth > 1
12672 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12673 1037750 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12674 : {
12675 47 : bool in_array_p = false;
12676 47 : for (struct constructor_stack *p = constructor_stack;
12677 73 : p && p->type; p = p->next)
12678 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12679 : {
12680 : in_array_p = true;
12681 : break;
12682 : }
12683 47 : if (in_array_p)
12684 : {
12685 33 : error_init (loc, "initialization of flexible array "
12686 : "member in a nested context");
12687 33 : break;
12688 : }
12689 : }
12690 :
12691 : /* Accept a string constant to initialize a subarray. */
12692 1037670 : if (value.value != NULL_TREE
12693 1037597 : && fieldcode == ARRAY_TYPE
12694 155161 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12695 1192344 : && string_flag)
12696 : value.value = orig_value;
12697 : /* Otherwise, if we have come to a subaggregate,
12698 : and we don't have an element of its type, push into it. */
12699 1034654 : else if (value.value != NULL_TREE
12700 1034350 : && initialize_elementwise_p (fieldtype, value.value))
12701 : {
12702 304 : push_init_level (loc, 1, braced_init_obstack);
12703 304 : continue;
12704 : }
12705 :
12706 1037366 : value.value = maybe_split_raw_data (value.value, &raw_data);
12707 1037366 : if (value.value)
12708 : {
12709 1037293 : push_member_name (constructor_fields);
12710 1037293 : output_init_element (loc, value.value, value.original_type,
12711 : strict_string, fieldtype,
12712 : constructor_fields, true, implicit,
12713 : braced_init_obstack);
12714 1037293 : RESTORE_SPELLING_DEPTH (constructor_depth);
12715 : }
12716 : else
12717 : /* Do the bookkeeping for an element that was
12718 : directly output as a constructor. */
12719 : {
12720 : /* For a record, keep track of end position of last field. */
12721 73 : if (DECL_SIZE (constructor_fields))
12722 30 : constructor_bit_index
12723 30 : = size_binop_loc (input_location, PLUS_EXPR,
12724 : bit_position (constructor_fields),
12725 30 : DECL_SIZE (constructor_fields));
12726 :
12727 : /* If the current field was the first one not yet written out,
12728 : it isn't now, so update. */
12729 73 : if (constructor_unfilled_fields == constructor_fields)
12730 : {
12731 64 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12732 : /* Skip any nameless bit fields. */
12733 64 : while (constructor_unfilled_fields != 0
12734 64 : && (DECL_UNNAMED_BIT_FIELD
12735 : (constructor_unfilled_fields)))
12736 0 : constructor_unfilled_fields =
12737 0 : DECL_CHAIN (constructor_unfilled_fields);
12738 : }
12739 : }
12740 :
12741 1037366 : constructor_fields = DECL_CHAIN (constructor_fields);
12742 : /* Skip any nameless bit fields at the beginning. */
12743 1037366 : while (constructor_fields != NULL_TREE
12744 1037467 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12745 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12746 : }
12747 9284161 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12748 : {
12749 31152 : tree fieldtype;
12750 31152 : enum tree_code fieldcode;
12751 :
12752 31152 : if (constructor_fields == NULL_TREE)
12753 : {
12754 3 : pedwarn_init (loc, 0,
12755 : "excess elements in union initializer");
12756 3 : break;
12757 : }
12758 :
12759 31149 : fieldtype = TREE_TYPE (constructor_fields);
12760 31149 : if (fieldtype != error_mark_node)
12761 31149 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12762 31149 : fieldcode = TREE_CODE (fieldtype);
12763 :
12764 : /* Warn that traditional C rejects initialization of unions.
12765 : We skip the warning if the value is zero. This is done
12766 : under the assumption that the zero initializer in user
12767 : code appears conditioned on e.g. __STDC__ to avoid
12768 : "missing initializer" warnings and relies on default
12769 : initialization to zero in the traditional C case.
12770 : We also skip the warning if the initializer is designated,
12771 : again on the assumption that this must be conditional on
12772 : __STDC__ anyway (and we've already complained about the
12773 : member-designator already). */
12774 33392 : if (!in_system_header_at (input_location) && !constructor_designated
12775 32630 : && !(value.value && (integer_zerop (value.value)
12776 1374 : || real_zerop (value.value))))
12777 1367 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12778 : "of unions");
12779 :
12780 : /* Error for non-static initialization of a flexible array member. */
12781 31149 : if (fieldcode == ARRAY_TYPE
12782 766 : && !require_constant_value
12783 31206 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12784 : {
12785 3 : error_init (loc, "non-static initialization of a flexible "
12786 : "array member");
12787 3 : break;
12788 : }
12789 :
12790 : /* Error for initialization of a flexible array member with
12791 : a string constant if the structure is in an array. E.g.:
12792 : union U { int x; char y[]; };
12793 : union U s[] = { { 1, "foo" } };
12794 : is invalid. */
12795 31146 : if (string_flag
12796 31146 : && fieldcode == ARRAY_TYPE
12797 7 : && constructor_depth > 1
12798 31150 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12799 : {
12800 3 : bool in_array_p = false;
12801 3 : for (struct constructor_stack *p = constructor_stack;
12802 3 : p && p->type; p = p->next)
12803 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12804 : {
12805 : in_array_p = true;
12806 : break;
12807 : }
12808 3 : if (in_array_p)
12809 : {
12810 3 : error_init (loc, "initialization of flexible array "
12811 : "member in a nested context");
12812 3 : break;
12813 : }
12814 : }
12815 :
12816 : /* Accept a string constant to initialize a subarray. */
12817 31143 : if (value.value != NULL_TREE
12818 31143 : && fieldcode == ARRAY_TYPE
12819 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12820 31898 : && string_flag)
12821 : value.value = orig_value;
12822 : /* Otherwise, if we have come to a subaggregate,
12823 : and we don't have an element of its type, push into it. */
12824 31189 : else if (value.value != NULL_TREE
12825 31139 : && initialize_elementwise_p (fieldtype, value.value))
12826 : {
12827 50 : push_init_level (loc, 1, braced_init_obstack);
12828 50 : continue;
12829 : }
12830 :
12831 31093 : value.value = maybe_split_raw_data (value.value, &raw_data);
12832 31093 : if (value.value)
12833 : {
12834 31093 : push_member_name (constructor_fields);
12835 31093 : output_init_element (loc, value.value, value.original_type,
12836 : strict_string, fieldtype,
12837 : constructor_fields, true, implicit,
12838 : braced_init_obstack);
12839 31093 : RESTORE_SPELLING_DEPTH (constructor_depth);
12840 : }
12841 : else
12842 : /* Do the bookkeeping for an element that was
12843 : directly output as a constructor. */
12844 : {
12845 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12846 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12847 : }
12848 :
12849 31093 : constructor_fields = NULL_TREE;
12850 : }
12851 9253009 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12852 : {
12853 2421732 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12854 2421732 : enum tree_code eltcode = TREE_CODE (elttype);
12855 :
12856 : /* Accept a string constant to initialize a subarray. */
12857 2421732 : if (value.value != NULL_TREE
12858 2421732 : && eltcode == ARRAY_TYPE
12859 7029 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12860 2427280 : && string_flag)
12861 : value.value = orig_value;
12862 : /* Otherwise, if we have come to a subaggregate,
12863 : and we don't have an element of its type, push into it. */
12864 3123562 : else if (value.value != NULL_TREE
12865 2421236 : && initialize_elementwise_p (elttype, value.value))
12866 : {
12867 702326 : push_init_level (loc, 1, braced_init_obstack);
12868 702326 : continue;
12869 : }
12870 :
12871 1719406 : if (constructor_max_index != NULL_TREE
12872 676480 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12873 676365 : || integer_all_onesp (constructor_max_index))
12874 : /* For VLA we got an error already. */
12875 1719521 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
12876 : {
12877 113 : pedwarn_init (loc, 0,
12878 : "excess elements in array initializer");
12879 113 : break;
12880 : }
12881 :
12882 1719293 : if (value.value
12883 1719293 : && TREE_CODE (value.value) == RAW_DATA_CST
12884 198 : && RAW_DATA_LENGTH (value.value) > 1
12885 198 : && (TREE_CODE (elttype) == INTEGER_TYPE
12886 198 : || TREE_CODE (elttype) == BITINT_TYPE)
12887 198 : && TYPE_PRECISION (elttype) == CHAR_BIT
12888 1719491 : && (constructor_max_index == NULL_TREE
12889 60 : || tree_int_cst_lt (constructor_index,
12890 : constructor_max_index)))
12891 : {
12892 198 : unsigned int len = RAW_DATA_LENGTH (value.value);
12893 198 : if (constructor_max_index)
12894 : {
12895 60 : widest_int w = wi::to_widest (constructor_max_index);
12896 60 : w -= wi::to_widest (constructor_index);
12897 60 : w += 1;
12898 60 : if (w < len)
12899 3 : len = w.to_uhwi ();
12900 60 : }
12901 198 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12902 : {
12903 3 : raw_data = copy_node (value.value);
12904 3 : RAW_DATA_LENGTH (raw_data) -= len;
12905 3 : RAW_DATA_POINTER (raw_data) += len;
12906 3 : RAW_DATA_LENGTH (value.value) = len;
12907 : }
12908 198 : TREE_TYPE (value.value) = elttype;
12909 198 : push_array_bounds (tree_to_uhwi (constructor_index));
12910 198 : output_init_element (loc, value.value, value.original_type,
12911 : false, elttype, constructor_index, true,
12912 : implicit, braced_init_obstack);
12913 198 : RESTORE_SPELLING_DEPTH (constructor_depth);
12914 198 : constructor_index
12915 198 : = size_binop_loc (input_location, PLUS_EXPR,
12916 198 : constructor_index, bitsize_int (len));
12917 : }
12918 : else
12919 : {
12920 1719095 : value.value = maybe_split_raw_data (value.value, &raw_data);
12921 : /* Now output the actual element. */
12922 1719095 : if (value.value)
12923 : {
12924 1719095 : push_array_bounds (tree_to_uhwi (constructor_index));
12925 1719095 : output_init_element (loc, value.value, value.original_type,
12926 : strict_string, elttype,
12927 : constructor_index, true, implicit,
12928 : braced_init_obstack);
12929 1719095 : RESTORE_SPELLING_DEPTH (constructor_depth);
12930 : }
12931 :
12932 1719095 : constructor_index
12933 1719095 : = size_binop_loc (input_location, PLUS_EXPR,
12934 : constructor_index, bitsize_one_node);
12935 :
12936 1719095 : if (!value.value)
12937 : /* If we are doing the bookkeeping for an element that was
12938 : directly output as a constructor, we must update
12939 : constructor_unfilled_index. */
12940 0 : constructor_unfilled_index = constructor_index;
12941 : }
12942 : }
12943 6831277 : else if (gnu_vector_type_p (constructor_type))
12944 : {
12945 6830785 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12946 :
12947 : /* Do a basic check of initializer size. Note that vectors
12948 : may not always have a fixed size derived from their type. */
12949 6830785 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12950 6830785 : tree_to_poly_uint64 (constructor_index)))
12951 : {
12952 : /* Diagose VLA out-of-bounds as errors. */
12953 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12954 2 : pedwarn_init (loc, 0,
12955 : "excess elements in vector initializer");
12956 : else
12957 : error_init (loc, "excess elements in vector initializer");
12958 :
12959 : break;
12960 : }
12961 :
12962 6830783 : value.value = maybe_split_raw_data (value.value, &raw_data);
12963 : /* Now output the actual element. */
12964 6830783 : if (value.value)
12965 : {
12966 6830783 : if (TREE_CODE (value.value) == VECTOR_CST)
12967 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
12968 6830783 : output_init_element (loc, value.value, value.original_type,
12969 : strict_string, elttype,
12970 : constructor_index, true, implicit,
12971 : braced_init_obstack);
12972 : }
12973 :
12974 6830783 : constructor_index
12975 6830783 : = size_binop_loc (input_location,
12976 : PLUS_EXPR, constructor_index, bitsize_one_node);
12977 :
12978 6830783 : if (!value.value)
12979 : /* If we are doing the bookkeeping for an element that was
12980 : directly output as a constructor, we must update
12981 : constructor_unfilled_index. */
12982 0 : constructor_unfilled_index = constructor_index;
12983 : }
12984 :
12985 : /* Handle the sole element allowed in a braced initializer
12986 : for a scalar variable. */
12987 492 : else if (constructor_type != error_mark_node
12988 492 : && constructor_fields == NULL_TREE)
12989 : {
12990 29 : pedwarn_init (loc, 0,
12991 : "excess elements in scalar initializer");
12992 29 : break;
12993 : }
12994 : else
12995 : {
12996 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
12997 463 : if (value.value)
12998 463 : output_init_element (loc, value.value, value.original_type,
12999 : strict_string, constructor_type,
13000 : NULL_TREE, true, implicit,
13001 : braced_init_obstack);
13002 463 : constructor_fields = NULL_TREE;
13003 : }
13004 :
13005 : /* Handle range initializers either at this level or anywhere higher
13006 : in the designator stack. */
13007 9618998 : if (constructor_range_stack)
13008 : {
13009 11564 : struct constructor_range_stack *p, *range_stack;
13010 11564 : int finish = 0;
13011 :
13012 11564 : range_stack = constructor_range_stack;
13013 11564 : constructor_range_stack = 0;
13014 11564 : while (constructor_stack != range_stack->stack)
13015 : {
13016 0 : gcc_assert (constructor_stack->implicit);
13017 0 : process_init_element (loc,
13018 : pop_init_level (loc, 1,
13019 : braced_init_obstack,
13020 : last_init_list_comma),
13021 : true, braced_init_obstack);
13022 : }
13023 325 : for (p = range_stack;
13024 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13025 325 : p = p->prev)
13026 : {
13027 325 : gcc_assert (constructor_stack->implicit);
13028 325 : process_init_element (loc,
13029 : pop_init_level (loc, 1,
13030 : braced_init_obstack,
13031 : last_init_list_comma),
13032 : true, braced_init_obstack);
13033 : }
13034 :
13035 11564 : p->index = size_binop_loc (input_location,
13036 : PLUS_EXPR, p->index, bitsize_one_node);
13037 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13038 379 : finish = 1;
13039 :
13040 11889 : while (1)
13041 : {
13042 11889 : constructor_index = p->index;
13043 11889 : constructor_fields = p->fields;
13044 11889 : if (finish && p->range_end && p->index == p->range_start)
13045 : {
13046 8 : finish = 0;
13047 8 : p->prev = 0;
13048 : }
13049 11889 : p = p->next;
13050 11889 : if (!p)
13051 : break;
13052 325 : finish_implicit_inits (loc, braced_init_obstack);
13053 325 : push_init_level (loc, 2, braced_init_obstack);
13054 325 : p->stack = constructor_stack;
13055 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13056 33 : p->index = p->range_start;
13057 : }
13058 :
13059 11564 : if (!finish)
13060 11193 : constructor_range_stack = range_stack;
13061 11564 : continue;
13062 11564 : }
13063 :
13064 : break;
13065 : }
13066 :
13067 9608910 : constructor_range_stack = 0;
13068 :
13069 9608910 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13070 : {
13071 212 : gcc_assert (!string_flag && !was_designated);
13072 212 : value.value = raw_data;
13073 212 : raw_data = NULL_TREE;
13074 212 : goto retry;
13075 : }
13076 : }
13077 :
13078 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13079 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13080 : an ASM_EXPR node). */
13081 : tree
13082 204666 : build_asm_stmt (bool is_volatile, tree args)
13083 : {
13084 204666 : if (is_volatile)
13085 164892 : ASM_VOLATILE_P (args) = 1;
13086 204666 : return add_stmt (args);
13087 : }
13088 :
13089 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13090 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13091 : SIMPLE indicates whether there was anything at all after the
13092 : string in the asm expression -- asm("blah") and asm("blah" : )
13093 : are subtly different. We use a ASM_EXPR node to represent this.
13094 : LOC is the location of the asm, and IS_INLINE says whether this
13095 : is asm inline. */
13096 : tree
13097 204718 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13098 : tree clobbers, tree labels, bool simple, bool is_inline)
13099 : {
13100 204718 : tree tail;
13101 204718 : tree args;
13102 204718 : int i;
13103 204718 : const char *constraint;
13104 204718 : const char **oconstraints;
13105 204718 : bool allows_mem, allows_reg, is_inout;
13106 204718 : int ninputs, noutputs;
13107 :
13108 204718 : ninputs = list_length (inputs);
13109 204718 : noutputs = list_length (outputs);
13110 204718 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13111 :
13112 204718 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13113 :
13114 : /* Remove output conversions that change the type but not the mode. */
13115 553094 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13116 : {
13117 348376 : tree output = TREE_VALUE (tail);
13118 :
13119 348376 : output = c_fully_fold (output, false, NULL, true);
13120 :
13121 : /* ??? Really, this should not be here. Users should be using a
13122 : proper lvalue, dammit. But there's a long history of using casts
13123 : in the output operands. In cases like longlong.h, this becomes a
13124 : primitive form of typechecking -- if the cast can be removed, then
13125 : the output operand had a type of the proper width; otherwise we'll
13126 : get an error. Gross, but ... */
13127 348376 : STRIP_NOPS (output);
13128 :
13129 348376 : if (!lvalue_or_else (loc, output, lv_asm))
13130 4 : output = error_mark_node;
13131 :
13132 348376 : if (output != error_mark_node
13133 348376 : && (TREE_READONLY (output)
13134 348369 : || TYPE_READONLY (TREE_TYPE (output))
13135 348369 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13136 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13137 2 : readonly_error (loc, output, lv_asm);
13138 :
13139 348376 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13140 348376 : oconstraints[i] = constraint;
13141 :
13142 348376 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13143 : &allows_mem, &allows_reg, &is_inout,
13144 : nullptr))
13145 : {
13146 : /* If the operand is going to end up in memory,
13147 : mark it addressable. */
13148 348368 : if (!allows_reg && !c_mark_addressable (output))
13149 3 : output = error_mark_node;
13150 1419 : if (!(!allows_reg && allows_mem)
13151 346949 : && output != error_mark_node
13152 695315 : && VOID_TYPE_P (TREE_TYPE (output)))
13153 : {
13154 4 : error_at (loc, "invalid use of void expression");
13155 4 : output = error_mark_node;
13156 : }
13157 348368 : if (allows_reg && current_function_decl == NULL_TREE)
13158 : {
13159 1 : error_at (loc, "constraint allows registers outside of "
13160 : "a function");
13161 1 : output = error_mark_node;
13162 : }
13163 : }
13164 : else
13165 8 : output = error_mark_node;
13166 :
13167 348376 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13168 : {
13169 7 : if (TREE_SIDE_EFFECTS (output))
13170 : {
13171 0 : error_at (loc, "side-effects in output operand outside "
13172 : "of a function");
13173 0 : output = error_mark_node;
13174 : }
13175 : else
13176 : {
13177 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13178 7 : if (addr == error_mark_node)
13179 : output = error_mark_node;
13180 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13181 : {
13182 1 : error_at (loc, "output operand outside of a function is not "
13183 : "constant");
13184 1 : output = error_mark_node;
13185 : }
13186 : }
13187 : }
13188 348369 : else if (output != error_mark_node && strstr (constraint, "-"))
13189 : {
13190 1 : error_at (loc, "%<-%> modifier used inside of a function");
13191 1 : output = error_mark_node;
13192 : }
13193 :
13194 348376 : TREE_VALUE (tail) = output;
13195 : }
13196 :
13197 568874 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13198 : {
13199 364156 : tree input;
13200 :
13201 364156 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13202 364156 : input = TREE_VALUE (tail);
13203 :
13204 364156 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13205 : oconstraints, &allows_mem, &allows_reg,
13206 : nullptr))
13207 : {
13208 : /* If the operand is going to end up in memory,
13209 : mark it addressable. */
13210 364148 : if (!allows_reg && allows_mem)
13211 : {
13212 1356 : input = c_fully_fold (input, false, NULL, true);
13213 :
13214 : /* Strip the nops as we allow this case. FIXME, this really
13215 : should be rejected or made deprecated. */
13216 1356 : STRIP_NOPS (input);
13217 1356 : if (!c_mark_addressable (input))
13218 2 : input = error_mark_node;
13219 : }
13220 : else
13221 : {
13222 362792 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13223 : true, false),
13224 : false, NULL);
13225 :
13226 362792 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13227 : {
13228 4 : error_at (loc, "invalid use of void expression");
13229 4 : input = error_mark_node;
13230 : }
13231 : }
13232 364148 : if (allows_reg && current_function_decl == NULL_TREE)
13233 : {
13234 1 : error_at (loc, "constraint allows registers outside of "
13235 : "a function");
13236 1 : input = error_mark_node;
13237 : }
13238 364148 : if (constraint[0] == ':' && input != error_mark_node)
13239 : {
13240 35 : tree t = input;
13241 35 : STRIP_NOPS (t);
13242 35 : if (TREE_CODE (t) != ADDR_EXPR
13243 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13244 23 : || (VAR_P (TREE_OPERAND (t, 0))
13245 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13246 : {
13247 5 : error_at (loc, "%<:%> constraint operand is not address "
13248 : "of a function or non-automatic variable");
13249 5 : input = error_mark_node;
13250 : }
13251 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13252 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13253 : }
13254 : }
13255 : else
13256 8 : input = error_mark_node;
13257 :
13258 364156 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13259 : {
13260 58 : if (TREE_SIDE_EFFECTS (input))
13261 : {
13262 2 : error_at (loc, "side-effects in input operand outside "
13263 : "of a function");
13264 2 : input = error_mark_node;
13265 : }
13266 : else
13267 : {
13268 56 : tree tem = input;
13269 56 : if (allows_mem && lvalue_p (input))
13270 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13271 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13272 : {
13273 2 : error_at (loc, "input operand outside of a function is not "
13274 : "constant");
13275 2 : input = error_mark_node;
13276 : }
13277 : }
13278 : }
13279 364098 : else if (input != error_mark_node && strstr (constraint, "-"))
13280 : {
13281 3 : error_at (loc, "%<-%> modifier used inside of a function");
13282 3 : input = error_mark_node;
13283 : }
13284 :
13285 364156 : TREE_VALUE (tail) = input;
13286 : }
13287 :
13288 204718 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13289 :
13290 : /* asm statements without outputs, including simple ones, are treated
13291 : as volatile. */
13292 204718 : ASM_BASIC_P (args) = simple;
13293 204718 : ASM_VOLATILE_P (args) = (noutputs == 0);
13294 204718 : ASM_INLINE_P (args) = is_inline;
13295 :
13296 204718 : return args;
13297 : }
13298 :
13299 : /* Generate a goto statement to LABEL. LOC is the location of the
13300 : GOTO. */
13301 :
13302 : tree
13303 82733 : c_finish_goto_label (location_t loc, tree label)
13304 : {
13305 82733 : tree decl = lookup_label_for_goto (loc, label);
13306 82733 : if (!decl)
13307 : return NULL_TREE;
13308 82733 : TREE_USED (decl) = 1;
13309 82733 : mark_decl_used (decl, false);
13310 82733 : {
13311 82733 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13312 82733 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13313 82733 : SET_EXPR_LOCATION (t, loc);
13314 82733 : return add_stmt (t);
13315 : }
13316 : }
13317 :
13318 : /* Generate a computed goto statement to EXPR. LOC is the location of
13319 : the GOTO. */
13320 :
13321 : tree
13322 942 : c_finish_goto_ptr (location_t loc, c_expr val)
13323 : {
13324 942 : tree expr = val.value;
13325 942 : tree t;
13326 942 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13327 942 : if (expr != error_mark_node
13328 941 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13329 947 : && !null_pointer_constant_p (expr))
13330 : {
13331 2 : error_at (val.get_location (),
13332 : "computed goto must be pointer type");
13333 2 : expr = build_zero_cst (ptr_type_node);
13334 : }
13335 942 : expr = c_fully_fold (expr, false, NULL);
13336 942 : expr = convert (ptr_type_node, expr);
13337 942 : t = build1 (GOTO_EXPR, void_type_node, expr);
13338 942 : SET_EXPR_LOCATION (t, loc);
13339 942 : return add_stmt (t);
13340 : }
13341 :
13342 : /* Generate a C `return' statement. RETVAL is the expression for what
13343 : to return, or a null pointer for `return;' with no value. LOC is
13344 : the location of the return statement, or the location of the expression,
13345 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13346 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13347 : attribute. */
13348 :
13349 : tree
13350 34629137 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13351 : {
13352 34629137 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13353 34629137 : bool no_warning = false;
13354 34629137 : bool npc = false;
13355 :
13356 : /* Use the expansion point to handle cases such as returning NULL
13357 : in a function returning void. */
13358 34629137 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13359 :
13360 34629137 : if (TREE_THIS_VOLATILE (current_function_decl))
13361 23 : warning_at (xloc, 0,
13362 : "function declared %<noreturn%> has a %<return%> statement");
13363 :
13364 34629137 : set_musttail_on_return (retval, xloc, musttail_p);
13365 :
13366 34629137 : if (retval)
13367 : {
13368 34607728 : tree semantic_type = NULL_TREE;
13369 34607728 : npc = null_pointer_constant_p (retval);
13370 34607728 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13371 : {
13372 92 : semantic_type = TREE_TYPE (retval);
13373 92 : retval = TREE_OPERAND (retval, 0);
13374 : }
13375 34607728 : retval = c_fully_fold (retval, false, NULL);
13376 34607728 : if (semantic_type
13377 34607728 : && valtype != NULL_TREE
13378 92 : && TREE_CODE (valtype) != VOID_TYPE)
13379 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13380 : }
13381 :
13382 34607728 : if (!retval)
13383 : {
13384 21409 : current_function_returns_null = 1;
13385 21409 : if ((warn_return_type >= 0 || flag_isoc99)
13386 21284 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13387 : {
13388 46 : no_warning = true;
13389 47 : if (emit_diagnostic (flag_isoc99
13390 : ? diagnostics::kind::permerror
13391 : : diagnostics::kind::warning,
13392 46 : loc, OPT_Wreturn_mismatch,
13393 : "%<return%> with no value,"
13394 : " in function returning non-void"))
13395 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13396 : "declared here");
13397 : }
13398 : }
13399 34607728 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13400 : {
13401 322 : current_function_returns_null = 1;
13402 322 : bool warned_here;
13403 322 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13404 64 : warned_here = permerror_opt
13405 64 : (xloc, OPT_Wreturn_mismatch,
13406 : "%<return%> with a value, in function returning void");
13407 : else
13408 258 : warned_here = pedwarn
13409 258 : (xloc, OPT_Wpedantic, "ISO C forbids "
13410 : "%<return%> with expression, in function returning void");
13411 322 : if (warned_here)
13412 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13413 : "declared here");
13414 : }
13415 : else
13416 : {
13417 34607406 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13418 : retval, origtype, ic_return,
13419 : npc, NULL_TREE, NULL_TREE, 0);
13420 34607406 : tree res = DECL_RESULT (current_function_decl);
13421 34607406 : tree inner;
13422 34607406 : bool save;
13423 :
13424 34607406 : current_function_returns_value = 1;
13425 34607406 : if (t == error_mark_node)
13426 : {
13427 : /* Suppress -Wreturn-type for this function. */
13428 298 : if (warn_return_type)
13429 297 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13430 298 : return NULL_TREE;
13431 : }
13432 :
13433 34607108 : save = in_late_binary_op;
13434 69206105 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13435 34598192 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13436 69204020 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13437 333331 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13438 333331 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13439 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13440 10196 : in_late_binary_op = true;
13441 34607108 : inner = t = convert (TREE_TYPE (res), t);
13442 34607108 : in_late_binary_op = save;
13443 :
13444 : /* Strip any conversions, additions, and subtractions, and see if
13445 : we are returning the address of a local variable. Warn if so. */
13446 37882784 : while (1)
13447 : {
13448 37882784 : switch (TREE_CODE (inner))
13449 : {
13450 3274250 : CASE_CONVERT:
13451 3274250 : case NON_LVALUE_EXPR:
13452 3274250 : case PLUS_EXPR:
13453 3274250 : case POINTER_PLUS_EXPR:
13454 3274250 : inner = TREE_OPERAND (inner, 0);
13455 3274250 : continue;
13456 :
13457 1431 : case MINUS_EXPR:
13458 : /* If the second operand of the MINUS_EXPR has a pointer
13459 : type (or is converted from it), this may be valid, so
13460 : don't give a warning. */
13461 1431 : {
13462 1431 : tree op1 = TREE_OPERAND (inner, 1);
13463 :
13464 3111 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13465 3365 : && (CONVERT_EXPR_P (op1)
13466 1426 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13467 254 : op1 = TREE_OPERAND (op1, 0);
13468 :
13469 1431 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13470 : break;
13471 :
13472 1426 : inner = TREE_OPERAND (inner, 0);
13473 1426 : continue;
13474 1426 : }
13475 :
13476 2925 : case ADDR_EXPR:
13477 2925 : inner = TREE_OPERAND (inner, 0);
13478 :
13479 2925 : while (REFERENCE_CLASS_P (inner)
13480 3811 : && !INDIRECT_REF_P (inner))
13481 886 : inner = TREE_OPERAND (inner, 0);
13482 :
13483 2925 : if (DECL_P (inner)
13484 1604 : && !DECL_EXTERNAL (inner)
13485 973 : && DECL_CONTEXT (inner) == current_function_decl
13486 3142 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13487 : {
13488 177 : if (TREE_CODE (inner) == LABEL_DECL)
13489 4 : warning_at (loc, OPT_Wreturn_local_addr,
13490 : "function returns address of label");
13491 173 : else if (TREE_CODE (inner) == FUNCTION_DECL
13492 173 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13493 8 : || !DECL_INITIAL (inner)))
13494 10 : warning_at (loc, OPT_Wreturn_local_addr,
13495 : "function returns address of nested function "
13496 : "referencing local context");
13497 163 : else if (TREE_CODE (inner) != FUNCTION_DECL
13498 156 : && !TREE_STATIC (inner))
13499 : {
13500 9 : warning_at (loc, OPT_Wreturn_local_addr,
13501 : "function returns address of local variable");
13502 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13503 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13504 : }
13505 : }
13506 : break;
13507 :
13508 : default:
13509 : break;
13510 3274250 : }
13511 :
13512 34607108 : break;
13513 : }
13514 :
13515 34607108 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13516 34607108 : SET_EXPR_LOCATION (retval, loc);
13517 :
13518 34607108 : if (warn_sequence_point)
13519 2964475 : verify_sequence_points (retval);
13520 : }
13521 :
13522 34628839 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13523 34628839 : if (no_warning)
13524 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13525 34628839 : return add_stmt (ret_stmt);
13526 : }
13527 :
13528 : struct c_switch {
13529 : /* The SWITCH_STMT being built. */
13530 : tree switch_stmt;
13531 :
13532 : /* The original type of the testing expression, i.e. before the
13533 : default conversion is applied. */
13534 : tree orig_type;
13535 :
13536 : /* A splay-tree mapping the low element of a case range to the high
13537 : element, or NULL_TREE if there is no high element. Used to
13538 : determine whether or not a new case label duplicates an old case
13539 : label. We need a tree, rather than simply a hash table, because
13540 : of the GNU case range extension. */
13541 : splay_tree cases;
13542 :
13543 : /* The bindings at the point of the switch. This is used for
13544 : warnings crossing decls when branching to a case label. */
13545 : struct c_spot_bindings *bindings;
13546 :
13547 : /* Whether the switch includes any break statements. */
13548 : bool break_stmt_seen_p;
13549 :
13550 : /* The next node on the stack. */
13551 : struct c_switch *next;
13552 :
13553 : /* Remember whether the controlling expression had boolean type
13554 : before integer promotions for the sake of -Wswitch-bool. */
13555 : bool bool_cond_p;
13556 : };
13557 :
13558 : /* A stack of the currently active switch statements. The innermost
13559 : switch statement is on the top of the stack. There is no need to
13560 : mark the stack for garbage collection because it is only active
13561 : during the processing of the body of a function, and we never
13562 : collect at that point. */
13563 :
13564 : struct c_switch *c_switch_stack;
13565 :
13566 : /* Start a C switch statement, testing expression EXP. Return the new
13567 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13568 : SWITCH_COND_LOC is the location of the switch's condition.
13569 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13570 :
13571 : tree
13572 37299 : c_start_switch (location_t switch_loc,
13573 : location_t switch_cond_loc,
13574 : tree exp, bool explicit_cast_p, tree switch_name)
13575 : {
13576 37299 : tree orig_type = error_mark_node;
13577 37299 : bool bool_cond_p = false;
13578 37299 : struct c_switch *cs;
13579 :
13580 37299 : if (exp != error_mark_node)
13581 : {
13582 37256 : orig_type = TREE_TYPE (exp);
13583 :
13584 37256 : if (!INTEGRAL_TYPE_P (orig_type))
13585 : {
13586 12 : if (orig_type != error_mark_node)
13587 : {
13588 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13589 12 : orig_type = error_mark_node;
13590 : }
13591 12 : exp = integer_zero_node;
13592 : }
13593 : else
13594 : {
13595 37244 : tree type = TYPE_MAIN_VARIANT (orig_type);
13596 37244 : tree e = exp;
13597 :
13598 : /* Warn if the condition has boolean value. */
13599 37247 : while (TREE_CODE (e) == COMPOUND_EXPR)
13600 3 : e = TREE_OPERAND (e, 1);
13601 :
13602 37219 : if ((C_BOOLEAN_TYPE_P (type)
13603 37219 : || truth_value_p (TREE_CODE (e)))
13604 : /* Explicit cast to int suppresses this warning. */
13605 37266 : && !(TREE_CODE (type) == INTEGER_TYPE
13606 : && explicit_cast_p))
13607 : bool_cond_p = true;
13608 :
13609 37244 : if (!in_system_header_at (input_location)
13610 37244 : && (type == long_integer_type_node
13611 11953 : || type == long_unsigned_type_node))
13612 280 : warning_at (switch_cond_loc,
13613 280 : OPT_Wtraditional, "%<long%> switch expression not "
13614 : "converted to %<int%> in ISO C");
13615 :
13616 37244 : exp = c_fully_fold (exp, false, NULL);
13617 37244 : exp = default_conversion (exp);
13618 :
13619 37244 : if (warn_sequence_point)
13620 6009 : verify_sequence_points (exp);
13621 : }
13622 : }
13623 :
13624 : /* Add this new SWITCH_STMT to the stack. */
13625 37299 : cs = XNEW (struct c_switch);
13626 37299 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13627 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13628 37299 : cs->orig_type = orig_type;
13629 37299 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13630 37299 : cs->bindings = c_get_switch_bindings ();
13631 37299 : cs->break_stmt_seen_p = false;
13632 37299 : cs->bool_cond_p = bool_cond_p;
13633 37299 : cs->next = c_switch_stack;
13634 37299 : c_switch_stack = cs;
13635 :
13636 37299 : return add_stmt (cs->switch_stmt);
13637 : }
13638 :
13639 : /* Process a case label at location LOC, with attributes ATTRS. */
13640 :
13641 : tree
13642 1030323 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13643 : {
13644 1030323 : tree label = NULL_TREE;
13645 :
13646 1030323 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13647 : {
13648 87 : low_value = c_fully_fold (low_value, false, NULL);
13649 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13650 9 : pedwarn (loc, OPT_Wpedantic,
13651 : "case label is not an integer constant expression");
13652 : }
13653 :
13654 1030323 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13655 : {
13656 0 : high_value = c_fully_fold (high_value, false, NULL);
13657 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13658 0 : pedwarn (input_location, OPT_Wpedantic,
13659 : "case label is not an integer constant expression");
13660 : }
13661 :
13662 1030323 : if (c_switch_stack == NULL)
13663 : {
13664 3 : if (low_value)
13665 2 : error_at (loc, "case label not within a switch statement");
13666 : else
13667 1 : error_at (loc, "%<default%> label not within a switch statement");
13668 3 : return NULL_TREE;
13669 : }
13670 :
13671 1030320 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13672 1030320 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13673 : loc))
13674 : return NULL_TREE;
13675 :
13676 1030308 : label = c_add_case_label (loc, c_switch_stack->cases,
13677 1030308 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13678 : low_value, high_value, attrs);
13679 1030308 : if (label == error_mark_node)
13680 144 : label = NULL_TREE;
13681 : return label;
13682 : }
13683 :
13684 : /* Finish the switch statement. TYPE is the original type of the
13685 : controlling expression of the switch, or NULL_TREE. */
13686 :
13687 : void
13688 37299 : c_finish_switch (tree body, tree type)
13689 : {
13690 37299 : struct c_switch *cs = c_switch_stack;
13691 37299 : location_t switch_location;
13692 :
13693 37299 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13694 :
13695 : /* Emit warnings as needed. */
13696 37299 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13697 43497 : c_do_switch_warnings (cs->cases, switch_location,
13698 6198 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13699 37299 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13700 37299 : if (c_switch_covers_all_cases_p (cs->cases,
13701 37299 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13702 33379 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13703 37299 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13704 :
13705 : /* Pop the stack. */
13706 37299 : c_switch_stack = cs->next;
13707 37299 : splay_tree_delete (cs->cases);
13708 37299 : c_release_switch_bindings (cs->bindings);
13709 37299 : XDELETE (cs);
13710 37299 : }
13711 :
13712 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13713 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13714 : may be null. */
13715 :
13716 : void
13717 1269378 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13718 : tree else_block)
13719 : {
13720 1269378 : tree stmt;
13721 :
13722 1269378 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13723 1269378 : SET_EXPR_LOCATION (stmt, if_locus);
13724 1269378 : add_stmt (stmt);
13725 1269378 : }
13726 :
13727 : tree
13728 187404 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13729 : {
13730 : /* In switch statements break is sometimes stylistically used after
13731 : a return statement. This can lead to spurious warnings about
13732 : control reaching the end of a non-void function when it is
13733 : inlined. Note that we are calling block_may_fallthru with
13734 : language specific tree nodes; this works because
13735 : block_may_fallthru returns true when given something it does not
13736 : understand. */
13737 187404 : bool skip = !block_may_fallthru (cur_stmt_list);
13738 :
13739 187404 : if (is_break)
13740 174242 : switch (in_statement & ~IN_NAMED_STMT)
13741 : {
13742 9 : case 0:
13743 9 : error_at (loc, "break statement not within loop or switch");
13744 9 : return NULL_TREE;
13745 4 : case IN_OMP_BLOCK:
13746 4 : error_at (loc, "invalid exit from OpenMP structured block");
13747 4 : return NULL_TREE;
13748 12 : case IN_OMP_FOR:
13749 12 : error_at (loc, "break statement used with OpenMP for loop");
13750 12 : return NULL_TREE;
13751 : case IN_ITERATION_STMT:
13752 : case IN_OBJC_FOREACH:
13753 : break;
13754 158465 : default:
13755 158465 : gcc_assert (in_statement & IN_SWITCH_STMT);
13756 158465 : c_switch_stack->break_stmt_seen_p = true;
13757 158465 : break;
13758 : }
13759 : else
13760 13162 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13761 : {
13762 7 : case 0:
13763 7 : error_at (loc, "continue statement not within a loop");
13764 7 : return NULL_TREE;
13765 4 : case IN_OMP_BLOCK:
13766 4 : error_at (loc, "invalid exit from OpenMP structured block");
13767 4 : return NULL_TREE;
13768 : case IN_ITERATION_STMT:
13769 : case IN_OMP_FOR:
13770 : case IN_OBJC_FOREACH:
13771 : break;
13772 0 : default:
13773 0 : gcc_unreachable ();
13774 : }
13775 :
13776 187368 : if (skip)
13777 : return NULL_TREE;
13778 186814 : else if ((in_statement & IN_OBJC_FOREACH)
13779 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13780 0 : && name == NULL_TREE)
13781 : {
13782 : /* The foreach expander produces low-level code using gotos instead
13783 : of a structured loop construct. */
13784 0 : gcc_assert (label);
13785 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13786 : }
13787 186911 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13788 : {
13789 0 : label = DECL_CHAIN (name);
13790 0 : if (!is_break)
13791 0 : label = DECL_CHAIN (label);
13792 : /* Foreach expander from some outer level. */
13793 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13794 : }
13795 199965 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13796 186814 : name));
13797 : }
13798 :
13799 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13800 :
13801 : static void
13802 6360171 : emit_side_effect_warnings (location_t loc, tree expr)
13803 : {
13804 6360171 : maybe_warn_nodiscard (loc, expr);
13805 6360171 : if (!warn_unused_value)
13806 : return;
13807 1294035 : if (expr == error_mark_node)
13808 : ;
13809 1293960 : else if (!TREE_SIDE_EFFECTS (expr))
13810 : {
13811 6446 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13812 6446 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13813 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13814 : }
13815 1287514 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13816 : {
13817 : tree r = expr;
13818 : location_t cloc = loc;
13819 11991 : while (TREE_CODE (r) == COMPOUND_EXPR)
13820 : {
13821 6011 : if (EXPR_HAS_LOCATION (r))
13822 5481 : cloc = EXPR_LOCATION (r);
13823 6011 : r = TREE_OPERAND (r, 1);
13824 : }
13825 5980 : if (!TREE_SIDE_EFFECTS (r)
13826 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13827 24 : && !CONVERT_EXPR_P (r)
13828 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13829 5992 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13830 8 : warning_at (cloc, OPT_Wunused_value,
13831 : "right-hand operand of comma expression has no effect");
13832 : }
13833 : else
13834 1281534 : warn_if_unused_value (expr, loc);
13835 : }
13836 :
13837 : /* Process an expression as if it were a complete statement. Emit
13838 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13839 : statement. */
13840 :
13841 : tree
13842 6236758 : c_process_expr_stmt (location_t loc, tree expr)
13843 : {
13844 6236758 : tree exprv;
13845 :
13846 6236758 : if (!expr)
13847 : return NULL_TREE;
13848 :
13849 6232435 : expr = c_fully_fold (expr, false, NULL);
13850 :
13851 6232435 : if (warn_sequence_point)
13852 1289827 : verify_sequence_points (expr);
13853 :
13854 6232435 : if (TREE_TYPE (expr) != error_mark_node
13855 6229858 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13856 6232435 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13857 0 : error_at (loc, "expression statement has incomplete type");
13858 :
13859 : /* If we're not processing a statement expression, warn about unused values.
13860 : Warnings for statement expressions will be emitted later, once we figure
13861 : out which is the result. */
13862 6232435 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13863 6232435 : && (warn_unused_value || warn_unused_result))
13864 6146350 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13865 :
13866 : exprv = expr;
13867 6271150 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13868 38716 : exprv = TREE_OPERAND (exprv, 1);
13869 6243595 : while (CONVERT_EXPR_P (exprv))
13870 11161 : exprv = TREE_OPERAND (exprv, 0);
13871 6232434 : if (DECL_P (exprv)
13872 6233019 : || handled_component_p (exprv)
13873 12445331 : || TREE_CODE (exprv) == ADDR_EXPR)
13874 20122 : mark_exp_read (exprv);
13875 :
13876 : /* If the expression is not of a type to which we cannot assign a line
13877 : number, wrap the thing in a no-op NOP_EXPR. */
13878 6232434 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13879 : {
13880 14871 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13881 14871 : SET_EXPR_LOCATION (expr, loc);
13882 : }
13883 :
13884 : return expr;
13885 : }
13886 :
13887 : /* Emit an expression as a statement. LOC is the location of the
13888 : expression. */
13889 :
13890 : tree
13891 5936333 : c_finish_expr_stmt (location_t loc, tree expr)
13892 : {
13893 5936333 : if (expr)
13894 5923825 : return add_stmt (c_process_expr_stmt (loc, expr));
13895 : else
13896 : return NULL;
13897 : }
13898 :
13899 : /* Do the opposite and emit a statement as an expression. To begin,
13900 : create a new binding level and return it. */
13901 :
13902 : tree
13903 34875 : c_begin_stmt_expr (void)
13904 : {
13905 34875 : tree ret;
13906 :
13907 : /* We must force a BLOCK for this level so that, if it is not expanded
13908 : later, there is a way to turn off the entire subtree of blocks that
13909 : are contained in it. */
13910 34875 : keep_next_level ();
13911 34875 : ret = c_begin_compound_stmt (true);
13912 :
13913 34875 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13914 : ? NULL
13915 : : c_switch_stack->bindings);
13916 :
13917 : /* Mark the current statement list as belonging to a statement list. */
13918 34875 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13919 :
13920 34875 : return ret;
13921 : }
13922 :
13923 : /* LOC is the location of the compound statement to which this body
13924 : belongs. */
13925 :
13926 : tree
13927 34875 : c_finish_stmt_expr (location_t loc, tree body)
13928 : {
13929 34875 : tree last, type, tmp, val;
13930 34875 : tree *last_p;
13931 :
13932 34875 : body = c_end_compound_stmt (loc, body, true);
13933 :
13934 34875 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13935 : ? NULL
13936 : : c_switch_stack->bindings);
13937 :
13938 : /* Locate the last statement in BODY. See c_end_compound_stmt
13939 : about always returning a BIND_EXPR. */
13940 34875 : last_p = &BIND_EXPR_BODY (body);
13941 34875 : last = BIND_EXPR_BODY (body);
13942 :
13943 34875 : continue_searching:
13944 34875 : if (TREE_CODE (last) == STATEMENT_LIST)
13945 : {
13946 26061 : tree_stmt_iterator l = tsi_last (last);
13947 :
13948 26067 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13949 6 : tsi_prev (&l);
13950 :
13951 : /* This can happen with degenerate cases like ({ }). No value. */
13952 26061 : if (tsi_end_p (l))
13953 389 : return body;
13954 :
13955 : /* If we're supposed to generate side effects warnings, process
13956 : all of the statements except the last. */
13957 25672 : if (warn_unused_value || warn_unused_result)
13958 : {
13959 25672 : for (tree_stmt_iterator i = tsi_start (last);
13960 275711 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13961 : {
13962 250039 : location_t tloc;
13963 250039 : tree t = tsi_stmt (i);
13964 :
13965 250039 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13966 250039 : emit_side_effect_warnings (tloc, t);
13967 : }
13968 : }
13969 25672 : last_p = tsi_stmt_ptr (l);
13970 25672 : last = *last_p;
13971 : }
13972 :
13973 : /* If the end of the list is exception related, then the list was split
13974 : by a call to push_cleanup. Continue searching. */
13975 34486 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
13976 34486 : || TREE_CODE (last) == TRY_CATCH_EXPR)
13977 : {
13978 0 : last_p = &TREE_OPERAND (last, 0);
13979 0 : last = *last_p;
13980 0 : goto continue_searching;
13981 : }
13982 :
13983 34486 : if (last == error_mark_node)
13984 : return last;
13985 :
13986 : /* In the case that the BIND_EXPR is not necessary, return the
13987 : expression out from inside it. */
13988 34477 : if ((last == BIND_EXPR_BODY (body)
13989 : /* Skip nested debug stmts. */
13990 25671 : || last == expr_first (BIND_EXPR_BODY (body)))
13991 45484 : && BIND_EXPR_VARS (body) == NULL)
13992 : {
13993 : /* Even if this looks constant, do not allow it in a constant
13994 : expression. */
13995 10995 : last = c_wrap_maybe_const (last, true);
13996 : /* Do not warn if the return value of a statement expression is
13997 : unused. */
13998 10995 : suppress_warning (last, OPT_Wunused);
13999 10995 : return last;
14000 : }
14001 :
14002 : /* Extract the type of said expression. */
14003 23482 : type = TREE_TYPE (last);
14004 :
14005 : /* If we're not returning a value at all, then the BIND_EXPR that
14006 : we already have is a fine expression to return. */
14007 23482 : if (!type || VOID_TYPE_P (type))
14008 : return body;
14009 :
14010 : /* Now that we've located the expression containing the value, it seems
14011 : silly to make voidify_wrapper_expr repeat the process. Create a
14012 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14013 18158 : tmp = create_tmp_var_raw (type);
14014 :
14015 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14016 : tree_expr_nonnegative_p giving up immediately. */
14017 18158 : val = last;
14018 18158 : if (TREE_CODE (val) == NOP_EXPR
14019 18158 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14020 3773 : val = TREE_OPERAND (val, 0);
14021 :
14022 18158 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14023 18158 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14024 :
14025 18158 : {
14026 18158 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14027 18158 : SET_EXPR_LOCATION (t, loc);
14028 18158 : return t;
14029 : }
14030 : }
14031 :
14032 : /* Begin and end compound statements. This is as simple as pushing
14033 : and popping new statement lists from the tree. */
14034 :
14035 : tree
14036 41144277 : c_begin_compound_stmt (bool do_scope)
14037 : {
14038 41144277 : tree stmt = push_stmt_list ();
14039 41144277 : if (do_scope)
14040 41048268 : push_scope ();
14041 41144277 : return stmt;
14042 : }
14043 :
14044 : /* End a compound statement. STMT is the statement. LOC is the
14045 : location of the compound statement-- this is usually the location
14046 : of the opening brace. */
14047 :
14048 : tree
14049 41144276 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14050 : {
14051 41144276 : tree block = NULL;
14052 :
14053 41144276 : if (do_scope)
14054 : {
14055 41048267 : if (c_dialect_objc ())
14056 0 : objc_clear_super_receiver ();
14057 41048267 : block = pop_scope ();
14058 : }
14059 :
14060 41144276 : stmt = pop_stmt_list (stmt);
14061 41144276 : stmt = c_build_bind_expr (loc, block, stmt);
14062 :
14063 : /* If this compound statement is nested immediately inside a statement
14064 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14065 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14066 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14067 : was really last. */
14068 82288552 : if (building_stmt_list_p ()
14069 41144191 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14070 41266937 : && TREE_CODE (stmt) != BIND_EXPR)
14071 : {
14072 120918 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14073 120918 : TREE_SIDE_EFFECTS (stmt) = 1;
14074 120918 : SET_EXPR_LOCATION (stmt, loc);
14075 : }
14076 :
14077 41144276 : return stmt;
14078 : }
14079 :
14080 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14081 : when the current scope is exited. EH_ONLY is true when this is not
14082 : meant to apply to normal control flow transfer. */
14083 :
14084 : void
14085 131 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14086 : {
14087 131 : enum tree_code code;
14088 131 : tree stmt, list;
14089 131 : bool stmt_expr;
14090 :
14091 131 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14092 131 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14093 131 : add_stmt (stmt);
14094 131 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14095 131 : list = push_stmt_list ();
14096 131 : TREE_OPERAND (stmt, 0) = list;
14097 131 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14098 131 : }
14099 :
14100 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14101 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14102 :
14103 : static tree
14104 100944 : build_vec_cmp (tree_code code, tree type,
14105 : tree arg0, tree arg1)
14106 : {
14107 100944 : tree zero_vec = build_zero_cst (type);
14108 100944 : tree minus_one_vec = build_minus_one_cst (type);
14109 100944 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14110 100944 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14111 100944 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14112 : }
14113 :
14114 : /* Possibly warn about an address of OP never being NULL in a comparison
14115 : operation CODE involving null. */
14116 :
14117 : static void
14118 51687 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14119 : {
14120 : /* Prevent warnings issued for macro expansion. */
14121 51687 : if (!warn_address
14122 30203 : || warning_suppressed_p (op, OPT_Waddress)
14123 81546 : || from_macro_expansion_at (loc))
14124 24584 : return;
14125 :
14126 27103 : if (TREE_CODE (op) == NOP_EXPR)
14127 : {
14128 : /* Allow casts to intptr_t to suppress the warning. */
14129 1133 : tree type = TREE_TYPE (op);
14130 1133 : if (TREE_CODE (type) == INTEGER_TYPE)
14131 : return;
14132 1133 : op = TREE_OPERAND (op, 0);
14133 : }
14134 :
14135 27103 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14136 : {
14137 : /* Allow a cast to void* to suppress the warning. */
14138 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14139 24 : if (VOID_TYPE_P (type))
14140 : return;
14141 :
14142 : /* Adding any value to a null pointer, including zero, is undefined
14143 : in C. This includes the expression &p[0] where p is the null
14144 : pointer, although &p[0] will have been folded to p by this point
14145 : and so not diagnosed. */
14146 24 : if (code == EQ_EXPR)
14147 22 : warning_at (loc, OPT_Waddress,
14148 : "the comparison will always evaluate as %<false%> "
14149 : "for the pointer operand in %qE must not be NULL",
14150 : op);
14151 : else
14152 2 : warning_at (loc, OPT_Waddress,
14153 : "the comparison will always evaluate as %<true%> "
14154 : "for the pointer operand in %qE must not be NULL",
14155 : op);
14156 :
14157 24 : return;
14158 : }
14159 :
14160 27079 : if (TREE_CODE (op) != ADDR_EXPR)
14161 : return;
14162 :
14163 254 : op = TREE_OPERAND (op, 0);
14164 :
14165 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14166 254 : || TREE_CODE (op) == REALPART_EXPR)
14167 : {
14168 : /* The address of either complex part may not be null. */
14169 14 : if (code == EQ_EXPR)
14170 9 : warning_at (loc, OPT_Waddress,
14171 : "the comparison will always evaluate as %<false%> "
14172 : "for the address of %qE will never be NULL",
14173 : op);
14174 : else
14175 5 : warning_at (loc, OPT_Waddress,
14176 : "the comparison will always evaluate as %<true%> "
14177 : "for the address of %qE will never be NULL",
14178 : op);
14179 14 : return;
14180 : }
14181 :
14182 : /* Set to true in the loop below if OP dereferences is operand.
14183 : In such a case the ultimate target need not be a decl for
14184 : the null [in]equality test to be constant. */
14185 : bool deref = false;
14186 :
14187 : /* Get the outermost array or object, or member. */
14188 294 : while (handled_component_p (op))
14189 : {
14190 72 : if (TREE_CODE (op) == COMPONENT_REF)
14191 : {
14192 : /* Get the member (its address is never null). */
14193 18 : op = TREE_OPERAND (op, 1);
14194 18 : break;
14195 : }
14196 :
14197 : /* Get the outer array/object to refer to in the warning. */
14198 54 : op = TREE_OPERAND (op, 0);
14199 54 : deref = true;
14200 : }
14201 :
14202 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14203 371 : || from_macro_expansion_at (loc))
14204 109 : return;
14205 :
14206 131 : bool w;
14207 131 : if (code == EQ_EXPR)
14208 93 : w = warning_at (loc, OPT_Waddress,
14209 : "the comparison will always evaluate as %<false%> "
14210 : "for the address of %qE will never be NULL",
14211 : op);
14212 : else
14213 38 : w = warning_at (loc, OPT_Waddress,
14214 : "the comparison will always evaluate as %<true%> "
14215 : "for the address of %qE will never be NULL",
14216 : op);
14217 :
14218 131 : if (w && DECL_P (op))
14219 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14220 : }
14221 :
14222 : /* Build a binary-operation expression without default conversions.
14223 : CODE is the kind of expression to build.
14224 : LOCATION is the operator's location.
14225 : This function differs from `build' in several ways:
14226 : the data type of the result is computed and recorded in it,
14227 : warnings are generated if arg data types are invalid,
14228 : special handling for addition and subtraction of pointers is known,
14229 : and some optimization is done (operations on narrow ints
14230 : are done in the narrower type when that gives the same result).
14231 : Constant folding is also done before the result is returned.
14232 :
14233 : Note that the operands will never have enumeral types, or function
14234 : or array types, because either they will have the default conversions
14235 : performed or they have both just been converted to some other type in which
14236 : the arithmetic is to be done. */
14237 :
14238 : tree
14239 16810833 : build_binary_op (location_t location, enum tree_code code,
14240 : tree orig_op0, tree orig_op1, bool convert_p)
14241 : {
14242 16810833 : tree type0, type1, orig_type0, orig_type1;
14243 16810833 : tree eptype;
14244 16810833 : enum tree_code code0, code1;
14245 16810833 : tree op0, op1;
14246 16810833 : tree ret = error_mark_node;
14247 16810833 : const char *invalid_op_diag;
14248 16810833 : bool op0_int_operands, op1_int_operands;
14249 16810833 : bool int_const, int_const_or_overflow, int_operands;
14250 :
14251 : /* Expression code to give to the expression when it is built.
14252 : Normally this is CODE, which is what the caller asked for,
14253 : but in some special cases we change it. */
14254 16810833 : enum tree_code resultcode = code;
14255 :
14256 : /* Data type in which the computation is to be performed.
14257 : In the simplest cases this is the common type of the arguments. */
14258 16810833 : tree result_type = NULL;
14259 :
14260 : /* When the computation is in excess precision, the type of the
14261 : final EXCESS_PRECISION_EXPR. */
14262 16810833 : tree semantic_result_type = NULL;
14263 :
14264 : /* Nonzero means operands have already been type-converted
14265 : in whatever way is necessary.
14266 : Zero means they need to be converted to RESULT_TYPE. */
14267 16810833 : int converted = 0;
14268 :
14269 : /* Nonzero means create the expression with this type, rather than
14270 : RESULT_TYPE. */
14271 16810833 : tree build_type = NULL_TREE;
14272 :
14273 : /* Nonzero means after finally constructing the expression
14274 : convert it to this type. */
14275 16810833 : tree final_type = NULL_TREE;
14276 :
14277 : /* Nonzero if this is an operation like MIN or MAX which can
14278 : safely be computed in short if both args are promoted shorts.
14279 : Also implies COMMON.
14280 : -1 indicates a bitwise operation; this makes a difference
14281 : in the exact conditions for when it is safe to do the operation
14282 : in a narrower mode. */
14283 16810833 : int shorten = 0;
14284 :
14285 : /* Nonzero if this is a comparison operation;
14286 : if both args are promoted shorts, compare the original shorts.
14287 : Also implies COMMON. */
14288 16810833 : int short_compare = 0;
14289 :
14290 : /* Nonzero if this is a right-shift operation, which can be computed on the
14291 : original short and then promoted if the operand is a promoted short. */
14292 16810833 : int short_shift = 0;
14293 :
14294 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14295 16810833 : int common = 0;
14296 :
14297 : /* True means types are compatible as far as ObjC is concerned. */
14298 16810833 : bool objc_ok;
14299 :
14300 : /* True means this is an arithmetic operation that may need excess
14301 : precision. */
14302 16810833 : bool may_need_excess_precision;
14303 :
14304 : /* True means this is a boolean operation that converts both its
14305 : operands to truth-values. */
14306 16810833 : bool boolean_op = false;
14307 :
14308 : /* Remember whether we're doing / or %. */
14309 16810833 : bool doing_div_or_mod = false;
14310 :
14311 : /* Remember whether we're doing << or >>. */
14312 16810833 : bool doing_shift = false;
14313 :
14314 : /* Tree holding instrumentation expression. */
14315 16810833 : tree instrument_expr = NULL;
14316 :
14317 16810833 : if (location == UNKNOWN_LOCATION)
14318 79 : location = input_location;
14319 :
14320 16810833 : op0 = orig_op0;
14321 16810833 : op1 = orig_op1;
14322 :
14323 16810833 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14324 8123541 : if (op0_int_operands)
14325 8123541 : op0 = remove_c_maybe_const_expr (op0);
14326 16810833 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14327 11384901 : if (op1_int_operands)
14328 11384901 : op1 = remove_c_maybe_const_expr (op1);
14329 28195734 : int_operands = (op0_int_operands && op1_int_operands);
14330 11384901 : if (int_operands)
14331 : {
14332 15893659 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14333 7961996 : && TREE_CODE (orig_op1) == INTEGER_CST);
14334 7931663 : int_const = (int_const_or_overflow
14335 7931663 : && !TREE_OVERFLOW (orig_op0)
14336 7931594 : && !TREE_OVERFLOW (orig_op1));
14337 : }
14338 : else
14339 : int_const = int_const_or_overflow = false;
14340 :
14341 : /* Do not apply default conversion in mixed vector/scalar expression. */
14342 16810833 : if (convert_p
14343 16810833 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14344 : {
14345 10115586 : op0 = default_conversion (op0);
14346 10115586 : op1 = default_conversion (op1);
14347 : }
14348 :
14349 16810833 : orig_type0 = type0 = TREE_TYPE (op0);
14350 :
14351 16810833 : orig_type1 = type1 = TREE_TYPE (op1);
14352 :
14353 : /* The expression codes of the data types of the arguments tell us
14354 : whether the arguments are integers, floating, pointers, etc. */
14355 16810833 : code0 = TREE_CODE (type0);
14356 16810833 : code1 = TREE_CODE (type1);
14357 :
14358 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14359 16810835 : STRIP_TYPE_NOPS (op0);
14360 16810836 : STRIP_TYPE_NOPS (op1);
14361 :
14362 : /* If an error was already reported for one of the arguments,
14363 : avoid reporting another error. */
14364 :
14365 16810833 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14366 710 : return error_mark_node;
14367 :
14368 16810123 : if (code0 == POINTER_TYPE
14369 16810123 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14370 11 : return error_mark_node;
14371 :
14372 16810112 : if (code1 == POINTER_TYPE
14373 16810112 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14374 11 : return error_mark_node;
14375 :
14376 33620202 : if ((invalid_op_diag
14377 16810101 : = targetm.invalid_binary_op (code, type0, type1)))
14378 : {
14379 0 : error_at (location, invalid_op_diag);
14380 0 : return error_mark_node;
14381 : }
14382 :
14383 16810101 : switch (code)
14384 : {
14385 : case PLUS_EXPR:
14386 : case MINUS_EXPR:
14387 : case MULT_EXPR:
14388 : case TRUNC_DIV_EXPR:
14389 : case CEIL_DIV_EXPR:
14390 : case FLOOR_DIV_EXPR:
14391 : case ROUND_DIV_EXPR:
14392 : case EXACT_DIV_EXPR:
14393 : may_need_excess_precision = true;
14394 : break;
14395 :
14396 2729544 : case EQ_EXPR:
14397 2729544 : case NE_EXPR:
14398 2729544 : case LE_EXPR:
14399 2729544 : case GE_EXPR:
14400 2729544 : case LT_EXPR:
14401 2729544 : case GT_EXPR:
14402 : /* Excess precision for implicit conversions of integers to
14403 : floating point in C11 and later. */
14404 2729544 : may_need_excess_precision = (flag_isoc11
14405 2729544 : && (ANY_INTEGRAL_TYPE_P (type0)
14406 485735 : || ANY_INTEGRAL_TYPE_P (type1)));
14407 : break;
14408 :
14409 : default:
14410 16810101 : may_need_excess_precision = false;
14411 : break;
14412 : }
14413 16810101 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14414 : {
14415 184 : op0 = TREE_OPERAND (op0, 0);
14416 184 : type0 = TREE_TYPE (op0);
14417 : }
14418 16809917 : else if (may_need_excess_precision
14419 16809917 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14420 : {
14421 771 : type0 = eptype;
14422 771 : op0 = convert (eptype, op0);
14423 : }
14424 16810101 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14425 : {
14426 887 : op1 = TREE_OPERAND (op1, 0);
14427 887 : type1 = TREE_TYPE (op1);
14428 : }
14429 16809214 : else if (may_need_excess_precision
14430 16809214 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14431 : {
14432 592 : type1 = eptype;
14433 592 : op1 = convert (eptype, op1);
14434 : }
14435 :
14436 16810101 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14437 :
14438 : /* In case when one of the operands of the binary operation is
14439 : a vector and another is a scalar -- convert scalar to vector. */
14440 18389019 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14441 18387139 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14442 : {
14443 2486 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14444 : orig_op1, true);
14445 :
14446 2486 : switch (convert_flag)
14447 : {
14448 12 : case stv_error:
14449 12 : return error_mark_node;
14450 593 : case stv_firstarg:
14451 593 : {
14452 593 : bool maybe_const = true;
14453 593 : tree sc;
14454 593 : sc = c_fully_fold (op0, false, &maybe_const);
14455 593 : sc = save_expr (sc);
14456 593 : sc = convert (TREE_TYPE (type1), sc);
14457 593 : op0 = build_vector_from_val (type1, sc);
14458 593 : if (!maybe_const)
14459 93 : op0 = c_wrap_maybe_const (op0, true);
14460 593 : orig_type0 = type0 = TREE_TYPE (op0);
14461 593 : code0 = TREE_CODE (type0);
14462 593 : converted = 1;
14463 593 : break;
14464 : }
14465 1047 : case stv_secondarg:
14466 1047 : {
14467 1047 : bool maybe_const = true;
14468 1047 : tree sc;
14469 1047 : sc = c_fully_fold (op1, false, &maybe_const);
14470 1047 : sc = save_expr (sc);
14471 1047 : sc = convert (TREE_TYPE (type0), sc);
14472 1047 : op1 = build_vector_from_val (type0, sc);
14473 1047 : if (!maybe_const)
14474 37 : op1 = c_wrap_maybe_const (op1, true);
14475 1047 : orig_type1 = type1 = TREE_TYPE (op1);
14476 1047 : code1 = TREE_CODE (type1);
14477 1047 : converted = 1;
14478 1047 : break;
14479 : }
14480 : default:
14481 : break;
14482 : }
14483 : }
14484 :
14485 16810089 : switch (code)
14486 : {
14487 8617982 : case PLUS_EXPR:
14488 : /* Handle the pointer + int case. */
14489 8617982 : if (code0 == POINTER_TYPE
14490 1248934 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14491 : {
14492 1248928 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14493 1248928 : goto return_build_binary_op;
14494 : }
14495 7369054 : else if (code1 == POINTER_TYPE
14496 1538 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14497 : {
14498 1532 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14499 1532 : goto return_build_binary_op;
14500 : }
14501 : else
14502 : common = 1;
14503 : break;
14504 :
14505 790590 : case MINUS_EXPR:
14506 : /* Subtraction of two similar pointers.
14507 : We must subtract them as integers, then divide by object size. */
14508 790590 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14509 790590 : && comp_target_types (location, type0, type1))
14510 : {
14511 3681 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14512 3681 : goto return_build_binary_op;
14513 : }
14514 : /* Handle pointer minus int. Just like pointer plus int. */
14515 786909 : else if (code0 == POINTER_TYPE
14516 16818 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14517 : {
14518 16806 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14519 16806 : goto return_build_binary_op;
14520 : }
14521 : else
14522 : common = 1;
14523 : break;
14524 :
14525 : case MULT_EXPR:
14526 : common = 1;
14527 : break;
14528 :
14529 273619 : case TRUNC_DIV_EXPR:
14530 273619 : case CEIL_DIV_EXPR:
14531 273619 : case FLOOR_DIV_EXPR:
14532 273619 : case ROUND_DIV_EXPR:
14533 273619 : case EXACT_DIV_EXPR:
14534 273619 : doing_div_or_mod = true;
14535 273619 : warn_for_div_by_zero (location, op1);
14536 :
14537 273618 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14538 49594 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14539 47608 : || code0 == COMPLEX_TYPE
14540 45558 : || gnu_vector_type_p (type0))
14541 323211 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14542 47160 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14543 47021 : || code1 == COMPLEX_TYPE
14544 45560 : || gnu_vector_type_p (type1)))
14545 : {
14546 273614 : enum tree_code tcode0 = code0, tcode1 = code1;
14547 :
14548 273614 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14549 47607 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14550 273614 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14551 47018 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14552 :
14553 273614 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14554 69728 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14555 203887 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14556 12030 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14557 81756 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14558 : resultcode = RDIV_EXPR;
14559 : else
14560 : /* Although it would be tempting to shorten always here, that
14561 : loses on some targets, since the modulo instruction is
14562 : undefined if the quotient can't be represented in the
14563 : computation mode. We shorten only if unsigned or if
14564 : dividing by something we know != -1. */
14565 191858 : shorten = may_shorten_divmod (op0, op1);
14566 : common = 1;
14567 : }
14568 : break;
14569 :
14570 1584500 : case BIT_AND_EXPR:
14571 1584500 : case BIT_IOR_EXPR:
14572 1584500 : case BIT_XOR_EXPR:
14573 1584500 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14574 1049397 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14575 : shorten = -1;
14576 : /* Allow vector types which are not floating point types. */
14577 535139 : else if (gnu_vector_type_p (type0)
14578 535062 : && gnu_vector_type_p (type1)
14579 535062 : && !VECTOR_FLOAT_TYPE_P (type0)
14580 1070198 : && !VECTOR_FLOAT_TYPE_P (type1))
14581 : common = 1;
14582 : break;
14583 :
14584 58478 : case TRUNC_MOD_EXPR:
14585 58478 : case FLOOR_MOD_EXPR:
14586 58478 : doing_div_or_mod = true;
14587 58478 : warn_for_div_by_zero (location, op1);
14588 :
14589 58478 : if (gnu_vector_type_p (type0)
14590 297 : && gnu_vector_type_p (type1)
14591 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14592 58775 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14593 : common = 1;
14594 58181 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14595 58180 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14596 : {
14597 : /* Although it would be tempting to shorten always here, that loses
14598 : on some targets, since the modulo instruction is undefined if the
14599 : quotient can't be represented in the computation mode. We shorten
14600 : only if unsigned or if dividing by something we know != -1. */
14601 58180 : shorten = may_shorten_divmod (op0, op1);
14602 58180 : common = 1;
14603 : }
14604 : break;
14605 :
14606 508793 : case TRUTH_ANDIF_EXPR:
14607 508793 : case TRUTH_ORIF_EXPR:
14608 508793 : case TRUTH_AND_EXPR:
14609 508793 : case TRUTH_OR_EXPR:
14610 508793 : case TRUTH_XOR_EXPR:
14611 508793 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14612 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14613 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14614 0 : || code0 == BITINT_TYPE)
14615 508793 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14616 159 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14617 20 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14618 18 : || code1 == BITINT_TYPE))
14619 : {
14620 : /* Result of these operations is always an int,
14621 : but that does not mean the operands should be
14622 : converted to ints! */
14623 508793 : result_type = integer_type_node;
14624 508793 : if (op0_int_operands)
14625 : {
14626 109820 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14627 109820 : op0 = remove_c_maybe_const_expr (op0);
14628 : }
14629 : else
14630 398973 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14631 508793 : if (op1_int_operands)
14632 : {
14633 71210 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14634 71210 : op1 = remove_c_maybe_const_expr (op1);
14635 : }
14636 : else
14637 437583 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14638 : converted = 1;
14639 : boolean_op = true;
14640 : }
14641 508793 : if (code == TRUTH_ANDIF_EXPR)
14642 : {
14643 247647 : int_const_or_overflow = (int_operands
14644 63074 : && TREE_CODE (orig_op0) == INTEGER_CST
14645 247663 : && (op0 == truthvalue_false_node
14646 13052 : || TREE_CODE (orig_op1) == INTEGER_CST));
14647 : int_const = (int_const_or_overflow
14648 63047 : && !TREE_OVERFLOW (orig_op0)
14649 63047 : && (op0 == truthvalue_false_node
14650 13036 : || !TREE_OVERFLOW (orig_op1)));
14651 : }
14652 324193 : else if (code == TRUTH_ORIF_EXPR)
14653 : {
14654 330868 : int_const_or_overflow = (int_operands
14655 6826 : && TREE_CODE (orig_op0) == INTEGER_CST
14656 330891 : && (op0 == truthvalue_true_node
14657 2141 : || TREE_CODE (orig_op1) == INTEGER_CST));
14658 : int_const = (int_const_or_overflow
14659 6796 : && !TREE_OVERFLOW (orig_op0)
14660 6796 : && (op0 == truthvalue_true_node
14661 2118 : || !TREE_OVERFLOW (orig_op1)));
14662 : }
14663 : break;
14664 :
14665 : /* Shift operations: result has same type as first operand;
14666 : always convert second operand to int.
14667 : Also set SHORT_SHIFT if shifting rightward. */
14668 :
14669 231135 : case RSHIFT_EXPR:
14670 231135 : if (gnu_vector_type_p (type0)
14671 807 : && gnu_vector_type_p (type1)
14672 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14673 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14674 231391 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14675 : TYPE_VECTOR_SUBPARTS (type1)))
14676 : {
14677 : result_type = type0;
14678 : converted = 1;
14679 : }
14680 230881 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14681 1089 : || code0 == BITINT_TYPE
14682 557 : || (gnu_vector_type_p (type0)
14683 553 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14684 231963 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14685 : {
14686 230872 : doing_shift = true;
14687 230872 : if (TREE_CODE (op1) == INTEGER_CST)
14688 : {
14689 201485 : if (tree_int_cst_sgn (op1) < 0)
14690 : {
14691 269 : int_const = false;
14692 269 : if (c_inhibit_evaluation_warnings == 0)
14693 129 : warning_at (location, OPT_Wshift_count_negative,
14694 : "right shift count is negative");
14695 : }
14696 201216 : else if (code0 == VECTOR_TYPE)
14697 : {
14698 458 : if (compare_tree_int (op1,
14699 458 : TYPE_PRECISION (TREE_TYPE (type0)))
14700 : >= 0)
14701 : {
14702 16 : int_const = false;
14703 16 : if (c_inhibit_evaluation_warnings == 0)
14704 16 : warning_at (location, OPT_Wshift_count_overflow,
14705 : "right shift count >= width of vector element");
14706 : }
14707 : }
14708 : else
14709 : {
14710 200758 : if (!integer_zerop (op1))
14711 200400 : short_shift = 1;
14712 :
14713 200758 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14714 : {
14715 584 : int_const = false;
14716 584 : if (c_inhibit_evaluation_warnings == 0)
14717 50 : warning_at (location, OPT_Wshift_count_overflow,
14718 : "right shift count >= width of type");
14719 : }
14720 : }
14721 : }
14722 :
14723 : /* Use the type of the value to be shifted. */
14724 : result_type = type0;
14725 : /* Avoid converting op1 to result_type later. */
14726 : converted = 1;
14727 : }
14728 : break;
14729 :
14730 802749 : case LSHIFT_EXPR:
14731 802749 : if (gnu_vector_type_p (type0)
14732 595 : && gnu_vector_type_p (type1)
14733 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14734 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14735 803062 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14736 : TYPE_VECTOR_SUBPARTS (type1)))
14737 : {
14738 : result_type = type0;
14739 : converted = 1;
14740 : }
14741 802438 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14742 760 : || code0 == BITINT_TYPE
14743 290 : || (gnu_vector_type_p (type0)
14744 284 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14745 803189 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14746 : {
14747 802424 : doing_shift = true;
14748 802424 : if (TREE_CODE (op0) == INTEGER_CST
14749 438429 : && tree_int_cst_sgn (op0) < 0
14750 803173 : && !TYPE_OVERFLOW_WRAPS (type0))
14751 : {
14752 : /* Don't reject a left shift of a negative value in a context
14753 : where a constant expression is needed in C90. */
14754 712 : if (flag_isoc99)
14755 666 : int_const = false;
14756 712 : if (c_inhibit_evaluation_warnings == 0)
14757 712 : warning_at (location, OPT_Wshift_negative_value,
14758 : "left shift of negative value");
14759 : }
14760 802424 : if (TREE_CODE (op1) == INTEGER_CST)
14761 : {
14762 730018 : if (tree_int_cst_sgn (op1) < 0)
14763 : {
14764 313 : int_const = false;
14765 313 : if (c_inhibit_evaluation_warnings == 0)
14766 135 : warning_at (location, OPT_Wshift_count_negative,
14767 : "left shift count is negative");
14768 : }
14769 729705 : else if (code0 == VECTOR_TYPE)
14770 : {
14771 215 : if (compare_tree_int (op1,
14772 215 : TYPE_PRECISION (TREE_TYPE (type0)))
14773 : >= 0)
14774 : {
14775 6 : int_const = false;
14776 6 : if (c_inhibit_evaluation_warnings == 0)
14777 6 : warning_at (location, OPT_Wshift_count_overflow,
14778 : "left shift count >= width of vector element");
14779 : }
14780 : }
14781 729490 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14782 : {
14783 29733 : int_const = false;
14784 29733 : if (c_inhibit_evaluation_warnings == 0)
14785 113 : warning_at (location, OPT_Wshift_count_overflow,
14786 : "left shift count >= width of type");
14787 : }
14788 699757 : else if (TREE_CODE (op0) == INTEGER_CST
14789 364663 : && maybe_warn_shift_overflow (location, op0, op1)
14790 700385 : && flag_isoc99)
14791 : int_const = false;
14792 : }
14793 :
14794 : /* Use the type of the value to be shifted. */
14795 : result_type = type0;
14796 : /* Avoid converting op1 to result_type later. */
14797 : converted = 1;
14798 : }
14799 : break;
14800 :
14801 1733305 : case EQ_EXPR:
14802 1733305 : case NE_EXPR:
14803 1733305 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14804 : {
14805 41855 : tree intt;
14806 41855 : if (!vector_types_compatible_elements_p (type0, type1))
14807 : {
14808 4 : error_at (location, "comparing vectors with different "
14809 : "element types");
14810 4 : return error_mark_node;
14811 : }
14812 :
14813 41851 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14814 83702 : TYPE_VECTOR_SUBPARTS (type1)))
14815 : {
14816 1 : error_at (location, "comparing vectors with different "
14817 : "number of elements");
14818 1 : return error_mark_node;
14819 : }
14820 :
14821 : /* It's not precisely specified how the usual arithmetic
14822 : conversions apply to the vector types. Here, we use
14823 : the unsigned type if one of the operands is signed and
14824 : the other one is unsigned. */
14825 41850 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14826 : {
14827 4 : if (!TYPE_UNSIGNED (type0))
14828 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14829 : else
14830 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14831 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14832 : "types %qT and %qT", type0, type1);
14833 : }
14834 :
14835 : /* Always construct signed integer vector type. */
14836 41850 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14837 : result_type = type0;
14838 : else
14839 : {
14840 41850 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14841 :
14842 125550 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14843 83700 : (SCALAR_TYPE_MODE
14844 : (TREE_TYPE (type0))), 0);
14845 41850 : if (!intt)
14846 : {
14847 0 : error_at (location, "could not find an integer type "
14848 : "of the same size as %qT",
14849 0 : TREE_TYPE (type0));
14850 0 : return error_mark_node;
14851 : }
14852 41850 : result_type = build_opaque_vector_type (intt, nelts);
14853 : }
14854 41850 : converted = 1;
14855 41850 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14856 41850 : goto return_build_binary_op;
14857 : }
14858 1691450 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14859 252752 : warning_at (location,
14860 252752 : OPT_Wfloat_equal,
14861 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14862 : /* Result of comparison is always int,
14863 : but don't convert the args to int! */
14864 1691450 : build_type = integer_type_node;
14865 1691450 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14866 156395 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14867 1570367 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14868 : || code1 == BITINT_TYPE
14869 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14870 : short_compare = 1;
14871 121349 : else if (code0 == POINTER_TYPE
14872 121349 : && (code1 == NULLPTR_TYPE
14873 120972 : || null_pointer_constant_p (orig_op1)))
14874 : {
14875 51399 : maybe_warn_for_null_address (location, op0, code);
14876 51399 : result_type = type0;
14877 : }
14878 69950 : else if (code1 == POINTER_TYPE
14879 69950 : && (code0 == NULLPTR_TYPE
14880 69848 : || null_pointer_constant_p (orig_op0)))
14881 : {
14882 288 : maybe_warn_for_null_address (location, op1, code);
14883 288 : result_type = type1;
14884 : }
14885 69662 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14886 : {
14887 69572 : tree tt0 = TREE_TYPE (type0);
14888 69572 : tree tt1 = TREE_TYPE (type1);
14889 69572 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14890 69572 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14891 69572 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14892 :
14893 : /* Anything compares with void *. void * compares with anything.
14894 : Otherwise, the targets must be compatible
14895 : and both must be object or both incomplete. */
14896 69572 : if (comp_target_types (location, type0, type1))
14897 48277 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14898 21295 : else if (!addr_space_superset (as0, as1, &as_common))
14899 : {
14900 0 : error_at (location, "comparison of pointers to "
14901 : "disjoint address spaces");
14902 0 : return error_mark_node;
14903 : }
14904 21295 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14905 : {
14906 21086 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14907 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14908 : "comparison of %<void *%> with function pointer");
14909 : }
14910 209 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14911 : {
14912 161 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14913 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14914 : "comparison of %<void *%> with function pointer");
14915 : }
14916 : else
14917 : /* Avoid warning about the volatile ObjC EH puts on decls. */
14918 48 : if (!objc_ok)
14919 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14920 : "comparison of distinct pointer types lacks a cast");
14921 :
14922 48333 : if (result_type == NULL_TREE)
14923 : {
14924 21295 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14925 21295 : result_type = c_build_pointer_type
14926 21295 : (c_build_qualified_type (void_type_node, qual));
14927 : }
14928 : }
14929 90 : else if (code0 == POINTER_TYPE
14930 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14931 : {
14932 26 : result_type = type0;
14933 26 : pedwarn (location, 0, "comparison between pointer and integer");
14934 : }
14935 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14936 21 : && code1 == POINTER_TYPE)
14937 : {
14938 8 : result_type = type1;
14939 8 : pedwarn (location, 0, "comparison between pointer and integer");
14940 : }
14941 : /* 6.5.9: One of the following shall hold:
14942 : -- both operands have type nullptr_t; */
14943 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14944 : {
14945 22 : result_type = nullptr_type_node;
14946 : /* No need to convert the operands to result_type later. */
14947 22 : converted = 1;
14948 : }
14949 : /* -- one operand has type nullptr_t and the other is a null pointer
14950 : constant. We will have to convert the former to the type of the
14951 : latter, because during gimplification we can't have mismatching
14952 : comparison operand type. We convert from nullptr_t to the other
14953 : type, since only nullptr_t can be converted to nullptr_t. Also,
14954 : even a constant 0 is a null pointer constant, so we may have to
14955 : create a pointer type from its type. */
14956 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14957 19 : result_type = (INTEGRAL_TYPE_P (type1)
14958 19 : ? c_build_pointer_type (type1) : type1);
14959 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14960 11 : result_type = (INTEGRAL_TYPE_P (type0)
14961 11 : ? c_build_pointer_type (type0) : type0);
14962 5000020 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14963 3308545 : || truth_value_p (TREE_CODE (orig_op0)))
14964 3375623 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14965 3375619 : || truth_value_p (TREE_CODE (orig_op1))))
14966 71374 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14967 : break;
14968 :
14969 996235 : case LE_EXPR:
14970 996235 : case GE_EXPR:
14971 996235 : case LT_EXPR:
14972 996235 : case GT_EXPR:
14973 996235 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14974 : {
14975 59099 : tree intt;
14976 59099 : if (!vector_types_compatible_elements_p (type0, type1))
14977 : {
14978 5 : error_at (location, "comparing vectors with different "
14979 : "element types");
14980 5 : return error_mark_node;
14981 : }
14982 :
14983 59094 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14984 118188 : TYPE_VECTOR_SUBPARTS (type1)))
14985 : {
14986 0 : error_at (location, "comparing vectors with different "
14987 : "number of elements");
14988 0 : return error_mark_node;
14989 : }
14990 :
14991 : /* It's not precisely specified how the usual arithmetic
14992 : conversions apply to the vector types. Here, we use
14993 : the unsigned type if one of the operands is signed and
14994 : the other one is unsigned. */
14995 59094 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14996 : {
14997 15 : if (!TYPE_UNSIGNED (type0))
14998 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14999 : else
15000 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15001 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15002 : "types %qT and %qT", type0, type1);
15003 : }
15004 :
15005 : /* Always construct signed integer vector type. */
15006 177282 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15007 118188 : (SCALAR_TYPE_MODE
15008 : (TREE_TYPE (type0))), 0);
15009 59094 : if (!intt)
15010 : {
15011 0 : error_at (location, "could not find an integer type "
15012 : "of the same size as %qT",
15013 0 : TREE_TYPE (type0));
15014 0 : return error_mark_node;
15015 : }
15016 59094 : result_type = build_opaque_vector_type (intt,
15017 59094 : TYPE_VECTOR_SUBPARTS (type0));
15018 59094 : converted = 1;
15019 59094 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15020 59094 : goto return_build_binary_op;
15021 : }
15022 937136 : build_type = integer_type_node;
15023 937136 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15024 64699 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15025 872983 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15026 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15027 : short_compare = 1;
15028 64185 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15029 : {
15030 64118 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15031 64118 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15032 64118 : addr_space_t as_common;
15033 :
15034 64118 : if (comp_target_types (location, type0, type1))
15035 : {
15036 64005 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15037 64005 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15038 64005 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15039 32 : pedwarn_c99 (location, OPT_Wpedantic,
15040 : "comparison of complete and incomplete pointers");
15041 63973 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15042 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15043 : "ordered comparisons of pointers to functions");
15044 63973 : else if (null_pointer_constant_p (orig_op0)
15045 63973 : || null_pointer_constant_p (orig_op1))
15046 8 : warning_at (location, OPT_Wextra,
15047 : "ordered comparison of pointer with null pointer");
15048 :
15049 : }
15050 113 : else if (!addr_space_superset (as0, as1, &as_common))
15051 : {
15052 0 : error_at (location, "comparison of pointers to "
15053 : "disjoint address spaces");
15054 0 : return error_mark_node;
15055 : }
15056 : else
15057 : {
15058 113 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
15059 113 : result_type = c_build_pointer_type
15060 113 : (c_build_qualified_type (void_type_node, qual));
15061 113 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15062 : "comparison of distinct pointer types lacks a cast");
15063 : }
15064 : }
15065 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15066 : {
15067 17 : result_type = type0;
15068 17 : if (pedantic)
15069 11 : pedwarn (location, OPT_Wpedantic,
15070 : "ordered comparison of pointer with integer zero");
15071 6 : else if (extra_warnings)
15072 1 : warning_at (location, OPT_Wextra,
15073 : "ordered comparison of pointer with integer zero");
15074 : }
15075 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15076 : {
15077 12 : result_type = type1;
15078 12 : if (pedantic)
15079 2 : pedwarn (location, OPT_Wpedantic,
15080 : "ordered comparison of pointer with integer zero");
15081 10 : else if (extra_warnings)
15082 1 : warning_at (location, OPT_Wextra,
15083 : "ordered comparison of pointer with integer zero");
15084 : }
15085 38 : else if (code0 == POINTER_TYPE
15086 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15087 : {
15088 18 : result_type = type0;
15089 18 : pedwarn (location, 0, "comparison between pointer and integer");
15090 : }
15091 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15092 20 : && code1 == POINTER_TYPE)
15093 : {
15094 18 : result_type = type1;
15095 18 : pedwarn (location, 0, "comparison between pointer and integer");
15096 : }
15097 :
15098 937136 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15099 64183 : && current_function_decl != NULL_TREE
15100 1001308 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15101 : {
15102 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15103 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15104 :
15105 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15106 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15107 : }
15108 :
15109 2811316 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15110 1874180 : || truth_value_p (TREE_CODE (orig_op0)))
15111 1874197 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15112 1874197 : || truth_value_p (TREE_CODE (orig_op1))))
15113 511 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15114 : break;
15115 :
15116 43 : case MIN_EXPR:
15117 43 : case MAX_EXPR:
15118 : /* Used for OpenMP atomics. */
15119 43 : gcc_assert (flag_openmp);
15120 : common = 1;
15121 : break;
15122 :
15123 0 : default:
15124 0 : gcc_unreachable ();
15125 : }
15126 :
15127 15438187 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15128 0 : return error_mark_node;
15129 :
15130 15438187 : if (gnu_vector_type_p (type0)
15131 1478553 : && gnu_vector_type_p (type1)
15132 16915911 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15133 1477704 : || !vector_types_compatible_elements_p (type0, type1)))
15134 : {
15135 26 : gcc_rich_location richloc (location);
15136 26 : maybe_range_label_for_tree_type_mismatch
15137 26 : label_for_op0 (orig_op0, orig_op1),
15138 26 : label_for_op1 (orig_op1, orig_op0);
15139 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15140 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15141 26 : binary_op_error (&richloc, code, type0, type1);
15142 26 : return error_mark_node;
15143 26 : }
15144 :
15145 15438161 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15146 1732013 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15147 1664178 : || gnu_vector_type_p (type0))
15148 16984523 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15149 1571187 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15150 1479807 : || gnu_vector_type_p (type1)))
15151 : {
15152 15250406 : bool first_complex = (code0 == COMPLEX_TYPE);
15153 15250406 : bool second_complex = (code1 == COMPLEX_TYPE);
15154 15250406 : int none_complex = (!first_complex && !second_complex);
15155 :
15156 15250406 : if (shorten || common || short_compare)
15157 : {
15158 13709467 : result_type = c_common_type (type0, type1);
15159 13709467 : do_warn_double_promotion (result_type, type0, type1,
15160 : "implicit conversion from %qT to %qT "
15161 : "to match other operand of binary "
15162 : "expression",
15163 : location);
15164 13709467 : if (result_type == error_mark_node)
15165 : return error_mark_node;
15166 : }
15167 :
15168 15250384 : if (first_complex != second_complex
15169 82043 : && (code == PLUS_EXPR
15170 : || code == MINUS_EXPR
15171 82043 : || code == MULT_EXPR
15172 17525 : || (code == TRUNC_DIV_EXPR && first_complex))
15173 65932 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15174 15312638 : && flag_signed_zeros)
15175 : {
15176 : /* An operation on mixed real/complex operands must be
15177 : handled specially, but the language-independent code can
15178 : more easily optimize the plain complex arithmetic if
15179 : -fno-signed-zeros. */
15180 61942 : tree real_type = TREE_TYPE (result_type);
15181 61942 : tree real, imag;
15182 61942 : if (type0 != orig_type0 || type1 != orig_type1)
15183 : {
15184 94 : gcc_assert (may_need_excess_precision && common);
15185 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15186 : }
15187 61942 : if (first_complex)
15188 : {
15189 8253 : if (TREE_TYPE (op0) != result_type)
15190 1787 : op0 = convert_and_check (location, result_type, op0);
15191 8253 : if (TREE_TYPE (op1) != real_type)
15192 4601 : op1 = convert_and_check (location, real_type, op1);
15193 : }
15194 : else
15195 : {
15196 53689 : if (TREE_TYPE (op0) != real_type)
15197 2698 : op0 = convert_and_check (location, real_type, op0);
15198 53689 : if (TREE_TYPE (op1) != result_type)
15199 1866 : op1 = convert_and_check (location, result_type, op1);
15200 : }
15201 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15202 0 : return error_mark_node;
15203 61942 : if (first_complex)
15204 : {
15205 8253 : op0 = save_expr (op0);
15206 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15207 : op0, true);
15208 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15209 : op0, true);
15210 8253 : switch (code)
15211 : {
15212 3488 : case MULT_EXPR:
15213 3488 : case TRUNC_DIV_EXPR:
15214 3488 : op1 = save_expr (op1);
15215 3488 : imag = build2 (resultcode, real_type, imag, op1);
15216 : /* Fall through. */
15217 8253 : case PLUS_EXPR:
15218 8253 : case MINUS_EXPR:
15219 8253 : real = build2 (resultcode, real_type, real, op1);
15220 8253 : break;
15221 0 : default:
15222 0 : gcc_unreachable();
15223 : }
15224 : }
15225 : else
15226 : {
15227 53689 : op1 = save_expr (op1);
15228 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15229 : op1, true);
15230 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15231 : op1, true);
15232 53689 : switch (code)
15233 : {
15234 2008 : case MULT_EXPR:
15235 2008 : op0 = save_expr (op0);
15236 2008 : imag = build2 (resultcode, real_type, op0, imag);
15237 : /* Fall through. */
15238 31957 : case PLUS_EXPR:
15239 31957 : real = build2 (resultcode, real_type, op0, real);
15240 31957 : break;
15241 21732 : case MINUS_EXPR:
15242 21732 : real = build2 (resultcode, real_type, op0, real);
15243 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15244 21732 : break;
15245 0 : default:
15246 0 : gcc_unreachable();
15247 : }
15248 : }
15249 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15250 61942 : goto return_build_binary_op;
15251 : }
15252 :
15253 : /* For certain operations (which identify themselves by shorten != 0)
15254 : if both args were extended from the same smaller type,
15255 : do the arithmetic in that type and then extend.
15256 :
15257 : shorten !=0 and !=1 indicates a bitwise operation.
15258 : For them, this optimization is safe only if
15259 : both args are zero-extended or both are sign-extended.
15260 : Otherwise, we might change the result.
15261 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15262 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15263 :
15264 15188442 : if (shorten && none_complex)
15265 : {
15266 1292744 : final_type = result_type;
15267 1292744 : result_type = shorten_binary_op (result_type, op0, op1,
15268 : shorten == -1);
15269 : }
15270 :
15271 : /* Shifts can be shortened if shifting right. */
15272 :
15273 15188442 : if (short_shift)
15274 : {
15275 200400 : int unsigned_arg;
15276 200400 : tree arg0 = get_narrower (op0, &unsigned_arg);
15277 :
15278 200400 : final_type = result_type;
15279 :
15280 200400 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15281 140954 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15282 :
15283 200400 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15284 2989 : && tree_int_cst_sgn (op1) > 0
15285 : /* We can shorten only if the shift count is less than the
15286 : number of bits in the smaller type size. */
15287 2989 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15288 : /* We cannot drop an unsigned shift after sign-extension. */
15289 203249 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15290 : {
15291 : /* Do an unsigned shift if the operand was zero-extended. */
15292 2835 : result_type
15293 2835 : = c_common_signed_or_unsigned_type (unsigned_arg,
15294 2835 : TREE_TYPE (arg0));
15295 : /* Convert value-to-be-shifted to that type. */
15296 2835 : if (TREE_TYPE (op0) != result_type)
15297 2835 : op0 = convert (result_type, op0);
15298 : converted = 1;
15299 : }
15300 : }
15301 :
15302 : /* Comparison operations are shortened too but differently.
15303 : They identify themselves by setting short_compare = 1. */
15304 :
15305 15188442 : if (short_compare)
15306 : {
15307 : /* Don't write &op0, etc., because that would prevent op0
15308 : from being kept in a register.
15309 : Instead, make copies of the our local variables and
15310 : pass the copies by reference, then copy them back afterward. */
15311 2443050 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15312 2443050 : enum tree_code xresultcode = resultcode;
15313 2443050 : tree val
15314 2443050 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15315 : &xresultcode);
15316 :
15317 2443050 : if (val != NULL_TREE)
15318 : {
15319 15787 : ret = val;
15320 15787 : goto return_build_binary_op;
15321 : }
15322 :
15323 2427263 : op0 = xop0, op1 = xop1;
15324 2427263 : converted = 1;
15325 2427263 : resultcode = xresultcode;
15326 :
15327 2427263 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15328 : {
15329 2279660 : bool op0_maybe_const = true;
15330 2279660 : bool op1_maybe_const = true;
15331 2279660 : tree orig_op0_folded, orig_op1_folded;
15332 :
15333 2279660 : if (in_late_binary_op)
15334 : {
15335 : orig_op0_folded = orig_op0;
15336 : orig_op1_folded = orig_op1;
15337 : }
15338 : else
15339 : {
15340 : /* Fold for the sake of possible warnings, as in
15341 : build_conditional_expr. This requires the
15342 : "original" values to be folded, not just op0 and
15343 : op1. */
15344 2271282 : c_inhibit_evaluation_warnings++;
15345 2271282 : op0 = c_fully_fold (op0, require_constant_value,
15346 : &op0_maybe_const);
15347 2271282 : op1 = c_fully_fold (op1, require_constant_value,
15348 : &op1_maybe_const);
15349 2271282 : c_inhibit_evaluation_warnings--;
15350 2271282 : orig_op0_folded = c_fully_fold (orig_op0,
15351 : require_constant_value,
15352 : NULL);
15353 2271282 : orig_op1_folded = c_fully_fold (orig_op1,
15354 : require_constant_value,
15355 : NULL);
15356 : }
15357 :
15358 2279660 : if (warn_sign_compare)
15359 373474 : warn_for_sign_compare (location, orig_op0_folded,
15360 : orig_op1_folded, op0, op1,
15361 : result_type, resultcode);
15362 2279660 : if (!in_late_binary_op && !int_operands)
15363 : {
15364 2095461 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15365 2091939 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15366 2095461 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15367 825127 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15368 : }
15369 : }
15370 : }
15371 : }
15372 :
15373 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15374 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15375 : Then the expression will be built.
15376 : It will be given type FINAL_TYPE if that is nonzero;
15377 : otherwise, it will be given type RESULT_TYPE. */
15378 :
15379 15360410 : if (!result_type)
15380 : {
15381 : /* Favor showing any expression locations that are available. */
15382 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15383 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15384 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15385 514 : return error_mark_node;
15386 514 : }
15387 :
15388 15359896 : if (build_type == NULL_TREE)
15389 : {
15390 12747105 : build_type = result_type;
15391 12747105 : if ((type0 != orig_type0 || type1 != orig_type1)
15392 797 : && !boolean_op)
15393 : {
15394 797 : gcc_assert (may_need_excess_precision && common);
15395 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15396 : }
15397 : }
15398 :
15399 15359896 : if (!converted)
15400 : {
15401 11388734 : op0 = ep_convert_and_check (location, result_type, op0,
15402 : semantic_result_type);
15403 11388734 : op1 = ep_convert_and_check (location, result_type, op1,
15404 : semantic_result_type);
15405 :
15406 : /* This can happen if one operand has a vector type, and the other
15407 : has a different type. */
15408 11388734 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15409 3 : return error_mark_node;
15410 : }
15411 :
15412 15359893 : if (sanitize_flags_p ((SANITIZE_SHIFT
15413 : | SANITIZE_DIVIDE
15414 : | SANITIZE_FLOAT_DIVIDE
15415 : | SANITIZE_SI_OVERFLOW))
15416 23309 : && current_function_decl != NULL_TREE
15417 19846 : && (doing_div_or_mod || doing_shift)
15418 15362818 : && !require_constant_value)
15419 : {
15420 : /* OP0 and/or OP1 might have side-effects. */
15421 2869 : op0 = save_expr (op0);
15422 2869 : op1 = save_expr (op1);
15423 2869 : op0 = c_fully_fold (op0, false, NULL);
15424 2869 : op1 = c_fully_fold (op1, false, NULL);
15425 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15426 : | SANITIZE_FLOAT_DIVIDE
15427 : | SANITIZE_SI_OVERFLOW))))
15428 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15429 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15430 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15431 : }
15432 :
15433 : /* Treat expressions in initializers specially as they can't trap. */
15434 15359893 : if (int_const_or_overflow)
15435 7916234 : ret = (require_constant_value
15436 7916234 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15437 : op0, op1)
15438 7870112 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15439 : else
15440 7443659 : ret = build2 (resultcode, build_type, op0, op1);
15441 15359893 : if (final_type != NULL_TREE)
15442 1493144 : ret = convert (final_type, ret);
15443 :
15444 13866749 : return_build_binary_op:
15445 16809513 : gcc_assert (ret != error_mark_node);
15446 16809513 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15447 1750 : ret = (int_operands
15448 1750 : ? note_integer_operands (ret)
15449 430 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15450 16807763 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15451 60300 : && !in_late_binary_op)
15452 60300 : ret = note_integer_operands (ret);
15453 16809513 : protected_set_expr_location (ret, location);
15454 :
15455 16809513 : if (instrument_expr != NULL)
15456 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15457 : instrument_expr, ret);
15458 :
15459 16809513 : if (semantic_result_type)
15460 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15461 : semantic_result_type, ret);
15462 :
15463 : return ret;
15464 : }
15465 :
15466 :
15467 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15468 : purpose. LOCATION is the source location for the expression. */
15469 :
15470 : tree
15471 4115395 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15472 : {
15473 4115395 : bool int_const, int_operands;
15474 :
15475 4115395 : switch (TREE_CODE (TREE_TYPE (expr)))
15476 : {
15477 72 : case ARRAY_TYPE:
15478 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15479 72 : return error_mark_node;
15480 :
15481 86 : case RECORD_TYPE:
15482 86 : error_at (location, "used struct type value where scalar is required");
15483 86 : return error_mark_node;
15484 :
15485 83 : case UNION_TYPE:
15486 83 : error_at (location, "used union type value where scalar is required");
15487 83 : return error_mark_node;
15488 :
15489 22 : case VOID_TYPE:
15490 22 : error_at (location, "void value not ignored as it ought to be");
15491 22 : return error_mark_node;
15492 :
15493 30599 : case POINTER_TYPE:
15494 30599 : if (reject_gcc_builtin (expr))
15495 3 : return error_mark_node;
15496 : break;
15497 :
15498 0 : case FUNCTION_TYPE:
15499 0 : gcc_unreachable ();
15500 :
15501 8 : case VECTOR_TYPE:
15502 8 : error_at (location, "used vector type where scalar is required");
15503 8 : return error_mark_node;
15504 :
15505 : default:
15506 : break;
15507 : }
15508 :
15509 : /* Conversion of a floating constant to boolean goes through here
15510 : and yields an integer constant expression. Otherwise, the result
15511 : is only an integer constant expression if the argument is. */
15512 893606 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15513 4115197 : || ((TREE_CODE (expr) == REAL_CST
15514 3221217 : || TREE_CODE (expr) == COMPLEX_CST)
15515 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15516 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15517 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15518 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15519 : == BOOLEAN_TYPE)))));
15520 4115121 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15521 893766 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15522 : {
15523 319 : expr = remove_c_maybe_const_expr (expr);
15524 319 : expr = build2 (NE_EXPR, type, expr,
15525 319 : convert (TREE_TYPE (expr), integer_zero_node));
15526 319 : expr = note_integer_operands (expr);
15527 : }
15528 : else
15529 : {
15530 : /* ??? Should we also give an error for vectors rather than leaving
15531 : those to give errors later? */
15532 4114802 : expr = c_common_truthvalue_conversion (location, expr);
15533 4114802 : expr = fold_convert_loc (location, type, expr);
15534 : }
15535 :
15536 4115121 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15537 : {
15538 76 : if (TREE_OVERFLOW (expr))
15539 : return expr;
15540 : else
15541 76 : return note_integer_operands (expr);
15542 : }
15543 4115045 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15544 754 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15545 : return expr;
15546 : }
15547 :
15548 :
15549 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15550 : required. */
15551 :
15552 : tree
15553 119233153 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15554 : {
15555 119233153 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15556 : {
15557 699 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15558 : /* Executing a compound literal inside a function reinitializes
15559 : it. */
15560 699 : if (!TREE_STATIC (decl))
15561 433 : *se = true;
15562 699 : return decl;
15563 : }
15564 : else
15565 : return expr;
15566 : }
15567 :
15568 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15569 : statement. LOC is the location of the construct. */
15570 :
15571 : tree
15572 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15573 : tree clauses)
15574 : {
15575 2154 : body = c_end_compound_stmt (loc, body, true);
15576 :
15577 2154 : tree stmt = make_node (code);
15578 2154 : TREE_TYPE (stmt) = void_type_node;
15579 2154 : OMP_BODY (stmt) = body;
15580 2154 : OMP_CLAUSES (stmt) = clauses;
15581 2154 : SET_EXPR_LOCATION (stmt, loc);
15582 :
15583 2154 : return add_stmt (stmt);
15584 : }
15585 :
15586 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15587 : statement. LOC is the location of the OACC_DATA. */
15588 :
15589 : tree
15590 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15591 : {
15592 497 : tree stmt;
15593 :
15594 497 : block = c_end_compound_stmt (loc, block, true);
15595 :
15596 497 : stmt = make_node (OACC_DATA);
15597 497 : TREE_TYPE (stmt) = void_type_node;
15598 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15599 497 : OACC_DATA_BODY (stmt) = block;
15600 497 : SET_EXPR_LOCATION (stmt, loc);
15601 :
15602 497 : return add_stmt (stmt);
15603 : }
15604 :
15605 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15606 : statement. LOC is the location of the OACC_HOST_DATA. */
15607 :
15608 : tree
15609 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15610 : {
15611 21 : tree stmt;
15612 :
15613 21 : block = c_end_compound_stmt (loc, block, true);
15614 :
15615 21 : stmt = make_node (OACC_HOST_DATA);
15616 21 : TREE_TYPE (stmt) = void_type_node;
15617 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15618 21 : OACC_HOST_DATA_BODY (stmt) = block;
15619 21 : SET_EXPR_LOCATION (stmt, loc);
15620 :
15621 21 : return add_stmt (stmt);
15622 : }
15623 :
15624 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15625 :
15626 : tree
15627 12003 : c_begin_omp_parallel (void)
15628 : {
15629 12003 : tree block;
15630 :
15631 12003 : keep_next_level ();
15632 12003 : block = c_begin_compound_stmt (true);
15633 :
15634 12003 : return block;
15635 : }
15636 :
15637 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15638 : statement. LOC is the location of the OMP_PARALLEL. */
15639 :
15640 : tree
15641 5677 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15642 : {
15643 5677 : tree stmt;
15644 :
15645 5677 : block = c_end_compound_stmt (loc, block, true);
15646 :
15647 5677 : stmt = make_node (OMP_PARALLEL);
15648 5677 : TREE_TYPE (stmt) = void_type_node;
15649 5677 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15650 5677 : OMP_PARALLEL_BODY (stmt) = block;
15651 5677 : SET_EXPR_LOCATION (stmt, loc);
15652 :
15653 5677 : return add_stmt (stmt);
15654 : }
15655 :
15656 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15657 :
15658 : tree
15659 898 : c_begin_omp_task (void)
15660 : {
15661 898 : tree block;
15662 :
15663 898 : keep_next_level ();
15664 898 : block = c_begin_compound_stmt (true);
15665 :
15666 898 : return block;
15667 : }
15668 :
15669 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15670 : statement. LOC is the location of the #pragma. */
15671 :
15672 : tree
15673 898 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15674 : {
15675 898 : tree stmt;
15676 :
15677 898 : block = c_end_compound_stmt (loc, block, true);
15678 :
15679 898 : stmt = make_node (OMP_TASK);
15680 898 : TREE_TYPE (stmt) = void_type_node;
15681 898 : OMP_TASK_CLAUSES (stmt) = clauses;
15682 898 : OMP_TASK_BODY (stmt) = block;
15683 898 : SET_EXPR_LOCATION (stmt, loc);
15684 :
15685 898 : return add_stmt (stmt);
15686 : }
15687 :
15688 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15689 :
15690 : void
15691 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15692 : {
15693 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15694 213 : int mask = 0;
15695 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15696 : mask = 1;
15697 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15698 : mask = 2;
15699 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15700 : mask = 4;
15701 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15702 : mask = 8;
15703 : else
15704 : {
15705 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15706 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15707 : "clauses");
15708 0 : return;
15709 : }
15710 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15711 213 : if (ifc != NULL_TREE)
15712 : {
15713 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15714 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15715 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15716 : "expected %<cancel%> %<if%> clause modifier");
15717 : else
15718 : {
15719 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15720 29 : if (ifc2 != NULL_TREE)
15721 : {
15722 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15723 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15724 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15725 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15726 : "expected %<cancel%> %<if%> clause modifier");
15727 : }
15728 : }
15729 :
15730 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15731 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15732 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15733 : build_zero_cst (type));
15734 : }
15735 : else
15736 182 : ifc = boolean_true_node;
15737 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15738 213 : build_int_cst (integer_type_node, mask),
15739 : ifc);
15740 213 : add_stmt (stmt);
15741 : }
15742 :
15743 : /* Generate GOMP_cancellation_point call for
15744 : #pragma omp cancellation point. */
15745 :
15746 : void
15747 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15748 : {
15749 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15750 167 : int mask = 0;
15751 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15752 : mask = 1;
15753 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15754 : mask = 2;
15755 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15756 : mask = 4;
15757 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15758 : mask = 8;
15759 : else
15760 : {
15761 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15762 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15763 : "clauses");
15764 1 : return;
15765 : }
15766 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15767 166 : build_int_cst (integer_type_node, mask));
15768 166 : add_stmt (stmt);
15769 : }
15770 :
15771 : /* Helper function for handle_omp_array_sections. Called recursively
15772 : to handle multiple array-section-subscripts. C is the clause,
15773 : T current expression (initially OMP_CLAUSE_DECL), which is either
15774 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15775 : expression if specified, TREE_VALUE length expression if specified,
15776 : TREE_CHAIN is what it has been specified after, or some decl.
15777 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15778 : set to true if any of the array-section-subscript could have length
15779 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15780 : first array-section-subscript which is known not to have length
15781 : of one. Given say:
15782 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15783 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15784 : all are or may have length of 1, array-section-subscript [:2] is the
15785 : first one known not to have length 1. For array-section-subscript
15786 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15787 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15788 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15789 : case though, as some lengths could be zero. */
15790 :
15791 : static tree
15792 6396 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15793 : bool &maybe_zero_len, unsigned int &first_non_one,
15794 : enum c_omp_region_type ort)
15795 : {
15796 6396 : tree ret, low_bound, length, type;
15797 6396 : bool openacc = (ort & C_ORT_ACC) != 0;
15798 6396 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15799 : {
15800 2979 : if (error_operand_p (t))
15801 2 : return error_mark_node;
15802 2977 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15803 2977 : ret = t;
15804 2977 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15805 2878 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15806 5541 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15807 : {
15808 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15809 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15810 6 : return error_mark_node;
15811 : }
15812 2971 : if (!ai.check_clause (c))
15813 0 : return error_mark_node;
15814 2971 : else if (ai.component_access_p ()
15815 3272 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15816 15 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15817 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15818 301 : t = ai.get_root_term (true);
15819 : else
15820 2670 : t = ai.unconverted_ref_origin ();
15821 2971 : if (t == error_mark_node)
15822 : return error_mark_node;
15823 2971 : if (!VAR_P (t)
15824 742 : && (ort == C_ORT_ACC || !EXPR_P (t))
15825 739 : && TREE_CODE (t) != PARM_DECL)
15826 : {
15827 5 : if (DECL_P (t))
15828 5 : error_at (OMP_CLAUSE_LOCATION (c),
15829 : "%qD is not a variable in %qs clause", t,
15830 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15831 : else
15832 0 : error_at (OMP_CLAUSE_LOCATION (c),
15833 : "%qE is not a variable in %qs clause", t,
15834 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15835 5 : return error_mark_node;
15836 : }
15837 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15838 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15839 5521 : && TYPE_ATOMIC (TREE_TYPE (t)))
15840 : {
15841 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15842 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15843 0 : return error_mark_node;
15844 : }
15845 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15846 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15847 2555 : && VAR_P (t)
15848 4972 : && DECL_THREAD_LOCAL_P (t))
15849 : {
15850 3 : error_at (OMP_CLAUSE_LOCATION (c),
15851 : "%qD is threadprivate variable in %qs clause", t,
15852 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15853 3 : return error_mark_node;
15854 : }
15855 2963 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15856 2865 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15857 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15858 2965 : && POINTER_TYPE_P (TREE_TYPE (t)))
15859 : {
15860 : /* If the array section is pointer based and the pointer
15861 : itself is _Atomic qualified, we need to atomically load
15862 : the pointer. */
15863 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15864 : ret, false, false);
15865 : }
15866 2963 : return ret;
15867 2977 : }
15868 :
15869 3417 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15870 : maybe_zero_len, first_non_one, ort);
15871 3417 : if (ret == error_mark_node || ret == NULL_TREE)
15872 : return ret;
15873 :
15874 3352 : type = TREE_TYPE (ret);
15875 3352 : low_bound = TREE_OPERAND (t, 1);
15876 3352 : length = TREE_OPERAND (t, 2);
15877 :
15878 3352 : if (low_bound == error_mark_node || length == error_mark_node)
15879 : return error_mark_node;
15880 :
15881 3352 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15882 : {
15883 13 : error_at (OMP_CLAUSE_LOCATION (c),
15884 : "low bound %qE of array section does not have integral type",
15885 : low_bound);
15886 13 : return error_mark_node;
15887 : }
15888 3339 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15889 : {
15890 12 : error_at (OMP_CLAUSE_LOCATION (c),
15891 : "length %qE of array section does not have integral type",
15892 : length);
15893 12 : return error_mark_node;
15894 : }
15895 3327 : if (low_bound
15896 2743 : && TREE_CODE (low_bound) == INTEGER_CST
15897 5781 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15898 2454 : > TYPE_PRECISION (sizetype))
15899 0 : low_bound = fold_convert (sizetype, low_bound);
15900 3327 : if (length
15901 3139 : && TREE_CODE (length) == INTEGER_CST
15902 5521 : && TYPE_PRECISION (TREE_TYPE (length))
15903 2194 : > TYPE_PRECISION (sizetype))
15904 0 : length = fold_convert (sizetype, length);
15905 3327 : if (low_bound == NULL_TREE)
15906 584 : low_bound = integer_zero_node;
15907 3327 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15908 3327 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15909 1937 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15910 : {
15911 10 : if (length != integer_one_node)
15912 : {
15913 6 : error_at (OMP_CLAUSE_LOCATION (c),
15914 : "expected single pointer in %qs clause",
15915 : user_omp_clause_code_name (c, openacc));
15916 6 : return error_mark_node;
15917 : }
15918 : }
15919 3321 : if (length != NULL_TREE)
15920 : {
15921 3137 : if (!integer_nonzerop (length))
15922 : {
15923 974 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15924 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15925 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15926 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15927 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15928 : {
15929 180 : if (integer_zerop (length))
15930 : {
15931 12 : error_at (OMP_CLAUSE_LOCATION (c),
15932 : "zero length array section in %qs clause",
15933 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15934 12 : return error_mark_node;
15935 : }
15936 : }
15937 : else
15938 794 : maybe_zero_len = true;
15939 : }
15940 3125 : if (first_non_one == types.length ()
15941 3125 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15942 1598 : first_non_one++;
15943 : }
15944 3309 : if (TREE_CODE (type) == ARRAY_TYPE)
15945 : {
15946 1496 : if (length == NULL_TREE
15947 1496 : && (TYPE_DOMAIN (type) == NULL_TREE
15948 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15949 : {
15950 8 : error_at (OMP_CLAUSE_LOCATION (c),
15951 : "for unknown bound array type length expression must "
15952 : "be specified");
15953 8 : return error_mark_node;
15954 : }
15955 1488 : if (TREE_CODE (low_bound) == INTEGER_CST
15956 1488 : && tree_int_cst_sgn (low_bound) == -1)
15957 : {
15958 35 : error_at (OMP_CLAUSE_LOCATION (c),
15959 : "negative low bound in array section in %qs clause",
15960 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15961 35 : return error_mark_node;
15962 : }
15963 1453 : if (length != NULL_TREE
15964 1297 : && TREE_CODE (length) == INTEGER_CST
15965 2310 : && tree_int_cst_sgn (length) == -1)
15966 : {
15967 35 : error_at (OMP_CLAUSE_LOCATION (c),
15968 : "negative length in array section in %qs clause",
15969 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15970 35 : return error_mark_node;
15971 : }
15972 1418 : if (TYPE_DOMAIN (type)
15973 1407 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15974 2825 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15975 : == INTEGER_CST)
15976 : {
15977 1061 : tree size
15978 1061 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15979 1061 : size = size_binop (PLUS_EXPR, size, size_one_node);
15980 1061 : if (TREE_CODE (low_bound) == INTEGER_CST)
15981 : {
15982 874 : if (tree_int_cst_lt (size, low_bound))
15983 : {
15984 10 : error_at (OMP_CLAUSE_LOCATION (c),
15985 : "low bound %qE above array section size "
15986 : "in %qs clause", low_bound,
15987 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15988 10 : return error_mark_node;
15989 : }
15990 864 : if (tree_int_cst_equal (size, low_bound))
15991 : {
15992 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15993 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15994 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15995 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15996 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15997 : {
15998 5 : error_at (OMP_CLAUSE_LOCATION (c),
15999 : "zero length array section in %qs clause",
16000 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16001 5 : return error_mark_node;
16002 : }
16003 0 : maybe_zero_len = true;
16004 : }
16005 859 : else if (length == NULL_TREE
16006 256 : && first_non_one == types.length ()
16007 914 : && tree_int_cst_equal
16008 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16009 : low_bound))
16010 30 : first_non_one++;
16011 : }
16012 187 : else if (length == NULL_TREE)
16013 : {
16014 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16015 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16016 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16017 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16018 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16019 0 : maybe_zero_len = true;
16020 6 : if (first_non_one == types.length ())
16021 1 : first_non_one++;
16022 : }
16023 1046 : if (length && TREE_CODE (length) == INTEGER_CST)
16024 : {
16025 799 : if (tree_int_cst_lt (size, length))
16026 : {
16027 11 : error_at (OMP_CLAUSE_LOCATION (c),
16028 : "length %qE above array section size "
16029 : "in %qs clause", length,
16030 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16031 11 : return error_mark_node;
16032 : }
16033 788 : if (TREE_CODE (low_bound) == INTEGER_CST)
16034 : {
16035 688 : tree lbpluslen
16036 688 : = size_binop (PLUS_EXPR,
16037 : fold_convert (sizetype, low_bound),
16038 : fold_convert (sizetype, length));
16039 688 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16040 688 : && tree_int_cst_lt (size, lbpluslen))
16041 : {
16042 10 : error_at (OMP_CLAUSE_LOCATION (c),
16043 : "high bound %qE above array section size "
16044 : "in %qs clause", lbpluslen,
16045 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16046 10 : return error_mark_node;
16047 : }
16048 : }
16049 : }
16050 : }
16051 357 : else if (length == NULL_TREE)
16052 : {
16053 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16054 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16055 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16056 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16057 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16058 0 : maybe_zero_len = true;
16059 20 : if (first_non_one == types.length ())
16060 10 : first_non_one++;
16061 : }
16062 :
16063 : /* For [lb:] we will need to evaluate lb more than once. */
16064 929 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16065 : {
16066 127 : tree lb = save_expr (low_bound);
16067 127 : if (lb != low_bound)
16068 : {
16069 2 : TREE_OPERAND (t, 1) = lb;
16070 2 : low_bound = lb;
16071 : }
16072 : }
16073 : }
16074 1813 : else if (TREE_CODE (type) == POINTER_TYPE)
16075 : {
16076 1802 : if (length == NULL_TREE)
16077 : {
16078 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16079 8 : error_at (OMP_CLAUSE_LOCATION (c),
16080 : "for array function parameter length expression "
16081 : "must be specified");
16082 : else
16083 2 : error_at (OMP_CLAUSE_LOCATION (c),
16084 : "for pointer type length expression must be specified");
16085 10 : return error_mark_node;
16086 : }
16087 1792 : if (length != NULL_TREE
16088 1792 : && TREE_CODE (length) == INTEGER_CST
16089 1287 : && tree_int_cst_sgn (length) == -1)
16090 : {
16091 20 : error_at (OMP_CLAUSE_LOCATION (c),
16092 : "negative length in array section in %qs clause",
16093 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16094 20 : return error_mark_node;
16095 : }
16096 : /* If there is a pointer type anywhere but in the very first
16097 : array-section-subscript, the array section could be non-contiguous. */
16098 1772 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16099 1611 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16100 3354 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16101 : {
16102 : /* If any prior dimension has a non-one length, then deem this
16103 : array section as non-contiguous. */
16104 42 : for (tree d = TREE_OPERAND (t, 0);
16105 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16106 40 : d = TREE_OPERAND (d, 0))
16107 : {
16108 44 : tree d_length = TREE_OPERAND (d, 2);
16109 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16110 : {
16111 4 : error_at (OMP_CLAUSE_LOCATION (c),
16112 : "array section is not contiguous in %qs clause",
16113 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16114 4 : return error_mark_node;
16115 : }
16116 : }
16117 : }
16118 : }
16119 : else
16120 : {
16121 11 : error_at (OMP_CLAUSE_LOCATION (c),
16122 : "%qE does not have pointer or array type", ret);
16123 11 : return error_mark_node;
16124 : }
16125 3150 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16126 2834 : types.safe_push (TREE_TYPE (ret));
16127 : /* We will need to evaluate lb more than once. */
16128 3150 : tree lb = save_expr (low_bound);
16129 3150 : if (lb != low_bound)
16130 : {
16131 273 : TREE_OPERAND (t, 1) = lb;
16132 273 : low_bound = lb;
16133 : }
16134 3150 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16135 3150 : return ret;
16136 : }
16137 :
16138 : /* Handle array sections for clause C. */
16139 :
16140 : static bool
16141 2979 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16142 : {
16143 2979 : bool maybe_zero_len = false;
16144 2979 : unsigned int first_non_one = 0;
16145 2979 : auto_vec<tree, 10> types;
16146 2979 : tree *tp = &OMP_CLAUSE_DECL (c);
16147 2979 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16148 2665 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16149 3078 : && OMP_ITERATOR_DECL_P (*tp))
16150 66 : tp = &TREE_VALUE (*tp);
16151 2979 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16152 : maybe_zero_len, first_non_one,
16153 : ort);
16154 2979 : if (first == error_mark_node)
16155 : return true;
16156 2761 : if (first == NULL_TREE)
16157 : return false;
16158 2761 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16159 2761 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16160 : {
16161 336 : tree t = *tp;
16162 336 : tree tem = NULL_TREE;
16163 : /* Need to evaluate side effects in the length expressions
16164 : if any. */
16165 336 : while (TREE_CODE (t) == TREE_LIST)
16166 : {
16167 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16168 : {
16169 0 : if (tem == NULL_TREE)
16170 : tem = TREE_VALUE (t);
16171 : else
16172 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16173 0 : TREE_VALUE (t), tem);
16174 : }
16175 0 : t = TREE_CHAIN (t);
16176 : }
16177 336 : if (tem)
16178 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16179 336 : first = c_fully_fold (first, false, NULL, true);
16180 336 : *tp = first;
16181 : }
16182 : else
16183 : {
16184 2425 : unsigned int num = types.length (), i;
16185 2425 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16186 2425 : tree condition = NULL_TREE;
16187 :
16188 2425 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16189 3 : maybe_zero_len = true;
16190 :
16191 5050 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16192 2625 : t = TREE_OPERAND (t, 0))
16193 : {
16194 2640 : tree low_bound = TREE_OPERAND (t, 1);
16195 2640 : tree length = TREE_OPERAND (t, 2);
16196 :
16197 2640 : i--;
16198 2640 : if (low_bound
16199 2176 : && TREE_CODE (low_bound) == INTEGER_CST
16200 4630 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16201 1990 : > TYPE_PRECISION (sizetype))
16202 0 : low_bound = fold_convert (sizetype, low_bound);
16203 2640 : if (length
16204 2548 : && TREE_CODE (length) == INTEGER_CST
16205 4261 : && TYPE_PRECISION (TREE_TYPE (length))
16206 1621 : > TYPE_PRECISION (sizetype))
16207 0 : length = fold_convert (sizetype, length);
16208 2640 : if (low_bound == NULL_TREE)
16209 464 : low_bound = integer_zero_node;
16210 2640 : if (!maybe_zero_len && i > first_non_one)
16211 : {
16212 109 : if (integer_nonzerop (low_bound))
16213 6 : goto do_warn_noncontiguous;
16214 103 : if (length != NULL_TREE
16215 50 : && TREE_CODE (length) == INTEGER_CST
16216 50 : && TYPE_DOMAIN (types[i])
16217 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16218 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16219 : == INTEGER_CST)
16220 : {
16221 50 : tree size;
16222 50 : size = size_binop (PLUS_EXPR,
16223 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16224 : size_one_node);
16225 50 : if (!tree_int_cst_equal (length, size))
16226 : {
16227 7 : do_warn_noncontiguous:
16228 26 : error_at (OMP_CLAUSE_LOCATION (c),
16229 : "array section is not contiguous in %qs "
16230 : "clause",
16231 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16232 2425 : return true;
16233 : }
16234 : }
16235 96 : if (length != NULL_TREE
16236 96 : && TREE_SIDE_EFFECTS (length))
16237 : {
16238 0 : if (side_effects == NULL_TREE)
16239 : side_effects = length;
16240 : else
16241 0 : side_effects = build2 (COMPOUND_EXPR,
16242 0 : TREE_TYPE (side_effects),
16243 : length, side_effects);
16244 : }
16245 : }
16246 : else
16247 : {
16248 2531 : tree l;
16249 :
16250 2531 : if (i > first_non_one
16251 2531 : && ((length && integer_nonzerop (length))
16252 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16253 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16254 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16255 0 : continue;
16256 2531 : if (length)
16257 2498 : l = fold_convert (sizetype, length);
16258 : else
16259 : {
16260 33 : l = size_binop (PLUS_EXPR,
16261 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16262 : size_one_node);
16263 33 : l = size_binop (MINUS_EXPR, l,
16264 : fold_convert (sizetype, low_bound));
16265 : }
16266 2531 : if (i > first_non_one)
16267 : {
16268 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16269 : size_zero_node);
16270 0 : if (condition == NULL_TREE)
16271 : condition = l;
16272 : else
16273 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16274 : l, condition);
16275 : }
16276 2531 : else if (size == NULL_TREE)
16277 : {
16278 2412 : size = size_in_bytes (TREE_TYPE (types[i]));
16279 2412 : tree eltype = TREE_TYPE (types[num - 1]);
16280 2432 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16281 20 : eltype = TREE_TYPE (eltype);
16282 2412 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16283 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16284 4329 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16285 : {
16286 539 : if (integer_zerop (size)
16287 1076 : || integer_zerop (size_in_bytes (eltype)))
16288 : {
16289 4 : error_at (OMP_CLAUSE_LOCATION (c),
16290 : "zero length array section in %qs clause",
16291 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16292 2 : return error_mark_node;
16293 : }
16294 537 : size = size_binop (EXACT_DIV_EXPR, size,
16295 : size_in_bytes (eltype));
16296 : }
16297 2410 : size = size_binop (MULT_EXPR, size, l);
16298 2410 : if (condition)
16299 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16300 : size, size_zero_node);
16301 : }
16302 : else
16303 119 : size = size_binop (MULT_EXPR, size, l);
16304 : }
16305 : }
16306 2410 : if (side_effects)
16307 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16308 2410 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16309 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16310 4327 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16311 : {
16312 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16313 537 : size = c_fully_fold (size, false, NULL);
16314 537 : size = save_expr (size);
16315 537 : tree index_type = build_index_type (size);
16316 537 : tree eltype = TREE_TYPE (first);
16317 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16318 12 : eltype = TREE_TYPE (eltype);
16319 537 : tree type = c_build_array_type (eltype, index_type);
16320 537 : tree ptype = c_build_pointer_type (eltype);
16321 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16322 296 : t = build_fold_addr_expr (t);
16323 537 : tree t2 = build_fold_addr_expr (first);
16324 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16325 : ptrdiff_type_node, t2);
16326 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16327 : ptrdiff_type_node, t2,
16328 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16329 : ptrdiff_type_node, t));
16330 537 : t2 = c_fully_fold (t2, false, NULL);
16331 537 : if (tree_fits_shwi_p (t2))
16332 398 : t = build2 (MEM_REF, type, t,
16333 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16334 : else
16335 : {
16336 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16337 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16338 139 : TREE_TYPE (t), t, t2);
16339 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16340 : }
16341 537 : OMP_CLAUSE_DECL (c) = t;
16342 537 : return false;
16343 : }
16344 1873 : first = c_fully_fold (first, false, NULL);
16345 1873 : OMP_CLAUSE_DECL (c) = first;
16346 1873 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16347 : return false;
16348 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16349 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16350 1862 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16351 1722 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16352 1720 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16353 : {
16354 1858 : if (size)
16355 1858 : size = c_fully_fold (size, false, NULL);
16356 1858 : OMP_CLAUSE_SIZE (c) = size;
16357 : }
16358 :
16359 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16360 : return false;
16361 :
16362 1724 : auto_vec<omp_addr_token *, 10> addr_tokens;
16363 :
16364 1724 : if (!omp_parse_expr (addr_tokens, first))
16365 1724 : return true;
16366 :
16367 1724 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16368 :
16369 1724 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16370 1724 : if (nc != error_mark_node)
16371 : {
16372 1724 : using namespace omp_addr_tokenizer;
16373 :
16374 1724 : if (ai.maybe_zero_length_array_section (c))
16375 1720 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16376 :
16377 : /* !!! If we're accessing a base decl via chained access
16378 : methods (e.g. multiple indirections), duplicate clause
16379 : detection won't work properly. Skip it in that case. */
16380 1724 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16381 1437 : || addr_tokens[0]->type == ARRAY_BASE)
16382 1724 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16383 1724 : && addr_tokens[1]->type == ACCESS_METHOD
16384 3448 : && omp_access_chain_p (addr_tokens, 1))
16385 34 : c = nc;
16386 :
16387 1724 : return false;
16388 : }
16389 : }
16390 : return false;
16391 2979 : }
16392 :
16393 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16394 : an inline call. But, remap
16395 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16396 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16397 :
16398 : static tree
16399 510 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16400 : tree decl, tree placeholder)
16401 : {
16402 510 : copy_body_data id;
16403 510 : hash_map<tree, tree> decl_map;
16404 :
16405 510 : decl_map.put (omp_decl1, placeholder);
16406 510 : decl_map.put (omp_decl2, decl);
16407 510 : memset (&id, 0, sizeof (id));
16408 510 : id.src_fn = DECL_CONTEXT (omp_decl1);
16409 510 : id.dst_fn = current_function_decl;
16410 510 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16411 510 : id.decl_map = &decl_map;
16412 :
16413 510 : id.copy_decl = copy_decl_no_change;
16414 510 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16415 510 : id.transform_new_cfg = true;
16416 510 : id.transform_return_to_modify = false;
16417 510 : id.eh_lp_nr = 0;
16418 510 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16419 510 : return stmt;
16420 510 : }
16421 :
16422 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16423 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16424 :
16425 : static tree
16426 1123 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16427 : {
16428 1123 : if (*tp == (tree) data)
16429 28 : return *tp;
16430 : return NULL_TREE;
16431 : }
16432 :
16433 : /* Similarly, but also walk aggregate fields. */
16434 :
16435 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16436 :
16437 : static tree
16438 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16439 : {
16440 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16441 : return *tp;
16442 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16443 : {
16444 2 : tree field;
16445 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16446 :
16447 2 : for (field = TYPE_FIELDS (*tp); field;
16448 0 : field = DECL_CHAIN (field))
16449 2 : if (TREE_CODE (field) == FIELD_DECL)
16450 : {
16451 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16452 : c_find_omp_var_r, data, pset);
16453 2 : if (ret)
16454 : return ret;
16455 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16456 2 : if (ret)
16457 : return ret;
16458 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16459 : pset);
16460 2 : if (ret)
16461 : return ret;
16462 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16463 2 : if (ret)
16464 : return ret;
16465 : }
16466 : }
16467 278 : else if (INTEGRAL_TYPE_P (*tp))
16468 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16469 : ((struct c_find_omp_var_s *) data)->pset);
16470 : return NULL_TREE;
16471 : }
16472 :
16473 : /* Finish OpenMP iterators ITER. Return true if they are errorneous
16474 : and clauses containing them should be removed. */
16475 :
16476 : static bool
16477 143 : c_omp_finish_iterators (tree iter)
16478 : {
16479 143 : bool ret = false;
16480 325 : for (tree it = iter; it; it = TREE_CHAIN (it))
16481 : {
16482 182 : tree var = TREE_VEC_ELT (it, 0);
16483 182 : tree begin = TREE_VEC_ELT (it, 1);
16484 182 : tree end = TREE_VEC_ELT (it, 2);
16485 182 : tree step = TREE_VEC_ELT (it, 3);
16486 182 : tree orig_step;
16487 182 : tree type = TREE_TYPE (var);
16488 182 : location_t loc = DECL_SOURCE_LOCATION (var);
16489 182 : if (type == error_mark_node)
16490 : {
16491 0 : ret = true;
16492 34 : continue;
16493 : }
16494 182 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16495 : {
16496 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16497 : var);
16498 6 : ret = true;
16499 6 : continue;
16500 : }
16501 176 : else if (TYPE_ATOMIC (type))
16502 : {
16503 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16504 2 : ret = true;
16505 2 : continue;
16506 : }
16507 174 : else if (TYPE_READONLY (type))
16508 : {
16509 4 : error_at (loc, "iterator %qD has const qualified type", var);
16510 4 : ret = true;
16511 4 : continue;
16512 : }
16513 170 : else if (step == error_mark_node
16514 170 : || TREE_TYPE (step) == error_mark_node)
16515 : {
16516 0 : ret = true;
16517 0 : continue;
16518 : }
16519 170 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16520 : {
16521 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16522 : "iterator step with non-integral type");
16523 4 : ret = true;
16524 4 : continue;
16525 : }
16526 166 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16527 166 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16528 166 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16529 166 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16530 166 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16531 166 : if (POINTER_TYPE_P (type))
16532 : {
16533 14 : begin = save_expr (begin);
16534 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16535 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16536 : fold_convert (sizetype, step),
16537 : fold_convert (sizetype, begin));
16538 14 : step = fold_convert (ssizetype, step);
16539 : }
16540 166 : if (integer_zerop (step))
16541 : {
16542 6 : error_at (loc, "iterator %qD has zero step", var);
16543 6 : ret = true;
16544 6 : continue;
16545 : }
16546 :
16547 160 : if (begin == error_mark_node
16548 158 : || end == error_mark_node
16549 156 : || step == error_mark_node
16550 156 : || orig_step == error_mark_node)
16551 : {
16552 4 : ret = true;
16553 4 : continue;
16554 : }
16555 156 : hash_set<tree> pset;
16556 156 : tree it2;
16557 197 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16558 : {
16559 49 : tree var2 = TREE_VEC_ELT (it2, 0);
16560 49 : tree begin2 = TREE_VEC_ELT (it2, 1);
16561 49 : tree end2 = TREE_VEC_ELT (it2, 2);
16562 49 : tree step2 = TREE_VEC_ELT (it2, 3);
16563 49 : tree type2 = TREE_TYPE (var2);
16564 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16565 49 : struct c_find_omp_var_s data = { var, &pset };
16566 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16567 : {
16568 2 : error_at (loc2,
16569 : "type of iterator %qD refers to outer iterator %qD",
16570 : var2, var);
16571 10 : break;
16572 : }
16573 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16574 : {
16575 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16576 : "begin expression refers to outer iterator %qD", var);
16577 2 : break;
16578 : }
16579 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16580 : {
16581 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16582 : "end expression refers to outer iterator %qD", var);
16583 2 : break;
16584 : }
16585 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16586 : {
16587 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16588 : "step expression refers to outer iterator %qD", var);
16589 2 : break;
16590 : }
16591 : }
16592 156 : if (it2)
16593 : {
16594 8 : ret = true;
16595 8 : continue;
16596 : }
16597 148 : TREE_VEC_ELT (it, 1) = begin;
16598 148 : TREE_VEC_ELT (it, 2) = end;
16599 148 : TREE_VEC_ELT (it, 3) = step;
16600 148 : TREE_VEC_ELT (it, 4) = orig_step;
16601 156 : }
16602 143 : return ret;
16603 : }
16604 :
16605 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16606 : Return true if an error has been detected. */
16607 :
16608 : static bool
16609 9496 : c_oacc_check_attachments (tree c)
16610 : {
16611 9496 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16612 : return false;
16613 :
16614 : /* OpenACC attach / detach clauses must be pointers. */
16615 6501 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16616 6501 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16617 : {
16618 71 : tree t = OMP_CLAUSE_DECL (c);
16619 :
16620 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16621 6 : t = TREE_OPERAND (t, 0);
16622 :
16623 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16624 : {
16625 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16626 : user_omp_clause_code_name (c, true));
16627 6 : return true;
16628 : }
16629 : }
16630 :
16631 : return false;
16632 : }
16633 :
16634 : /* For all elements of CLAUSES, validate them against their constraints.
16635 : Remove any elements from the list that are invalid. */
16636 :
16637 : tree
16638 29470 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16639 : {
16640 29470 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16641 29470 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16642 29470 : bitmap_head oacc_reduction_head, is_on_device_head;
16643 29470 : tree c, t, type, *pc;
16644 29470 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16645 29470 : bool branch_seen = false;
16646 29470 : bool copyprivate_seen = false;
16647 29470 : bool mergeable_seen = false;
16648 29470 : tree *detach_seen = NULL;
16649 29470 : bool linear_variable_step_check = false;
16650 29470 : tree *nowait_clause = NULL;
16651 29470 : tree *grainsize_seen = NULL;
16652 29470 : bool num_tasks_seen = false;
16653 29470 : tree ordered_clause = NULL_TREE;
16654 29470 : tree schedule_clause = NULL_TREE;
16655 29470 : bool oacc_async = false;
16656 29470 : tree last_iterators = NULL_TREE;
16657 29470 : bool last_iterators_remove = false;
16658 29470 : tree *nogroup_seen = NULL;
16659 29470 : tree *order_clause = NULL;
16660 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16661 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16662 29470 : int reduction_seen = 0;
16663 29470 : bool allocate_seen = false;
16664 29470 : bool implicit_moved = false;
16665 29470 : bool target_in_reduction_seen = false;
16666 29470 : tree *full_seen = NULL;
16667 29470 : bool partial_seen = false;
16668 29470 : bool openacc = (ort & C_ORT_ACC) != 0;
16669 29470 : bool init_seen = false;
16670 29470 : bool init_use_destroy_seen = false;
16671 29470 : tree init_no_targetsync_clause = NULL_TREE;
16672 29470 : tree depend_clause = NULL_TREE;
16673 :
16674 29470 : bitmap_obstack_initialize (NULL);
16675 29470 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16676 29470 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16677 29470 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16678 29470 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16679 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16680 29470 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16681 29470 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16682 29470 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16683 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16684 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16685 29470 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16686 29470 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16687 :
16688 29470 : if (openacc)
16689 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16690 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16691 : {
16692 : oacc_async = true;
16693 : break;
16694 : }
16695 :
16696 29470 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16697 :
16698 77416 : for (pc = &clauses, c = clauses; c ; c = *pc)
16699 : {
16700 47946 : bool remove = false;
16701 47946 : bool need_complete = false;
16702 47946 : bool need_implicitly_determined = false;
16703 :
16704 : /* We've reached the end of a list of expanded nodes. Reset the group
16705 : start pointer. */
16706 47946 : if (c == grp_sentinel)
16707 : {
16708 2470 : if (grp_start_p
16709 2470 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16710 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16711 22 : gc = OMP_CLAUSE_CHAIN (gc))
16712 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16713 : grp_start_p = NULL;
16714 : }
16715 :
16716 47946 : switch (OMP_CLAUSE_CODE (c))
16717 : {
16718 1139 : case OMP_CLAUSE_SHARED:
16719 1139 : need_implicitly_determined = true;
16720 1139 : goto check_dup_generic;
16721 :
16722 847 : case OMP_CLAUSE_PRIVATE:
16723 847 : need_complete = true;
16724 847 : need_implicitly_determined = true;
16725 847 : goto check_dup_generic;
16726 :
16727 3519 : case OMP_CLAUSE_REDUCTION:
16728 3519 : if (reduction_seen == 0)
16729 2920 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16730 599 : else if (reduction_seen != -2
16731 1198 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16732 599 : ? -1 : 1))
16733 : {
16734 2 : error_at (OMP_CLAUSE_LOCATION (c),
16735 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16736 : "on the same construct");
16737 2 : reduction_seen = -2;
16738 : }
16739 : /* FALLTHRU */
16740 4183 : case OMP_CLAUSE_IN_REDUCTION:
16741 4183 : case OMP_CLAUSE_TASK_REDUCTION:
16742 4183 : need_implicitly_determined = true;
16743 4183 : t = OMP_CLAUSE_DECL (c);
16744 4183 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16745 : {
16746 553 : if (handle_omp_array_sections (c, ort))
16747 : {
16748 : remove = true;
16749 : break;
16750 : }
16751 :
16752 537 : t = OMP_CLAUSE_DECL (c);
16753 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16754 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16755 : {
16756 1 : error_at (OMP_CLAUSE_LOCATION (c),
16757 : "%<inscan%> %<reduction%> clause with array "
16758 : "section");
16759 1 : remove = true;
16760 1 : break;
16761 : }
16762 : }
16763 4166 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16764 4166 : if (t == error_mark_node)
16765 : {
16766 : remove = true;
16767 : break;
16768 : }
16769 4164 : if (oacc_async)
16770 3 : c_mark_addressable (t);
16771 4164 : type = TREE_TYPE (t);
16772 4164 : if (TREE_CODE (t) == MEM_REF)
16773 536 : type = TREE_TYPE (type);
16774 4164 : if (TREE_CODE (type) == ARRAY_TYPE)
16775 : {
16776 74 : tree oatype = type;
16777 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16778 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16779 74 : type = TREE_TYPE (type);
16780 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16781 : {
16782 1 : error_at (OMP_CLAUSE_LOCATION (c),
16783 : "%qD in %<reduction%> clause is a zero size array",
16784 : t);
16785 1 : remove = true;
16786 1 : break;
16787 : }
16788 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16789 : TYPE_SIZE_UNIT (type));
16790 73 : if (integer_zerop (size))
16791 : {
16792 1 : error_at (OMP_CLAUSE_LOCATION (c),
16793 : "%qD in %<reduction%> clause is a zero size array",
16794 : t);
16795 1 : remove = true;
16796 1 : break;
16797 : }
16798 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16799 72 : size = save_expr (size);
16800 72 : tree index_type = build_index_type (size);
16801 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16802 : index_type);
16803 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16804 72 : tree ptype = c_build_pointer_type (type);
16805 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16806 72 : t = build_fold_addr_expr (t);
16807 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16808 72 : OMP_CLAUSE_DECL (c) = t;
16809 : }
16810 4162 : if (TYPE_ATOMIC (type))
16811 : {
16812 3 : error_at (OMP_CLAUSE_LOCATION (c),
16813 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16814 3 : remove = true;
16815 3 : break;
16816 : }
16817 4159 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16818 4159 : || OMP_CLAUSE_REDUCTION_TASK (c))
16819 : {
16820 : /* Disallow zero sized or potentially zero sized task
16821 : reductions. */
16822 871 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16823 : {
16824 6 : error_at (OMP_CLAUSE_LOCATION (c),
16825 : "zero sized type %qT in %qs clause", type,
16826 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16827 3 : remove = true;
16828 3 : break;
16829 : }
16830 868 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16831 : {
16832 0 : error_at (OMP_CLAUSE_LOCATION (c),
16833 : "variable sized type %qT in %qs clause", type,
16834 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16835 0 : remove = true;
16836 0 : break;
16837 : }
16838 : }
16839 4156 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16840 4156 : && (FLOAT_TYPE_P (type)
16841 3604 : || TREE_CODE (type) == COMPLEX_TYPE))
16842 : {
16843 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16844 340 : const char *r_name = NULL;
16845 :
16846 340 : switch (r_code)
16847 : {
16848 : case PLUS_EXPR:
16849 : case MULT_EXPR:
16850 : case MINUS_EXPR:
16851 : case TRUTH_ANDIF_EXPR:
16852 : case TRUTH_ORIF_EXPR:
16853 : break;
16854 13 : case MIN_EXPR:
16855 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16856 : r_name = "min";
16857 : break;
16858 38 : case MAX_EXPR:
16859 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16860 : r_name = "max";
16861 : break;
16862 3 : case BIT_AND_EXPR:
16863 3 : r_name = "&";
16864 3 : break;
16865 2 : case BIT_XOR_EXPR:
16866 2 : r_name = "^";
16867 2 : break;
16868 : case BIT_IOR_EXPR:
16869 : r_name = "|";
16870 : break;
16871 0 : default:
16872 0 : gcc_unreachable ();
16873 : }
16874 5 : if (r_name)
16875 : {
16876 12 : error_at (OMP_CLAUSE_LOCATION (c),
16877 : "%qE has invalid type for %<reduction(%s)%>",
16878 : t, r_name);
16879 12 : remove = true;
16880 12 : break;
16881 : }
16882 : }
16883 3816 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16884 : {
16885 2 : error_at (OMP_CLAUSE_LOCATION (c),
16886 : "user defined reduction not found for %qE", t);
16887 2 : remove = true;
16888 2 : break;
16889 : }
16890 3814 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16891 : {
16892 277 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16893 277 : type = TYPE_MAIN_VARIANT (type);
16894 277 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16895 : VAR_DECL, NULL_TREE, type);
16896 277 : tree decl_placeholder = NULL_TREE;
16897 277 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16898 277 : DECL_ARTIFICIAL (placeholder) = 1;
16899 277 : DECL_IGNORED_P (placeholder) = 1;
16900 277 : if (TREE_CODE (t) == MEM_REF)
16901 : {
16902 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16903 : VAR_DECL, NULL_TREE, type);
16904 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16905 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16906 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16907 : }
16908 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16909 42 : c_mark_addressable (placeholder);
16910 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16911 76 : c_mark_addressable (decl_placeholder ? decl_placeholder
16912 34 : : OMP_CLAUSE_DECL (c));
16913 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16914 775 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16915 277 : TREE_VEC_ELT (list, 0),
16916 277 : TREE_VEC_ELT (list, 1),
16917 : decl_placeholder ? decl_placeholder
16918 221 : : OMP_CLAUSE_DECL (c), placeholder);
16919 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16920 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16921 : void_type_node, NULL_TREE,
16922 277 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16923 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16924 277 : if (TREE_VEC_LENGTH (list) == 6)
16925 : {
16926 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16927 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16928 33 : : OMP_CLAUSE_DECL (c));
16929 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16930 27 : c_mark_addressable (placeholder);
16931 233 : tree init = TREE_VEC_ELT (list, 5);
16932 233 : if (init == error_mark_node)
16933 197 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16934 233 : OMP_CLAUSE_REDUCTION_INIT (c)
16935 651 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16936 233 : TREE_VEC_ELT (list, 3),
16937 : decl_placeholder ? decl_placeholder
16938 185 : : OMP_CLAUSE_DECL (c), placeholder);
16939 233 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16940 : {
16941 197 : tree v = decl_placeholder ? decl_placeholder : t;
16942 197 : OMP_CLAUSE_REDUCTION_INIT (c)
16943 394 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16944 197 : OMP_CLAUSE_REDUCTION_INIT (c));
16945 : }
16946 233 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16947 : c_find_omp_placeholder_r,
16948 : placeholder, NULL))
16949 28 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16950 : }
16951 : else
16952 : {
16953 44 : tree init;
16954 44 : tree v = decl_placeholder ? decl_placeholder : t;
16955 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16956 34 : init = build_constructor (TREE_TYPE (v), NULL);
16957 : else
16958 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
16959 44 : OMP_CLAUSE_REDUCTION_INIT (c)
16960 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16961 : }
16962 277 : OMP_CLAUSE_REDUCTION_INIT (c)
16963 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16964 : void_type_node, NULL_TREE,
16965 277 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16966 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16967 : }
16968 4142 : if (TREE_CODE (t) == MEM_REF)
16969 : {
16970 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16971 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16972 : != INTEGER_CST)
16973 : {
16974 0 : sorry ("variable length element type in array "
16975 : "%<reduction%> clause");
16976 0 : remove = true;
16977 0 : break;
16978 : }
16979 607 : t = TREE_OPERAND (t, 0);
16980 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16981 139 : t = TREE_OPERAND (t, 0);
16982 607 : if (TREE_CODE (t) == ADDR_EXPR)
16983 367 : t = TREE_OPERAND (t, 0);
16984 : }
16985 4142 : goto check_dup_generic_t;
16986 :
16987 24 : case OMP_CLAUSE_COPYPRIVATE:
16988 24 : copyprivate_seen = true;
16989 24 : if (nowait_clause)
16990 : {
16991 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
16992 : "%<nowait%> clause must not be used together "
16993 : "with %<copyprivate%> clause");
16994 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
16995 1 : nowait_clause = NULL;
16996 : }
16997 24 : goto check_dup_generic;
16998 :
16999 99 : case OMP_CLAUSE_COPYIN:
17000 99 : t = OMP_CLAUSE_DECL (c);
17001 99 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17002 : {
17003 5 : error_at (OMP_CLAUSE_LOCATION (c),
17004 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17005 5 : remove = true;
17006 5 : break;
17007 : }
17008 94 : goto check_dup_generic;
17009 :
17010 480 : case OMP_CLAUSE_LINEAR:
17011 480 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17012 327 : need_implicitly_determined = true;
17013 480 : t = OMP_CLAUSE_DECL (c);
17014 480 : if (ort != C_ORT_OMP_DECLARE_SIMD
17015 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17016 485 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17017 : {
17018 5 : error_at (OMP_CLAUSE_LOCATION (c),
17019 : "modifier should not be specified in %<linear%> "
17020 : "clause on %<simd%> or %<for%> constructs when not "
17021 : "using OpenMP 5.2 modifiers");
17022 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17023 : }
17024 960 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17025 503 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17026 : {
17027 1 : error_at (OMP_CLAUSE_LOCATION (c),
17028 : "linear clause applied to non-integral non-pointer "
17029 1 : "variable with type %qT", TREE_TYPE (t));
17030 1 : remove = true;
17031 1 : break;
17032 : }
17033 479 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17034 : {
17035 3 : error_at (OMP_CLAUSE_LOCATION (c),
17036 : "%<_Atomic%> %qD in %<linear%> clause", t);
17037 3 : remove = true;
17038 3 : break;
17039 : }
17040 476 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17041 : {
17042 152 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17043 152 : if (TREE_CODE (s) == PARM_DECL)
17044 : {
17045 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17046 : /* map_head bitmap is used as uniform_head if
17047 : declare_simd. */
17048 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17049 5 : linear_variable_step_check = true;
17050 9 : goto check_dup_generic;
17051 : }
17052 143 : if (TREE_CODE (s) != INTEGER_CST)
17053 : {
17054 7 : error_at (OMP_CLAUSE_LOCATION (c),
17055 : "%<linear%> clause step %qE is neither constant "
17056 : "nor a parameter", s);
17057 7 : remove = true;
17058 7 : break;
17059 : }
17060 : }
17061 460 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17062 : {
17063 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17064 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17065 20 : OMP_CLAUSE_DECL (c), s);
17066 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17067 : sizetype, fold_convert (sizetype, s),
17068 20 : fold_convert
17069 : (sizetype, OMP_CLAUSE_DECL (c)));
17070 20 : if (s == error_mark_node)
17071 0 : s = size_one_node;
17072 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17073 : }
17074 : else
17075 440 : OMP_CLAUSE_LINEAR_STEP (c)
17076 880 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17077 460 : goto check_dup_generic;
17078 :
17079 2887 : check_dup_generic:
17080 2887 : t = OMP_CLAUSE_DECL (c);
17081 7118 : check_dup_generic_t:
17082 7118 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17083 : {
17084 4 : error_at (OMP_CLAUSE_LOCATION (c),
17085 : "%qE is not a variable in clause %qs", t,
17086 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17087 2 : remove = true;
17088 : }
17089 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17090 6077 : || (ort == C_ORT_OMP
17091 5358 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17092 5347 : || (OMP_CLAUSE_CODE (c)
17093 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17094 13146 : || (ort == C_ORT_OMP_TARGET
17095 347 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17096 : {
17097 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17098 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17099 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17100 : {
17101 2 : error_at (OMP_CLAUSE_LOCATION (c),
17102 : "%qD appears more than once in data-sharing "
17103 : "clauses", t);
17104 2 : remove = true;
17105 2 : break;
17106 : }
17107 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17108 108 : target_in_reduction_seen = true;
17109 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17110 : {
17111 4 : error_at (OMP_CLAUSE_LOCATION (c),
17112 : openacc
17113 : ? "%qD appears more than once in reduction clauses"
17114 : : "%qD appears more than once in data clauses",
17115 : t);
17116 2 : remove = true;
17117 : }
17118 : else
17119 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17120 : }
17121 5920 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17122 5897 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17123 5896 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17124 11816 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17125 : {
17126 24 : error_at (OMP_CLAUSE_LOCATION (c),
17127 : "%qE appears more than once in data clauses", t);
17128 24 : remove = true;
17129 : }
17130 5896 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17131 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17132 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17133 1047 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17134 : {
17135 3 : if (openacc)
17136 0 : error_at (OMP_CLAUSE_LOCATION (c),
17137 : "%qD appears more than once in data clauses", t);
17138 : else
17139 3 : error_at (OMP_CLAUSE_LOCATION (c),
17140 : "%qD appears both in data and map clauses", t);
17141 : remove = true;
17142 : }
17143 : else
17144 5893 : bitmap_set_bit (&generic_head, DECL_UID (t));
17145 : break;
17146 :
17147 1333 : case OMP_CLAUSE_FIRSTPRIVATE:
17148 1333 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17149 : {
17150 186 : move_implicit:
17151 186 : implicit_moved = true;
17152 : /* Move firstprivate and map clauses with
17153 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17154 : clauses chain. */
17155 186 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17156 186 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17157 1072 : while (*pc1)
17158 886 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17159 886 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17160 : {
17161 113 : *pc3 = *pc1;
17162 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17163 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17164 : }
17165 773 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17166 773 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17167 : {
17168 164 : *pc2 = *pc1;
17169 164 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17170 164 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17171 : }
17172 : else
17173 609 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17174 186 : *pc3 = NULL;
17175 186 : *pc2 = cl2;
17176 186 : *pc1 = cl1;
17177 186 : continue;
17178 186 : }
17179 1238 : t = OMP_CLAUSE_DECL (c);
17180 1238 : need_complete = true;
17181 1238 : need_implicitly_determined = true;
17182 1238 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17183 : {
17184 1 : error_at (OMP_CLAUSE_LOCATION (c),
17185 : "%qE is not a variable in clause %<firstprivate%>", t);
17186 1 : remove = true;
17187 : }
17188 1237 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17189 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17190 1343 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17191 : remove = true;
17192 1237 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17193 1235 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17194 2471 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17195 : {
17196 4 : error_at (OMP_CLAUSE_LOCATION (c),
17197 : "%qE appears more than once in data clauses", t);
17198 4 : remove = true;
17199 : }
17200 1233 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17201 1233 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17202 : {
17203 7 : if (openacc)
17204 0 : error_at (OMP_CLAUSE_LOCATION (c),
17205 : "%qD appears more than once in data clauses", t);
17206 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17207 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17208 : /* Silently drop the clause. */;
17209 : else
17210 6 : error_at (OMP_CLAUSE_LOCATION (c),
17211 : "%qD appears both in data and map clauses", t);
17212 : remove = true;
17213 : }
17214 : else
17215 1226 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17216 : break;
17217 :
17218 1523 : case OMP_CLAUSE_LASTPRIVATE:
17219 1523 : t = OMP_CLAUSE_DECL (c);
17220 1523 : need_complete = true;
17221 1523 : need_implicitly_determined = true;
17222 1523 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17223 : {
17224 0 : error_at (OMP_CLAUSE_LOCATION (c),
17225 : "%qE is not a variable in clause %<lastprivate%>", t);
17226 0 : remove = true;
17227 : }
17228 1523 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17229 1523 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17230 : {
17231 3 : error_at (OMP_CLAUSE_LOCATION (c),
17232 : "%qE appears more than once in data clauses", t);
17233 3 : remove = true;
17234 : }
17235 : else
17236 1520 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17237 : break;
17238 :
17239 253 : case OMP_CLAUSE_ALIGNED:
17240 253 : t = OMP_CLAUSE_DECL (c);
17241 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17242 : {
17243 0 : error_at (OMP_CLAUSE_LOCATION (c),
17244 : "%qE is not a variable in %<aligned%> clause", t);
17245 0 : remove = true;
17246 : }
17247 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17248 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17249 : {
17250 7 : error_at (OMP_CLAUSE_LOCATION (c),
17251 : "%qE in %<aligned%> clause is neither a pointer nor "
17252 : "an array", t);
17253 7 : remove = true;
17254 : }
17255 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17256 : {
17257 1 : error_at (OMP_CLAUSE_LOCATION (c),
17258 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17259 1 : remove = true;
17260 1 : break;
17261 : }
17262 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17263 : {
17264 0 : error_at (OMP_CLAUSE_LOCATION (c),
17265 : "%qE appears more than once in %<aligned%> clauses",
17266 : t);
17267 0 : remove = true;
17268 : }
17269 : else
17270 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17271 : break;
17272 :
17273 125 : case OMP_CLAUSE_NONTEMPORAL:
17274 125 : t = OMP_CLAUSE_DECL (c);
17275 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17276 : {
17277 0 : error_at (OMP_CLAUSE_LOCATION (c),
17278 : "%qE is not a variable in %<nontemporal%> clause", t);
17279 0 : remove = true;
17280 : }
17281 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17282 : {
17283 2 : error_at (OMP_CLAUSE_LOCATION (c),
17284 : "%qE appears more than once in %<nontemporal%> "
17285 : "clauses", t);
17286 2 : remove = true;
17287 : }
17288 : else
17289 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17290 : break;
17291 :
17292 802 : case OMP_CLAUSE_ALLOCATE:
17293 802 : t = OMP_CLAUSE_DECL (c);
17294 802 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17295 : {
17296 1 : error_at (OMP_CLAUSE_LOCATION (c),
17297 : "%qE is not a variable in %<allocate%> clause", t);
17298 1 : remove = true;
17299 : }
17300 801 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17301 : {
17302 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17303 : "%qE appears more than once in %<allocate%> clauses",
17304 : t);
17305 2 : remove = true;
17306 : }
17307 : else
17308 : {
17309 799 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17310 799 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17311 700 : allocate_seen = true;
17312 : }
17313 : break;
17314 :
17315 357 : case OMP_CLAUSE_DOACROSS:
17316 357 : t = OMP_CLAUSE_DECL (c);
17317 357 : if (t == NULL_TREE)
17318 : break;
17319 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17320 : {
17321 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17322 1315 : for (; t; t = TREE_CHAIN (t))
17323 : {
17324 1146 : tree decl = TREE_VALUE (t);
17325 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17326 : {
17327 3 : tree offset = TREE_PURPOSE (t);
17328 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17329 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17330 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17331 : neg ? MINUS_EXPR : PLUS_EXPR,
17332 : decl, offset);
17333 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17334 : sizetype,
17335 : fold_convert (sizetype, t2),
17336 : fold_convert (sizetype, decl));
17337 3 : if (t2 == error_mark_node)
17338 : {
17339 : remove = true;
17340 : break;
17341 : }
17342 3 : TREE_PURPOSE (t) = t2;
17343 : }
17344 : }
17345 : break;
17346 : }
17347 0 : gcc_unreachable ();
17348 :
17349 36 : case OMP_CLAUSE_USES_ALLOCATORS:
17350 36 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17351 36 : if (t == error_mark_node)
17352 : {
17353 : remove = true;
17354 : break;
17355 : }
17356 6 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17357 33 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17358 27 : || bitmap_bit_p (&map_head, DECL_UID (t))
17359 27 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17360 26 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17361 : {
17362 1 : error_at (OMP_CLAUSE_LOCATION (c),
17363 : "%qE appears more than once in data clauses", t);
17364 1 : remove = true;
17365 1 : break;
17366 : }
17367 : else
17368 31 : bitmap_set_bit (&generic_head, DECL_UID (t));
17369 31 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17370 59 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17371 : "omp_allocator_handle_t") != 0)
17372 : {
17373 3 : error_at (OMP_CLAUSE_LOCATION (c),
17374 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17375 : "type", t);
17376 3 : remove = true;
17377 3 : break;
17378 : }
17379 28 : tree init;
17380 28 : if (!DECL_P (t)
17381 28 : || (TREE_CODE (t) == CONST_DECL
17382 5 : && ((init = DECL_INITIAL(t)) == nullptr
17383 5 : || TREE_CODE (init) != INTEGER_CST
17384 5 : || ((wi::to_widest (init) < 0
17385 5 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17386 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17387 0 : || (wi::to_widest (init)
17388 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17389 : {
17390 0 : remove = true;
17391 0 : error_at (OMP_CLAUSE_LOCATION (c),
17392 : "allocator %qE must be either a variable or a "
17393 : "predefined allocator", t);
17394 0 : break;
17395 : }
17396 28 : else if (TREE_CODE (t) == CONST_DECL)
17397 : {
17398 : /* omp_null_allocator is ignored and for predefined allocators,
17399 : not special handling is required; thus, remove them removed. */
17400 5 : remove = true;
17401 :
17402 5 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17403 5 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17404 : {
17405 0 : error_at (OMP_CLAUSE_LOCATION (c),
17406 : "modifiers cannot be used with predefined "
17407 : "allocator %qE", t);
17408 0 : break;
17409 : }
17410 : }
17411 28 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17412 28 : if (t == error_mark_node)
17413 : {
17414 : remove = true;
17415 : break;
17416 : }
17417 28 : if (t != NULL_TREE
17418 28 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17419 5 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17420 8 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17421 : "omp_memspace_handle_t") != 0))
17422 : {
17423 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17424 : " constant enum of %<omp_memspace_handle_t%> type", t);
17425 1 : remove = true;
17426 1 : break;
17427 : }
17428 27 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17429 27 : if (t == error_mark_node)
17430 : {
17431 : remove = true;
17432 : break;
17433 : }
17434 27 : if (t != NULL_TREE
17435 : && t != error_mark_node
17436 27 : && (DECL_EXTERNAL (t)
17437 9 : || TREE_CODE (t) == PARM_DECL))
17438 : {
17439 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17440 : "defined in same scope as the construct on which the "
17441 : "clause appears", t);
17442 3 : remove = true;
17443 : }
17444 27 : if (t != NULL_TREE)
17445 : {
17446 11 : bool type_err = false;
17447 :
17448 11 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17449 10 : || DECL_SIZE (t) == NULL_TREE
17450 20 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17451 : type_err = true;
17452 : else
17453 : {
17454 9 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17455 9 : if (TREE_CODE (elem_t) != RECORD_TYPE
17456 18 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17457 : "omp_alloctrait_t") != 0
17458 18 : || !TYPE_READONLY (elem_t))
17459 : type_err = true;
17460 : }
17461 8 : if (type_err)
17462 : {
17463 3 : if (t != error_mark_node)
17464 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17465 : "be of %<const omp_alloctrait_t []%> type", t);
17466 : else
17467 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17468 : "be of %<const omp_alloctrait_t []%> type");
17469 : remove = true;
17470 : }
17471 : else
17472 : {
17473 8 : tree cst_val = decl_constant_value_1 (t, true);
17474 8 : if (cst_val == t)
17475 : {
17476 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17477 : "initialized with constants");
17478 :
17479 1 : remove = true;
17480 : }
17481 : }
17482 : }
17483 24 : if (remove)
17484 : break;
17485 18 : pc = &OMP_CLAUSE_CHAIN (c);
17486 18 : continue;
17487 713 : case OMP_CLAUSE_DEPEND:
17488 713 : depend_clause = c;
17489 : /* FALLTHRU */
17490 870 : case OMP_CLAUSE_AFFINITY:
17491 870 : t = OMP_CLAUSE_DECL (c);
17492 870 : if (OMP_ITERATOR_DECL_P (t))
17493 : {
17494 131 : if (TREE_PURPOSE (t) != last_iterators)
17495 111 : last_iterators_remove
17496 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17497 131 : last_iterators = TREE_PURPOSE (t);
17498 131 : t = TREE_VALUE (t);
17499 131 : if (last_iterators_remove)
17500 34 : t = error_mark_node;
17501 : }
17502 : else
17503 : last_iterators = NULL_TREE;
17504 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17505 : {
17506 413 : if (handle_omp_array_sections (c, ort))
17507 : remove = true;
17508 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17509 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17510 : {
17511 1 : error_at (OMP_CLAUSE_LOCATION (c),
17512 : "%<depend%> clause with %<depobj%> dependence "
17513 : "type on array section");
17514 1 : remove = true;
17515 : }
17516 : break;
17517 : }
17518 457 : if (t == error_mark_node)
17519 : remove = true;
17520 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17521 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17522 : {
17523 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17524 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17525 : {
17526 3 : error_at (OMP_CLAUSE_LOCATION (c),
17527 : "%<omp_all_memory%> used with %<depend%> kind "
17528 : "other than %<out%> or %<inout%>");
17529 3 : remove = true;
17530 : }
17531 : }
17532 408 : else if (!lvalue_p (t))
17533 : {
17534 6 : error_at (OMP_CLAUSE_LOCATION (c),
17535 : "%qE is not lvalue expression nor array section in "
17536 : "%qs clause", t,
17537 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17538 3 : remove = true;
17539 : }
17540 405 : else if (TREE_CODE (t) == COMPONENT_REF
17541 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17542 : {
17543 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17544 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17545 2 : error_at (OMP_CLAUSE_LOCATION (c),
17546 : "bit-field %qE in %qs clause", t,
17547 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17548 2 : remove = true;
17549 : }
17550 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17551 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17552 : {
17553 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17554 : {
17555 2 : error_at (OMP_CLAUSE_LOCATION (c),
17556 : "%qE does not have %<omp_depend_t%> type in "
17557 : "%<depend%> clause with %<depobj%> dependence "
17558 : "type", t);
17559 2 : remove = true;
17560 : }
17561 : }
17562 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17563 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17564 : {
17565 2 : error_at (OMP_CLAUSE_LOCATION (c),
17566 : "%qE should not have %<omp_depend_t%> type in "
17567 : "%<depend%> clause with dependence type other than "
17568 : "%<depobj%>", t);
17569 2 : remove = true;
17570 : }
17571 12 : if (!remove)
17572 : {
17573 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17574 12 : t = null_pointer_node;
17575 : else
17576 : {
17577 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17578 : ADDR_EXPR, t, false);
17579 399 : if (addr == error_mark_node)
17580 : {
17581 : remove = true;
17582 : break;
17583 : }
17584 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17585 : RO_UNARY_STAR);
17586 399 : if (t == error_mark_node)
17587 : {
17588 : remove = true;
17589 : break;
17590 : }
17591 : }
17592 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17593 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17594 : else
17595 380 : OMP_CLAUSE_DECL (c) = t;
17596 : }
17597 : break;
17598 :
17599 6628 : case OMP_CLAUSE_MAP:
17600 6628 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17601 91 : goto move_implicit;
17602 6537 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17603 6537 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17604 : {
17605 : remove = true;
17606 : break;
17607 : }
17608 : /* FALLTHRU */
17609 9390 : case OMP_CLAUSE_TO:
17610 9390 : case OMP_CLAUSE_FROM:
17611 9390 : if (OMP_CLAUSE_ITERATORS (c)
17612 9390 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17613 : {
17614 22808 : t = error_mark_node;
17615 : break;
17616 : }
17617 : /* FALLTHRU */
17618 9526 : case OMP_CLAUSE__CACHE_:
17619 9526 : {
17620 9526 : using namespace omp_addr_tokenizer;
17621 9526 : auto_vec<omp_addr_token *, 10> addr_tokens;
17622 :
17623 9526 : t = OMP_CLAUSE_DECL (c);
17624 9526 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17625 : {
17626 2002 : grp_start_p = pc;
17627 2002 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17628 :
17629 2002 : if (handle_omp_array_sections (c, ort))
17630 : remove = true;
17631 : else
17632 : {
17633 1862 : t = OMP_CLAUSE_DECL (c);
17634 1862 : if (!omp_mappable_type (TREE_TYPE (t)))
17635 : {
17636 2 : error_at (OMP_CLAUSE_LOCATION (c),
17637 : "array section does not have mappable type "
17638 : "in %qs clause",
17639 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17640 1 : remove = true;
17641 : }
17642 1861 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17643 : {
17644 2 : error_at (OMP_CLAUSE_LOCATION (c),
17645 : "%<_Atomic%> %qE in %qs clause", t,
17646 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17647 1 : remove = true;
17648 : }
17649 2511 : while (TREE_CODE (t) == ARRAY_REF)
17650 649 : t = TREE_OPERAND (t, 0);
17651 :
17652 1862 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17653 :
17654 1862 : if (!omp_parse_expr (addr_tokens, t))
17655 : {
17656 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17657 : "unsupported map expression %qE",
17658 0 : OMP_CLAUSE_DECL (c));
17659 0 : remove = true;
17660 0 : break;
17661 : }
17662 :
17663 : /* This check is to determine if this will be the only map
17664 : node created for this clause. Otherwise, we'll check
17665 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17666 : node on the next iteration(s) of the loop. */
17667 3706 : if (addr_tokens.length () >= 4
17668 304 : && addr_tokens[0]->type == STRUCTURE_BASE
17669 302 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17670 302 : && addr_tokens[1]->type == ACCESS_METHOD
17671 302 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17672 302 : && addr_tokens[3]->type == ACCESS_METHOD
17673 2103 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17674 188 : || (addr_tokens[3]->u.access_kind
17675 : == ACCESS_INDEXED_ARRAY)))
17676 : {
17677 55 : tree rt = addr_tokens[1]->expr;
17678 :
17679 55 : gcc_assert (DECL_P (rt));
17680 :
17681 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17682 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17683 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17684 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17685 0 : || bitmap_bit_p (&map_firstprivate_head,
17686 0 : DECL_UID (rt))))
17687 : {
17688 : remove = true;
17689 : break;
17690 : }
17691 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17692 : break;
17693 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17694 : {
17695 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17696 0 : error_at (OMP_CLAUSE_LOCATION (c),
17697 : "%qD appears more than once in motion "
17698 : "clauses", rt);
17699 0 : else if (openacc)
17700 0 : error_at (OMP_CLAUSE_LOCATION (c),
17701 : "%qD appears more than once in data "
17702 : "clauses", rt);
17703 : else
17704 0 : error_at (OMP_CLAUSE_LOCATION (c),
17705 : "%qD appears more than once in map "
17706 : "clauses", rt);
17707 : remove = true;
17708 : }
17709 : else
17710 : {
17711 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17712 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17713 : }
17714 : }
17715 1862 : }
17716 1984 : if (c_oacc_check_attachments (c))
17717 2 : remove = true;
17718 1984 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17719 1808 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17720 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17721 2009 : && !OMP_CLAUSE_SIZE (c))
17722 : /* In this case, we have a single array element which is a
17723 : pointer, and we already set OMP_CLAUSE_SIZE in
17724 : handle_omp_array_sections above. For attach/detach
17725 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17726 : to zero here. */
17727 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17728 : break;
17729 : }
17730 7524 : else if (!omp_parse_expr (addr_tokens, t))
17731 : {
17732 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17733 : "unsupported map expression %qE",
17734 0 : OMP_CLAUSE_DECL (c));
17735 0 : remove = true;
17736 0 : break;
17737 : }
17738 7524 : if (t == error_mark_node)
17739 : {
17740 : remove = true;
17741 : break;
17742 : }
17743 : /* OpenACC attach / detach clauses must be pointers. */
17744 7512 : if (c_oacc_check_attachments (c))
17745 : {
17746 : remove = true;
17747 : break;
17748 : }
17749 7508 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17750 4689 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17751 4662 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17752 7550 : && !OMP_CLAUSE_SIZE (c))
17753 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17754 : bias) to zero here, so it is not set erroneously to the pointer
17755 : size later on in gimplify.cc. */
17756 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17757 :
17758 7508 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17759 :
17760 7508 : if (!ai.check_clause (c))
17761 : {
17762 : remove = true;
17763 : break;
17764 : }
17765 :
17766 7500 : if (!ai.map_supported_p ())
17767 : {
17768 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17769 : "unsupported map expression %qE",
17770 7 : OMP_CLAUSE_DECL (c));
17771 7 : remove = true;
17772 7 : break;
17773 : }
17774 :
17775 14986 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17776 : || addr_tokens[0]->type == STRUCTURE_BASE)
17777 : && addr_tokens[1]->type == ACCESS_METHOD);
17778 :
17779 7493 : t = addr_tokens[1]->expr;
17780 :
17781 7493 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17782 0 : goto skip_decl_checks;
17783 :
17784 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17785 : mapping. OpenACC allows multiple fields of the same structure
17786 : to be written. */
17787 7493 : if (addr_tokens[0]->type == STRUCTURE_BASE
17788 7493 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17789 238 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17790 246 : goto skip_decl_checks;
17791 :
17792 7247 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17793 : {
17794 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17795 : break;
17796 :
17797 6 : error_at (OMP_CLAUSE_LOCATION (c),
17798 : "%qE is not a variable in %qs clause", t,
17799 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17800 3 : remove = true;
17801 : }
17802 7244 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17803 : {
17804 0 : error_at (OMP_CLAUSE_LOCATION (c),
17805 : "%qD is threadprivate variable in %qs clause", t,
17806 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17807 0 : remove = true;
17808 : }
17809 7244 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17810 4432 : || (OMP_CLAUSE_MAP_KIND (c)
17811 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17812 10632 : && !c_mark_addressable (t))
17813 : remove = true;
17814 7244 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17815 4432 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17816 4432 : || (OMP_CLAUSE_MAP_KIND (c)
17817 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17818 3388 : || (OMP_CLAUSE_MAP_KIND (c)
17819 : == GOMP_MAP_FORCE_DEVICEPTR)))
17820 6142 : && t == OMP_CLAUSE_DECL (c)
17821 13150 : && !omp_mappable_type (TREE_TYPE (t)))
17822 : {
17823 16 : error_at (OMP_CLAUSE_LOCATION (c),
17824 : "%qD does not have a mappable type in %qs clause", t,
17825 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17826 8 : remove = true;
17827 : }
17828 7236 : else if (TREE_TYPE (t) == error_mark_node)
17829 : remove = true;
17830 7235 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17831 : {
17832 8 : error_at (OMP_CLAUSE_LOCATION (c),
17833 : "%<_Atomic%> %qE in %qs clause", t,
17834 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17835 4 : remove = true;
17836 : }
17837 7231 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17838 4422 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17839 7395 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17840 128 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17841 128 : || bitmap_bit_p (&map_firstprivate_head,
17842 128 : DECL_UID (t))))
17843 : remove = true;
17844 7194 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17845 7194 : && (OMP_CLAUSE_MAP_KIND (c)
17846 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17847 : {
17848 1042 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17849 1042 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17850 2083 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17851 : {
17852 5 : error_at (OMP_CLAUSE_LOCATION (c),
17853 : "%qD appears more than once in data clauses", t);
17854 5 : remove = true;
17855 : }
17856 1037 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17857 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17858 1039 : && openacc)
17859 : {
17860 1 : error_at (OMP_CLAUSE_LOCATION (c),
17861 : "%qD appears more than once in data clauses", t);
17862 1 : remove = true;
17863 : }
17864 : else
17865 1036 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17866 : }
17867 6152 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17868 59 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17869 13 : && ort != C_ORT_OMP
17870 6165 : && ort != C_ORT_OMP_EXIT_DATA)
17871 : {
17872 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17873 0 : error_at (OMP_CLAUSE_LOCATION (c),
17874 : "%qD appears more than once in motion clauses", t);
17875 10 : else if (openacc)
17876 7 : error_at (OMP_CLAUSE_LOCATION (c),
17877 : "%qD appears more than once in data clauses", t);
17878 : else
17879 3 : error_at (OMP_CLAUSE_LOCATION (c),
17880 : "%qD appears more than once in map clauses", t);
17881 : remove = true;
17882 : }
17883 6142 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17884 : {
17885 0 : error_at (OMP_CLAUSE_LOCATION (c),
17886 : "%qD appears more than once in data clauses", t);
17887 0 : remove = true;
17888 : }
17889 6142 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17890 6142 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17891 : {
17892 9 : if (openacc)
17893 0 : error_at (OMP_CLAUSE_LOCATION (c),
17894 : "%qD appears more than once in data clauses", t);
17895 : else
17896 9 : error_at (OMP_CLAUSE_LOCATION (c),
17897 : "%qD appears both in data and map clauses", t);
17898 : remove = true;
17899 : }
17900 6133 : else if (!omp_access_chain_p (addr_tokens, 1))
17901 : {
17902 6133 : bitmap_set_bit (&map_head, DECL_UID (t));
17903 6133 : if (t != OMP_CLAUSE_DECL (c)
17904 6133 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17905 232 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17906 : }
17907 :
17908 0 : skip_decl_checks:
17909 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17910 : the containing loop (here) iterates through the new nodes
17911 : created by that expansion. Avoid expanding those again (just
17912 : by checking the node type). */
17913 7493 : if (!remove
17914 7493 : && ort != C_ORT_DECLARE_SIMD
17915 7493 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17916 4600 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17917 3564 : && (OMP_CLAUSE_MAP_KIND (c)
17918 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17919 3564 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17920 3564 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17921 3264 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17922 3237 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17923 : {
17924 6037 : grp_start_p = pc;
17925 6037 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17926 6037 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17927 : addr_tokens, ort);
17928 6037 : if (nc != error_mark_node)
17929 6037 : c = nc;
17930 : }
17931 9541 : }
17932 7493 : break;
17933 :
17934 232 : case OMP_CLAUSE_ENTER:
17935 232 : case OMP_CLAUSE_LINK:
17936 232 : t = OMP_CLAUSE_DECL (c);
17937 232 : const char *cname;
17938 232 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17939 232 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17940 232 : && OMP_CLAUSE_ENTER_TO (c))
17941 : cname = "to";
17942 232 : if (TREE_CODE (t) == FUNCTION_DECL
17943 232 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17944 : ;
17945 151 : else if (!VAR_P (t))
17946 : {
17947 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17948 0 : error_at (OMP_CLAUSE_LOCATION (c),
17949 : "%qE is neither a variable nor a function name in "
17950 : "clause %qs", t, cname);
17951 : else
17952 1 : error_at (OMP_CLAUSE_LOCATION (c),
17953 : "%qE is not a variable in clause %qs", t, cname);
17954 : remove = true;
17955 : }
17956 150 : else if (DECL_THREAD_LOCAL_P (t))
17957 : {
17958 2 : error_at (OMP_CLAUSE_LOCATION (c),
17959 : "%qD is threadprivate variable in %qs clause", t,
17960 : cname);
17961 2 : remove = true;
17962 : }
17963 148 : else if (!omp_mappable_type (TREE_TYPE (t)))
17964 : {
17965 6 : error_at (OMP_CLAUSE_LOCATION (c),
17966 : "%qD does not have a mappable type in %qs clause", t,
17967 : cname);
17968 6 : remove = true;
17969 : }
17970 8 : if (remove)
17971 : break;
17972 223 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17973 : {
17974 8 : error_at (OMP_CLAUSE_LOCATION (c),
17975 : "%qE appears more than once on the same "
17976 : "%<declare target%> directive", t);
17977 8 : remove = true;
17978 : }
17979 : else
17980 215 : bitmap_set_bit (&generic_head, DECL_UID (t));
17981 : break;
17982 :
17983 117 : case OMP_CLAUSE_UNIFORM:
17984 117 : t = OMP_CLAUSE_DECL (c);
17985 117 : if (TREE_CODE (t) != PARM_DECL)
17986 : {
17987 0 : if (DECL_P (t))
17988 0 : error_at (OMP_CLAUSE_LOCATION (c),
17989 : "%qD is not an argument in %<uniform%> clause", t);
17990 : else
17991 0 : error_at (OMP_CLAUSE_LOCATION (c),
17992 : "%qE is not an argument in %<uniform%> clause", t);
17993 : remove = true;
17994 : break;
17995 : }
17996 : /* map_head bitmap is used as uniform_head if declare_simd. */
17997 117 : bitmap_set_bit (&map_head, DECL_UID (t));
17998 117 : goto check_dup_generic;
17999 :
18000 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18001 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18002 161 : t = OMP_CLAUSE_DECL (c);
18003 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18004 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18005 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18006 : {
18007 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18008 21 : && !openacc)
18009 : {
18010 1 : error_at (OMP_CLAUSE_LOCATION (c),
18011 : "%qs variable is not a pointer",
18012 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18013 1 : remove = true;
18014 : }
18015 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18016 : {
18017 4 : error_at (OMP_CLAUSE_LOCATION (c),
18018 : "%qs variable is neither a pointer nor an array",
18019 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18020 4 : remove = true;
18021 : }
18022 : }
18023 161 : goto check_dup_generic;
18024 :
18025 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18026 89 : t = OMP_CLAUSE_DECL (c);
18027 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18028 : {
18029 11 : if (handle_omp_array_sections (c, ort))
18030 : remove = true;
18031 : else
18032 : {
18033 11 : t = OMP_CLAUSE_DECL (c);
18034 24 : while (TREE_CODE (t) == ARRAY_REF)
18035 13 : t = TREE_OPERAND (t, 0);
18036 : }
18037 : }
18038 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18039 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18040 89 : c_mark_addressable (t);
18041 89 : goto check_dup_generic_t;
18042 :
18043 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18044 36 : t = OMP_CLAUSE_DECL (c);
18045 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18046 36 : c_mark_addressable (t);
18047 36 : goto check_dup_generic;
18048 :
18049 4791 : case OMP_CLAUSE_NOWAIT:
18050 4791 : if (copyprivate_seen)
18051 : {
18052 1 : error_at (OMP_CLAUSE_LOCATION (c),
18053 : "%<nowait%> clause must not be used together "
18054 : "with %<copyprivate%> clause");
18055 1 : remove = true;
18056 1 : break;
18057 : }
18058 4790 : nowait_clause = pc;
18059 4790 : pc = &OMP_CLAUSE_CHAIN (c);
18060 4790 : continue;
18061 :
18062 523 : case OMP_CLAUSE_ORDER:
18063 523 : if (ordered_clause)
18064 : {
18065 2 : error_at (OMP_CLAUSE_LOCATION (c),
18066 : "%<order%> clause must not be used together "
18067 : "with %<ordered%> clause");
18068 2 : remove = true;
18069 2 : break;
18070 : }
18071 521 : else if (order_clause)
18072 : {
18073 : /* Silently remove duplicates. */
18074 : remove = true;
18075 : break;
18076 : }
18077 513 : order_clause = pc;
18078 513 : pc = &OMP_CLAUSE_CHAIN (c);
18079 513 : continue;
18080 :
18081 32 : case OMP_CLAUSE_DETACH:
18082 32 : t = OMP_CLAUSE_DECL (c);
18083 32 : if (detach_seen)
18084 : {
18085 1 : error_at (OMP_CLAUSE_LOCATION (c),
18086 : "too many %qs clauses on a task construct",
18087 : "detach");
18088 1 : remove = true;
18089 1 : break;
18090 : }
18091 31 : detach_seen = pc;
18092 31 : pc = &OMP_CLAUSE_CHAIN (c);
18093 31 : c_mark_addressable (t);
18094 31 : continue;
18095 :
18096 14583 : case OMP_CLAUSE_IF:
18097 14583 : case OMP_CLAUSE_SELF:
18098 14583 : case OMP_CLAUSE_NUM_THREADS:
18099 14583 : case OMP_CLAUSE_NUM_TEAMS:
18100 14583 : case OMP_CLAUSE_THREAD_LIMIT:
18101 14583 : case OMP_CLAUSE_DEFAULT:
18102 14583 : case OMP_CLAUSE_UNTIED:
18103 14583 : case OMP_CLAUSE_COLLAPSE:
18104 14583 : case OMP_CLAUSE_FINAL:
18105 14583 : case OMP_CLAUSE_DEVICE:
18106 14583 : case OMP_CLAUSE_DIST_SCHEDULE:
18107 14583 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18108 14583 : case OMP_CLAUSE_PARALLEL:
18109 14583 : case OMP_CLAUSE_FOR:
18110 14583 : case OMP_CLAUSE_SECTIONS:
18111 14583 : case OMP_CLAUSE_TASKGROUP:
18112 14583 : case OMP_CLAUSE_PROC_BIND:
18113 14583 : case OMP_CLAUSE_DEVICE_TYPE:
18114 14583 : case OMP_CLAUSE_PRIORITY:
18115 14583 : case OMP_CLAUSE_THREADS:
18116 14583 : case OMP_CLAUSE_SIMD:
18117 14583 : case OMP_CLAUSE_HINT:
18118 14583 : case OMP_CLAUSE_FILTER:
18119 14583 : case OMP_CLAUSE_DEFAULTMAP:
18120 14583 : case OMP_CLAUSE_BIND:
18121 14583 : case OMP_CLAUSE_NUM_GANGS:
18122 14583 : case OMP_CLAUSE_NUM_WORKERS:
18123 14583 : case OMP_CLAUSE_VECTOR_LENGTH:
18124 14583 : case OMP_CLAUSE_ASYNC:
18125 14583 : case OMP_CLAUSE_WAIT:
18126 14583 : case OMP_CLAUSE_AUTO:
18127 14583 : case OMP_CLAUSE_INDEPENDENT:
18128 14583 : case OMP_CLAUSE_SEQ:
18129 14583 : case OMP_CLAUSE_GANG:
18130 14583 : case OMP_CLAUSE_WORKER:
18131 14583 : case OMP_CLAUSE_VECTOR:
18132 14583 : case OMP_CLAUSE_TILE:
18133 14583 : case OMP_CLAUSE_IF_PRESENT:
18134 14583 : case OMP_CLAUSE_FINALIZE:
18135 14583 : case OMP_CLAUSE_NOHOST:
18136 14583 : case OMP_CLAUSE_INDIRECT:
18137 14583 : case OMP_CLAUSE_NOVARIANTS:
18138 14583 : case OMP_CLAUSE_NOCONTEXT:
18139 14583 : pc = &OMP_CLAUSE_CHAIN (c);
18140 14583 : continue;
18141 :
18142 62 : case OMP_CLAUSE_GRAINSIZE:
18143 62 : grainsize_seen = pc;
18144 62 : pc = &OMP_CLAUSE_CHAIN (c);
18145 62 : continue;
18146 :
18147 50 : case OMP_CLAUSE_NUM_TASKS:
18148 50 : num_tasks_seen = true;
18149 50 : pc = &OMP_CLAUSE_CHAIN (c);
18150 50 : continue;
18151 :
18152 79 : case OMP_CLAUSE_MERGEABLE:
18153 79 : mergeable_seen = true;
18154 79 : pc = &OMP_CLAUSE_CHAIN (c);
18155 79 : continue;
18156 :
18157 18 : case OMP_CLAUSE_NOGROUP:
18158 18 : nogroup_seen = pc;
18159 18 : pc = &OMP_CLAUSE_CHAIN (c);
18160 18 : continue;
18161 :
18162 3465 : case OMP_CLAUSE_SCHEDULE:
18163 3465 : schedule_clause = c;
18164 3465 : pc = &OMP_CLAUSE_CHAIN (c);
18165 3465 : continue;
18166 :
18167 304 : case OMP_CLAUSE_ORDERED:
18168 304 : ordered_clause = c;
18169 304 : if (order_clause)
18170 : {
18171 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18172 : "%<order%> clause must not be used together "
18173 : "with %<ordered%> clause");
18174 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18175 2 : order_clause = NULL;
18176 : }
18177 304 : pc = &OMP_CLAUSE_CHAIN (c);
18178 304 : continue;
18179 :
18180 223 : case OMP_CLAUSE_SAFELEN:
18181 223 : safelen = c;
18182 223 : pc = &OMP_CLAUSE_CHAIN (c);
18183 223 : continue;
18184 320 : case OMP_CLAUSE_SIMDLEN:
18185 320 : simdlen = c;
18186 320 : pc = &OMP_CLAUSE_CHAIN (c);
18187 320 : continue;
18188 :
18189 69 : case OMP_CLAUSE_FULL:
18190 69 : full_seen = pc;
18191 69 : pc = &OMP_CLAUSE_CHAIN (c);
18192 69 : continue;
18193 :
18194 223 : case OMP_CLAUSE_PARTIAL:
18195 223 : partial_seen = true;
18196 223 : pc = &OMP_CLAUSE_CHAIN (c);
18197 223 : continue;
18198 :
18199 0 : case OMP_CLAUSE_SIZES:
18200 0 : pc = &OMP_CLAUSE_CHAIN (c);
18201 0 : continue;
18202 :
18203 205 : case OMP_CLAUSE_INBRANCH:
18204 205 : case OMP_CLAUSE_NOTINBRANCH:
18205 205 : if (branch_seen)
18206 : {
18207 1 : error_at (OMP_CLAUSE_LOCATION (c),
18208 : "%<inbranch%> clause is incompatible with "
18209 : "%<notinbranch%>");
18210 1 : remove = true;
18211 1 : break;
18212 : }
18213 204 : branch_seen = true;
18214 204 : pc = &OMP_CLAUSE_CHAIN (c);
18215 204 : continue;
18216 :
18217 367 : case OMP_CLAUSE_INCLUSIVE:
18218 367 : case OMP_CLAUSE_EXCLUSIVE:
18219 367 : need_complete = true;
18220 367 : need_implicitly_determined = true;
18221 367 : t = OMP_CLAUSE_DECL (c);
18222 367 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18223 : {
18224 0 : error_at (OMP_CLAUSE_LOCATION (c),
18225 : "%qE is not a variable in clause %qs", t,
18226 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18227 0 : remove = true;
18228 : }
18229 : break;
18230 :
18231 152 : case OMP_CLAUSE_INIT:
18232 152 : init_seen = true;
18233 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18234 71 : init_no_targetsync_clause = c;
18235 : /* FALLTHRU */
18236 242 : case OMP_CLAUSE_DESTROY:
18237 242 : case OMP_CLAUSE_USE:
18238 242 : init_use_destroy_seen = true;
18239 242 : t = OMP_CLAUSE_DECL (c);
18240 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18241 : {
18242 3 : error_at (OMP_CLAUSE_LOCATION (c),
18243 : "%qD appears more than once in action clauses", t);
18244 3 : remove = true;
18245 3 : break;
18246 : }
18247 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18248 : /* FALLTHRU */
18249 298 : case OMP_CLAUSE_INTEROP:
18250 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18251 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18252 : {
18253 40 : error_at (OMP_CLAUSE_LOCATION (c),
18254 : "%qD must be of %<omp_interop_t%>",
18255 20 : OMP_CLAUSE_DECL (c));
18256 20 : remove = true;
18257 : }
18258 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18259 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18260 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18261 : {
18262 12 : error_at (OMP_CLAUSE_LOCATION (c),
18263 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18264 6 : remove = true;
18265 : }
18266 298 : pc = &OMP_CLAUSE_CHAIN (c);
18267 298 : break;
18268 0 : default:
18269 0 : gcc_unreachable ();
18270 24952 : }
18271 :
18272 22808 : if (!remove)
18273 : {
18274 22223 : t = OMP_CLAUSE_DECL (c);
18275 :
18276 22223 : if (need_complete)
18277 : {
18278 3953 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18279 3953 : if (t == error_mark_node)
18280 7 : remove = true;
18281 : }
18282 :
18283 22223 : if (need_implicitly_determined)
18284 : {
18285 9552 : const char *share_name = NULL;
18286 :
18287 9552 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18288 : share_name = "threadprivate";
18289 9546 : else switch (c_omp_predetermined_sharing (t))
18290 : {
18291 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18292 : break;
18293 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18294 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18295 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18296 28 : && c_omp_predefined_variable (t))
18297 : /* The __func__ variable and similar function-local
18298 : predefined variables may be listed in a shared or
18299 : firstprivate clause. */
18300 : break;
18301 : share_name = "shared";
18302 : break;
18303 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18304 : share_name = "private";
18305 : break;
18306 0 : default:
18307 0 : gcc_unreachable ();
18308 : }
18309 : if (share_name)
18310 : {
18311 20 : error_at (OMP_CLAUSE_LOCATION (c),
18312 : "%qE is predetermined %qs for %qs",
18313 : t, share_name,
18314 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18315 10 : remove = true;
18316 : }
18317 9542 : else if (TREE_READONLY (t)
18318 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18319 9555 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18320 : {
18321 4 : error_at (OMP_CLAUSE_LOCATION (c),
18322 : "%<const%> qualified %qE may appear only in "
18323 : "%<shared%> or %<firstprivate%> clauses", t);
18324 4 : remove = true;
18325 : }
18326 : }
18327 : }
18328 :
18329 22223 : if (remove)
18330 : {
18331 606 : if (grp_start_p)
18332 : {
18333 : /* If we found a clause to remove, we want to remove the whole
18334 : expanded group, otherwise gimplify
18335 : (omp_resolve_clause_dependencies) can get confused. */
18336 154 : *grp_start_p = grp_sentinel;
18337 154 : pc = grp_start_p;
18338 154 : grp_start_p = NULL;
18339 : }
18340 : else
18341 452 : *pc = OMP_CLAUSE_CHAIN (c);
18342 : }
18343 : else
18344 22202 : pc = &OMP_CLAUSE_CHAIN (c);
18345 : }
18346 :
18347 29470 : if (grp_start_p
18348 29470 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18349 52 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18350 32 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18351 :
18352 29470 : if (simdlen
18353 29470 : && safelen
18354 29587 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18355 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18356 : {
18357 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18358 : "%<simdlen%> clause value is bigger than "
18359 : "%<safelen%> clause value");
18360 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18361 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18362 : }
18363 :
18364 29470 : if (ordered_clause
18365 29470 : && schedule_clause
18366 29470 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18367 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18368 : {
18369 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18370 : "%<nonmonotonic%> schedule modifier specified together "
18371 : "with %<ordered%> clause");
18372 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18373 7 : = (enum omp_clause_schedule_kind)
18374 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18375 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18376 : }
18377 :
18378 29470 : if (reduction_seen < 0 && ordered_clause)
18379 : {
18380 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18381 : "%qs clause specified together with %<inscan%> "
18382 : "%<reduction%> clause", "ordered");
18383 2 : reduction_seen = -2;
18384 : }
18385 :
18386 29470 : if (reduction_seen < 0 && schedule_clause)
18387 : {
18388 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18389 : "%qs clause specified together with %<inscan%> "
18390 : "%<reduction%> clause", "schedule");
18391 3 : reduction_seen = -2;
18392 : }
18393 :
18394 29470 : if (linear_variable_step_check
18395 29470 : || reduction_seen == -2
18396 : || allocate_seen
18397 29458 : || target_in_reduction_seen)
18398 5190 : for (pc = &clauses, c = clauses; c ; c = *pc)
18399 : {
18400 4589 : bool remove = false;
18401 4589 : if (allocate_seen)
18402 4431 : switch (OMP_CLAUSE_CODE (c))
18403 : {
18404 430 : case OMP_CLAUSE_REDUCTION:
18405 430 : case OMP_CLAUSE_IN_REDUCTION:
18406 430 : case OMP_CLAUSE_TASK_REDUCTION:
18407 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18408 : {
18409 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18410 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18411 0 : t = TREE_OPERAND (t, 0);
18412 23 : if (TREE_CODE (t) == ADDR_EXPR
18413 10 : || INDIRECT_REF_P (t))
18414 13 : t = TREE_OPERAND (t, 0);
18415 23 : if (DECL_P (t))
18416 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18417 : break;
18418 : }
18419 : /* FALLTHRU */
18420 1318 : case OMP_CLAUSE_PRIVATE:
18421 1318 : case OMP_CLAUSE_FIRSTPRIVATE:
18422 1318 : case OMP_CLAUSE_LASTPRIVATE:
18423 1318 : case OMP_CLAUSE_LINEAR:
18424 1318 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18425 2636 : bitmap_clear_bit (&aligned_head,
18426 1318 : DECL_UID (OMP_CLAUSE_DECL (c)));
18427 : break;
18428 : default:
18429 : break;
18430 : }
18431 4589 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18432 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18433 4596 : && !bitmap_bit_p (&map_head,
18434 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18435 : {
18436 2 : error_at (OMP_CLAUSE_LOCATION (c),
18437 : "%<linear%> clause step is a parameter %qD not "
18438 : "specified in %<uniform%> clause",
18439 2 : OMP_CLAUSE_LINEAR_STEP (c));
18440 2 : remove = true;
18441 : }
18442 4587 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18443 4587 : && reduction_seen == -2)
18444 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18445 4589 : if (target_in_reduction_seen
18446 4589 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18447 : {
18448 256 : tree t = OMP_CLAUSE_DECL (c);
18449 256 : while (handled_component_p (t)
18450 : || INDIRECT_REF_P (t)
18451 : || TREE_CODE (t) == ADDR_EXPR
18452 : || TREE_CODE (t) == MEM_REF
18453 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18454 32 : t = TREE_OPERAND (t, 0);
18455 256 : if (DECL_P (t)
18456 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18457 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18458 : }
18459 :
18460 4589 : if (remove)
18461 2 : *pc = OMP_CLAUSE_CHAIN (c);
18462 : else
18463 4587 : pc = &OMP_CLAUSE_CHAIN (c);
18464 : }
18465 :
18466 601 : if (allocate_seen)
18467 5006 : for (pc = &clauses, c = clauses; c ; c = *pc)
18468 : {
18469 4431 : bool remove = false;
18470 4431 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18471 720 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18472 5131 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18473 : {
18474 3 : error_at (OMP_CLAUSE_LOCATION (c),
18475 : "%qD specified in %<allocate%> clause but not in "
18476 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18477 3 : remove = true;
18478 : }
18479 4431 : if (remove)
18480 3 : *pc = OMP_CLAUSE_CHAIN (c);
18481 : else
18482 4428 : pc = &OMP_CLAUSE_CHAIN (c);
18483 : }
18484 :
18485 29470 : if (nogroup_seen && reduction_seen)
18486 : {
18487 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18488 : "%<nogroup%> clause must not be used together with "
18489 : "%<reduction%> clause");
18490 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18491 : }
18492 :
18493 29470 : if (grainsize_seen && num_tasks_seen)
18494 : {
18495 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18496 : "%<grainsize%> clause must not be used together with "
18497 : "%<num_tasks%> clause");
18498 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18499 : }
18500 :
18501 29470 : if (full_seen && partial_seen)
18502 : {
18503 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18504 : "%<full%> clause must not be used together with "
18505 : "%<partial%> clause");
18506 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18507 : }
18508 :
18509 29470 : if (detach_seen)
18510 : {
18511 31 : if (mergeable_seen)
18512 : {
18513 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18514 : "%<detach%> clause must not be used together with "
18515 : "%<mergeable%> clause");
18516 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18517 : }
18518 : else
18519 : {
18520 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18521 :
18522 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18523 : {
18524 50 : bool remove = false;
18525 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18526 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18527 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18528 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18529 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18530 : {
18531 2 : error_at (OMP_CLAUSE_LOCATION (c),
18532 : "the event handle of a %<detach%> clause "
18533 : "should not be in a data-sharing clause");
18534 2 : remove = true;
18535 : }
18536 50 : if (remove)
18537 2 : *pc = OMP_CLAUSE_CHAIN (c);
18538 : else
18539 48 : pc = &OMP_CLAUSE_CHAIN (c);
18540 : }
18541 : }
18542 : }
18543 :
18544 29470 : if (ort == C_ORT_OMP_INTEROP
18545 29470 : && depend_clause
18546 21 : && (!init_use_destroy_seen
18547 20 : || (init_seen && init_no_targetsync_clause)))
18548 : {
18549 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18550 : "%<depend%> clause requires action clauses with "
18551 : "%<targetsync%> interop-type");
18552 3 : if (init_no_targetsync_clause)
18553 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18554 : "%<init%> clause lacks the %<targetsync%> modifier");
18555 : }
18556 :
18557 29470 : bitmap_obstack_release (NULL);
18558 29470 : return clauses;
18559 : }
18560 :
18561 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18562 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18563 :
18564 : tree
18565 13 : c_omp_finish_mapper_clauses (tree clauses)
18566 : {
18567 13 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18568 : }
18569 :
18570 : /* Return code to initialize DST with a copy constructor from SRC.
18571 : C doesn't have copy constructors nor assignment operators, only for
18572 : _Atomic vars we need to perform __atomic_load from src into a temporary
18573 : followed by __atomic_store of the temporary to dst. */
18574 :
18575 : tree
18576 18735 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18577 : {
18578 18735 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18579 18731 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18580 :
18581 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18582 4 : tree type = TREE_TYPE (dst);
18583 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18584 4 : tree tmp = create_tmp_var (nonatomic_type);
18585 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18586 4 : TREE_ADDRESSABLE (tmp) = 1;
18587 4 : suppress_warning (tmp);
18588 4 : tree src_addr = build_fold_addr_expr (src);
18589 4 : tree dst_addr = build_fold_addr_expr (dst);
18590 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18591 4 : vec<tree, va_gc> *params;
18592 : /* Expansion of a generic atomic load may require an addition
18593 : element, so allocate enough to prevent a resize. */
18594 4 : vec_alloc (params, 4);
18595 :
18596 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18597 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18598 4 : params->quick_push (src_addr);
18599 4 : params->quick_push (tmp_addr);
18600 4 : params->quick_push (seq_cst);
18601 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18602 :
18603 4 : vec_alloc (params, 4);
18604 :
18605 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18606 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18607 4 : params->quick_push (dst_addr);
18608 4 : params->quick_push (tmp_addr);
18609 4 : params->quick_push (seq_cst);
18610 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18611 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18612 : }
18613 :
18614 : /* Create a transaction node. */
18615 :
18616 : tree
18617 135 : c_finish_transaction (location_t loc, tree block, int flags)
18618 : {
18619 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18620 135 : if (flags & TM_STMT_ATTR_OUTER)
18621 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18622 135 : if (flags & TM_STMT_ATTR_RELAXED)
18623 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18624 135 : return add_stmt (stmt);
18625 : }
18626 :
18627 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18628 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18629 : NULL, then it should be used as the qualified type
18630 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18631 : preserve information about the typedef name from which an array
18632 : type was derived). */
18633 :
18634 : tree
18635 74545140 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18636 : size_t orig_qual_indirect)
18637 : {
18638 74545140 : if (type == error_mark_node)
18639 : return type;
18640 :
18641 74544988 : if (TREE_CODE (type) == ARRAY_TYPE)
18642 : {
18643 3162687 : tree t;
18644 3162687 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18645 : type_quals, orig_qual_type,
18646 : orig_qual_indirect - 1);
18647 :
18648 : /* See if we already have an identically qualified type. */
18649 3162687 : if (orig_qual_type && orig_qual_indirect == 0)
18650 : t = orig_qual_type;
18651 : else
18652 4605263 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18653 : {
18654 4465516 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18655 3043825 : && TYPE_NAME (t) == TYPE_NAME (type)
18656 3022899 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18657 7488415 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18658 3022899 : TYPE_ATTRIBUTES (type)))
18659 : break;
18660 : }
18661 3162646 : if (!t)
18662 : {
18663 139747 : tree domain = TYPE_DOMAIN (type);
18664 :
18665 139747 : t = build_variant_type_copy (type);
18666 139747 : TREE_TYPE (t) = element_type;
18667 139747 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18668 :
18669 139747 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18670 139747 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18671 196 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18672 139551 : else if (TYPE_CANONICAL (element_type) != element_type
18673 139551 : || (domain && TYPE_CANONICAL (domain) != domain))
18674 : {
18675 2777 : tree unqualified_canon
18676 5328 : = c_build_array_type (TYPE_CANONICAL (element_type),
18677 2551 : domain ? TYPE_CANONICAL (domain)
18678 : : NULL_TREE);
18679 2777 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18680 : {
18681 0 : unqualified_canon
18682 0 : = build_distinct_type_copy (unqualified_canon);
18683 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18684 : }
18685 2777 : TYPE_CANONICAL (t)
18686 5554 : = c_build_qualified_type (unqualified_canon, type_quals);
18687 : }
18688 : else
18689 136774 : TYPE_CANONICAL (t) = t;
18690 : }
18691 3162687 : return t;
18692 : }
18693 :
18694 : /* A restrict-qualified pointer type must be a pointer to object or
18695 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18696 : REFERENCE_TYPEs, which is appropriate for C++. */
18697 71382301 : if ((type_quals & TYPE_QUAL_RESTRICT)
18698 71382301 : && (!POINTER_TYPE_P (type)
18699 3382844 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18700 : {
18701 3 : error ("invalid use of %<restrict%>");
18702 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18703 : }
18704 :
18705 71382301 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18706 71382301 : ? orig_qual_type
18707 71382270 : : build_qualified_type (type, type_quals));
18708 :
18709 71382301 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18710 : == C_TYPE_VARIABLE_SIZE (type));
18711 71382301 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18712 : == c_type_variably_modified_p (type));
18713 :
18714 : /* A variant type does not inherit the list of incomplete vars from the
18715 : type main variant. */
18716 71382301 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18717 69460513 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18718 71487897 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18719 1413029 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18720 : return var_type;
18721 : }
18722 :
18723 : /* Build a VA_ARG_EXPR for the C parser. */
18724 :
18725 : tree
18726 19912 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18727 : tree type_expr)
18728 : {
18729 19912 : if (error_operand_p (type))
18730 2 : return error_mark_node;
18731 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18732 : order because it takes the address of the expression. */
18733 19910 : else if (handled_component_p (expr)
18734 31 : && reverse_storage_order_for_component_p (expr))
18735 : {
18736 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18737 0 : return error_mark_node;
18738 : }
18739 19910 : else if (!COMPLETE_TYPE_P (type))
18740 : {
18741 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18742 : "type %qT", type);
18743 11 : return error_mark_node;
18744 : }
18745 19899 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18746 : {
18747 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18748 : type);
18749 1 : return error_mark_node;
18750 : }
18751 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18752 19901 : && !flag_isoc99)
18753 : {
18754 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18755 : type);
18756 1 : return error_mark_node;
18757 : }
18758 19897 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18759 1 : warning_at (loc2, OPT_Wc___compat,
18760 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18761 :
18762 19897 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18763 : {
18764 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18765 : type);
18766 : /* We create a trap but evaluate side effects first. */
18767 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18768 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18769 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18770 : /* Return a compound literal of the right type. */
18771 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18772 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18773 3 : }
18774 : else
18775 19894 : expr = build_va_arg (loc2, expr, type);
18776 :
18777 19897 : if (type_expr)
18778 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18779 :
18780 : return expr;
18781 : }
18782 :
18783 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18784 : Return 1 if they are the same. Return false if they are different. */
18785 :
18786 : bool
18787 1854 : c_tree_equal (tree t1, tree t2)
18788 : {
18789 1883 : enum tree_code code1, code2;
18790 :
18791 1883 : if (t1 == t2)
18792 : return true;
18793 661 : if (!t1 || !t2)
18794 : return false;
18795 :
18796 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18797 20 : code1 = TREE_CODE (t1))
18798 20 : t1 = TREE_OPERAND (t1, 0);
18799 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18800 21 : code2 = TREE_CODE (t2))
18801 21 : t2 = TREE_OPERAND (t2, 0);
18802 :
18803 : /* They might have become equal now. */
18804 661 : if (t1 == t2)
18805 : return true;
18806 :
18807 640 : if (code1 != code2)
18808 : return false;
18809 :
18810 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18811 : return false;
18812 :
18813 379 : switch (code1)
18814 : {
18815 2 : case INTEGER_CST:
18816 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18817 :
18818 10 : case REAL_CST:
18819 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18820 :
18821 0 : case STRING_CST:
18822 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18823 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18824 0 : TREE_STRING_LENGTH (t1));
18825 :
18826 0 : case FIXED_CST:
18827 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18828 : TREE_FIXED_CST (t2));
18829 :
18830 0 : case COMPLEX_CST:
18831 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18832 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18833 :
18834 0 : case VECTOR_CST:
18835 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18836 :
18837 0 : case CONSTRUCTOR:
18838 : /* We need to do this when determining whether or not two
18839 : non-type pointer to member function template arguments
18840 : are the same. */
18841 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18842 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18843 : return false;
18844 : {
18845 : tree field, value;
18846 : unsigned int i;
18847 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18848 : {
18849 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18850 0 : if (!c_tree_equal (field, elt2->index)
18851 0 : || !c_tree_equal (value, elt2->value))
18852 0 : return false;
18853 : }
18854 : }
18855 : return true;
18856 :
18857 0 : case TREE_LIST:
18858 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18859 : return false;
18860 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18861 : return false;
18862 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18863 :
18864 0 : case SAVE_EXPR:
18865 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18866 :
18867 25 : case CALL_EXPR:
18868 25 : {
18869 25 : tree arg1, arg2;
18870 25 : call_expr_arg_iterator iter1, iter2;
18871 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18872 : return false;
18873 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18874 25 : arg2 = first_call_expr_arg (t2, &iter2);
18875 25 : arg1 && arg2;
18876 0 : arg1 = next_call_expr_arg (&iter1),
18877 0 : arg2 = next_call_expr_arg (&iter2))
18878 0 : if (!c_tree_equal (arg1, arg2))
18879 : return false;
18880 25 : if (arg1 || arg2)
18881 : return false;
18882 : return true;
18883 : }
18884 :
18885 0 : case TARGET_EXPR:
18886 0 : {
18887 0 : tree o1 = TREE_OPERAND (t1, 0);
18888 0 : tree o2 = TREE_OPERAND (t2, 0);
18889 :
18890 : /* Special case: if either target is an unallocated VAR_DECL,
18891 : it means that it's going to be unified with whatever the
18892 : TARGET_EXPR is really supposed to initialize, so treat it
18893 : as being equivalent to anything. */
18894 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18895 0 : && !DECL_RTL_SET_P (o1))
18896 : /*Nop*/;
18897 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18898 0 : && !DECL_RTL_SET_P (o2))
18899 : /*Nop*/;
18900 0 : else if (!c_tree_equal (o1, o2))
18901 : return false;
18902 :
18903 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18904 : }
18905 :
18906 29 : case COMPONENT_REF:
18907 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18908 : return false;
18909 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18910 :
18911 : case PARM_DECL:
18912 : case VAR_DECL:
18913 : case CONST_DECL:
18914 : case FIELD_DECL:
18915 : case FUNCTION_DECL:
18916 : case IDENTIFIER_NODE:
18917 : case SSA_NAME:
18918 : return false;
18919 :
18920 0 : case TREE_VEC:
18921 0 : {
18922 0 : unsigned ix;
18923 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18924 : return false;
18925 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18926 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18927 0 : TREE_VEC_ELT (t2, ix)))
18928 : return false;
18929 : return true;
18930 : }
18931 :
18932 22 : CASE_CONVERT:
18933 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18934 : return false;
18935 : break;
18936 :
18937 : default:
18938 : break;
18939 : }
18940 :
18941 131 : switch (TREE_CODE_CLASS (code1))
18942 : {
18943 131 : case tcc_unary:
18944 131 : case tcc_binary:
18945 131 : case tcc_comparison:
18946 131 : case tcc_expression:
18947 131 : case tcc_vl_exp:
18948 131 : case tcc_reference:
18949 131 : case tcc_statement:
18950 131 : {
18951 131 : int i, n = TREE_OPERAND_LENGTH (t1);
18952 :
18953 131 : switch (code1)
18954 : {
18955 0 : case PREINCREMENT_EXPR:
18956 0 : case PREDECREMENT_EXPR:
18957 0 : case POSTINCREMENT_EXPR:
18958 0 : case POSTDECREMENT_EXPR:
18959 0 : n = 1;
18960 0 : break;
18961 16 : case ARRAY_REF:
18962 16 : n = 2;
18963 16 : break;
18964 : default:
18965 : break;
18966 : }
18967 :
18968 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18969 131 : && n != TREE_OPERAND_LENGTH (t2))
18970 : return false;
18971 :
18972 305 : for (i = 0; i < n; ++i)
18973 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18974 : return false;
18975 :
18976 : return true;
18977 : }
18978 :
18979 0 : case tcc_type:
18980 0 : return comptypes (t1, t2);
18981 0 : default:
18982 0 : gcc_unreachable ();
18983 : }
18984 : }
18985 :
18986 : /* Returns true when the function declaration FNDECL is implicit,
18987 : introduced as a result of a call to an otherwise undeclared
18988 : function, and false otherwise. */
18989 :
18990 : bool
18991 2013 : c_decl_implicit (const_tree fndecl)
18992 : {
18993 2013 : return C_DECL_IMPLICIT (fndecl);
18994 : }
|