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 : static bool top_array_vla_p (const_tree type);
145 :
146 :
147 : /* Return true if EXP is a null pointer constant, false otherwise. */
148 :
149 : bool
150 192962451 : null_pointer_constant_p (const_tree expr)
151 : {
152 : /* This should really operate on c_expr structures, but they aren't
153 : yet available everywhere required. */
154 192962451 : tree type = TREE_TYPE (expr);
155 :
156 : /* An integer constant expression with the value 0, such an expression
157 : cast to type void*, or the predefined constant nullptr, are a null
158 : pointer constant. */
159 192962451 : if (expr == nullptr_node)
160 : return true;
161 :
162 192961767 : return (TREE_CODE (expr) == INTEGER_CST
163 19465201 : && !TREE_OVERFLOW (expr)
164 19465104 : && integer_zerop (expr)
165 196918414 : && (INTEGRAL_TYPE_P (type)
166 338583 : || (TREE_CODE (type) == POINTER_TYPE
167 338581 : && VOID_TYPE_P (TREE_TYPE (type))
168 263264 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
169 : }
170 :
171 : /* EXPR may appear in an unevaluated part of an integer constant
172 : expression, but not in an evaluated part. Wrap it in a
173 : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
174 : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
175 :
176 : static tree
177 62369 : note_integer_operands (tree expr)
178 : {
179 62369 : tree ret;
180 62369 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
181 : {
182 22 : ret = copy_node (expr);
183 22 : TREE_OVERFLOW (ret) = 1;
184 : }
185 : else
186 : {
187 62347 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
188 62347 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
189 : }
190 62369 : return ret;
191 : }
192 :
193 : /* Having checked whether EXPR may appear in an unevaluated part of an
194 : integer constant expression and found that it may, remove any
195 : C_MAYBE_CONST_EXPR noting this fact and return the resulting
196 : expression. */
197 :
198 : static inline tree
199 34474126 : remove_c_maybe_const_expr (tree expr)
200 : {
201 34474126 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
202 61517 : return C_MAYBE_CONST_EXPR_EXPR (expr);
203 : else
204 : return expr;
205 : }
206 :
207 : /* This is a cache to hold if two types are seen. */
208 :
209 : struct tagged_tu_seen_cache {
210 : const struct tagged_tu_seen_cache * next;
211 : const_tree t1;
212 : const_tree t2;
213 : };
214 :
215 : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
216 : does not have an incomplete type. (That includes void types.)
217 : LOC is the location of the use. */
218 :
219 : tree
220 792763614 : require_complete_type (location_t loc, tree value)
221 : {
222 792763614 : tree type = TREE_TYPE (value);
223 :
224 792763614 : if (error_operand_p (value))
225 8976 : return error_mark_node;
226 :
227 : /* First, detect a valid value with a complete type. */
228 792754638 : if (COMPLETE_TYPE_P (type))
229 : return value;
230 :
231 164 : c_incomplete_type_error (loc, value, type);
232 164 : return error_mark_node;
233 : }
234 :
235 : /* Print an error message for invalid use of an incomplete type.
236 : VALUE is the expression that was used (or 0 if that isn't known)
237 : and TYPE is the type that was invalid. LOC is the location for
238 : the error. */
239 :
240 : void
241 232 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
242 : {
243 : /* Avoid duplicate error message. */
244 232 : if (TREE_CODE (type) == ERROR_MARK)
245 : return;
246 :
247 232 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
248 98 : error_at (loc, "%qD has an incomplete type %qT", value, type);
249 : else
250 : {
251 134 : retry:
252 : /* We must print an error message. Be clever about what it says. */
253 :
254 134 : switch (TREE_CODE (type))
255 : {
256 90 : case RECORD_TYPE:
257 90 : case UNION_TYPE:
258 90 : case ENUMERAL_TYPE:
259 90 : break;
260 :
261 38 : case VOID_TYPE:
262 38 : error_at (loc, "invalid use of void expression");
263 38 : return;
264 :
265 6 : case ARRAY_TYPE:
266 6 : if (TYPE_DOMAIN (type))
267 : {
268 3 : if (flexible_array_member_type_p (type))
269 : {
270 3 : error_at (loc, "invalid use of flexible array member");
271 3 : return;
272 : }
273 0 : type = TREE_TYPE (type);
274 0 : goto retry;
275 : }
276 3 : error_at (loc, "invalid use of array with unspecified bounds");
277 3 : return;
278 :
279 0 : default:
280 0 : gcc_unreachable ();
281 : }
282 :
283 90 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
284 88 : error_at (loc, "invalid use of undefined type %qT", type);
285 : else
286 : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
288 : }
289 : }
290 :
291 : /* Given a type, apply default promotions wrt unnamed function
292 : arguments and return the new type. */
293 :
294 : tree
295 127802150 : c_type_promotes_to (tree type)
296 : {
297 127802150 : tree ret = NULL_TREE;
298 :
299 127802150 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 1619807 : ret = double_type_node;
301 126182343 : else if (c_promoting_integer_type_p (type))
302 : {
303 : /* Preserve unsignedness if not really getting any wider. */
304 19175094 : if (TYPE_UNSIGNED (type)
305 19175094 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 0 : ret = unsigned_type_node;
307 : else
308 19175094 : ret = integer_type_node;
309 : }
310 :
311 20794901 : if (ret != NULL_TREE)
312 20794901 : return (TYPE_ATOMIC (type)
313 20794901 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
314 : : ret);
315 :
316 : return type;
317 : }
318 :
319 : /* Return true if between two named address spaces, whether there is a superset
320 : named address space that encompasses both address spaces. If there is a
321 : superset, return which address space is the superset. */
322 :
323 : static bool
324 7713634 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
325 : {
326 7713634 : if (as1 == as2)
327 : {
328 7713634 : *common = as1;
329 7713634 : return true;
330 : }
331 0 : else if (targetm.addr_space.subset_p (as1, as2))
332 : {
333 0 : *common = as2;
334 0 : return true;
335 : }
336 0 : else if (targetm.addr_space.subset_p (as2, as1))
337 : {
338 0 : *common = as1;
339 0 : return true;
340 : }
341 : else
342 : return false;
343 : }
344 :
345 : /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 : as well as those of TYPE. */
347 :
348 : static tree
349 2909474 : qualify_type (tree type, tree like)
350 : {
351 2909474 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 2909474 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 2909474 : addr_space_t as_common;
354 :
355 : /* If the two named address spaces are different, determine the common
356 : superset address space. If there isn't one, raise an error. */
357 2909474 : if (!addr_space_superset (as_type, as_like, &as_common))
358 : {
359 0 : as_common = as_type;
360 0 : error ("%qT and %qT are in disjoint named address spaces",
361 : type, like);
362 : }
363 :
364 5818948 : return c_build_qualified_type (type,
365 2909474 : TYPE_QUALS_NO_ADDR_SPACE (type)
366 2909474 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
367 2909474 : | ENCODE_QUAL_ADDR_SPACE (as_common));
368 : }
369 :
370 :
371 : /* Check consistency of type TYPE. For derived types, we test that
372 : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
373 : the requirements of the base type. We also check that arrays with a
374 : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
375 : inconsistency is detected false is returned and true otherwise. */
376 :
377 : static bool
378 144889323 : c_verify_type (tree type)
379 : {
380 144889323 : switch (TREE_CODE (type))
381 : {
382 69380257 : case POINTER_TYPE:
383 69380257 : case FUNCTION_TYPE:
384 69380257 : case ARRAY_TYPE:
385 : /* Pointer, array, functions are variably modified if and only if the
386 : target, element, return type is variably modified. */
387 69380257 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
388 69380257 : && (C_TYPE_VARIABLY_MODIFIED (type)
389 68993841 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
390 : return false;
391 : /* If the target type is structural equality, the type should also. */
392 69380257 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
393 69380257 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
394 : return false;
395 144889323 : default:
396 144889323 : break;
397 : }
398 :
399 144889323 : switch (TREE_CODE (type))
400 : {
401 68460220 : case POINTER_TYPE:
402 68460220 : case FUNCTION_TYPE:
403 : /* Pointer and functions can not have variable size. */
404 68460220 : if (C_TYPE_VARIABLE_SIZE (type))
405 0 : return false;
406 : break;
407 920037 : case ARRAY_TYPE:
408 : /* An array has variable size if and only if it has a non-constant
409 : dimensions or its element type has variable size. */
410 920037 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
411 920037 : || top_array_vla_p (type))
412 920037 : != C_TYPE_VARIABLE_SIZE (type))
413 : return false;
414 : /* If the element type or the array has variable size, then the
415 : array has variable size and is variably modified. */
416 920037 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
417 920037 : || C_TYPE_VARIABLE_SIZE (type))
418 : {
419 27696 : if (!C_TYPE_VARIABLE_SIZE (type))
420 : return false;
421 27696 : if (!C_TYPE_VARIABLY_MODIFIED (type))
422 : return false;
423 : }
424 :
425 : /* va_list violates this, accept such types here. */
426 920037 : if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
427 : return true;
428 :
429 920031 : if (COMPLETE_TYPE_P (type))
430 : {
431 : /* If the size is unknown, it can not be complete. */
432 911819 : if (NULL_TREE == TYPE_DOMAIN (type))
433 : return false;
434 :
435 : /* Zero-length arrays must have size zero. */
436 911819 : if (zero_length_array_type_p (type)
437 911819 : && !integer_zerop (TYPE_SIZE (type)))
438 : return false;
439 : }
440 : else
441 : {
442 : /* If not complete but has a domain, it must be a FAM. */
443 8212 : if (NULL_TREE != TYPE_DOMAIN (type)
444 8212 : && !flexible_array_member_type_p (type))
445 : return false;
446 : }
447 : default:
448 : break;
449 : }
450 : return true;
451 : }
452 :
453 : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
454 : from a base type to a newly built derived or qualified type. */
455 :
456 : static tree
457 137617737 : c_set_type_bits (tree new_type, tree old_type)
458 : {
459 137617737 : gcc_checking_assert (c_verify_type (old_type));
460 :
461 137617737 : if (c_type_variably_modified_p (old_type))
462 28346 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
463 :
464 137617737 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
465 : {
466 16809 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
467 16809 : C_TYPE_VARIABLE_SIZE (new_type) = true;
468 : }
469 137617737 : return new_type;
470 : }
471 :
472 : /* Build a pointer type using the default pointer mode. */
473 :
474 : tree
475 73614608 : c_build_pointer_type (tree to_type)
476 : {
477 73614608 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
478 73614608 : : TYPE_ADDR_SPACE (to_type);
479 73614608 : machine_mode pointer_mode;
480 :
481 73614608 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
482 73614608 : pointer_mode = targetm.addr_space.pointer_mode (as);
483 : else
484 : pointer_mode = c_default_pointer_mode;
485 :
486 73614608 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
487 : }
488 :
489 : /* Build a pointer type using the given mode. */
490 :
491 : tree
492 73863439 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
493 : {
494 73863439 : tree ret = build_pointer_type_for_mode (type, mode, m);
495 73863439 : return c_set_type_bits (ret, type);
496 : }
497 :
498 : /* Build a function type. */
499 :
500 : tree
501 55026524 : c_build_function_type (tree type, tree args, bool no)
502 : {
503 55026524 : tree ret = build_function_type (type, args, no);
504 55026524 : return c_set_type_bits (ret, type);
505 : }
506 :
507 : /* Build an array type. This sets typeless storage as required
508 : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
509 : based on the element type and domain. */
510 :
511 : tree
512 1161916 : c_build_array_type (tree type, tree domain)
513 : {
514 1161916 : int type_quals = TYPE_QUALS (type);
515 :
516 : /* Identify typeless storage as introduced in C2Y
517 : and supported also in earlier language modes. */
518 1726388 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
519 1161916 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
520 :
521 1161916 : tree ret = build_array_type (type, domain, typeless);
522 :
523 1161916 : if (top_array_vla_p (ret))
524 : {
525 24272 : C_TYPE_VARIABLE_SIZE (ret) = 1;
526 24272 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
527 : }
528 :
529 1161916 : return c_set_type_bits (ret, type);
530 : }
531 :
532 : /* Build an array type of unspecified size. */
533 : tree
534 151 : c_build_array_type_unspecified (tree type)
535 : {
536 151 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
537 : integer_zero_node, size_zero_node);
538 151 : return c_build_array_type (type, build_index_type (upper));
539 : }
540 :
541 : /* Build an array type of zero size. */
542 : tree
543 2609 : c_build_array_type_zero_size (tree type)
544 : {
545 : /* The GCC extension for zero-length arrays differs from
546 : ISO flexible array members in that sizeof yields
547 : zero. */
548 2609 : type = c_build_array_type (type, build_index_type (NULL_TREE));
549 2609 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
550 2609 : TYPE_SIZE (type) = bitsize_zero_node;
551 2609 : TYPE_SIZE_UNIT (type) = size_zero_node;
552 2609 : SET_TYPE_STRUCTURAL_EQUALITY (type);
553 2609 : return type;
554 : }
555 :
556 : tree
557 7565858 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
558 : {
559 7565858 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
560 7565858 : return c_set_type_bits (ret, type);
561 : }
562 :
563 : tree
564 7376111 : c_build_type_attribute_variant (tree type, tree attrs)
565 : {
566 7376111 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
567 : }
568 :
569 : /* Reconstruct a complex derived type. This is used to re-construct types
570 : with the vector attribute. It is called via a langhook. */
571 :
572 : tree
573 843852 : c_reconstruct_complex_type (tree type, tree bottom)
574 : {
575 843852 : tree inner, outer;
576 :
577 843852 : if (TREE_CODE (type) == POINTER_TYPE)
578 : {
579 175372 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
580 175372 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
581 175372 : TYPE_REF_CAN_ALIAS_ALL (type));
582 : }
583 : else if (TREE_CODE (type) == ARRAY_TYPE)
584 : {
585 7397 : bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
586 7397 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
587 7397 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
588 7397 : if (rso)
589 : {
590 1 : outer = build_distinct_type_copy (outer);
591 1 : TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
592 : }
593 :
594 7397 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
595 : == C_TYPE_VARIABLE_SIZE (outer));
596 : }
597 : else if (TREE_CODE (type) == FUNCTION_TYPE)
598 : {
599 602 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
600 602 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
601 602 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
602 : }
603 : else if (TREE_CODE (type) == REFERENCE_TYPE
604 : || TREE_CODE (type) == OFFSET_TYPE)
605 0 : gcc_unreachable ();
606 : else
607 : return bottom;
608 :
609 183371 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
610 366742 : TYPE_QUALS (type));
611 : }
612 :
613 :
614 : /* Helper function for c_canonical_type. Check whether FIELD
615 : contains a pointer to a structure or union with tag,
616 : possibly nested in other type derivations, and return the
617 : type of this nested structure or union. */
618 : static tree
619 3449134 : ptr_to_tagged_member (tree field)
620 : {
621 3449134 : gcc_assert (FIELD_DECL == TREE_CODE (field));
622 3449134 : tree type = TREE_TYPE (field);
623 3449134 : bool ptr_seen = false;
624 3449134 : while (TREE_CODE (type) == ARRAY_TYPE
625 : || TREE_CODE (type) == FUNCTION_TYPE
626 4728891 : || TREE_CODE (type) == POINTER_TYPE)
627 : {
628 1279757 : if (TREE_CODE (type) == POINTER_TYPE)
629 701781 : ptr_seen = true;
630 1279757 : type = TREE_TYPE (type);
631 : }
632 :
633 3449134 : if (ptr_seen
634 681479 : && RECORD_OR_UNION_TYPE_P (type)
635 3698255 : && NULL_TREE != c_type_tag (type))
636 244966 : return type;
637 :
638 : return NULL_TREE;
639 : }
640 :
641 : /* For a record or union type, make necessary adaptations so that the
642 : type can be used as TYPE_CANONICAL.
643 :
644 : If the TYPE contains a pointer (possibly nested in other type
645 : derivations) to a structure or union as a member, create a copy and
646 : change the nested type to an incomplete type. Otherwise, the middle-end
647 : gets confused when recording component aliases in the case where we
648 : have formed equivalency classes that include types for which these
649 : member pointers end up pointing to other structure or unions types
650 : which have the same tag but are not compatible. */
651 : tree
652 889491 : c_type_canonical (tree type)
653 : {
654 889491 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
655 :
656 889491 : bool needs_mod = false;
657 3642296 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
658 : {
659 2834757 : if (!ptr_to_tagged_member (x))
660 2752805 : continue;
661 : needs_mod = true;
662 : break;
663 : }
664 889491 : if (!needs_mod)
665 : return type;
666 :
667 : /* Construct new version with such members replaced. */
668 81952 : tree n = build_distinct_type_copy (type);
669 81952 : tree *fields = &TYPE_FIELDS (n);
670 696329 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
671 : {
672 614377 : tree f = copy_node (x);
673 614377 : if (tree m = ptr_to_tagged_member (x))
674 : {
675 163014 : tree new_node = make_node (TREE_CODE (m));
676 163014 : TYPE_NAME (new_node) = TYPE_NAME (m);
677 163014 : SET_TYPE_STRUCTURAL_EQUALITY (new_node);
678 163014 : new_node = qualify_type (new_node, m);
679 163014 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
680 : new_node);
681 : }
682 614377 : *fields = f;
683 614377 : fields = &DECL_CHAIN (f);
684 : }
685 81952 : TYPE_CANONICAL (n) = n;
686 81952 : return n;
687 : }
688 :
689 :
690 : /* If NTYPE is a type of a non-variadic function with a prototype
691 : and OTYPE is a type of a function without a prototype and ATTRS
692 : contains attribute format, diagnosess and removes it from ATTRS.
693 : Returns the result of build_type_attribute_variant of NTYPE and
694 : the (possibly) modified ATTRS. */
695 :
696 : static tree
697 10773 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
698 : {
699 10773 : if (!prototype_p (otype)
700 10761 : && prototype_p (ntype)
701 21504 : && lookup_attribute ("format", attrs))
702 : {
703 14 : warning_at (input_location, OPT_Wattributes,
704 : "%qs attribute can only be applied to variadic functions",
705 : "format");
706 14 : attrs = remove_attribute ("format", attrs);
707 : }
708 10773 : return c_build_type_attribute_variant (ntype, attrs);
709 :
710 : }
711 :
712 : /* Given a type which could be a typedef name, make sure to return the
713 : original type. See set_underlying_type. */
714 : static const_tree
715 62493225 : c_type_original (const_tree t)
716 : {
717 : /* It may even be a typedef of a typedef...
718 : In the case of compiler-created builtin structs the TYPE_DECL
719 : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
720 62493225 : while (TYPE_NAME (t)
721 32111703 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
722 62537509 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
723 22140 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
724 62493225 : return t;
725 : }
726 :
727 : /* Return the tag for a tagged type. */
728 : tree
729 62493225 : c_type_tag (const_tree t)
730 : {
731 62493225 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
732 62493225 : const_tree orig = c_type_original (t);
733 62493225 : tree name = TYPE_NAME (orig);
734 62493225 : if (!name)
735 : return NULL_TREE;
736 32089563 : if (TREE_CODE (name) == TYPE_DECL)
737 : {
738 4 : if (!DECL_NAME (name))
739 : return NULL_TREE;
740 2 : name = DECL_NAME (name);
741 : }
742 32089561 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
743 : return name;
744 : }
745 :
746 : /* Remove qualifiers but not atomic. For arrays remove qualifiers
747 : on the element type but also do not remove atomic. */
748 : static tree
749 66204677 : remove_qualifiers (tree t)
750 : {
751 66204677 : if (!t || t == error_mark_node)
752 : return t;
753 66204652 : return TYPE_ATOMIC (strip_array_types (t))
754 66204652 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
755 66189077 : : TYPE_MAIN_VARIANT (t);
756 : }
757 :
758 :
759 : /* Helper function for composite_type_internal. Find a compatible type
760 : in a (transparent) union U compatible to T. If found, return the
761 : type of the corresponding member. Otherwise, return the union type U. */
762 : static tree
763 8727184 : transparent_union_replacement (tree u, tree t)
764 : {
765 8727179 : if (u == error_mark_node || t == error_mark_node
766 8727174 : || TREE_CODE (u) != UNION_TYPE
767 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
768 8727248 : || comptypes (u, t))
769 : return u;
770 :
771 80 : for (tree memb = TYPE_FIELDS (u); memb; memb = DECL_CHAIN (memb))
772 : {
773 80 : tree m = remove_qualifiers (TREE_TYPE (memb));
774 80 : if (comptypes (m, t))
775 : {
776 64 : pedwarn (input_location, OPT_Wpedantic,
777 : "function types not truly compatible in ISO C");
778 64 : return m;
779 : }
780 : }
781 :
782 : return u;
783 : }
784 :
785 :
786 :
787 : /* Return the composite type of two compatible types.
788 :
789 : We assume that comptypes has already been done and returned
790 : nonzero; if that isn't so, this may crash. In particular, we
791 : assume that qualifiers match. */
792 :
793 : struct composite_cache {
794 : tree t1;
795 : tree t2;
796 : tree composite;
797 : struct composite_cache* next;
798 : };
799 :
800 : tree
801 13639302 : composite_type_internal (tree t1, tree t2, tree cond,
802 : struct composite_cache* cache)
803 : {
804 13639302 : enum tree_code code1;
805 13639302 : enum tree_code code2;
806 13639302 : tree attributes;
807 :
808 : /* Save time if the two types are the same. */
809 :
810 13639302 : if (t1 == t2) return t1;
811 :
812 : /* If one type is nonsense, use the other. */
813 2788958 : if (t1 == error_mark_node)
814 : return t2;
815 2788954 : if (t2 == error_mark_node)
816 : return t1;
817 :
818 2788952 : code1 = TREE_CODE (t1);
819 2788952 : code2 = TREE_CODE (t2);
820 :
821 : /* Merge the attributes. */
822 2788952 : attributes = targetm.merge_type_attributes (t1, t2);
823 :
824 : /* If one is an enumerated type and the other is the compatible
825 : integer type, the composite type might be either of the two
826 : (DR#013 question 3). For consistency, use the enumerated type as
827 : the composite type. */
828 :
829 2788952 : if (code1 == ENUMERAL_TYPE
830 195 : && (code2 == INTEGER_TYPE
831 195 : || code2 == BOOLEAN_TYPE))
832 : return t1;
833 2788757 : if (code2 == ENUMERAL_TYPE
834 69 : && (code1 == INTEGER_TYPE
835 69 : || code1 == BOOLEAN_TYPE))
836 : return t2;
837 :
838 2788688 : gcc_assert (code1 == code2);
839 :
840 2788688 : switch (code1)
841 : {
842 6376 : case POINTER_TYPE:
843 : /* For two pointers, do this recursively on the target type. */
844 6376 : {
845 6376 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
846 6376 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
847 : cond, cache);
848 6376 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
849 12752 : return c_build_type_attribute_qual_variant (n, attributes,
850 6376 : TYPE_QUALS (t2));
851 : }
852 :
853 10996 : case ARRAY_TYPE:
854 10996 : {
855 10996 : tree d1 = TYPE_DOMAIN (t1);
856 10996 : tree d2 = TYPE_DOMAIN (t2);
857 :
858 : /* We should not have any type quals on arrays at all. */
859 10996 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
860 : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
861 :
862 10996 : bool d1_zero = zero_length_array_type_p (t1)
863 10996 : || flexible_array_member_type_p (t1);
864 10996 : bool d2_zero = zero_length_array_type_p (t2)
865 10996 : || flexible_array_member_type_p (t2);
866 :
867 10996 : bool d1_variable = top_array_vla_p (t1);
868 10996 : bool d2_variable = top_array_vla_p (t2);
869 :
870 10996 : bool use1 = d1 && (d2_variable || !d2 || d2_zero || !d1_variable);
871 10996 : bool use2 = d2 && (d1_variable || !d1 || d1_zero || !d2_variable);
872 :
873 : /* If the first is an unspecified size pick the other one. */
874 10996 : if (d2_variable && c_type_unspecified_p (t1))
875 : {
876 29 : gcc_assert (use1 && use2);
877 : use1 = false;
878 : }
879 :
880 : /* If both are VLAs but not unspecified and we are in the
881 : conditional operator, we create a conditional to select
882 : the size of the active branch. */
883 232 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
884 11225 : && d2_variable && !c_type_unspecified_p (t2);
885 :
886 10996 : tree td;
887 10996 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
888 : cond, cache);
889 :
890 10996 : if (!use0)
891 : {
892 : /* Save space: see if the result is identical to one of the args. */
893 10879 : if (elt == TREE_TYPE (t1) && use1)
894 10088 : return c_build_type_attribute_variant (t1, attributes);
895 791 : if (elt == TREE_TYPE (t2) && use2)
896 673 : return c_build_type_attribute_variant (t2, attributes);
897 :
898 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
899 10 : return c_build_type_attribute_variant (t1, attributes);
900 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
901 2 : return c_build_type_attribute_variant (t2, attributes);
902 :
903 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
904 : }
905 : else
906 : {
907 : /* Not used in C. */
908 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
909 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
910 :
911 117 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
912 117 : cond, TYPE_MAX_VALUE (d1),
913 117 : TYPE_MAX_VALUE (d2));
914 :
915 117 : td = build_index_type (d);
916 : }
917 :
918 : /* Merge the element types, and have a size if either arg has
919 : one. We may have qualifiers on the element types. To set
920 : up TYPE_MAIN_VARIANT correctly, we need to form the
921 : composite of the unqualified types and add the qualifiers
922 : back at the end. */
923 223 : int quals = TYPE_QUALS (strip_array_types (elt));
924 223 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
925 :
926 223 : if ((!d1 || d1_zero) && (!d2 || d2_zero) && (d1 || d2))
927 1 : t1 = c_build_array_type_zero_size (unqual_elt);
928 : else
929 222 : t1 = c_build_array_type (unqual_elt, td);
930 :
931 : /* Check that a type which has a varying outermost dimension
932 : got marked as having a variable size. */
933 223 : bool varsize = (d1_variable && d2_variable)
934 97 : || (d1_variable && !d2)
935 309 : || (d2_variable && !d1);
936 157 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
937 :
938 223 : t1 = c_build_qualified_type (t1, quals);
939 223 : return c_build_type_attribute_variant (t1, attributes);
940 : }
941 :
942 253 : case RECORD_TYPE:
943 253 : case UNION_TYPE:
944 253 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
945 : {
946 : /* Go to the original type to get the right tag. */
947 150 : tree tag = c_type_tag (t1);
948 :
949 150 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
950 150 : gcc_checking_assert (!tag || comptypes (t1, t2));
951 :
952 : /* If a composite type for these two types is already under
953 : construction, return it. */
954 :
955 376 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
956 246 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
957 20 : return c->composite;
958 :
959 : /* Otherwise, create a new type node and link it into the cache. */
960 :
961 130 : tree n = make_node (code1);
962 130 : SET_TYPE_STRUCTURAL_EQUALITY (n);
963 130 : TYPE_NAME (n) = tag;
964 :
965 130 : struct composite_cache cache2 = { t1, t2, n, cache };
966 130 : cache = &cache2;
967 :
968 130 : tree f1 = TYPE_FIELDS (t1);
969 130 : tree f2 = TYPE_FIELDS (t2);
970 130 : tree fields = NULL_TREE;
971 :
972 321 : for (tree a = f1, b = f2; a && b;
973 191 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
974 : {
975 191 : tree ta = TREE_TYPE (a);
976 191 : tree tb = TREE_TYPE (b);
977 :
978 191 : if (DECL_C_BIT_FIELD (a))
979 : {
980 19 : ta = DECL_BIT_FIELD_TYPE (a);
981 19 : tb = DECL_BIT_FIELD_TYPE (b);
982 : }
983 :
984 191 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
985 191 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
986 :
987 191 : tree t = composite_type_internal (ta, tb, cond, cache);
988 191 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
989 :
990 191 : DECL_PACKED (f) = DECL_PACKED (a);
991 191 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
992 191 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
993 191 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
994 :
995 191 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
996 :
997 191 : finish_decl (f, input_location, NULL, NULL, NULL);
998 :
999 191 : if (DECL_C_BIT_FIELD (a))
1000 : {
1001 : /* This will be processed by finish_struct. */
1002 19 : SET_DECL_C_BIT_FIELD (f);
1003 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
1004 19 : tree_to_uhwi (DECL_SIZE (a)));
1005 19 : DECL_NONADDRESSABLE_P (f) = true;
1006 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
1007 : }
1008 :
1009 191 : DECL_CHAIN (f) = fields;
1010 191 : fields = f;
1011 : }
1012 :
1013 130 : fields = nreverse (fields);
1014 :
1015 : /* Setup the struct/union type. Because we inherit all variably
1016 : modified components, we can ignore the size expression. */
1017 130 : tree expr = NULL_TREE;
1018 :
1019 : /* Set TYPE_STUB_DECL for debugging symbols. */
1020 130 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
1021 : NULL_TREE, n));
1022 :
1023 130 : n = finish_struct (input_location, n, fields, attributes, NULL,
1024 : &expr);
1025 :
1026 130 : return qualify_type (n, t1);
1027 : }
1028 : /* FALLTHRU */
1029 103 : case ENUMERAL_TYPE:
1030 103 : if (attributes != NULL)
1031 : {
1032 : /* Try harder not to create a new aggregate type. */
1033 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1034 : return t1;
1035 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1036 : return t2;
1037 : }
1038 99 : return c_build_type_attribute_variant (t1, attributes);
1039 :
1040 2721870 : case FUNCTION_TYPE:
1041 : /* Function types: prefer the one that specified arg types.
1042 : If both do, merge the arg types. Also merge the return types. */
1043 2721870 : {
1044 2721870 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1045 : cond, cache);
1046 2721870 : tree p1 = TYPE_ARG_TYPES (t1);
1047 2721870 : tree p2 = TYPE_ARG_TYPES (t2);
1048 :
1049 : /* Save space: see if the result is identical to one of the args. */
1050 2721870 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1051 10783 : return c_build_functype_attribute_variant (t1, t2, attributes);
1052 2711622 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
1053 525 : return c_build_functype_attribute_variant (t2, t1, attributes);
1054 :
1055 : /* Simple way if one arg fails to specify argument types. */
1056 2711097 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
1057 : {
1058 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
1059 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
1060 9 : t1 = c_build_type_attribute_variant (t1, attributes);
1061 9 : return qualify_type (t1, t2);
1062 : }
1063 2711088 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
1064 : {
1065 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
1066 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
1067 1 : t1 = c_build_type_attribute_variant (t1, attributes);
1068 1 : return qualify_type (t1, t2);
1069 : }
1070 :
1071 : /* If both args specify argument types, we must merge the two
1072 : lists, argument by argument. */
1073 2711087 : tree newargs = NULL_TREE;
1074 2711087 : tree *endp = &newargs;
1075 :
1076 7074679 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1077 : {
1078 6905926 : if (p1 == void_list_node)
1079 : {
1080 2542334 : *endp = void_list_node;
1081 2542334 : break;
1082 : }
1083 4363592 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1084 4363592 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1085 :
1086 4363592 : gcc_assert (mv1);
1087 4363592 : gcc_assert (mv2);
1088 :
1089 4363592 : mv1 = transparent_union_replacement (mv1, mv2);
1090 4363592 : mv2 = transparent_union_replacement (mv2, mv1);
1091 :
1092 4363592 : *endp = tree_cons (NULL_TREE,
1093 : composite_type_internal (mv1, mv2, cond, cache),
1094 : NULL_TREE);
1095 :
1096 4363592 : endp = &TREE_CHAIN (*endp);
1097 : }
1098 :
1099 2711087 : t1 = c_build_function_type (valtype, newargs);
1100 2711087 : t1 = qualify_type (t1, t2);
1101 : }
1102 : /* FALLTHRU */
1103 :
1104 2760280 : default:
1105 2760280 : return c_build_type_attribute_variant (t1, attributes);
1106 : }
1107 : }
1108 :
1109 : tree
1110 6536277 : composite_type_cond (tree t1, tree t2, tree cond)
1111 : {
1112 6536277 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1113 :
1114 6536277 : struct composite_cache cache = { };
1115 6536277 : tree n = composite_type_internal (t1, t2, cond, &cache);
1116 :
1117 6536277 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1118 6536277 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1119 6536277 : return n;
1120 : }
1121 :
1122 :
1123 : tree
1124 6531804 : composite_type (tree t1, tree t2)
1125 : {
1126 6531804 : return composite_type_cond (t1, t2, NULL_TREE);
1127 : }
1128 :
1129 : /* Return the type of a conditional expression between pointers to
1130 : possibly differently qualified versions of compatible types.
1131 :
1132 : We assume that comp_target_types has already been done and returned
1133 : true; if that isn't so, this may crash. */
1134 :
1135 : static tree
1136 116836 : common_pointer_type (tree t1, tree t2, tree cond)
1137 : {
1138 116836 : tree attributes;
1139 116836 : unsigned target_quals;
1140 116836 : addr_space_t as1, as2, as_common;
1141 116836 : int quals1, quals2;
1142 :
1143 : /* Save time if the two types are the same. */
1144 :
1145 116836 : if (t1 == t2) return t1;
1146 :
1147 : /* If one type is nonsense, use the other. */
1148 4473 : if (t1 == error_mark_node)
1149 : return t2;
1150 4473 : if (t2 == error_mark_node)
1151 : return t1;
1152 :
1153 4473 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1154 : && TREE_CODE (t2) == POINTER_TYPE);
1155 :
1156 : /* Merge the attributes. */
1157 4473 : attributes = targetm.merge_type_attributes (t1, t2);
1158 :
1159 : /* Find the composite type of the target types, and combine the
1160 : qualifiers of the two types' targets. */
1161 4473 : tree pointed_to_1 = TREE_TYPE (t1);
1162 4473 : tree pointed_to_2 = TREE_TYPE (t2);
1163 4473 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1164 4473 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1165 :
1166 : /* Strip array types to get correct qualifier for pointers to arrays */
1167 4473 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1168 4473 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1169 :
1170 : /* For function types do not merge const qualifiers, but drop them
1171 : if used inconsistently. The middle-end uses these to mark const
1172 : and noreturn functions. */
1173 4473 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1174 2129 : target_quals = (quals1 & quals2);
1175 : else
1176 2344 : target_quals = (quals1 | quals2);
1177 :
1178 : /* If the two named address spaces are different, determine the common
1179 : superset address space. This is guaranteed to exist due to the
1180 : assumption that comp_target_type returned true. */
1181 4473 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1182 4473 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1183 4473 : if (!addr_space_superset (as1, as2, &as_common))
1184 0 : gcc_unreachable ();
1185 :
1186 4473 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1187 :
1188 4473 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1189 4473 : return c_build_type_attribute_variant (t1, attributes);
1190 : }
1191 :
1192 : /* Return the common type for two arithmetic types under the usual
1193 : arithmetic conversions. The default conversions have already been
1194 : applied, and enumerated types converted to their compatible integer
1195 : types. The resulting type is unqualified and has no attributes.
1196 :
1197 : This is the type for the result of most arithmetic operations
1198 : if the operands have the given two types. */
1199 :
1200 : static tree
1201 14063669 : c_common_type (tree t1, tree t2)
1202 : {
1203 14063669 : enum tree_code code1;
1204 14063669 : enum tree_code code2;
1205 :
1206 : /* If one type is nonsense, use the other. */
1207 14063669 : if (t1 == error_mark_node)
1208 : return t2;
1209 14063669 : if (t2 == error_mark_node)
1210 : return t1;
1211 :
1212 14063669 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1213 38010 : t1 = TYPE_MAIN_VARIANT (t1);
1214 :
1215 14063669 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1216 49414 : t2 = TYPE_MAIN_VARIANT (t2);
1217 :
1218 14063669 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1219 : {
1220 189182 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1221 189182 : t1 = c_build_type_attribute_variant (t1, attrs);
1222 : }
1223 :
1224 14063669 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1225 : {
1226 188660 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1227 188660 : t2 = c_build_type_attribute_variant (t2, attrs);
1228 : }
1229 :
1230 : /* Save time if the two types are the same. */
1231 :
1232 14063669 : if (t1 == t2) return t1;
1233 :
1234 2339344 : code1 = TREE_CODE (t1);
1235 2339344 : code2 = TREE_CODE (t2);
1236 :
1237 2339344 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1238 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1239 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1240 2339344 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1241 : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1242 : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1243 :
1244 : /* When one operand is a decimal float type, the other operand cannot be
1245 : a generic float type or a complex type. We also disallow vector types
1246 : here. */
1247 2339344 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1248 2341767 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1249 : {
1250 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1251 : {
1252 3 : error ("cannot mix operands of decimal floating and vector types");
1253 3 : return error_mark_node;
1254 : }
1255 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1256 : {
1257 5 : error ("cannot mix operands of decimal floating and complex types");
1258 5 : return error_mark_node;
1259 : }
1260 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1261 : {
1262 16 : error ("cannot mix operands of decimal floating "
1263 : "and other floating types");
1264 16 : return error_mark_node;
1265 : }
1266 : }
1267 :
1268 : /* If one type is a vector type, return that type. (How the usual
1269 : arithmetic conversions apply to the vector types extension is not
1270 : precisely specified.) */
1271 2339320 : if (code1 == VECTOR_TYPE)
1272 : return t1;
1273 :
1274 2339028 : if (code2 == VECTOR_TYPE)
1275 : return t2;
1276 :
1277 : /* If one type is complex, form the common type of the non-complex
1278 : components, then make that complex. Use T1 or T2 if it is the
1279 : required type. */
1280 2339025 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1281 : {
1282 89358 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1283 89358 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1284 89358 : tree subtype = c_common_type (subtype1, subtype2);
1285 :
1286 89358 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1287 : return t1;
1288 59243 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1289 : return t2;
1290 3258 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1291 : {
1292 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1293 4 : TYPE_PRECISION (subtype));
1294 4 : return code1 == COMPLEX_TYPE ? t1 : t2;
1295 : }
1296 : else
1297 3254 : return build_complex_type (subtype);
1298 : }
1299 :
1300 : /* If only one is real, use it as the result. */
1301 :
1302 2249667 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1303 : return t1;
1304 :
1305 2120644 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1306 : return t2;
1307 :
1308 : /* If both are real and either are decimal floating point types, use
1309 : the decimal floating point type with the greater precision. */
1310 :
1311 2089307 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1312 : {
1313 107559 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1314 107559 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1315 : return dfloat128_type_node;
1316 : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1317 : implementation the same mode. */
1318 107010 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1319 107010 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1320 : return dfloat64x_type_node;
1321 107006 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1322 107006 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1323 : return dfloat64_type_node;
1324 106561 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1325 106561 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1326 : return dfloat32_type_node;
1327 : }
1328 :
1329 : /* Deal with fixed-point types. */
1330 2087964 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1331 : {
1332 0 : unsigned int unsignedp = 0, satp = 0;
1333 0 : scalar_mode m1, m2;
1334 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1335 :
1336 0 : m1 = SCALAR_TYPE_MODE (t1);
1337 0 : m2 = SCALAR_TYPE_MODE (t2);
1338 :
1339 : /* If one input type is saturating, the result type is saturating. */
1340 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1341 : satp = 1;
1342 :
1343 : /* If both fixed-point types are unsigned, the result type is unsigned.
1344 : When mixing fixed-point and integer types, follow the sign of the
1345 : fixed-point type.
1346 : Otherwise, the result type is signed. */
1347 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1348 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1349 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1350 0 : && TYPE_UNSIGNED (t1))
1351 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1352 0 : && TYPE_UNSIGNED (t2)))
1353 : unsignedp = 1;
1354 :
1355 : /* The result type is signed. */
1356 0 : if (unsignedp == 0)
1357 : {
1358 : /* If the input type is unsigned, we need to convert to the
1359 : signed type. */
1360 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1361 : {
1362 0 : enum mode_class mclass = (enum mode_class) 0;
1363 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1364 : mclass = MODE_FRACT;
1365 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1366 : mclass = MODE_ACCUM;
1367 : else
1368 0 : gcc_unreachable ();
1369 0 : m1 = as_a <scalar_mode>
1370 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1371 : }
1372 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1373 : {
1374 0 : enum mode_class mclass = (enum mode_class) 0;
1375 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1376 : mclass = MODE_FRACT;
1377 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1378 : mclass = MODE_ACCUM;
1379 : else
1380 0 : gcc_unreachable ();
1381 0 : m2 = as_a <scalar_mode>
1382 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1383 : }
1384 : }
1385 :
1386 0 : if (code1 == FIXED_POINT_TYPE)
1387 : {
1388 0 : fbit1 = GET_MODE_FBIT (m1);
1389 0 : ibit1 = GET_MODE_IBIT (m1);
1390 : }
1391 : else
1392 : {
1393 0 : fbit1 = 0;
1394 : /* Signed integers need to subtract one sign bit. */
1395 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1396 : }
1397 :
1398 0 : if (code2 == FIXED_POINT_TYPE)
1399 : {
1400 0 : fbit2 = GET_MODE_FBIT (m2);
1401 0 : ibit2 = GET_MODE_IBIT (m2);
1402 : }
1403 : else
1404 : {
1405 0 : fbit2 = 0;
1406 : /* Signed integers need to subtract one sign bit. */
1407 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1408 : }
1409 :
1410 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1411 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1412 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1413 : satp);
1414 : }
1415 :
1416 : /* Both real or both integers; use the one with greater precision. */
1417 :
1418 2087964 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1419 : return t1;
1420 1113073 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1421 : return t2;
1422 :
1423 : /* Same precision. Prefer long longs to longs to ints when the
1424 : same precision, following the C99 rules on integer type rank
1425 : (which are equivalent to the C90 rules for C90 types). */
1426 :
1427 747487 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1428 747487 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1429 : return long_long_unsigned_type_node;
1430 :
1431 658227 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1432 658227 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1433 : {
1434 15318 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1435 361 : return long_long_unsigned_type_node;
1436 : else
1437 : return long_long_integer_type_node;
1438 : }
1439 :
1440 642909 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1441 642909 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1442 : return long_unsigned_type_node;
1443 :
1444 543604 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1445 543604 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1446 : {
1447 : /* But preserve unsignedness from the other type,
1448 : since long cannot hold all the values of an unsigned int. */
1449 26467 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1450 129 : return long_unsigned_type_node;
1451 : else
1452 : return long_integer_type_node;
1453 : }
1454 :
1455 : /* For floating types of the same TYPE_PRECISION (which we here
1456 : assume means either the same set of values, or sets of values
1457 : neither a subset of the other, with behavior being undefined in
1458 : the latter case), follow the rules from TS 18661-3: prefer
1459 : interchange types _FloatN, then standard types long double,
1460 : double, float, then extended types _FloatNx. For extended types,
1461 : check them starting with _Float128x as that seems most consistent
1462 : in spirit with preferring long double to double; for interchange
1463 : types, also check in that order for consistency although it's not
1464 : possible for more than one of them to have the same
1465 : precision. */
1466 517137 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1467 517137 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1468 :
1469 2585039 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1470 2068085 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1471 : return FLOATN_TYPE_NODE (i);
1472 :
1473 : /* Likewise, prefer long double to double even if same size. */
1474 516954 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1475 : return long_double_type_node;
1476 :
1477 : /* Likewise, prefer double to float even if same size.
1478 : We got a couple of embedded targets with 32 bit doubles, and the
1479 : pdp11 might have 64 bit floats. */
1480 516696 : if (mv1 == double_type_node || mv2 == double_type_node)
1481 : return double_type_node;
1482 :
1483 516124 : if (mv1 == float_type_node || mv2 == float_type_node)
1484 : return float_type_node;
1485 :
1486 2055756 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1487 1541817 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1488 : return FLOATNX_TYPE_NODE (i);
1489 :
1490 513939 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1491 : {
1492 : /* Prefer any other integral types over bit-precise integer types. */
1493 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1494 4 : return code1 == BITINT_TYPE ? t2 : t1;
1495 : /* If BITINT_TYPE is unsigned and the other type is signed
1496 : non-BITINT_TYPE with the same precision, the latter has higher rank.
1497 : In that case:
1498 : Otherwise, both operands are converted to the unsigned integer type
1499 : corresponding to the type of the operand with signed integer type. */
1500 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1501 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1502 : }
1503 :
1504 : /* Otherwise prefer the unsigned one. */
1505 :
1506 513931 : if (TYPE_UNSIGNED (t1))
1507 : return t1;
1508 : else
1509 212303 : return t2;
1510 : }
1511 :
1512 : /* Wrapper around c_common_type that is used by c-common.cc and other
1513 : front end optimizations that remove promotions. ENUMERAL_TYPEs
1514 : are allowed here and are converted to their compatible integer types.
1515 : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1516 : preferably a non-Boolean type as the common type. */
1517 : tree
1518 104085 : common_type (tree t1, tree t2)
1519 : {
1520 104085 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1521 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1522 104085 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1523 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1524 :
1525 : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1526 104085 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1527 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1528 584 : return boolean_type_node;
1529 :
1530 : /* If either type is BOOLEAN_TYPE, then return the other. */
1531 103501 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1532 : return t2;
1533 103501 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1534 : return t1;
1535 :
1536 103501 : return c_common_type (t1, t2);
1537 : }
1538 :
1539 :
1540 :
1541 : /* Helper function for comptypes. For two compatible types, return 1
1542 : if they pass consistency checks. In particular we test that
1543 : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1544 :
1545 : static bool
1546 43296194 : comptypes_verify (tree type1, tree type2)
1547 : {
1548 43296194 : if (type1 == type2 || !type1 || !type2
1549 3635794 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1550 : return true;
1551 :
1552 3635793 : gcc_checking_assert (c_verify_type (type1));
1553 3635793 : gcc_checking_assert (c_verify_type (type2));
1554 :
1555 3635793 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1556 403315 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1557 4037617 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1558 : {
1559 : /* FIXME: check other types. */
1560 401513 : if (RECORD_OR_UNION_TYPE_P (type1)
1561 401513 : || TREE_CODE (type1) == ENUMERAL_TYPE
1562 401513 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1563 0 : return false;
1564 : }
1565 : return true;
1566 : }
1567 :
1568 : struct comptypes_data {
1569 :
1570 : /* output */
1571 : bool enum_and_int_p;
1572 : bool different_types_p;
1573 : bool warning_needed;
1574 :
1575 : /* context */
1576 : bool anon_field;
1577 : bool pointedto;
1578 :
1579 : /* configuration */
1580 : bool equiv;
1581 : bool ignore_promoting_args;
1582 :
1583 : const struct tagged_tu_seen_cache* cache;
1584 : };
1585 :
1586 :
1587 : /* Helper function for composite_type. This function ignores when the
1588 : function type of an old-style declaration is incompatible with a type
1589 : of a declaration with prototype because some are arguments are not
1590 : self-promoting. This is ignored only for function types but not
1591 : ignored in a nested context. */
1592 :
1593 : static bool
1594 19608831 : comptypes_check_for_composite (tree t1, tree t2)
1595 : {
1596 19608831 : struct comptypes_data data = { };
1597 19608831 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1598 19608831 : return comptypes_internal (t1, t2, &data);
1599 : }
1600 :
1601 :
1602 : /* C implementation of compatible_types_for_indirection_note_p. */
1603 :
1604 : bool
1605 762 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1606 : {
1607 762 : return comptypes (type1, type2);
1608 : }
1609 :
1610 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1611 : or various other operations. */
1612 :
1613 : bool
1614 56786265 : comptypes (tree type1, tree type2)
1615 : {
1616 56786265 : struct comptypes_data data = { };
1617 56786265 : bool ret = comptypes_internal (type1, type2, &data);
1618 :
1619 56786265 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1620 :
1621 56786265 : return ret;
1622 : }
1623 :
1624 :
1625 : /* Like comptypes, but it returns non-zero only for identical
1626 : types. */
1627 :
1628 : bool
1629 46444 : comptypes_same_p (tree type1, tree type2)
1630 : {
1631 46444 : struct comptypes_data data = { };
1632 46444 : bool ret = comptypes_internal (type1, type2, &data);
1633 :
1634 46444 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1635 :
1636 46444 : if (data.different_types_p)
1637 163 : return false;
1638 :
1639 : return ret;
1640 : }
1641 :
1642 :
1643 : /* Like comptypes, but if it returns true because enum and int are
1644 : compatible, it sets *ENUM_AND_INT_P to true. */
1645 :
1646 : bool
1647 5181817 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1648 : {
1649 5181817 : struct comptypes_data data = { };
1650 5181817 : bool ret = comptypes_internal (type1, type2, &data);
1651 5181817 : *enum_and_int_p = data.enum_and_int_p;
1652 :
1653 5181817 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1654 :
1655 5181817 : return ret;
1656 : }
1657 :
1658 :
1659 :
1660 : /* Like comptypes, but if it returns true for struct and union types
1661 : considered equivalent for aliasing purposes, i.e. for setting
1662 : TYPE_CANONICAL after completing a struct or union.
1663 :
1664 : This function must return false only for types which are not
1665 : compatible according to C language semantics (cf. comptypes),
1666 : otherwise the middle-end would make incorrect aliasing decisions.
1667 : It may return true for some similar types that are not compatible
1668 : according to those stricter rules.
1669 :
1670 : In particular, we ignore size expression in arrays so that the
1671 : following structs are in the same equivalence class:
1672 :
1673 : struct foo { char (*buf)[]; };
1674 : struct foo { char (*buf)[3]; };
1675 : struct foo { char (*buf)[4]; };
1676 :
1677 : We also treat unions / structs with members which are pointers to
1678 : structures or unions with the same tag as equivalent (if they are not
1679 : incompatible for other reasons). Although incomplete structure
1680 : or union types are not compatible to any other type, they may become
1681 : compatible to different types when completed. To avoid having to update
1682 : TYPE_CANONICAL at this point, we only consider the tag when forming
1683 : the equivalence classes. For example, the following types with tag
1684 : 'foo' are all considered equivalent:
1685 :
1686 : struct bar;
1687 : struct foo { struct bar *x };
1688 : struct foo { struct bar { int a; } *x };
1689 : struct foo { struct bar { char b; } *x }; */
1690 :
1691 : bool
1692 14689026 : comptypes_equiv_p (tree type1, tree type2)
1693 : {
1694 14689026 : struct comptypes_data data = { };
1695 14689026 : data.equiv = true;
1696 14689026 : bool ret = comptypes_internal (type1, type2, &data);
1697 :
1698 : /* check that different equivance classes are assigned only
1699 : to types that are not compatible. */
1700 14689026 : gcc_checking_assert (ret || !comptypes (type1, type2));
1701 :
1702 14689026 : return ret;
1703 : }
1704 :
1705 :
1706 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1707 : or various other operations. If they are compatible but a warning may
1708 : be needed if you use them together, 'warning_needed' in DATA is set.
1709 : If one type is an enum and the other a compatible integer type, then
1710 : this sets 'enum_and_int_p' in DATA to true (it is never set to
1711 : false). If the types are compatible but different enough not to be
1712 : permitted in C11 typedef redeclarations, then this sets
1713 : 'different_types_p' in DATA to true; it is never set to
1714 : false, but may or may not be set if the types are incompatible.
1715 : If two functions types are not compatible only because one is
1716 : an old-style definition that does not have self-promoting arguments,
1717 : then this can be ignored by setting 'ignore_promoting_args_p'.
1718 : For 'equiv' we can compute equivalency classes (see above). */
1719 :
1720 : static bool
1721 136224524 : comptypes_internal (const_tree type1, const_tree type2,
1722 : struct comptypes_data *data)
1723 : {
1724 136509076 : const_tree t1 = type1;
1725 136509076 : const_tree t2 = type2;
1726 :
1727 : /* Suppress errors caused by previously reported errors. */
1728 :
1729 136509076 : if (t1 == t2 || !t1 || !t2
1730 45799310 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1731 : return true;
1732 :
1733 : /* Qualifiers must match. C99 6.7.3p9 */
1734 :
1735 45799307 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1736 : return false;
1737 :
1738 : /* Enumerated types are compatible with integer types, but this is
1739 : not transitive: two enumerated types in the same translation unit
1740 : are compatible with each other only if they are the same type. */
1741 :
1742 45759086 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1743 1004 : && COMPLETE_TYPE_P (t1)
1744 45760046 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1745 : {
1746 844 : t1 = ENUM_UNDERLYING_TYPE (t1);
1747 844 : if (TREE_CODE (t2) != VOID_TYPE)
1748 : {
1749 840 : data->enum_and_int_p = true;
1750 840 : data->different_types_p = true;
1751 : }
1752 : }
1753 45758242 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1754 4733 : && COMPLETE_TYPE_P (t2)
1755 45762969 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1756 : {
1757 4611 : t2 = ENUM_UNDERLYING_TYPE (t2);
1758 4611 : if (TREE_CODE (t1) != VOID_TYPE)
1759 : {
1760 3712 : data->enum_and_int_p = true;
1761 3712 : data->different_types_p = true;
1762 : }
1763 : }
1764 :
1765 45759086 : if (t1 == t2)
1766 : return true;
1767 :
1768 : /* Different classes of types can't be compatible. */
1769 :
1770 45757706 : if (TREE_CODE (t1) != TREE_CODE (t2))
1771 : return false;
1772 :
1773 : /* Allow for two different type nodes which have essentially the same
1774 : definition. Note that we already checked for equality of the type
1775 : qualifiers (just above). */
1776 :
1777 37717497 : if (TREE_CODE (t1) != ARRAY_TYPE
1778 37717497 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1779 : return true;
1780 :
1781 37348809 : int attrval;
1782 :
1783 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1784 37348809 : if (!(attrval = comp_type_attributes (t1, t2)))
1785 : return false;
1786 :
1787 37348432 : if (2 == attrval)
1788 336 : data->warning_needed = true;
1789 :
1790 37348432 : switch (TREE_CODE (t1))
1791 : {
1792 3374045 : case INTEGER_TYPE:
1793 3374045 : case FIXED_POINT_TYPE:
1794 3374045 : case REAL_TYPE:
1795 3374045 : case BITINT_TYPE:
1796 : /* With these nodes, we can't determine type equivalence by
1797 : looking at what is stored in the nodes themselves, because
1798 : two nodes might have different TYPE_MAIN_VARIANTs but still
1799 : represent the same type. For example, wchar_t and int could
1800 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1801 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1802 : and are distinct types. On the other hand, int and the
1803 : following typedef
1804 :
1805 : typedef int INT __attribute((may_alias));
1806 :
1807 : have identical properties, different TYPE_MAIN_VARIANTs, but
1808 : represent the same type. The canonical type system keeps
1809 : track of equivalence in this case, so we fall back on it. */
1810 3374045 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1811 :
1812 284552 : case POINTER_TYPE:
1813 : /* Do not remove mode information. */
1814 284552 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1815 : return false;
1816 284552 : data->pointedto = true;
1817 284552 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1818 :
1819 9471040 : case FUNCTION_TYPE:
1820 9471040 : return function_types_compatible_p (t1, t2, data);
1821 :
1822 154969 : case ARRAY_TYPE:
1823 154969 : {
1824 154969 : tree d1 = TYPE_DOMAIN (t1);
1825 154969 : tree d2 = TYPE_DOMAIN (t2);
1826 :
1827 : /* Target types must match incl. qualifiers. */
1828 154969 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1829 : return false;
1830 :
1831 120005 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1832 6354 : data->different_types_p = true;
1833 : /* Ignore size mismatches when forming equivalence classes. */
1834 120005 : if (data->equiv)
1835 : return true;
1836 : /* Sizes must match unless one is missing or variable. */
1837 50406 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1838 : return true;
1839 :
1840 34127 : bool d1_variable = top_array_vla_p (t1);
1841 34127 : bool d2_variable = top_array_vla_p (t2);
1842 :
1843 34127 : if (d1_variable != d2_variable)
1844 943 : data->different_types_p = true;
1845 34127 : if (d1_variable || d2_variable)
1846 : return true;
1847 :
1848 4027 : bool d1_zero = zero_length_array_type_p (t1)
1849 4027 : || flexible_array_member_type_p (t1);
1850 :
1851 4027 : bool d2_zero = zero_length_array_type_p (t2)
1852 4027 : || flexible_array_member_type_p (t2);
1853 :
1854 93 : if (d1_zero && d2_zero)
1855 : return true;
1856 4027 : if (d1_zero || d2_zero)
1857 : return false;
1858 :
1859 3931 : if (!tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1860 3931 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1861 3931 : return false;
1862 :
1863 : return true;
1864 : }
1865 :
1866 23524417 : case ENUMERAL_TYPE:
1867 23524417 : case RECORD_TYPE:
1868 23524417 : case UNION_TYPE:
1869 :
1870 23524417 : if (!flag_isoc23)
1871 : return false;
1872 :
1873 23524370 : return tagged_types_tu_compatible_p (t1, t2, data);
1874 :
1875 538604 : case VECTOR_TYPE:
1876 1077208 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1877 538604 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1878 :
1879 : default:
1880 : return false;
1881 : }
1882 : gcc_unreachable ();
1883 : }
1884 :
1885 : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1886 : their qualifiers, except for named address spaces. If the pointers point to
1887 : different named addresses, then we must determine if one address space is a
1888 : subset of the other. */
1889 :
1890 : static bool
1891 1779936 : comp_target_types (location_t location, tree ttl, tree ttr)
1892 : {
1893 1779936 : tree mvl = TREE_TYPE (ttl);
1894 1779936 : tree mvr = TREE_TYPE (ttr);
1895 1779936 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1896 1779936 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1897 1779936 : addr_space_t as_common;
1898 :
1899 : /* Fail if pointers point to incompatible address spaces. */
1900 1779936 : if (!addr_space_superset (asl, asr, &as_common))
1901 : return 0;
1902 :
1903 : /* Qualifiers on element types of array types that are
1904 : pointer targets are also removed. */
1905 1779936 : struct comptypes_data data = { };
1906 1779936 : if (!comptypes_internal (remove_qualifiers (mvl),
1907 1779936 : remove_qualifiers (mvr), &data))
1908 : return false;
1909 :
1910 : /* For pedantic use comptypes on arrays before removing
1911 : qualifiers on the element type. */
1912 1678494 : if (TREE_CODE (mvl) == ARRAY_TYPE
1913 1891 : && TREE_CODE (mvr) == ARRAY_TYPE
1914 1680385 : && !comptypes (mvl, mvr))
1915 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays "
1916 : "with different qualifiers in ISO C "
1917 : "before C23");
1918 :
1919 1678494 : if (data.warning_needed)
1920 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1921 :
1922 1678494 : if (data.enum_and_int_p && warn_cxx_compat)
1923 2 : warning_at (location, OPT_Wc___compat,
1924 : "pointer target types incompatible in C++");
1925 :
1926 : return true;
1927 : }
1928 :
1929 : /* Subroutines of `comptypes'. */
1930 :
1931 : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1932 : compatible. The two types are not the same (which has been
1933 : checked earlier in comptypes_internal). */
1934 :
1935 : static bool
1936 23524370 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1937 : struct comptypes_data *data)
1938 : {
1939 23524370 : tree s1, s2;
1940 :
1941 23524370 : if (c_type_tag (t1) != c_type_tag (t2))
1942 : return false;
1943 :
1944 : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1945 : structs with the same tag as equivalent, but only when they are targets
1946 : of pointers inside other structs. */
1947 19445669 : if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1948 : return true;
1949 :
1950 : /* Different types without tag are incompatible except as an anonymous
1951 : field or when forming equivalence classes for TYPE_CANONICAL. */
1952 19445607 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1953 : return false;
1954 :
1955 14284715 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1956 14283654 : data->different_types_p = true;
1957 :
1958 : /* Incomplete types are incompatible inside a TU. */
1959 14284715 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1960 : return false;
1961 :
1962 14284705 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1963 14284705 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1964 14284650 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1965 : return false;
1966 :
1967 14284695 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1968 144976 : data->different_types_p = true;
1969 :
1970 : /* For types already being looked at in some active
1971 : invocation of this function, assume compatibility.
1972 : The cache is built as a linked list on the stack
1973 : with the head of the list passed downwards. */
1974 14284695 : for (const struct tagged_tu_seen_cache *t = data->cache;
1975 14294388 : t != NULL; t = t->next)
1976 9988 : if (t->t1 == t1 && t->t2 == t2)
1977 : return true;
1978 :
1979 14284400 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1980 :
1981 14284400 : switch (TREE_CODE (t1))
1982 : {
1983 55 : case ENUMERAL_TYPE:
1984 55 : {
1985 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1986 : return false;
1987 :
1988 : /* Speed up the case where the type values are in the same order. */
1989 51 : tree tv1 = TYPE_VALUES (t1);
1990 51 : tree tv2 = TYPE_VALUES (t2);
1991 :
1992 51 : if (tv1 == tv2)
1993 : return true;
1994 :
1995 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1996 : {
1997 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1998 : break;
1999 :
2000 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
2001 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
2002 : break;
2003 : }
2004 :
2005 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
2006 : return true;
2007 :
2008 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
2009 : return false;
2010 :
2011 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
2012 : return false;
2013 :
2014 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
2015 : {
2016 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
2017 :
2018 16 : if (s2 == NULL
2019 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
2020 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
2021 : return false;
2022 : }
2023 :
2024 : return true;
2025 : }
2026 :
2027 14284345 : case UNION_TYPE:
2028 14284345 : case RECORD_TYPE:
2029 :
2030 14284345 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2031 : return false;
2032 :
2033 12334429 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2034 13857333 : s1 && s2;
2035 1522904 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2036 : {
2037 13791664 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2038 13791664 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2039 :
2040 13791664 : tree ft1 = TREE_TYPE (s1);
2041 13791664 : tree ft2 = TREE_TYPE (s2);
2042 :
2043 13791664 : if (DECL_NAME (s1) != DECL_NAME (s2))
2044 : return false;
2045 :
2046 12137804 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2047 : return false;
2048 :
2049 3234537 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2050 : return false;
2051 :
2052 3234517 : if (DECL_C_BIT_FIELD (s1))
2053 : {
2054 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2055 : return false;
2056 :
2057 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2058 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2059 : }
2060 :
2061 3234363 : if (!ft1 || !ft2)
2062 : return false;
2063 :
2064 3234363 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2065 : return false;
2066 :
2067 3234362 : data->anon_field = !DECL_NAME (s1);
2068 3234362 : data->pointedto = false;
2069 :
2070 3234362 : const struct tagged_tu_seen_cache *cache = data->cache;
2071 3234362 : data->cache = &entry;
2072 3234362 : bool ret = comptypes_internal (ft1, ft2, data);
2073 3234362 : data->cache = cache;
2074 3234362 : if (!ret)
2075 : return false;
2076 :
2077 1592391 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2078 1592391 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2079 :
2080 1592391 : if (data->equiv
2081 998107 : && st1 && TREE_CODE (st1) == INTEGER_CST
2082 997625 : && st2 && TREE_CODE (st2) == INTEGER_CST
2083 2590010 : && !tree_int_cst_equal (st1, st2))
2084 : return false;
2085 :
2086 1522938 : tree counted_by1 = lookup_attribute ("counted_by",
2087 1522938 : DECL_ATTRIBUTES (s1));
2088 1522938 : tree counted_by2 = lookup_attribute ("counted_by",
2089 1522938 : DECL_ATTRIBUTES (s2));
2090 : /* If there is no counted_by attribute for both fields. */
2091 1522938 : if (!counted_by1 && !counted_by2)
2092 1522874 : continue;
2093 :
2094 : /* If only one field has counted_by attribute. */
2095 64 : if ((counted_by1 && !counted_by2)
2096 64 : || (!counted_by1 && counted_by2))
2097 : return false;
2098 :
2099 : /* Now both s1 and s2 have counted_by attributes, check
2100 : whether they are the same. */
2101 :
2102 52 : tree counted_by_field1
2103 52 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2104 52 : tree counted_by_field2
2105 52 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2106 :
2107 52 : gcc_assert (counted_by_field1 && counted_by_field2);
2108 :
2109 94 : while (TREE_CHAIN (counted_by_field1))
2110 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2111 86 : while (TREE_CHAIN (counted_by_field2))
2112 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2113 :
2114 52 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2115 52 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2116 : return false;
2117 : }
2118 : return true;
2119 :
2120 0 : default:
2121 0 : gcc_unreachable ();
2122 : }
2123 : }
2124 :
2125 : /* Return true if two function types F1 and F2 are compatible.
2126 : If either type specifies no argument types,
2127 : the other must specify a fixed number of self-promoting arg types.
2128 : Otherwise, if one type specifies only the number of arguments,
2129 : the other must specify that number of self-promoting arg types.
2130 : Otherwise, the argument types must match. */
2131 :
2132 : static bool
2133 9471040 : function_types_compatible_p (const_tree f1, const_tree f2,
2134 : struct comptypes_data *data)
2135 : {
2136 9471040 : tree ret1 = TREE_TYPE (f1);
2137 9471040 : tree ret2 = TREE_TYPE (f2);
2138 :
2139 : /* 'volatile' qualifiers on a function's return type used to mean
2140 : the function is noreturn. */
2141 9471040 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2142 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2143 9471040 : if (TYPE_VOLATILE (ret1))
2144 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2145 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2146 9471040 : if (TYPE_VOLATILE (ret2))
2147 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2148 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2149 :
2150 9471040 : bool ignore_pargs = data->ignore_promoting_args;
2151 9471040 : data->ignore_promoting_args = false;
2152 :
2153 9471040 : if (!comptypes_internal (ret1, ret2, data))
2154 : return false;
2155 :
2156 9468445 : data->ignore_promoting_args = ignore_pargs;
2157 :
2158 9468445 : tree args1 = TYPE_ARG_TYPES (f1);
2159 9468445 : tree args2 = TYPE_ARG_TYPES (f2);
2160 :
2161 9468445 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2162 45273 : data->different_types_p = true;
2163 :
2164 : /* An unspecified parmlist matches any specified parmlist
2165 : whose argument types don't need default promotions. */
2166 :
2167 9468445 : if (args1 == NULL_TREE)
2168 : {
2169 23777 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2170 : return false;
2171 23774 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2172 : return false;
2173 23302 : data->ignore_promoting_args = false;
2174 : /* If one of these types comes from a non-prototype fn definition,
2175 : compare that with the other type's arglist.
2176 : If they don't match, ask for a warning (but no error). */
2177 23302 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2178 23302 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2179 12 : data->warning_needed = true;
2180 : return true;
2181 : }
2182 9444668 : if (args2 == NULL_TREE)
2183 : {
2184 21578 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2185 : return false;
2186 21576 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2187 : return false;
2188 21515 : data->ignore_promoting_args = false;
2189 21515 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2190 21515 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2191 1 : data->warning_needed = true;
2192 : return true;
2193 : }
2194 :
2195 : /* Both types have argument lists: compare them and propagate results. */
2196 9423090 : return type_lists_compatible_p (args1, args2, data);
2197 : }
2198 :
2199 : /* Check two lists of types for compatibility, returning false for
2200 : incompatible, true for compatible. */
2201 :
2202 : static bool
2203 9423166 : type_lists_compatible_p (const_tree args1, const_tree args2,
2204 : struct comptypes_data *data)
2205 : {
2206 58989786 : while (1)
2207 : {
2208 34206476 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2209 : return true;
2210 : /* If one list is shorter than the other,
2211 : they fail to match. */
2212 24928921 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2213 : return false;
2214 24928914 : tree a1 = TREE_VALUE (args1);
2215 24928914 : tree a2 = TREE_VALUE (args2);
2216 24928914 : tree mv1 = remove_qualifiers (a1);
2217 24928914 : tree mv2 = remove_qualifiers (a2);
2218 :
2219 24928914 : gcc_assert (mv2);
2220 24928914 : gcc_assert (mv2);
2221 :
2222 : /* If one of the lists has an error marker, ignore this arg. */
2223 24928914 : if (TREE_CODE (a1) == ERROR_MARK
2224 24928910 : || TREE_CODE (a2) == ERROR_MARK)
2225 : ;
2226 24928894 : else if (!comptypes_internal (mv1, mv2, data))
2227 : {
2228 145835 : data->different_types_p = true;
2229 : /* Allow wait (union {union wait *u; int *i} *)
2230 : and wait (union wait *) to be compatible. */
2231 145835 : if (TREE_CODE (a1) == UNION_TYPE
2232 130 : && (TYPE_NAME (a1) == NULL_TREE
2233 98 : || TYPE_TRANSPARENT_AGGR (a1))
2234 130 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2235 145965 : && tree_int_cst_equal (TYPE_SIZE (a1),
2236 130 : TYPE_SIZE (a2)))
2237 : {
2238 130 : tree memb;
2239 130 : for (memb = TYPE_FIELDS (a1);
2240 166 : memb; memb = DECL_CHAIN (memb))
2241 : {
2242 164 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2243 164 : if (comptypes_internal (mv3, mv2, data))
2244 : break;
2245 : }
2246 : if (memb == NULL_TREE)
2247 : return false;
2248 : }
2249 145705 : else if (TREE_CODE (a2) == UNION_TYPE
2250 105 : && (TYPE_NAME (a2) == NULL_TREE
2251 74 : || TYPE_TRANSPARENT_AGGR (a2))
2252 105 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2253 145810 : && tree_int_cst_equal (TYPE_SIZE (a2),
2254 105 : TYPE_SIZE (a1)))
2255 : {
2256 105 : tree memb;
2257 105 : for (memb = TYPE_FIELDS (a2);
2258 133 : memb; memb = DECL_CHAIN (memb))
2259 : {
2260 131 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2261 131 : if (comptypes_internal (mv3, mv1, data))
2262 : break;
2263 : }
2264 : if (memb == NULL_TREE)
2265 : return false;
2266 : }
2267 : else
2268 : return false;
2269 : }
2270 :
2271 24783310 : args1 = TREE_CHAIN (args1);
2272 24783310 : args2 = TREE_CHAIN (args2);
2273 24783310 : }
2274 : }
2275 :
2276 : /* Compute the size to increment a pointer by. When a function type or void
2277 : type or incomplete type is passed, size_one_node is returned.
2278 : This function does not emit any diagnostics; the caller is responsible
2279 : for that. */
2280 :
2281 : static tree
2282 236779 : c_size_in_bytes (const_tree type)
2283 : {
2284 236779 : enum tree_code code = TREE_CODE (type);
2285 :
2286 236779 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2287 236779 : || !COMPLETE_TYPE_P (type))
2288 191 : return size_one_node;
2289 :
2290 : /* Convert in case a char is more than one unit. */
2291 236588 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2292 236588 : size_int (TYPE_PRECISION (char_type_node)
2293 : / BITS_PER_UNIT));
2294 : }
2295 :
2296 : /* Return either DECL or its known constant value (if it has one). */
2297 :
2298 : tree
2299 14414083 : decl_constant_value_1 (tree decl, bool in_init)
2300 : {
2301 14414083 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2302 14414083 : TREE_CODE (decl) != PARM_DECL
2303 14414083 : && !TREE_THIS_VOLATILE (decl)
2304 14068581 : && TREE_READONLY (decl)
2305 37098 : && DECL_INITIAL (decl) != NULL_TREE
2306 36084 : && !error_operand_p (DECL_INITIAL (decl))
2307 : /* This is invalid if initial value is not constant.
2308 : If it has either a function call, a memory reference,
2309 : or a variable, then re-evaluating it could give different results. */
2310 36075 : && TREE_CONSTANT (DECL_INITIAL (decl))
2311 : /* Check for cases where this is sub-optimal, even though valid. */
2312 14437010 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2313 17651 : return DECL_INITIAL (decl);
2314 : return decl;
2315 : }
2316 :
2317 : /* Return either DECL or its known constant value (if it has one).
2318 : Like the above, but always return decl outside of functions. */
2319 :
2320 : tree
2321 14413819 : decl_constant_value (tree decl)
2322 : {
2323 : /* Don't change a variable array bound or initial value to a constant
2324 : in a place where a variable is invalid. */
2325 14413819 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2326 : }
2327 :
2328 : /* Convert the array expression EXP to a pointer. */
2329 : static tree
2330 830454 : array_to_pointer_conversion (location_t loc, tree exp)
2331 : {
2332 830454 : tree orig_exp = exp;
2333 830454 : tree type = TREE_TYPE (exp);
2334 830454 : tree adr;
2335 830454 : tree restype = TREE_TYPE (type);
2336 830454 : tree ptrtype;
2337 :
2338 830454 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2339 :
2340 830454 : STRIP_TYPE_NOPS (exp);
2341 :
2342 830454 : copy_warning (exp, orig_exp);
2343 :
2344 830454 : ptrtype = c_build_pointer_type (restype);
2345 :
2346 830454 : if (INDIRECT_REF_P (exp))
2347 2906 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2348 :
2349 : /* In C++ array compound literals are temporary objects unless they are
2350 : const or appear in namespace scope, so they are destroyed too soon
2351 : to use them for much of anything (c++/53220). */
2352 827548 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2353 : {
2354 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2355 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2356 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2357 : "converting an array compound literal to a pointer "
2358 : "leads to a dangling pointer in C++");
2359 : }
2360 :
2361 827548 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2362 827548 : return convert (ptrtype, adr);
2363 : }
2364 :
2365 : /* Convert the function expression EXP to a pointer. */
2366 : static tree
2367 52176396 : function_to_pointer_conversion (location_t loc, tree exp)
2368 : {
2369 52176396 : tree orig_exp = exp;
2370 :
2371 52176396 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2372 :
2373 52176396 : STRIP_TYPE_NOPS (exp);
2374 :
2375 52176396 : copy_warning (exp, orig_exp);
2376 :
2377 52176396 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2378 :
2379 : /* If the function is defined and known to not require a non-local
2380 : context, make sure no trampoline is generated. */
2381 52176396 : if (TREE_CODE (exp) == FUNCTION_DECL
2382 52176396 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2383 16633192 : TREE_NO_TRAMPOLINE (exp2) = 1;
2384 :
2385 52176396 : return exp2;
2386 : }
2387 :
2388 : /* Mark EXP as read, not just set, for set but not used -Wunused
2389 : warning purposes. */
2390 :
2391 : void
2392 501440717 : mark_exp_read (tree exp)
2393 : {
2394 631517623 : switch (TREE_CODE (exp))
2395 : {
2396 208030590 : case VAR_DECL:
2397 208030590 : case PARM_DECL:
2398 208030590 : DECL_READ_P (exp) = 1;
2399 208030590 : break;
2400 20369421 : CASE_CONVERT:
2401 20369421 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2402 3806 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2403 : {
2404 : case PREINCREMENT_EXPR:
2405 : case PREDECREMENT_EXPR:
2406 : case POSTINCREMENT_EXPR:
2407 : case POSTDECREMENT_EXPR:
2408 : return;
2409 : default:
2410 : break;
2411 : }
2412 : /* FALLTHRU */
2413 127320926 : case ARRAY_REF:
2414 127320926 : case COMPONENT_REF:
2415 127320926 : case MODIFY_EXPR:
2416 127320926 : case REALPART_EXPR:
2417 127320926 : case IMAGPART_EXPR:
2418 127320926 : case ADDR_EXPR:
2419 127320926 : case VIEW_CONVERT_EXPR:
2420 127320926 : case PREINCREMENT_EXPR:
2421 127320926 : case PREDECREMENT_EXPR:
2422 127320926 : case POSTINCREMENT_EXPR:
2423 127320926 : case POSTDECREMENT_EXPR:
2424 127320926 : mark_exp_read (TREE_OPERAND (exp, 0));
2425 127320926 : break;
2426 236120 : case COMPOUND_EXPR:
2427 : /* Pattern match what build_atomic_assign produces with modifycode
2428 : NOP_EXPR. */
2429 236120 : if (VAR_P (TREE_OPERAND (exp, 1))
2430 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2431 263263 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2432 : {
2433 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2434 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2435 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2436 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2437 4410 : && TREE_CODE (t2) == CALL_EXPR)
2438 : {
2439 2200 : tree fndecl = get_callee_fndecl (t2);
2440 2200 : tree arg = NULL_TREE;
2441 2200 : if (fndecl
2442 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2443 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2444 4400 : && call_expr_nargs (t2) >= 2)
2445 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2446 : {
2447 130 : case BUILT_IN_ATOMIC_STORE:
2448 130 : arg = CALL_EXPR_ARG (t2, 1);
2449 130 : break;
2450 2070 : case BUILT_IN_ATOMIC_STORE_1:
2451 2070 : case BUILT_IN_ATOMIC_STORE_2:
2452 2070 : case BUILT_IN_ATOMIC_STORE_4:
2453 2070 : case BUILT_IN_ATOMIC_STORE_8:
2454 2070 : case BUILT_IN_ATOMIC_STORE_16:
2455 2070 : arg = CALL_EXPR_ARG (t2, 0);
2456 2070 : break;
2457 : default:
2458 : break;
2459 : }
2460 2200 : if (arg)
2461 : {
2462 2200 : STRIP_NOPS (arg);
2463 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2464 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2465 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2466 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2467 : }
2468 : }
2469 : }
2470 : /* FALLTHRU */
2471 2755435 : case C_MAYBE_CONST_EXPR:
2472 2755435 : mark_exp_read (TREE_OPERAND (exp, 1));
2473 2755435 : break;
2474 580 : case OMP_ARRAY_SECTION:
2475 580 : mark_exp_read (TREE_OPERAND (exp, 0));
2476 580 : if (TREE_OPERAND (exp, 1))
2477 386 : mark_exp_read (TREE_OPERAND (exp, 1));
2478 580 : if (TREE_OPERAND (exp, 2))
2479 545 : mark_exp_read (TREE_OPERAND (exp, 2));
2480 : break;
2481 : default:
2482 : break;
2483 : }
2484 : }
2485 :
2486 : /* Perform the default conversion of arrays and functions to pointers.
2487 : Return the result of converting EXP. For any other expression, just
2488 : return EXP.
2489 :
2490 : LOC is the location of the expression. */
2491 :
2492 : struct c_expr
2493 371023962 : default_function_array_conversion (location_t loc, struct c_expr exp)
2494 : {
2495 371023962 : tree orig_exp = exp.value;
2496 371023962 : tree type = TREE_TYPE (exp.value);
2497 371023962 : enum tree_code code = TREE_CODE (type);
2498 :
2499 371023962 : switch (code)
2500 : {
2501 : case ARRAY_TYPE:
2502 : {
2503 : bool not_lvalue = false;
2504 : bool lvalue_array_p;
2505 :
2506 783525 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2507 783525 : || CONVERT_EXPR_P (exp.value))
2508 783525 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2509 : {
2510 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2511 0 : not_lvalue = true;
2512 0 : exp.value = TREE_OPERAND (exp.value, 0);
2513 : }
2514 :
2515 783525 : copy_warning (exp.value, orig_exp);
2516 :
2517 783525 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2518 783525 : if (!flag_isoc99 && !lvalue_array_p)
2519 : {
2520 : /* Before C99, non-lvalue arrays do not decay to pointers.
2521 : Normally, using such an array would be invalid; but it can
2522 : be used correctly inside sizeof or as a statement expression.
2523 : Thus, do not give an error here; an error will result later. */
2524 109 : return exp;
2525 : }
2526 :
2527 783416 : exp.value = array_to_pointer_conversion (loc, exp.value);
2528 : }
2529 783416 : break;
2530 762378 : case FUNCTION_TYPE:
2531 762378 : exp.value = function_to_pointer_conversion (loc, exp.value);
2532 762378 : break;
2533 : default:
2534 : break;
2535 : }
2536 :
2537 371023853 : return exp;
2538 : }
2539 :
2540 : struct c_expr
2541 12679 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2542 : {
2543 12679 : mark_exp_read (exp.value);
2544 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2545 12679 : if (TREE_CODE (exp.value) == COMPONENT_REF
2546 12679 : && handle_counted_by_p (exp.value))
2547 4 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2548 12679 : return default_function_array_conversion (loc, exp);
2549 : }
2550 :
2551 : /* Return whether EXPR should be treated as an atomic lvalue for the
2552 : purposes of load and store handling. */
2553 :
2554 : static bool
2555 372139231 : really_atomic_lvalue (tree expr)
2556 : {
2557 372139231 : if (error_operand_p (expr))
2558 : return false;
2559 372131645 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2560 : return false;
2561 57423 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2562 : return false;
2563 57420 : if (!lvalue_p (expr))
2564 : return false;
2565 :
2566 : /* Ignore _Atomic on register variables, since their addresses can't
2567 : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2568 : sequences wouldn't work. Ignore _Atomic on structures containing
2569 : bit-fields, since accessing elements of atomic structures or
2570 : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2571 : it's undefined at translation time or execution time, and the
2572 : normal atomic sequences again wouldn't work. */
2573 57562 : while (handled_component_p (expr))
2574 : {
2575 144 : if (TREE_CODE (expr) == COMPONENT_REF
2576 144 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2577 : return false;
2578 144 : expr = TREE_OPERAND (expr, 0);
2579 : }
2580 57418 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2581 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2582 112632 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2583 7 : return false;
2584 : return true;
2585 : }
2586 :
2587 : /* If EXPR is a named constant (C23) derived from a constexpr variable
2588 : - that is, a reference to such a variable, or a member extracted by
2589 : a sequence of structure and union (but not array) member accesses
2590 : (where union member accesses must access the same member as
2591 : initialized) - then return the corresponding initializer;
2592 : otherwise, return NULL_TREE. */
2593 :
2594 : static tree
2595 368919460 : maybe_get_constexpr_init (tree expr)
2596 : {
2597 368919460 : tree decl = NULL_TREE;
2598 368919460 : if (TREE_CODE (expr) == VAR_DECL)
2599 : decl = expr;
2600 357139641 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2601 825951 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2602 825951 : if (decl
2603 12605770 : && C_DECL_DECLARED_CONSTEXPR (decl)
2604 390 : && DECL_INITIAL (decl) != NULL_TREE
2605 826341 : && !error_operand_p (DECL_INITIAL (decl)))
2606 390 : return DECL_INITIAL (decl);
2607 368919070 : if (TREE_CODE (expr) != COMPONENT_REF)
2608 : return NULL_TREE;
2609 878518 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2610 878518 : if (inner == NULL_TREE)
2611 : return NULL_TREE;
2612 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2613 47 : && !error_operand_p (inner)
2614 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2615 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2616 47 : inner = TREE_OPERAND (inner, 0);
2617 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2618 : return NULL_TREE;
2619 126 : tree field = TREE_OPERAND (expr, 1);
2620 126 : unsigned HOST_WIDE_INT cidx;
2621 126 : tree cfield, cvalue;
2622 126 : bool have_other_init = false;
2623 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2624 : {
2625 250 : if (cfield == field)
2626 : return cvalue;
2627 140 : have_other_init = true;
2628 : }
2629 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2630 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2631 : return NULL_TREE;
2632 : /* Return a default initializer. */
2633 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2634 5 : return build_constructor (TREE_TYPE (expr), NULL);
2635 8 : return build_zero_cst (TREE_TYPE (expr));
2636 : }
2637 :
2638 : /* Helper function for convert_lvalue_to_rvalue called via
2639 : walk_tree_without_duplicates. Find DATA inside of the expression. */
2640 :
2641 : static tree
2642 230349 : c_find_var_r (tree *tp, int *walk_subtrees, void *data)
2643 : {
2644 230349 : if (TYPE_P (*tp))
2645 0 : *walk_subtrees = 0;
2646 230349 : else if (*tp == (tree) data)
2647 0 : return *tp;
2648 : return NULL_TREE;
2649 : }
2650 :
2651 : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2652 : including converting functions and arrays to pointers if CONVERT_P.
2653 : If READ_P, also mark the expression as having been read. If
2654 : FOR_INIT, constexpr expressions of structure and union type should
2655 : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2656 : constexpr scalars (including elements of structures and unions) are
2657 : replaced by their initializers. */
2658 :
2659 : struct c_expr
2660 368228729 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2661 : bool convert_p, bool read_p, bool for_init)
2662 : {
2663 368228729 : bool force_non_npc = false;
2664 368228729 : if (read_p)
2665 323103746 : mark_exp_read (exp.value);
2666 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2667 323103746 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2668 785410 : && handle_counted_by_p (exp.value))
2669 785381 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2670 :
2671 368228729 : if (convert_p)
2672 368215423 : exp = default_function_array_conversion (loc, exp);
2673 368228729 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2674 368228729 : || (flag_isoc2y
2675 1574 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2676 365716365 : exp.value = require_complete_type (loc, exp.value);
2677 368228729 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2678 : {
2679 368040942 : tree init = maybe_get_constexpr_init (exp.value);
2680 368040942 : if (init != NULL_TREE)
2681 : {
2682 : /* A named constant of pointer type or type nullptr_t is not
2683 : a null pointer constant even if the initializer is
2684 : one. */
2685 387 : if (TREE_CODE (init) == INTEGER_CST
2686 252 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2687 413 : && integer_zerop (init))
2688 : force_non_npc = true;
2689 : exp.value = init;
2690 : }
2691 : }
2692 368228729 : if (really_atomic_lvalue (exp.value))
2693 : {
2694 26944 : vec<tree, va_gc> *params;
2695 26944 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2696 26944 : tree expr_type = TREE_TYPE (exp.value);
2697 26944 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2698 26944 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2699 :
2700 26944 : gcc_assert (TYPE_ATOMIC (expr_type));
2701 :
2702 : /* Expansion of a generic atomic load may require an addition
2703 : element, so allocate enough to prevent a resize. */
2704 26944 : vec_alloc (params, 4);
2705 :
2706 : /* Remove the qualifiers for the rest of the expressions and
2707 : create the VAL temp variable to hold the RHS. */
2708 26944 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2709 26944 : tmp = create_tmp_var_raw (nonatomic_type);
2710 26944 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2711 26944 : TREE_ADDRESSABLE (tmp) = 1;
2712 : /* Do not disable warnings for TMP even though it's artificial.
2713 : -Winvalid-memory-model depends on it. */
2714 :
2715 : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2716 26944 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2717 26944 : params->quick_push (expr_addr);
2718 26944 : params->quick_push (tmp_addr);
2719 26944 : params->quick_push (seq_cst);
2720 26944 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2721 :
2722 : /* EXPR is always read. */
2723 26944 : mark_exp_read (exp.value);
2724 :
2725 : /* Optimize the common case where c_build_function_call_vec
2726 : immediately folds __atomic_load (&expr, &tmp, SEQ_CST); into
2727 : tmp = __atomic_load_<N> (&expr, SEQ_CST);
2728 : In that case tmp is not addressable and can be initialized
2729 : fully by the rhs of the MODIFY_EXPR. */
2730 26944 : tree tem = func_call;
2731 26944 : if (CONVERT_EXPR_P (tem) && VOID_TYPE_P (TREE_TYPE (tem)))
2732 : {
2733 25689 : tem = TREE_OPERAND (tem, 0);
2734 25689 : if (TREE_CODE (tem) == MODIFY_EXPR
2735 25689 : && TREE_OPERAND (tem, 0) == tmp
2736 51378 : && !walk_tree_without_duplicates (&TREE_OPERAND (tem, 1),
2737 : c_find_var_r, tmp))
2738 : {
2739 25689 : TREE_ADDRESSABLE (tmp) = 0;
2740 25689 : func_call = TREE_OPERAND (tem, 1);
2741 : }
2742 : }
2743 :
2744 : /* Return tmp which contains the value loaded. */
2745 26944 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2746 : NULL_TREE, NULL_TREE);
2747 : }
2748 368215423 : if (convert_p && !error_operand_p (exp.value)
2749 736436611 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2750 368207773 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2751 368228729 : if (force_non_npc)
2752 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2753 :
2754 368228729 : {
2755 368228729 : tree false_value, true_value;
2756 368215423 : if (convert_p && !error_operand_p (exp.value)
2757 736436611 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2758 : &false_value, &true_value))
2759 : {
2760 9721 : tree t = save_expr (exp.value);
2761 :
2762 9721 : mark_exp_read (exp.value);
2763 :
2764 9721 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2765 9721 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2766 9721 : expr = build_compound_expr (loc, expr, boolean_true_node);
2767 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2768 : fold_build2_loc (loc, NE_EXPR,
2769 : boolean_type_node,
2770 : t, true_value),
2771 : expr, boolean_true_node);
2772 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2773 : fold_build2_loc (loc, NE_EXPR,
2774 : boolean_type_node,
2775 : t, false_value),
2776 : expr, boolean_false_node);
2777 :
2778 9721 : exp.value = expr;
2779 : }
2780 : }
2781 :
2782 368228729 : return exp;
2783 : }
2784 :
2785 : /* Wrapper for the overload above, same arguments but for tree rather than
2786 : c_expr. This is important for hardbools to decay to bools. */
2787 :
2788 : static inline tree
2789 578440 : convert_lvalue_to_rvalue (location_t loc, tree val,
2790 : bool convert_p, bool read_p, bool for_init = false)
2791 : {
2792 578440 : struct c_expr expr;
2793 578440 : memset (&expr, 0, sizeof (expr));
2794 578440 : expr.value = val;
2795 578440 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2796 578440 : return expr.value;
2797 : }
2798 :
2799 : /* EXP is an expression of integer type. Apply the integer promotions
2800 : to it and return the promoted value. */
2801 :
2802 : tree
2803 78111092 : perform_integral_promotions (tree exp)
2804 : {
2805 78111092 : tree type = TREE_TYPE (exp);
2806 78111092 : enum tree_code code = TREE_CODE (type);
2807 :
2808 78111092 : gcc_assert (INTEGRAL_TYPE_P (type));
2809 :
2810 : /* Convert enums to the result of applying the integer promotions to
2811 : their underlying type. */
2812 78111092 : if (code == ENUMERAL_TYPE)
2813 : {
2814 653317 : type = ENUM_UNDERLYING_TYPE (type);
2815 653317 : if (c_promoting_integer_type_p (type))
2816 : {
2817 94 : if (TYPE_UNSIGNED (type)
2818 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2819 0 : type = unsigned_type_node;
2820 : else
2821 94 : type = integer_type_node;
2822 : }
2823 :
2824 653317 : return convert (type, exp);
2825 : }
2826 :
2827 : /* ??? This should no longer be needed now bit-fields have their
2828 : proper types. */
2829 77457775 : if (TREE_CODE (exp) == COMPONENT_REF
2830 77457775 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2831 : {
2832 62272 : if (BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))))
2833 : {
2834 484 : tree btype = DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1));
2835 484 : if (TREE_CODE (btype) == BITINT_TYPE)
2836 454 : return convert (btype, exp);
2837 : else
2838 30 : return convert (ENUM_UNDERLYING_TYPE (btype), exp);
2839 : }
2840 : /* If it's thinner than an int, promote it like a
2841 : c_promoting_integer_type_p, otherwise leave it alone. */
2842 61788 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2843 61788 : TYPE_PRECISION (integer_type_node)) < 0)
2844 53526 : return convert (integer_type_node, exp);
2845 : }
2846 :
2847 77403765 : if (c_promoting_integer_type_p (type))
2848 : {
2849 : /* Preserve unsignedness if not really getting any wider. */
2850 651949 : if (TYPE_UNSIGNED (type)
2851 651949 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2852 0 : return convert (unsigned_type_node, exp);
2853 :
2854 651949 : return convert (integer_type_node, exp);
2855 : }
2856 :
2857 : return exp;
2858 : }
2859 :
2860 :
2861 : /* Perform default promotions for C data used in expressions.
2862 : Enumeral types or short or char are converted to int.
2863 : In addition, manifest constants symbols are replaced by their values. */
2864 :
2865 : tree
2866 87690362 : default_conversion (tree exp)
2867 : {
2868 87690362 : tree orig_exp;
2869 87690362 : tree type = TREE_TYPE (exp);
2870 87690362 : enum tree_code code = TREE_CODE (type);
2871 87690362 : tree promoted_type;
2872 :
2873 87690362 : mark_exp_read (exp);
2874 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2875 87690362 : if (TREE_CODE (exp) == COMPONENT_REF
2876 87690362 : && handle_counted_by_p (exp))
2877 602435 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2878 :
2879 : /* Functions and arrays have been converted during parsing. */
2880 87690362 : gcc_assert (code != FUNCTION_TYPE);
2881 87690362 : if (code == ARRAY_TYPE)
2882 : return exp;
2883 :
2884 : /* Constants can be used directly unless they're not loadable. */
2885 87690202 : if (TREE_CODE (exp) == CONST_DECL)
2886 0 : exp = DECL_INITIAL (exp);
2887 :
2888 : /* Strip no-op conversions. */
2889 87690202 : orig_exp = exp;
2890 88063013 : STRIP_TYPE_NOPS (exp);
2891 :
2892 87690202 : copy_warning (exp, orig_exp);
2893 :
2894 87690202 : if (code == VOID_TYPE)
2895 : {
2896 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2897 : "void value not ignored as it ought to be");
2898 2 : return error_mark_node;
2899 : }
2900 :
2901 87690200 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2902 87690200 : if (exp == error_mark_node)
2903 : return error_mark_node;
2904 :
2905 87689296 : promoted_type = targetm.promoted_type (type);
2906 87689296 : if (promoted_type)
2907 0 : return convert (promoted_type, exp);
2908 :
2909 87689296 : if (INTEGRAL_TYPE_P (type))
2910 77111909 : return perform_integral_promotions (exp);
2911 :
2912 : return exp;
2913 : }
2914 :
2915 : /* Look up COMPONENT in a structure or union TYPE.
2916 :
2917 : If the component name is not found, returns NULL_TREE. Otherwise,
2918 : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2919 : stepping down the chain to the component, which is in the last
2920 : TREE_VALUE of the list. Normally the list is of length one, but if
2921 : the component is embedded within (nested) anonymous structures or
2922 : unions, the list steps down the chain to the component. */
2923 :
2924 : tree
2925 2327043 : lookup_field (const_tree type, tree component)
2926 : {
2927 2327043 : tree field;
2928 :
2929 : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2930 : to the field elements. Use a binary search on this array to quickly
2931 : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2932 : will always be set for structures which have many elements.
2933 :
2934 : Duplicate field checking replaces duplicates with NULL_TREE so
2935 : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2936 : case just iterate using DECL_CHAIN. */
2937 :
2938 2629275 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2939 2629275 : && !seen_error ())
2940 : {
2941 302211 : int bot, top, half;
2942 302211 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2943 :
2944 302211 : field = TYPE_FIELDS (type);
2945 302211 : bot = 0;
2946 302211 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2947 1390378 : while (top - bot > 1)
2948 : {
2949 1360966 : half = (top - bot + 1) >> 1;
2950 1360966 : field = field_array[bot+half];
2951 :
2952 1360966 : if (DECL_NAME (field) == NULL_TREE)
2953 : {
2954 : /* Step through all anon unions in linear fashion. */
2955 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2956 : {
2957 0 : field = field_array[bot++];
2958 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2959 : {
2960 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2961 :
2962 0 : if (anon)
2963 0 : return tree_cons (NULL_TREE, field, anon);
2964 :
2965 : /* The Plan 9 compiler permits referring
2966 : directly to an anonymous struct/union field
2967 : using a typedef name. */
2968 0 : if (flag_plan9_extensions
2969 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2970 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2971 : == TYPE_DECL)
2972 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2973 : == component))
2974 : break;
2975 : }
2976 : }
2977 :
2978 : /* Entire record is only anon unions. */
2979 0 : if (bot > top)
2980 : return NULL_TREE;
2981 :
2982 : /* Restart the binary search, with new lower bound. */
2983 0 : continue;
2984 0 : }
2985 :
2986 1360966 : if (DECL_NAME (field) == component)
2987 : break;
2988 1088167 : if (DECL_NAME (field) < component)
2989 : bot += half;
2990 : else
2991 901706 : top = bot + half;
2992 : }
2993 :
2994 302211 : if (DECL_NAME (field_array[bot]) == component)
2995 : field = field_array[bot];
2996 272799 : else if (DECL_NAME (field) != component)
2997 : return NULL_TREE;
2998 : }
2999 : else
3000 : {
3001 5270552 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3002 : {
3003 5261059 : if (DECL_NAME (field) == NULL_TREE
3004 5261059 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3005 : {
3006 12405 : tree anon = lookup_field (TREE_TYPE (field), component);
3007 :
3008 12405 : if (anon)
3009 3007 : return tree_cons (NULL_TREE, field, anon);
3010 :
3011 : /* The Plan 9 compiler permits referring directly to an
3012 : anonymous struct/union field using a typedef
3013 : name. */
3014 9398 : if (flag_plan9_extensions
3015 174 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3016 172 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
3017 9460 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3018 : == component))
3019 : break;
3020 : }
3021 :
3022 5258048 : if (DECL_NAME (field) == component)
3023 : break;
3024 : }
3025 :
3026 2021825 : if (field == NULL_TREE)
3027 : return NULL_TREE;
3028 : }
3029 :
3030 2314543 : return tree_cons (NULL_TREE, field, NULL_TREE);
3031 : }
3032 :
3033 : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
3034 :
3035 : static void
3036 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
3037 : vec<tree> *candidates)
3038 : {
3039 101 : tree field;
3040 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3041 : {
3042 157 : if (DECL_NAME (field) == NULL_TREE
3043 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3044 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
3045 : candidates);
3046 :
3047 157 : if (DECL_NAME (field))
3048 111 : candidates->safe_push (DECL_NAME (field));
3049 : }
3050 101 : }
3051 :
3052 : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
3053 : rather than returning a TREE_LIST for an exact match. */
3054 :
3055 : static tree
3056 71 : lookup_field_fuzzy (tree type, tree component)
3057 : {
3058 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
3059 :
3060 : /* First, gather a list of candidates. */
3061 71 : auto_vec <tree> candidates;
3062 :
3063 71 : lookup_field_fuzzy_find_candidates (type, component,
3064 : &candidates);
3065 :
3066 71 : return find_closest_identifier (component, &candidates);
3067 71 : }
3068 :
3069 : /* Support function for build_component_ref's error-handling.
3070 :
3071 : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
3072 : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
3073 :
3074 : static bool
3075 72 : should_suggest_deref_p (tree datum_type)
3076 : {
3077 : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
3078 : allows "." for ptrs; we could be handling a failed attempt
3079 : to access a property. */
3080 72 : if (c_dialect_objc ())
3081 : return false;
3082 :
3083 : /* Only suggest it for pointers... */
3084 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3085 : return false;
3086 :
3087 : /* ...to structs/unions. */
3088 8 : tree underlying_type = TREE_TYPE (datum_type);
3089 8 : enum tree_code code = TREE_CODE (underlying_type);
3090 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3091 : return true;
3092 : else
3093 1 : return false;
3094 : }
3095 :
3096 : /* Give a component ref REF, decide whether we should handle its counted_by
3097 : attribute based on its context:
3098 : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3099 :
3100 : static bool
3101 2213513 : handle_counted_by_p (tree ref)
3102 : {
3103 2213513 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3104 2213513 : tree datum = TREE_OPERAND (ref, 0);
3105 : /* If the component_ref is build for a offsetof, i.e., the datum
3106 : of the component_ref is a indirect_ref of null_pointer_node,
3107 : we should not generate call to .ACCESS_WITH_SIZE. */
3108 2213513 : if (TREE_CODE (datum) == INDIRECT_REF
3109 2213513 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3110 : return false;
3111 2213175 : if (in_typeof || in_alignof)
3112 8786 : return false;
3113 : return true;
3114 : }
3115 :
3116 : /* Given a component ref REF, if there is a counted_by attribute attached,
3117 : issue error when the element_type is a structure or union including a
3118 : flexible array member. */
3119 :
3120 : static void
3121 2280599 : check_counted_by_attribute (location_t loc, tree ref)
3122 : {
3123 2280599 : tree subdatum = TREE_OPERAND (ref, 1);
3124 2280599 : tree sub_type = TREE_TYPE (subdatum);
3125 :
3126 2280599 : if (!flexible_array_member_type_p (sub_type)
3127 2280599 : && TREE_CODE (sub_type) != POINTER_TYPE)
3128 : return;
3129 :
3130 389664 : tree element_type = TREE_TYPE (sub_type);
3131 :
3132 389664 : tree attr_counted_by = lookup_attribute ("counted_by",
3133 389664 : DECL_ATTRIBUTES (subdatum));
3134 389664 : if (attr_counted_by)
3135 : {
3136 : /* Issue error when the element_type is a structure or
3137 : union including a flexible array member. */
3138 881 : if (RECORD_OR_UNION_TYPE_P (element_type)
3139 881 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3140 : {
3141 2 : error_at (loc,
3142 : "%<counted_by%> attribute is not allowed for a pointer to"
3143 : " structure or union with flexible array member");
3144 2 : return;
3145 : }
3146 : }
3147 : }
3148 :
3149 : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3150 : to the object that represents its counted_by per the attribute
3151 : counted_by attached to this field if it's a flexible array member
3152 : or a pointer field, otherwise return NULL_TREE.
3153 : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3154 : For example, if:
3155 :
3156 : struct P {
3157 : int k;
3158 : int x[] __attribute__ ((counted_by (k)));
3159 : } *p;
3160 :
3161 : for:
3162 : p->x
3163 :
3164 : the ref to the object that represents its element count will be:
3165 :
3166 : &(p->k)
3167 : */
3168 :
3169 : static tree
3170 441832 : build_counted_by_ref (tree datum, tree subdatum,
3171 : tree *counted_by_type)
3172 : {
3173 441832 : tree sub_type = TREE_TYPE (subdatum);
3174 441832 : if (!flexible_array_member_type_p (sub_type)
3175 441832 : && TREE_CODE (sub_type) != POINTER_TYPE)
3176 : return NULL_TREE;
3177 :
3178 441832 : tree attr_counted_by = lookup_attribute ("counted_by",
3179 441832 : DECL_ATTRIBUTES (subdatum));
3180 441832 : if (!attr_counted_by)
3181 : return NULL_TREE;
3182 :
3183 660 : tree counted_by_ref = NULL_TREE;
3184 660 : *counted_by_type = NULL_TREE;
3185 :
3186 660 : tree type = TREE_TYPE (datum);
3187 :
3188 : /* If the type of the containing structure is an anonymous struct/union,
3189 : and this anonymous struct/union is not a root type, get the first
3190 : outer named structure/union type. */
3191 660 : while (TREE_CODE (datum) == COMPONENT_REF
3192 35 : && c_type_tag (type) == NULL_TREE
3193 730 : && DECL_NAME (TREE_OPERAND (datum, 1)) == NULL_TREE)
3194 : {
3195 35 : datum = TREE_OPERAND (datum, 0);
3196 35 : type = TREE_TYPE (datum);
3197 : }
3198 :
3199 660 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3200 660 : tree counted_by_field = lookup_field (type, field_id);
3201 660 : gcc_assert (counted_by_field);
3202 :
3203 1072 : tree counted_by_subdatum;
3204 1072 : do
3205 : {
3206 1072 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3207 : /* Get the TYPE of the counted_by field. */
3208 1072 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3209 :
3210 1072 : counted_by_ref
3211 1072 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3212 : datum, counted_by_subdatum, NULL_TREE);
3213 :
3214 1072 : datum = counted_by_ref;
3215 1072 : counted_by_field = TREE_CHAIN (counted_by_field);
3216 : }
3217 1072 : while (counted_by_field);
3218 :
3219 660 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3220 660 : return counted_by_ref;
3221 : }
3222 :
3223 : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3224 : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3225 : call to the internal function .ACCESS_WITH_SIZE.
3226 :
3227 : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3228 : the internal function .ACCESS_WITH_SIZE.
3229 :
3230 : REF
3231 :
3232 : to:
3233 :
3234 : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3235 : TYPE_SIZE_UNIT for element)
3236 :
3237 : NOTE: The return type of this function is the POINTER type pointing
3238 : to the original flexible array type. Then the type of the INDIRECT_REF
3239 : is the original flexible array type.
3240 : The type of the first argument of this function is a POINTER type
3241 : to the original flexible array type.
3242 :
3243 : B: For pointers with counted_by, generate a call to the internal function
3244 : .ACCESS_WITH_SIZE.
3245 :
3246 : REF
3247 :
3248 : to:
3249 :
3250 : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3251 : TYPE_SIZE_UNIT for element)
3252 :
3253 : NOTE: The return type of this function is the original pointer type.
3254 : The type of the first argument of this function is the original
3255 : pointer type.
3256 :
3257 : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3258 : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3259 :
3260 : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3261 : of the array.
3262 :
3263 : */
3264 : static tree
3265 660 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3266 : tree counted_by_ref,
3267 : tree counted_by_type)
3268 : {
3269 660 : gcc_assert (flexible_array_member_type_p (TREE_TYPE (ref))
3270 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3271 :
3272 660 : bool is_fam = flexible_array_member_type_p (TREE_TYPE (ref));
3273 :
3274 : /* The result type of the call is a pointer to the flexible array type;
3275 : or is the original pointer type to the pointer field with counted_by. */
3276 660 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3277 459 : : TREE_TYPE (ref);
3278 :
3279 660 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3280 660 : tree element_size = VOID_TYPE_P (element_type)
3281 660 : ? size_one_node
3282 660 : : TYPE_SIZE_UNIT (element_type);
3283 660 : if (element_size == NULL_TREE)
3284 : return ref;
3285 :
3286 646 : tree first_param = is_fam
3287 646 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3288 : false, NULL)
3289 445 : : c_fully_fold (ref, false, NULL);
3290 646 : tree second_param
3291 646 : = c_fully_fold (counted_by_ref, false, NULL);
3292 646 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3293 :
3294 646 : tree call
3295 646 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3296 : result_type, 4,
3297 : first_param,
3298 : second_param,
3299 : third_param,
3300 : element_size);
3301 :
3302 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3303 646 : if (is_fam)
3304 201 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3305 646 : SET_EXPR_LOCATION (call, loc);
3306 646 : return call;
3307 : }
3308 :
3309 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3310 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3311 : function .ACCESS_WITH_SIZE.
3312 : Otherwise, return the ref itself. */
3313 :
3314 : tree
3315 2204469 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3316 : {
3317 2204469 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3318 2204469 : tree datum = TREE_OPERAND (ref, 0);
3319 2204469 : tree subdatum = TREE_OPERAND (ref, 1);
3320 2204469 : tree counted_by_type = NULL_TREE;
3321 :
3322 2204469 : if (!(flexible_array_member_type_p (TREE_TYPE (ref))
3323 2133998 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3324 : return ref;
3325 :
3326 441832 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3327 : &counted_by_type);
3328 441832 : if (counted_by_ref)
3329 660 : ref = build_access_with_size_for_counted_by (loc, ref,
3330 : counted_by_ref,
3331 : counted_by_type);
3332 : return ref;
3333 : }
3334 :
3335 : /* Make an expression to refer to the COMPONENT field of structure or
3336 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3337 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3338 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3339 : it is from -> operator.
3340 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3341 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3342 :
3343 : tree
3344 2278481 : build_component_ref (location_t loc, tree datum, tree component,
3345 : location_t component_loc, location_t arrow_loc)
3346 : {
3347 2278481 : tree type = TREE_TYPE (datum);
3348 2278481 : enum tree_code code = TREE_CODE (type);
3349 2278481 : tree field = NULL;
3350 2278481 : tree ref;
3351 2278481 : bool datum_lvalue = lvalue_p (datum);
3352 :
3353 2278481 : if (!objc_is_public (datum, component))
3354 0 : return error_mark_node;
3355 :
3356 : /* Detect Objective-C property syntax object.property. */
3357 2278481 : if (c_dialect_objc ()
3358 2278481 : && (ref = objc_maybe_build_component_ref (datum, component)))
3359 : return ref;
3360 :
3361 : /* See if there is a field or component with name COMPONENT. */
3362 :
3363 2278481 : if (code == RECORD_TYPE || code == UNION_TYPE)
3364 : {
3365 2278409 : if (!COMPLETE_TYPE_P (type))
3366 : {
3367 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3368 19 : return error_mark_node;
3369 : }
3370 :
3371 2278390 : field = lookup_field (type, component);
3372 :
3373 2278390 : if (!field)
3374 : {
3375 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3376 63 : if (guessed_id)
3377 : {
3378 : /* Attempt to provide a fixit replacement hint, if
3379 : we have a valid range for the component. */
3380 0 : location_t reported_loc
3381 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3382 26 : gcc_rich_location rich_loc (reported_loc);
3383 26 : if (component_loc != UNKNOWN_LOCATION)
3384 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3385 26 : error_at (&rich_loc,
3386 : "%qT has no member named %qE; did you mean %qE?",
3387 : type, component, guessed_id);
3388 26 : }
3389 : else
3390 37 : error_at (loc, "%qT has no member named %qE", type, component);
3391 63 : return error_mark_node;
3392 : }
3393 :
3394 : /* Accessing elements of atomic structures or unions is undefined
3395 : behavior (C11 6.5.2.3#5). */
3396 2278327 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3397 : {
3398 18 : if (code == RECORD_TYPE)
3399 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3400 : "structure %qE", component, datum);
3401 : else
3402 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3403 : "union %qE", component, datum);
3404 : }
3405 :
3406 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3407 : This might be better solved in future the way the C++ front
3408 : end does it - by giving the anonymous entities each a
3409 : separate name and type, and then have build_component_ref
3410 : recursively call itself. We can't do that here. */
3411 2280599 : do
3412 : {
3413 2280599 : tree subdatum = TREE_VALUE (field);
3414 2280599 : int quals;
3415 2280599 : tree subtype;
3416 2280599 : bool use_datum_quals;
3417 :
3418 2280599 : if (TREE_TYPE (subdatum) == error_mark_node)
3419 : return error_mark_node;
3420 :
3421 : /* If this is an rvalue, it does not have qualifiers in C
3422 : standard terms and we must avoid propagating such
3423 : qualifiers down to a non-lvalue array that is then
3424 : converted to a pointer. */
3425 4561198 : use_datum_quals = (datum_lvalue
3426 2280599 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3427 :
3428 2280599 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3429 2280599 : if (use_datum_quals)
3430 2280337 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3431 2280599 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3432 :
3433 2280599 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3434 : NULL_TREE);
3435 2280599 : SET_EXPR_LOCATION (ref, loc);
3436 :
3437 2280599 : check_counted_by_attribute (loc, ref);
3438 :
3439 2280599 : if (TREE_READONLY (subdatum)
3440 2280599 : || (use_datum_quals && TREE_READONLY (datum)))
3441 30700 : TREE_READONLY (ref) = 1;
3442 2280599 : if (TREE_THIS_VOLATILE (subdatum)
3443 2279605 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3444 2436 : TREE_THIS_VOLATILE (ref) = 1;
3445 :
3446 2280599 : if (TREE_UNAVAILABLE (subdatum))
3447 10 : error_unavailable_use (subdatum, NULL_TREE);
3448 2280589 : else if (TREE_DEPRECATED (subdatum))
3449 16 : warn_deprecated_use (subdatum, NULL_TREE);
3450 :
3451 2280599 : datum = ref;
3452 :
3453 2280599 : field = TREE_CHAIN (field);
3454 : }
3455 2280599 : while (field);
3456 :
3457 : return ref;
3458 : }
3459 72 : else if (should_suggest_deref_p (type))
3460 : {
3461 : /* Special-case the error message for "ptr.field" for the case
3462 : where the user has confused "." vs "->". */
3463 7 : rich_location richloc (line_table, loc);
3464 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3465 : {
3466 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3467 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3468 2 : error_at (&richloc,
3469 : "%qE is a pointer to pointer; did you mean to dereference "
3470 : "it before applying %<->%> to it?",
3471 2 : TREE_OPERAND (datum, 0));
3472 : }
3473 : else
3474 : {
3475 : /* "loc" should be the "." token. */
3476 5 : richloc.add_fixit_replace ("->");
3477 5 : error_at (&richloc,
3478 : "%qE is a pointer; did you mean to use %<->%>?",
3479 : datum);
3480 : }
3481 7 : return error_mark_node;
3482 7 : }
3483 65 : else if (code != ERROR_MARK)
3484 1 : error_at (loc,
3485 : "request for member %qE in something not a structure or union",
3486 : component);
3487 :
3488 65 : return error_mark_node;
3489 : }
3490 :
3491 : /* Given an expression PTR for a pointer, return an expression
3492 : for the value pointed to.
3493 : ERRORSTRING is the name of the operator to appear in error messages.
3494 :
3495 : LOC is the location to use for the generated tree. */
3496 :
3497 : tree
3498 2764959 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3499 : {
3500 2764959 : tree pointer = default_conversion (ptr);
3501 2764959 : tree type = TREE_TYPE (pointer);
3502 2764959 : tree ref;
3503 :
3504 2764959 : if (TREE_CODE (type) == POINTER_TYPE)
3505 : {
3506 2764605 : if (CONVERT_EXPR_P (pointer)
3507 1981772 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3508 : {
3509 : /* If a warning is issued, mark it to avoid duplicates from
3510 : the backend. This only needs to be done at
3511 : warn_strict_aliasing > 2. */
3512 782833 : if (warn_strict_aliasing > 2)
3513 335151 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3514 335151 : type, TREE_OPERAND (pointer, 0)))
3515 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3516 : }
3517 :
3518 2764605 : if (TREE_CODE (pointer) == ADDR_EXPR
3519 2764605 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3520 67469 : == TREE_TYPE (type)))
3521 : {
3522 67460 : ref = TREE_OPERAND (pointer, 0);
3523 67460 : protected_set_expr_location (ref, loc);
3524 67460 : return ref;
3525 : }
3526 : else
3527 : {
3528 2697145 : tree t = TREE_TYPE (type);
3529 :
3530 2697145 : ref = build1 (INDIRECT_REF, t, pointer);
3531 :
3532 2697145 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3533 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3534 :
3535 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3536 : so that we get the proper error message if the result is used
3537 : to assign to. Also, &* is supposed to be a no-op.
3538 : And ANSI C seems to specify that the type of the result
3539 : should be the const type. */
3540 : /* A de-reference of a pointer to const is not a const. It is valid
3541 : to change it via some other pointer. */
3542 2697145 : TREE_READONLY (ref) = TYPE_READONLY (t);
3543 2697145 : TREE_SIDE_EFFECTS (ref)
3544 2697145 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3545 2697145 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3546 2697145 : protected_set_expr_location (ref, loc);
3547 2697145 : return ref;
3548 : }
3549 : }
3550 354 : else if (TREE_CODE (pointer) != ERROR_MARK)
3551 278 : invalid_indirection_error (loc, type, errstring);
3552 :
3553 354 : return error_mark_node;
3554 : }
3555 :
3556 : /* This handles expressions of the form "a[i]", which denotes
3557 : an array reference.
3558 :
3559 : This is logically equivalent in C to *(a+i), but we may do it differently.
3560 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3561 : This avoids forcing the array out of registers, and can work on
3562 : arrays that are not lvalues (for example, members of structures returned
3563 : by functions).
3564 :
3565 : For vector types, allow vector[i] but not i[vector], and create
3566 : *(((type*)&vectortype) + i) for the expression.
3567 :
3568 : LOC is the location to use for the returned expression. */
3569 :
3570 : tree
3571 3488869 : build_array_ref (location_t loc, tree array, tree index)
3572 : {
3573 3488869 : tree ret;
3574 3488869 : bool swapped = false;
3575 3488869 : if (TREE_TYPE (array) == error_mark_node
3576 3488869 : || TREE_TYPE (index) == error_mark_node)
3577 : return error_mark_node;
3578 :
3579 3488761 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3580 2005343 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3581 : /* Allow vector[index] but not index[vector]. */
3582 4516221 : && !gnu_vector_type_p (TREE_TYPE (array)))
3583 : {
3584 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3585 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3586 : {
3587 2 : error_at (loc,
3588 : "subscripted value is neither array nor pointer nor vector");
3589 :
3590 2 : return error_mark_node;
3591 : }
3592 192 : std::swap (array, index);
3593 192 : swapped = true;
3594 : }
3595 :
3596 3488759 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3597 : {
3598 2 : error_at (loc, "array subscript is not an integer");
3599 2 : return error_mark_node;
3600 : }
3601 :
3602 3488757 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3603 : {
3604 10 : error_at (loc, "subscripted value is pointer to function");
3605 10 : return error_mark_node;
3606 : }
3607 :
3608 : /* ??? Existing practice has been to warn only when the char
3609 : index is syntactically the index, not for char[array]. */
3610 3488747 : if (!swapped)
3611 3488561 : warn_array_subscript_with_type_char (loc, index);
3612 :
3613 : /* Apply default promotions *after* noticing character types. */
3614 3488747 : index = default_conversion (index);
3615 3488747 : if (index == error_mark_node)
3616 : return error_mark_node;
3617 :
3618 3488746 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3619 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3620 :
3621 3488746 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3622 3488746 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3623 :
3624 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3625 3488746 : if (TREE_CODE (array) == COMPONENT_REF
3626 3488746 : && handle_counted_by_p (array))
3627 816569 : array = handle_counted_by_for_component_ref (loc, array);
3628 :
3629 3488746 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3630 : {
3631 2510691 : tree rval, type;
3632 :
3633 : /* An array that is indexed by a non-constant
3634 : cannot be stored in a register; we must be able to do
3635 : address arithmetic on its address.
3636 : Likewise an array of elements of variable size. */
3637 2510691 : if (TREE_CODE (index) != INTEGER_CST
3638 2510691 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3639 2030569 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3640 : {
3641 480862 : if (!c_mark_addressable (array, true, true))
3642 1 : return error_mark_node;
3643 : }
3644 : /* An array that is indexed by a constant value which is not within
3645 : the array bounds cannot be stored in a register either; because we
3646 : would get a crash in store_bit_field/extract_bit_field when trying
3647 : to access a non-existent part of the register. */
3648 2510690 : if (TREE_CODE (index) == INTEGER_CST
3649 2030569 : && TYPE_DOMAIN (TREE_TYPE (array))
3650 4537900 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3651 : {
3652 60657 : if (!c_mark_addressable (array, false, true))
3653 0 : return error_mark_node;
3654 : /* ISO C2Y disallows a negative integer constant expression index
3655 : when array subscripting has an operand of array type. */
3656 60657 : if (flag_isoc2y
3657 26 : && !TREE_OVERFLOW (index)
3658 60680 : && tree_int_cst_sgn (index) < 0)
3659 10 : error_at (loc, "array subscript is negative");
3660 : }
3661 :
3662 2510690 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3663 2487249 : && ! was_vector)
3664 : {
3665 1482704 : tree foo = array;
3666 2318345 : while (TREE_CODE (foo) == COMPONENT_REF)
3667 835641 : foo = TREE_OPERAND (foo, 0);
3668 983106 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3669 2465744 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3670 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3671 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3672 : "ISO C forbids subscripting %<register%> array "
3673 : "before C2Y");
3674 1482626 : else if (!lvalue_p (foo))
3675 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3676 : "ISO C90 forbids subscripting non-lvalue "
3677 : "array");
3678 : }
3679 :
3680 2510690 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3681 2510690 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3682 3 : index = fold_convert (sizetype, index);
3683 :
3684 2510690 : type = TREE_TYPE (TREE_TYPE (array));
3685 2510690 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3686 : /* Array ref is const/volatile if the array elements are
3687 : or if the array is. */
3688 7532070 : TREE_READONLY (rval)
3689 2510690 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3690 2510690 : | TREE_READONLY (array));
3691 7532070 : TREE_SIDE_EFFECTS (rval)
3692 2510690 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3693 2510690 : | TREE_SIDE_EFFECTS (array));
3694 5021380 : TREE_THIS_VOLATILE (rval)
3695 2510690 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3696 : /* This was added by rms on 16 Nov 91.
3697 : It fixes vol struct foo *a; a->elts[1]
3698 : in an inline function.
3699 : Hope it doesn't break something else. */
3700 2510690 : | TREE_THIS_VOLATILE (array));
3701 2510690 : ret = require_complete_type (loc, rval);
3702 2510690 : protected_set_expr_location (ret, loc);
3703 2510690 : if (non_lvalue)
3704 105577 : ret = non_lvalue_loc (loc, ret);
3705 : return ret;
3706 : }
3707 : else
3708 : {
3709 978055 : tree ar = default_conversion (array);
3710 :
3711 978055 : if (ar == error_mark_node)
3712 : return ar;
3713 :
3714 978055 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3715 978055 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3716 :
3717 978055 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3718 : index, false),
3719 : RO_ARRAY_INDEXING);
3720 978055 : if (non_lvalue)
3721 0 : ret = non_lvalue_loc (loc, ret);
3722 : return ret;
3723 : }
3724 : }
3725 :
3726 : /* Build an OpenMP array section reference, creating an exact type for the
3727 : resulting expression based on the element type and bounds if possible. If
3728 : we have variable bounds, create an incomplete array type for the result
3729 : instead. */
3730 :
3731 : tree
3732 3963 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3733 : {
3734 3963 : tree type = TREE_TYPE (array);
3735 3963 : gcc_assert (type);
3736 :
3737 3963 : tree sectype, eltype = TREE_TYPE (type);
3738 :
3739 : /* It's not an array or pointer type. Just reuse the type of the original
3740 : expression as the type of the array section (an error will be raised
3741 : anyway, later). */
3742 3963 : if (eltype == NULL_TREE || error_operand_p (eltype))
3743 12 : sectype = TREE_TYPE (array);
3744 : else
3745 : {
3746 3951 : tree idxtype = NULL_TREE;
3747 :
3748 3951 : if (index != NULL_TREE
3749 3951 : && length != NULL_TREE
3750 3019 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3751 6965 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3752 : {
3753 3007 : tree low = fold_convert (sizetype, index);
3754 3007 : tree high = fold_convert (sizetype, length);
3755 3007 : high = size_binop (PLUS_EXPR, low, high);
3756 3007 : high = size_binop (MINUS_EXPR, high, size_one_node);
3757 3007 : idxtype = build_range_type (sizetype, low, high);
3758 : }
3759 137 : else if ((index == NULL_TREE || integer_zerop (index))
3760 832 : && length != NULL_TREE
3761 1622 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3762 661 : idxtype = build_index_type (length);
3763 :
3764 3951 : gcc_assert (!error_operand_p (idxtype));
3765 :
3766 3951 : sectype = c_build_array_type (eltype, idxtype);
3767 : }
3768 :
3769 3963 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3770 : }
3771 :
3772 :
3773 : /* Record that REF is used. This is relevant for undeclared static
3774 : function and declarations referenced from a non-local context.
3775 : ADDRESS indicates whether the address is taken. */
3776 :
3777 : void
3778 488004336 : mark_decl_used (tree ref, bool address)
3779 : {
3780 488004336 : if (!ref || !DECL_P (ref) || in_alignof)
3781 : return;
3782 :
3783 : /* Non-file-scope and non-local reference in nested function. */
3784 357840163 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3785 124607279 : && DECL_CONTEXT (current_function_decl)
3786 487970288 : && DECL_CONTEXT (ref) != current_function_decl;
3787 :
3788 : /* An undeclared static function. */
3789 487960190 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3790 105075545 : && DECL_INITIAL (ref) == NULL_TREE
3791 71745584 : && DECL_EXTERNAL (ref)
3792 559703078 : && !TREE_PUBLIC (ref);
3793 :
3794 487826098 : if (!static_p && !nonloc_p)
3795 : return;
3796 :
3797 : /* If we may be in an unevaluated context, delay the decision. */
3798 136731 : if (in_sizeof || in_typeof || in_countof || in_generic)
3799 273 : return record_maybe_used_decl (ref, address);
3800 :
3801 136458 : if (static_p)
3802 133836 : C_DECL_USED (ref) = 1;
3803 :
3804 136458 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3805 : || TREE_CODE (ref) == PARM_DECL))
3806 2059 : DECL_NONLOCAL (ref) = 1;
3807 :
3808 : /* Nothing to do anymore. */
3809 2622 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3810 : return;
3811 :
3812 : /* Filter out the cases where referencing a non-local variable does not
3813 : require a non-local context passed via the static chain. */
3814 1217 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3815 1145 : switch (TREE_CODE (ref))
3816 : {
3817 219 : case FUNCTION_DECL:
3818 : /* Use of another local function that requires no context is ok. */
3819 219 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3820 : return;
3821 : break;
3822 440 : case VAR_DECL:
3823 : /* Static variables and constexpr are ok, but for the later only
3824 : when the address is not taken. */
3825 440 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3826 : return;
3827 : break;
3828 : case TYPE_DECL:
3829 : /* A typedef is ok when not for a variably-modified type. */
3830 : return;
3831 : case CONST_DECL:
3832 : /* An enumeration constant is ok. */
3833 : return;
3834 : case PARM_DECL:
3835 : break;
3836 : case LABEL_DECL:
3837 : break;
3838 0 : default:
3839 0 : gcc_unreachable ();
3840 : }
3841 :
3842 : /* Mark all parent functions up to the nesting level of the variable as
3843 : as needing the non-local context. */
3844 2027 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3845 : {
3846 2027 : if (cont == DECL_CONTEXT (ref))
3847 : break;
3848 :
3849 : /* There should not be any other type of context used for function
3850 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3851 1019 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3852 :
3853 1019 : if (TREE_CODE (cont) == FUNCTION_DECL)
3854 1019 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3855 : }
3856 : }
3857 :
3858 :
3859 : /* Build an external reference to identifier ID. FUN indicates
3860 : whether this will be used for a function call. LOC is the source
3861 : location of the identifier. This sets *TYPE to the type of the
3862 : identifier, which is not the same as the type of the returned value
3863 : for CONST_DECLs defined as enum constants. If the type of the
3864 : identifier is not available, *TYPE is set to NULL. */
3865 : tree
3866 178968096 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3867 : {
3868 178968096 : tree ref;
3869 178968096 : tree decl = lookup_name (id);
3870 :
3871 : /* In Objective-C, an instance variable (ivar) may be preferred to
3872 : whatever lookup_name() found. */
3873 178968096 : decl = objc_lookup_ivar (decl, id);
3874 :
3875 178968096 : *type = NULL;
3876 178968096 : if (decl && decl != error_mark_node)
3877 : {
3878 178961914 : ref = decl;
3879 178961914 : *type = TREE_TYPE (ref);
3880 178961914 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3881 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3882 : decl);
3883 : }
3884 6182 : else if (fun)
3885 : /* Implicit function declaration. */
3886 4729 : ref = implicitly_declare (loc, id);
3887 1453 : else if (decl == error_mark_node)
3888 : /* Don't complain about something that's already been
3889 : complained about. */
3890 : return error_mark_node;
3891 : else
3892 : {
3893 1218 : undeclared_variable (loc, id);
3894 1218 : return error_mark_node;
3895 : }
3896 :
3897 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3898 : unmappable types if we return the decl with an error_mark_node type,
3899 : rather than returning error_mark_node for the decl itself. */
3900 178966643 : if (TREE_TYPE (ref) == error_mark_node
3901 178966643 : && !c_omp_array_section_p)
3902 : return error_mark_node;
3903 :
3904 178966625 : if (TREE_UNAVAILABLE (ref))
3905 14 : error_unavailable_use (ref, NULL_TREE);
3906 178966611 : else if (TREE_DEPRECATED (ref))
3907 100 : warn_deprecated_use (ref, NULL_TREE);
3908 :
3909 : /* Recursive call does not count as usage. */
3910 178966625 : if (ref != current_function_decl)
3911 : {
3912 178963935 : TREE_USED (ref) = 1;
3913 : }
3914 :
3915 178966625 : mark_decl_used (ref, false);
3916 :
3917 178966625 : if (TREE_CODE (ref) == CONST_DECL)
3918 : {
3919 419365 : used_types_insert (TREE_TYPE (ref));
3920 :
3921 419365 : if (warn_cxx_compat
3922 429 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3923 419792 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3924 : {
3925 1 : warning_at (loc, OPT_Wc___compat,
3926 : "enum constant defined in struct or union "
3927 : "is not visible in C++");
3928 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3929 : }
3930 :
3931 419365 : ref = DECL_INITIAL (ref);
3932 419365 : TREE_CONSTANT (ref) = 1;
3933 : }
3934 : /* C99 6.7.4p3: An inline definition of a function with external
3935 : linkage ... shall not contain a reference to an identifier with
3936 : internal linkage. */
3937 178547260 : else if (current_function_decl != NULL_TREE
3938 177032646 : && DECL_DECLARED_INLINE_P (current_function_decl)
3939 163263398 : && DECL_EXTERNAL (current_function_decl)
3940 162490004 : && VAR_OR_FUNCTION_DECL_P (ref)
3941 58109647 : && (!VAR_P (ref) || TREE_STATIC (ref))
3942 48919920 : && ! TREE_PUBLIC (ref)
3943 178547294 : && DECL_CONTEXT (ref) != current_function_decl)
3944 25 : record_inline_static (loc, current_function_decl, ref,
3945 : csi_internal);
3946 :
3947 : return ref;
3948 : }
3949 :
3950 : static struct maybe_used_decl *maybe_used_decls;
3951 :
3952 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3953 : _Generic, might be used if the operand of sizeof is a VLA type or the
3954 : operand of typeof is a variably modified type or the operand of _Countof has
3955 : a variable number of elements or the operand of _Generic is the one selected
3956 : as the result. */
3957 :
3958 : static void
3959 273 : record_maybe_used_decl (tree decl, bool address)
3960 : {
3961 273 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3962 273 : t->decl = decl;
3963 273 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3964 273 : t->address = address;
3965 273 : t->next = maybe_used_decls;
3966 273 : maybe_used_decls = t;
3967 273 : }
3968 :
3969 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3970 : _Generic. If USED is false, just discard them. If it is true, mark them
3971 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3972 : them to the next level up (if still inside sizeof or typeof or _Countof or
3973 : _Generic). */
3974 :
3975 : void
3976 1471472 : pop_maybe_used (bool used)
3977 : {
3978 1471472 : struct maybe_used_decl *p = maybe_used_decls;
3979 1471472 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3980 1471769 : while (p && p->level > cur_level)
3981 : {
3982 297 : if (used)
3983 : {
3984 77 : if (cur_level == 0)
3985 53 : mark_decl_used (p->decl, p->address);
3986 : else
3987 24 : p->level = cur_level;
3988 : }
3989 297 : p = p->next;
3990 : }
3991 1471472 : if (!used || cur_level == 0)
3992 1471391 : maybe_used_decls = p;
3993 1471472 : }
3994 :
3995 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3996 : _Generic, without acting on them, and return the pointer to the previous top
3997 : of the stack. This for use at the end of a default generic association when
3998 : it is not yet known whether the expression is used. If it later turns out
3999 : the expression is used (or treated as used before C23), restore_maybe_used
4000 : should be called on the return value followed by pop_maybe_used (true);
4001 : otherwise, the return value can be discarded. */
4002 :
4003 : struct maybe_used_decl *
4004 1135 : save_maybe_used ()
4005 : {
4006 1135 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
4007 1135 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
4008 1167 : while (p && p->level > cur_level)
4009 32 : p = p->next;
4010 1135 : maybe_used_decls = p;
4011 1135 : return orig;
4012 : }
4013 :
4014 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
4015 : or _Generic returned by save_maybe_used. It is required that the stack is
4016 : at exactly the point where it was left by save_maybe_used. */
4017 :
4018 : void
4019 277 : restore_maybe_used (struct maybe_used_decl *stack)
4020 : {
4021 277 : maybe_used_decls = stack;
4022 277 : }
4023 :
4024 : /* Return the result of sizeof applied to EXPR. */
4025 :
4026 : struct c_expr
4027 313508 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4028 : {
4029 313508 : struct c_expr ret;
4030 313508 : if (expr.value == error_mark_node)
4031 : {
4032 111 : ret.value = error_mark_node;
4033 111 : ret.original_code = ERROR_MARK;
4034 111 : ret.original_type = NULL;
4035 111 : ret.m_decimal = 0;
4036 111 : pop_maybe_used (false);
4037 : }
4038 : else
4039 : {
4040 313397 : bool expr_const_operands = true;
4041 :
4042 313397 : if (TREE_CODE (expr.value) == PARM_DECL
4043 313397 : && C_ARRAY_PARAMETER (expr.value))
4044 : {
4045 55 : auto_diagnostic_group d;
4046 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4047 : "%<sizeof%> on array function parameter %qE will "
4048 : "return size of %qT", expr.value,
4049 55 : TREE_TYPE (expr.value)))
4050 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4051 55 : }
4052 313397 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4053 : &expr_const_operands);
4054 313397 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4055 313397 : c_last_sizeof_arg = expr.value;
4056 313397 : c_last_sizeof_loc = loc;
4057 313397 : ret.original_code = SIZEOF_EXPR;
4058 313397 : ret.original_type = NULL;
4059 313397 : ret.m_decimal = 0;
4060 313397 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4061 : {
4062 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4063 288 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4064 : folded_expr, ret.value);
4065 288 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4066 288 : SET_EXPR_LOCATION (ret.value, loc);
4067 : }
4068 313397 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4069 : }
4070 313508 : return ret;
4071 : }
4072 :
4073 : /* Return the result of sizeof applied to T, a structure for the type
4074 : name passed to sizeof (rather than the type itself). LOC is the
4075 : location of the original expression. */
4076 :
4077 : struct c_expr
4078 332408 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4079 : {
4080 332408 : tree type;
4081 332408 : struct c_expr ret;
4082 332408 : tree type_expr = NULL_TREE;
4083 332408 : bool type_expr_const = true;
4084 332408 : type = groktypename (t, &type_expr, &type_expr_const);
4085 332408 : ret.value = c_sizeof (loc, type);
4086 332408 : c_last_sizeof_arg = type;
4087 332408 : c_last_sizeof_loc = loc;
4088 332408 : ret.original_code = SIZEOF_EXPR;
4089 332408 : ret.original_type = NULL;
4090 332408 : ret.m_decimal = 0;
4091 332408 : if (type == error_mark_node)
4092 : {
4093 5 : ret.value = error_mark_node;
4094 5 : ret.original_code = ERROR_MARK;
4095 : }
4096 : else
4097 332361 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4098 664679 : && C_TYPE_VARIABLE_SIZE (type))
4099 : {
4100 : /* If the type is a [*] array, it is a VLA but is represented as
4101 : having a size of zero. In such a case we must ensure that
4102 : the result of sizeof does not get folded to a constant by
4103 : c_fully_fold, because if the size is evaluated the result is
4104 : not constant and so constraints on zero or negative size
4105 : arrays must not be applied when this sizeof call is inside
4106 : another array declarator. */
4107 41 : if (!type_expr)
4108 16 : type_expr = integer_zero_node;
4109 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4110 : type_expr, ret.value);
4111 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4112 : }
4113 332408 : pop_maybe_used (type != error_mark_node
4114 332408 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4115 332408 : return ret;
4116 : }
4117 :
4118 : static bool
4119 2156054 : top_array_vla_p (const_tree type)
4120 : {
4121 2156054 : if (TREE_CODE (type) != ARRAY_TYPE)
4122 : return false;
4123 :
4124 2156036 : tree d = TYPE_DOMAIN (type);
4125 :
4126 2156036 : if (!d)
4127 : return false;
4128 2062382 : if (!TYPE_MAX_VALUE (d))
4129 : return false;
4130 :
4131 1970085 : return TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST
4132 1970085 : || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST;
4133 : }
4134 :
4135 : /* Return the result of countof applied to EXPR. */
4136 :
4137 : struct c_expr
4138 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4139 : {
4140 54 : struct c_expr ret;
4141 54 : if (expr.value == error_mark_node)
4142 : {
4143 1 : ret.value = error_mark_node;
4144 1 : ret.original_code = ERROR_MARK;
4145 1 : ret.original_type = NULL;
4146 1 : ret.m_decimal = 0;
4147 1 : pop_maybe_used (false);
4148 : }
4149 : else
4150 : {
4151 53 : bool expr_const_operands = true;
4152 :
4153 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4154 : &expr_const_operands);
4155 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4156 53 : c_last_sizeof_arg = expr.value;
4157 53 : c_last_sizeof_loc = loc;
4158 53 : ret.original_code = COUNTOF_EXPR;
4159 53 : ret.original_type = NULL;
4160 53 : ret.m_decimal = 0;
4161 53 : if (top_array_vla_p (TREE_TYPE (folded_expr)))
4162 : {
4163 : /* countof is evaluated when given a vla. */
4164 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4165 : folded_expr, ret.value);
4166 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4167 11 : SET_EXPR_LOCATION (ret.value, loc);
4168 : }
4169 53 : pop_maybe_used (top_array_vla_p (TREE_TYPE (folded_expr)));
4170 : }
4171 54 : return ret;
4172 : }
4173 :
4174 : /* Return the result of countof applied to T, a structure for the type
4175 : name passed to countof (rather than the type itself). LOC is the
4176 : location of the original expression. */
4177 :
4178 : struct c_expr
4179 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4180 : {
4181 18 : tree type;
4182 18 : struct c_expr ret;
4183 18 : tree type_expr = NULL_TREE;
4184 18 : bool type_expr_const = true;
4185 18 : type = groktypename (t, &type_expr, &type_expr_const);
4186 18 : ret.value = c_countof_type (loc, type);
4187 18 : c_last_sizeof_arg = type;
4188 18 : c_last_sizeof_loc = loc;
4189 18 : ret.original_code = COUNTOF_EXPR;
4190 18 : ret.original_type = NULL;
4191 18 : ret.m_decimal = 0;
4192 18 : if (type == error_mark_node)
4193 : {
4194 0 : ret.value = error_mark_node;
4195 0 : ret.original_code = ERROR_MARK;
4196 : }
4197 : else
4198 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4199 22 : && top_array_vla_p (type))
4200 : {
4201 : /* If the type is a [*] array, it is a VLA but is represented as
4202 : having a size of zero. In such a case we must ensure that
4203 : the result of countof does not get folded to a constant by
4204 : c_fully_fold, because if the number of elements is evaluated
4205 : the result is not constant and so
4206 : constraints on zero or negative size arrays must not be applied
4207 : when this countof call is inside another array declarator. */
4208 6 : if (!type_expr)
4209 0 : type_expr = integer_zero_node;
4210 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4211 : type_expr, ret.value);
4212 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4213 : }
4214 18 : pop_maybe_used (type != error_mark_node ? top_array_vla_p (type) : false);
4215 18 : return ret;
4216 : }
4217 :
4218 : /* Return the result of maxof applied to T, a structure for the type
4219 : name passed to maxof (rather than the type itself). LOC is the
4220 : location of the original expression. */
4221 :
4222 : struct c_expr
4223 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4224 : {
4225 46 : tree type;
4226 46 : struct c_expr ret;
4227 46 : tree type_expr = NULL_TREE;
4228 46 : bool type_expr_const = true;
4229 46 : type = groktypename (t, &type_expr, &type_expr_const);
4230 46 : ret.value = c_maxof_type (loc, type);
4231 46 : c_last_sizeof_arg = type;
4232 46 : c_last_sizeof_loc = loc;
4233 46 : ret.original_code = MAXOF_EXPR;
4234 46 : ret.original_type = NULL;
4235 46 : ret.m_decimal = 0;
4236 46 : if (type == error_mark_node)
4237 : {
4238 0 : ret.value = error_mark_node;
4239 0 : ret.original_code = ERROR_MARK;
4240 : }
4241 46 : pop_maybe_used (type != error_mark_node);
4242 46 : return ret;
4243 : }
4244 :
4245 : /* Return the result of minof applied to T, a structure for the type
4246 : name passed to minof (rather than the type itself). LOC is the
4247 : location of the original expression. */
4248 :
4249 : struct c_expr
4250 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4251 : {
4252 46 : tree type;
4253 46 : struct c_expr ret;
4254 46 : tree type_expr = NULL_TREE;
4255 46 : bool type_expr_const = true;
4256 46 : type = groktypename (t, &type_expr, &type_expr_const);
4257 46 : ret.value = c_minof_type (loc, type);
4258 46 : c_last_sizeof_arg = type;
4259 46 : c_last_sizeof_loc = loc;
4260 46 : ret.original_code = MINOF_EXPR;
4261 46 : ret.original_type = NULL;
4262 46 : ret.m_decimal = 0;
4263 46 : if (type == error_mark_node)
4264 : {
4265 0 : ret.value = error_mark_node;
4266 0 : ret.original_code = ERROR_MARK;
4267 : }
4268 46 : pop_maybe_used (type != error_mark_node);
4269 46 : return ret;
4270 : }
4271 :
4272 : /* Build a function call to function FUNCTION with parameters PARAMS.
4273 : The function call is at LOC.
4274 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4275 : TREE_VALUE of each node is a parameter-expression.
4276 : FUNCTION's data type may be a function type or a pointer-to-function. */
4277 :
4278 : tree
4279 0 : build_function_call (location_t loc, tree function, tree params)
4280 : {
4281 0 : vec<tree, va_gc> *v;
4282 0 : tree ret;
4283 :
4284 0 : vec_alloc (v, list_length (params));
4285 0 : for (; params; params = TREE_CHAIN (params))
4286 0 : v->quick_push (TREE_VALUE (params));
4287 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4288 0 : vec_free (v);
4289 0 : return ret;
4290 : }
4291 :
4292 : /* Give a note about the location of the declaration of DECL,
4293 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4294 :
4295 : static void
4296 299 : inform_declaration (tree decl, tree function_expr)
4297 : {
4298 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4299 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4300 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4301 92 : else if (function_expr)
4302 92 : switch (TREE_CODE (function_expr))
4303 : {
4304 : default:
4305 : break;
4306 13 : case COMPONENT_REF:
4307 : /* Show the decl of the pertinent field (e.g. for callback
4308 : fields in a struct. */
4309 13 : {
4310 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4311 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4312 13 : inform (loc, "declared here");
4313 : }
4314 : break;
4315 : }
4316 299 : }
4317 :
4318 :
4319 : /* Build a function call to function FUNCTION with parameters PARAMS.
4320 : If FUNCTION is the result of resolving an overloaded target built-in,
4321 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4322 : ORIGTYPES, if not NULL, is a vector of types; each element is
4323 : either NULL or the original type of the corresponding element in
4324 : PARAMS. The original type may differ from TREE_TYPE of the
4325 : parameter for enums. FUNCTION's data type may be a function type
4326 : or pointer-to-function. This function changes the elements of
4327 : PARAMS. */
4328 :
4329 : tree
4330 51477067 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4331 : tree function, vec<tree, va_gc> *params,
4332 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4333 : {
4334 51477067 : tree fntype, fundecl = NULL_TREE;
4335 51477067 : tree name = NULL_TREE, result;
4336 51477067 : tree tem;
4337 51477067 : int nargs;
4338 51477067 : tree *argarray;
4339 :
4340 :
4341 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4342 51477067 : STRIP_TYPE_NOPS (function);
4343 :
4344 : /* Convert anything with function type to a pointer-to-function. */
4345 51477067 : if (TREE_CODE (function) == FUNCTION_DECL)
4346 : {
4347 51399453 : name = DECL_NAME (function);
4348 :
4349 51399453 : if (flag_tm)
4350 421 : tm_malloc_replacement (function);
4351 51399453 : fundecl = function;
4352 51399453 : if (!orig_fundecl)
4353 51399453 : orig_fundecl = fundecl;
4354 : /* Atomic functions have type checking/casting already done. They are
4355 : often rewritten and don't match the original parameter list. */
4356 102798906 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4357 : origtypes = NULL;
4358 : }
4359 51477067 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4360 51414018 : function = function_to_pointer_conversion (loc, function);
4361 :
4362 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4363 : expressions, like those used for ObjC messenger dispatches. */
4364 51477067 : if (params && !params->is_empty ())
4365 39210485 : function = objc_rewrite_function_call (function, (*params)[0]);
4366 :
4367 51477067 : function = c_fully_fold (function, false, NULL);
4368 :
4369 51477067 : fntype = TREE_TYPE (function);
4370 :
4371 51477067 : if (TREE_CODE (fntype) == ERROR_MARK)
4372 5 : return error_mark_node;
4373 :
4374 51477062 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4375 51477028 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4376 : {
4377 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4378 33 : error_at (loc,
4379 : "called object %qE is not a function or function pointer",
4380 : function);
4381 4 : else if (DECL_P (function))
4382 : {
4383 1 : auto_diagnostic_group d;
4384 1 : error_at (loc,
4385 : "called object %qD is not a function or function pointer",
4386 : function);
4387 1 : inform_declaration (function, NULL_TREE);
4388 1 : }
4389 : else
4390 3 : error_at (loc,
4391 : "called object is not a function or function pointer");
4392 37 : return error_mark_node;
4393 : }
4394 :
4395 51477025 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4396 382706 : current_function_returns_abnormally = 1;
4397 :
4398 : /* fntype now gets the type of function pointed to. */
4399 51477025 : fntype = TREE_TYPE (fntype);
4400 51477025 : tree return_type = TREE_TYPE (fntype);
4401 :
4402 : /* Convert the parameters to the types declared in the
4403 : function prototype, or apply default promotions. */
4404 :
4405 51477025 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4406 : origtypes, function, fundecl);
4407 51477025 : if (nargs < 0)
4408 178 : return error_mark_node;
4409 :
4410 : /* Check that the function is called through a compatible prototype.
4411 : If it is not, warn. */
4412 51475940 : if (CONVERT_EXPR_P (function)
4413 923 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4414 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4415 51477032 : && !comptypes (fntype, TREE_TYPE (tem)))
4416 : {
4417 : /* This situation leads to run-time undefined behavior. We can't,
4418 : therefore, simply error unless we can prove that all possible
4419 : executions of the program must execute the code. */
4420 20 : warning_at (loc, 0, "function called through a non-compatible type");
4421 :
4422 20 : if (VOID_TYPE_P (return_type)
4423 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4424 1 : pedwarn (loc, 0,
4425 : "function with qualified void return type called");
4426 : }
4427 :
4428 51476847 : argarray = vec_safe_address (params);
4429 :
4430 : /* Check that arguments to builtin functions match the expectations. */
4431 51476847 : if (fundecl
4432 51399290 : && fndecl_built_in_p (fundecl)
4433 85731264 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4434 : orig_fundecl, nargs, argarray))
4435 338 : return error_mark_node;
4436 :
4437 : /* Check that the arguments to the function are valid. */
4438 51476509 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4439 : nargs, argarray, &arg_loc,
4440 : comptypes);
4441 :
4442 51476509 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4443 51476509 : && !VOID_TYPE_P (return_type))
4444 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4445 51476509 : if (name != NULL_TREE
4446 102875461 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4447 : {
4448 33522783 : if (require_constant_value)
4449 3462 : result
4450 3462 : = fold_build_call_array_initializer_loc (loc, return_type,
4451 : function, nargs, argarray);
4452 : else
4453 33519321 : result = fold_build_call_array_loc (loc, return_type,
4454 : function, nargs, argarray);
4455 33522783 : if (TREE_CODE (result) == NOP_EXPR
4456 33522783 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4457 26488 : STRIP_TYPE_NOPS (result);
4458 : }
4459 : else
4460 17953726 : result = build_call_array_loc (loc, return_type,
4461 : function, nargs, argarray);
4462 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4463 : later. */
4464 51476509 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4465 215 : suppress_warning (result, OPT_Wnonnull);
4466 :
4467 : /* In this improbable scenario, a nested function returns a VM type.
4468 : Create a TARGET_EXPR so that the call always has a LHS, much as
4469 : what the C++ FE does for functions returning non-PODs. */
4470 51476509 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4471 : {
4472 83 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4473 83 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4474 : NULL_TREE, NULL_TREE);
4475 : }
4476 :
4477 51476509 : if (VOID_TYPE_P (TREE_TYPE (result)))
4478 : {
4479 2422088 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4480 2 : pedwarn (loc, 0,
4481 : "function with qualified void return type called");
4482 : return result;
4483 : }
4484 49054421 : return require_complete_type (loc, result);
4485 : }
4486 :
4487 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4488 :
4489 : tree
4490 51476623 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4491 : tree function, vec<tree, va_gc> *params,
4492 : vec<tree, va_gc> *origtypes)
4493 : {
4494 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4495 51476634 : STRIP_TYPE_NOPS (function);
4496 :
4497 : /* Convert anything with function type to a pointer-to-function. */
4498 51476623 : if (TREE_CODE (function) == FUNCTION_DECL)
4499 : {
4500 : /* Implement type-directed function overloading for builtins.
4501 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4502 : handle all the type checking. The result is a complete expression
4503 : that implements this function call. */
4504 51399009 : tree tem = resolve_overloaded_builtin (loc, function, params);
4505 51399009 : if (tem)
4506 : return tem;
4507 : }
4508 51390380 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4509 : }
4510 :
4511 : /* Helper for convert_arguments called to convert the VALue of argument
4512 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4513 : PARMNUM and TYPE.
4514 : PLOC is the location where the conversion is being performed.
4515 : FUNCTION and FUNDECL are the same as in convert_arguments.
4516 : VALTYPE is the original type of VAL before the conversion and,
4517 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4518 : NPC is true if VAL represents the null pointer constant (VAL itself
4519 : will have been folded to an integer constant).
4520 : RNAME is the same as FUNCTION except in Objective C when it's
4521 : the function selector.
4522 : EXCESS_PRECISION is true when VAL was originally represented
4523 : as EXCESS_PRECISION_EXPR.
4524 : WARNOPT is the same as in convert_for_assignment. */
4525 :
4526 : static tree
4527 128791294 : convert_argument (location_t ploc, tree function, tree fundecl,
4528 : tree type, tree origtype, tree val, tree valtype,
4529 : bool npc, tree rname, int parmnum, int argnum,
4530 : bool excess_precision, int warnopt)
4531 : {
4532 : /* Formal parm type is specified by a function prototype. */
4533 :
4534 128791294 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4535 : {
4536 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4537 : parmnum + 1);
4538 3 : return error_mark_node;
4539 : }
4540 :
4541 : /* Optionally warn about conversions that differ from the default
4542 : conversions. */
4543 128791291 : if (warn_traditional_conversion || warn_traditional)
4544 : {
4545 193 : if (INTEGRAL_TYPE_P (type)
4546 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4547 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4548 : "passing argument %d of %qE as integer rather "
4549 : "than floating due to prototype",
4550 : argnum, rname);
4551 193 : if (INTEGRAL_TYPE_P (type)
4552 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4553 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4554 : "passing argument %d of %qE as integer rather "
4555 : "than complex due to prototype",
4556 : argnum, rname);
4557 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4558 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4559 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4560 : "passing argument %d of %qE as complex rather "
4561 : "than floating due to prototype",
4562 : argnum, rname);
4563 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4564 71 : && INTEGRAL_TYPE_P (valtype))
4565 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4566 : "passing argument %d of %qE as floating rather "
4567 : "than integer due to prototype",
4568 : argnum, rname);
4569 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4570 7 : && INTEGRAL_TYPE_P (valtype))
4571 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4572 : "passing argument %d of %qE as complex rather "
4573 : "than integer due to prototype",
4574 : argnum, rname);
4575 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4576 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4577 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4578 : "passing argument %d of %qE as floating rather "
4579 : "than complex due to prototype",
4580 : argnum, rname);
4581 : /* ??? At some point, messages should be written about
4582 : conversions between complex types, but that's too messy
4583 : to do now. */
4584 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4585 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4586 : {
4587 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4588 :
4589 : /* Warn if any argument is passed as `float',
4590 : since without a prototype it would be `double'. */
4591 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4592 45 : && type != dfloat32_type_node)
4593 7 : warning_at (ploc, 0,
4594 : "passing argument %d of %qE as %<float%> "
4595 : "rather than %<double%> due to prototype",
4596 : argnum, rname);
4597 :
4598 : /* Warn if mismatch between argument and prototype
4599 : for decimal float types. Warn of conversions with
4600 : binary float types and of precision narrowing due to
4601 : prototype. */
4602 38 : else if (type != valtype
4603 32 : && (type == dfloat32_type_node
4604 22 : || type == dfloat64_type_node
4605 12 : || type == dfloat128_type_node
4606 2 : || valtype == dfloat32_type_node
4607 2 : || valtype == dfloat64_type_node
4608 2 : || valtype == dfloat128_type_node)
4609 38 : && (formal_prec
4610 30 : <= TYPE_PRECISION (valtype)
4611 14 : || (type == dfloat128_type_node
4612 10 : && (valtype
4613 10 : != dfloat64_type_node
4614 8 : && (valtype
4615 : != dfloat32_type_node)))
4616 8 : || (type == dfloat64_type_node
4617 4 : && (valtype
4618 : != dfloat32_type_node))))
4619 24 : warning_at (ploc, 0,
4620 : "passing argument %d of %qE as %qT "
4621 : "rather than %qT due to prototype",
4622 : argnum, rname, type, valtype);
4623 :
4624 : }
4625 : /* Detect integer changing in width or signedness.
4626 : These warnings are only activated with
4627 : -Wtraditional-conversion, not with -Wtraditional. */
4628 105 : else if (warn_traditional_conversion
4629 105 : && INTEGRAL_TYPE_P (type)
4630 102 : && INTEGRAL_TYPE_P (valtype))
4631 : {
4632 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4633 83 : tree would_have_been = default_conversion (val);
4634 83 : tree type1 = TREE_TYPE (would_have_been);
4635 :
4636 83 : if (val == error_mark_node)
4637 : /* VAL could have been of incomplete type. */;
4638 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4639 83 : && (TYPE_MAIN_VARIANT (type)
4640 2 : == TYPE_MAIN_VARIANT (valtype)))
4641 : /* No warning if function asks for enum
4642 : and the actual arg is that enum type. */
4643 : ;
4644 81 : else if (formal_prec != TYPE_PRECISION (type1))
4645 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4646 : "passing argument %d of %qE "
4647 : "with different width due to prototype",
4648 : argnum, rname);
4649 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4650 : ;
4651 : /* Don't complain if the formal parameter type
4652 : is an enum, because we can't tell now whether
4653 : the value was an enum--even the same enum. */
4654 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4655 : ;
4656 32 : else if (TREE_CODE (val) == INTEGER_CST
4657 5 : && int_fits_type_p (val, type))
4658 : /* Change in signedness doesn't matter
4659 : if a constant value is unaffected. */
4660 : ;
4661 : /* If the value is extended from a narrower
4662 : unsigned type, it doesn't matter whether we
4663 : pass it as signed or unsigned; the value
4664 : certainly is the same either way. */
4665 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4666 32 : && TYPE_UNSIGNED (valtype))
4667 : ;
4668 32 : else if (TYPE_UNSIGNED (type))
4669 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4670 : "passing argument %d of %qE "
4671 : "as unsigned due to prototype",
4672 : argnum, rname);
4673 : else
4674 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4675 : "passing argument %d of %qE "
4676 : "as signed due to prototype",
4677 : argnum, rname);
4678 : }
4679 : }
4680 :
4681 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4682 : sake of better warnings from convert_and_check. */
4683 128791291 : if (excess_precision)
4684 380 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4685 :
4686 128791291 : tree parmval = convert_for_assignment (ploc, ploc, type,
4687 : val, origtype, ic_argpass,
4688 : npc, fundecl, function,
4689 : parmnum + 1, warnopt);
4690 128791291 : return parmval;
4691 : }
4692 :
4693 : /* Convert the argument expressions in the vector VALUES
4694 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4695 :
4696 : If the list is exhausted, or when an element has NULL as its type,
4697 : perform the default conversions.
4698 :
4699 : ORIGTYPES is the original types of the expressions in VALUES. This
4700 : holds the type of enum values which have been converted to integral
4701 : types. It may be NULL.
4702 :
4703 : FUNCTION is a tree for the called function. It is used only for
4704 : error messages, where it is formatted with %qE.
4705 :
4706 : This is also where warnings about wrong number of args are generated.
4707 :
4708 : ARG_LOC are locations of function arguments (if any).
4709 :
4710 : Returns the actual number of arguments processed (which may be less
4711 : than the length of VALUES in some error situations), or -1 on
4712 : failure. */
4713 :
4714 : static int
4715 51477025 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4716 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4717 : tree function, tree fundecl)
4718 : {
4719 51477025 : tree typelist = TYPE_ARG_TYPES (fntype);
4720 51477025 : unsigned int parmnum;
4721 51477025 : bool error_args = false;
4722 51477025 : const bool type_generic = fundecl
4723 51477025 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4724 51477025 : bool type_generic_remove_excess_precision = false;
4725 51477025 : bool type_generic_overflow_p = false;
4726 51477025 : bool type_generic_bit_query = false;
4727 51477025 : tree selector;
4728 :
4729 : /* Change pointer to function to the function itself for
4730 : diagnostics. */
4731 51477025 : if (TREE_CODE (function) == ADDR_EXPR
4732 51477025 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4733 51401551 : function = TREE_OPERAND (function, 0);
4734 :
4735 : /* Handle an ObjC selector specially for diagnostics. */
4736 51477025 : selector = objc_message_selector ();
4737 :
4738 : /* For a call to a built-in function declared without a prototype,
4739 : set to the built-in function's argument list. */
4740 51477025 : tree builtin_typelist = NULL_TREE;
4741 :
4742 : /* For type-generic built-in functions, determine whether excess
4743 : precision should be removed (classification) or not
4744 : (comparison). */
4745 51477025 : if (fundecl
4746 51477025 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4747 : {
4748 1412502 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4749 1412502 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4750 : {
4751 : /* For a call to a built-in function declared without a prototype
4752 : use the types of the parameters of the internal built-in to
4753 : match those of the arguments to. */
4754 694171 : if (tree bdecl = builtin_decl_explicit (code))
4755 694171 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4756 : }
4757 :
4758 : /* For type-generic built-in functions, determine whether excess
4759 : precision should be removed (classification) or not
4760 : (comparison). */
4761 1412502 : if (type_generic)
4762 61088 : switch (code)
4763 : {
4764 5441 : case BUILT_IN_ISFINITE:
4765 5441 : case BUILT_IN_ISINF:
4766 5441 : case BUILT_IN_ISINF_SIGN:
4767 5441 : case BUILT_IN_ISNAN:
4768 5441 : case BUILT_IN_ISNORMAL:
4769 5441 : case BUILT_IN_ISSIGNALING:
4770 5441 : case BUILT_IN_FPCLASSIFY:
4771 5441 : type_generic_remove_excess_precision = true;
4772 5441 : break;
4773 :
4774 21026 : case BUILT_IN_ADD_OVERFLOW_P:
4775 21026 : case BUILT_IN_SUB_OVERFLOW_P:
4776 21026 : case BUILT_IN_MUL_OVERFLOW_P:
4777 : /* The last argument of these type-generic builtins
4778 : should not be promoted. */
4779 21026 : type_generic_overflow_p = true;
4780 21026 : break;
4781 :
4782 1361 : case BUILT_IN_CLZG:
4783 1361 : case BUILT_IN_CTZG:
4784 1361 : case BUILT_IN_CLRSBG:
4785 1361 : case BUILT_IN_FFSG:
4786 1361 : case BUILT_IN_PARITYG:
4787 1361 : case BUILT_IN_POPCOUNTG:
4788 : /* The first argument of these type-generic builtins
4789 : should not be promoted. */
4790 1361 : type_generic_bit_query = true;
4791 1361 : break;
4792 :
4793 : default:
4794 : break;
4795 : }
4796 : }
4797 :
4798 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4799 : individual converted arguments. */
4800 :
4801 51477025 : tree typetail, builtin_typetail, val;
4802 129730017 : for (typetail = typelist,
4803 51477025 : builtin_typetail = builtin_typelist,
4804 51477025 : parmnum = 0;
4805 181207042 : values && values->iterate (parmnum, &val);
4806 : ++parmnum)
4807 : {
4808 : /* The type of the function parameter (if it was declared with one). */
4809 258519461 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4810 : /* The type of the built-in function parameter (if the function
4811 : is a built-in). Used to detect type incompatibilities in
4812 : calls to built-ins declared without a prototype. */
4813 129730053 : tree builtin_type = (builtin_typetail
4814 130771112 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4815 : /* The original type of the argument being passed to the function. */
4816 129730053 : tree valtype = TREE_TYPE (val);
4817 : /* The called function (or function selector in Objective C). */
4818 129730053 : tree rname = function;
4819 129730053 : int argnum = parmnum + 1;
4820 129730053 : const char *invalid_func_diag;
4821 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4822 129730053 : bool excess_precision = false;
4823 : /* The value of the argument after conversion to the type
4824 : of the function parameter it is passed to. */
4825 129730053 : tree parmval;
4826 : /* Some __atomic_* builtins have additional hidden argument at
4827 : position 0. */
4828 129730053 : location_t ploc
4829 129460281 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4830 129459681 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4831 129730053 : : input_location;
4832 :
4833 129730053 : if (type == void_type_node)
4834 : {
4835 33 : auto_diagnostic_group d;
4836 33 : int num_expected = parmnum;
4837 33 : int num_actual = values->length ();
4838 33 : gcc_rich_location rich_loc (loc);
4839 33 : if (ploc != input_location)
4840 32 : rich_loc.add_range (ploc);
4841 33 : if (selector)
4842 0 : error_at (&rich_loc,
4843 : "too many arguments to method %qE; expected %i, have %i",
4844 : selector, num_expected, num_actual);
4845 : else
4846 33 : error_at (&rich_loc,
4847 : "too many arguments to function %qE; expected %i, have %i",
4848 : function, num_expected, num_actual);
4849 33 : inform_declaration (fundecl, function);
4850 65 : return error_args ? -1 : (int) parmnum;
4851 33 : }
4852 :
4853 129730020 : if (builtin_type == void_type_node)
4854 : {
4855 12 : auto_diagnostic_group d;
4856 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4857 : "too many arguments to built-in function %qE "
4858 : "expecting %d", function, parmnum))
4859 12 : inform_declaration (fundecl, function);
4860 12 : builtin_typetail = NULL_TREE;
4861 12 : }
4862 :
4863 90698 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4864 129820599 : && !(fundecl && fndecl_built_in_p (fundecl)))
4865 : {
4866 6961 : auto_diagnostic_group d;
4867 6961 : bool warned;
4868 6961 : if (selector)
4869 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4870 : "ISO C23 does not allow arguments"
4871 : " for method %qE declared without parameters",
4872 : function);
4873 : else
4874 6961 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4875 : "ISO C23 does not allow arguments"
4876 : " for function %qE declared without parameters",
4877 : function);
4878 6961 : if (warned)
4879 4 : inform_declaration (fundecl, function);
4880 6961 : }
4881 :
4882 129730020 : if (selector && argnum > 2)
4883 : {
4884 0 : rname = selector;
4885 0 : argnum -= 2;
4886 : }
4887 :
4888 : /* Determine if VAL is a null pointer constant before folding it. */
4889 129730020 : bool npc = null_pointer_constant_p (val);
4890 :
4891 : /* If there is excess precision and a prototype, convert once to
4892 : the required type rather than converting via the semantic
4893 : type. Likewise without a prototype a float value represented
4894 : as long double should be converted once to double. But for
4895 : type-generic classification functions excess precision must
4896 : be removed here. */
4897 129730020 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4898 433 : && (type || !type_generic || !type_generic_remove_excess_precision))
4899 : {
4900 413 : val = TREE_OPERAND (val, 0);
4901 413 : excess_precision = true;
4902 : }
4903 129730020 : val = c_fully_fold (val, false, NULL);
4904 259460041 : STRIP_TYPE_NOPS (val);
4905 :
4906 129730020 : val = require_complete_type (ploc, val);
4907 :
4908 : /* Some floating-point arguments must be promoted to double when
4909 : no type is specified by a prototype. This applies to
4910 : arguments of type float, and to architecture-specific types
4911 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4912 129730020 : bool promote_float_arg = false;
4913 129730020 : if (type == NULL_TREE
4914 940645 : && TREE_CODE (valtype) == REAL_TYPE
4915 269655 : && (TYPE_PRECISION (valtype)
4916 269655 : <= TYPE_PRECISION (double_type_node))
4917 262401 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4918 131395 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4919 129861415 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4920 : {
4921 : /* Promote this argument, unless it has a _FloatN or
4922 : _FloatNx type. */
4923 1027662 : promote_float_arg = true;
4924 1027662 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4925 900277 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4926 : {
4927 : promote_float_arg = false;
4928 : break;
4929 : }
4930 : /* Don't promote __bf16 either. */
4931 130662 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4932 129599428 : promote_float_arg = false;
4933 : }
4934 :
4935 129730020 : if (type != NULL_TREE)
4936 : {
4937 128789375 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4938 128789375 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4939 : val, valtype, npc, rname, parmnum, argnum,
4940 : excess_precision, 0);
4941 : }
4942 : /* A NULLPTR type is just a nullptr always. */
4943 940645 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4944 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4945 940636 : else if (promote_float_arg)
4946 : {
4947 127315 : if (type_generic)
4948 : parmval = val;
4949 : else
4950 : {
4951 : /* Convert `float' to `double'. */
4952 124522 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4953 6 : warning_at (ploc, OPT_Wdouble_promotion,
4954 : "implicit conversion from %qT to %qT when passing "
4955 : "argument to function",
4956 : valtype, double_type_node);
4957 124522 : parmval = convert (double_type_node, val);
4958 : }
4959 : }
4960 813321 : else if ((excess_precision && !type_generic)
4961 813319 : || (type_generic_overflow_p && parmnum == 2)
4962 792297 : || (type_generic_bit_query && parmnum == 0))
4963 : /* A "double" argument with excess precision being passed
4964 : without a prototype or in variable arguments.
4965 : The last argument of __builtin_*_overflow_p should not be
4966 : promoted, similarly the first argument of
4967 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4968 22379 : parmval = convert (valtype, val);
4969 1581884 : else if ((invalid_func_diag =
4970 790942 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4971 : {
4972 0 : error (invalid_func_diag);
4973 0 : return -1;
4974 : }
4975 790942 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4976 : {
4977 : return -1;
4978 : }
4979 : else
4980 : /* Convert `short' and `char' to full-size `int'. */
4981 790939 : parmval = default_conversion (val);
4982 :
4983 129730017 : (*values)[parmnum] = parmval;
4984 129730017 : if (parmval == error_mark_node)
4985 105 : error_args = true;
4986 :
4987 129730017 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4988 : {
4989 : /* For a call to a built-in function declared without a prototype,
4990 : perform the conversions from the argument to the expected type
4991 : but issue warnings rather than errors for any mismatches.
4992 : Ignore the converted argument and use the PARMVAL obtained
4993 : above by applying default conversions instead. */
4994 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4995 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4996 : val, valtype, npc, rname, parmnum, argnum,
4997 : excess_precision,
4998 : OPT_Wbuiltin_declaration_mismatch);
4999 : }
5000 :
5001 129730017 : if (typetail)
5002 128789375 : typetail = TREE_CHAIN (typetail);
5003 :
5004 129730017 : if (builtin_typetail)
5005 1041042 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5006 : }
5007 :
5008 90687427 : gcc_assert (parmnum == vec_safe_length (values));
5009 :
5010 102620703 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5011 : {
5012 : /* Not enough args.
5013 : Determine minimum number of arguments required. */
5014 : int min_expected_num = 0;
5015 231 : bool at_least_p = false;
5016 : tree iter = typelist;
5017 386 : while (true)
5018 : {
5019 231 : if (!iter)
5020 : {
5021 : /* Variadic arguments; stop iterating. */
5022 : at_least_p = true;
5023 : break;
5024 : }
5025 222 : if (iter == void_list_node)
5026 : /* End of arguments; stop iterating. */
5027 : break;
5028 155 : ++min_expected_num;
5029 155 : iter = TREE_CHAIN (iter);
5030 : }
5031 76 : auto_diagnostic_group d;
5032 76 : int actual_num = vec_safe_length (values);
5033 143 : error_at (loc,
5034 : at_least_p
5035 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5036 : : G_("too few arguments to function %qE; expected %i, have %i"),
5037 : function, min_expected_num, actual_num);
5038 76 : inform_declaration (fundecl, function);
5039 76 : return -1;
5040 76 : }
5041 :
5042 52134779 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5043 : {
5044 616 : unsigned nargs = parmnum;
5045 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5046 428 : ++nargs;
5047 :
5048 188 : auto_diagnostic_group d;
5049 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5050 : "too few arguments to built-in function %qE "
5051 : "expecting %u", function, nargs - 1))
5052 173 : inform_declaration (fundecl, function);
5053 188 : }
5054 :
5055 51476913 : return error_args ? -1 : (int) parmnum;
5056 : }
5057 :
5058 : /* This is the entry point used by the parser to build unary operators
5059 : in the input. CODE, a tree_code, specifies the unary operator, and
5060 : ARG is the operand. For unary plus, the C parser currently uses
5061 : CONVERT_EXPR for code.
5062 :
5063 : LOC is the location to use for the tree generated.
5064 : */
5065 :
5066 : struct c_expr
5067 8450248 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5068 : {
5069 8450248 : struct c_expr result;
5070 :
5071 8450248 : result.original_code = code;
5072 8450248 : result.original_type = NULL;
5073 8450248 : result.m_decimal = 0;
5074 :
5075 8450248 : if (reject_gcc_builtin (arg.value))
5076 : {
5077 2 : result.value = error_mark_node;
5078 : }
5079 : else
5080 : {
5081 8450246 : result.value = build_unary_op (loc, code, arg.value, false);
5082 :
5083 8450246 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5084 5 : overflow_warning (loc, result.value, arg.value);
5085 : }
5086 :
5087 : /* We are typically called when parsing a prefix token at LOC acting on
5088 : ARG. Reflect this by updating the source range of the result to
5089 : start at LOC and end at the end of ARG. */
5090 8450248 : set_c_expr_source_range (&result,
5091 : loc, arg.get_finish ());
5092 :
5093 8450248 : return result;
5094 : }
5095 :
5096 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5097 :
5098 : bool
5099 1272830 : char_type_p (tree type)
5100 : {
5101 1272830 : return (type == char_type_node
5102 981166 : || type == unsigned_char_type_node
5103 962053 : || type == signed_char_type_node
5104 961205 : || type == char16_type_node
5105 2012763 : || type == char32_type_node);
5106 : }
5107 :
5108 : /* This is the entry point used by the parser to build binary operators
5109 : in the input. CODE, a tree_code, specifies the binary operator, and
5110 : ARG1 and ARG2 are the operands. In addition to constructing the
5111 : expression, we check for operands that were written with other binary
5112 : operators in a way that is likely to confuse the user. ORIG_ARG1 is
5113 : the original first operand for TRUTH_{AND,OR}IF_EXPR before it is
5114 : converted to truth value, otherwise NULL_TREE.
5115 :
5116 : LOCATION is the location of the binary operator. */
5117 :
5118 : struct c_expr
5119 9386956 : parser_build_binary_op (location_t location, enum tree_code code,
5120 : struct c_expr arg1, struct c_expr arg2, tree orig_arg1)
5121 : {
5122 9386956 : struct c_expr result;
5123 9386956 : result.m_decimal = 0;
5124 :
5125 9386956 : enum tree_code code1 = arg1.original_code;
5126 9386956 : enum tree_code code2 = arg2.original_code;
5127 9386956 : tree type1 = (arg1.original_type
5128 9386956 : ? arg1.original_type
5129 6024618 : : TREE_TYPE (arg1.value));
5130 9386956 : tree type2 = (arg2.original_type
5131 9386956 : ? arg2.original_type
5132 6925428 : : TREE_TYPE (arg2.value));
5133 :
5134 9386956 : result.value = build_binary_op (location, code,
5135 : arg1.value, arg2.value, true);
5136 9386955 : result.original_code = code;
5137 9386955 : result.original_type = NULL;
5138 9386955 : result.m_decimal = 0;
5139 :
5140 9386955 : if (TREE_CODE (result.value) == ERROR_MARK)
5141 : {
5142 1297 : set_c_expr_source_range (&result,
5143 : arg1.get_start (),
5144 : arg2.get_finish ());
5145 1297 : return result;
5146 : }
5147 :
5148 9385658 : if (location != UNKNOWN_LOCATION)
5149 9385658 : protected_set_expr_location (result.value, location);
5150 :
5151 9385658 : set_c_expr_source_range (&result,
5152 : arg1.get_start (),
5153 : arg2.get_finish ());
5154 :
5155 : /* Check for cases such as x+y<<z which users are likely
5156 : to misinterpret. */
5157 9385658 : if (warn_parentheses)
5158 2013127 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5159 : arg2.value);
5160 :
5161 9385658 : if (warn_logical_op)
5162 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5163 : code1, arg1.value, code2, arg2.value);
5164 :
5165 9385658 : if (warn_constant_logical_operand
5166 2011542 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5167 74568 : && INTEGRAL_NB_TYPE_P (type1)
5168 72763 : && INTEGRAL_NB_TYPE_P (type2))
5169 : {
5170 71170 : const char *name = code == TRUTH_ANDIF_EXPR ? "&&" : "||";
5171 71170 : if (orig_arg1 == NULL_TREE)
5172 0 : orig_arg1 = arg1.value;
5173 76567 : auto enum_other_than_0_1 = [] (tree type) {
5174 5397 : if (TREE_CODE (type) != ENUMERAL_TYPE)
5175 : return false;
5176 48 : for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
5177 : {
5178 40 : tree v = DECL_INITIAL (TREE_VALUE (l));
5179 40 : if (!integer_zerop (v) && !integer_onep (v))
5180 : return true;
5181 : }
5182 : return false;
5183 : };
5184 213494 : auto diagnose_constant_logical_operand = [=] (tree val, tree type) {
5185 142324 : if (TREE_CODE (val) != INTEGER_CST || integer_zerop (val))
5186 : return false;
5187 5421 : if (integer_onep (val) && !enum_other_than_0_1 (type))
5188 : return false;
5189 32 : gcc_rich_location richloc (location);
5190 32 : richloc.add_fixit_replace (name + 1);
5191 32 : auto_diagnostic_group d;
5192 32 : if (warning_at (location, OPT_Wconstant_logical_operand,
5193 : "use of logical %qs with constant operand %qE",
5194 : name, val))
5195 32 : inform (&richloc, "use %qs for bitwise operation", name + 1);
5196 32 : return true;
5197 32 : };
5198 71170 : if (!diagnose_constant_logical_operand (arg2.value, type2))
5199 71154 : diagnose_constant_logical_operand (orig_arg1, type1);
5200 : }
5201 :
5202 9385658 : if (warn_tautological_compare)
5203 : {
5204 2011640 : tree lhs = arg1.value;
5205 2011640 : tree rhs = arg2.value;
5206 2011640 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5207 : {
5208 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5209 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5210 : lhs = NULL_TREE;
5211 : else
5212 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5213 : }
5214 2011640 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5215 : {
5216 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5217 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5218 : rhs = NULL_TREE;
5219 : else
5220 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5221 : }
5222 2011640 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5223 2011640 : warn_tautological_cmp (location, code, lhs, rhs);
5224 : }
5225 :
5226 9385658 : if (warn_logical_not_paren
5227 2011723 : && TREE_CODE_CLASS (code) == tcc_comparison
5228 410413 : && code1 == TRUTH_NOT_EXPR
5229 410413 : && code2 != TRUTH_NOT_EXPR
5230 : /* Avoid warning for !!x == y. */
5231 9385792 : && (TREE_CODE (arg1.value) != NE_EXPR
5232 27 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5233 : {
5234 : /* Avoid warning for !b == y where b has _Bool type. */
5235 107 : tree t = integer_zero_node;
5236 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5237 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5238 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5239 : {
5240 90 : t = TREE_OPERAND (arg1.value, 0);
5241 199 : do
5242 : {
5243 199 : if (TREE_TYPE (t) != integer_type_node)
5244 : break;
5245 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5246 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5247 90 : else if (CONVERT_EXPR_P (t))
5248 19 : t = TREE_OPERAND (t, 0);
5249 : else
5250 : break;
5251 : }
5252 : while (1);
5253 : }
5254 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5255 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5256 : }
5257 :
5258 : /* Warn about comparisons against string literals, with the exception
5259 : of testing for equality or inequality of a string literal with NULL. */
5260 9385658 : if (code == EQ_EXPR || code == NE_EXPR)
5261 : {
5262 1178540 : if ((code1 == STRING_CST
5263 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5264 1178551 : || (code2 == STRING_CST
5265 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5266 36 : warning_at (location, OPT_Waddress,
5267 : "comparison with string literal results in unspecified behavior");
5268 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5269 1178540 : if (POINTER_TYPE_P (type1)
5270 92488 : && null_pointer_constant_p (arg2.value)
5271 1199854 : && char_type_p (type2))
5272 : {
5273 18 : auto_diagnostic_group d;
5274 18 : if (warning_at (location, OPT_Wpointer_compare,
5275 : "comparison between pointer and zero character "
5276 : "constant"))
5277 10 : inform (arg1.get_start (),
5278 : "did you mean to dereference the pointer?");
5279 18 : }
5280 1178522 : else if (POINTER_TYPE_P (type2)
5281 89831 : && null_pointer_constant_p (arg1.value)
5282 1178794 : && char_type_p (type1))
5283 : {
5284 10 : auto_diagnostic_group d;
5285 10 : if (warning_at (location, OPT_Wpointer_compare,
5286 : "comparison between pointer and zero character "
5287 : "constant"))
5288 10 : inform (arg2.get_start (),
5289 : "did you mean to dereference the pointer?");
5290 10 : }
5291 : }
5292 8207118 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5293 1008681 : && (code1 == STRING_CST || code2 == STRING_CST))
5294 0 : warning_at (location, OPT_Waddress,
5295 : "comparison with string literal results in unspecified "
5296 : "behavior");
5297 :
5298 9385658 : if (warn_zero_as_null_pointer_constant
5299 24 : && c_inhibit_evaluation_warnings == 0
5300 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5301 : {
5302 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5303 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5304 9 : && INTEGRAL_TYPE_P (type2)
5305 33 : && null_pointer_constant_p (arg2.value))
5306 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5307 : "zero as null pointer constant");
5308 :
5309 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5310 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5311 11 : && INTEGRAL_TYPE_P (type1)
5312 35 : && null_pointer_constant_p (arg1.value))
5313 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5314 : "zero as null pointer constant");
5315 : }
5316 :
5317 9385658 : if (warn_array_compare
5318 2011478 : && TREE_CODE_CLASS (code) == tcc_comparison
5319 410198 : && TREE_CODE (type1) == ARRAY_TYPE
5320 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5321 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5322 :
5323 2199906 : if (TREE_OVERFLOW_P (result.value)
5324 299 : && !TREE_OVERFLOW_P (arg1.value)
5325 9385950 : && !TREE_OVERFLOW_P (arg2.value))
5326 249 : overflow_warning (location, result.value);
5327 :
5328 : /* Warn about comparisons of different enum types. */
5329 9385657 : if (warn_enum_compare
5330 2031600 : && TREE_CODE_CLASS (code) == tcc_comparison
5331 414739 : && TREE_CODE (type1) == ENUMERAL_TYPE
5332 7269 : && TREE_CODE (type2) == ENUMERAL_TYPE
5333 9392862 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5334 2 : warning_at (location, OPT_Wenum_compare,
5335 : "comparison between %qT and %qT",
5336 : type1, type2);
5337 :
5338 9385657 : if (warn_xor_used_as_pow
5339 9385657 : && code == BIT_XOR_EXPR
5340 77702 : && arg1.m_decimal
5341 732 : && arg2.m_decimal)
5342 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5343 : location,
5344 : arg2.get_location (), arg2.value);
5345 :
5346 : return result;
5347 : }
5348 :
5349 : /* Return a tree for the difference of pointers OP0 and OP1.
5350 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5351 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5352 :
5353 : static tree
5354 3750 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5355 : {
5356 3750 : tree restype = ptrdiff_type_node;
5357 3750 : tree result, inttype;
5358 :
5359 3750 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5360 3750 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5361 3750 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5362 3750 : tree orig_op0 = op0;
5363 3750 : tree orig_op1 = op1;
5364 :
5365 : /* If the operands point into different address spaces, we need to
5366 : explicitly convert them to pointers into the common address space
5367 : before we can subtract the numerical address values. */
5368 3750 : if (as0 != as1)
5369 : {
5370 0 : addr_space_t as_common;
5371 0 : tree common_type;
5372 :
5373 : /* Determine the common superset address space. This is guaranteed
5374 : to exist because the caller verified that comp_target_types
5375 : returned non-zero. */
5376 0 : if (!addr_space_superset (as0, as1, &as_common))
5377 0 : gcc_unreachable ();
5378 :
5379 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5380 0 : op0 = convert (common_type, op0);
5381 0 : op1 = convert (common_type, op1);
5382 : }
5383 :
5384 : /* Determine integer type result of the subtraction. This will usually
5385 : be the same as the result type (ptrdiff_t), but may need to be a wider
5386 : type if pointers for the address space are wider than ptrdiff_t. */
5387 3750 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5388 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5389 : else
5390 : inttype = restype;
5391 :
5392 3750 : if (VOID_TYPE_P (target_type))
5393 146 : pedwarn (loc, OPT_Wpointer_arith,
5394 : "pointer of type %<void *%> used in subtraction");
5395 3750 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5396 4 : pedwarn (loc, OPT_Wpointer_arith,
5397 : "pointer to a function used in subtraction");
5398 :
5399 3750 : if (current_function_decl != NULL_TREE
5400 3750 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5401 : {
5402 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5403 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5404 :
5405 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5406 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5407 : }
5408 :
5409 : /* First do the subtraction, then build the divide operator
5410 : and only convert at the very end.
5411 : Do not do default conversions in case restype is a short type. */
5412 :
5413 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5414 : pointers. If some platform cannot provide that, or has a larger
5415 : ptrdiff_type to support differences larger than half the address
5416 : space, cast the pointers to some larger integer type and do the
5417 : computations in that type. */
5418 3750 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5419 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5420 : convert (inttype, op1), false);
5421 : else
5422 : {
5423 : /* Cast away qualifiers. */
5424 3750 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5425 3750 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5426 3750 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5427 : }
5428 :
5429 : /* This generates an error if op1 is pointer to incomplete type. */
5430 3750 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5431 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5432 3746 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5433 3746 : TREE_TYPE (TREE_TYPE (orig_op0))))
5434 7492 : verify_type_context (loc, TCTX_POINTER_ARITH,
5435 3746 : TREE_TYPE (TREE_TYPE (orig_op1)));
5436 :
5437 3750 : op1 = c_size_in_bytes (target_type);
5438 :
5439 3750 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5440 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5441 :
5442 : /* Divide by the size, in easiest possible way. */
5443 3750 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5444 : op0, convert (inttype, op1));
5445 :
5446 : /* Convert to final result type if necessary. */
5447 3750 : return convert (restype, result);
5448 : }
5449 :
5450 : /* Expand atomic compound assignments into an appropriate sequence as
5451 : specified by the C11 standard section 6.5.16.2.
5452 :
5453 : _Atomic T1 E1
5454 : T2 E2
5455 : E1 op= E2
5456 :
5457 : This sequence is used for all types for which these operations are
5458 : supported.
5459 :
5460 : In addition, built-in versions of the 'fe' prefixed routines may
5461 : need to be invoked for floating point (real, complex or vector) when
5462 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5463 :
5464 : T1 newval;
5465 : T1 old;
5466 : T1 *addr
5467 : T2 val
5468 : fenv_t fenv
5469 :
5470 : addr = &E1;
5471 : val = (E2);
5472 : __atomic_load (addr, &old, SEQ_CST);
5473 : feholdexcept (&fenv);
5474 : loop:
5475 : newval = old op val;
5476 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5477 : SEQ_CST))
5478 : goto done;
5479 : feclearexcept (FE_ALL_EXCEPT);
5480 : goto loop:
5481 : done:
5482 : feupdateenv (&fenv);
5483 :
5484 : The compiler will issue the __atomic_fetch_* built-in when possible,
5485 : otherwise it will generate the generic form of the atomic operations.
5486 : This requires temp(s) and has their address taken. The atomic processing
5487 : is smart enough to figure out when the size of an object can utilize
5488 : a lock-free version, and convert the built-in call to the appropriate
5489 : lock-free routine. The optimizers will then dispose of any temps that
5490 : are no longer required, and lock-free implementations are utilized as
5491 : long as there is target support for the required size.
5492 :
5493 : If the operator is NOP_EXPR, then this is a simple assignment, and
5494 : an __atomic_store is issued to perform the assignment rather than
5495 : the above loop. */
5496 :
5497 : /* Build an atomic assignment at LOC, expanding into the proper
5498 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5499 : the result of the operation, unless RETURN_OLD_P, in which case
5500 : return the old value of LHS (this is only for postincrement and
5501 : postdecrement). */
5502 :
5503 : static tree
5504 30457 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5505 : tree rhs, bool return_old_p)
5506 : {
5507 30457 : tree fndecl, func_call;
5508 30457 : vec<tree, va_gc> *params;
5509 30457 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5510 30457 : tree old, old_addr;
5511 30457 : tree compound_stmt = NULL_TREE;
5512 30457 : tree stmt, goto_stmt;
5513 30457 : tree loop_label, loop_decl, done_label, done_decl;
5514 :
5515 30457 : tree lhs_type = TREE_TYPE (lhs);
5516 30457 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5517 30457 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5518 30457 : tree rhs_semantic_type = TREE_TYPE (rhs);
5519 30457 : tree nonatomic_rhs_semantic_type;
5520 30457 : tree rhs_type;
5521 :
5522 30457 : gcc_assert (TYPE_ATOMIC (lhs_type));
5523 :
5524 30457 : if (return_old_p)
5525 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5526 :
5527 : /* Allocate enough vector items for a compare_exchange. */
5528 30457 : vec_alloc (params, 6);
5529 :
5530 : /* Create a compound statement to hold the sequence of statements
5531 : with a loop. */
5532 30457 : if (modifycode != NOP_EXPR)
5533 : {
5534 19954 : compound_stmt = c_begin_compound_stmt (false);
5535 :
5536 : /* For consistency with build_modify_expr on non-_Atomic,
5537 : mark the lhs as read. Also, it would be very hard to match
5538 : such expressions in mark_exp_read. */
5539 19954 : mark_exp_read (lhs);
5540 : }
5541 :
5542 : /* Remove any excess precision (which is only present here in the
5543 : case of compound assignments). */
5544 30457 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5545 : {
5546 0 : gcc_assert (modifycode != NOP_EXPR);
5547 0 : rhs = TREE_OPERAND (rhs, 0);
5548 : }
5549 30457 : rhs_type = TREE_TYPE (rhs);
5550 :
5551 : /* Fold the RHS if it hasn't already been folded. */
5552 30457 : if (modifycode != NOP_EXPR)
5553 19954 : rhs = c_fully_fold (rhs, false, NULL);
5554 :
5555 : /* Remove the qualifiers for the rest of the expressions and create
5556 : the VAL temp variable to hold the RHS. */
5557 30457 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5558 30457 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5559 30457 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5560 : TYPE_UNQUALIFIED);
5561 30457 : val = create_tmp_var_raw (nonatomic_rhs_type);
5562 30457 : TREE_ADDRESSABLE (val) = 1;
5563 30457 : suppress_warning (val);
5564 30457 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5565 : NULL_TREE);
5566 30457 : TREE_SIDE_EFFECTS (rhs) = 1;
5567 30457 : SET_EXPR_LOCATION (rhs, loc);
5568 30457 : if (modifycode != NOP_EXPR)
5569 19954 : add_stmt (rhs);
5570 :
5571 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5572 : an atomic_store. */
5573 19954 : if (modifycode == NOP_EXPR)
5574 : {
5575 10503 : compound_stmt = rhs;
5576 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5577 10503 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5578 10503 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5579 10503 : params->quick_push (lhs_addr);
5580 10503 : params->quick_push (rhs);
5581 10503 : params->quick_push (seq_cst);
5582 10503 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5583 :
5584 10503 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5585 : compound_stmt, func_call);
5586 :
5587 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5588 : the statement and that value. */
5589 10503 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5590 : }
5591 :
5592 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5593 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5594 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5595 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5596 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5597 : {
5598 11713 : built_in_function fncode;
5599 11713 : switch (modifycode)
5600 : {
5601 3075 : case PLUS_EXPR:
5602 3075 : case POINTER_PLUS_EXPR:
5603 2124 : fncode = (return_old_p
5604 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5605 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5606 : break;
5607 2889 : case MINUS_EXPR:
5608 2094 : fncode = (return_old_p
5609 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5610 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5611 : break;
5612 609 : case BIT_AND_EXPR:
5613 609 : fncode = (return_old_p
5614 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5615 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5616 : break;
5617 609 : case BIT_IOR_EXPR:
5618 609 : fncode = (return_old_p
5619 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5620 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5621 : break;
5622 609 : case BIT_XOR_EXPR:
5623 609 : fncode = (return_old_p
5624 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5625 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5626 : break;
5627 3922 : default:
5628 3922 : goto cas_loop;
5629 : }
5630 :
5631 : /* We can only use "_1" through "_16" variants of the atomic fetch
5632 : built-ins. */
5633 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5634 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5635 0 : goto cas_loop;
5636 :
5637 : /* If this is a pointer type, we need to multiply by the size of
5638 : the pointer target type. */
5639 7791 : if (POINTER_TYPE_P (lhs_type))
5640 : {
5641 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5642 : /* ??? This would introduce -Wdiscarded-qualifiers
5643 : warning: __atomic_fetch_* expect volatile void *
5644 : type as the first argument. (Assignments between
5645 : atomic and non-atomic objects are OK.) */
5646 1018 : || TYPE_RESTRICT (lhs_type))
5647 17 : goto cas_loop;
5648 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5649 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5650 : convert (ptrdiff_type_node, rhs),
5651 : convert (ptrdiff_type_node, sz));
5652 : }
5653 :
5654 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5655 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5656 7774 : fndecl = builtin_decl_explicit (fncode);
5657 7774 : params->quick_push (lhs_addr);
5658 7774 : params->quick_push (rhs);
5659 7774 : params->quick_push (seq_cst);
5660 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5661 :
5662 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5663 7774 : TREE_ADDRESSABLE (newval) = 1;
5664 7774 : suppress_warning (newval);
5665 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5666 : NULL_TREE, NULL_TREE);
5667 7774 : SET_EXPR_LOCATION (rhs, loc);
5668 7774 : add_stmt (rhs);
5669 :
5670 : /* Finish the compound statement. */
5671 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5672 :
5673 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5674 : the statement and that value. */
5675 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5676 : }
5677 :
5678 8241 : cas_loop:
5679 : /* Create the variables and labels required for the op= form. */
5680 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5681 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5682 12180 : TREE_ADDRESSABLE (old) = 1;
5683 12180 : suppress_warning (old);
5684 :
5685 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5686 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5687 12180 : TREE_ADDRESSABLE (newval) = 1;
5688 12180 : suppress_warning (newval);
5689 :
5690 12180 : loop_decl = create_artificial_label (loc);
5691 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5692 :
5693 12180 : done_decl = create_artificial_label (loc);
5694 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5695 :
5696 : /* __atomic_load (addr, &old, SEQ_CST). */
5697 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5698 12180 : params->quick_push (lhs_addr);
5699 12180 : params->quick_push (old_addr);
5700 12180 : params->quick_push (seq_cst);
5701 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5702 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5703 : NULL_TREE);
5704 12180 : add_stmt (old);
5705 12180 : params->truncate (0);
5706 :
5707 : /* Create the expressions for floating-point environment
5708 : manipulation, if required. */
5709 12180 : bool need_fenv = (flag_trapping_math
5710 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5711 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5712 12180 : if (need_fenv)
5713 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5714 :
5715 12180 : if (hold_call)
5716 7054 : add_stmt (hold_call);
5717 :
5718 : /* loop: */
5719 12180 : add_stmt (loop_label);
5720 :
5721 : /* newval = old + val; */
5722 12180 : if (rhs_type != rhs_semantic_type)
5723 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5724 12180 : rhs = build_binary_op (loc, modifycode,
5725 : convert_lvalue_to_rvalue (loc, old, true, true),
5726 : val, true);
5727 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5728 : {
5729 0 : tree eptype = TREE_TYPE (rhs);
5730 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5731 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5732 : }
5733 : else
5734 12180 : rhs = c_fully_fold (rhs, false, NULL);
5735 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5736 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5737 : NULL_TREE, 0);
5738 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5739 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5740 : subsequent ICEs in nested function processing after an error, ensure such
5741 : a TARGET_EXPR is built even after an error. */
5742 12180 : if (rhs == error_mark_node)
5743 10 : rhs = old;
5744 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5745 : NULL_TREE);
5746 12180 : SET_EXPR_LOCATION (rhs, loc);
5747 12180 : add_stmt (rhs);
5748 :
5749 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5750 : goto done; */
5751 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5752 12180 : params->quick_push (lhs_addr);
5753 12180 : params->quick_push (old_addr);
5754 12180 : params->quick_push (newval_addr);
5755 12180 : params->quick_push (integer_zero_node);
5756 12180 : params->quick_push (seq_cst);
5757 12180 : params->quick_push (seq_cst);
5758 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5759 :
5760 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5761 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5762 :
5763 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5764 12180 : SET_EXPR_LOCATION (stmt, loc);
5765 12180 : add_stmt (stmt);
5766 :
5767 12180 : if (clear_call)
5768 7054 : add_stmt (clear_call);
5769 :
5770 : /* goto loop; */
5771 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5772 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5773 12180 : add_stmt (goto_stmt);
5774 :
5775 : /* done: */
5776 12180 : add_stmt (done_label);
5777 :
5778 12180 : if (update_call)
5779 7054 : add_stmt (update_call);
5780 :
5781 : /* Finish the compound statement. */
5782 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5783 :
5784 : /* NEWVAL is the value that was successfully stored, return a
5785 : COMPOUND_EXPR of the statement and the appropriate value. */
5786 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5787 12180 : return_old_p ? old : newval);
5788 : }
5789 :
5790 : /* Construct and perhaps optimize a tree representation
5791 : for a unary operation. CODE, a tree_code, specifies the operation
5792 : and XARG is the operand.
5793 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5794 : promotions (such as from short to int).
5795 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5796 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5797 : to pointers in C99.
5798 :
5799 : LOCATION is the location of the operator. */
5800 :
5801 : tree
5802 62405968 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5803 : bool noconvert)
5804 : {
5805 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5806 62405968 : tree arg = xarg;
5807 62405968 : tree argtype = NULL_TREE;
5808 62405968 : enum tree_code typecode;
5809 62405968 : tree val;
5810 62405968 : tree ret = error_mark_node;
5811 62405968 : tree eptype = NULL_TREE;
5812 62405968 : const char *invalid_op_diag;
5813 :
5814 : /* If ARG is wrapped in a call to .ACCESS_WITH_SIZE — created when the
5815 : C parser rvalue-converts a counted_by-annotated member access (see
5816 : PR123569) — unwrap it here. Unary operators that consume an rvalue
5817 : (!, -, +) read the value itself rather than dereferencing into the
5818 : pointed-to data, so the bounds-checking wrapper is unnecessary and
5819 : would otherwise reach build_unary_op while it is not equipped to
5820 : handle the wrapped form. PR123569 fixed the ++/-- side of this by
5821 : suppressing wrapper creation in the parser; the rvalue-consuming
5822 : ops still receive the wrapper and need to strip it here. */
5823 62405968 : if (is_access_with_size_p (arg))
5824 1 : xarg = arg = get_ref_from_access_with_size (arg);
5825 :
5826 62405968 : gcc_checking_assert (!is_access_with_size_p (arg));
5827 :
5828 62405968 : bool int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5829 : if (int_operands)
5830 6738694 : arg = remove_c_maybe_const_expr (arg);
5831 :
5832 62405968 : if (code != ADDR_EXPR)
5833 8829477 : arg = require_complete_type (location, arg);
5834 :
5835 62405968 : typecode = TREE_CODE (TREE_TYPE (arg));
5836 62405968 : if (typecode == ERROR_MARK)
5837 74 : return error_mark_node;
5838 62405894 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5839 32308 : typecode = INTEGER_TYPE;
5840 :
5841 124811788 : if ((invalid_op_diag
5842 62405894 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5843 : {
5844 0 : error_at (location, invalid_op_diag);
5845 0 : return error_mark_node;
5846 : }
5847 :
5848 62405894 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5849 : {
5850 245 : eptype = TREE_TYPE (arg);
5851 245 : arg = TREE_OPERAND (arg, 0);
5852 : }
5853 :
5854 62405894 : switch (code)
5855 : {
5856 28760 : case CONVERT_EXPR:
5857 : /* This is used for unary plus, because a CONVERT_EXPR
5858 : is enough to prevent anybody from looking inside for
5859 : associativity, but won't generate any code. */
5860 28765 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5861 13 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5862 10 : || typecode == BITINT_TYPE
5863 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5864 : {
5865 1 : error_at (location, "wrong type argument to unary plus");
5866 1 : return error_mark_node;
5867 : }
5868 28759 : else if (!noconvert)
5869 28759 : arg = default_conversion (arg);
5870 28759 : arg = non_lvalue_loc (location, arg);
5871 28759 : break;
5872 :
5873 6976476 : case NEGATE_EXPR:
5874 7425972 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5875 486210 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5876 454942 : || typecode == BITINT_TYPE
5877 449496 : || gnu_vector_type_p (TREE_TYPE (arg))))
5878 : {
5879 1 : error_at (location, "wrong type argument to unary minus");
5880 1 : return error_mark_node;
5881 : }
5882 6976475 : else if (!noconvert)
5883 6976469 : arg = default_conversion (arg);
5884 : break;
5885 :
5886 296626 : case BIT_NOT_EXPR:
5887 : /* ~ works on integer types and non float vectors. */
5888 296626 : if (typecode == INTEGER_TYPE
5889 296626 : || typecode == BITINT_TYPE
5890 296626 : || (gnu_vector_type_p (TREE_TYPE (arg))
5891 2120 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5892 : {
5893 : tree e = arg;
5894 :
5895 : /* Warn if the expression has boolean value. */
5896 296027 : while (TREE_CODE (e) == COMPOUND_EXPR)
5897 11 : e = TREE_OPERAND (e, 1);
5898 :
5899 592016 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5900 592016 : || truth_value_p (TREE_CODE (e))))
5901 : {
5902 148 : auto_diagnostic_group d;
5903 148 : if (warning_at (location, OPT_Wbool_operation,
5904 : "%<~%> on a boolean expression"))
5905 : {
5906 12 : gcc_rich_location richloc (location);
5907 12 : richloc.add_fixit_insert_before (location, "!");
5908 12 : inform (&richloc, "did you mean to use logical not?");
5909 12 : }
5910 148 : }
5911 296016 : if (!noconvert)
5912 296013 : arg = default_conversion (arg);
5913 : }
5914 610 : else if (typecode == COMPLEX_TYPE)
5915 : {
5916 605 : code = CONJ_EXPR;
5917 605 : pedwarn (location, OPT_Wpedantic,
5918 : "ISO C does not support %<~%> for complex conjugation");
5919 605 : if (!noconvert)
5920 605 : arg = default_conversion (arg);
5921 : }
5922 : else
5923 : {
5924 5 : error_at (location, "wrong type argument to bit-complement");
5925 5 : return error_mark_node;
5926 : }
5927 : break;
5928 :
5929 3 : case ABS_EXPR:
5930 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5931 : {
5932 0 : error_at (location, "wrong type argument to abs");
5933 0 : return error_mark_node;
5934 : }
5935 3 : else if (!noconvert)
5936 0 : arg = default_conversion (arg);
5937 : break;
5938 :
5939 7 : case ABSU_EXPR:
5940 7 : if (!(typecode == INTEGER_TYPE))
5941 : {
5942 0 : error_at (location, "wrong type argument to absu");
5943 0 : return error_mark_node;
5944 : }
5945 7 : else if (!noconvert)
5946 0 : arg = default_conversion (arg);
5947 : break;
5948 :
5949 0 : case CONJ_EXPR:
5950 : /* Conjugating a real value is a no-op, but allow it anyway. */
5951 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5952 : || typecode == COMPLEX_TYPE))
5953 : {
5954 0 : error_at (location, "wrong type argument to conjugation");
5955 0 : return error_mark_node;
5956 : }
5957 0 : else if (!noconvert)
5958 0 : arg = default_conversion (arg);
5959 : break;
5960 :
5961 378734 : case TRUTH_NOT_EXPR:
5962 378734 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5963 7236 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5964 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5965 18 : && typecode != BITINT_TYPE)
5966 : {
5967 10 : error_at (location,
5968 : "wrong type argument to unary exclamation mark");
5969 10 : return error_mark_node;
5970 : }
5971 378724 : if (int_operands)
5972 : {
5973 139875 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5974 139875 : arg = remove_c_maybe_const_expr (arg);
5975 : }
5976 : else
5977 238849 : arg = c_objc_common_truthvalue_conversion (location, arg);
5978 378724 : ret = invert_truthvalue_loc (location, arg);
5979 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5980 378724 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5981 238442 : location = EXPR_LOCATION (ret);
5982 378724 : goto return_build_unary_op;
5983 :
5984 203104 : case REALPART_EXPR:
5985 203104 : case IMAGPART_EXPR:
5986 203104 : ret = build_real_imag_expr (location, code, arg);
5987 203104 : if (ret == error_mark_node)
5988 : return error_mark_node;
5989 203096 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5990 2 : eptype = TREE_TYPE (eptype);
5991 203096 : goto return_build_unary_op;
5992 :
5993 945724 : case PREINCREMENT_EXPR:
5994 945724 : case POSTINCREMENT_EXPR:
5995 945724 : case PREDECREMENT_EXPR:
5996 945724 : case POSTDECREMENT_EXPR:
5997 :
5998 945724 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5999 : {
6000 2 : tree inner = build_unary_op (location, code,
6001 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
6002 : noconvert);
6003 2 : if (inner == error_mark_node)
6004 : return error_mark_node;
6005 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6006 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6007 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6008 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
6009 2 : goto return_build_unary_op;
6010 : }
6011 :
6012 : /* Complain about anything that is not a true lvalue. In
6013 : Objective-C, skip this check for property_refs. */
6014 945722 : if (!objc_is_property_ref (arg)
6015 1891444 : && !lvalue_or_else (location,
6016 945722 : arg, ((code == PREINCREMENT_EXPR
6017 945722 : || code == POSTINCREMENT_EXPR)
6018 : ? lv_increment
6019 : : lv_decrement)))
6020 22 : return error_mark_node;
6021 :
6022 945700 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
6023 : {
6024 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6025 2 : warning_at (location, OPT_Wc___compat,
6026 : "increment of enumeration value is invalid in C++");
6027 : else
6028 2 : warning_at (location, OPT_Wc___compat,
6029 : "decrement of enumeration value is invalid in C++");
6030 : }
6031 :
6032 945700 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6033 : {
6034 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6035 328 : warning_at (location, OPT_Wbool_operation,
6036 : "increment of a boolean expression");
6037 : else
6038 336 : warning_at (location, OPT_Wbool_operation,
6039 : "decrement of a boolean expression");
6040 : }
6041 :
6042 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
6043 945700 : arg = c_fully_fold (arg, false, NULL, true);
6044 :
6045 945700 : bool atomic_op;
6046 945700 : atomic_op = really_atomic_lvalue (arg);
6047 :
6048 : /* Increment or decrement the real part of the value,
6049 : and don't change the imaginary part. */
6050 945700 : if (typecode == COMPLEX_TYPE)
6051 : {
6052 57 : tree real, imag;
6053 :
6054 57 : pedwarn_c23 (location, OPT_Wpedantic,
6055 : "ISO C does not support %<++%> and %<--%> on complex "
6056 : "types before C2Y");
6057 :
6058 57 : if (!atomic_op)
6059 : {
6060 53 : arg = stabilize_reference (arg);
6061 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6062 : true);
6063 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6064 : true);
6065 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6066 53 : if (real == error_mark_node || imag == error_mark_node)
6067 : return error_mark_node;
6068 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6069 : real, imag);
6070 53 : goto return_build_unary_op;
6071 : }
6072 : }
6073 :
6074 : /* Report invalid types. */
6075 :
6076 945647 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6077 712618 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6078 208 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6079 945703 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6080 : {
6081 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6082 3 : error_at (location, "wrong type argument to increment");
6083 : else
6084 2 : error_at (location, "wrong type argument to decrement");
6085 :
6086 5 : return error_mark_node;
6087 : }
6088 :
6089 945642 : {
6090 945642 : tree inc;
6091 :
6092 945642 : argtype = TREE_TYPE (arg);
6093 :
6094 : /* Compute the increment. */
6095 :
6096 945642 : if (typecode == POINTER_TYPE)
6097 : {
6098 : /* If pointer target is an incomplete type,
6099 : we just cannot know how to do the arithmetic. */
6100 233029 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6101 : {
6102 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6103 17 : error_at (location,
6104 : "increment of pointer to an incomplete type %qT",
6105 17 : TREE_TYPE (argtype));
6106 : else
6107 10 : error_at (location,
6108 : "decrement of pointer to an incomplete type %qT",
6109 10 : TREE_TYPE (argtype));
6110 : }
6111 233002 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6112 233002 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6113 : {
6114 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6115 6 : pedwarn (location, OPT_Wpointer_arith,
6116 : "wrong type argument to increment");
6117 : else
6118 4 : pedwarn (location, OPT_Wpointer_arith,
6119 : "wrong type argument to decrement");
6120 : }
6121 : else
6122 465984 : verify_type_context (location, TCTX_POINTER_ARITH,
6123 232992 : TREE_TYPE (argtype));
6124 :
6125 233029 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6126 233029 : inc = convert_to_ptrofftype_loc (location, inc);
6127 : }
6128 712613 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6129 : {
6130 : /* For signed fract types, we invert ++ to -- or
6131 : -- to ++, and change inc from 1 to -1, because
6132 : it is not possible to represent 1 in signed fract constants.
6133 : For unsigned fract types, the result always overflows and
6134 : we get an undefined (original) or the maximum value. */
6135 0 : if (code == PREINCREMENT_EXPR)
6136 : code = PREDECREMENT_EXPR;
6137 : else if (code == PREDECREMENT_EXPR)
6138 : code = PREINCREMENT_EXPR;
6139 : else if (code == POSTINCREMENT_EXPR)
6140 : code = POSTDECREMENT_EXPR;
6141 : else /* code == POSTDECREMENT_EXPR */
6142 0 : code = POSTINCREMENT_EXPR;
6143 :
6144 0 : inc = integer_minus_one_node;
6145 0 : inc = convert (argtype, inc);
6146 : }
6147 : else
6148 : {
6149 712562 : inc = VECTOR_TYPE_P (argtype)
6150 712613 : ? build_one_cst (argtype)
6151 : : integer_one_node;
6152 712613 : inc = convert (argtype, inc);
6153 : }
6154 :
6155 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6156 : need to ask Objective-C to build the increment or decrement
6157 : expression for it. */
6158 945642 : if (objc_is_property_ref (arg))
6159 0 : return objc_build_incr_expr_for_property_ref (location, code,
6160 20 : arg, inc);
6161 :
6162 : /* Report a read-only lvalue. */
6163 945642 : if (TYPE_READONLY (argtype))
6164 : {
6165 20 : readonly_error (location, arg,
6166 20 : ((code == PREINCREMENT_EXPR
6167 20 : || code == POSTINCREMENT_EXPR)
6168 : ? lv_increment : lv_decrement));
6169 20 : return error_mark_node;
6170 : }
6171 945622 : else if (TREE_READONLY (arg))
6172 4 : readonly_warning (arg,
6173 4 : ((code == PREINCREMENT_EXPR
6174 4 : || code == POSTINCREMENT_EXPR)
6175 : ? lv_increment : lv_decrement));
6176 :
6177 : /* If the argument is atomic, use the special code sequences for
6178 : atomic compound assignment. */
6179 945622 : if (atomic_op)
6180 : {
6181 4469 : arg = stabilize_reference (arg);
6182 8938 : ret = build_atomic_assign (location, arg,
6183 4469 : ((code == PREINCREMENT_EXPR
6184 4469 : || code == POSTINCREMENT_EXPR)
6185 : ? PLUS_EXPR
6186 : : MINUS_EXPR),
6187 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6188 : ? inc
6189 : : integer_one_node),
6190 : (code == POSTINCREMENT_EXPR
6191 4469 : || code == POSTDECREMENT_EXPR));
6192 945622 : goto return_build_unary_op;
6193 : }
6194 :
6195 941153 : tree true_res;
6196 941153 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6197 : {
6198 364 : tree larg = stabilize_reference (arg);
6199 364 : tree sarg = save_expr (larg);
6200 364 : switch (code)
6201 : {
6202 91 : case PREINCREMENT_EXPR:
6203 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6204 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6205 91 : break;
6206 91 : case POSTINCREMENT_EXPR:
6207 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6208 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6209 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6210 91 : break;
6211 91 : case PREDECREMENT_EXPR:
6212 91 : {
6213 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6214 : true, true);
6215 91 : rarg = invert_truthvalue_loc (location, rarg);
6216 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6217 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6218 : }
6219 91 : break;
6220 91 : case POSTDECREMENT_EXPR:
6221 91 : {
6222 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6223 : true, true);
6224 91 : rarg = invert_truthvalue_loc (location, rarg);
6225 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6226 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6227 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6228 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6229 : }
6230 91 : break;
6231 : default:
6232 : gcc_unreachable ();
6233 : }
6234 364 : TREE_SIDE_EFFECTS (val) = 1;
6235 : }
6236 940789 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6237 64 : val = boolean_increment (code, arg);
6238 : else
6239 940725 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6240 941153 : TREE_SIDE_EFFECTS (val) = 1;
6241 941153 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6242 3940 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6243 : TYPE_UNQUALIFIED);
6244 941153 : ret = val;
6245 941153 : goto return_build_unary_op;
6246 : }
6247 :
6248 53576450 : case ADDR_EXPR:
6249 : /* Note that this operation never does default_conversion. */
6250 :
6251 : /* The operand of unary '&' must be an lvalue (which excludes
6252 : expressions of type void), or, in C99, the result of a [] or
6253 : unary '*' operator. */
6254 53576450 : if (VOID_TYPE_P (TREE_TYPE (arg))
6255 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6256 53576469 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6257 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6258 :
6259 : /* Let &* cancel out to simplify resulting code. */
6260 53576450 : if (INDIRECT_REF_P (arg))
6261 : {
6262 : /* Don't let this be an lvalue. */
6263 22446 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6264 16890 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6265 5556 : ret = TREE_OPERAND (arg, 0);
6266 5556 : goto return_build_unary_op;
6267 : }
6268 :
6269 : /* Anything not already handled and not a true memory reference
6270 : or a non-lvalue array is an error. */
6271 53554004 : if (typecode != FUNCTION_TYPE && !noconvert
6272 53554004 : && !lvalue_or_else (location, arg, lv_addressof))
6273 17 : return error_mark_node;
6274 :
6275 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6276 : folding later. */
6277 53553987 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6278 : {
6279 14 : tree inner = build_unary_op (location, code,
6280 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6281 : noconvert);
6282 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6283 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6284 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6285 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6286 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6287 14 : goto return_build_unary_op;
6288 : }
6289 :
6290 : /* Ordinary case; arg is a COMPONENT_REF or a decl. */
6291 :
6292 53553973 : argtype = TREE_TYPE (arg);
6293 :
6294 : /* If the lvalue is const or volatile, merge that into the type
6295 : to which the address will point. This is only needed
6296 : for function types. */
6297 53553973 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6298 53061057 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6299 84622130 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6300 : {
6301 30987970 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6302 30987970 : int quals = orig_quals;
6303 :
6304 30987970 : if (TREE_READONLY (arg))
6305 30606016 : quals |= TYPE_QUAL_CONST;
6306 30987970 : if (TREE_THIS_VOLATILE (arg))
6307 382893 : quals |= TYPE_QUAL_VOLATILE;
6308 :
6309 30987970 : argtype = c_build_qualified_type (argtype, quals);
6310 : }
6311 :
6312 53553973 : switch (TREE_CODE (arg))
6313 : {
6314 65760 : case COMPONENT_REF:
6315 65760 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6316 : {
6317 8 : error_at (location, "cannot take address of bit-field %qD",
6318 8 : TREE_OPERAND (arg, 1));
6319 8 : return error_mark_node;
6320 : }
6321 :
6322 : /* fall through */
6323 :
6324 141559 : case ARRAY_REF:
6325 141559 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6326 : {
6327 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6328 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6329 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6330 : {
6331 6 : error_at (location, "cannot take address of scalar with "
6332 : "reverse storage order");
6333 6 : return error_mark_node;
6334 : }
6335 :
6336 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6337 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6338 58 : warning_at (location, OPT_Wscalar_storage_order,
6339 : "address of array with reverse scalar storage "
6340 : "order requested");
6341 : }
6342 :
6343 53553959 : default:
6344 53553959 : break;
6345 : }
6346 :
6347 53553959 : if (!c_mark_addressable (arg))
6348 20 : return error_mark_node;
6349 :
6350 53553939 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6351 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6352 :
6353 53553939 : argtype = c_build_pointer_type (argtype);
6354 :
6355 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6356 : when we have proper support for integer constant expressions. */
6357 53553939 : val = get_base_address (arg);
6358 53553939 : if (val && INDIRECT_REF_P (val)
6359 53578130 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6360 : {
6361 540 : ret = fold_offsetof (arg, argtype);
6362 540 : goto return_build_unary_op;
6363 : }
6364 :
6365 53553399 : val = build1 (ADDR_EXPR, argtype, arg);
6366 :
6367 53553399 : ret = val;
6368 53553399 : goto return_build_unary_op;
6369 :
6370 10 : case PAREN_EXPR:
6371 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6372 10 : goto return_build_unary_op;
6373 :
6374 0 : default:
6375 0 : gcc_unreachable ();
6376 : }
6377 :
6378 7301865 : if (argtype == NULL_TREE)
6379 7301865 : argtype = TREE_TYPE (arg);
6380 7301865 : if (TREE_CODE (arg) == INTEGER_CST)
6381 6594476 : ret = (require_constant_value
6382 6594476 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6383 6575779 : : fold_build1_loc (location, code, argtype, arg));
6384 : else
6385 707389 : ret = build1 (code, argtype, arg);
6386 62388881 : return_build_unary_op:
6387 62388881 : gcc_assert (ret != error_mark_node);
6388 6739170 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6389 69128042 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6390 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6391 62388319 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6392 74 : ret = note_integer_operands (ret);
6393 62388881 : if (eptype)
6394 245 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6395 62388881 : protected_set_expr_location (ret, location);
6396 62388881 : return ret;
6397 : }
6398 :
6399 : /* Return nonzero if REF is an lvalue valid for this language.
6400 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6401 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6402 :
6403 : bool
6404 183062952 : lvalue_p (const_tree ref)
6405 : {
6406 184572655 : const enum tree_code code = TREE_CODE (ref);
6407 :
6408 184572655 : switch (code)
6409 : {
6410 1484726 : case REALPART_EXPR:
6411 1484726 : case IMAGPART_EXPR:
6412 1484726 : case COMPONENT_REF:
6413 1484726 : return lvalue_p (TREE_OPERAND (ref, 0));
6414 :
6415 24977 : case C_MAYBE_CONST_EXPR:
6416 24977 : return lvalue_p (TREE_OPERAND (ref, 1));
6417 :
6418 : case COMPOUND_LITERAL_EXPR:
6419 : case STRING_CST:
6420 : return true;
6421 :
6422 25721671 : case MEM_REF:
6423 25721671 : case TARGET_MEM_REF:
6424 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6425 : here as well. */
6426 25721671 : case INDIRECT_REF:
6427 25721671 : case ARRAY_REF:
6428 25721671 : case VAR_DECL:
6429 25721671 : case PARM_DECL:
6430 25721671 : case RESULT_DECL:
6431 25721671 : case ERROR_MARK:
6432 25721671 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6433 25721671 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6434 :
6435 6 : case BIND_EXPR:
6436 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6437 :
6438 7239803 : case CALL_EXPR:
6439 7239803 : return is_access_with_size_p (ref);
6440 :
6441 : default:
6442 : return false;
6443 : }
6444 : }
6445 :
6446 : /* Give a warning for storing in something that is read-only in GCC
6447 : terms but not const in ISO C terms. */
6448 :
6449 : static void
6450 5 : readonly_warning (tree arg, enum lvalue_use use)
6451 : {
6452 5 : switch (use)
6453 : {
6454 1 : case lv_assign:
6455 1 : warning (0, "assignment of read-only location %qE", arg);
6456 1 : break;
6457 2 : case lv_increment:
6458 2 : warning (0, "increment of read-only location %qE", arg);
6459 2 : break;
6460 2 : case lv_decrement:
6461 2 : warning (0, "decrement of read-only location %qE", arg);
6462 2 : break;
6463 0 : default:
6464 0 : gcc_unreachable ();
6465 : }
6466 5 : return;
6467 : }
6468 :
6469 :
6470 : /* Return nonzero if REF is an lvalue valid for this language;
6471 : otherwise, print an error message and return zero. USE says
6472 : how the lvalue is being used and so selects the error message.
6473 : LOCATION is the location at which any error should be reported. */
6474 :
6475 : static int
6476 4759099 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6477 : {
6478 4759099 : int win = lvalue_p (ref);
6479 :
6480 4759099 : if (!win)
6481 75 : lvalue_error (loc, use);
6482 :
6483 4759099 : return win;
6484 : }
6485 :
6486 : /* Mark EXP saying that we need to be able to take the
6487 : address of it; it should not be allocated in a register.
6488 : Returns true if successful. ARRAY_REF_P is true if this
6489 : is for ARRAY_REF construction - in that case we don't want
6490 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6491 : it is fine to use ARRAY_REFs for vector subscripts on vector
6492 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6493 : than producing an error for taking the address of a register. */
6494 :
6495 : bool
6496 54105322 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6497 : {
6498 54105322 : tree x = exp;
6499 :
6500 54687931 : while (1)
6501 54687931 : switch (TREE_CODE (x))
6502 : {
6503 10103 : case VIEW_CONVERT_EXPR:
6504 10103 : if (array_ref_p
6505 9993 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6506 20096 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6507 : return true;
6508 110 : x = TREE_OPERAND (x, 0);
6509 110 : break;
6510 :
6511 396736 : case COMPONENT_REF:
6512 396736 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6513 : {
6514 6 : error ("cannot take address of bit-field %qD",
6515 3 : TREE_OPERAND (x, 1));
6516 3 : return false;
6517 : }
6518 : /* FALLTHRU */
6519 582499 : case ADDR_EXPR:
6520 582499 : case ARRAY_REF:
6521 582499 : case REALPART_EXPR:
6522 582499 : case IMAGPART_EXPR:
6523 582499 : x = TREE_OPERAND (x, 0);
6524 582499 : break;
6525 :
6526 594 : case COMPOUND_LITERAL_EXPR:
6527 594 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6528 : {
6529 16 : if (override_register)
6530 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6531 : else
6532 : {
6533 4 : error ("address of register compound literal requested");
6534 4 : return false;
6535 : }
6536 : }
6537 590 : TREE_ADDRESSABLE (x) = 1;
6538 590 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6539 590 : return true;
6540 :
6541 0 : case CONSTRUCTOR:
6542 0 : TREE_ADDRESSABLE (x) = 1;
6543 0 : return true;
6544 :
6545 1335447 : case VAR_DECL:
6546 1335447 : case CONST_DECL:
6547 1335447 : case PARM_DECL:
6548 1335447 : case RESULT_DECL:
6549 1335447 : if (C_DECL_REGISTER (x)
6550 1335447 : && DECL_NONLOCAL (x))
6551 : {
6552 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6553 : {
6554 0 : error
6555 0 : ("global register variable %qD used in nested function", x);
6556 0 : return false;
6557 : }
6558 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6559 : }
6560 1335447 : else if (C_DECL_REGISTER (x))
6561 : {
6562 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6563 4 : error ("address of global register variable %qD requested", x);
6564 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6565 : {
6566 54 : DECL_REGISTER (x) = 0;
6567 54 : TREE_ADDRESSABLE (x) = 1;
6568 54 : mark_decl_used (x, true);
6569 54 : return true;
6570 : }
6571 : else
6572 15 : error ("address of register variable %qD requested", x);
6573 : return false;
6574 : }
6575 :
6576 : /* FALLTHRU */
6577 53526377 : case FUNCTION_DECL:
6578 53526377 : TREE_ADDRESSABLE (x) = 1;
6579 53526377 : mark_decl_used (x, true);
6580 : /* FALLTHRU */
6581 : default:
6582 : return true;
6583 : }
6584 : }
6585 :
6586 : /* Convert EXPR to TYPE, warning about conversion problems with
6587 : constants. SEMANTIC_TYPE is the type this conversion would use
6588 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6589 : is equivalent to convert_and_check. This function is a wrapper that
6590 : handles conversions that may be different than
6591 : the usual ones because of excess precision. */
6592 :
6593 : static tree
6594 23780296 : ep_convert_and_check (location_t loc, tree type, tree expr,
6595 : tree semantic_type)
6596 : {
6597 23780296 : if (TREE_TYPE (expr) == type)
6598 : return expr;
6599 :
6600 : /* For C11, integer conversions may have results with excess
6601 : precision. */
6602 2068429 : if (flag_isoc11 || !semantic_type)
6603 2068425 : return convert_and_check (loc, type, expr);
6604 :
6605 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6606 4 : && TREE_TYPE (expr) != semantic_type)
6607 : {
6608 : /* For integers, we need to check the real conversion, not
6609 : the conversion to the excess precision type. */
6610 4 : expr = convert_and_check (loc, semantic_type, expr);
6611 : }
6612 : /* Result type is the excess precision type, which should be
6613 : large enough, so do not check. */
6614 4 : return convert (type, expr);
6615 : }
6616 :
6617 : /* If EXPR refers to a built-in declared without a prototype returns
6618 : the actual type of the built-in and, if non-null, set *BLTIN to
6619 : a pointer to the built-in. Otherwise return the type of EXPR
6620 : and clear *BLTIN if non-null. */
6621 :
6622 : static tree
6623 2838860 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6624 : {
6625 2838860 : tree dummy;
6626 2838860 : if (!bltin)
6627 0 : bltin = &dummy;
6628 :
6629 2838860 : *bltin = NULL_TREE;
6630 :
6631 2838860 : tree type = TREE_TYPE (expr);
6632 2838860 : if (TREE_CODE (expr) != ADDR_EXPR)
6633 : return type;
6634 :
6635 209672 : tree oper = TREE_OPERAND (expr, 0);
6636 209672 : if (!DECL_P (oper)
6637 171005 : || TREE_CODE (oper) != FUNCTION_DECL
6638 246104 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6639 : return type;
6640 :
6641 2085 : built_in_function code = DECL_FUNCTION_CODE (oper);
6642 2085 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6643 : return type;
6644 :
6645 1977 : if ((*bltin = builtin_decl_implicit (code)))
6646 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6647 :
6648 : return type;
6649 : }
6650 :
6651 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6652 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6653 : if folded to an integer constant then the unselected half may
6654 : contain arbitrary operations not normally permitted in constant
6655 : expressions. Set the location of the expression to LOC. */
6656 :
6657 : tree
6658 404689 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6659 : tree op1, tree op1_original_type, location_t op1_loc,
6660 : tree op2, tree op2_original_type, location_t op2_loc)
6661 : {
6662 404689 : tree type1;
6663 404689 : tree type2;
6664 404689 : tree result_type = NULL;
6665 404689 : tree semantic_result_type = NULL;
6666 404689 : tree orig_op1 = op1, orig_op2 = op2;
6667 404689 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6668 404689 : bool ifexp_int_operands;
6669 404689 : tree ret;
6670 :
6671 404689 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6672 : if (op1_int_operands)
6673 110588 : op1 = remove_c_maybe_const_expr (op1);
6674 404689 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6675 : if (op2_int_operands)
6676 145756 : op2 = remove_c_maybe_const_expr (op2);
6677 404689 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6678 : if (ifexp_int_operands)
6679 242703 : ifexp = remove_c_maybe_const_expr (ifexp);
6680 :
6681 : /* Promote both alternatives. */
6682 :
6683 404689 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6684 400766 : op1 = default_conversion (op1);
6685 404689 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6686 374843 : op2 = default_conversion (op2);
6687 :
6688 404689 : if (TREE_CODE (ifexp) == ERROR_MARK
6689 404620 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6690 809295 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6691 83 : return error_mark_node;
6692 :
6693 404606 : tree bltin1 = NULL_TREE;
6694 404606 : tree bltin2 = NULL_TREE;
6695 404606 : type1 = type_or_builtin_type (op1, &bltin1);
6696 404606 : const enum tree_code code1 = TREE_CODE (type1);
6697 404606 : type2 = type_or_builtin_type (op2, &bltin2);
6698 404606 : const enum tree_code code2 = TREE_CODE (type2);
6699 :
6700 404606 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6701 1 : return error_mark_node;
6702 :
6703 404605 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6704 1 : return error_mark_node;
6705 :
6706 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6707 : In C99 they will be pointers by now. */
6708 404604 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6709 : {
6710 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6711 1 : return error_mark_node;
6712 : }
6713 :
6714 404603 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6715 404597 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6716 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6717 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6718 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6719 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6720 : {
6721 7 : semantic_result_type = c_common_type (type1, type2);
6722 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6723 : {
6724 6 : op1 = TREE_OPERAND (op1, 0);
6725 6 : type1 = TREE_TYPE (op1);
6726 6 : gcc_assert (TREE_CODE (type1) == code1);
6727 : }
6728 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6729 : {
6730 1 : op2 = TREE_OPERAND (op2, 0);
6731 1 : type2 = TREE_TYPE (op2);
6732 1 : gcc_assert (TREE_CODE (type2) == code2);
6733 : }
6734 : }
6735 :
6736 404603 : if (warn_cxx_compat)
6737 : {
6738 420 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6739 420 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6740 :
6741 420 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6742 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6743 426 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6744 2 : warning_at (colon_loc, OPT_Wc___compat,
6745 : "different enum types in conditional is "
6746 : "invalid in C++: %qT vs %qT", t1, t2);
6747 : }
6748 :
6749 404603 : if (warn_zero_as_null_pointer_constant
6750 20 : && c_inhibit_evaluation_warnings == 0)
6751 : {
6752 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6753 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6754 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6755 : "zero as null pointer constant");
6756 :
6757 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6758 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6759 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6760 : "zero as null pointer constant");
6761 : }
6762 :
6763 : /* Quickly detect the usual case where op1 and op2 have the same type
6764 : after promotion. */
6765 404603 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6766 : {
6767 269451 : if (type1 == type2)
6768 : result_type = type1;
6769 : else
6770 6412 : result_type = TYPE_MAIN_VARIANT (type1);
6771 : }
6772 135152 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6773 80467 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6774 54734 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6775 26152 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6776 : {
6777 : /* In C11, a conditional expression between a floating-point
6778 : type and an integer type should convert the integer type to
6779 : the evaluation format of the floating-point type, with
6780 : possible excess precision. */
6781 28682 : tree eptype1 = type1;
6782 28682 : tree eptype2 = type2;
6783 28682 : if (flag_isoc11)
6784 : {
6785 28369 : tree eptype;
6786 11644 : if (ANY_INTEGRAL_TYPE_P (type1)
6787 28369 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6788 : {
6789 2 : eptype2 = eptype;
6790 2 : if (!semantic_result_type)
6791 2 : semantic_result_type = c_common_type (type1, type2);
6792 : }
6793 502 : else if (ANY_INTEGRAL_TYPE_P (type2)
6794 28367 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6795 : {
6796 4626 : eptype1 = eptype;
6797 4626 : if (!semantic_result_type)
6798 4626 : semantic_result_type = c_common_type (type1, type2);
6799 : }
6800 : }
6801 28682 : result_type = c_common_type (eptype1, eptype2);
6802 28682 : if (result_type == error_mark_node)
6803 : return error_mark_node;
6804 28680 : do_warn_double_promotion (result_type, type1, type2,
6805 : "implicit conversion from %qT to %qT to "
6806 : "match other result of conditional",
6807 : colon_loc);
6808 :
6809 : /* If -Wsign-compare, warn here if type1 and type2 have
6810 : different signedness. We'll promote the signed to unsigned
6811 : and later code won't know it used to be different.
6812 : Do this check on the original types, so that explicit casts
6813 : will be considered, but default promotions won't. */
6814 28680 : if (c_inhibit_evaluation_warnings == 0)
6815 : {
6816 28489 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6817 28489 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6818 :
6819 28489 : if (unsigned_op1 ^ unsigned_op2)
6820 : {
6821 : /* Do not warn if the result type is signed, since the
6822 : signed type will only be chosen if it can represent
6823 : all the values of the unsigned type. */
6824 13041 : if (!TYPE_UNSIGNED (result_type))
6825 : /* OK */;
6826 : else
6827 : {
6828 12937 : bool op1_maybe_const = true;
6829 12937 : bool op2_maybe_const = true;
6830 :
6831 : /* Do not warn if the signed quantity is an
6832 : unsuffixed integer literal (or some static
6833 : constant expression involving such literals) and
6834 : it is non-negative. This warning requires the
6835 : operands to be folded for best results, so do
6836 : that folding in this case even without
6837 : warn_sign_compare to avoid warning options
6838 : possibly affecting code generation. */
6839 12937 : c_inhibit_evaluation_warnings
6840 12937 : += (ifexp == truthvalue_false_node);
6841 12937 : op1 = c_fully_fold (op1, require_constant_value,
6842 : &op1_maybe_const);
6843 12937 : c_inhibit_evaluation_warnings
6844 12937 : -= (ifexp == truthvalue_false_node);
6845 :
6846 12937 : c_inhibit_evaluation_warnings
6847 12937 : += (ifexp == truthvalue_true_node);
6848 12937 : op2 = c_fully_fold (op2, require_constant_value,
6849 : &op2_maybe_const);
6850 12937 : c_inhibit_evaluation_warnings
6851 12937 : -= (ifexp == truthvalue_true_node);
6852 :
6853 12937 : if (warn_sign_compare)
6854 : {
6855 1245 : if ((unsigned_op2
6856 685 : && tree_expr_nonnegative_p (op1))
6857 1248 : || (unsigned_op1
6858 560 : && tree_expr_nonnegative_p (op2)))
6859 : /* OK */;
6860 10 : else if (unsigned_op2)
6861 3 : warning_at (op1_loc, OPT_Wsign_compare,
6862 : "operand of %<?:%> changes signedness from "
6863 : "%qT to %qT due to unsignedness of other "
6864 3 : "operand", TREE_TYPE (orig_op1),
6865 3 : TREE_TYPE (orig_op2));
6866 : else
6867 7 : warning_at (op2_loc, OPT_Wsign_compare,
6868 : "operand of %<?:%> changes signedness from "
6869 : "%qT to %qT due to unsignedness of other "
6870 7 : "operand", TREE_TYPE (orig_op2),
6871 7 : TREE_TYPE (orig_op1));
6872 : }
6873 12937 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6874 7577 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6875 12937 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6876 4397 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6877 : }
6878 : }
6879 : }
6880 : }
6881 106470 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6882 : {
6883 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6884 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6885 : "ISO C forbids conditional expr with only one void side");
6886 25995 : result_type = void_type_node;
6887 : }
6888 80475 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6889 : {
6890 80057 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6891 80057 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6892 80057 : addr_space_t as_common;
6893 :
6894 80057 : if (comp_target_types (colon_loc, type1, type2))
6895 : {
6896 2622 : ifexp = save_expr (ifexp);
6897 2622 : result_type = common_pointer_type (type1, type2, ifexp);
6898 : }
6899 77435 : else if (null_pointer_constant_p (orig_op1))
6900 : result_type = type2;
6901 73273 : else if (null_pointer_constant_p (orig_op2))
6902 : result_type = type1;
6903 35281 : else if (!addr_space_superset (as1, as2, &as_common))
6904 : {
6905 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6906 : "used in conditional expression");
6907 0 : return error_mark_node;
6908 : }
6909 35281 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6910 326 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6911 35283 : || (VOID_TYPE_P (TREE_TYPE (type2))
6912 34910 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6913 : {
6914 35233 : tree t1 = TREE_TYPE (type1);
6915 35233 : tree t2 = TREE_TYPE (type2);
6916 35233 : if (!(VOID_TYPE_P (t1)
6917 325 : && !TYPE_ATOMIC (t1)))
6918 : {
6919 : /* roles are swapped */
6920 34909 : t1 = t2;
6921 34909 : t2 = TREE_TYPE (type1);
6922 : }
6923 35233 : tree t2_stripped = strip_array_types (t2);
6924 35233 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6925 35233 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6926 : {
6927 18 : if (!flag_isoc23)
6928 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6929 : "pointer to array loses qualifier "
6930 : "in conditional expression");
6931 12 : else if (warn_c11_c23_compat > 0)
6932 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6933 : "pointer to array loses qualifier "
6934 : "in conditional expression in ISO C before C23");
6935 : }
6936 35233 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6937 4 : pedwarn (colon_loc, OPT_Wpedantic,
6938 : "ISO C forbids conditional expr between "
6939 : "%<void *%> and function pointer");
6940 : /* for array, use qualifiers of element type */
6941 35233 : if (flag_isoc23)
6942 11452 : t2 = t2_stripped;
6943 35233 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6944 : }
6945 : /* Objective-C pointer comparisons are a bit more lenient. */
6946 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6947 0 : result_type = objc_common_type (type1, type2);
6948 : else
6949 : {
6950 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6951 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6952 48 : if (!flag_isoc99)
6953 : /* This downgrade to a warning ensures that -std=gnu89
6954 : -pedantic-errors does not flag these mismatches between
6955 : builtins as errors (as diagnostics::kind::permerror would)
6956 : ISO C99 and later do not have implicit function declarations,
6957 : so the mismatch cannot occur naturally there. */
6958 5 : kind = (bltin1 && bltin2
6959 5 : ? diagnostics::kind::warning
6960 : : diagnostics::kind::pedwarn);
6961 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6962 : "pointer type mismatch "
6963 : "in conditional expression"))
6964 : {
6965 46 : inform (op1_loc, "first expression has type %qT", type1);
6966 46 : inform (op2_loc, "second expression has type %qT", type2);
6967 : }
6968 48 : result_type = c_build_pointer_type
6969 48 : (c_build_qualified_type (void_type_node, qual));
6970 : }
6971 : }
6972 418 : else if (code1 == POINTER_TYPE
6973 298 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6974 : {
6975 294 : if (!null_pointer_constant_p (orig_op2))
6976 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6977 : "pointer/integer type mismatch "
6978 : "in conditional expression");
6979 : else
6980 : {
6981 280 : op2 = null_pointer_node;
6982 : }
6983 : result_type = type1;
6984 : }
6985 124 : else if (code2 == POINTER_TYPE
6986 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6987 : {
6988 86 : if (!null_pointer_constant_p (orig_op1))
6989 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6990 : "pointer/integer type mismatch "
6991 : "in conditional expression");
6992 : else
6993 : {
6994 72 : op1 = null_pointer_node;
6995 : }
6996 : result_type = type2;
6997 : }
6998 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6999 : type nullptr_t and the other is a pointer, the result type is the pointer
7000 : type." */
7001 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
7002 : result_type = type2;
7003 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
7004 : result_type = type1;
7005 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
7006 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
7007 8 : TYPE_MAIN_VARIANT (type2)))
7008 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
7009 8 : TYPE_MAIN_VARIANT (type2));
7010 :
7011 404579 : if (!result_type)
7012 : {
7013 22 : if (flag_cond_mismatch)
7014 0 : result_type = void_type_node;
7015 : else
7016 : {
7017 22 : error_at (colon_loc, "type mismatch in conditional expression");
7018 22 : return error_mark_node;
7019 : }
7020 : }
7021 :
7022 : /* Merge const and volatile flags of the incoming types. */
7023 404579 : result_type
7024 404579 : = build_type_variant (result_type,
7025 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
7026 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
7027 :
7028 404579 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
7029 : semantic_result_type);
7030 404579 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
7031 : semantic_result_type);
7032 :
7033 404579 : if (ifexp_bcp && ifexp == truthvalue_true_node)
7034 : {
7035 10 : op2_int_operands = true;
7036 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
7037 : }
7038 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
7039 : {
7040 49 : op1_int_operands = true;
7041 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
7042 : }
7043 906231 : int_const = int_operands = (ifexp_int_operands
7044 404579 : && op1_int_operands
7045 404579 : && op2_int_operands);
7046 97073 : if (int_operands)
7047 : {
7048 97073 : int_const = ((ifexp == truthvalue_true_node
7049 58781 : && TREE_CODE (orig_op1) == INTEGER_CST
7050 58767 : && !TREE_OVERFLOW (orig_op1))
7051 97087 : || (ifexp == truthvalue_false_node
7052 38246 : && TREE_CODE (orig_op2) == INTEGER_CST
7053 38230 : && !TREE_OVERFLOW (orig_op2)));
7054 : }
7055 :
7056 : /* Need to convert condition operand into a vector mask. */
7057 404579 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7058 : {
7059 0 : tree vectype = TREE_TYPE (ifexp);
7060 0 : tree elem_type = TREE_TYPE (vectype);
7061 0 : tree zero = build_int_cst (elem_type, 0);
7062 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7063 0 : tree cmp_type = truth_type_for (vectype);
7064 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7065 : }
7066 :
7067 404579 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7068 97029 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7069 : else
7070 : {
7071 307550 : if (int_operands)
7072 : {
7073 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7074 : nested inside of the expression. */
7075 76 : op1 = c_fully_fold (op1, false, NULL);
7076 76 : op2 = c_fully_fold (op2, false, NULL);
7077 : }
7078 307550 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7079 307550 : if (int_operands)
7080 76 : ret = note_integer_operands (ret);
7081 : }
7082 404579 : if (semantic_result_type)
7083 4635 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7084 :
7085 404579 : protected_set_expr_location (ret, colon_loc);
7086 :
7087 : /* If the OP1 and OP2 are the same and don't have side-effects,
7088 : warn here, because the COND_EXPR will be turned into OP1. */
7089 404579 : if (warn_duplicated_branches
7090 34 : && TREE_CODE (ret) == COND_EXPR
7091 404613 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7092 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7093 : "this condition has identical branches");
7094 :
7095 : return ret;
7096 : }
7097 :
7098 : /* EXPR is an expression, location LOC, whose result is discarded.
7099 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7100 : whose right-hand operand is such a call, possibly recursively). */
7101 :
7102 : static void
7103 6545663 : maybe_warn_nodiscard (location_t loc, tree expr)
7104 : {
7105 6545663 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7106 : return;
7107 4000830 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7108 : {
7109 51952 : expr = TREE_OPERAND (expr, 1);
7110 51952 : if (EXPR_HAS_LOCATION (expr))
7111 51952 : loc = EXPR_LOCATION (expr);
7112 : }
7113 3948878 : if (TREE_CODE (expr) != CALL_EXPR)
7114 : return;
7115 347486 : tree fn = CALL_EXPR_FN (expr);
7116 347486 : if (!fn)
7117 : return;
7118 347470 : tree attr;
7119 347470 : if (TREE_CODE (fn) == ADDR_EXPR
7120 346803 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7121 694273 : && (attr = lookup_attribute ("nodiscard",
7122 346803 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7123 : {
7124 3 : fn = TREE_OPERAND (fn, 0);
7125 3 : tree args = TREE_VALUE (attr);
7126 3 : if (args)
7127 1 : args = TREE_VALUE (args);
7128 3 : auto_diagnostic_group d;
7129 3 : auto_urlify_attributes sentinel;
7130 3 : int warned;
7131 3 : if (args)
7132 1 : warned = warning_at (loc, OPT_Wunused_result,
7133 : "ignoring return value of %qD, declared with "
7134 : "attribute %<nodiscard%>: %E", fn, args);
7135 : else
7136 2 : warned = warning_at (loc, OPT_Wunused_result,
7137 : "ignoring return value of %qD, declared with "
7138 : "attribute %<nodiscard%>", fn);
7139 3 : if (warned)
7140 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7141 3 : }
7142 : else
7143 : {
7144 347467 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7145 347467 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7146 347467 : if (!attr)
7147 347455 : return;
7148 12 : tree args = TREE_VALUE (attr);
7149 12 : if (args)
7150 5 : args = TREE_VALUE (args);
7151 12 : auto_diagnostic_group d;
7152 12 : auto_urlify_attributes sentinel;
7153 12 : int warned;
7154 12 : if (args)
7155 5 : warned = warning_at (loc, OPT_Wunused_result,
7156 : "ignoring return value of type %qT, declared "
7157 : "with attribute %<nodiscard%>: %E",
7158 : rettype, args);
7159 : else
7160 7 : warned = warning_at (loc, OPT_Wunused_result,
7161 : "ignoring return value of type %qT, declared "
7162 : "with attribute %<nodiscard%>", rettype);
7163 12 : if (warned)
7164 : {
7165 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7166 : {
7167 11 : fn = TREE_OPERAND (fn, 0);
7168 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7169 11 : inform (DECL_SOURCE_LOCATION (fn),
7170 : "in call to %qD, declared here", fn);
7171 : }
7172 : }
7173 12 : }
7174 : }
7175 :
7176 : /* Return a compound expression that performs two expressions and
7177 : returns the value of the second of them.
7178 :
7179 : LOC is the location of the COMPOUND_EXPR. */
7180 :
7181 : tree
7182 114233 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7183 : {
7184 114233 : bool expr1_int_operands, expr2_int_operands;
7185 114233 : tree eptype = NULL_TREE;
7186 114233 : tree ret;
7187 :
7188 114233 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7189 : if (expr1_int_operands)
7190 326 : expr1 = remove_c_maybe_const_expr (expr1);
7191 114233 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7192 : if (expr2_int_operands)
7193 66136 : expr2 = remove_c_maybe_const_expr (expr2);
7194 :
7195 114233 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7196 0 : expr1 = TREE_OPERAND (expr1, 0);
7197 114233 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7198 : {
7199 1 : eptype = TREE_TYPE (expr2);
7200 1 : expr2 = TREE_OPERAND (expr2, 0);
7201 : }
7202 :
7203 114233 : if (!TREE_SIDE_EFFECTS (expr1))
7204 : {
7205 : /* The left-hand operand of a comma expression is like an expression
7206 : statement: with -Wunused, we should warn if it doesn't have
7207 : any side-effects, unless it was explicitly cast to (void). */
7208 8433 : if (warn_unused_value)
7209 : {
7210 1284 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7211 1284 : && CONVERT_EXPR_P (expr1))
7212 : ; /* (void) a, b */
7213 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7214 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7215 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7216 : ; /* (void) a, (void) b, c */
7217 : else
7218 11 : warning_at (loc, OPT_Wunused_value,
7219 : "left-hand operand of comma expression has no effect");
7220 : }
7221 : }
7222 105800 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7223 13113 : && warn_unused_value)
7224 : {
7225 : tree r = expr1;
7226 : location_t cloc = loc;
7227 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7228 : {
7229 2417 : if (EXPR_HAS_LOCATION (r))
7230 2417 : cloc = EXPR_LOCATION (r);
7231 2417 : r = TREE_OPERAND (r, 1);
7232 : }
7233 2416 : if (!TREE_SIDE_EFFECTS (r)
7234 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7235 2420 : && !CONVERT_EXPR_P (r))
7236 4 : warning_at (cloc, OPT_Wunused_value,
7237 : "right-hand operand of comma expression has no effect");
7238 : }
7239 :
7240 : /* With -Wunused, we should also warn if the left-hand operand does have
7241 : side-effects, but computes a value which is not used. For example, in
7242 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7243 : so we should issue a warning. */
7244 103384 : else if (warn_unused_value)
7245 35423 : warn_if_unused_value (expr1, loc);
7246 :
7247 114233 : maybe_warn_nodiscard (loc, expr1);
7248 :
7249 114233 : if (expr2 == error_mark_node)
7250 : return error_mark_node;
7251 :
7252 114220 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7253 :
7254 114220 : if (flag_isoc99
7255 : && expr1_int_operands
7256 113760 : && expr2_int_operands)
7257 153 : ret = note_integer_operands (ret);
7258 :
7259 114220 : if (eptype)
7260 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7261 :
7262 114220 : protected_set_expr_location (ret, loc);
7263 114220 : return ret;
7264 : }
7265 :
7266 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7267 : which we are casting. OTYPE is the type of the expression being
7268 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7269 : of the cast. -Wcast-qual appeared on the command line. Named
7270 : address space qualifiers are not handled here, because they result
7271 : in different warnings. */
7272 :
7273 : static void
7274 14071 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7275 : {
7276 14071 : tree in_type = type;
7277 14071 : tree in_otype = otype;
7278 14071 : int added = 0;
7279 14071 : int discarded = 0;
7280 14241 : bool is_const;
7281 :
7282 : /* Check that the qualifiers on IN_TYPE are a superset of the
7283 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7284 : nodes is uninteresting and we stop as soon as we hit a
7285 : non-POINTER_TYPE node on either type. */
7286 14241 : do
7287 : {
7288 14241 : in_otype = TREE_TYPE (in_otype);
7289 14241 : in_type = TREE_TYPE (in_type);
7290 :
7291 : /* GNU C allows cv-qualified function types. 'const' means the
7292 : function is very pure, 'volatile' means it can't return. We
7293 : need to warn when such qualifiers are added, not when they're
7294 : taken away. */
7295 14241 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7296 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7297 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7298 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7299 : else
7300 14223 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7301 14223 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7302 : }
7303 14241 : while (TREE_CODE (in_type) == POINTER_TYPE
7304 14241 : && TREE_CODE (in_otype) == POINTER_TYPE);
7305 :
7306 14071 : if (added)
7307 2 : warning_at (loc, OPT_Wcast_qual,
7308 : "cast adds %q#v qualifier to function type", added);
7309 :
7310 14071 : if (discarded)
7311 : /* There are qualifiers present in IN_OTYPE that are not present
7312 : in IN_TYPE. */
7313 1203 : warning_at (loc, OPT_Wcast_qual,
7314 : "cast discards %qv qualifier from pointer target type",
7315 : discarded);
7316 :
7317 14071 : if (added || discarded)
7318 : return;
7319 :
7320 : /* A cast from **T to const **T is unsafe, because it can cause a
7321 : const value to be changed with no additional warning. We only
7322 : issue this warning if T is the same on both sides, and we only
7323 : issue the warning if there are the same number of pointers on
7324 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7325 : cast is unsafe when a qualifier is added at one level and const
7326 : is not present at all outer levels.
7327 :
7328 : To issue this warning, we check at each level whether the cast
7329 : adds new qualifiers not already seen. We don't need to special
7330 : case function types, as they won't have the same
7331 : TYPE_MAIN_VARIANT. */
7332 :
7333 12866 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7334 : return;
7335 227 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7336 : return;
7337 :
7338 48 : in_type = type;
7339 48 : in_otype = otype;
7340 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7341 130 : do
7342 : {
7343 130 : in_type = TREE_TYPE (in_type);
7344 130 : in_otype = TREE_TYPE (in_otype);
7345 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7346 130 : && !is_const)
7347 : {
7348 27 : warning_at (loc, OPT_Wcast_qual,
7349 : "to be safe all intermediate pointers in cast from "
7350 : "%qT to %qT must be %<const%> qualified",
7351 : otype, type);
7352 27 : break;
7353 : }
7354 103 : if (is_const)
7355 72 : is_const = TYPE_READONLY (in_type);
7356 : }
7357 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7358 : }
7359 :
7360 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7361 :
7362 : static bool
7363 1316 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7364 : {
7365 1316 : if (error_operand_p (t1) || error_operand_p (t2))
7366 : return true;
7367 :
7368 1315 : t1 = TYPE_MAIN_VARIANT (t1);
7369 1315 : t2 = TYPE_MAIN_VARIANT (t2);
7370 :
7371 1315 : if (TREE_CODE (t1) == POINTER_TYPE
7372 229 : && TREE_CODE (t2) == POINTER_TYPE)
7373 : return true;
7374 :
7375 : /* Only the precision of the parameter matters. This check should
7376 : make sure that the callee does not see undefined values in argument
7377 : registers. */
7378 1107 : if (INTEGRAL_TYPE_P (t1)
7379 917 : && INTEGRAL_TYPE_P (t2)
7380 1452 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7381 : return true;
7382 :
7383 942 : return comptypes (t1, t2);
7384 : }
7385 :
7386 : /* Check if a type cast between two function types can be considered safe. */
7387 :
7388 : static bool
7389 7563 : c_safe_function_type_cast_p (tree t1, tree t2)
7390 : {
7391 7563 : if (TREE_TYPE (t1) == void_type_node &&
7392 5561 : TYPE_ARG_TYPES (t1) == void_list_node)
7393 : return true;
7394 :
7395 3726 : if (TREE_TYPE (t2) == void_type_node &&
7396 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7397 : return true;
7398 :
7399 896 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7400 : return false;
7401 :
7402 197 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7403 513 : t1 && t2;
7404 316 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7405 420 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7406 : return false;
7407 :
7408 : return true;
7409 : }
7410 :
7411 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7412 : LOC is the location of the cast-- typically the open paren of the cast. */
7413 :
7414 : tree
7415 124212231 : build_c_cast (location_t loc, tree type, tree expr)
7416 : {
7417 124212231 : tree value;
7418 :
7419 124212231 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7420 :
7421 124212231 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7422 66 : expr = TREE_OPERAND (expr, 0);
7423 :
7424 124212231 : value = expr;
7425 124212231 : if (int_operands)
7426 7148308 : value = remove_c_maybe_const_expr (value);
7427 :
7428 124212231 : if (type == error_mark_node || expr == error_mark_node)
7429 : return error_mark_node;
7430 :
7431 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7432 : only in <protocol> qualifications. But when constructing cast expressions,
7433 : the protocols do matter and must be kept around. */
7434 124211211 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7435 0 : return build1 (NOP_EXPR, type, expr);
7436 :
7437 124211211 : type = TYPE_MAIN_VARIANT (type);
7438 :
7439 124211211 : if (TREE_CODE (type) == ARRAY_TYPE)
7440 : {
7441 13 : error_at (loc, "cast specifies array type");
7442 13 : return error_mark_node;
7443 : }
7444 :
7445 124211198 : if (TREE_CODE (type) == FUNCTION_TYPE)
7446 : {
7447 6 : error_at (loc, "cast specifies function type");
7448 6 : return error_mark_node;
7449 : }
7450 :
7451 124211192 : if (!VOID_TYPE_P (type))
7452 : {
7453 124166559 : value = require_complete_type (loc, value);
7454 124166559 : if (value == error_mark_node)
7455 : return error_mark_node;
7456 : }
7457 :
7458 124211191 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7459 : {
7460 18799986 : if (RECORD_OR_UNION_TYPE_P (type)
7461 18799986 : && pedwarn (loc, OPT_Wpedantic,
7462 : "ISO C forbids casting nonscalar to the same type"))
7463 : ;
7464 18799982 : else if (warn_useless_cast && c_inhibit_evaluation_warnings == 0)
7465 8 : warning_at (loc, OPT_Wuseless_cast,
7466 : "useless cast to type %qT", type);
7467 :
7468 : /* Convert to remove any qualifiers from VALUE's type. */
7469 18799986 : value = convert (type, value);
7470 : }
7471 105411205 : else if (TREE_CODE (type) == UNION_TYPE)
7472 : {
7473 137 : tree field;
7474 :
7475 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7476 185 : if (TREE_TYPE (field) != error_mark_node
7477 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7478 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7479 : break;
7480 :
7481 137 : if (field)
7482 : {
7483 130 : tree t;
7484 130 : bool maybe_const = true;
7485 :
7486 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7487 130 : t = c_fully_fold (value, false, &maybe_const);
7488 130 : t = build_constructor_single (type, field, t);
7489 130 : if (!maybe_const)
7490 15 : t = c_wrap_maybe_const (t, true);
7491 130 : t = digest_init (loc, field, type, t,
7492 : NULL_TREE, false, false, false, true, false, false);
7493 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7494 130 : return t;
7495 : }
7496 7 : error_at (loc, "cast to union type from type not present in union");
7497 7 : return error_mark_node;
7498 : }
7499 : else
7500 : {
7501 105411068 : tree otype, ovalue;
7502 :
7503 105411068 : if (type == void_type_node)
7504 : {
7505 18663 : tree t = build1 (CONVERT_EXPR, type, value);
7506 18663 : SET_EXPR_LOCATION (t, loc);
7507 18663 : return t;
7508 : }
7509 :
7510 105392405 : otype = TREE_TYPE (value);
7511 :
7512 : /* Optionally warn about potentially worrisome casts. */
7513 105392405 : if (warn_cast_qual
7514 204226 : && TREE_CODE (type) == POINTER_TYPE
7515 27403 : && TREE_CODE (otype) == POINTER_TYPE)
7516 14071 : handle_warn_cast_qual (loc, type, otype);
7517 :
7518 : /* Warn about conversions between pointers to disjoint
7519 : address spaces. */
7520 105392405 : if (TREE_CODE (type) == POINTER_TYPE
7521 3364251 : && TREE_CODE (otype) == POINTER_TYPE
7522 108409419 : && !null_pointer_constant_p (value))
7523 : {
7524 2962968 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7525 2962968 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7526 2962968 : addr_space_t as_common;
7527 :
7528 2962968 : if (!addr_space_superset (as_to, as_from, &as_common))
7529 : {
7530 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7531 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7532 : "from disjoint generic address space pointer",
7533 : c_addr_space_name (as_to));
7534 :
7535 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7536 0 : warning_at (loc, 0, "cast to generic address space pointer "
7537 : "from disjoint %qs address space pointer",
7538 : c_addr_space_name (as_from));
7539 :
7540 : else
7541 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7542 : "from disjoint %qs address space pointer",
7543 : c_addr_space_name (as_to),
7544 : c_addr_space_name (as_from));
7545 : }
7546 :
7547 : /* Warn of new allocations that are not big enough for the target
7548 : type. */
7549 2962968 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7550 1069 : if (tree fndecl = get_callee_fndecl (value))
7551 1065 : if (DECL_IS_MALLOC (fndecl))
7552 : {
7553 573 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7554 573 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7555 573 : if (alloc_size)
7556 215 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7557 : alloc_size);
7558 : }
7559 : }
7560 :
7561 : /* Warn about possible alignment problems. */
7562 105392405 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7563 7 : && TREE_CODE (type) == POINTER_TYPE
7564 7 : && TREE_CODE (otype) == POINTER_TYPE
7565 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7566 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7567 : /* Don't warn about opaque types, where the actual alignment
7568 : restriction is unknown. */
7569 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7570 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7571 105392419 : && min_align_of_type (TREE_TYPE (type))
7572 7 : > min_align_of_type (TREE_TYPE (otype)))
7573 5 : warning_at (loc, OPT_Wcast_align,
7574 : "cast increases required alignment of target type");
7575 :
7576 105392405 : if ((TREE_CODE (type) == INTEGER_TYPE
7577 105392405 : || TREE_CODE (type) == BITINT_TYPE)
7578 7365583 : && TREE_CODE (otype) == POINTER_TYPE
7579 105417793 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7580 : /* Unlike conversion of integers to pointers, where the
7581 : warning is disabled for converting constants because
7582 : of cases such as SIG_*, warn about converting constant
7583 : pointers to integers. In some cases it may cause unwanted
7584 : sign extension, and a warning is appropriate. */
7585 1464 : warning_at (loc, OPT_Wpointer_to_int_cast,
7586 : "cast from pointer to integer of different size");
7587 :
7588 105392405 : if ((TREE_CODE (value) == CALL_EXPR
7589 36104313 : && !is_access_with_size_p (value))
7590 141496714 : && TREE_CODE (type) != TREE_CODE (otype))
7591 2308 : warning_at (loc, OPT_Wbad_function_cast,
7592 : "cast from function call of type %qT "
7593 : "to non-matching type %qT", otype, type);
7594 :
7595 105392405 : if (TREE_CODE (type) == POINTER_TYPE
7596 3364251 : && (TREE_CODE (otype) == INTEGER_TYPE
7597 3364251 : || TREE_CODE (otype) == BITINT_TYPE)
7598 347148 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7599 : /* Don't warn about converting any constant. */
7600 105701597 : && !TREE_CONSTANT (value))
7601 407 : warning_at (loc,
7602 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7603 : "of different size");
7604 :
7605 105392405 : if (warn_strict_aliasing <= 2)
7606 96626828 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7607 :
7608 : /* If pedantic, warn for conversions between function and object
7609 : pointer types, except for converting a null pointer constant
7610 : to function pointer type. */
7611 105392405 : if (pedantic
7612 261397 : && TREE_CODE (type) == POINTER_TYPE
7613 144380 : && TREE_CODE (otype) == POINTER_TYPE
7614 3035 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7615 105392426 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7616 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7617 : "conversion of function pointer to object pointer type");
7618 :
7619 105392405 : if (pedantic
7620 261397 : && TREE_CODE (type) == POINTER_TYPE
7621 144380 : && TREE_CODE (otype) == POINTER_TYPE
7622 3035 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7623 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7624 105392415 : && !null_pointer_constant_p (value))
7625 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7626 : "conversion of object pointer to function pointer type");
7627 :
7628 105392405 : if (TREE_CODE (type) == POINTER_TYPE
7629 3364251 : && TREE_CODE (otype) == POINTER_TYPE
7630 3017014 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7631 8684 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7632 105399968 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7633 7563 : TREE_TYPE (otype)))
7634 803 : warning_at (loc, OPT_Wcast_function_type,
7635 : "cast between incompatible function types"
7636 : " from %qT to %qT", otype, type);
7637 :
7638 105392405 : ovalue = value;
7639 : /* If converting to boolean a value with integer operands that
7640 : is not itself represented as an INTEGER_CST, the call below
7641 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7642 : build_binary_op as called by c_common_truthvalue_conversion
7643 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7644 : subexpression has been fully folded. To avoid nested
7645 : C_MAYBE_CONST_EXPR, ensure that
7646 : c_objc_common_truthvalue_conversion receives an argument
7647 : properly marked as having integer operands in that case. */
7648 105392405 : if (int_operands
7649 6957373 : && TREE_CODE (value) != INTEGER_CST
7650 105392447 : && (TREE_CODE (type) == BOOLEAN_TYPE
7651 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7652 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7653 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7654 28 : value = note_integer_operands (value);
7655 105392405 : value = convert (type, value);
7656 :
7657 : /* Ignore any integer overflow caused by the cast. */
7658 105392405 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7659 : {
7660 6904015 : if (TREE_OVERFLOW_P (ovalue))
7661 : {
7662 9 : if (!TREE_OVERFLOW (value))
7663 : {
7664 : /* Avoid clobbering a shared constant. */
7665 0 : value = copy_node (value);
7666 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7667 : }
7668 : }
7669 6904006 : else if (TREE_OVERFLOW (value))
7670 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7671 3093 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7672 : }
7673 : }
7674 :
7675 : /* Don't let a cast be an lvalue. */
7676 124192391 : if (lvalue_p (value))
7677 91085 : value = non_lvalue_loc (loc, value);
7678 :
7679 : /* Don't allow the results of casting to floating-point or complex
7680 : types be confused with actual constants, or casts involving
7681 : integer and pointer types other than direct integer-to-integer
7682 : and integer-to-pointer be confused with integer constant
7683 : expressions and null pointer constants. */
7684 124192391 : if (TREE_CODE (value) == REAL_CST
7685 124142443 : || TREE_CODE (value) == COMPLEX_CST
7686 248312211 : || (TREE_CODE (value) == INTEGER_CST
7687 7153845 : && !((TREE_CODE (expr) == INTEGER_CST
7688 7080059 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7689 66383 : || TREE_CODE (expr) == REAL_CST
7690 : || TREE_CODE (expr) == COMPLEX_CST)))
7691 134587 : value = build1 (NOP_EXPR, type, value);
7692 :
7693 : /* If the expression has integer operands and so can occur in an
7694 : unevaluated part of an integer constant expression, ensure the
7695 : return value reflects this. */
7696 124192391 : if (int_operands
7697 7134356 : && INTEGRAL_TYPE_P (type)
7698 6687368 : && value != error_mark_node
7699 130879758 : && !EXPR_INT_CONST_OPERANDS (value))
7700 11 : value = note_integer_operands (value);
7701 :
7702 124192391 : protected_set_expr_location (value, loc);
7703 124192391 : return value;
7704 : }
7705 :
7706 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7707 : location of the open paren of the cast, or the position of the cast
7708 : expr. */
7709 : tree
7710 124211735 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7711 : {
7712 124211735 : tree type;
7713 124211735 : tree type_expr = NULL_TREE;
7714 124211735 : bool type_expr_const = true;
7715 124211735 : tree ret;
7716 124211735 : int saved_wsp = warn_strict_prototypes;
7717 :
7718 : /* This avoids warnings about unprototyped casts on
7719 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7720 124211735 : if (TREE_CODE (expr) == INTEGER_CST)
7721 7202465 : warn_strict_prototypes = 0;
7722 124211735 : type = groktypename (type_name, &type_expr, &type_expr_const);
7723 124211735 : warn_strict_prototypes = saved_wsp;
7724 :
7725 765111 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7726 124976591 : && reject_gcc_builtin (expr))
7727 2 : return error_mark_node;
7728 :
7729 124211733 : ret = build_c_cast (loc, type, expr);
7730 124211733 : if (ret == error_mark_node)
7731 : return error_mark_node;
7732 :
7733 124210573 : if (type_expr)
7734 : {
7735 250 : bool inner_expr_const = true;
7736 250 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7737 250 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7738 500 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7739 145 : && inner_expr_const);
7740 250 : SET_EXPR_LOCATION (ret, loc);
7741 : }
7742 :
7743 124210573 : if (!EXPR_HAS_LOCATION (ret))
7744 7080605 : protected_set_expr_location (ret, loc);
7745 :
7746 : /* C++ does not permits types to be defined in a cast, but it
7747 : allows references to incomplete types. */
7748 124210573 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7749 1 : warning_at (loc, OPT_Wc___compat,
7750 : "defining a type in a cast is invalid in C++");
7751 :
7752 : return ret;
7753 : }
7754 :
7755 : /* Build an assignment expression of lvalue LHS from value RHS.
7756 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7757 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7758 : MODIFYCODE is the code for a binary operator that we use
7759 : to combine the old value of LHS with RHS to get the new value.
7760 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7761 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7762 : which may differ from TREE_TYPE (RHS) for an enum value.
7763 :
7764 : LOCATION is the location of the MODIFYCODE operator.
7765 : RHS_LOC is the location of the RHS. */
7766 :
7767 : tree
7768 2927802 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7769 : enum tree_code modifycode,
7770 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7771 : {
7772 2927802 : tree result;
7773 2927802 : tree newrhs;
7774 2927802 : tree rhseval = NULL_TREE;
7775 2927802 : tree lhstype = TREE_TYPE (lhs);
7776 2927802 : tree olhstype = lhstype;
7777 2927802 : bool npc;
7778 2927802 : bool is_atomic_op;
7779 :
7780 : /* Types that aren't fully specified cannot be used in assignments. */
7781 2927802 : lhs = require_complete_type (location, lhs);
7782 2927802 : rhs = require_complete_type (location, rhs);
7783 :
7784 : /* Avoid duplicate error messages from operands that had errors. */
7785 2927802 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7786 473 : return error_mark_node;
7787 :
7788 : /* Ensure an error for assigning a non-lvalue array to an array in
7789 : C90. */
7790 2927329 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7791 : {
7792 1 : error_at (location, "assignment to expression with array type");
7793 1 : return error_mark_node;
7794 : }
7795 :
7796 : /* For ObjC properties, defer this check. */
7797 2927328 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7798 32 : return error_mark_node;
7799 :
7800 2927296 : is_atomic_op = really_atomic_lvalue (lhs);
7801 :
7802 2927296 : newrhs = rhs;
7803 :
7804 2927296 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7805 : {
7806 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7807 : lhs_origtype, modifycode, rhs_loc, rhs,
7808 : rhs_origtype);
7809 2 : if (inner == error_mark_node)
7810 : return error_mark_node;
7811 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7812 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7813 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7814 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7815 2 : protected_set_expr_location (result, location);
7816 2 : return result;
7817 : }
7818 :
7819 : /* If a binary op has been requested, combine the old LHS value with the RHS
7820 : producing the value we should actually store into the LHS. */
7821 :
7822 2927294 : if (modifycode != NOP_EXPR)
7823 : {
7824 216617 : lhs = c_fully_fold (lhs, false, NULL, true);
7825 216617 : lhs = stabilize_reference (lhs);
7826 :
7827 : /* Construct the RHS for any non-atomic compound assignment. */
7828 216617 : if (!is_atomic_op)
7829 : {
7830 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7831 : are preevaluated before the rest of the assignment expression's
7832 : side-effects, because RHS could contain e.g. function calls
7833 : that modify LHS. */
7834 201132 : if (TREE_SIDE_EFFECTS (rhs))
7835 : {
7836 8917 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7837 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7838 : else
7839 8889 : newrhs = save_expr (rhs);
7840 8917 : rhseval = newrhs;
7841 8917 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7842 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7843 : newrhs);
7844 : }
7845 201132 : bool clear_decl_read = false;
7846 89021 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7847 154553 : && !DECL_READ_P (lhs)
7848 253873 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7849 : : warn_unused_but_set_parameter) > 2)
7850 : {
7851 21647 : mark_exp_read (newrhs);
7852 21647 : if (!DECL_READ_P (lhs))
7853 201132 : clear_decl_read = true;
7854 : }
7855 :
7856 201132 : newrhs = build_binary_op (location, modifycode,
7857 : convert_lvalue_to_rvalue (location, lhs,
7858 : true, true),
7859 : newrhs, true);
7860 201132 : if (clear_decl_read)
7861 21647 : DECL_READ_P (lhs) = 0;
7862 :
7863 : /* The original type of the right hand side is no longer
7864 : meaningful. */
7865 : rhs_origtype = NULL_TREE;
7866 : }
7867 : }
7868 :
7869 2927294 : if (c_dialect_objc ())
7870 : {
7871 : /* Check if we are modifying an Objective-C property reference;
7872 : if so, we need to generate setter calls. */
7873 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7874 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7875 : else
7876 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7877 0 : if (result)
7878 0 : goto return_result;
7879 :
7880 : /* Else, do the check that we postponed for Objective-C. */
7881 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7882 0 : return error_mark_node;
7883 : }
7884 :
7885 : /* Give an error for storing in something that is 'const'. */
7886 :
7887 2927294 : if (TYPE_READONLY (lhstype)
7888 2927294 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7889 25781 : && C_TYPE_FIELDS_READONLY (lhstype)))
7890 : {
7891 88 : readonly_error (location, lhs, lv_assign);
7892 88 : return error_mark_node;
7893 : }
7894 2927206 : else if (TREE_READONLY (lhs))
7895 1 : readonly_warning (lhs, lv_assign);
7896 :
7897 : /* If storing into a structure or union member,
7898 : it has probably been given type `int'.
7899 : Compute the type that would go with
7900 : the actual amount of storage the member occupies. */
7901 :
7902 2927206 : if (TREE_CODE (lhs) == COMPONENT_REF
7903 250649 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7904 250649 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7905 110463 : || SCALAR_FLOAT_TYPE_P (lhstype)
7906 87027 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7907 170630 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7908 :
7909 : /* If storing in a field that is in actuality a short or narrower than one,
7910 : we must store in the field in its actual type. */
7911 :
7912 2927206 : if (lhstype != TREE_TYPE (lhs))
7913 : {
7914 0 : lhs = copy_node (lhs);
7915 0 : TREE_TYPE (lhs) = lhstype;
7916 : }
7917 :
7918 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7919 : when LHS does not have its original type. This happens for,
7920 : e.g., an enum bitfield in a struct. */
7921 2927206 : if (warn_cxx_compat
7922 3272 : && lhs_origtype != NULL_TREE
7923 3272 : && lhs_origtype != lhstype
7924 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7925 : {
7926 4 : tree checktype = (rhs_origtype != NULL_TREE
7927 4 : ? rhs_origtype
7928 0 : : TREE_TYPE (rhs));
7929 4 : if (checktype != error_mark_node
7930 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7931 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7932 2 : warning_at (location, OPT_Wc___compat,
7933 : "enum conversion in assignment is invalid in C++");
7934 : }
7935 :
7936 : /* Remove qualifiers. */
7937 2927206 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7938 2927206 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7939 :
7940 : /* Convert new value to destination type. Fold it first, then
7941 : restore any excess precision information, for the sake of
7942 : conversion warnings. */
7943 :
7944 2927206 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7945 : {
7946 2911721 : tree rhs_semantic_type = NULL_TREE;
7947 2911721 : if (!c_in_omp_for)
7948 : {
7949 2895378 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7950 : {
7951 5077 : rhs_semantic_type = TREE_TYPE (newrhs);
7952 5077 : newrhs = TREE_OPERAND (newrhs, 0);
7953 : }
7954 2895378 : npc = null_pointer_constant_p (newrhs);
7955 2895378 : newrhs = c_fully_fold (newrhs, false, NULL);
7956 2895378 : if (rhs_semantic_type)
7957 5077 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7958 : }
7959 : else
7960 16343 : npc = null_pointer_constant_p (newrhs);
7961 2911721 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7962 : rhs_origtype, ic_assign, npc,
7963 : NULL_TREE, NULL_TREE, 0);
7964 2911721 : if (TREE_CODE (newrhs) == ERROR_MARK)
7965 127 : return error_mark_node;
7966 : }
7967 :
7968 : /* Emit ObjC write barrier, if necessary. */
7969 2927079 : if (c_dialect_objc () && flag_objc_gc)
7970 : {
7971 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7972 0 : if (result)
7973 : {
7974 0 : protected_set_expr_location (result, location);
7975 0 : goto return_result;
7976 : }
7977 : }
7978 :
7979 : /* Scan operands. */
7980 :
7981 2927079 : if (is_atomic_op)
7982 25988 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7983 : else
7984 : {
7985 2901091 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7986 2901091 : TREE_SIDE_EFFECTS (result) = 1;
7987 2901091 : protected_set_expr_location (result, location);
7988 : }
7989 :
7990 : /* If we got the LHS in a different type for storing in,
7991 : convert the result back to the nominal type of LHS
7992 : so that the value we return always has the same type
7993 : as the LHS argument. */
7994 :
7995 2927079 : if (olhstype == TREE_TYPE (result))
7996 2927079 : goto return_result;
7997 :
7998 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7999 : rhs_origtype, ic_assign, false, NULL_TREE,
8000 : NULL_TREE, 0);
8001 0 : protected_set_expr_location (result, location);
8002 :
8003 2927079 : return_result:
8004 2927079 : if (rhseval)
8005 8917 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
8006 : return result;
8007 : }
8008 :
8009 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
8010 : This is used to implement -fplan9-extensions. */
8011 :
8012 : static bool
8013 26 : find_anonymous_field_with_type (tree struct_type, tree type)
8014 : {
8015 26 : tree field;
8016 26 : bool found;
8017 :
8018 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
8019 26 : found = false;
8020 26 : for (field = TYPE_FIELDS (struct_type);
8021 68 : field != NULL_TREE;
8022 42 : field = TREE_CHAIN (field))
8023 : {
8024 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8025 42 : if (DECL_NAME (field) == NULL
8026 42 : && comptypes (type, fieldtype))
8027 : {
8028 4 : if (found)
8029 : return false;
8030 : found = true;
8031 : }
8032 38 : else if (DECL_NAME (field) == NULL
8033 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
8034 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
8035 : {
8036 0 : if (found)
8037 : return false;
8038 : found = true;
8039 : }
8040 : }
8041 : return found;
8042 : }
8043 :
8044 : /* RHS is an expression whose type is pointer to struct. If there is
8045 : an anonymous field in RHS with type TYPE, then return a pointer to
8046 : that field in RHS. This is used with -fplan9-extensions. This
8047 : returns NULL if no conversion could be found. */
8048 :
8049 : static tree
8050 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
8051 : {
8052 28 : tree rhs_struct_type, lhs_main_type;
8053 28 : tree field, found_field;
8054 28 : bool found_sub_field;
8055 28 : tree ret;
8056 :
8057 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8058 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8059 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8060 :
8061 28 : gcc_assert (POINTER_TYPE_P (type));
8062 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8063 :
8064 28 : found_field = NULL_TREE;
8065 28 : found_sub_field = false;
8066 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8067 172 : field != NULL_TREE;
8068 144 : field = TREE_CHAIN (field))
8069 : {
8070 148 : if (DECL_NAME (field) != NULL_TREE
8071 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8072 96 : continue;
8073 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8074 52 : if (comptypes (lhs_main_type, fieldtype))
8075 : {
8076 28 : if (found_field != NULL_TREE)
8077 : return NULL_TREE;
8078 : found_field = field;
8079 : }
8080 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8081 : lhs_main_type))
8082 : {
8083 4 : if (found_field != NULL_TREE)
8084 : return NULL_TREE;
8085 : found_field = field;
8086 : found_sub_field = true;
8087 : }
8088 : }
8089 :
8090 24 : if (found_field == NULL_TREE)
8091 : return NULL_TREE;
8092 :
8093 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8094 : build_fold_indirect_ref (rhs), found_field,
8095 : NULL_TREE);
8096 24 : ret = build_fold_addr_expr_loc (location, ret);
8097 :
8098 24 : if (found_sub_field)
8099 : {
8100 2 : ret = convert_to_anonymous_field (location, type, ret);
8101 2 : gcc_assert (ret != NULL_TREE);
8102 : }
8103 :
8104 : return ret;
8105 : }
8106 :
8107 : /* Issue an error message for a bad initializer component.
8108 : GMSGID identifies the message.
8109 : The component name is taken from the spelling stack. */
8110 :
8111 : static void ATTRIBUTE_GCC_DIAG (2,0)
8112 944 : error_init (location_t loc, const char *gmsgid, ...)
8113 : {
8114 944 : char *ofwhat;
8115 :
8116 944 : auto_diagnostic_group d;
8117 :
8118 : /* The gmsgid may be a format string with %< and %>. */
8119 944 : va_list ap;
8120 944 : va_start (ap, gmsgid);
8121 944 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8122 944 : loc, -1, gmsgid, &ap);
8123 944 : va_end (ap);
8124 :
8125 944 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8126 944 : if (*ofwhat && warned)
8127 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8128 944 : }
8129 :
8130 : /* Used to implement pedwarn_init and permerror_init. */
8131 :
8132 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8133 2478 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8134 : va_list *ap, enum diagnostics::kind kind)
8135 : {
8136 : /* Use the location where a macro was expanded rather than where
8137 : it was defined to make sure macros defined in system headers
8138 : but used incorrectly elsewhere are diagnosed. */
8139 2478 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8140 2478 : auto_diagnostic_group d;
8141 2478 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8142 2478 : if (warned)
8143 : {
8144 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8145 1815 : if (*ofwhat)
8146 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8147 : }
8148 4956 : return warned;
8149 2478 : }
8150 :
8151 : /* Issue a pedantic warning for a bad initializer component. OPT is
8152 : the option OPT_* (from options.h) controlling this warning or 0 if
8153 : it is unconditionally given. GMSGID identifies the message. The
8154 : component name is taken from the spelling stack. */
8155 :
8156 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8157 2127 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8158 : {
8159 2127 : va_list ap;
8160 2127 : va_start (ap, gmsgid);
8161 2127 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8162 : diagnostics::kind::pedwarn);
8163 2127 : va_end (ap);
8164 2127 : return warned;
8165 : }
8166 :
8167 : /* Like pedwarn_init, but issue a permerror. */
8168 :
8169 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8170 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8171 : {
8172 351 : va_list ap;
8173 351 : va_start (ap, gmsgid);
8174 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8175 : diagnostics::kind::permerror);
8176 351 : va_end (ap);
8177 351 : return warned;
8178 : }
8179 :
8180 : /* Issue a warning for a bad initializer component.
8181 :
8182 : OPT is the OPT_W* value corresponding to the warning option that
8183 : controls this warning. GMSGID identifies the message. The
8184 : component name is taken from the spelling stack. */
8185 :
8186 : static void
8187 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8188 : {
8189 146 : char *ofwhat;
8190 146 : bool warned;
8191 :
8192 146 : auto_diagnostic_group d;
8193 :
8194 : /* Use the location where a macro was expanded rather than where
8195 : it was defined to make sure macros defined in system headers
8196 : but used incorrectly elsewhere are diagnosed. */
8197 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8198 :
8199 : /* The gmsgid may be a format string with %< and %>. */
8200 146 : warned = warning_at (exploc, opt, gmsgid);
8201 146 : if (warned)
8202 : {
8203 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8204 133 : if (*ofwhat)
8205 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8206 : }
8207 146 : }
8208 :
8209 : /* If TYPE is an array type and EXPR is a parenthesized string
8210 : constant, warn if pedantic that EXPR is being used to initialize an
8211 : object of type TYPE. */
8212 :
8213 : void
8214 7312331 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8215 : {
8216 7312331 : if (pedantic
8217 81843 : && TREE_CODE (type) == ARRAY_TYPE
8218 1193 : && TREE_CODE (expr.value) == STRING_CST
8219 472 : && expr.original_code != STRING_CST)
8220 13 : pedwarn_init (loc, OPT_Wpedantic,
8221 : "array initialized from parenthesized string constant");
8222 7312331 : }
8223 :
8224 : /* Attempt to locate the parameter with the given index within FNDECL,
8225 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8226 :
8227 : static location_t
8228 937 : get_fndecl_argument_location (tree fndecl, int argnum)
8229 : {
8230 937 : int i;
8231 937 : tree param;
8232 :
8233 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8234 937 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8235 1016 : i < argnum && param;
8236 79 : i++, param = TREE_CHAIN (param))
8237 : ;
8238 :
8239 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8240 : return DECL_SOURCE_LOCATION (FNDECL). */
8241 937 : if (param == NULL)
8242 653 : return DECL_SOURCE_LOCATION (fndecl);
8243 :
8244 284 : return DECL_SOURCE_LOCATION (param);
8245 : }
8246 :
8247 : /* Issue a note about a mismatching argument for parameter PARMNUM
8248 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8249 : Attempt to issue the note at the pertinent parameter of the decl;
8250 : failing that issue it at the location of FUNDECL; failing that
8251 : issue it at PLOC.
8252 : Use highlight_colors::actual for the ACTUAL_TYPE
8253 : and highlight_colors::expected for EXPECTED_TYPE and the
8254 : parameter of FUNDECL*/
8255 :
8256 : static void
8257 1062 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8258 : tree expected_type, tree actual_type)
8259 : {
8260 1062 : location_t loc;
8261 1062 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8262 937 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8263 : else
8264 : loc = ploc;
8265 :
8266 1062 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8267 :
8268 1062 : pp_markup::element_expected_type elem_expected_type (expected_type);
8269 1062 : pp_markup::element_actual_type elem_actual_type (actual_type);
8270 1062 : inform (&richloc,
8271 : "expected %e but argument is of type %e",
8272 : &elem_expected_type, &elem_actual_type);
8273 1062 : }
8274 :
8275 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8276 : function FUNDECL declared without prototype to parameter PARMNUM of
8277 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8278 :
8279 : static void
8280 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8281 : tree parmtype, tree argtype)
8282 : {
8283 418 : tree_code parmcode = TREE_CODE (parmtype);
8284 418 : tree_code argcode = TREE_CODE (argtype);
8285 418 : tree promoted = c_type_promotes_to (argtype);
8286 :
8287 : /* Avoid warning for enum arguments that promote to an integer type
8288 : of the same size/mode. */
8289 418 : if (parmcode == INTEGER_TYPE
8290 418 : && argcode == ENUMERAL_TYPE
8291 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8292 : return;
8293 :
8294 417 : if ((parmcode == argcode
8295 222 : || (parmcode == INTEGER_TYPE
8296 : && argcode == ENUMERAL_TYPE))
8297 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8298 : return;
8299 :
8300 : /* This diagnoses even signed/unsigned mismatches. Those might be
8301 : safe in many cases but GCC may emit suboptimal code for them so
8302 : warning on those cases drives efficiency improvements. */
8303 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8304 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8305 : ? G_("%qD argument %d type is %qT where %qT is expected "
8306 : "in a call to built-in function declared without "
8307 : "prototype")
8308 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8309 : "in a call to built-in function declared without "
8310 : "prototype"),
8311 : fundecl, parmnum, promoted, parmtype))
8312 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8313 : "built-in %qD declared here",
8314 : fundecl);
8315 : }
8316 :
8317 : /* Print a declaration in quotes, with the given highlight_color.
8318 : Analogous to handler for %qD, but with a specific highlight color. */
8319 :
8320 199 : class pp_element_quoted_decl : public pp_element
8321 : {
8322 : public:
8323 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8324 199 : : m_decl (decl),
8325 199 : m_highlight_color (highlight_color)
8326 : {
8327 : }
8328 :
8329 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8330 : {
8331 199 : ctxt.begin_quote ();
8332 199 : ctxt.begin_highlight_color (m_highlight_color);
8333 :
8334 199 : print_decl (ctxt);
8335 :
8336 199 : ctxt.end_highlight_color ();
8337 199 : ctxt.end_quote ();
8338 199 : }
8339 :
8340 199 : void print_decl (pp_markup::context &ctxt)
8341 : {
8342 199 : pretty_printer *const pp = &ctxt.m_pp;
8343 199 : pp->set_padding (pp_none);
8344 199 : if (DECL_NAME (m_decl))
8345 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8346 : else
8347 0 : pp_string (pp, _("({anonymous})"));
8348 199 : }
8349 :
8350 : private:
8351 : tree m_decl;
8352 : const char *m_highlight_color;
8353 : };
8354 :
8355 : /* If TYPE is from a typedef, issue a note showing the location
8356 : to the user.
8357 : Use HIGHLIGHT_COLOR as the highlight color. */
8358 :
8359 : static void
8360 1180 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8361 : {
8362 1180 : if (!typedef_variant_p (type))
8363 1146 : return;
8364 :
8365 34 : tree typedef_decl = TYPE_NAME (type);
8366 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8367 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8368 34 : nullptr, highlight_color);
8369 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8370 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8371 34 : }
8372 :
8373 : /* Convert value RHS to type TYPE as preparation for an assignment to
8374 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8375 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8376 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8377 : constant before any folding.
8378 : The real work of conversion is done by `convert'.
8379 : The purpose of this function is to generate error messages
8380 : for assignments that are not allowed in C.
8381 : ERRTYPE says whether it is argument passing, assignment,
8382 : initialization or return.
8383 :
8384 : In the following example, '~' denotes where EXPR_LOC and '^' where
8385 : LOCATION point to:
8386 :
8387 : f (var); [ic_argpass]
8388 : ^ ~~~
8389 : x = var; [ic_assign]
8390 : ^ ~~~;
8391 : int x = var; [ic_init]
8392 : ^^^
8393 : return x; [ic_return]
8394 : ^
8395 :
8396 : FUNCTION is a tree for the function being called.
8397 : PARMNUM is the number of the argument, for printing in error messages.
8398 : WARNOPT may be set to a warning option to issue the corresponding warning
8399 : rather than an error for invalid conversions. Used for calls to built-in
8400 : functions declared without a prototype. */
8401 :
8402 : static tree
8403 171268752 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8404 : tree rhs, tree origtype, enum impl_conv errtype,
8405 : bool null_pointer_constant, tree fundecl,
8406 : tree function, int parmnum, int warnopt /* = 0 */)
8407 : {
8408 171268752 : enum tree_code codel = TREE_CODE (type);
8409 171268752 : tree orig_rhs = rhs;
8410 171268752 : tree rhstype;
8411 171268752 : enum tree_code coder;
8412 171268752 : tree rname = NULL_TREE;
8413 171268752 : bool objc_ok = false;
8414 :
8415 : /* Use the expansion point location to handle cases such as user's
8416 : function returning a wrong-type macro defined in a system header. */
8417 171268752 : location = expansion_point_location_if_in_system_header (location);
8418 :
8419 171268752 : if (errtype == ic_argpass)
8420 : {
8421 128791577 : tree selector;
8422 : /* Change pointer to function to the function itself for
8423 : diagnostics. */
8424 128791577 : if (TREE_CODE (function) == ADDR_EXPR
8425 128791577 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8426 0 : function = TREE_OPERAND (function, 0);
8427 :
8428 : /* Handle an ObjC selector specially for diagnostics. */
8429 128791577 : selector = objc_message_selector ();
8430 128791577 : rname = function;
8431 128791577 : if (selector && parmnum > 2)
8432 : {
8433 0 : rname = selector;
8434 0 : parmnum -= 2;
8435 : }
8436 : }
8437 :
8438 : /* This macro is used to emit diagnostics to ensure that all format
8439 : strings are complete sentences, visible to gettext and checked at
8440 : compile time. */
8441 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8442 : do { \
8443 : switch (errtype) \
8444 : { \
8445 : case ic_argpass: \
8446 : { \
8447 : auto_diagnostic_group d; \
8448 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8449 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8450 : } \
8451 : break; \
8452 : case ic_assign: \
8453 : pedwarn (LOCATION, OPT, AS); \
8454 : break; \
8455 : case ic_init: \
8456 : case ic_init_const: \
8457 : pedwarn_init (LOCATION, OPT, IN); \
8458 : break; \
8459 : case ic_return: \
8460 : pedwarn (LOCATION, OPT, RE); \
8461 : break; \
8462 : default: \
8463 : gcc_unreachable (); \
8464 : } \
8465 : } while (0)
8466 :
8467 : /* This macro is used to emit diagnostics to ensure that all format
8468 : strings are complete sentences, visible to gettext and checked at
8469 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8470 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8471 : do { \
8472 : switch (errtype) \
8473 : { \
8474 : case ic_argpass: \
8475 : { \
8476 : auto_diagnostic_group d; \
8477 : if (PEDWARN) { \
8478 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8479 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8480 : } else { \
8481 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8482 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8483 : } \
8484 : } \
8485 : break; \
8486 : case ic_assign: \
8487 : if (PEDWARN) \
8488 : pedwarn (LOCATION, OPT, AS, QUALS); \
8489 : else \
8490 : warning_at (LOCATION, OPT, AS, QUALS); \
8491 : break; \
8492 : case ic_init: \
8493 : case ic_init_const: \
8494 : if (PEDWARN) \
8495 : pedwarn (LOCATION, OPT, IN, QUALS); \
8496 : else \
8497 : warning_at (LOCATION, OPT, IN, QUALS); \
8498 : break; \
8499 : case ic_return: \
8500 : if (PEDWARN) \
8501 : pedwarn (LOCATION, OPT, RE, QUALS); \
8502 : else \
8503 : warning_at (LOCATION, OPT, RE, QUALS); \
8504 : break; \
8505 : default: \
8506 : gcc_unreachable (); \
8507 : } \
8508 : } while (0)
8509 :
8510 : /* This macro is used to emit diagnostics to ensure that all format
8511 : strings are complete sentences, visible to gettext and checked at
8512 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8513 : extra parameter to enumerate qualifiers. */
8514 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8515 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8516 :
8517 :
8518 171268752 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8519 5985 : rhs = TREE_OPERAND (rhs, 0);
8520 :
8521 171268752 : rhstype = TREE_TYPE (rhs);
8522 171268752 : coder = TREE_CODE (rhstype);
8523 :
8524 171268752 : if (coder == ERROR_MARK)
8525 347 : return error_mark_node;
8526 :
8527 171268405 : if (c_dialect_objc ())
8528 : {
8529 0 : int parmno;
8530 :
8531 0 : switch (errtype)
8532 : {
8533 : case ic_return:
8534 : parmno = 0;
8535 : break;
8536 :
8537 : case ic_assign:
8538 : parmno = -1;
8539 : break;
8540 :
8541 : case ic_init:
8542 : case ic_init_const:
8543 : parmno = -2;
8544 : break;
8545 :
8546 : default:
8547 0 : parmno = parmnum;
8548 : break;
8549 : }
8550 :
8551 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8552 : }
8553 :
8554 171268405 : if (warn_cxx_compat)
8555 : {
8556 29117 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8557 29117 : if (checktype != error_mark_node
8558 29117 : && TREE_CODE (type) == ENUMERAL_TYPE
8559 29223 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8560 45 : switch (errtype)
8561 : {
8562 8 : case ic_argpass:
8563 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8564 : "passing argument %d of %qE is invalid in C++",
8565 : parmnum, rname))
8566 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8567 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8568 : "expected %qT but argument is of type %qT",
8569 : type, rhstype);
8570 : break;
8571 10 : case ic_assign:
8572 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8573 : "%qT in assignment is invalid in C++", rhstype, type);
8574 10 : break;
8575 18 : case ic_init:
8576 18 : case ic_init_const:
8577 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8578 : "%qT to %qT in initialization is invalid in C++",
8579 : rhstype, type);
8580 18 : break;
8581 9 : case ic_return:
8582 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8583 : "%qT in return is invalid in C++", rhstype, type);
8584 9 : break;
8585 0 : default:
8586 0 : gcc_unreachable ();
8587 : }
8588 : }
8589 :
8590 171268405 : if (warn_enum_conversion)
8591 : {
8592 14879222 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8593 14879222 : if (checktype != error_mark_node
8594 14879222 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8595 12843 : && TREE_CODE (type) == ENUMERAL_TYPE
8596 14887518 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8597 : {
8598 7 : gcc_rich_location loc (location);
8599 7 : warning_at (&loc, OPT_Wenum_conversion,
8600 : "implicit conversion from %qT to %qT",
8601 : checktype, type);
8602 7 : }
8603 : }
8604 :
8605 171268405 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8606 : {
8607 152066275 : warn_for_address_of_packed_member (type, orig_rhs);
8608 152066275 : if (type != rhstype)
8609 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8610 37825068 : rhs = convert (type, rhs);
8611 : return rhs;
8612 : }
8613 :
8614 19202130 : if (coder == VOID_TYPE)
8615 : {
8616 : /* Except for passing an argument to an unprototyped function,
8617 : this is a constraint violation. When passing an argument to
8618 : an unprototyped function, it is compile-time undefined;
8619 : making it a constraint in that case was rejected in
8620 : DR#252. */
8621 8 : const char msg[] = "void value not ignored as it ought to be";
8622 8 : if (warnopt)
8623 0 : warning_at (location, warnopt, msg);
8624 : else
8625 8 : error_at (location, msg);
8626 8 : return error_mark_node;
8627 : }
8628 19202122 : rhs = require_complete_type (location, rhs);
8629 19202122 : if (rhs == error_mark_node)
8630 : return error_mark_node;
8631 :
8632 19202122 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8633 6 : return error_mark_node;
8634 :
8635 : /* A non-reference type can convert to a reference. This handles
8636 : va_start, va_copy and possibly port built-ins. */
8637 19202116 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8638 : {
8639 286 : if (!lvalue_p (rhs))
8640 : {
8641 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8642 0 : if (warnopt)
8643 0 : warning_at (location, warnopt, msg);
8644 : else
8645 0 : error_at (location, msg);
8646 0 : return error_mark_node;
8647 : }
8648 286 : if (!c_mark_addressable (rhs))
8649 0 : return error_mark_node;
8650 286 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8651 286 : SET_EXPR_LOCATION (rhs, location);
8652 :
8653 286 : rhs = convert_for_assignment (location, expr_loc,
8654 286 : c_build_pointer_type (TREE_TYPE (type)),
8655 : rhs, origtype, errtype,
8656 : null_pointer_constant, fundecl, function,
8657 : parmnum, warnopt);
8658 286 : if (rhs == error_mark_node)
8659 : return error_mark_node;
8660 :
8661 286 : rhs = build1 (NOP_EXPR, type, rhs);
8662 286 : SET_EXPR_LOCATION (rhs, location);
8663 286 : return rhs;
8664 : }
8665 : /* Some types can interconvert without explicit casts. */
8666 19201830 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8667 19201830 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8668 9329014 : return convert (type, rhs);
8669 : /* Arithmetic types all interconvert, and enum is treated like int. */
8670 9872816 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8671 2295277 : || codel == FIXED_POINT_TYPE
8672 2295277 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8673 2208736 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8674 7764605 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8675 83839 : || coder == FIXED_POINT_TYPE
8676 83839 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8677 40585 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8678 : {
8679 7763231 : if (warnopt && errtype == ic_argpass)
8680 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8681 : rhstype);
8682 :
8683 7763231 : bool save = in_late_binary_op;
8684 7726153 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8685 15432219 : || (coder == REAL_TYPE
8686 276911 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8687 24208 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8688 100987 : in_late_binary_op = true;
8689 11159161 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8690 : ? expr_loc : location, type, orig_rhs,
8691 : errtype == ic_init_const);
8692 7763231 : in_late_binary_op = save;
8693 7763231 : return ret;
8694 : }
8695 :
8696 : /* Aggregates in different TUs might need conversion. */
8697 2109585 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8698 105 : && codel == coder
8699 2109607 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8700 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8701 9 : ? expr_loc : location, type, rhs);
8702 :
8703 : /* Conversion to a transparent union or record from its member types.
8704 : This applies only to function arguments. */
8705 2109576 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8706 96 : && TYPE_TRANSPARENT_AGGR (type))
8707 2109615 : && errtype == ic_argpass)
8708 : {
8709 39 : tree memb, marginal_memb = NULL_TREE;
8710 :
8711 50 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8712 : {
8713 50 : tree memb_type = TREE_TYPE (memb);
8714 :
8715 50 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8716 50 : TYPE_MAIN_VARIANT (rhstype)))
8717 : break;
8718 :
8719 27 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8720 1 : continue;
8721 :
8722 26 : if (coder == POINTER_TYPE)
8723 : {
8724 26 : tree ttl = TREE_TYPE (memb_type);
8725 26 : tree ttr = TREE_TYPE (rhstype);
8726 :
8727 : /* Any non-function converts to a [const][volatile] void *
8728 : and vice versa; otherwise, targets must be the same.
8729 : Meanwhile, the lhs target must have all the qualifiers of
8730 : the rhs. */
8731 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8732 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8733 45 : || comp_target_types (location, memb_type, rhstype))
8734 : {
8735 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8736 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8737 : /* If this type won't generate any warnings, use it. */
8738 16 : if (lquals == rquals
8739 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8740 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8741 0 : ? ((lquals | rquals) == rquals)
8742 16 : : ((lquals | rquals) == lquals)))
8743 : break;
8744 :
8745 : /* Keep looking for a better type, but remember this one. */
8746 0 : if (!marginal_memb)
8747 10 : marginal_memb = memb;
8748 : }
8749 : }
8750 :
8751 : /* Can convert integer zero to any pointer type. */
8752 10 : if (null_pointer_constant)
8753 : {
8754 0 : rhs = null_pointer_node;
8755 0 : break;
8756 : }
8757 : }
8758 :
8759 39 : if (memb || marginal_memb)
8760 : {
8761 39 : if (!memb)
8762 : {
8763 : /* We have only a marginally acceptable member type;
8764 : it needs a warning. */
8765 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8766 0 : tree ttr = TREE_TYPE (rhstype);
8767 :
8768 : /* Const and volatile mean something different for function
8769 : types, so the usual warnings are not appropriate. */
8770 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8771 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8772 : {
8773 : /* Because const and volatile on functions are
8774 : restrictions that say the function will not do
8775 : certain things, it is okay to use a const or volatile
8776 : function where an ordinary one is wanted, but not
8777 : vice-versa. */
8778 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8779 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8780 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8781 : OPT_Wdiscarded_qualifiers,
8782 : G_("passing argument %d of %qE "
8783 : "makes %q#v qualified function "
8784 : "pointer from unqualified"),
8785 : G_("assignment makes %q#v qualified "
8786 : "function pointer from "
8787 : "unqualified"),
8788 : G_("initialization makes %q#v qualified "
8789 : "function pointer from "
8790 : "unqualified"),
8791 : G_("return makes %q#v qualified function "
8792 : "pointer from unqualified"),
8793 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8794 : }
8795 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8796 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8797 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8798 : OPT_Wdiscarded_qualifiers,
8799 : G_("passing argument %d of %qE discards "
8800 : "%qv qualifier from pointer target type"),
8801 : G_("assignment discards %qv qualifier "
8802 : "from pointer target type"),
8803 : G_("initialization discards %qv qualifier "
8804 : "from pointer target type"),
8805 : G_("return discards %qv qualifier from "
8806 : "pointer target type"),
8807 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8808 :
8809 : memb = marginal_memb;
8810 : }
8811 :
8812 39 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8813 31 : pedwarn (location, OPT_Wpedantic,
8814 : "ISO C prohibits argument conversion to union type");
8815 :
8816 39 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8817 39 : return build_constructor_single (type, memb, rhs);
8818 : }
8819 : }
8820 :
8821 : /* Conversions among pointers */
8822 2109537 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8823 2108007 : && (coder == codel))
8824 : {
8825 : /* If RHS refers to a built-in declared without a prototype
8826 : BLTIN is the declaration of the built-in with a prototype
8827 : and RHSTYPE is set to the actual type of the built-in. */
8828 2029648 : tree bltin;
8829 2029648 : rhstype = type_or_builtin_type (rhs, &bltin);
8830 :
8831 2029648 : tree ttl = TREE_TYPE (type);
8832 2029648 : tree ttr = TREE_TYPE (rhstype);
8833 2029648 : bool is_opaque_pointer;
8834 2029648 : bool target_cmp = false; /* Cache comp_target_types () result. */
8835 2029648 : addr_space_t asl;
8836 2029648 : addr_space_t asr;
8837 :
8838 2029648 : tree mvl = remove_qualifiers (ttl);
8839 2029648 : tree mvr = remove_qualifiers (ttr);
8840 :
8841 : /* Opaque pointers are treated like void pointers. */
8842 2029648 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8843 :
8844 : /* The Plan 9 compiler permits a pointer to a struct to be
8845 : automatically converted into a pointer to an anonymous field
8846 : within the struct. */
8847 2029648 : if (flag_plan9_extensions
8848 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8849 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8850 26 : && mvl != mvr)
8851 : {
8852 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8853 26 : if (new_rhs != NULL_TREE)
8854 : {
8855 22 : rhs = new_rhs;
8856 22 : rhstype = TREE_TYPE (rhs);
8857 22 : coder = TREE_CODE (rhstype);
8858 22 : ttr = TREE_TYPE (rhstype);
8859 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8860 : }
8861 : }
8862 :
8863 : /* C++ does not allow the implicit conversion void* -> T*. However,
8864 : for the purpose of reducing the number of false positives, we
8865 : tolerate the special case of
8866 :
8867 : int *p = NULL;
8868 :
8869 : where NULL is typically defined in C to be '(void *) 0'. */
8870 2029648 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8871 27417 : warning_at (errtype == ic_argpass ? expr_loc : location,
8872 14532 : OPT_Wc___compat,
8873 : "request for implicit conversion "
8874 : "from %qT to %qT not permitted in C++", rhstype, type);
8875 :
8876 : /* Warn of new allocations that are not big enough for the target
8877 : type. */
8878 2029648 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8879 5463 : if (tree fndecl = get_callee_fndecl (rhs))
8880 5463 : if (DECL_IS_MALLOC (fndecl))
8881 : {
8882 4614 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8883 4614 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8884 4614 : if (alloc_size)
8885 451 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8886 : }
8887 :
8888 : /* See if the pointers point to incompatible address spaces. */
8889 2029648 : asl = TYPE_ADDR_SPACE (ttl);
8890 2029648 : asr = TYPE_ADDR_SPACE (ttr);
8891 2029648 : if (!null_pointer_constant_p (rhs)
8892 2029648 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8893 : {
8894 3 : auto_diagnostic_group d;
8895 3 : bool diagnosed = true;
8896 3 : switch (errtype)
8897 : {
8898 1 : case ic_argpass:
8899 1 : {
8900 1 : const char msg[] = G_("passing argument %d of %qE from "
8901 : "pointer to non-enclosed address space");
8902 1 : if (warnopt)
8903 0 : diagnosed
8904 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8905 : else
8906 1 : error_at (expr_loc, msg, parmnum, rname);
8907 0 : break;
8908 : }
8909 0 : case ic_assign:
8910 0 : {
8911 0 : const char msg[] = G_("assignment from pointer to "
8912 : "non-enclosed address space");
8913 0 : if (warnopt)
8914 0 : diagnosed = warning_at (location, warnopt, msg);
8915 : else
8916 0 : error_at (location, msg);
8917 0 : break;
8918 : }
8919 0 : case ic_init:
8920 0 : case ic_init_const:
8921 0 : {
8922 0 : const char msg[] = G_("initialization from pointer to "
8923 : "non-enclosed address space");
8924 0 : if (warnopt)
8925 0 : diagnosed = warning_at (location, warnopt, msg);
8926 : else
8927 0 : error_at (location, msg);
8928 0 : break;
8929 : }
8930 2 : case ic_return:
8931 2 : {
8932 2 : const char msg[] = G_("return from pointer to "
8933 : "non-enclosed address space");
8934 2 : if (warnopt)
8935 0 : diagnosed = warning_at (location, warnopt, msg);
8936 : else
8937 2 : error_at (location, msg);
8938 0 : break;
8939 : }
8940 0 : default:
8941 0 : gcc_unreachable ();
8942 : }
8943 3 : if (diagnosed)
8944 : {
8945 3 : if (errtype == ic_argpass)
8946 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8947 : else
8948 2 : inform (location, "expected %qT but pointer is of type %qT",
8949 : type, rhstype);
8950 : }
8951 3 : return error_mark_node;
8952 3 : }
8953 :
8954 : /* Check if the right-hand side has a format attribute but the
8955 : left-hand side doesn't. */
8956 2029645 : if (warn_suggest_attribute_format
8957 2029645 : && check_missing_format_attribute (type, rhstype))
8958 : {
8959 16 : switch (errtype)
8960 : {
8961 4 : case ic_argpass:
8962 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8963 : "argument %d of %qE might be "
8964 : "a candidate for a format attribute",
8965 : parmnum, rname);
8966 4 : break;
8967 4 : case ic_assign:
8968 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8969 : "assignment left-hand side might be "
8970 : "a candidate for a format attribute");
8971 4 : break;
8972 4 : case ic_init:
8973 4 : case ic_init_const:
8974 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8975 : "initialization left-hand side might be "
8976 : "a candidate for a format attribute");
8977 4 : break;
8978 4 : case ic_return:
8979 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8980 : "return type might be "
8981 : "a candidate for a format attribute");
8982 4 : break;
8983 0 : default:
8984 0 : gcc_unreachable ();
8985 : }
8986 : }
8987 :
8988 : /* See if the pointers point to incompatible scalar storage orders. */
8989 2029645 : if (warn_scalar_storage_order
8990 2026729 : && !null_pointer_constant_p (rhs)
8991 6014323 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8992 1992339 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8993 : {
8994 18 : tree t;
8995 :
8996 18 : switch (errtype)
8997 : {
8998 11 : case ic_argpass:
8999 : /* Do not warn for built-in functions, for example memcpy, since we
9000 : control how they behave and they can be useful in this area. */
9001 11 : if (TREE_CODE (rname) != FUNCTION_DECL
9002 11 : || !fndecl_built_in_p (rname))
9003 1 : warning_at (location, OPT_Wscalar_storage_order,
9004 : "passing argument %d of %qE from incompatible "
9005 : "scalar storage order", parmnum, rname);
9006 : break;
9007 3 : case ic_assign:
9008 : /* Do not warn if the RHS is a call to a function that returns a
9009 : pointer that is not an alias. */
9010 3 : if (TREE_CODE (rhs) != CALL_EXPR
9011 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9012 5 : || !DECL_IS_MALLOC (t))
9013 1 : warning_at (location, OPT_Wscalar_storage_order,
9014 : "assignment to %qT from pointer type %qT with "
9015 : "incompatible scalar storage order", type, rhstype);
9016 : break;
9017 3 : case ic_init:
9018 3 : case ic_init_const:
9019 : /* Likewise. */
9020 3 : if (TREE_CODE (rhs) != CALL_EXPR
9021 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9022 5 : || !DECL_IS_MALLOC (t))
9023 1 : warning_at (location, OPT_Wscalar_storage_order,
9024 : "initialization of %qT from pointer type %qT with "
9025 : "incompatible scalar storage order", type, rhstype);
9026 : break;
9027 1 : case ic_return:
9028 1 : warning_at (location, OPT_Wscalar_storage_order,
9029 : "returning %qT from pointer type with incompatible "
9030 : "scalar storage order %qT", rhstype, type);
9031 1 : break;
9032 0 : default:
9033 0 : gcc_unreachable ();
9034 : }
9035 : }
9036 :
9037 : /* Any non-function converts to a [const][volatile] void *
9038 : and vice versa; otherwise, targets must be the same.
9039 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
9040 420481 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
9041 1609165 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
9042 1560382 : || (target_cmp = comp_target_types (location, type, rhstype))
9043 2483 : || is_opaque_pointer
9044 2034611 : || ((c_common_unsigned_type (mvl)
9045 2483 : == c_common_unsigned_type (mvr))
9046 3412 : && (c_common_signed_type (mvl)
9047 1706 : == c_common_signed_type (mvr))
9048 1691 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
9049 : {
9050 : /* Warn about loss of qualifers from pointers to arrays with
9051 : qualifiers on the element type. */
9052 2028845 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9053 : {
9054 2131 : ttr = strip_array_types (ttr);
9055 2131 : ttl = strip_array_types (ttl);
9056 :
9057 2131 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9058 2131 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9059 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9060 : location, expr_loc,
9061 : OPT_Wdiscarded_array_qualifiers,
9062 : G_("passing argument %d of %qE discards "
9063 : "%qv qualifier from pointer target type"),
9064 : G_("assignment discards %qv qualifier "
9065 : "from pointer target type"),
9066 : G_("initialization discards %qv qualifier "
9067 : "from pointer target type"),
9068 : G_("return discards %qv qualifier from "
9069 : "pointer target type"),
9070 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9071 : }
9072 2026714 : else if (pedantic
9073 146622 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9074 146602 : ||
9075 : (VOID_TYPE_P (ttr)
9076 2923 : && !null_pointer_constant
9077 184 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9078 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9079 : G_("ISO C forbids passing argument %d of "
9080 : "%qE between function pointer "
9081 : "and %<void *%>"),
9082 : G_("ISO C forbids assignment between "
9083 : "function pointer and %<void *%>"),
9084 : G_("ISO C forbids initialization between "
9085 : "function pointer and %<void *%>"),
9086 : G_("ISO C forbids return between function "
9087 : "pointer and %<void *%>"));
9088 : /* Const and volatile mean something different for function types,
9089 : so the usual warnings are not appropriate. */
9090 2026680 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9091 1994249 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9092 : {
9093 : /* Assignments between atomic and non-atomic objects are OK. */
9094 1993510 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9095 1993510 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9096 1993510 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9097 1993510 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9098 :
9099 : /* Don't warn about loss of qualifier for conversions from
9100 : qualified void* to pointers to arrays with corresponding
9101 : qualifier on the element type (except for pedantic before C23). */
9102 1993510 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9103 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9104 : OPT_Wdiscarded_qualifiers,
9105 : G_("passing argument %d of %qE discards "
9106 : "%qv qualifier from pointer target type"),
9107 : G_("assignment discards %qv qualifier "
9108 : "from pointer target type"),
9109 : G_("initialization discards %qv qualifier "
9110 : "from pointer target type"),
9111 : G_("return discards %qv qualifier from "
9112 : "pointer target type"),
9113 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9114 11 : else if (warn_quals_ped)
9115 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9116 : "array with qualifier on the element is not qualified before C23");
9117 :
9118 : /* If this is not a case of ignoring a mismatch in signedness,
9119 : no warning. */
9120 1992802 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9121 1555462 : || target_cmp)
9122 : ;
9123 : /* If there is a mismatch, do warn. */
9124 1683 : else if (warn_pointer_sign)
9125 82 : switch (errtype)
9126 : {
9127 33 : case ic_argpass:
9128 33 : {
9129 33 : auto_diagnostic_group d;
9130 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9131 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9132 33 : highlight_colors::actual);
9133 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9134 : "pointer targets in passing argument %d of "
9135 : "%qE differ in signedness", parmnum, rname))
9136 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9137 : rhstype);
9138 33 : }
9139 33 : break;
9140 21 : case ic_assign:
9141 21 : pedwarn (location, OPT_Wpointer_sign,
9142 : "pointer targets in assignment from %qT to %qT "
9143 : "differ in signedness", rhstype, type);
9144 21 : break;
9145 14 : case ic_init:
9146 14 : case ic_init_const:
9147 14 : pedwarn_init (location, OPT_Wpointer_sign,
9148 : "pointer targets in initialization of %qT "
9149 : "from %qT differ in signedness", type,
9150 : rhstype);
9151 14 : break;
9152 14 : case ic_return:
9153 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9154 : "returning %qT from a function with return type "
9155 : "%qT differ in signedness", rhstype, type);
9156 14 : break;
9157 0 : default:
9158 0 : gcc_unreachable ();
9159 : }
9160 : }
9161 33170 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9162 4188 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9163 : {
9164 : /* Because const and volatile on functions are restrictions
9165 : that say the function will not do certain things,
9166 : it is okay to use a const or volatile function
9167 : where an ordinary one is wanted, but not vice-versa. */
9168 3449 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9169 3449 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9170 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9171 : OPT_Wdiscarded_qualifiers,
9172 : G_("passing argument %d of %qE makes "
9173 : "%q#v qualified function pointer "
9174 : "from unqualified"),
9175 : G_("assignment makes %q#v qualified function "
9176 : "pointer from unqualified"),
9177 : G_("initialization makes %q#v qualified "
9178 : "function pointer from unqualified"),
9179 : G_("return makes %q#v qualified function "
9180 : "pointer from unqualified"),
9181 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
9182 : }
9183 : }
9184 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9185 800 : else if (!objc_ok)
9186 : {
9187 800 : auto_diagnostic_group d;
9188 800 : bool warned = false;
9189 800 : pp_markup::element_expected_type e_type (type);
9190 800 : pp_markup::element_actual_type e_rhstype (rhstype);
9191 800 : switch (errtype)
9192 : {
9193 307 : case ic_argpass:
9194 307 : {
9195 307 : range_label_for_type_mismatch rhs_label (rhstype, type);
9196 307 : gcc_rich_location richloc (expr_loc, &rhs_label,
9197 307 : highlight_colors::actual);
9198 307 : warned
9199 307 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9200 : "passing argument %d of %qE from "
9201 : "incompatible pointer type",
9202 : parmnum, rname);
9203 307 : if (warned)
9204 175 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9205 307 : }
9206 307 : break;
9207 267 : case ic_assign:
9208 267 : if (bltin)
9209 11 : warned
9210 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9211 : "assignment to %e from pointer to "
9212 : "%qD with incompatible type %e",
9213 : &e_type, bltin, &e_rhstype);
9214 : else
9215 256 : warned
9216 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9217 : "assignment to %e from incompatible "
9218 : "pointer type %e",
9219 : &e_type, &e_rhstype);
9220 : break;
9221 167 : case ic_init:
9222 167 : case ic_init_const:
9223 167 : if (bltin)
9224 13 : warned
9225 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9226 : "initialization of %e from pointer to "
9227 : "%qD with incompatible type %e",
9228 : &e_type, bltin, &e_rhstype);
9229 : else
9230 154 : warned
9231 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9232 : "initialization of %e from incompatible "
9233 : "pointer type %e",
9234 : &e_type, &e_rhstype);
9235 : break;
9236 59 : case ic_return:
9237 59 : if (bltin)
9238 11 : warned
9239 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9240 : "returning pointer to %qD of type %e from "
9241 : "a function with incompatible type %e",
9242 : bltin, &e_rhstype, &e_type);
9243 : else
9244 48 : warned
9245 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9246 : "returning %e from a function with "
9247 : "incompatible return type %e",
9248 : &e_rhstype, &e_type);
9249 : break;
9250 0 : default:
9251 0 : gcc_unreachable ();
9252 : }
9253 800 : if (warned)
9254 : {
9255 : /* If the mismatching function type is a pointer to a function,
9256 : try to show the decl of the function. */
9257 590 : if (TREE_CODE (rhs) == ADDR_EXPR
9258 590 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9259 : {
9260 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9261 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9262 : {
9263 165 : gcc_rich_location richloc
9264 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9265 165 : highlight_colors::actual);
9266 165 : pp_element_quoted_decl e_rhs_fndecl
9267 165 : (rhs_fndecl, highlight_colors::actual);
9268 165 : inform (&richloc,
9269 : "%e declared here", &e_rhs_fndecl);
9270 165 : }
9271 : }
9272 : /* If either/both of the types are typedefs, show the decl. */
9273 590 : maybe_inform_typedef_location (type,
9274 : highlight_colors::expected);
9275 590 : maybe_inform_typedef_location (rhstype,
9276 : highlight_colors::actual);
9277 : }
9278 800 : }
9279 :
9280 : /* If RHS isn't an address, check pointer or array of packed
9281 : struct or union. */
9282 2029645 : warn_for_address_of_packed_member (type, orig_rhs);
9283 :
9284 2029645 : return convert (type, rhs);
9285 : }
9286 79889 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9287 : {
9288 : /* ??? This should not be an error when inlining calls to
9289 : unprototyped functions. */
9290 4 : const char msg[] = "invalid use of non-lvalue array";
9291 4 : if (warnopt)
9292 0 : warning_at (location, warnopt, msg);
9293 : else
9294 4 : error_at (location, msg);
9295 4 : return error_mark_node;
9296 : }
9297 79885 : else if (codel == POINTER_TYPE
9298 78355 : && (coder == INTEGER_TYPE
9299 78355 : || coder == ENUMERAL_TYPE
9300 616 : || coder == BOOLEAN_TYPE
9301 616 : || coder == NULLPTR_TYPE
9302 27 : || coder == BITINT_TYPE))
9303 : {
9304 78342 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9305 77344 : && coder != NULLPTR_TYPE)
9306 76784 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9307 : "zero as null pointer constant");
9308 : /* A NULLPTR type is just a nullptr always. */
9309 77782 : if (coder == NULLPTR_TYPE)
9310 575 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9311 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9312 : or one that results from arithmetic, even including a cast to
9313 : integer type. */
9314 77767 : else if (!null_pointer_constant)
9315 972 : switch (errtype)
9316 : {
9317 800 : case ic_argpass:
9318 800 : {
9319 800 : auto_diagnostic_group d;
9320 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9321 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9322 800 : highlight_colors::actual);
9323 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9324 : "passing argument %d of %qE makes pointer "
9325 : "from integer without a cast", parmnum, rname))
9326 : {
9327 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9328 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9329 : }
9330 800 : }
9331 800 : break;
9332 93 : case ic_assign:
9333 93 : permerror_opt (location, OPT_Wint_conversion,
9334 : "assignment to %qT from %qT makes pointer from "
9335 : "integer without a cast", type, rhstype);
9336 93 : break;
9337 56 : case ic_init:
9338 56 : case ic_init_const:
9339 56 : permerror_init (location, OPT_Wint_conversion,
9340 : "initialization of %qT from %qT makes pointer "
9341 : "from integer without a cast", type, rhstype);
9342 56 : break;
9343 23 : case ic_return:
9344 23 : permerror_init (location, OPT_Wint_conversion,
9345 : "returning %qT from a function with return type "
9346 : "%qT makes pointer from integer without a cast",
9347 : rhstype, type);
9348 23 : break;
9349 0 : default:
9350 0 : gcc_unreachable ();
9351 : }
9352 :
9353 77767 : return convert (type, rhs);
9354 : }
9355 1543 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9356 540 : && coder == POINTER_TYPE)
9357 : {
9358 514 : switch (errtype)
9359 : {
9360 344 : case ic_argpass:
9361 344 : {
9362 344 : auto_diagnostic_group d;
9363 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9364 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9365 344 : highlight_colors::actual);
9366 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9367 : "passing argument %d of %qE makes integer from "
9368 : "pointer without a cast", parmnum, rname))
9369 : {
9370 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9371 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9372 : }
9373 344 : }
9374 344 : break;
9375 52 : case ic_assign:
9376 52 : permerror_opt (location, OPT_Wint_conversion,
9377 : "assignment to %qT from %qT makes integer from "
9378 : "pointer without a cast", type, rhstype);
9379 52 : break;
9380 83 : case ic_init:
9381 83 : case ic_init_const:
9382 83 : permerror_init (location, OPT_Wint_conversion,
9383 : "initialization of %qT from %qT makes integer "
9384 : "from pointer without a cast", type, rhstype);
9385 83 : break;
9386 35 : case ic_return:
9387 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9388 : "function with return type %qT makes integer from "
9389 : "pointer without a cast", rhstype, type);
9390 35 : break;
9391 0 : default:
9392 0 : gcc_unreachable ();
9393 : }
9394 :
9395 514 : return convert (type, rhs);
9396 : }
9397 626 : else if (C_BOOLEAN_TYPE_P (type)
9398 : /* The type nullptr_t may be converted to bool. The
9399 : result is false. */
9400 1031 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9401 : {
9402 405 : tree ret;
9403 405 : bool save = in_late_binary_op;
9404 405 : in_late_binary_op = true;
9405 405 : ret = convert (type, rhs);
9406 405 : in_late_binary_op = save;
9407 405 : return ret;
9408 : }
9409 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9410 6 : return convert (type, rhs);
9411 :
9412 618 : switch (errtype)
9413 : {
9414 35 : case ic_argpass:
9415 35 : {
9416 35 : auto_diagnostic_group d;
9417 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9418 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9419 35 : highlight_colors::actual);
9420 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9421 35 : if (warnopt)
9422 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9423 : else
9424 27 : error_at (&richloc, msg, parmnum, rname);
9425 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9426 35 : }
9427 35 : break;
9428 108 : case ic_assign:
9429 108 : {
9430 108 : const char msg[]
9431 : = G_("incompatible types when assigning to type %qT from type %qT");
9432 108 : if (warnopt)
9433 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9434 : else
9435 108 : error_at (expr_loc, msg, type, rhstype);
9436 108 : break;
9437 : }
9438 460 : case ic_init:
9439 460 : case ic_init_const:
9440 460 : {
9441 460 : const char msg[]
9442 : = G_("incompatible types when initializing type %qT using type %qT");
9443 460 : if (warnopt)
9444 0 : warning_at (location, 0, msg, type, rhstype);
9445 : else
9446 460 : error_at (location, msg, type, rhstype);
9447 460 : break;
9448 : }
9449 15 : case ic_return:
9450 15 : {
9451 15 : const char msg[]
9452 : = G_("incompatible types when returning type %qT but %qT was expected");
9453 15 : if (warnopt)
9454 0 : warning_at (location, 0, msg, rhstype, type);
9455 : else
9456 15 : error_at (location, msg, rhstype, type);
9457 15 : break;
9458 : }
9459 0 : default:
9460 0 : gcc_unreachable ();
9461 : }
9462 :
9463 618 : return error_mark_node;
9464 : }
9465 :
9466 : /* If VALUE is a compound expr all of whose expressions are constant, then
9467 : return its value. Otherwise, return error_mark_node.
9468 :
9469 : This is for handling COMPOUND_EXPRs as initializer elements
9470 : which is allowed with a warning when -pedantic is specified. */
9471 :
9472 : static tree
9473 2 : valid_compound_expr_initializer (tree value, tree endtype)
9474 : {
9475 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9476 : {
9477 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9478 1 : == error_mark_node)
9479 : return error_mark_node;
9480 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9481 0 : endtype);
9482 : }
9483 1 : else if (!initializer_constant_valid_p (value, endtype))
9484 1 : return error_mark_node;
9485 : else
9486 : return value;
9487 : }
9488 :
9489 : /* Perform appropriate conversions on the initial value of a variable,
9490 : store it in the declaration DECL,
9491 : and print any error messages that are appropriate.
9492 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9493 : If the init is invalid, store an ERROR_MARK.
9494 :
9495 : INIT_LOC is the location of the initial value. */
9496 :
9497 : void
9498 7303429 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9499 : {
9500 7303429 : tree value, type;
9501 7303429 : bool npc = false;
9502 7303429 : bool int_const_expr = false;
9503 7303429 : bool arith_const_expr = false;
9504 :
9505 : /* If variable's type was invalidly declared, just ignore it. */
9506 :
9507 7303429 : type = TREE_TYPE (decl);
9508 7303429 : if (TREE_CODE (type) == ERROR_MARK)
9509 : return;
9510 :
9511 : /* Digest the specified initializer into an expression. */
9512 :
9513 7303421 : if (init)
9514 : {
9515 7303418 : npc = null_pointer_constant_p (init);
9516 7303418 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9517 422409 : && !TREE_OVERFLOW (init)
9518 7725791 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9519 : /* Not fully determined before folding. */
9520 : arith_const_expr = true;
9521 : }
9522 7303421 : bool constexpr_p = (VAR_P (decl)
9523 7303421 : && C_DECL_DECLARED_CONSTEXPR (decl));
9524 7303421 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9525 : int_const_expr, arith_const_expr, true,
9526 7303421 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9527 :
9528 : /* Store the expression if valid; else report error. */
9529 :
9530 7303421 : if (!in_system_header_at (input_location)
9531 7303421 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9532 29067 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9533 : "aggregate initialization");
9534 :
9535 7303421 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9536 7303420 : DECL_INITIAL (decl) = value;
9537 :
9538 : /* ANSI wants warnings about out-of-range constant initializers. */
9539 7303432 : STRIP_TYPE_NOPS (value);
9540 7303421 : if (TREE_STATIC (decl))
9541 181258 : constant_expression_warning (value);
9542 :
9543 : /* Check if we need to set array size from compound literal size. */
9544 7303421 : if (TREE_CODE (type) == ARRAY_TYPE
9545 26711 : && TYPE_DOMAIN (type) == NULL_TREE
9546 7316069 : && value != error_mark_node)
9547 : {
9548 : tree inside_init = init;
9549 :
9550 11776 : STRIP_TYPE_NOPS (inside_init);
9551 11776 : inside_init = fold (inside_init);
9552 :
9553 11776 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9554 : {
9555 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9556 :
9557 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9558 : {
9559 : /* For int foo[] = (int [3]){1}; we need to set array size
9560 : now since later on array initializer will be just the
9561 : brace enclosed list of the compound literal. */
9562 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9563 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9564 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9565 13 : layout_type (type);
9566 13 : layout_decl (cldecl, 0);
9567 13 : TREE_TYPE (decl)
9568 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9569 : }
9570 : }
9571 : }
9572 : }
9573 :
9574 : /* Methods for storing and printing names for error messages. */
9575 :
9576 : /* Implement a spelling stack that allows components of a name to be pushed
9577 : and popped. Each element on the stack is this structure. */
9578 :
9579 : struct spelling
9580 : {
9581 : int kind;
9582 : union
9583 : {
9584 : unsigned HOST_WIDE_INT i;
9585 : const char *s;
9586 : } u;
9587 : };
9588 :
9589 : #define SPELLING_STRING 1
9590 : #define SPELLING_MEMBER 2
9591 : #define SPELLING_BOUNDS 3
9592 :
9593 : static struct spelling *spelling; /* Next stack element (unused). */
9594 : static struct spelling *spelling_base; /* Spelling stack base. */
9595 : static int spelling_size; /* Size of the spelling stack. */
9596 :
9597 : /* Macros to save and restore the spelling stack around push_... functions.
9598 : Alternative to SAVE_SPELLING_STACK. */
9599 :
9600 : #define SPELLING_DEPTH() (spelling - spelling_base)
9601 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9602 :
9603 : /* Push an element on the spelling stack with type KIND and assign VALUE
9604 : to MEMBER. */
9605 :
9606 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9607 : { \
9608 : int depth = SPELLING_DEPTH (); \
9609 : \
9610 : if (depth >= spelling_size) \
9611 : { \
9612 : spelling_size += 10; \
9613 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9614 : spelling_size); \
9615 : RESTORE_SPELLING_DEPTH (depth); \
9616 : } \
9617 : \
9618 : spelling->kind = (KIND); \
9619 : spelling->MEMBER = (VALUE); \
9620 : spelling++; \
9621 : }
9622 :
9623 : /* Push STRING on the stack. Printed literally. */
9624 :
9625 : static void
9626 7300631 : push_string (const char *string)
9627 : {
9628 7300631 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9629 7300631 : }
9630 :
9631 : /* Push a member name on the stack. Printed as '.' STRING. */
9632 :
9633 : static void
9634 1231121 : push_member_name (tree decl)
9635 : {
9636 1231121 : const char *const string
9637 1231121 : = (DECL_NAME (decl)
9638 1231121 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9639 210 : : _("<anonymous>"));
9640 1231121 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9641 1231121 : }
9642 :
9643 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9644 :
9645 : static void
9646 2606204 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9647 : {
9648 2606204 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9649 2606204 : }
9650 :
9651 : /* Compute the maximum size in bytes of the printed spelling. */
9652 :
9653 : static int
9654 2892 : spelling_length (void)
9655 : {
9656 2892 : int size = 0;
9657 2892 : struct spelling *p;
9658 :
9659 6416 : for (p = spelling_base; p < spelling; p++)
9660 : {
9661 3524 : if (p->kind == SPELLING_BOUNDS)
9662 1479 : size += 25;
9663 : else
9664 2045 : size += strlen (p->u.s) + 1;
9665 : }
9666 :
9667 2892 : return size;
9668 : }
9669 :
9670 : /* Print the spelling to BUFFER and return it. */
9671 :
9672 : static char *
9673 2892 : print_spelling (char *buffer)
9674 : {
9675 2892 : char *d = buffer;
9676 2892 : struct spelling *p;
9677 :
9678 6416 : for (p = spelling_base; p < spelling; p++)
9679 3524 : if (p->kind == SPELLING_BOUNDS)
9680 : {
9681 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9682 1479 : d += strlen (d);
9683 : }
9684 : else
9685 : {
9686 2045 : const char *s;
9687 2045 : if (p->kind == SPELLING_MEMBER)
9688 198 : *d++ = '.';
9689 16715 : for (s = p->u.s; (*d = *s++); d++)
9690 : ;
9691 : }
9692 2892 : *d++ = '\0';
9693 2892 : return buffer;
9694 : }
9695 :
9696 : /* Check whether INIT, a floating or integer constant, is
9697 : representable in TYPE, a real floating type with the same radix or
9698 : a decimal floating type initialized with a binary floating
9699 : constant. Return true if OK, false if not. */
9700 : static bool
9701 362 : constexpr_init_fits_real_type (tree type, tree init)
9702 : {
9703 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9704 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9705 362 : if (TREE_CODE (init) == REAL_CST
9706 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9707 : {
9708 : /* Same mode, no conversion required except for the case of
9709 : signaling NaNs if the types are incompatible (e.g. double and
9710 : long double with the same mode). */
9711 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9712 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9713 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9714 0 : return false;
9715 : return true;
9716 : }
9717 185 : if (TREE_CODE (init) == INTEGER_CST)
9718 : {
9719 54 : tree converted = build_real_from_int_cst (type, init);
9720 54 : bool fail = false;
9721 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9722 54 : TYPE_PRECISION (TREE_TYPE (init)));
9723 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9724 54 : }
9725 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9726 : return false;
9727 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9728 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9729 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9730 44 : && MODE_HAS_NANS (TYPE_MODE (type))))
9731 : return true;
9732 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9733 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9734 : {
9735 : /* This is valid if the real number represented by the
9736 : initializer can be exactly represented in the decimal
9737 : type. Compare the values using MPFR. */
9738 8 : REAL_VALUE_TYPE t;
9739 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9740 8 : mpfr_t bin_val, dec_val;
9741 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9742 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9743 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9744 8 : char string[256];
9745 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9746 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9747 8 : && mpfr_equal_p (bin_val, dec_val));
9748 8 : mpfr_clear (bin_val);
9749 8 : mpfr_clear (dec_val);
9750 8 : return res;
9751 : }
9752 : /* exact_real_truncate is not quite right here, since it doesn't
9753 : allow even an exact conversion to subnormal values. */
9754 86 : REAL_VALUE_TYPE t;
9755 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9756 86 : return real_identical (&t, &TREE_REAL_CST (init));
9757 : }
9758 :
9759 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9760 : initializer for type TYPE, and give an error if not. INIT has
9761 : already been folded and verified to be constant. INT_CONST_EXPR
9762 : and ARITH_CONST_EXPR say whether it is an integer constant
9763 : expression or arithmetic constant expression, respectively. If
9764 : TYPE is not a scalar type, this function does nothing. */
9765 :
9766 : static void
9767 920 : check_constexpr_init (location_t loc, tree type, tree init,
9768 : bool int_const_expr, bool arith_const_expr)
9769 : {
9770 920 : if (POINTER_TYPE_P (type))
9771 : {
9772 : /* The initializer must be null. */
9773 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9774 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9775 : return;
9776 : }
9777 834 : if (INTEGRAL_TYPE_P (type))
9778 : {
9779 : /* The initializer must be an integer constant expression,
9780 : representable in the target type. */
9781 324 : if (!int_const_expr)
9782 : {
9783 13 : if (TREE_CODE (init) == RAW_DATA_CST
9784 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9785 : {
9786 4 : if (!TYPE_UNSIGNED (type))
9787 137 : for (unsigned int i = 0;
9788 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9789 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9790 : {
9791 1 : error_at (loc, "%<constexpr%> initializer not "
9792 : "representable in type of object");
9793 1 : break;
9794 : }
9795 : }
9796 : else
9797 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9798 : "integer constant expression");
9799 : }
9800 311 : else if (!int_fits_type_p (init, type))
9801 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9802 : "type of object");
9803 : return;
9804 : }
9805 : /* We don't apply any extra checks to extension types such as vector
9806 : or fixed-point types. */
9807 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9808 : return;
9809 353 : if (!arith_const_expr)
9810 : {
9811 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9812 : "constant expression");
9813 5 : return;
9814 : }
9815 : /* We don't apply any extra checks to complex integers. */
9816 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9817 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9818 : return;
9819 : /* Following N3082, a real type cannot be initialized from a complex
9820 : type and a binary type cannot be initialized from a decimal type
9821 : (but initializing a decimal type from a binary type is OK).
9822 : Signaling NaN initializers are OK only if the types are
9823 : compatible (not just the same mode); all quiet NaN and infinity
9824 : initializations are considered to preserve the value. */
9825 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9826 348 : && SCALAR_FLOAT_TYPE_P (type))
9827 : {
9828 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9829 : "complex type");
9830 6 : return;
9831 : }
9832 342 : if (SCALAR_FLOAT_TYPE_P (type)
9833 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9834 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9835 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9836 : {
9837 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9838 : "floating-point type is of decimal type");
9839 6 : return;
9840 : }
9841 336 : bool fits;
9842 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9843 : {
9844 42 : switch (TREE_CODE (init))
9845 : {
9846 10 : case INTEGER_CST:
9847 10 : case REAL_CST:
9848 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9849 10 : break;
9850 32 : case COMPLEX_CST:
9851 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9852 32 : TREE_REALPART (init))
9853 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9854 26 : TREE_IMAGPART (init)));
9855 : break;
9856 0 : default:
9857 0 : gcc_unreachable ();
9858 : }
9859 : }
9860 : else
9861 294 : fits = constexpr_init_fits_real_type (type, init);
9862 304 : if (!fits)
9863 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9864 : "type of object");
9865 : }
9866 :
9867 : /* Digest the parser output INIT as an initializer for type TYPE
9868 : initializing DECL.
9869 : Return a C expression of type TYPE to represent the initial value.
9870 :
9871 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9872 :
9873 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9874 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9875 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9876 : constant expression, false if it has already been determined in the
9877 : caller that it is not (but folding may have made the value passed here
9878 : indistinguishable from an arithmetic constant expression).
9879 :
9880 : If INIT is a string constant, STRICT_STRING is true if it is
9881 : unparenthesized or we should not warn here for it being parenthesized.
9882 : For other types of INIT, STRICT_STRING is not used.
9883 :
9884 : INIT_LOC is the location of the INIT.
9885 :
9886 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9887 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9888 : on initializers for 'constexpr' objects apply. */
9889 :
9890 : static tree
9891 16967597 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9892 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9893 : bool arith_const_expr, bool strict_string,
9894 : bool require_constant, bool require_constexpr)
9895 : {
9896 16967597 : enum tree_code code = TREE_CODE (type);
9897 16967597 : tree inside_init = init;
9898 16967597 : tree semantic_type = NULL_TREE;
9899 16967597 : bool maybe_const = true;
9900 :
9901 16967597 : if (type == error_mark_node
9902 16967597 : || !init
9903 33935191 : || error_operand_p (init))
9904 : return error_mark_node;
9905 :
9906 16975049 : STRIP_TYPE_NOPS (inside_init);
9907 :
9908 : /* If require_constant is TRUE, when the initializer is a call to
9909 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9910 : For example:
9911 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9912 : will be converted to
9913 : y = &static_annotated.c. */
9914 :
9915 16966474 : if (require_constant
9916 2896199 : && TREE_CODE (inside_init) == NOP_EXPR
9917 736267 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9918 16966476 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9919 2 : inside_init
9920 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9921 :
9922 16966474 : if (!c_in_omp_for)
9923 : {
9924 16961720 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9925 : {
9926 441 : semantic_type = TREE_TYPE (inside_init);
9927 441 : inside_init = TREE_OPERAND (inside_init, 0);
9928 : }
9929 16961720 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9930 : }
9931 : /* TODO: this may not detect all cases of expressions folding to
9932 : constants that are not arithmetic constant expressions. */
9933 16966474 : if (!maybe_const)
9934 : arith_const_expr = false;
9935 25314619 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9936 5676798 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9937 16893828 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9938 : arith_const_expr = false;
9939 8449457 : else if (TREE_CODE (inside_init) != INTEGER_CST
9940 : && TREE_CODE (inside_init) != REAL_CST
9941 : && TREE_CODE (inside_init) != COMPLEX_CST
9942 : && TREE_CODE (inside_init) != RAW_DATA_CST)
9943 : arith_const_expr = false;
9944 5097139 : else if (TREE_OVERFLOW (inside_init))
9945 11869379 : arith_const_expr = false;
9946 :
9947 : /* Initialization of an array of chars from a string constant
9948 : optionally enclosed in braces. */
9949 :
9950 16966474 : if (code == ARRAY_TYPE && inside_init
9951 189669 : && TREE_CODE (inside_init) == STRING_CST)
9952 : {
9953 11803 : tree typ1
9954 11803 : = (TYPE_ATOMIC (TREE_TYPE (type))
9955 11803 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9956 : TYPE_QUAL_ATOMIC)
9957 11789 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9958 : /* Note that an array could be both an array of character type
9959 : and an array of wchar_t if wchar_t is signed char or unsigned
9960 : char. */
9961 23606 : bool char_array = (typ1 == char_type_node
9962 516 : || typ1 == signed_char_type_node
9963 12284 : || typ1 == unsigned_char_type_node);
9964 11803 : bool wchar_array = comptypes (typ1, wchar_type_node);
9965 11803 : bool char16_array = comptypes (typ1, char16_type_node);
9966 11803 : bool char32_array = comptypes (typ1, char32_type_node);
9967 :
9968 11803 : if (char_array || wchar_array || char16_array || char32_array)
9969 : {
9970 11774 : struct c_expr expr;
9971 11774 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9972 11774 : bool incompat_string_cst = false;
9973 11774 : expr.value = inside_init;
9974 11774 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9975 11774 : expr.original_type = NULL;
9976 11774 : expr.m_decimal = 0;
9977 11774 : maybe_warn_string_init (init_loc, type, expr);
9978 :
9979 11774 : if (flexible_array_member_type_p (type))
9980 61 : pedwarn_init (init_loc, OPT_Wpedantic,
9981 : "initialization of a flexible array member");
9982 :
9983 11774 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9984 11774 : TYPE_MAIN_VARIANT (type)))
9985 : return inside_init;
9986 :
9987 4186 : if (char_array)
9988 : {
9989 4075 : if (typ2 != char_type_node && typ2 != char8_type_node)
9990 : incompat_string_cst = true;
9991 : }
9992 111 : else if (!comptypes (typ1, typ2))
9993 : incompat_string_cst = true;
9994 :
9995 : if (incompat_string_cst)
9996 : {
9997 72 : error_init (init_loc, "cannot initialize array of %qT from "
9998 : "a string literal with type array of %qT",
9999 : typ1, typ2);
10000 72 : return error_mark_node;
10001 : }
10002 :
10003 4114 : if (require_constexpr
10004 4114 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
10005 : {
10006 : /* Check if all characters of the string can be
10007 : represented in the type of the constexpr object being
10008 : initialized. */
10009 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10010 24 : const unsigned char *p =
10011 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
10012 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
10013 70 : for (unsigned i = 0; i < len; i++)
10014 58 : if (p[i] > 127)
10015 : {
10016 12 : error_init (init_loc, "%<constexpr%> initializer not "
10017 : "representable in type of object");
10018 12 : break;
10019 : }
10020 : }
10021 :
10022 4114 : if (TYPE_DOMAIN (type) != NULL_TREE
10023 4020 : && TYPE_SIZE (type) != NULL_TREE
10024 8075 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10025 : {
10026 3961 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10027 :
10028 3961 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
10029 : {
10030 397 : unsigned HOST_WIDE_INT avail
10031 397 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10032 397 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
10033 397 : const char *p = TREE_STRING_POINTER (inside_init);
10034 :
10035 : /* Construct truncated string. */
10036 397 : inside_init = build_string (avail, p);
10037 :
10038 : /* Subtract the size of a single (possibly wide) character
10039 : because it may be ok to ignore the terminating NUL char
10040 : that is counted in the length of the constant. */
10041 397 : if (len - unit > avail)
10042 63 : pedwarn_init (init_loc, 0,
10043 : "initializer-string for array of %qT "
10044 : "is too long (%wu chars into %wu "
10045 : "available)", typ1, len, avail);
10046 334 : else if (warn_cxx_compat)
10047 9 : warning_at (init_loc, OPT_Wc___compat,
10048 : "initializer-string for array of %qT "
10049 : "is too long for C++ (%wu chars into %wu "
10050 : "available)", typ1, len, avail);
10051 325 : else if (warn_unterminated_string_initialization
10052 325 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10053 22 : warning_at (init_loc,
10054 22 : OPT_Wunterminated_string_initialization,
10055 : "initializer-string for array of %qT "
10056 : "truncates NUL terminator but destination "
10057 : "lacks %qs attribute (%wu chars into %wu "
10058 : "available)", typ1, "nonstring", len, avail);
10059 : }
10060 : }
10061 :
10062 4114 : TREE_TYPE (inside_init) = type;
10063 4114 : return inside_init;
10064 : }
10065 29 : else if (INTEGRAL_TYPE_P (typ1))
10066 : {
10067 29 : error_init (init_loc, "array of inappropriate type initialized "
10068 : "from string constant");
10069 29 : return error_mark_node;
10070 : }
10071 : }
10072 :
10073 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10074 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10075 : below and handle as a constructor. */
10076 16954671 : if (code == VECTOR_TYPE
10077 6339923 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10078 6339921 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10079 23294554 : && TREE_CONSTANT (inside_init))
10080 : {
10081 635957 : if (TREE_CODE (inside_init) == VECTOR_CST
10082 636054 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10083 97 : TYPE_MAIN_VARIANT (type)))
10084 : return inside_init;
10085 :
10086 635860 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10087 : {
10088 : unsigned HOST_WIDE_INT ix;
10089 : tree value;
10090 4189999 : bool constant_p = true;
10091 :
10092 : /* Iterate through elements and check if all constructor
10093 : elements are *_CSTs. */
10094 4189999 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10095 3554195 : if (!CONSTANT_CLASS_P (value))
10096 : {
10097 : constant_p = false;
10098 : break;
10099 : }
10100 :
10101 635860 : if (constant_p)
10102 635804 : return build_vector_from_ctor (type,
10103 635804 : CONSTRUCTOR_ELTS (inside_init));
10104 : }
10105 : }
10106 :
10107 16318770 : if (warn_sequence_point)
10108 2449319 : verify_sequence_points (inside_init);
10109 :
10110 : /* Any type can be initialized
10111 : from an expression of the same type, optionally with braces. */
10112 :
10113 16318770 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10114 32637540 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10115 16318770 : TYPE_MAIN_VARIANT (type))
10116 3147741 : || (code == ARRAY_TYPE
10117 477 : && comptypes (TREE_TYPE (inside_init), type))
10118 3147741 : || (gnu_vector_type_p (type)
10119 189 : && comptypes (TREE_TYPE (inside_init), type))
10120 3147741 : || (code == POINTER_TYPE
10121 112754 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10122 9298 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10123 9298 : TREE_TYPE (type)))))
10124 : {
10125 13173007 : if (code == POINTER_TYPE)
10126 : {
10127 785351 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10128 : {
10129 1978 : if (TREE_CODE (inside_init) == STRING_CST
10130 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10131 1975 : inside_init = array_to_pointer_conversion
10132 1975 : (init_loc, inside_init);
10133 : else
10134 : {
10135 3 : error_init (init_loc, "invalid use of non-lvalue array");
10136 3 : return error_mark_node;
10137 : }
10138 : }
10139 : }
10140 :
10141 13173004 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10142 : /* Although the types are compatible, we may require a
10143 : conversion. */
10144 5703966 : inside_init = convert (type, inside_init);
10145 :
10146 13173004 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10147 13173004 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10148 : {
10149 0 : error_init (init_loc, "invalid initializer");
10150 0 : return error_mark_node;
10151 : }
10152 :
10153 13173004 : if (require_constant
10154 2299071 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10155 : {
10156 : /* As an extension, allow initializing objects with static storage
10157 : duration with compound literals (which are then treated just as
10158 : the brace enclosed list they contain). Also allow this for
10159 : vectors, as we can only assign them with compound literals. */
10160 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10161 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10162 : "is not constant");
10163 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10164 31 : inside_init = DECL_INITIAL (decl);
10165 : }
10166 :
10167 13173004 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10168 177389 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10169 : {
10170 2 : error_init (init_loc, "array initialized from non-constant array "
10171 : "expression");
10172 2 : return error_mark_node;
10173 : }
10174 :
10175 : /* Compound expressions can only occur here if -Wpedantic or
10176 : -pedantic-errors is specified. In the later case, we always want
10177 : an error. In the former case, we simply want a warning. */
10178 13173002 : if (require_constant && pedantic
10179 20351 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10180 : {
10181 1 : inside_init
10182 1 : = valid_compound_expr_initializer (inside_init,
10183 1 : TREE_TYPE (inside_init));
10184 1 : if (inside_init == error_mark_node)
10185 1 : error_init (init_loc, "initializer element is not constant");
10186 : else
10187 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10188 : "initializer element is not constant");
10189 1 : if (flag_pedantic_errors)
10190 0 : inside_init = error_mark_node;
10191 : }
10192 2299070 : else if (require_constant
10193 2299070 : && !initializer_constant_valid_p (inside_init,
10194 2299070 : TREE_TYPE (inside_init)))
10195 : {
10196 94 : error_init (init_loc, "initializer element is not constant");
10197 94 : inside_init = error_mark_node;
10198 : }
10199 13172907 : else if (require_constant && !maybe_const)
10200 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10201 : "initializer element is not a constant expression");
10202 13172688 : else if (require_constexpr)
10203 588 : check_constexpr_init (init_loc, type, inside_init,
10204 : int_const_expr, arith_const_expr);
10205 :
10206 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10207 13173002 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10208 854946 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10209 : type, inside_init, origtype,
10210 : (require_constant
10211 : ? ic_init_const
10212 : : ic_init), null_pointer_constant,
10213 : NULL_TREE, NULL_TREE, 0);
10214 13173002 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10215 258 : && c_inhibit_evaluation_warnings == 0
10216 258 : && warn_conversion
10217 1 : && !TYPE_UNSIGNED (type)
10218 13173003 : && TYPE_PRECISION (type) == CHAR_BIT)
10219 303 : for (unsigned int i = 0;
10220 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10221 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10222 10 : warning_at (init_loc, OPT_Wconversion,
10223 : "conversion from %qT to %qT changes value from "
10224 : "%qd to %qd",
10225 : integer_type_node, type,
10226 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10227 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10228 : return inside_init;
10229 : }
10230 :
10231 : /* Handle scalar types, including conversions. */
10232 :
10233 3145763 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10234 130217 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10235 11827 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10236 9764 : || code == BITINT_TYPE)
10237 : {
10238 3145275 : tree unconverted_init = inside_init;
10239 3145275 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10240 3145275 : && (TREE_CODE (init) == STRING_CST
10241 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10242 7321 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10243 3145275 : if (semantic_type)
10244 436 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10245 : inside_init);
10246 3145275 : inside_init
10247 5708259 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10248 : inside_init, origtype,
10249 : require_constant ? ic_init_const : ic_init,
10250 : null_pointer_constant, NULL_TREE, NULL_TREE,
10251 : 0);
10252 :
10253 : /* Check to see if we have already given an error message. */
10254 3145275 : if (inside_init == error_mark_node)
10255 : ;
10256 3144815 : else if (require_constant && !TREE_CONSTANT (inside_init))
10257 : {
10258 36 : error_init (init_loc, "initializer element is not constant");
10259 36 : inside_init = error_mark_node;
10260 : }
10261 3144779 : else if (require_constant
10262 3726642 : && !initializer_constant_valid_p (inside_init,
10263 581863 : TREE_TYPE (inside_init)))
10264 : {
10265 10 : error_init (init_loc, "initializer element is not computable at "
10266 : "load time");
10267 10 : inside_init = error_mark_node;
10268 : }
10269 3144769 : else if (require_constant && !maybe_const)
10270 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10271 : "initializer element is not a constant expression");
10272 3144764 : else if (require_constexpr)
10273 332 : check_constexpr_init (init_loc, type, unconverted_init,
10274 : int_const_expr, arith_const_expr);
10275 :
10276 : return inside_init;
10277 : }
10278 :
10279 : /* Come here only for records and arrays. */
10280 :
10281 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10282 : {
10283 0 : error_init (init_loc,
10284 : "variable-sized object may not be initialized except "
10285 : "with an empty initializer");
10286 0 : return error_mark_node;
10287 : }
10288 :
10289 488 : error_init (init_loc, "invalid initializer");
10290 488 : return error_mark_node;
10291 : }
10292 :
10293 : /* Handle initializers that use braces. */
10294 :
10295 : /* Type of object we are accumulating a constructor for.
10296 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10297 : static tree constructor_type;
10298 :
10299 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10300 : left to fill. */
10301 : static tree constructor_fields;
10302 :
10303 : /* For an ARRAY_TYPE, this is the specified index
10304 : at which to store the next element we get. */
10305 : static tree constructor_index;
10306 :
10307 : /* For an ARRAY_TYPE, this is the maximum index. */
10308 : static tree constructor_max_index;
10309 :
10310 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10311 : static tree constructor_unfilled_fields;
10312 :
10313 : /* For an ARRAY_TYPE, this is the index of the first element
10314 : not yet written out. */
10315 : static tree constructor_unfilled_index;
10316 :
10317 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10318 : This is so we can generate gaps between fields, when appropriate. */
10319 : static tree constructor_bit_index;
10320 :
10321 : /* If we are saving up the elements rather than allocating them,
10322 : this is the list of elements so far (in reverse order,
10323 : most recent first). */
10324 : static vec<constructor_elt, va_gc> *constructor_elements;
10325 :
10326 : /* 1 if constructor should be incrementally stored into a constructor chain,
10327 : 0 if all the elements should be kept in AVL tree. */
10328 : static int constructor_incremental;
10329 :
10330 : /* 1 if so far this constructor's elements are all compile-time constants. */
10331 : static int constructor_constant;
10332 :
10333 : /* 1 if so far this constructor's elements are all valid address constants. */
10334 : static int constructor_simple;
10335 :
10336 : /* 1 if this constructor has an element that cannot be part of a
10337 : constant expression. */
10338 : static int constructor_nonconst;
10339 :
10340 : /* 1 if this constructor is erroneous so far. */
10341 : static int constructor_erroneous;
10342 :
10343 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10344 : static int constructor_zeroinit;
10345 :
10346 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10347 : static bool constructor_zero_padding_bits;
10348 :
10349 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10350 : of braces are an error). */
10351 : static bool constructor_braced_scalar;
10352 :
10353 : /* Structure for managing pending initializer elements, organized as an
10354 : AVL tree. */
10355 :
10356 : struct init_node
10357 : {
10358 : struct init_node *left, *right;
10359 : struct init_node *parent;
10360 : int balance;
10361 : tree purpose;
10362 : tree value;
10363 : tree origtype;
10364 : };
10365 :
10366 : /* Tree of pending elements at this constructor level.
10367 : These are elements encountered out of order
10368 : which belong at places we haven't reached yet in actually
10369 : writing the output.
10370 : Will never hold tree nodes across GC runs. */
10371 : static struct init_node *constructor_pending_elts;
10372 :
10373 : /* The SPELLING_DEPTH of this constructor. */
10374 : static int constructor_depth;
10375 :
10376 : /* DECL node for which an initializer is being read.
10377 : 0 means we are reading a constructor expression
10378 : such as (struct foo) {...}. */
10379 : static tree constructor_decl;
10380 :
10381 : /* Nonzero if there were any member designators in this initializer. */
10382 : static int constructor_designated;
10383 :
10384 : /* Nesting depth of designator list. */
10385 : static int designator_depth;
10386 :
10387 : /* Nonzero if there were diagnosed errors in this designator list. */
10388 : static int designator_erroneous;
10389 :
10390 :
10391 : /* This stack has a level for each implicit or explicit level of
10392 : structuring in the initializer, including the outermost one. It
10393 : saves the values of most of the variables above. */
10394 :
10395 : struct constructor_range_stack;
10396 :
10397 : struct constructor_stack
10398 : {
10399 : struct constructor_stack *next;
10400 : tree type;
10401 : tree fields;
10402 : tree index;
10403 : tree max_index;
10404 : tree unfilled_index;
10405 : tree unfilled_fields;
10406 : tree bit_index;
10407 : vec<constructor_elt, va_gc> *elements;
10408 : struct init_node *pending_elts;
10409 : int offset;
10410 : int depth;
10411 : /* If value nonzero, this value should replace the entire
10412 : constructor at this level. */
10413 : struct c_expr replacement_value;
10414 : struct constructor_range_stack *range_stack;
10415 : char constant;
10416 : char simple;
10417 : char nonconst;
10418 : char implicit;
10419 : char erroneous;
10420 : char outer;
10421 : char incremental;
10422 : char designated;
10423 : bool zero_padding_bits;
10424 : bool braced_scalar;
10425 : int designator_depth;
10426 : };
10427 :
10428 : static struct constructor_stack *constructor_stack;
10429 :
10430 : /* This stack represents designators from some range designator up to
10431 : the last designator in the list. */
10432 :
10433 : struct constructor_range_stack
10434 : {
10435 : struct constructor_range_stack *next, *prev;
10436 : struct constructor_stack *stack;
10437 : tree range_start;
10438 : tree index;
10439 : tree range_end;
10440 : tree fields;
10441 : };
10442 :
10443 : static struct constructor_range_stack *constructor_range_stack;
10444 :
10445 : /* This stack records separate initializers that are nested.
10446 : Nested initializers can't happen in ANSI C, but GNU C allows them
10447 : in cases like { ... (struct foo) { ... } ... }. */
10448 :
10449 : struct initializer_stack
10450 : {
10451 : struct initializer_stack *next;
10452 : tree decl;
10453 : struct constructor_stack *constructor_stack;
10454 : struct constructor_range_stack *constructor_range_stack;
10455 : vec<constructor_elt, va_gc> *elements;
10456 : struct spelling *spelling;
10457 : struct spelling *spelling_base;
10458 : int spelling_size;
10459 : char require_constant_value;
10460 : char require_constant_elements;
10461 : char require_constexpr_value;
10462 : char designated;
10463 : rich_location *missing_brace_richloc;
10464 : };
10465 :
10466 : static struct initializer_stack *initializer_stack;
10467 :
10468 : /* Prepare to parse and output the initializer for variable DECL. */
10469 :
10470 : void
10471 7300631 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10472 : bool init_require_constant, bool init_require_constexpr,
10473 : rich_location *richloc)
10474 : {
10475 7300631 : const char *locus;
10476 7300631 : struct initializer_stack *p = XNEW (struct initializer_stack);
10477 :
10478 7300631 : p->decl = constructor_decl;
10479 7300631 : p->require_constant_value = require_constant_value;
10480 7300631 : p->require_constant_elements = require_constant_elements;
10481 7300631 : p->require_constexpr_value = require_constexpr_value;
10482 7300631 : p->constructor_stack = constructor_stack;
10483 7300631 : p->constructor_range_stack = constructor_range_stack;
10484 7300631 : p->elements = constructor_elements;
10485 7300631 : p->spelling = spelling;
10486 7300631 : p->spelling_base = spelling_base;
10487 7300631 : p->spelling_size = spelling_size;
10488 7300631 : p->next = initializer_stack;
10489 7300631 : p->missing_brace_richloc = richloc;
10490 7300631 : p->designated = constructor_designated;
10491 7300631 : initializer_stack = p;
10492 :
10493 7300631 : constructor_decl = decl;
10494 7300631 : constructor_designated = 0;
10495 :
10496 7300631 : require_constant_value = init_require_constant;
10497 7300631 : require_constexpr_value = init_require_constexpr;
10498 7300631 : if (decl != NULL_TREE && decl != error_mark_node)
10499 : {
10500 6374664 : require_constant_elements
10501 6196620 : = ((init_require_constant || (pedantic && !flag_isoc99))
10502 : /* For a scalar, you can always use any value to initialize,
10503 : even within braces. */
10504 6387797 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10505 6374664 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10506 : }
10507 : else
10508 : {
10509 925967 : require_constant_elements = false;
10510 925967 : locus = _("(anonymous)");
10511 : }
10512 :
10513 7300631 : constructor_stack = 0;
10514 7300631 : constructor_range_stack = 0;
10515 :
10516 7300631 : found_missing_braces = 0;
10517 :
10518 7300631 : spelling_base = 0;
10519 7300631 : spelling_size = 0;
10520 7300631 : RESTORE_SPELLING_DEPTH (0);
10521 :
10522 7300631 : if (locus)
10523 7300631 : push_string (locus);
10524 7300631 : }
10525 :
10526 : void
10527 7300629 : finish_init (void)
10528 : {
10529 7300629 : struct initializer_stack *p = initializer_stack;
10530 :
10531 : /* Free the whole constructor stack of this initializer. */
10532 7300629 : while (constructor_stack)
10533 : {
10534 0 : struct constructor_stack *q = constructor_stack;
10535 0 : constructor_stack = q->next;
10536 0 : XDELETE (q);
10537 : }
10538 :
10539 7300629 : gcc_assert (!constructor_range_stack);
10540 :
10541 : /* Pop back to the data of the outer initializer (if any). */
10542 7300629 : XDELETE (spelling_base);
10543 :
10544 7300629 : constructor_decl = p->decl;
10545 7300629 : require_constant_value = p->require_constant_value;
10546 7300629 : require_constant_elements = p->require_constant_elements;
10547 7300629 : require_constexpr_value = p->require_constexpr_value;
10548 7300629 : constructor_stack = p->constructor_stack;
10549 7300629 : constructor_designated = p->designated;
10550 7300629 : constructor_range_stack = p->constructor_range_stack;
10551 7300629 : constructor_elements = p->elements;
10552 7300629 : spelling = p->spelling;
10553 7300629 : spelling_base = p->spelling_base;
10554 7300629 : spelling_size = p->spelling_size;
10555 7300629 : initializer_stack = p->next;
10556 7300629 : XDELETE (p);
10557 7300629 : }
10558 :
10559 : /* Call here when we see the initializer is surrounded by braces.
10560 : This is instead of a call to push_init_level;
10561 : it is matched by a call to pop_init_level.
10562 :
10563 : TYPE is the type to initialize, for a constructor expression.
10564 : For an initializer for a decl, TYPE is zero. */
10565 :
10566 : void
10567 1030661 : really_start_incremental_init (tree type)
10568 : {
10569 1030661 : struct constructor_stack *p = XNEW (struct constructor_stack);
10570 :
10571 1030661 : if (type == NULL_TREE)
10572 106662 : type = TREE_TYPE (constructor_decl);
10573 :
10574 1030661 : if (VECTOR_TYPE_P (type)
10575 1030661 : && TYPE_VECTOR_OPAQUE (type))
10576 0 : error ("opaque vector types cannot be initialized");
10577 :
10578 1030661 : p->type = constructor_type;
10579 1030661 : p->fields = constructor_fields;
10580 1030661 : p->index = constructor_index;
10581 1030661 : p->max_index = constructor_max_index;
10582 1030661 : p->unfilled_index = constructor_unfilled_index;
10583 1030661 : p->unfilled_fields = constructor_unfilled_fields;
10584 1030661 : p->bit_index = constructor_bit_index;
10585 1030661 : p->elements = constructor_elements;
10586 1030661 : p->constant = constructor_constant;
10587 1030661 : p->simple = constructor_simple;
10588 1030661 : p->nonconst = constructor_nonconst;
10589 1030661 : p->erroneous = constructor_erroneous;
10590 1030661 : p->pending_elts = constructor_pending_elts;
10591 1030661 : p->depth = constructor_depth;
10592 1030661 : p->replacement_value.value = 0;
10593 1030661 : p->replacement_value.original_code = ERROR_MARK;
10594 1030661 : p->replacement_value.original_type = NULL;
10595 1030661 : p->implicit = 0;
10596 1030661 : p->range_stack = 0;
10597 1030661 : p->outer = 0;
10598 1030661 : p->incremental = constructor_incremental;
10599 1030661 : p->designated = constructor_designated;
10600 1030661 : p->zero_padding_bits = constructor_zero_padding_bits;
10601 1030661 : p->braced_scalar = constructor_braced_scalar;
10602 1030661 : p->designator_depth = designator_depth;
10603 1030661 : p->next = 0;
10604 1030661 : constructor_stack = p;
10605 :
10606 1030661 : constructor_constant = 1;
10607 1030661 : constructor_simple = 1;
10608 1030661 : constructor_nonconst = 0;
10609 1030661 : constructor_depth = SPELLING_DEPTH ();
10610 1030661 : constructor_elements = NULL;
10611 1030661 : constructor_pending_elts = 0;
10612 1030661 : constructor_type = type;
10613 1030661 : constructor_incremental = 1;
10614 1030661 : constructor_designated = 0;
10615 1030661 : constructor_zero_padding_bits = false;
10616 1030661 : constructor_zeroinit = 1;
10617 1030661 : constructor_braced_scalar = false;
10618 1030661 : designator_depth = 0;
10619 1030661 : designator_erroneous = 0;
10620 :
10621 1030661 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10622 : {
10623 82290 : constructor_fields = TYPE_FIELDS (constructor_type);
10624 : /* Skip any nameless bit fields at the beginning. */
10625 82290 : while (constructor_fields != NULL_TREE
10626 82318 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10627 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10628 :
10629 82290 : constructor_unfilled_fields = constructor_fields;
10630 82290 : constructor_bit_index = bitsize_zero_node;
10631 : }
10632 948371 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10633 : {
10634 18153 : if (TYPE_DOMAIN (constructor_type))
10635 : {
10636 9596 : constructor_max_index
10637 9596 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10638 :
10639 : /* Detect non-empty initializations of zero-length arrays. */
10640 9596 : if (constructor_max_index == NULL_TREE
10641 9596 : && TYPE_SIZE (constructor_type))
10642 207 : constructor_max_index = integer_minus_one_node;
10643 :
10644 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10645 : to initialize VLAs with a nonempty initializer will cause a
10646 : proper error; avoid tree checking errors as well by setting a
10647 : safe value. */
10648 9596 : if (constructor_max_index
10649 9596 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10650 73 : constructor_max_index = integer_minus_one_node;
10651 :
10652 9596 : constructor_index
10653 9596 : = convert (bitsizetype,
10654 9596 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10655 : }
10656 : else
10657 : {
10658 8557 : constructor_index = bitsize_zero_node;
10659 8557 : constructor_max_index = NULL_TREE;
10660 : }
10661 :
10662 18153 : constructor_unfilled_index = constructor_index;
10663 : }
10664 930218 : else if (gnu_vector_type_p (constructor_type))
10665 : {
10666 : /* Vectors are like simple fixed-size arrays. */
10667 1859324 : constructor_max_index =
10668 929662 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10669 929662 : constructor_index = bitsize_zero_node;
10670 929662 : constructor_unfilled_index = constructor_index;
10671 : }
10672 : else
10673 : {
10674 : /* Handle the case of int x = {5}; */
10675 556 : constructor_fields = constructor_type;
10676 556 : constructor_unfilled_fields = constructor_type;
10677 556 : constructor_braced_scalar = true;
10678 : }
10679 1030661 : }
10680 :
10681 : extern location_t last_init_list_comma;
10682 :
10683 : /* Called when we see an open brace for a nested initializer. Finish
10684 : off any pending levels with implicit braces. */
10685 : void
10686 342429 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10687 : {
10688 342431 : while (constructor_stack->implicit)
10689 : {
10690 1154 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10691 208 : && constructor_fields == NULL_TREE)
10692 0 : process_init_element (input_location,
10693 : pop_init_level (loc, 1, braced_init_obstack,
10694 : last_init_list_comma),
10695 : true, braced_init_obstack);
10696 1154 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10697 946 : && constructor_max_index
10698 2100 : && tree_int_cst_lt (constructor_max_index,
10699 : constructor_index))
10700 2 : process_init_element (input_location,
10701 : pop_init_level (loc, 1, braced_init_obstack,
10702 : last_init_list_comma),
10703 : true, braced_init_obstack);
10704 : else
10705 : break;
10706 : }
10707 342429 : }
10708 :
10709 : /* Push down into a subobject, for initialization.
10710 : If this is for an explicit set of braces, IMPLICIT is 0.
10711 : If it is because the next element belongs at a lower level,
10712 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10713 :
10714 : void
10715 1045109 : push_init_level (location_t loc, int implicit,
10716 : struct obstack *braced_init_obstack)
10717 : {
10718 1045109 : struct constructor_stack *p;
10719 1045109 : tree value = NULL_TREE;
10720 :
10721 : /* Unless this is an explicit brace, we need to preserve previous
10722 : content if any. */
10723 1045109 : if (implicit)
10724 : {
10725 705323 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10726 1819 : value = find_init_member (constructor_fields, braced_init_obstack);
10727 703504 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10728 703504 : value = find_init_member (constructor_index, braced_init_obstack);
10729 : }
10730 :
10731 1045109 : p = XNEW (struct constructor_stack);
10732 1045109 : p->type = constructor_type;
10733 1045109 : p->fields = constructor_fields;
10734 1045109 : p->index = constructor_index;
10735 1045109 : p->max_index = constructor_max_index;
10736 1045109 : p->unfilled_index = constructor_unfilled_index;
10737 1045109 : p->unfilled_fields = constructor_unfilled_fields;
10738 1045109 : p->bit_index = constructor_bit_index;
10739 1045109 : p->elements = constructor_elements;
10740 1045109 : p->constant = constructor_constant;
10741 1045109 : p->simple = constructor_simple;
10742 1045109 : p->nonconst = constructor_nonconst;
10743 1045109 : p->erroneous = constructor_erroneous;
10744 1045109 : p->pending_elts = constructor_pending_elts;
10745 1045109 : p->depth = constructor_depth;
10746 1045109 : p->replacement_value.value = NULL_TREE;
10747 1045109 : p->replacement_value.original_code = ERROR_MARK;
10748 1045109 : p->replacement_value.original_type = NULL;
10749 1045109 : p->implicit = implicit;
10750 1045109 : p->outer = 0;
10751 1045109 : p->incremental = constructor_incremental;
10752 1045109 : p->designated = constructor_designated;
10753 1045109 : p->zero_padding_bits = constructor_zero_padding_bits;
10754 1045109 : p->braced_scalar = constructor_braced_scalar;
10755 1045109 : p->designator_depth = designator_depth;
10756 1045109 : p->next = constructor_stack;
10757 1045109 : p->range_stack = 0;
10758 1045109 : constructor_stack = p;
10759 :
10760 1045109 : constructor_constant = 1;
10761 1045109 : constructor_simple = 1;
10762 1045109 : constructor_nonconst = 0;
10763 1045109 : constructor_depth = SPELLING_DEPTH ();
10764 1045109 : constructor_elements = NULL;
10765 1045109 : constructor_incremental = 1;
10766 : /* If the upper initializer is designated, then mark this as
10767 : designated too to prevent bogus warnings. */
10768 1045109 : constructor_designated = p->designated;
10769 : /* If the upper initializer has padding bits zeroed, that includes
10770 : all nested initializers as well. */
10771 1045109 : constructor_zero_padding_bits = p->zero_padding_bits;
10772 1045109 : constructor_braced_scalar = false;
10773 1045109 : constructor_pending_elts = 0;
10774 1045109 : if (!implicit)
10775 : {
10776 339786 : p->range_stack = constructor_range_stack;
10777 339786 : constructor_range_stack = 0;
10778 339786 : designator_depth = 0;
10779 339786 : designator_erroneous = 0;
10780 : }
10781 :
10782 : /* Don't die if an entire brace-pair level is superfluous
10783 : in the containing level. */
10784 1045109 : if (constructor_type == NULL_TREE)
10785 : ;
10786 1045109 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10787 : {
10788 : /* Don't die if there are extra init elts at the end. */
10789 160057 : if (constructor_fields == NULL_TREE)
10790 51 : constructor_type = NULL_TREE;
10791 : else
10792 : {
10793 160006 : constructor_type = TREE_TYPE (constructor_fields);
10794 160006 : push_member_name (constructor_fields);
10795 160006 : constructor_depth++;
10796 : }
10797 : }
10798 885052 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10799 : {
10800 885001 : constructor_type = TREE_TYPE (constructor_type);
10801 885001 : push_array_bounds (tree_to_uhwi (constructor_index));
10802 885001 : constructor_depth++;
10803 : }
10804 :
10805 1045109 : if (constructor_type == NULL_TREE)
10806 : {
10807 51 : error_init (loc, "extra brace group at end of initializer");
10808 51 : constructor_fields = NULL_TREE;
10809 51 : constructor_unfilled_fields = NULL_TREE;
10810 51 : return;
10811 : }
10812 :
10813 1045058 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10814 : {
10815 1686 : constructor_constant = TREE_CONSTANT (value);
10816 1686 : constructor_simple = TREE_STATIC (value);
10817 1686 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10818 1686 : constructor_elements = CONSTRUCTOR_ELTS (value);
10819 1686 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10820 1686 : if (!vec_safe_is_empty (constructor_elements)
10821 1462 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10822 1462 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10823 1416 : set_nonincremental_init (braced_init_obstack);
10824 : }
10825 :
10826 1045058 : if (implicit == 1)
10827 : {
10828 702680 : found_missing_braces = 1;
10829 702680 : if (initializer_stack->missing_brace_richloc)
10830 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10831 702680 : (loc, "{");
10832 : }
10833 :
10834 1045058 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10835 : {
10836 880850 : constructor_fields = TYPE_FIELDS (constructor_type);
10837 : /* Skip any nameless bit fields at the beginning. */
10838 880850 : while (constructor_fields != NULL_TREE
10839 880914 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10840 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10841 :
10842 880850 : constructor_unfilled_fields = constructor_fields;
10843 880850 : constructor_bit_index = bitsize_zero_node;
10844 : }
10845 164208 : else if (gnu_vector_type_p (constructor_type))
10846 : {
10847 : /* Vectors are like simple fixed-size arrays. */
10848 9602 : constructor_max_index =
10849 4801 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10850 4801 : constructor_index = bitsize_int (0);
10851 4801 : constructor_unfilled_index = constructor_index;
10852 : }
10853 159407 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10854 : {
10855 159327 : if (TYPE_DOMAIN (constructor_type))
10856 : {
10857 159327 : constructor_max_index
10858 159327 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10859 :
10860 : /* Detect non-empty initializations of zero-length arrays. */
10861 159327 : if (constructor_max_index == NULL_TREE
10862 159327 : && TYPE_SIZE (constructor_type))
10863 135 : constructor_max_index = integer_minus_one_node;
10864 :
10865 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10866 : to initialize VLAs will cause a proper error; avoid tree
10867 : checking errors as well by setting a safe value. */
10868 159327 : if (constructor_max_index
10869 159057 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10870 2 : constructor_max_index = integer_minus_one_node;
10871 :
10872 159327 : constructor_index
10873 159327 : = convert (bitsizetype,
10874 159327 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10875 : }
10876 : else
10877 0 : constructor_index = bitsize_zero_node;
10878 :
10879 159327 : constructor_unfilled_index = constructor_index;
10880 159327 : if (value && TREE_CODE (value) == STRING_CST)
10881 : {
10882 : /* We need to split the char/wchar array into individual
10883 : characters, so that we don't have to special case it
10884 : everywhere. */
10885 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10886 : }
10887 : }
10888 : else
10889 : {
10890 80 : if (constructor_type != error_mark_node)
10891 : {
10892 51 : if (p->braced_scalar)
10893 22 : permerror_init (input_location, 0,
10894 : "braces around scalar initializer");
10895 : else
10896 29 : warning_init (input_location, 0,
10897 : "braces around scalar initializer");
10898 51 : constructor_braced_scalar = true;
10899 : }
10900 80 : constructor_fields = constructor_type;
10901 80 : constructor_unfilled_fields = constructor_type;
10902 : }
10903 : }
10904 :
10905 : /* At the end of an implicit or explicit brace level,
10906 : finish up that level of constructor. If a single expression
10907 : with redundant braces initialized that level, return the
10908 : c_expr structure for that expression. Otherwise, the original_code
10909 : element is set to ERROR_MARK.
10910 : If we were outputting the elements as they are read, return 0 as the value
10911 : from inner levels (process_init_element ignores that),
10912 : but return error_mark_node as the value from the outermost level
10913 : (that's what we want to put in DECL_INITIAL).
10914 : Otherwise, return a CONSTRUCTOR expression as the value. */
10915 :
10916 : struct c_expr
10917 2075770 : pop_init_level (location_t loc, int implicit,
10918 : struct obstack *braced_init_obstack,
10919 : location_t insert_before)
10920 : {
10921 2075770 : struct constructor_stack *p;
10922 2075770 : struct c_expr ret;
10923 2075770 : ret.value = NULL_TREE;
10924 2075770 : ret.original_code = ERROR_MARK;
10925 2075770 : ret.original_type = NULL;
10926 2075770 : ret.m_decimal = 0;
10927 :
10928 2075770 : if (implicit == 0)
10929 : {
10930 : /* When we come to an explicit close brace,
10931 : pop any inner levels that didn't have explicit braces. */
10932 1371789 : while (constructor_stack->implicit)
10933 1342 : process_init_element (input_location,
10934 : pop_init_level (loc, 1, braced_init_obstack,
10935 : insert_before),
10936 : true, braced_init_obstack);
10937 1370447 : gcc_assert (!constructor_range_stack);
10938 : }
10939 : else
10940 705323 : if (initializer_stack->missing_brace_richloc)
10941 705323 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10942 705323 : (insert_before, "}");
10943 :
10944 : /* Now output all pending elements. */
10945 2075770 : constructor_incremental = 1;
10946 2075770 : output_pending_init_elements (1, braced_init_obstack);
10947 :
10948 2075770 : p = constructor_stack;
10949 :
10950 : /* Error for initializing a flexible array member. */
10951 2075719 : if (constructor_type && constructor_fields
10952 2236791 : && flexible_array_member_type_p (constructor_type))
10953 : {
10954 : /* Silently discard empty initializations. The parser will
10955 : already have pedwarned for empty brackets for C17 and earlier. */
10956 270 : if (integer_zerop (constructor_unfilled_index))
10957 54 : constructor_type = NULL_TREE;
10958 270 : if (constructor_type || flag_isoc23)
10959 : {
10960 266 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10961 :
10962 266 : if (constructor_depth <= 2)
10963 219 : pedwarn_init (loc, OPT_Wpedantic,
10964 : "initialization of a flexible array member");
10965 47 : else if (constructor_type)
10966 33 : error_init (loc, "initialization of flexible array member "
10967 : "in a nested context");
10968 : else
10969 14 : pedwarn_init (loc, OPT_Wpedantic,
10970 : "initialization of flexible array member "
10971 : "in a nested context");
10972 :
10973 : /* We have already issued an error message for the existence
10974 : of a flexible array member not at the end of the structure.
10975 : Discard the initializer so that we do not die later. */
10976 266 : if (constructor_type
10977 216 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10978 272 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10979 0 : constructor_type = NULL_TREE;
10980 : }
10981 : }
10982 :
10983 2075770 : switch (vec_safe_length (constructor_elements))
10984 : {
10985 5105 : case 0:
10986 : /* Initialization with { } counts as zeroinit. */
10987 5105 : constructor_zeroinit = 1;
10988 5105 : break;
10989 916465 : case 1:
10990 : /* This might be zeroinit as well. */
10991 916465 : if (integer_zerop ((*constructor_elements)[0].value))
10992 2096 : constructor_zeroinit = 1;
10993 : break;
10994 1154200 : default:
10995 : /* If the constructor has more than one element, it can't be { 0 }. */
10996 1154200 : constructor_zeroinit = 0;
10997 1154200 : break;
10998 : }
10999 :
11000 : /* Warn when some structs are initialized with direct aggregation. */
11001 2075770 : if (!implicit && found_missing_braces && warn_missing_braces
11002 37 : && !constructor_zeroinit)
11003 : {
11004 18 : gcc_assert (initializer_stack->missing_brace_richloc);
11005 18 : warning_at (initializer_stack->missing_brace_richloc,
11006 18 : OPT_Wmissing_braces,
11007 : "missing braces around initializer");
11008 : }
11009 :
11010 : /* Warn when some struct elements are implicitly initialized to zero. */
11011 2075770 : if (warn_missing_field_initializers
11012 425477 : && constructor_type
11013 425463 : && TREE_CODE (constructor_type) == RECORD_TYPE
11014 193409 : && constructor_unfilled_fields)
11015 : {
11016 : /* Do not warn for flexible array members or zero-length arrays. */
11017 43 : while (constructor_unfilled_fields
11018 43 : && (!DECL_SIZE (constructor_unfilled_fields)
11019 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
11020 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
11021 :
11022 43 : if (constructor_unfilled_fields
11023 : /* Do not warn if this level of the initializer uses member
11024 : designators; it is likely to be deliberate. */
11025 43 : && !constructor_designated
11026 : /* Do not warn about initializing with { 0 } or with { }. */
11027 24 : && !constructor_zeroinit)
11028 : {
11029 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
11030 : "missing initializer for field %qD of %qT",
11031 : constructor_unfilled_fields,
11032 : constructor_type))
11033 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
11034 : "%qD declared here", constructor_unfilled_fields);
11035 : }
11036 : }
11037 :
11038 : /* Pad out the end of the structure. */
11039 2075770 : if (p->replacement_value.value)
11040 : /* If this closes a superfluous brace pair,
11041 : just pass out the element between them. */
11042 138 : ret = p->replacement_value;
11043 2075632 : else if (constructor_type == NULL_TREE)
11044 : ;
11045 2075535 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
11046 2075535 : && TREE_CODE (constructor_type) != ARRAY_TYPE
11047 2075535 : && !gnu_vector_type_p (constructor_type))
11048 : {
11049 : /* A nonincremental scalar initializer--just return
11050 : the element, after verifying there is just one.
11051 : Empty scalar initializers are supported in C23. */
11052 636 : if (vec_safe_is_empty (constructor_elements))
11053 : {
11054 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11055 85 : ret.value = error_mark_node;
11056 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11057 : {
11058 2 : error_init (loc, "invalid initializer");
11059 2 : ret.value = error_mark_node;
11060 : }
11061 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11062 : /* Ensure this is a null pointer constant in the case of a
11063 : 'constexpr' object initialized with {}. */
11064 33 : ret.value = build_zero_cst (ptr_type_node);
11065 : else
11066 81 : ret.value = build_zero_cst (constructor_type);
11067 : }
11068 435 : else if (vec_safe_length (constructor_elements) != 1)
11069 : {
11070 0 : error_init (loc, "extra elements in scalar initializer");
11071 0 : ret.value = (*constructor_elements)[0].value;
11072 : }
11073 : else
11074 435 : ret.value = (*constructor_elements)[0].value;
11075 : }
11076 : else
11077 : {
11078 2074899 : if (constructor_erroneous)
11079 302 : ret.value = error_mark_node;
11080 : else
11081 : {
11082 2074597 : ret.value = build_constructor (constructor_type,
11083 : constructor_elements);
11084 2074597 : if (constructor_constant)
11085 1742811 : TREE_CONSTANT (ret.value) = 1;
11086 2074597 : if (constructor_constant && constructor_simple)
11087 1742772 : TREE_STATIC (ret.value) = 1;
11088 2074597 : if (constructor_nonconst)
11089 673 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11090 2074597 : if (constructor_zero_padding_bits)
11091 687 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11092 : }
11093 : }
11094 :
11095 2075770 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11096 : {
11097 1076 : if (constructor_nonconst)
11098 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11099 1061 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11100 0 : ret.original_code = ERROR_MARK;
11101 : }
11102 :
11103 2075770 : constructor_type = p->type;
11104 2075770 : constructor_fields = p->fields;
11105 2075770 : constructor_index = p->index;
11106 2075770 : constructor_max_index = p->max_index;
11107 2075770 : constructor_unfilled_index = p->unfilled_index;
11108 2075770 : constructor_unfilled_fields = p->unfilled_fields;
11109 2075770 : constructor_bit_index = p->bit_index;
11110 2075770 : constructor_elements = p->elements;
11111 2075770 : constructor_constant = p->constant;
11112 2075770 : constructor_simple = p->simple;
11113 2075770 : constructor_nonconst = p->nonconst;
11114 2075770 : constructor_erroneous = p->erroneous;
11115 2075770 : constructor_incremental = p->incremental;
11116 2075770 : constructor_designated = p->designated;
11117 2075770 : constructor_zero_padding_bits = p->zero_padding_bits;
11118 2075770 : constructor_braced_scalar = p->braced_scalar;
11119 2075770 : designator_depth = p->designator_depth;
11120 2075770 : constructor_pending_elts = p->pending_elts;
11121 2075770 : constructor_depth = p->depth;
11122 2075770 : if (!p->implicit)
11123 1370447 : constructor_range_stack = p->range_stack;
11124 2075770 : RESTORE_SPELLING_DEPTH (constructor_depth);
11125 :
11126 2075770 : constructor_stack = p->next;
11127 2075770 : XDELETE (p);
11128 :
11129 2075770 : if (ret.value == NULL_TREE && constructor_stack == 0)
11130 0 : ret.value = error_mark_node;
11131 2075770 : return ret;
11132 : }
11133 :
11134 : /* Common handling for both array range and field name designators.
11135 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11136 :
11137 : static bool
11138 37081 : set_designator (location_t loc, bool array,
11139 : struct obstack *braced_init_obstack)
11140 : {
11141 37081 : tree subtype;
11142 37081 : enum tree_code subcode;
11143 :
11144 : /* Don't die if an entire brace-pair level is superfluous
11145 : in the containing level, or for an erroneous type. */
11146 37081 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11147 : return true;
11148 :
11149 : /* If there were errors in this designator list already, bail out
11150 : silently. */
11151 37072 : if (designator_erroneous)
11152 : return true;
11153 :
11154 : /* Likewise for an initializer for a variable-size type. Those are
11155 : diagnosed in the parser, except for empty initializer braces. */
11156 37072 : if (COMPLETE_TYPE_P (constructor_type)
11157 37072 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11158 : return true;
11159 :
11160 37062 : if (!designator_depth)
11161 : {
11162 34743 : gcc_assert (!constructor_range_stack);
11163 :
11164 : /* Designator list starts at the level of closest explicit
11165 : braces. */
11166 36418 : while (constructor_stack->implicit)
11167 1675 : process_init_element (input_location,
11168 : pop_init_level (loc, 1, braced_init_obstack,
11169 : last_init_list_comma),
11170 : true, braced_init_obstack);
11171 34743 : constructor_designated = 1;
11172 34743 : return false;
11173 : }
11174 :
11175 2319 : switch (TREE_CODE (constructor_type))
11176 : {
11177 1442 : case RECORD_TYPE:
11178 1442 : case UNION_TYPE:
11179 1442 : subtype = TREE_TYPE (constructor_fields);
11180 1442 : if (subtype != error_mark_node)
11181 1442 : subtype = TYPE_MAIN_VARIANT (subtype);
11182 : break;
11183 877 : case ARRAY_TYPE:
11184 877 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11185 877 : break;
11186 0 : default:
11187 0 : gcc_unreachable ();
11188 : }
11189 :
11190 2319 : subcode = TREE_CODE (subtype);
11191 2319 : if (array && subcode != ARRAY_TYPE)
11192 : {
11193 1 : error_init (loc, "array index in non-array initializer");
11194 1 : return true;
11195 : }
11196 2318 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11197 : {
11198 0 : error_init (loc, "field name not in record or union initializer");
11199 0 : return true;
11200 : }
11201 :
11202 2318 : constructor_designated = 1;
11203 2318 : finish_implicit_inits (loc, braced_init_obstack);
11204 2318 : push_init_level (loc, 2, braced_init_obstack);
11205 2318 : return false;
11206 : }
11207 :
11208 : /* If there are range designators in designator list, push a new designator
11209 : to constructor_range_stack. RANGE_END is end of such stack range or
11210 : NULL_TREE if there is no range designator at this level. */
11211 :
11212 : static void
11213 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11214 : {
11215 390 : struct constructor_range_stack *p;
11216 :
11217 780 : p = (struct constructor_range_stack *)
11218 390 : obstack_alloc (braced_init_obstack,
11219 : sizeof (struct constructor_range_stack));
11220 390 : p->prev = constructor_range_stack;
11221 390 : p->next = 0;
11222 390 : p->fields = constructor_fields;
11223 390 : p->range_start = constructor_index;
11224 390 : p->index = constructor_index;
11225 390 : p->stack = constructor_stack;
11226 390 : p->range_end = range_end;
11227 390 : if (constructor_range_stack)
11228 19 : constructor_range_stack->next = p;
11229 390 : constructor_range_stack = p;
11230 390 : }
11231 :
11232 : /* Within an array initializer, specify the next index to be initialized.
11233 : FIRST is that index. If LAST is nonzero, then initialize a range
11234 : of indices, running from FIRST through LAST. */
11235 :
11236 : void
11237 1931 : set_init_index (location_t loc, tree first, tree last,
11238 : struct obstack *braced_init_obstack)
11239 : {
11240 1931 : if (set_designator (loc, true, braced_init_obstack))
11241 : return;
11242 :
11243 1928 : designator_erroneous = 1;
11244 :
11245 3856 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11246 3845 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11247 : {
11248 12 : error_init (loc, "array index in initializer not of integer type");
11249 12 : return;
11250 : }
11251 :
11252 1916 : if (TREE_CODE (first) != INTEGER_CST)
11253 : {
11254 6 : first = c_fully_fold (first, false, NULL);
11255 6 : if (TREE_CODE (first) == INTEGER_CST)
11256 5 : pedwarn_init (loc, OPT_Wpedantic,
11257 : "array index in initializer is not "
11258 : "an integer constant expression");
11259 : }
11260 :
11261 1916 : if (last && TREE_CODE (last) != INTEGER_CST)
11262 : {
11263 2 : last = c_fully_fold (last, false, NULL);
11264 2 : if (TREE_CODE (last) == INTEGER_CST)
11265 1 : pedwarn_init (loc, OPT_Wpedantic,
11266 : "array index in initializer is not "
11267 : "an integer constant expression");
11268 : }
11269 :
11270 1916 : if (TREE_CODE (first) != INTEGER_CST)
11271 1 : error_init (loc, "nonconstant array index in initializer");
11272 1915 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11273 1 : error_init (loc, "nonconstant array index in initializer");
11274 1914 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11275 9 : error_init (loc, "array index in non-array initializer");
11276 1905 : else if (tree_int_cst_sgn (first) == -1)
11277 15 : error_init (loc, "array index in initializer exceeds array bounds");
11278 1890 : else if (constructor_max_index
11279 1890 : && tree_int_cst_lt (constructor_max_index, first))
11280 7 : error_init (loc, "array index in initializer exceeds array bounds");
11281 : else
11282 : {
11283 1883 : constant_expression_warning (first);
11284 1883 : if (last)
11285 385 : constant_expression_warning (last);
11286 1883 : constructor_index = convert (bitsizetype, first);
11287 1883 : if (tree_int_cst_lt (constructor_index, first))
11288 : {
11289 0 : constructor_index = copy_node (constructor_index);
11290 0 : TREE_OVERFLOW (constructor_index) = 1;
11291 : }
11292 :
11293 1883 : if (last)
11294 : {
11295 385 : if (tree_int_cst_equal (first, last))
11296 : last = NULL_TREE;
11297 384 : else if (tree_int_cst_lt (last, first))
11298 : {
11299 2 : error_init (loc, "empty index range in initializer");
11300 2 : last = NULL_TREE;
11301 : }
11302 : else
11303 : {
11304 382 : last = convert (bitsizetype, last);
11305 382 : if (constructor_max_index != NULL_TREE
11306 382 : && tree_int_cst_lt (constructor_max_index, last))
11307 : {
11308 3 : error_init (loc, "array index range in initializer exceeds "
11309 : "array bounds");
11310 3 : last = NULL_TREE;
11311 : }
11312 : }
11313 : }
11314 :
11315 1883 : designator_depth++;
11316 1883 : designator_erroneous = 0;
11317 1883 : if (constructor_range_stack || last)
11318 379 : push_range_stack (last, braced_init_obstack);
11319 : }
11320 : }
11321 :
11322 : /* Within a struct initializer, specify the next field to be initialized. */
11323 :
11324 : void
11325 35104 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11326 : struct obstack *braced_init_obstack)
11327 : {
11328 35104 : tree field;
11329 :
11330 35104 : if (set_designator (loc, false, braced_init_obstack))
11331 : return;
11332 :
11333 35087 : designator_erroneous = 1;
11334 :
11335 35087 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11336 : {
11337 3 : error_init (loc, "field name not in record or union initializer");
11338 3 : return;
11339 : }
11340 :
11341 35084 : field = lookup_field (constructor_type, fieldname);
11342 :
11343 35084 : if (field == NULL_TREE)
11344 : {
11345 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11346 8 : if (guessed_id)
11347 : {
11348 4 : gcc_rich_location rich_loc (fieldname_loc);
11349 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11350 4 : error_at (&rich_loc,
11351 : "%qT has no member named %qE; did you mean %qE?",
11352 : constructor_type, fieldname, guessed_id);
11353 4 : }
11354 : else
11355 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11356 : constructor_type, fieldname);
11357 : }
11358 : else
11359 35122 : do
11360 : {
11361 35122 : constructor_fields = TREE_VALUE (field);
11362 35122 : designator_depth++;
11363 35122 : designator_erroneous = 0;
11364 35122 : if (constructor_range_stack)
11365 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11366 35122 : field = TREE_CHAIN (field);
11367 35122 : if (field)
11368 : {
11369 46 : if (set_designator (loc, false, braced_init_obstack))
11370 : return;
11371 : }
11372 : }
11373 35122 : while (field != NULL_TREE);
11374 : }
11375 :
11376 : /* Helper function for add_pending_init. Find inorder successor of P
11377 : in AVL tree. */
11378 : static struct init_node *
11379 129 : init_node_successor (struct init_node *p)
11380 : {
11381 129 : struct init_node *r;
11382 129 : if (p->right)
11383 : {
11384 : r = p->right;
11385 58 : while (r->left)
11386 : r = r->left;
11387 : return r;
11388 : }
11389 75 : r = p->parent;
11390 114 : while (r && p == r->right)
11391 : {
11392 39 : p = r;
11393 39 : r = r->parent;
11394 : }
11395 : return r;
11396 : }
11397 :
11398 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11399 : identifies the initializer, either array index or field in a structure.
11400 : VALUE is the value of that index or field. If ORIGTYPE is not
11401 : NULL_TREE, it is the original type of VALUE.
11402 :
11403 : IMPLICIT is true if value comes from pop_init_level (1),
11404 : the new initializer has been merged with the existing one
11405 : and thus no warnings should be emitted about overriding an
11406 : existing initializer. */
11407 :
11408 : static void
11409 7984 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11410 : bool implicit, struct obstack *braced_init_obstack)
11411 : {
11412 7984 : struct init_node *p, **q, *r;
11413 :
11414 12533 : q = &constructor_pending_elts;
11415 7984 : p = 0;
11416 :
11417 7984 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11418 : {
11419 7214 : while (*q != 0)
11420 : {
11421 4384 : p = *q;
11422 4384 : if (tree_int_cst_lt (purpose, p->purpose))
11423 419 : q = &p->left;
11424 3965 : else if (tree_int_cst_lt (p->purpose, purpose))
11425 : {
11426 3354 : if (TREE_CODE (p->value) != RAW_DATA_CST
11427 3354 : || (p->right
11428 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11429 3224 : q = &p->right;
11430 : else
11431 : {
11432 130 : widest_int pp = wi::to_widest (p->purpose);
11433 130 : widest_int pw = wi::to_widest (purpose);
11434 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11435 98 : q = &p->right;
11436 : else
11437 : {
11438 : /* Override which should split the old RAW_DATA_CST
11439 : into 2 or 3 pieces. */
11440 32 : if (!implicit && warn_override_init)
11441 9 : warning_init (loc, OPT_Woverride_init,
11442 : "initialized field overwritten");
11443 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11444 32 : unsigned HOST_WIDE_INT len = 1;
11445 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11446 0 : len = RAW_DATA_LENGTH (value);
11447 32 : unsigned HOST_WIDE_INT end = 0;
11448 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11449 32 : gcc_checking_assert (start < plen && start);
11450 32 : if (plen - start > len)
11451 30 : end = plen - start - len;
11452 32 : tree v = p->value;
11453 32 : tree origtype = p->origtype;
11454 32 : if (start == 1)
11455 2 : p->value = build_int_cst (TREE_TYPE (v),
11456 2 : RAW_DATA_UCHAR_ELT (v, 0));
11457 : else
11458 : {
11459 30 : p->value = v;
11460 30 : if (end > 1)
11461 26 : v = copy_node (v);
11462 30 : RAW_DATA_LENGTH (p->value) = start;
11463 : }
11464 32 : if (end)
11465 : {
11466 30 : tree epurpose
11467 30 : = size_binop (PLUS_EXPR, purpose,
11468 : bitsize_int (len));
11469 30 : if (end > 1)
11470 : {
11471 28 : RAW_DATA_LENGTH (v) -= plen - end;
11472 28 : RAW_DATA_POINTER (v) += plen - end;
11473 : }
11474 : else
11475 2 : v = build_int_cst (TREE_TYPE (v),
11476 2 : RAW_DATA_UCHAR_ELT (v, plen
11477 : - end));
11478 30 : add_pending_init (loc, epurpose, v, origtype,
11479 : implicit, braced_init_obstack);
11480 : }
11481 32 : q = &constructor_pending_elts;
11482 32 : continue;
11483 32 : }
11484 130 : }
11485 : }
11486 : else
11487 : {
11488 611 : if (TREE_CODE (p->value) == RAW_DATA_CST
11489 621 : && (RAW_DATA_LENGTH (p->value)
11490 10 : > (TREE_CODE (value) == RAW_DATA_CST
11491 10 : ? RAW_DATA_LENGTH (value) : 1)))
11492 : {
11493 : /* Override which should split the old RAW_DATA_CST
11494 : into 2 pieces. */
11495 6 : if (!implicit && warn_override_init)
11496 3 : warning_init (loc, OPT_Woverride_init,
11497 : "initialized field overwritten");
11498 6 : unsigned HOST_WIDE_INT len = 1;
11499 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11500 2 : len = RAW_DATA_LENGTH (value);
11501 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11502 : {
11503 6 : RAW_DATA_LENGTH (p->value) -= len;
11504 6 : RAW_DATA_POINTER (p->value) += len;
11505 : }
11506 : else
11507 : {
11508 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11509 0 : p->value
11510 0 : = build_int_cst (TREE_TYPE (p->value),
11511 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11512 : }
11513 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11514 : bitsize_int (len));
11515 6 : continue;
11516 6 : }
11517 605 : if (TREE_CODE (value) == RAW_DATA_CST)
11518 : {
11519 8 : handle_raw_data:
11520 : /* RAW_DATA_CST value might overlap various further
11521 : prior initval entries. Find out how many. */
11522 13 : unsigned cnt = 0;
11523 13 : widest_int w
11524 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11525 13 : struct init_node *r = p, *last = NULL;
11526 13 : bool override_init = warn_override_init;
11527 13 : while ((r = init_node_successor (r))
11528 53 : && wi::to_widest (r->purpose) < w)
11529 : {
11530 40 : ++cnt;
11531 40 : if (TREE_SIDE_EFFECTS (r->value))
11532 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11533 : "initialized field with side-effects "
11534 : "overwritten");
11535 38 : else if (override_init)
11536 : {
11537 6 : warning_init (loc, OPT_Woverride_init,
11538 : "initialized field overwritten");
11539 6 : override_init = false;
11540 : }
11541 40 : last = r;
11542 : }
11543 13 : if (cnt)
11544 : {
11545 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11546 13 : && (wi::to_widest (last->purpose)
11547 13 : + RAW_DATA_LENGTH (last->value) > w))
11548 : {
11549 : /* The last overlapping prior initval overlaps
11550 : only partially. Shrink it and decrease cnt. */
11551 0 : unsigned int l = (wi::to_widest (last->purpose)
11552 0 : + RAW_DATA_LENGTH (last->value)
11553 0 : - w).to_uhwi ();
11554 0 : --cnt;
11555 0 : RAW_DATA_LENGTH (last->value) -= l;
11556 0 : RAW_DATA_POINTER (last->value) += l;
11557 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11558 0 : last->value
11559 0 : = build_int_cst (TREE_TYPE (last->value),
11560 0 : RAW_DATA_UCHAR_ELT (last->value,
11561 : 0));
11562 0 : last->purpose
11563 0 : = size_binop (PLUS_EXPR, last->purpose,
11564 : bitsize_int (l));
11565 : }
11566 : /* Instead of deleting cnt nodes from the AVL tree
11567 : and rebalancing, peel of last cnt bytes from the
11568 : RAW_DATA_CST. Overriding thousands of previously
11569 : initialized array elements with #embed needs to work,
11570 : but doesn't need to be super efficient. */
11571 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11572 : > cnt);
11573 13 : RAW_DATA_LENGTH (value) -= cnt;
11574 13 : const unsigned char *s
11575 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11576 13 : unsigned int o = RAW_DATA_LENGTH (value);
11577 53 : for (r = p; cnt--; ++o, ++s)
11578 : {
11579 40 : r = init_node_successor (r);
11580 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11581 : bitsize_int (o));
11582 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11583 40 : r->origtype = origtype;
11584 : }
11585 13 : if (RAW_DATA_LENGTH (value) == 1)
11586 0 : value = build_int_cst (TREE_TYPE (value),
11587 0 : RAW_DATA_UCHAR_ELT (value, 0));
11588 : }
11589 : }
11590 610 : if (!implicit)
11591 : {
11592 54 : if (TREE_SIDE_EFFECTS (p->value))
11593 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11594 : "initialized field with side-effects "
11595 : "overwritten");
11596 48 : else if (warn_override_init)
11597 13 : warning_init (loc, OPT_Woverride_init,
11598 : "initialized field overwritten");
11599 : }
11600 610 : p->value = value;
11601 610 : p->origtype = origtype;
11602 610 : return;
11603 : }
11604 : }
11605 2830 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11606 : {
11607 61 : struct init_node *r;
11608 61 : if (q == &p->left)
11609 : r = p;
11610 : else
11611 36 : r = init_node_successor (p);
11612 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11613 131 : + RAW_DATA_LENGTH (value)))
11614 : {
11615 : /* Overlap with at least one prior initval in the range but
11616 : not at the start. */
11617 5 : p = r;
11618 5 : p->purpose = purpose;
11619 5 : goto handle_raw_data;
11620 : }
11621 : }
11622 : }
11623 : else
11624 : {
11625 4549 : tree bitpos;
11626 :
11627 4549 : bitpos = bit_position (purpose);
11628 8187 : while (*q != NULL)
11629 : {
11630 4881 : p = *q;
11631 4881 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11632 81 : q = &p->left;
11633 4800 : else if (p->purpose != purpose)
11634 3557 : q = &p->right;
11635 : else
11636 : {
11637 1243 : if (!implicit)
11638 : {
11639 101 : if (TREE_SIDE_EFFECTS (p->value))
11640 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11641 : "initialized field with side-effects "
11642 : "overwritten");
11643 97 : else if (warn_override_init)
11644 6 : warning_init (loc, OPT_Woverride_init,
11645 : "initialized field overwritten");
11646 : }
11647 1243 : p->value = value;
11648 1243 : p->origtype = origtype;
11649 1243 : return;
11650 : }
11651 : }
11652 : }
11653 :
11654 6131 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11655 : sizeof (struct init_node));
11656 6131 : r->purpose = purpose;
11657 6131 : r->value = value;
11658 6131 : r->origtype = origtype;
11659 :
11660 6131 : *q = r;
11661 6131 : r->parent = p;
11662 6131 : r->left = 0;
11663 6131 : r->right = 0;
11664 6131 : r->balance = 0;
11665 :
11666 9955 : while (p)
11667 : {
11668 5212 : struct init_node *s;
11669 :
11670 5212 : if (r == p->left)
11671 : {
11672 343 : if (p->balance == 0)
11673 246 : p->balance = -1;
11674 97 : else if (p->balance < 0)
11675 : {
11676 45 : if (r->balance < 0)
11677 : {
11678 : /* L rotation. */
11679 40 : p->left = r->right;
11680 40 : if (p->left)
11681 2 : p->left->parent = p;
11682 40 : r->right = p;
11683 :
11684 40 : p->balance = 0;
11685 40 : r->balance = 0;
11686 :
11687 40 : s = p->parent;
11688 40 : p->parent = r;
11689 40 : r->parent = s;
11690 40 : if (s)
11691 : {
11692 31 : if (s->left == p)
11693 10 : s->left = r;
11694 : else
11695 21 : s->right = r;
11696 : }
11697 : else
11698 9 : constructor_pending_elts = r;
11699 : }
11700 : else
11701 : {
11702 : /* LR rotation. */
11703 5 : struct init_node *t = r->right;
11704 :
11705 5 : r->right = t->left;
11706 5 : if (r->right)
11707 0 : r->right->parent = r;
11708 5 : t->left = r;
11709 :
11710 5 : p->left = t->right;
11711 5 : if (p->left)
11712 0 : p->left->parent = p;
11713 5 : t->right = p;
11714 :
11715 5 : p->balance = t->balance < 0;
11716 5 : r->balance = -(t->balance > 0);
11717 5 : t->balance = 0;
11718 :
11719 5 : s = p->parent;
11720 5 : p->parent = t;
11721 5 : r->parent = t;
11722 5 : t->parent = s;
11723 5 : if (s)
11724 : {
11725 0 : if (s->left == p)
11726 0 : s->left = t;
11727 : else
11728 0 : s->right = t;
11729 : }
11730 : else
11731 5 : constructor_pending_elts = t;
11732 : }
11733 : break;
11734 : }
11735 : else
11736 : {
11737 : /* p->balance == +1; growth of left side balances the node. */
11738 52 : p->balance = 0;
11739 52 : break;
11740 : }
11741 : }
11742 : else /* r == p->right */
11743 : {
11744 4869 : if (p->balance == 0)
11745 : /* Growth propagation from right side. */
11746 3578 : p->balance++;
11747 1291 : else if (p->balance > 0)
11748 : {
11749 1228 : if (r->balance > 0)
11750 : {
11751 : /* R rotation. */
11752 1222 : p->right = r->left;
11753 1222 : if (p->right)
11754 193 : p->right->parent = p;
11755 1222 : r->left = p;
11756 :
11757 1222 : p->balance = 0;
11758 1222 : r->balance = 0;
11759 :
11760 1222 : s = p->parent;
11761 1222 : p->parent = r;
11762 1222 : r->parent = s;
11763 1222 : if (s)
11764 : {
11765 414 : if (s->left == p)
11766 2 : s->left = r;
11767 : else
11768 412 : s->right = r;
11769 : }
11770 : else
11771 808 : constructor_pending_elts = r;
11772 : }
11773 : else /* r->balance == -1 */
11774 : {
11775 : /* RL rotation */
11776 6 : struct init_node *t = r->left;
11777 :
11778 6 : r->left = t->right;
11779 6 : if (r->left)
11780 2 : r->left->parent = r;
11781 6 : t->right = r;
11782 :
11783 6 : p->right = t->left;
11784 6 : if (p->right)
11785 2 : p->right->parent = p;
11786 6 : t->left = p;
11787 :
11788 6 : r->balance = (t->balance < 0);
11789 6 : p->balance = -(t->balance > 0);
11790 6 : t->balance = 0;
11791 :
11792 6 : s = p->parent;
11793 6 : p->parent = t;
11794 6 : r->parent = t;
11795 6 : t->parent = s;
11796 6 : if (s)
11797 : {
11798 4 : if (s->left == p)
11799 0 : s->left = t;
11800 : else
11801 4 : s->right = t;
11802 : }
11803 : else
11804 2 : constructor_pending_elts = t;
11805 : }
11806 : break;
11807 : }
11808 : else
11809 : {
11810 : /* p->balance == -1; growth of right side balances the node. */
11811 63 : p->balance = 0;
11812 63 : break;
11813 : }
11814 : }
11815 :
11816 3824 : r = p;
11817 3824 : p = p->parent;
11818 : }
11819 : }
11820 :
11821 : /* Build AVL tree from a sorted chain. */
11822 :
11823 : static void
11824 1812 : set_nonincremental_init (struct obstack * braced_init_obstack)
11825 : {
11826 1812 : unsigned HOST_WIDE_INT ix;
11827 1812 : tree index, value;
11828 :
11829 1812 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11830 1812 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11831 : return;
11832 :
11833 5511 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11834 3699 : add_pending_init (input_location, index, value, NULL_TREE, true,
11835 : braced_init_obstack);
11836 1812 : constructor_elements = NULL;
11837 1812 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11838 : {
11839 948 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11840 : /* Skip any nameless bit fields at the beginning. */
11841 948 : while (constructor_unfilled_fields != NULL_TREE
11842 948 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11843 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11844 :
11845 : }
11846 864 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11847 : {
11848 864 : if (TYPE_DOMAIN (constructor_type))
11849 806 : constructor_unfilled_index
11850 806 : = convert (bitsizetype,
11851 806 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11852 : else
11853 58 : constructor_unfilled_index = bitsize_zero_node;
11854 : }
11855 1812 : constructor_incremental = 0;
11856 : }
11857 :
11858 : /* Build AVL tree from a string constant. */
11859 :
11860 : static void
11861 7 : set_nonincremental_init_from_string (tree str,
11862 : struct obstack * braced_init_obstack)
11863 : {
11864 7 : tree value, purpose, type;
11865 7 : HOST_WIDE_INT val[2];
11866 7 : const char *p, *end;
11867 7 : int byte, wchar_bytes, charwidth, bitpos;
11868 :
11869 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11870 :
11871 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11872 7 : charwidth = TYPE_PRECISION (char_type_node);
11873 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11874 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11875 7 : type = TREE_TYPE (constructor_type);
11876 7 : p = TREE_STRING_POINTER (str);
11877 7 : end = p + TREE_STRING_LENGTH (str);
11878 :
11879 7 : for (purpose = bitsize_zero_node;
11880 : p < end
11881 52 : && !(constructor_max_index
11882 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11883 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11884 : {
11885 25 : if (wchar_bytes == 1)
11886 : {
11887 9 : val[0] = (unsigned char) *p++;
11888 9 : val[1] = 0;
11889 : }
11890 : else
11891 : {
11892 16 : val[1] = 0;
11893 16 : val[0] = 0;
11894 64 : for (byte = 0; byte < wchar_bytes; byte++)
11895 : {
11896 48 : if (BYTES_BIG_ENDIAN)
11897 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11898 : else
11899 48 : bitpos = byte * charwidth;
11900 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11901 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11902 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11903 : }
11904 : }
11905 :
11906 25 : if (!TYPE_UNSIGNED (type))
11907 : {
11908 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11909 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11910 : {
11911 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11912 : {
11913 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11914 0 : val[1] = -1;
11915 : }
11916 : }
11917 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11918 : {
11919 0 : if (val[0] < 0)
11920 0 : val[1] = -1;
11921 : }
11922 0 : else if (val[1] & (HOST_WIDE_INT_1
11923 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11924 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11925 : }
11926 :
11927 25 : value = wide_int_to_tree (type,
11928 25 : wide_int::from_array (val, 2,
11929 : HOST_BITS_PER_WIDE_INT * 2));
11930 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11931 : braced_init_obstack);
11932 : }
11933 :
11934 7 : constructor_incremental = 0;
11935 7 : }
11936 :
11937 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11938 : not initialized yet. */
11939 :
11940 : static tree
11941 705323 : find_init_member (tree field, struct obstack * braced_init_obstack)
11942 : {
11943 705323 : struct init_node *p;
11944 :
11945 705323 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11946 : {
11947 703504 : if (constructor_incremental
11948 703504 : && tree_int_cst_lt (field, constructor_unfilled_index))
11949 16 : set_nonincremental_init (braced_init_obstack);
11950 :
11951 703504 : p = constructor_pending_elts;
11952 704103 : while (p)
11953 : {
11954 1151 : if (tree_int_cst_lt (field, p->purpose))
11955 93 : p = p->left;
11956 1058 : else if (tree_int_cst_lt (p->purpose, field))
11957 506 : p = p->right;
11958 : else
11959 552 : return p->value;
11960 : }
11961 : }
11962 1819 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11963 : {
11964 1696 : tree bitpos = bit_position (field);
11965 :
11966 1696 : if (constructor_incremental
11967 1696 : && (!constructor_unfilled_fields
11968 609 : || tree_int_cst_lt (bitpos,
11969 609 : bit_position (constructor_unfilled_fields))))
11970 289 : set_nonincremental_init (braced_init_obstack);
11971 :
11972 1696 : p = constructor_pending_elts;
11973 2652 : while (p)
11974 : {
11975 2047 : if (field == p->purpose)
11976 1091 : return p->value;
11977 956 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11978 7 : p = p->left;
11979 : else
11980 949 : p = p->right;
11981 : }
11982 : }
11983 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11984 : {
11985 123 : if (!vec_safe_is_empty (constructor_elements)
11986 50 : && (constructor_elements->last ().index == field))
11987 50 : return constructor_elements->last ().value;
11988 : }
11989 : return NULL_TREE;
11990 : }
11991 :
11992 : /* "Output" the next constructor element.
11993 : At top level, really output it to assembler code now.
11994 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11995 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11996 : TYPE is the data type that the containing data type wants here.
11997 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11998 : If VALUE is a string constant, STRICT_STRING is true if it is
11999 : unparenthesized or we should not warn here for it being parenthesized.
12000 : For other types of VALUE, STRICT_STRING is not used.
12001 :
12002 : PENDING if true means output pending elements that belong
12003 : right after this element. (PENDING is normally true;
12004 : it is false while outputting pending elements, to avoid recursion.)
12005 :
12006 : IMPLICIT is true if value comes from pop_init_level (1),
12007 : the new initializer has been merged with the existing one
12008 : and thus no warnings should be emitted about overriding an
12009 : existing initializer. */
12010 :
12011 : static void
12012 9664331 : output_init_element (location_t loc, tree value, tree origtype,
12013 : bool strict_string, tree type, tree field, bool pending,
12014 : bool implicit, struct obstack * braced_init_obstack)
12015 : {
12016 9664331 : tree semantic_type = NULL_TREE;
12017 9664331 : bool maybe_const = true;
12018 9664331 : bool npc, int_const_expr, arith_const_expr;
12019 :
12020 9664331 : if (type == error_mark_node || value == error_mark_node)
12021 : {
12022 274 : constructor_erroneous = 1;
12023 4608 : return;
12024 : }
12025 9664057 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
12026 201018 : && (TREE_CODE (value) == STRING_CST
12027 159587 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
12028 41439 : && !(TREE_CODE (value) == STRING_CST
12029 41431 : && TREE_CODE (type) == ARRAY_TYPE
12030 3898 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
12031 9701598 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
12032 37541 : TYPE_MAIN_VARIANT (type)))
12033 37541 : value = array_to_pointer_conversion (input_location, value);
12034 :
12035 9664057 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
12036 182 : && require_constant_value && pending)
12037 : {
12038 : /* As an extension, allow initializing objects with static storage
12039 : duration with compound literals (which are then treated just as
12040 : the brace enclosed list they contain). */
12041 114 : if (flag_isoc99)
12042 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
12043 : "constant");
12044 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
12045 114 : value = DECL_INITIAL (decl);
12046 : }
12047 :
12048 9664057 : npc = null_pointer_constant_p (value);
12049 19328114 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12050 4238388 : && !TREE_OVERFLOW (value)
12051 13902444 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12052 : /* Not fully determined before folding. */
12053 9664057 : arith_const_expr = true;
12054 9664057 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12055 : {
12056 16 : semantic_type = TREE_TYPE (value);
12057 16 : value = TREE_OPERAND (value, 0);
12058 : }
12059 9664057 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12060 : /* TODO: this may not detect all cases of expressions folding to
12061 : constants that are not arithmetic constant expressions. */
12062 9664057 : if (!maybe_const)
12063 : arith_const_expr = false;
12064 19325573 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12065 3195544 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12066 11509017 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12067 : arith_const_expr = false;
12068 7831926 : else if (TREE_CODE (value) != INTEGER_CST
12069 : && TREE_CODE (value) != REAL_CST
12070 : && TREE_CODE (value) != COMPLEX_CST
12071 : && TREE_CODE (value) != RAW_DATA_CST)
12072 : arith_const_expr = false;
12073 4628986 : else if (TREE_OVERFLOW (value))
12074 5035072 : arith_const_expr = false;
12075 :
12076 9664057 : if (value == error_mark_node)
12077 0 : constructor_erroneous = 1;
12078 9664057 : else if (!TREE_CONSTANT (value))
12079 3223456 : constructor_constant = 0;
12080 6440601 : else if (!initializer_constant_valid_p (value,
12081 6440601 : TREE_TYPE (value),
12082 6440601 : AGGREGATE_TYPE_P (constructor_type)
12083 2760936 : && TYPE_REVERSE_STORAGE_ORDER
12084 : (constructor_type))
12085 6440601 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12086 1041264 : && DECL_C_BIT_FIELD (field)
12087 7978 : && TREE_CODE (value) != INTEGER_CST))
12088 77 : constructor_simple = 0;
12089 9664057 : if (!maybe_const)
12090 1174 : constructor_nonconst = 1;
12091 :
12092 : /* Digest the initializer and issue any errors about incompatible
12093 : types before issuing errors about non-constant initializers. */
12094 9664057 : tree new_value = value;
12095 9664057 : if (semantic_type)
12096 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12097 : /* In the case of braces around a scalar initializer, the result of
12098 : this initializer processing goes through digest_init again at the
12099 : outer level. In the case of a constexpr initializer for a
12100 : pointer, avoid converting a null pointer constant to something
12101 : that is not a null pointer constant to avoid a spurious error
12102 : from that second processing. */
12103 9664057 : if (!require_constexpr_value
12104 398 : || !npc
12105 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12106 9664046 : new_value = digest_init (loc,
12107 9664046 : RECORD_OR_UNION_TYPE_P (constructor_type)
12108 8589586 : ? field : constructor_fields
12109 8589586 : ? constructor_fields : constructor_decl,
12110 : type, new_value, origtype, npc,
12111 : int_const_expr, arith_const_expr, strict_string,
12112 : require_constant_value, require_constexpr_value);
12113 9664057 : if (new_value == error_mark_node)
12114 : {
12115 76 : constructor_erroneous = 1;
12116 76 : return;
12117 : }
12118 9663981 : if (require_constant_value || require_constant_elements)
12119 2715752 : constant_expression_warning (new_value);
12120 :
12121 : /* Proceed to check the constness of the original initializer. */
12122 9663981 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12123 : {
12124 3223482 : if (require_constant_value)
12125 : {
12126 0 : error_init (loc, "initializer element is not constant");
12127 0 : value = error_mark_node;
12128 : }
12129 3223482 : else if (require_constant_elements)
12130 21 : pedwarn (loc, OPT_Wpedantic,
12131 : "initializer element is not computable at load time");
12132 : }
12133 6440499 : else if (!maybe_const
12134 54 : && (require_constant_value || require_constant_elements))
12135 28 : pedwarn_init (loc, OPT_Wpedantic,
12136 : "initializer element is not a constant expression");
12137 : /* digest_init has already carried out the additional checks
12138 : required for 'constexpr' initializers (using the information
12139 : passed to it about whether the original initializer was certain
12140 : kinds of constant expression), so that check does not need to be
12141 : repeated here. */
12142 :
12143 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12144 : enum type. */
12145 9663981 : if (warn_cxx_compat
12146 6900 : && field != NULL_TREE
12147 6900 : && TREE_CODE (field) == FIELD_DECL
12148 464 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12149 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12150 21 : != TYPE_MAIN_VARIANT (type))
12151 9664002 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12152 : {
12153 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12154 21 : if (checktype != error_mark_node
12155 21 : && (TYPE_MAIN_VARIANT (checktype)
12156 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12157 9 : warning_init (loc, OPT_Wc___compat,
12158 : "enum conversion in initialization is invalid in C++");
12159 : }
12160 :
12161 : /* If this field is empty and does not have side effects (and is not at
12162 : the end of structure), don't do anything other than checking the
12163 : initializer. */
12164 9663981 : if (field
12165 9663981 : && (TREE_TYPE (field) == error_mark_node
12166 9663546 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12167 9663237 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12168 92 : && !TREE_SIDE_EFFECTS (new_value)
12169 63 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12170 63 : || DECL_CHAIN (field)))))
12171 : return;
12172 :
12173 : /* Finally, set VALUE to the initializer value digested above. */
12174 9663953 : value = new_value;
12175 :
12176 : /* If this element doesn't come next in sequence,
12177 : put it on constructor_pending_elts. */
12178 9663953 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12179 9663953 : && (!constructor_incremental
12180 1722857 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12181 1722423 : || (TREE_CODE (value) == RAW_DATA_CST
12182 239 : && constructor_pending_elts
12183 66 : && pending)))
12184 : {
12185 1341 : if (constructor_incremental
12186 1341 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12187 405 : || (TREE_CODE (value) == RAW_DATA_CST
12188 14 : && constructor_pending_elts
12189 14 : && pending)))
12190 48 : set_nonincremental_init (braced_init_obstack);
12191 :
12192 1341 : add_pending_init (loc, field, value, origtype, implicit,
12193 : braced_init_obstack);
12194 1341 : return;
12195 : }
12196 9662612 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12197 1042968 : && (!constructor_incremental
12198 1041302 : || field != constructor_unfilled_fields))
12199 : {
12200 : /* We do this for records but not for unions. In a union,
12201 : no matter which field is specified, it can be initialized
12202 : right away since it starts at the beginning of the union. */
12203 2889 : if (constructor_incremental)
12204 : {
12205 1223 : if (!constructor_unfilled_fields)
12206 39 : set_nonincremental_init (braced_init_obstack);
12207 : else
12208 : {
12209 1184 : tree bitpos, unfillpos;
12210 :
12211 1184 : bitpos = bit_position (field);
12212 1184 : unfillpos = bit_position (constructor_unfilled_fields);
12213 :
12214 1184 : if (tree_int_cst_lt (bitpos, unfillpos))
12215 4 : set_nonincremental_init (braced_init_obstack);
12216 : }
12217 : }
12218 :
12219 2889 : add_pending_init (loc, field, value, origtype, implicit,
12220 : braced_init_obstack);
12221 2889 : return;
12222 : }
12223 9659723 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12224 9659723 : && !vec_safe_is_empty (constructor_elements))
12225 : {
12226 62 : if (!implicit)
12227 : {
12228 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12229 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12230 : "initialized field with side-effects overwritten");
12231 9 : else if (warn_override_init)
12232 6 : warning_init (loc, OPT_Woverride_init,
12233 : "initialized field overwritten");
12234 : }
12235 :
12236 : /* We can have just one union field set. */
12237 62 : constructor_elements = NULL;
12238 : }
12239 :
12240 : /* Otherwise, output this element either to
12241 : constructor_elements or to the assembler file. */
12242 :
12243 9659723 : constructor_elt celt = {field, value};
12244 9659723 : vec_safe_push (constructor_elements, celt);
12245 :
12246 : /* Advance the variable that indicates sequential elements output. */
12247 9659723 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12248 : {
12249 1722418 : tree inc = bitsize_one_node;
12250 1722418 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12251 234 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12252 1722418 : constructor_unfilled_index
12253 1722418 : = size_binop_loc (input_location, PLUS_EXPR,
12254 : constructor_unfilled_index, inc);
12255 : }
12256 7937305 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12257 : {
12258 1040079 : constructor_unfilled_fields
12259 1040079 : = DECL_CHAIN (constructor_unfilled_fields);
12260 :
12261 : /* Skip any nameless bit fields. */
12262 1040079 : while (constructor_unfilled_fields != NULL_TREE
12263 1040180 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12264 101 : constructor_unfilled_fields
12265 101 : = DECL_CHAIN (constructor_unfilled_fields);
12266 : }
12267 6897226 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12268 31450 : constructor_unfilled_fields = NULL_TREE;
12269 :
12270 : /* Now output any pending elements which have become next. */
12271 9659723 : if (pending)
12272 9653537 : output_pending_init_elements (0, braced_init_obstack);
12273 : }
12274 :
12275 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12276 : comes before field2, 1 if field1 comes after field2 and
12277 : 0 if field1 == field2. */
12278 :
12279 : static int
12280 7104 : init_field_decl_cmp (tree field1, tree field2)
12281 : {
12282 7104 : if (field1 == field2)
12283 : return 0;
12284 :
12285 3342 : tree bitpos1 = bit_position (field1);
12286 3342 : tree bitpos2 = bit_position (field2);
12287 3342 : if (tree_int_cst_equal (bitpos1, bitpos2))
12288 : {
12289 : /* If one of the fields has non-zero bitsize, then that
12290 : field must be the last one in a sequence of zero
12291 : sized fields, fields after it will have bigger
12292 : bit_position. */
12293 32 : if (TREE_TYPE (field1) != error_mark_node
12294 32 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12295 64 : && integer_nonzerop (TREE_TYPE (field1)))
12296 : return 1;
12297 32 : if (TREE_TYPE (field2) != error_mark_node
12298 32 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12299 57 : && integer_nonzerop (TREE_TYPE (field2)))
12300 : return -1;
12301 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12302 : which field comes earlier. Walk chains of both
12303 : fields, so that if field1 and field2 are close to each
12304 : other in either order, it is found soon even for large
12305 : sequences of zero sized fields. */
12306 : tree f1 = field1, f2 = field2;
12307 32 : while (1)
12308 : {
12309 32 : f1 = DECL_CHAIN (f1);
12310 32 : f2 = DECL_CHAIN (f2);
12311 32 : if (f1 == NULL_TREE)
12312 : {
12313 7 : gcc_assert (f2);
12314 : return 1;
12315 : }
12316 25 : if (f2 == NULL_TREE)
12317 : return -1;
12318 9 : if (f1 == field2)
12319 : return -1;
12320 0 : if (f2 == field1)
12321 : return 1;
12322 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12323 : return 1;
12324 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12325 : return -1;
12326 : }
12327 : }
12328 3310 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12329 : return -1;
12330 : else
12331 1948 : return 1;
12332 : }
12333 :
12334 : /* Output any pending elements which have become next.
12335 : As we output elements, constructor_unfilled_{fields,index}
12336 : advances, which may cause other elements to become next;
12337 : if so, they too are output.
12338 :
12339 : If ALL is 0, we return when there are
12340 : no more pending elements to output now.
12341 :
12342 : If ALL is 1, we output space as necessary so that
12343 : we can output all the pending elements. */
12344 : static void
12345 11729307 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12346 : {
12347 11729307 : struct init_node *elt = constructor_pending_elts;
12348 11730469 : tree next;
12349 :
12350 11730469 : retry:
12351 :
12352 : /* Look through the whole pending tree.
12353 : If we find an element that should be output now,
12354 : output it. Otherwise, set NEXT to the element
12355 : that comes first among those still pending. */
12356 :
12357 11730469 : next = NULL_TREE;
12358 11742638 : while (elt)
12359 : {
12360 15043 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12361 : {
12362 6812 : if (tree_int_cst_equal (elt->purpose,
12363 : constructor_unfilled_index))
12364 2829 : output_init_element (input_location, elt->value, elt->origtype,
12365 2829 : true, TREE_TYPE (constructor_type),
12366 : constructor_unfilled_index, false, false,
12367 : braced_init_obstack);
12368 3983 : else if (tree_int_cst_lt (constructor_unfilled_index,
12369 3983 : elt->purpose))
12370 : {
12371 : /* Advance to the next smaller node. */
12372 1133 : if (elt->left)
12373 : elt = elt->left;
12374 : else
12375 : {
12376 : /* We have reached the smallest node bigger than the
12377 : current unfilled index. Fill the space first. */
12378 345 : next = elt->purpose;
12379 345 : break;
12380 : }
12381 : }
12382 : else
12383 : {
12384 : /* Advance to the next bigger node. */
12385 2850 : if (elt->right)
12386 : elt = elt->right;
12387 : else
12388 : {
12389 : /* We have reached the biggest node in a subtree. Find
12390 : the parent of it, which is the next bigger node. */
12391 2850 : while (elt->parent && elt->parent->right == elt)
12392 : elt = elt->parent;
12393 1896 : elt = elt->parent;
12394 2679 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12395 783 : elt->purpose))
12396 : {
12397 22 : next = elt->purpose;
12398 22 : break;
12399 : }
12400 : }
12401 : }
12402 : }
12403 8231 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12404 : {
12405 : /* If the current record is complete we are done. */
12406 8231 : if (constructor_unfilled_fields == NULL_TREE)
12407 : break;
12408 :
12409 6628 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12410 : elt->purpose);
12411 6628 : if (cmp == 0)
12412 3357 : output_init_element (input_location, elt->value, elt->origtype,
12413 3357 : true, TREE_TYPE (elt->purpose),
12414 : elt->purpose, false, false,
12415 : braced_init_obstack);
12416 3271 : else if (cmp < 0)
12417 : {
12418 : /* Advance to the next smaller node. */
12419 1316 : if (elt->left)
12420 : elt = elt->left;
12421 : else
12422 : {
12423 : /* We have reached the smallest node bigger than the
12424 : current unfilled field. Fill the space first. */
12425 833 : next = elt->purpose;
12426 833 : break;
12427 : }
12428 : }
12429 : else
12430 : {
12431 : /* Advance to the next bigger node. */
12432 1955 : if (elt->right)
12433 : elt = elt->right;
12434 : else
12435 : {
12436 : /* We have reached the biggest node in a subtree. Find
12437 : the parent of it, which is the next bigger node. */
12438 1025 : while (elt->parent && elt->parent->right == elt)
12439 : elt = elt->parent;
12440 789 : elt = elt->parent;
12441 789 : if (elt
12442 789 : && init_field_decl_cmp (constructor_unfilled_fields,
12443 : elt->purpose) < 0)
12444 : {
12445 71 : next = elt->purpose;
12446 71 : break;
12447 : }
12448 : }
12449 : }
12450 : }
12451 : }
12452 :
12453 : /* Ordinarily return, but not if we want to output all
12454 : and there are elements left. */
12455 11730469 : if (!(all && next != NULL_TREE))
12456 11729307 : return;
12457 :
12458 : /* If it's not incremental, just skip over the gap, so that after
12459 : jumping to retry we will output the next successive element. */
12460 1162 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12461 834 : constructor_unfilled_fields = next;
12462 328 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12463 328 : constructor_unfilled_index = next;
12464 :
12465 : /* ELT now points to the node in the pending tree with the next
12466 : initializer to output. */
12467 1162 : goto retry;
12468 : }
12469 :
12470 : /* Expression VALUE coincides with the start of type TYPE in a braced
12471 : initializer. Return true if we should treat VALUE as initializing
12472 : the first element of TYPE, false if we should treat it as initializing
12473 : TYPE as a whole.
12474 :
12475 : If the initializer is clearly invalid, the question becomes:
12476 : which choice gives the best error message? */
12477 :
12478 : static bool
12479 3491291 : initialize_elementwise_p (tree type, tree value)
12480 : {
12481 3491291 : if (type == error_mark_node || value == error_mark_node)
12482 : return false;
12483 :
12484 3491038 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12485 :
12486 3491038 : tree value_type = TREE_TYPE (value);
12487 3491038 : if (value_type == error_mark_node)
12488 : return false;
12489 :
12490 : /* GNU vectors can be initialized elementwise. However, treat any
12491 : kind of vector value as initializing the vector type as a whole,
12492 : regardless of whether the value is a GNU vector. Such initializers
12493 : are valid if and only if they would have been valid in a non-braced
12494 : initializer like:
12495 :
12496 : TYPE foo = VALUE;
12497 :
12498 : so recursing into the vector type would be at best confusing or at
12499 : worst wrong. For example, when -flax-vector-conversions is in effect,
12500 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12501 : have different element types and different numbers of elements. */
12502 3491038 : if (gnu_vector_type_p (type))
12503 19598 : return !VECTOR_TYPE_P (value_type);
12504 :
12505 3471440 : if (AGGREGATE_TYPE_P (type))
12506 1743251 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12507 :
12508 : return false;
12509 : }
12510 :
12511 : /* Helper function for process_init_element. Split first element of
12512 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12513 :
12514 : static inline tree
12515 9657991 : maybe_split_raw_data (tree value, tree *raw_data)
12516 : {
12517 9657991 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12518 : return value;
12519 211 : *raw_data = value;
12520 211 : value = build_int_cst (integer_type_node,
12521 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12522 211 : ++RAW_DATA_POINTER (*raw_data);
12523 211 : --RAW_DATA_LENGTH (*raw_data);
12524 211 : return value;
12525 : }
12526 :
12527 : /* Return non-zero if c_parser_initval should attempt to optimize
12528 : large initializers into RAW_DATA_CST. In that case return how
12529 : many elements to optimize at most. */
12530 :
12531 : unsigned
12532 3006054 : c_maybe_optimize_large_byte_initializer (void)
12533 : {
12534 3006054 : if (!constructor_type
12535 3005984 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12536 211866 : || constructor_stack->implicit)
12537 : return 0;
12538 209101 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12539 209101 : if (TREE_CODE (elttype) != INTEGER_TYPE
12540 209101 : && TREE_CODE (elttype) != BITINT_TYPE)
12541 : return 0;
12542 186126 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12543 27955 : || constructor_stack->replacement_value.value
12544 27955 : || (COMPLETE_TYPE_P (constructor_type)
12545 26310 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12546 214081 : || constructor_range_stack)
12547 : return 0;
12548 27955 : if (constructor_max_index == NULL_TREE)
12549 : return INT_MAX;
12550 26310 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12551 26310 : || integer_all_onesp (constructor_max_index))
12552 : return 0;
12553 21540 : widest_int w = wi::to_widest (constructor_max_index);
12554 21540 : w -= wi::to_widest (constructor_index);
12555 21540 : w += 1;
12556 21540 : if (w < 64)
12557 : return 0;
12558 6894 : if (w > INT_MAX)
12559 : return INT_MAX;
12560 6894 : return w.to_uhwi ();
12561 3006054 : }
12562 :
12563 : /* Add one non-braced element to the current constructor level.
12564 : This adjusts the current position within the constructor's type.
12565 : This may also start or terminate implicit levels
12566 : to handle a partly-braced initializer.
12567 :
12568 : Once this has found the correct level for the new element,
12569 : it calls output_init_element.
12570 :
12571 : IMPLICIT is true if value comes from pop_init_level (1),
12572 : the new initializer has been merged with the existing one
12573 : and thus no warnings should be emitted about overriding an
12574 : existing initializer. */
12575 :
12576 : void
12577 9648289 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12578 : struct obstack * braced_init_obstack)
12579 : {
12580 9648289 : tree orig_value = value.value;
12581 19296578 : int string_flag
12582 9648289 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12583 9648289 : bool strict_string = value.original_code == STRING_CST;
12584 9648289 : bool was_designated = designator_depth != 0;
12585 9648289 : tree raw_data = NULL_TREE;
12586 :
12587 9648501 : retry:
12588 9648501 : designator_depth = 0;
12589 9648501 : designator_erroneous = 0;
12590 :
12591 9648501 : if (!implicit && value.value && !integer_zerop (value.value))
12592 7618377 : constructor_zeroinit = 0;
12593 :
12594 : /* Handle superfluous braces around string cst as in
12595 : char x[] = {"foo"}; */
12596 9648501 : if (constructor_type
12597 9648373 : && !was_designated
12598 9611367 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12599 1708365 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12600 10387324 : && integer_zerop (constructor_unfilled_index))
12601 : {
12602 163206 : if (constructor_stack->replacement_value.value)
12603 : {
12604 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12605 409 : return;
12606 : }
12607 163198 : else if (string_flag)
12608 : {
12609 138 : constructor_stack->replacement_value = value;
12610 138 : return;
12611 : }
12612 : }
12613 :
12614 9648355 : if (constructor_stack->replacement_value.value != NULL_TREE)
12615 : {
12616 0 : error_init (loc, "excess elements in struct initializer");
12617 0 : return;
12618 : }
12619 :
12620 : /* Ignore elements of a brace group if it is entirely superfluous
12621 : and has already been diagnosed, or if the type is erroneous. */
12622 9648355 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12623 : return;
12624 :
12625 : /* Ignore elements of an initializer for a variable-size type.
12626 : Those are diagnosed in the parser (empty initializer braces are OK). */
12627 9648158 : if (COMPLETE_TYPE_P (constructor_type)
12628 9648158 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12629 : return;
12630 :
12631 8942777 : if (!implicit && warn_designated_init && !was_designated
12632 8908089 : && TREE_CODE (constructor_type) == RECORD_TYPE
12633 10683313 : && lookup_attribute ("designated_init",
12634 1035213 : TYPE_ATTRIBUTES (constructor_type)))
12635 50 : warning_init (loc,
12636 : OPT_Wdesignated_init,
12637 : "positional initialization of field "
12638 : "in %<struct%> declared with %<designated_init%> attribute");
12639 :
12640 : /* If we've exhausted any levels that didn't have braces,
12641 : pop them now. */
12642 10350079 : while (constructor_stack->implicit)
12643 : {
12644 708550 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12645 703711 : && constructor_fields == NULL_TREE)
12646 701738 : process_init_element (loc,
12647 : pop_init_level (loc, 1, braced_init_obstack,
12648 : last_init_list_comma),
12649 : true, braced_init_obstack);
12650 6812 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12651 2617 : || gnu_vector_type_p (constructor_type))
12652 4839 : && constructor_max_index
12653 11624 : && tree_int_cst_lt (constructor_max_index,
12654 : constructor_index))
12655 241 : process_init_element (loc,
12656 : pop_init_level (loc, 1, braced_init_obstack,
12657 : last_init_list_comma),
12658 : true, braced_init_obstack);
12659 : else
12660 : break;
12661 : }
12662 :
12663 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12664 9648100 : if (constructor_range_stack)
12665 : {
12666 : /* If value is a compound literal and we'll be just using its
12667 : content, don't put it into a SAVE_EXPR. */
12668 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12669 3 : || !require_constant_value)
12670 : {
12671 368 : tree semantic_type = NULL_TREE;
12672 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12673 : {
12674 0 : semantic_type = TREE_TYPE (value.value);
12675 0 : value.value = TREE_OPERAND (value.value, 0);
12676 : }
12677 368 : value.value = save_expr (value.value);
12678 368 : if (semantic_type)
12679 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12680 : value.value);
12681 : }
12682 : }
12683 :
12684 10362344 : while (1)
12685 : {
12686 10362344 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12687 : {
12688 1041331 : tree fieldtype;
12689 1041331 : enum tree_code fieldcode;
12690 :
12691 1041331 : if (constructor_fields == NULL_TREE)
12692 : {
12693 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12694 1280 : break;
12695 : }
12696 :
12697 1040051 : fieldtype = TREE_TYPE (constructor_fields);
12698 1040051 : if (fieldtype != error_mark_node)
12699 1040050 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12700 1040051 : fieldcode = TREE_CODE (fieldtype);
12701 :
12702 : /* Error for non-static initialization of a flexible array member. */
12703 1040051 : if (fieldcode == ARRAY_TYPE
12704 156026 : && !require_constant_value
12705 1892 : && TYPE_SIZE (fieldtype) == NULL_TREE
12706 1040061 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12707 : {
12708 10 : error_init (loc, "non-static initialization of a flexible "
12709 : "array member");
12710 10 : break;
12711 : }
12712 :
12713 : /* Error for initialization of a flexible array member with
12714 : a string constant if the structure is in an array. E.g.:
12715 : struct S { int x; char y[]; };
12716 : struct S s[] = { { 1, "foo" } };
12717 : is invalid. */
12718 1040041 : if (string_flag
12719 1040041 : && fieldcode == ARRAY_TYPE
12720 3353 : && constructor_depth > 1
12721 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12722 1040088 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12723 : {
12724 47 : bool in_array_p = false;
12725 47 : for (struct constructor_stack *p = constructor_stack;
12726 73 : p && p->type; p = p->next)
12727 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12728 : {
12729 : in_array_p = true;
12730 : break;
12731 : }
12732 47 : if (in_array_p)
12733 : {
12734 33 : error_init (loc, "initialization of flexible array "
12735 : "member in a nested context");
12736 33 : break;
12737 : }
12738 : }
12739 :
12740 : /* Accept a string constant to initialize a subarray. */
12741 1040008 : if (value.value != NULL_TREE
12742 1039965 : && fieldcode == ARRAY_TYPE
12743 155940 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12744 1194637 : && string_flag)
12745 : value.value = orig_value;
12746 : /* Otherwise, if we have come to a subaggregate,
12747 : and we don't have an element of its type, push into it. */
12748 1036992 : else if (value.value != NULL_TREE
12749 1036688 : && initialize_elementwise_p (fieldtype, value.value))
12750 : {
12751 304 : push_init_level (loc, 1, braced_init_obstack);
12752 304 : continue;
12753 : }
12754 :
12755 1039704 : value.value = maybe_split_raw_data (value.value, &raw_data);
12756 1039704 : if (value.value)
12757 : {
12758 1039661 : push_member_name (constructor_fields);
12759 1039661 : output_init_element (loc, value.value, value.original_type,
12760 : strict_string, fieldtype,
12761 : constructor_fields, true, implicit,
12762 : braced_init_obstack);
12763 1039661 : RESTORE_SPELLING_DEPTH (constructor_depth);
12764 : }
12765 : else
12766 : /* Do the bookkeeping for an element that was
12767 : directly output as a constructor. */
12768 : {
12769 : /* For a record, keep track of end position of last field. */
12770 43 : if (DECL_SIZE (constructor_fields))
12771 0 : constructor_bit_index
12772 0 : = size_binop_loc (input_location, PLUS_EXPR,
12773 : bit_position (constructor_fields),
12774 0 : DECL_SIZE (constructor_fields));
12775 :
12776 : /* If the current field was the first one not yet written out,
12777 : it isn't now, so update. */
12778 43 : if (constructor_unfilled_fields == constructor_fields)
12779 : {
12780 34 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12781 : /* Skip any nameless bit fields. */
12782 34 : while (constructor_unfilled_fields != 0
12783 34 : && (DECL_UNNAMED_BIT_FIELD
12784 : (constructor_unfilled_fields)))
12785 0 : constructor_unfilled_fields =
12786 0 : DECL_CHAIN (constructor_unfilled_fields);
12787 : }
12788 : }
12789 :
12790 1039704 : constructor_fields = DECL_CHAIN (constructor_fields);
12791 : /* Skip any nameless bit fields at the beginning. */
12792 1039704 : while (constructor_fields != NULL_TREE
12793 1039805 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12794 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12795 : }
12796 9321013 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12797 : {
12798 31513 : tree fieldtype;
12799 31513 : enum tree_code fieldcode;
12800 :
12801 31513 : if (constructor_fields == NULL_TREE)
12802 : {
12803 3 : pedwarn_init (loc, 0,
12804 : "excess elements in union initializer");
12805 3 : break;
12806 : }
12807 :
12808 31510 : fieldtype = TREE_TYPE (constructor_fields);
12809 31510 : if (fieldtype != error_mark_node)
12810 31510 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12811 31510 : fieldcode = TREE_CODE (fieldtype);
12812 :
12813 : /* Warn that traditional C rejects initialization of unions.
12814 : We skip the warning if the value is zero. This is done
12815 : under the assumption that the zero initializer in user
12816 : code appears conditioned on e.g. __STDC__ to avoid
12817 : "missing initializer" warnings and relies on default
12818 : initialization to zero in the traditional C case.
12819 : We also skip the warning if the initializer is designated,
12820 : again on the assumption that this must be conditional on
12821 : __STDC__ anyway (and we've already complained about the
12822 : member-designator already). */
12823 33964 : if (!in_system_header_at (input_location) && !constructor_designated
12824 33065 : && !(value.value && (integer_zerop (value.value)
12825 1448 : || real_zerop (value.value))))
12826 1441 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12827 : "of unions");
12828 :
12829 : /* Error for non-static initialization of a flexible array member. */
12830 31510 : if (fieldcode == ARRAY_TYPE
12831 766 : && !require_constant_value
12832 31567 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12833 : {
12834 3 : error_init (loc, "non-static initialization of a flexible "
12835 : "array member");
12836 3 : break;
12837 : }
12838 :
12839 : /* Error for initialization of a flexible array member with
12840 : a string constant if the structure is in an array. E.g.:
12841 : union U { int x; char y[]; };
12842 : union U s[] = { { 1, "foo" } };
12843 : is invalid. */
12844 31507 : if (string_flag
12845 31507 : && fieldcode == ARRAY_TYPE
12846 7 : && constructor_depth > 1
12847 31511 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12848 : {
12849 3 : bool in_array_p = false;
12850 3 : for (struct constructor_stack *p = constructor_stack;
12851 3 : p && p->type; p = p->next)
12852 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12853 : {
12854 : in_array_p = true;
12855 : break;
12856 : }
12857 3 : if (in_array_p)
12858 : {
12859 3 : error_init (loc, "initialization of flexible array "
12860 : "member in a nested context");
12861 3 : break;
12862 : }
12863 : }
12864 :
12865 : /* Accept a string constant to initialize a subarray. */
12866 31504 : if (value.value != NULL_TREE
12867 31504 : && fieldcode == ARRAY_TYPE
12868 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12869 32259 : && string_flag)
12870 : value.value = orig_value;
12871 : /* Otherwise, if we have come to a subaggregate,
12872 : and we don't have an element of its type, push into it. */
12873 31550 : else if (value.value != NULL_TREE
12874 31500 : && initialize_elementwise_p (fieldtype, value.value))
12875 : {
12876 50 : push_init_level (loc, 1, braced_init_obstack);
12877 50 : continue;
12878 : }
12879 :
12880 31454 : value.value = maybe_split_raw_data (value.value, &raw_data);
12881 31454 : if (value.value)
12882 : {
12883 31454 : push_member_name (constructor_fields);
12884 31454 : output_init_element (loc, value.value, value.original_type,
12885 : strict_string, fieldtype,
12886 : constructor_fields, true, implicit,
12887 : braced_init_obstack);
12888 31454 : RESTORE_SPELLING_DEPTH (constructor_depth);
12889 : }
12890 : else
12891 : /* Do the bookkeeping for an element that was
12892 : directly output as a constructor. */
12893 : {
12894 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12895 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12896 : }
12897 :
12898 31454 : constructor_fields = NULL_TREE;
12899 : }
12900 9289500 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12901 : {
12902 2423642 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12903 2423642 : enum tree_code eltcode = TREE_CODE (elttype);
12904 :
12905 : /* Accept a string constant to initialize a subarray. */
12906 2423642 : if (value.value != NULL_TREE
12907 2423642 : && eltcode == ARRAY_TYPE
12908 7031 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12909 2429190 : && string_flag)
12910 : value.value = orig_value;
12911 : /* Otherwise, if we have come to a subaggregate,
12912 : and we don't have an element of its type, push into it. */
12913 3125472 : else if (value.value != NULL_TREE
12914 2423146 : && initialize_elementwise_p (elttype, value.value))
12915 : {
12916 702326 : push_init_level (loc, 1, braced_init_obstack);
12917 702326 : continue;
12918 : }
12919 :
12920 1721316 : if (constructor_max_index != NULL_TREE
12921 678068 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12922 677953 : || integer_all_onesp (constructor_max_index))
12923 : /* For VLA we got an error already. */
12924 1721431 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
12925 : {
12926 113 : pedwarn_init (loc, 0,
12927 : "excess elements in array initializer");
12928 113 : break;
12929 : }
12930 :
12931 1721203 : if (value.value
12932 1721203 : && TREE_CODE (value.value) == RAW_DATA_CST
12933 197 : && RAW_DATA_LENGTH (value.value) > 1
12934 197 : && (TREE_CODE (elttype) == INTEGER_TYPE
12935 197 : || TREE_CODE (elttype) == BITINT_TYPE)
12936 197 : && TYPE_PRECISION (elttype) == CHAR_BIT
12937 1721400 : && (constructor_max_index == NULL_TREE
12938 59 : || tree_int_cst_lt (constructor_index,
12939 : constructor_max_index)))
12940 : {
12941 197 : unsigned int len = RAW_DATA_LENGTH (value.value);
12942 197 : if (constructor_max_index)
12943 : {
12944 59 : widest_int w = wi::to_widest (constructor_max_index);
12945 59 : w -= wi::to_widest (constructor_index);
12946 59 : w += 1;
12947 59 : if (w < len)
12948 3 : len = w.to_uhwi ();
12949 59 : }
12950 197 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12951 : {
12952 3 : raw_data = copy_node (value.value);
12953 3 : RAW_DATA_LENGTH (raw_data) -= len;
12954 3 : RAW_DATA_POINTER (raw_data) += len;
12955 3 : RAW_DATA_LENGTH (value.value) = len;
12956 : }
12957 197 : TREE_TYPE (value.value) = elttype;
12958 197 : push_array_bounds (tree_to_uhwi (constructor_index));
12959 197 : output_init_element (loc, value.value, value.original_type,
12960 : false, elttype, constructor_index, true,
12961 : implicit, braced_init_obstack);
12962 197 : RESTORE_SPELLING_DEPTH (constructor_depth);
12963 197 : constructor_index
12964 197 : = size_binop_loc (input_location, PLUS_EXPR,
12965 197 : constructor_index, bitsize_int (len));
12966 : }
12967 : else
12968 : {
12969 1721006 : value.value = maybe_split_raw_data (value.value, &raw_data);
12970 : /* Now output the actual element. */
12971 1721006 : if (value.value)
12972 : {
12973 1721006 : push_array_bounds (tree_to_uhwi (constructor_index));
12974 1721006 : output_init_element (loc, value.value, value.original_type,
12975 : strict_string, elttype,
12976 : constructor_index, true, implicit,
12977 : braced_init_obstack);
12978 1721006 : RESTORE_SPELLING_DEPTH (constructor_depth);
12979 : }
12980 :
12981 1721006 : constructor_index
12982 1721006 : = size_binop_loc (input_location, PLUS_EXPR,
12983 : constructor_index, bitsize_one_node);
12984 :
12985 1721006 : if (!value.value)
12986 : /* If we are doing the bookkeeping for an element that was
12987 : directly output as a constructor, we must update
12988 : constructor_unfilled_index. */
12989 0 : constructor_unfilled_index = constructor_index;
12990 : }
12991 : }
12992 6865858 : else if (gnu_vector_type_p (constructor_type))
12993 : {
12994 6865366 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12995 :
12996 : /* Do a basic check of initializer size. Note that vectors
12997 : may not always have a fixed size derived from their type. */
12998 6865366 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12999 6865366 : tree_to_poly_uint64 (constructor_index)))
13000 : {
13001 : /* Diagose VLA out-of-bounds as errors. */
13002 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
13003 2 : pedwarn_init (loc, 0,
13004 : "excess elements in vector initializer");
13005 : else
13006 : error_init (loc, "excess elements in vector initializer");
13007 :
13008 : break;
13009 : }
13010 :
13011 6865364 : value.value = maybe_split_raw_data (value.value, &raw_data);
13012 : /* Now output the actual element. */
13013 6865364 : if (value.value)
13014 : {
13015 6865364 : if (TREE_CODE (value.value) == VECTOR_CST)
13016 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
13017 6865364 : output_init_element (loc, value.value, value.original_type,
13018 : strict_string, elttype,
13019 : constructor_index, true, implicit,
13020 : braced_init_obstack);
13021 : }
13022 :
13023 6865364 : constructor_index
13024 6865364 : = size_binop_loc (input_location,
13025 : PLUS_EXPR, constructor_index, bitsize_one_node);
13026 :
13027 6865364 : if (!value.value)
13028 : /* If we are doing the bookkeeping for an element that was
13029 : directly output as a constructor, we must update
13030 : constructor_unfilled_index. */
13031 0 : constructor_unfilled_index = constructor_index;
13032 : }
13033 :
13034 : /* Handle the sole element allowed in a braced initializer
13035 : for a scalar variable. */
13036 492 : else if (constructor_type != error_mark_node
13037 492 : && constructor_fields == NULL_TREE)
13038 : {
13039 29 : pedwarn_init (loc, 0,
13040 : "excess elements in scalar initializer");
13041 29 : break;
13042 : }
13043 : else
13044 : {
13045 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
13046 463 : if (value.value)
13047 463 : output_init_element (loc, value.value, value.original_type,
13048 : strict_string, constructor_type,
13049 : NULL_TREE, true, implicit,
13050 : braced_init_obstack);
13051 463 : constructor_fields = NULL_TREE;
13052 : }
13053 :
13054 : /* Handle range initializers either at this level or anywhere higher
13055 : in the designator stack. */
13056 9658188 : if (constructor_range_stack)
13057 : {
13058 11564 : struct constructor_range_stack *p, *range_stack;
13059 11564 : int finish = 0;
13060 :
13061 11564 : range_stack = constructor_range_stack;
13062 11564 : constructor_range_stack = 0;
13063 11564 : while (constructor_stack != range_stack->stack)
13064 : {
13065 0 : gcc_assert (constructor_stack->implicit);
13066 0 : process_init_element (loc,
13067 : pop_init_level (loc, 1,
13068 : braced_init_obstack,
13069 : last_init_list_comma),
13070 : true, braced_init_obstack);
13071 : }
13072 325 : for (p = range_stack;
13073 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13074 325 : p = p->prev)
13075 : {
13076 325 : gcc_assert (constructor_stack->implicit);
13077 325 : process_init_element (loc,
13078 : pop_init_level (loc, 1,
13079 : braced_init_obstack,
13080 : last_init_list_comma),
13081 : true, braced_init_obstack);
13082 : }
13083 :
13084 11564 : p->index = size_binop_loc (input_location,
13085 : PLUS_EXPR, p->index, bitsize_one_node);
13086 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13087 11564 : finish = 1;
13088 :
13089 11889 : while (1)
13090 : {
13091 11889 : constructor_index = p->index;
13092 11889 : constructor_fields = p->fields;
13093 11889 : if (finish && p->range_end && p->index == p->range_start)
13094 : {
13095 8 : finish = 0;
13096 8 : p->prev = 0;
13097 : }
13098 11889 : p = p->next;
13099 11889 : if (!p)
13100 : break;
13101 325 : finish_implicit_inits (loc, braced_init_obstack);
13102 325 : push_init_level (loc, 2, braced_init_obstack);
13103 325 : p->stack = constructor_stack;
13104 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13105 33 : p->index = p->range_start;
13106 : }
13107 :
13108 11564 : if (!finish)
13109 11193 : constructor_range_stack = range_stack;
13110 11564 : continue;
13111 11564 : }
13112 :
13113 : break;
13114 : }
13115 :
13116 9648100 : constructor_range_stack = 0;
13117 :
13118 9648100 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13119 : {
13120 212 : gcc_assert (!string_flag && !was_designated);
13121 212 : value.value = raw_data;
13122 212 : raw_data = NULL_TREE;
13123 212 : goto retry;
13124 : }
13125 : }
13126 :
13127 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13128 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13129 : an ASM_EXPR node). */
13130 : tree
13131 190263 : build_asm_stmt (bool is_volatile, tree args)
13132 : {
13133 190263 : if (is_volatile)
13134 150403 : ASM_VOLATILE_P (args) = 1;
13135 190263 : return add_stmt (args);
13136 : }
13137 :
13138 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13139 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13140 : SIMPLE indicates whether there was anything at all after the
13141 : string in the asm expression -- asm("blah") and asm("blah" : )
13142 : are subtly different. We use a ASM_EXPR node to represent this.
13143 : LOC is the location of the asm, and IS_INLINE says whether this
13144 : is asm inline. */
13145 : tree
13146 190315 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13147 : tree clobbers, tree labels, bool simple, bool is_inline)
13148 : {
13149 190315 : tree tail;
13150 190315 : tree args;
13151 190315 : int i;
13152 190315 : const char *constraint;
13153 190315 : const char **oconstraints;
13154 190315 : bool allows_mem, allows_reg, is_inout;
13155 190315 : int ninputs, noutputs;
13156 :
13157 190315 : ninputs = list_length (inputs);
13158 190315 : noutputs = list_length (outputs);
13159 190315 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13160 :
13161 190315 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13162 :
13163 : /* Remove output conversions that change the type but not the mode. */
13164 731272 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13165 : {
13166 350642 : tree output = TREE_VALUE (tail);
13167 :
13168 350642 : output = c_fully_fold (output, false, NULL, true);
13169 :
13170 : /* ??? Really, this should not be here. Users should be using a
13171 : proper lvalue, dammit. But there's a long history of using casts
13172 : in the output operands. In cases like longlong.h, this becomes a
13173 : primitive form of typechecking -- if the cast can be removed, then
13174 : the output operand had a type of the proper width; otherwise we'll
13175 : get an error. Gross, but ... */
13176 350642 : STRIP_NOPS (output);
13177 :
13178 350642 : if (!lvalue_or_else (loc, output, lv_asm))
13179 4 : output = error_mark_node;
13180 :
13181 350642 : if (output != error_mark_node
13182 350642 : && (TREE_READONLY (output)
13183 350635 : || TYPE_READONLY (TREE_TYPE (output))
13184 350635 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13185 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13186 2 : readonly_error (loc, output, lv_asm);
13187 :
13188 350642 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13189 350642 : oconstraints[i] = constraint;
13190 :
13191 350642 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13192 : &allows_mem, &allows_reg, &is_inout,
13193 : nullptr))
13194 : {
13195 : /* If the operand is going to end up in memory,
13196 : mark it addressable. */
13197 350634 : if (!allows_reg && !c_mark_addressable (output))
13198 3 : output = error_mark_node;
13199 1422 : if (!(!allows_reg && allows_mem)
13200 349212 : && output != error_mark_node
13201 699844 : && VOID_TYPE_P (TREE_TYPE (output)))
13202 : {
13203 4 : error_at (loc, "invalid use of void expression");
13204 4 : output = error_mark_node;
13205 : }
13206 350634 : if (allows_reg && current_function_decl == NULL_TREE)
13207 : {
13208 1 : error_at (loc, "constraint allows registers outside of "
13209 : "a function");
13210 1 : output = error_mark_node;
13211 : }
13212 : }
13213 : else
13214 8 : output = error_mark_node;
13215 :
13216 350642 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13217 : {
13218 7 : if (TREE_SIDE_EFFECTS (output))
13219 : {
13220 0 : error_at (loc, "side-effects in output operand outside "
13221 : "of a function");
13222 0 : output = error_mark_node;
13223 : }
13224 : else
13225 : {
13226 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13227 7 : if (addr == error_mark_node)
13228 : output = error_mark_node;
13229 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13230 : {
13231 1 : error_at (loc, "output operand outside of a function is not "
13232 : "constant");
13233 1 : output = error_mark_node;
13234 : }
13235 : }
13236 : }
13237 350635 : else if (output != error_mark_node && strstr (constraint, "-"))
13238 : {
13239 1 : error_at (loc, "%<-%> modifier used inside of a function");
13240 1 : output = error_mark_node;
13241 : }
13242 :
13243 350642 : TREE_VALUE (tail) = output;
13244 : }
13245 :
13246 556630 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13247 : {
13248 366315 : tree input;
13249 :
13250 366315 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13251 366315 : input = TREE_VALUE (tail);
13252 :
13253 366315 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13254 : oconstraints, &allows_mem, &allows_reg,
13255 : nullptr))
13256 : {
13257 : /* If the operand is going to end up in memory,
13258 : mark it addressable. */
13259 366307 : if (!allows_reg && allows_mem)
13260 : {
13261 1363 : input = c_fully_fold (input, false, NULL, true);
13262 :
13263 : /* Strip the nops as we allow this case. FIXME, this really
13264 : should be rejected or made deprecated. */
13265 1363 : STRIP_NOPS (input);
13266 1363 : if (!c_mark_addressable (input))
13267 2 : input = error_mark_node;
13268 : }
13269 : else
13270 : {
13271 364944 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13272 : true, false),
13273 : false, NULL);
13274 :
13275 364944 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13276 : {
13277 4 : error_at (loc, "invalid use of void expression");
13278 4 : input = error_mark_node;
13279 : }
13280 : }
13281 366307 : if (allows_reg && current_function_decl == NULL_TREE)
13282 : {
13283 1 : error_at (loc, "constraint allows registers outside of "
13284 : "a function");
13285 1 : input = error_mark_node;
13286 : }
13287 366307 : if (constraint[0] == ':' && input != error_mark_node)
13288 : {
13289 35 : tree t = input;
13290 35 : STRIP_NOPS (t);
13291 35 : if (TREE_CODE (t) != ADDR_EXPR
13292 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13293 23 : || (VAR_P (TREE_OPERAND (t, 0))
13294 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13295 : {
13296 5 : error_at (loc, "%<:%> constraint operand is not address "
13297 : "of a function or non-automatic variable");
13298 5 : input = error_mark_node;
13299 : }
13300 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13301 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13302 : }
13303 : }
13304 : else
13305 8 : input = error_mark_node;
13306 :
13307 366315 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13308 : {
13309 58 : if (TREE_SIDE_EFFECTS (input))
13310 : {
13311 2 : error_at (loc, "side-effects in input operand outside "
13312 : "of a function");
13313 2 : input = error_mark_node;
13314 : }
13315 : else
13316 : {
13317 56 : tree tem = input;
13318 56 : if (allows_mem && lvalue_p (input))
13319 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13320 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13321 : {
13322 2 : error_at (loc, "input operand outside of a function is not "
13323 : "constant");
13324 2 : input = error_mark_node;
13325 : }
13326 : }
13327 : }
13328 366257 : else if (input != error_mark_node && strstr (constraint, "-"))
13329 : {
13330 3 : error_at (loc, "%<-%> modifier used inside of a function");
13331 3 : input = error_mark_node;
13332 : }
13333 :
13334 366315 : TREE_VALUE (tail) = input;
13335 : }
13336 :
13337 190315 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13338 :
13339 : /* asm statements without outputs, including simple ones, are treated
13340 : as volatile. */
13341 190315 : ASM_BASIC_P (args) = simple;
13342 190315 : ASM_VOLATILE_P (args) = (noutputs == 0);
13343 190315 : ASM_INLINE_P (args) = is_inline;
13344 :
13345 190315 : return args;
13346 : }
13347 :
13348 : /* Generate a goto statement to LABEL. LOC is the location of the
13349 : GOTO. */
13350 :
13351 : tree
13352 83373 : c_finish_goto_label (location_t loc, tree label)
13353 : {
13354 83373 : tree decl = lookup_label_for_goto (loc, label);
13355 83373 : if (!decl)
13356 : return NULL_TREE;
13357 83373 : TREE_USED (decl) = 1;
13358 83373 : mark_decl_used (decl, false);
13359 83373 : {
13360 83373 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13361 83373 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13362 83373 : SET_EXPR_LOCATION (t, loc);
13363 83373 : return add_stmt (t);
13364 : }
13365 : }
13366 :
13367 : /* Generate a computed goto statement to EXPR. LOC is the location of
13368 : the GOTO. */
13369 :
13370 : tree
13371 957 : c_finish_goto_ptr (location_t loc, c_expr val)
13372 : {
13373 957 : tree expr = val.value;
13374 957 : tree t;
13375 957 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13376 957 : if (expr != error_mark_node
13377 956 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13378 962 : && !null_pointer_constant_p (expr))
13379 : {
13380 2 : error_at (val.get_location (),
13381 : "computed goto must be pointer type");
13382 2 : expr = build_zero_cst (ptr_type_node);
13383 : }
13384 957 : expr = c_fully_fold (expr, false, NULL);
13385 957 : expr = convert (ptr_type_node, expr);
13386 957 : t = build1 (GOTO_EXPR, void_type_node, expr);
13387 957 : SET_EXPR_LOCATION (t, loc);
13388 957 : return add_stmt (t);
13389 : }
13390 :
13391 : /* Generate a C `return' statement. RETVAL is the expression for what
13392 : to return, or a null pointer for `return;' with no value. LOC is
13393 : the location of the return statement, or the location of the expression,
13394 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13395 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13396 : attribute. */
13397 :
13398 : tree
13399 35644529 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13400 : {
13401 35644529 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13402 35644529 : bool no_warning = false;
13403 35644529 : bool npc = false;
13404 :
13405 : /* Use the expansion point to handle cases such as returning NULL
13406 : in a function returning void. */
13407 35644529 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13408 :
13409 35644529 : if (TREE_THIS_VOLATILE (current_function_decl))
13410 23 : warning_at (xloc, 0,
13411 : "function declared %<noreturn%> has a %<return%> statement");
13412 :
13413 35644529 : set_musttail_on_return (retval, xloc, musttail_p);
13414 :
13415 35644529 : if (retval)
13416 : {
13417 35622974 : tree semantic_type = NULL_TREE;
13418 35622974 : npc = null_pointer_constant_p (retval);
13419 35622974 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13420 : {
13421 92 : semantic_type = TREE_TYPE (retval);
13422 92 : retval = TREE_OPERAND (retval, 0);
13423 : }
13424 35622974 : retval = c_fully_fold (retval, false, NULL);
13425 35622974 : if (semantic_type
13426 35622974 : && valtype != NULL_TREE
13427 92 : && TREE_CODE (valtype) != VOID_TYPE)
13428 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13429 : }
13430 :
13431 35622974 : if (!retval)
13432 : {
13433 21555 : current_function_returns_null = 1;
13434 21555 : if ((warn_return_type >= 0 || flag_isoc99)
13435 21430 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13436 : {
13437 46 : no_warning = true;
13438 47 : if (emit_diagnostic (flag_isoc99
13439 : ? diagnostics::kind::permerror
13440 : : diagnostics::kind::warning,
13441 46 : loc, OPT_Wreturn_mismatch,
13442 : "%<return%> with no value,"
13443 : " in function returning non-void"))
13444 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13445 : "declared here");
13446 : }
13447 : }
13448 35622974 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13449 : {
13450 321 : current_function_returns_null = 1;
13451 321 : bool warned_here;
13452 321 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13453 64 : warned_here = permerror_opt
13454 64 : (xloc, OPT_Wreturn_mismatch,
13455 : "%<return%> with a value, in function returning void");
13456 : else
13457 257 : warned_here = pedwarn
13458 257 : (xloc, OPT_Wpedantic, "ISO C forbids "
13459 : "%<return%> with expression, in function returning void");
13460 321 : if (warned_here)
13461 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13462 : "declared here");
13463 : }
13464 : else
13465 : {
13466 35622653 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13467 : retval, origtype, ic_return,
13468 : npc, NULL_TREE, NULL_TREE, 0);
13469 35622653 : tree res = DECL_RESULT (current_function_decl);
13470 35622653 : tree inner;
13471 35622653 : bool save;
13472 :
13473 35622653 : current_function_returns_value = 1;
13474 35622653 : if (t == error_mark_node)
13475 : {
13476 : /* Suppress -Wreturn-type for this function. */
13477 299 : if (warn_return_type)
13478 298 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13479 : return NULL_TREE;
13480 : }
13481 :
13482 35622354 : save = in_late_binary_op;
13483 71236092 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13484 35612933 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13485 71233987 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13486 334456 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13487 334456 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13488 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13489 10721 : in_late_binary_op = true;
13490 35622354 : inner = t = convert (TREE_TYPE (res), t);
13491 35622354 : in_late_binary_op = save;
13492 :
13493 : /* Strip any conversions, additions, and subtractions, and see if
13494 : we are returning the address of a local variable. Warn if so. */
13495 38916072 : while (1)
13496 : {
13497 38916072 : switch (TREE_CODE (inner))
13498 : {
13499 3292275 : CASE_CONVERT:
13500 3292275 : case NON_LVALUE_EXPR:
13501 3292275 : case PLUS_EXPR:
13502 3292275 : case POINTER_PLUS_EXPR:
13503 3292275 : inner = TREE_OPERAND (inner, 0);
13504 3292275 : continue;
13505 :
13506 1448 : case MINUS_EXPR:
13507 : /* If the second operand of the MINUS_EXPR has a pointer
13508 : type (or is converted from it), this may be valid, so
13509 : don't give a warning. */
13510 1448 : {
13511 1448 : tree op1 = TREE_OPERAND (inner, 1);
13512 :
13513 3145 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13514 3399 : && (CONVERT_EXPR_P (op1)
13515 1443 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13516 254 : op1 = TREE_OPERAND (op1, 0);
13517 :
13518 1448 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13519 : break;
13520 :
13521 1443 : inner = TREE_OPERAND (inner, 0);
13522 1443 : continue;
13523 1443 : }
13524 :
13525 2957 : case ADDR_EXPR:
13526 2957 : inner = TREE_OPERAND (inner, 0);
13527 :
13528 2957 : while (REFERENCE_CLASS_P (inner)
13529 3851 : && !INDIRECT_REF_P (inner))
13530 894 : inner = TREE_OPERAND (inner, 0);
13531 :
13532 2957 : if (DECL_P (inner)
13533 1626 : && !DECL_EXTERNAL (inner)
13534 978 : && DECL_CONTEXT (inner) == current_function_decl
13535 3179 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13536 : {
13537 182 : if (TREE_CODE (inner) == LABEL_DECL)
13538 4 : warning_at (loc, OPT_Wreturn_local_addr,
13539 : "function returns address of label");
13540 178 : else if (TREE_CODE (inner) == FUNCTION_DECL
13541 178 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13542 9 : || !DECL_INITIAL (inner)))
13543 14 : warning_at (loc, OPT_Wreturn_local_addr,
13544 : "function returns address of nested function "
13545 : "referencing local context");
13546 164 : else if (TREE_CODE (inner) != FUNCTION_DECL
13547 156 : && !TREE_STATIC (inner))
13548 : {
13549 9 : warning_at (loc, OPT_Wreturn_local_addr,
13550 : "function returns address of local variable");
13551 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13552 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13553 : }
13554 : }
13555 : break;
13556 :
13557 : default:
13558 : break;
13559 3292275 : }
13560 :
13561 35622354 : break;
13562 : }
13563 :
13564 35622354 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13565 35622354 : SET_EXPR_LOCATION (retval, loc);
13566 :
13567 35622354 : if (warn_sequence_point)
13568 3034322 : verify_sequence_points (retval);
13569 : }
13570 :
13571 35644230 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13572 35644230 : if (no_warning)
13573 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13574 35644230 : return add_stmt (ret_stmt);
13575 : }
13576 :
13577 : struct c_switch {
13578 : /* The SWITCH_STMT being built. */
13579 : tree switch_stmt;
13580 :
13581 : /* The original type of the testing expression, i.e. before the
13582 : default conversion is applied. */
13583 : tree orig_type;
13584 :
13585 : /* A splay-tree mapping the low element of a case range to the high
13586 : element, or NULL_TREE if there is no high element. Used to
13587 : determine whether or not a new case label duplicates an old case
13588 : label. We need a tree, rather than simply a hash table, because
13589 : of the GNU case range extension. */
13590 : splay_tree cases;
13591 :
13592 : /* The bindings at the point of the switch. This is used for
13593 : warnings crossing decls when branching to a case label. */
13594 : struct c_spot_bindings *bindings;
13595 :
13596 : /* Whether the switch includes any break statements. */
13597 : bool break_stmt_seen_p;
13598 :
13599 : /* The next node on the stack. */
13600 : struct c_switch *next;
13601 :
13602 : /* Remember whether the controlling expression had boolean type
13603 : before integer promotions for the sake of -Wswitch-bool. */
13604 : bool bool_cond_p;
13605 : };
13606 :
13607 : /* A stack of the currently active switch statements. The innermost
13608 : switch statement is on the top of the stack. There is no need to
13609 : mark the stack for garbage collection because it is only active
13610 : during the processing of the body of a function, and we never
13611 : collect at that point. */
13612 :
13613 : struct c_switch *c_switch_stack;
13614 :
13615 : /* Start a C switch statement, testing expression EXP. Return the new
13616 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13617 : SWITCH_COND_LOC is the location of the switch's condition.
13618 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13619 :
13620 : tree
13621 37568 : c_start_switch (location_t switch_loc,
13622 : location_t switch_cond_loc,
13623 : tree exp, bool explicit_cast_p, tree switch_name)
13624 : {
13625 37568 : tree orig_type = error_mark_node;
13626 37568 : bool bool_cond_p = false;
13627 37568 : struct c_switch *cs;
13628 :
13629 37568 : if (exp != error_mark_node)
13630 : {
13631 37525 : orig_type = TREE_TYPE (exp);
13632 :
13633 37525 : if (!INTEGRAL_TYPE_P (orig_type))
13634 : {
13635 12 : if (orig_type != error_mark_node)
13636 : {
13637 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13638 12 : orig_type = error_mark_node;
13639 : }
13640 12 : exp = integer_zero_node;
13641 : }
13642 : else
13643 : {
13644 37513 : tree type = TYPE_MAIN_VARIANT (orig_type);
13645 37513 : tree e = exp;
13646 :
13647 : /* Warn if the condition has boolean value. */
13648 37516 : while (TREE_CODE (e) == COMPOUND_EXPR)
13649 3 : e = TREE_OPERAND (e, 1);
13650 :
13651 37488 : if ((C_BOOLEAN_TYPE_P (type)
13652 37488 : || truth_value_p (TREE_CODE (e)))
13653 : /* Explicit cast to int suppresses this warning. */
13654 37535 : && !(TREE_CODE (type) == INTEGER_TYPE
13655 : && explicit_cast_p))
13656 : bool_cond_p = true;
13657 :
13658 37513 : if (!in_system_header_at (input_location)
13659 37513 : && (type == long_integer_type_node
13660 12066 : || type == long_unsigned_type_node))
13661 306 : warning_at (switch_cond_loc,
13662 306 : OPT_Wtraditional, "%<long%> switch expression not "
13663 : "converted to %<int%> in ISO C");
13664 :
13665 37513 : exp = c_fully_fold (exp, false, NULL);
13666 37513 : exp = default_conversion (exp);
13667 :
13668 37513 : if (warn_sequence_point)
13669 6046 : verify_sequence_points (exp);
13670 : }
13671 : }
13672 :
13673 : /* Add this new SWITCH_STMT to the stack. */
13674 37568 : cs = XNEW (struct c_switch);
13675 37568 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13676 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13677 37568 : cs->orig_type = orig_type;
13678 37568 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13679 37568 : cs->bindings = c_get_switch_bindings ();
13680 37568 : cs->break_stmt_seen_p = false;
13681 37568 : cs->bool_cond_p = bool_cond_p;
13682 37568 : cs->next = c_switch_stack;
13683 37568 : c_switch_stack = cs;
13684 :
13685 37568 : return add_stmt (cs->switch_stmt);
13686 : }
13687 :
13688 : /* Process a case label at location LOC, with attributes ATTRS. */
13689 :
13690 : tree
13691 1032554 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13692 : {
13693 1032554 : tree label = NULL_TREE;
13694 :
13695 1032554 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13696 : {
13697 87 : low_value = c_fully_fold (low_value, false, NULL);
13698 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13699 9 : pedwarn (loc, OPT_Wpedantic,
13700 : "case label is not an integer constant expression");
13701 : }
13702 :
13703 1032554 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13704 : {
13705 0 : high_value = c_fully_fold (high_value, false, NULL);
13706 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13707 0 : pedwarn (input_location, OPT_Wpedantic,
13708 : "case label is not an integer constant expression");
13709 : }
13710 :
13711 1032554 : if (c_switch_stack == NULL)
13712 : {
13713 3 : if (low_value)
13714 2 : error_at (loc, "case label not within a switch statement");
13715 : else
13716 1 : error_at (loc, "%<default%> label not within a switch statement");
13717 : return NULL_TREE;
13718 : }
13719 :
13720 1032551 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13721 1032551 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13722 : loc))
13723 : return NULL_TREE;
13724 :
13725 1032539 : label = c_add_case_label (loc, c_switch_stack->cases,
13726 1032539 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13727 : low_value, high_value, attrs);
13728 1032539 : if (label == error_mark_node)
13729 144 : label = NULL_TREE;
13730 : return label;
13731 : }
13732 :
13733 : /* Finish the switch statement. TYPE is the original type of the
13734 : controlling expression of the switch, or NULL_TREE. */
13735 :
13736 : void
13737 37568 : c_finish_switch (tree body, tree type)
13738 : {
13739 37568 : struct c_switch *cs = c_switch_stack;
13740 37568 : location_t switch_location;
13741 :
13742 37568 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13743 :
13744 : /* Emit warnings as needed. */
13745 37568 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13746 43780 : c_do_switch_warnings (cs->cases, switch_location,
13747 6212 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13748 37568 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13749 37568 : if (c_switch_covers_all_cases_p (cs->cases,
13750 37568 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13751 33598 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13752 37568 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13753 :
13754 : /* Pop the stack. */
13755 37568 : c_switch_stack = cs->next;
13756 37568 : splay_tree_delete (cs->cases);
13757 37568 : c_release_switch_bindings (cs->bindings);
13758 37568 : XDELETE (cs);
13759 37568 : }
13760 :
13761 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13762 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13763 : may be null. */
13764 :
13765 : void
13766 1282832 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13767 : tree else_block)
13768 : {
13769 1282832 : tree stmt;
13770 :
13771 1282832 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13772 1282832 : SET_EXPR_LOCATION (stmt, if_locus);
13773 1282832 : add_stmt (stmt);
13774 1282832 : }
13775 :
13776 : tree
13777 188489 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13778 : {
13779 : /* In switch statements break is sometimes stylistically used after
13780 : a return statement. This can lead to spurious warnings about
13781 : control reaching the end of a non-void function when it is
13782 : inlined. Note that we are calling block_may_fallthru with
13783 : language specific tree nodes; this works because
13784 : block_may_fallthru returns true when given something it does not
13785 : understand. */
13786 188489 : bool skip = !block_may_fallthru (cur_stmt_list);
13787 :
13788 188489 : if (is_break)
13789 175188 : switch (in_statement & ~IN_NAMED_STMT)
13790 : {
13791 9 : case 0:
13792 9 : error_at (loc, "break statement not within loop or switch");
13793 9 : return NULL_TREE;
13794 4 : case IN_OMP_BLOCK:
13795 4 : error_at (loc, "invalid exit from OpenMP structured block");
13796 4 : return NULL_TREE;
13797 12 : case IN_OMP_FOR:
13798 12 : error_at (loc, "break statement used with OpenMP for loop");
13799 12 : return NULL_TREE;
13800 : case IN_ITERATION_STMT:
13801 : case IN_OBJC_FOREACH:
13802 : break;
13803 159197 : default:
13804 159197 : gcc_assert (in_statement & IN_SWITCH_STMT);
13805 159197 : c_switch_stack->break_stmt_seen_p = true;
13806 159197 : break;
13807 : }
13808 : else
13809 13301 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13810 : {
13811 7 : case 0:
13812 7 : error_at (loc, "continue statement not within a loop");
13813 7 : return NULL_TREE;
13814 4 : case IN_OMP_BLOCK:
13815 4 : error_at (loc, "invalid exit from OpenMP structured block");
13816 4 : return NULL_TREE;
13817 : case IN_ITERATION_STMT:
13818 : case IN_OMP_FOR:
13819 : case IN_OBJC_FOREACH:
13820 : break;
13821 0 : default:
13822 0 : gcc_unreachable ();
13823 : }
13824 :
13825 188453 : if (skip)
13826 : return NULL_TREE;
13827 187917 : else if ((in_statement & IN_OBJC_FOREACH)
13828 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13829 0 : && name == NULL_TREE)
13830 : {
13831 : /* The foreach expander produces low-level code using gotos instead
13832 : of a structured loop construct. */
13833 0 : gcc_assert (label);
13834 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13835 : }
13836 188014 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13837 : {
13838 0 : label = DECL_CHAIN (name);
13839 0 : if (!is_break)
13840 0 : label = DECL_CHAIN (label);
13841 : /* Foreach expander from some outer level. */
13842 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13843 : }
13844 201207 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13845 187917 : name));
13846 : }
13847 :
13848 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13849 :
13850 : static void
13851 6431430 : emit_side_effect_warnings (location_t loc, tree expr)
13852 : {
13853 6431430 : maybe_warn_nodiscard (loc, expr);
13854 6431430 : if (!warn_unused_value)
13855 : return;
13856 1306585 : if (expr == error_mark_node)
13857 : ;
13858 1306510 : else if (!TREE_SIDE_EFFECTS (expr))
13859 : {
13860 6567 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13861 6567 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13862 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13863 : }
13864 1299943 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13865 : {
13866 : tree r = expr;
13867 : location_t cloc = loc;
13868 12003 : while (TREE_CODE (r) == COMPOUND_EXPR)
13869 : {
13870 6017 : if (EXPR_HAS_LOCATION (r))
13871 5479 : cloc = EXPR_LOCATION (r);
13872 6017 : r = TREE_OPERAND (r, 1);
13873 : }
13874 5986 : if (!TREE_SIDE_EFFECTS (r)
13875 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13876 24 : && !CONVERT_EXPR_P (r)
13877 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13878 5998 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13879 8 : warning_at (cloc, OPT_Wunused_value,
13880 : "right-hand operand of comma expression has no effect");
13881 : }
13882 : else
13883 1293957 : warn_if_unused_value (expr, loc);
13884 : }
13885 :
13886 : /* Process an expression as if it were a complete statement. Emit
13887 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13888 : statement. */
13889 :
13890 : tree
13891 6308025 : c_process_expr_stmt (location_t loc, tree expr)
13892 : {
13893 6308025 : tree exprv;
13894 :
13895 6308025 : if (!expr)
13896 : return NULL_TREE;
13897 :
13898 6303611 : expr = c_fully_fold (expr, false, NULL);
13899 :
13900 6303611 : if (warn_sequence_point)
13901 1302286 : verify_sequence_points (expr);
13902 :
13903 6303611 : if (TREE_TYPE (expr) != error_mark_node
13904 6300957 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13905 6303611 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13906 0 : error_at (loc, "expression statement has incomplete type");
13907 :
13908 : /* If we're not processing a statement expression, warn about unused values.
13909 : Warnings for statement expressions will be emitted later, once we figure
13910 : out which is the result. */
13911 6303611 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13912 6303611 : && (warn_unused_value || warn_unused_result))
13913 6181235 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13914 :
13915 6303610 : exprv = expr;
13916 6344976 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13917 41366 : exprv = TREE_OPERAND (exprv, 1);
13918 6315314 : while (CONVERT_EXPR_P (exprv))
13919 11704 : exprv = TREE_OPERAND (exprv, 0);
13920 6303610 : if (DECL_P (exprv)
13921 6304196 : || handled_component_p (exprv)
13922 12587251 : || TREE_CODE (exprv) == ADDR_EXPR)
13923 20555 : mark_exp_read (exprv);
13924 :
13925 : /* If the expression is not of a type to which we cannot assign a line
13926 : number, wrap the thing in a no-op NOP_EXPR. */
13927 6303610 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13928 : {
13929 14896 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13930 14896 : SET_EXPR_LOCATION (expr, loc);
13931 : }
13932 :
13933 : return expr;
13934 : }
13935 :
13936 : /* Emit an expression as a statement. LOC is the location of the
13937 : expression. */
13938 :
13939 : tree
13940 6002391 : c_finish_expr_stmt (location_t loc, tree expr)
13941 : {
13942 6002391 : if (expr)
13943 5988164 : return add_stmt (c_process_expr_stmt (loc, expr));
13944 : else
13945 : return NULL;
13946 : }
13947 :
13948 : /* Do the opposite and emit a statement as an expression. To begin,
13949 : create a new binding level and return it. */
13950 :
13951 : tree
13952 34944 : c_begin_stmt_expr (void)
13953 : {
13954 34944 : tree ret;
13955 :
13956 : /* We must force a BLOCK for this level so that, if it is not expanded
13957 : later, there is a way to turn off the entire subtree of blocks that
13958 : are contained in it. */
13959 34944 : keep_next_level ();
13960 34944 : ret = c_begin_compound_stmt (true);
13961 :
13962 34944 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13963 : ? NULL
13964 : : c_switch_stack->bindings);
13965 :
13966 : /* Mark the current statement list as belonging to a statement list. */
13967 34944 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13968 :
13969 34944 : return ret;
13970 : }
13971 :
13972 : /* LOC is the location of the compound statement to which this body
13973 : belongs. */
13974 :
13975 : tree
13976 34944 : c_finish_stmt_expr (location_t loc, tree body)
13977 : {
13978 34944 : tree last, type, tmp, val;
13979 34944 : tree *last_p;
13980 :
13981 34944 : body = c_end_compound_stmt (loc, body, true);
13982 :
13983 34944 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13984 : ? NULL
13985 : : c_switch_stack->bindings);
13986 :
13987 : /* Locate the last statement in BODY. See c_end_compound_stmt
13988 : about always returning a BIND_EXPR. */
13989 34944 : last_p = &BIND_EXPR_BODY (body);
13990 34944 : last = BIND_EXPR_BODY (body);
13991 :
13992 34944 : continue_searching:
13993 34944 : if (TREE_CODE (last) == STATEMENT_LIST)
13994 : {
13995 26130 : tree_stmt_iterator l = tsi_last (last);
13996 :
13997 26136 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13998 6 : tsi_prev (&l);
13999 :
14000 : /* This can happen with degenerate cases like ({ }). No value. */
14001 26130 : if (tsi_end_p (l))
14002 389 : return body;
14003 :
14004 : /* If we're supposed to generate side effects warnings, process
14005 : all of the statements except the last. */
14006 25741 : if (warn_unused_value || warn_unused_result)
14007 : {
14008 25741 : for (tree_stmt_iterator i = tsi_start (last);
14009 275936 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
14010 : {
14011 250195 : location_t tloc;
14012 250195 : tree t = tsi_stmt (i);
14013 :
14014 250195 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
14015 250195 : emit_side_effect_warnings (tloc, t);
14016 : }
14017 : }
14018 25741 : last_p = tsi_stmt_ptr (l);
14019 25741 : last = *last_p;
14020 : }
14021 :
14022 : /* If the end of the list is exception related, then the list was split
14023 : by a call to push_cleanup. Continue searching. */
14024 34555 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
14025 34555 : || TREE_CODE (last) == TRY_CATCH_EXPR)
14026 : {
14027 0 : last_p = &TREE_OPERAND (last, 0);
14028 0 : last = *last_p;
14029 0 : goto continue_searching;
14030 : }
14031 :
14032 34555 : if (last == error_mark_node)
14033 : return last;
14034 :
14035 : /* In the case that the BIND_EXPR is not necessary, return the
14036 : expression out from inside it. */
14037 34546 : if ((last == BIND_EXPR_BODY (body)
14038 : /* Skip nested debug stmts. */
14039 25740 : || last == expr_first (BIND_EXPR_BODY (body)))
14040 45580 : && BIND_EXPR_VARS (body) == NULL)
14041 : {
14042 : /* Even if this looks constant, do not allow it in a constant
14043 : expression. */
14044 11022 : last = c_wrap_maybe_const (last, true);
14045 : /* Do not warn if the return value of a statement expression is
14046 : unused. */
14047 11022 : suppress_warning (last, OPT_Wunused);
14048 11022 : return last;
14049 : }
14050 :
14051 : /* Extract the type of said expression. */
14052 23524 : type = TREE_TYPE (last);
14053 :
14054 : /* If we're not returning a value at all, then the BIND_EXPR that
14055 : we already have is a fine expression to return. */
14056 23524 : if (!type || VOID_TYPE_P (type))
14057 : return body;
14058 :
14059 : /* Now that we've located the expression containing the value, it seems
14060 : silly to make voidify_wrapper_expr repeat the process. Create a
14061 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14062 18200 : tmp = create_tmp_var_raw (type);
14063 :
14064 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14065 : tree_expr_nonnegative_p giving up immediately. */
14066 18200 : val = last;
14067 18200 : if (TREE_CODE (val) == NOP_EXPR
14068 18200 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14069 3794 : val = TREE_OPERAND (val, 0);
14070 :
14071 18200 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14072 18200 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14073 :
14074 18200 : {
14075 18200 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14076 18200 : SET_EXPR_LOCATION (t, loc);
14077 18200 : return t;
14078 : }
14079 : }
14080 :
14081 : /* Begin and end compound statements. This is as simple as pushing
14082 : and popping new statement lists from the tree. */
14083 :
14084 : tree
14085 42242303 : c_begin_compound_stmt (bool do_scope)
14086 : {
14087 42242303 : tree stmt = push_stmt_list ();
14088 42242303 : if (do_scope)
14089 42146139 : push_scope ();
14090 42242303 : return stmt;
14091 : }
14092 :
14093 : /* End a compound statement. STMT is the statement. LOC is the
14094 : location of the compound statement-- this is usually the location
14095 : of the opening brace. */
14096 :
14097 : tree
14098 42242302 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14099 : {
14100 42242302 : tree block = NULL;
14101 :
14102 42242302 : if (do_scope)
14103 : {
14104 42146138 : if (c_dialect_objc ())
14105 0 : objc_clear_super_receiver ();
14106 42146138 : block = pop_scope ();
14107 : }
14108 :
14109 42242302 : stmt = pop_stmt_list (stmt);
14110 42242302 : stmt = c_build_bind_expr (loc, block, stmt);
14111 :
14112 : /* If this compound statement is nested immediately inside a statement
14113 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14114 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14115 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14116 : was really last. */
14117 84484604 : if (building_stmt_list_p ()
14118 42242217 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14119 42364994 : && TREE_CODE (stmt) != BIND_EXPR)
14120 : {
14121 120949 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14122 120949 : TREE_SIDE_EFFECTS (stmt) = 1;
14123 120949 : SET_EXPR_LOCATION (stmt, loc);
14124 : }
14125 :
14126 42242302 : return stmt;
14127 : }
14128 :
14129 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14130 : when the current scope is exited. EH_ONLY is true when this is not
14131 : meant to apply to normal control flow transfer. */
14132 :
14133 : void
14134 131 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14135 : {
14136 131 : enum tree_code code;
14137 131 : tree stmt, list;
14138 131 : bool stmt_expr;
14139 :
14140 131 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14141 131 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14142 131 : add_stmt (stmt);
14143 131 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14144 131 : list = push_stmt_list ();
14145 131 : TREE_OPERAND (stmt, 0) = list;
14146 131 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14147 131 : }
14148 :
14149 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14150 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14151 :
14152 : static tree
14153 101489 : build_vec_cmp (tree_code code, tree type,
14154 : tree arg0, tree arg1)
14155 : {
14156 101489 : tree zero_vec = build_zero_cst (type);
14157 101489 : tree minus_one_vec = build_minus_one_cst (type);
14158 101489 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14159 101489 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14160 101489 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14161 : }
14162 :
14163 : /* Possibly warn about an address of OP never being NULL in a comparison
14164 : operation CODE involving null. */
14165 :
14166 : static void
14167 52280 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14168 : {
14169 : /* Prevent warnings issued for macro expansion. */
14170 52280 : if (!warn_address
14171 30650 : || warning_suppressed_p (op, OPT_Waddress)
14172 82554 : || from_macro_expansion_at (loc))
14173 : return;
14174 :
14175 27527 : if (TREE_CODE (op) == NOP_EXPR)
14176 : {
14177 : /* Allow casts to intptr_t to suppress the warning. */
14178 1135 : tree type = TREE_TYPE (op);
14179 1135 : if (TREE_CODE (type) == INTEGER_TYPE)
14180 : return;
14181 1135 : op = TREE_OPERAND (op, 0);
14182 : }
14183 :
14184 27527 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14185 : {
14186 : /* Allow a cast to void* to suppress the warning. */
14187 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14188 24 : if (VOID_TYPE_P (type))
14189 : return;
14190 :
14191 : /* Adding any value to a null pointer, including zero, is undefined
14192 : in C. This includes the expression &p[0] where p is the null
14193 : pointer, although &p[0] will have been folded to p by this point
14194 : and so not diagnosed. */
14195 24 : if (code == EQ_EXPR)
14196 22 : warning_at (loc, OPT_Waddress,
14197 : "the comparison will always evaluate as %<false%> "
14198 : "for the pointer operand in %qE must not be NULL",
14199 : op);
14200 : else
14201 2 : warning_at (loc, OPT_Waddress,
14202 : "the comparison will always evaluate as %<true%> "
14203 : "for the pointer operand in %qE must not be NULL",
14204 : op);
14205 :
14206 : return;
14207 : }
14208 :
14209 27503 : if (TREE_CODE (op) != ADDR_EXPR)
14210 : return;
14211 :
14212 254 : op = TREE_OPERAND (op, 0);
14213 :
14214 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14215 254 : || TREE_CODE (op) == REALPART_EXPR)
14216 : {
14217 : /* The address of either complex part may not be null. */
14218 14 : if (code == EQ_EXPR)
14219 9 : warning_at (loc, OPT_Waddress,
14220 : "the comparison will always evaluate as %<false%> "
14221 : "for the address of %qE will never be NULL",
14222 : op);
14223 : else
14224 5 : warning_at (loc, OPT_Waddress,
14225 : "the comparison will always evaluate as %<true%> "
14226 : "for the address of %qE will never be NULL",
14227 : op);
14228 : return;
14229 : }
14230 :
14231 : /* Set to true in the loop below if OP dereferences is operand.
14232 : In such a case the ultimate target need not be a decl for
14233 : the null [in]equality test to be constant. */
14234 : bool deref = false;
14235 :
14236 : /* Get the outermost array or object, or member. */
14237 294 : while (handled_component_p (op))
14238 : {
14239 72 : if (TREE_CODE (op) == COMPONENT_REF)
14240 : {
14241 : /* Get the member (its address is never null). */
14242 18 : op = TREE_OPERAND (op, 1);
14243 18 : break;
14244 : }
14245 :
14246 : /* Get the outer array/object to refer to in the warning. */
14247 54 : op = TREE_OPERAND (op, 0);
14248 54 : deref = true;
14249 : }
14250 :
14251 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14252 371 : || from_macro_expansion_at (loc))
14253 : return;
14254 :
14255 131 : bool w;
14256 131 : if (code == EQ_EXPR)
14257 93 : w = warning_at (loc, OPT_Waddress,
14258 : "the comparison will always evaluate as %<false%> "
14259 : "for the address of %qE will never be NULL",
14260 : op);
14261 : else
14262 38 : w = warning_at (loc, OPT_Waddress,
14263 : "the comparison will always evaluate as %<true%> "
14264 : "for the address of %qE will never be NULL",
14265 : op);
14266 :
14267 131 : if (w && DECL_P (op))
14268 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14269 : }
14270 :
14271 : /* Build a binary-operation expression without default conversions.
14272 : CODE is the kind of expression to build.
14273 : LOCATION is the operator's location.
14274 : This function differs from `build' in several ways:
14275 : the data type of the result is computed and recorded in it,
14276 : warnings are generated if arg data types are invalid,
14277 : special handling for addition and subtraction of pointers is known,
14278 : and some optimization is done (operations on narrow ints
14279 : are done in the narrower type when that gives the same result).
14280 : Constant folding is also done before the result is returned.
14281 :
14282 : Note that the operands will never have enumeral types, or function
14283 : or array types, because either they will have the default conversions
14284 : performed or they have both just been converted to some other type in which
14285 : the arithmetic is to be done. */
14286 :
14287 : tree
14288 16951256 : build_binary_op (location_t location, enum tree_code code,
14289 : tree orig_op0, tree orig_op1, bool convert_p)
14290 : {
14291 16951256 : tree type0, type1, orig_type0, orig_type1;
14292 16951256 : tree eptype;
14293 16951256 : enum tree_code code0, code1;
14294 16951256 : tree op0, op1;
14295 16951256 : tree ret = error_mark_node;
14296 16951256 : const char *invalid_op_diag;
14297 16951256 : bool op0_int_operands, op1_int_operands;
14298 16951256 : bool int_const, int_const_or_overflow, int_operands;
14299 :
14300 : /* Expression code to give to the expression when it is built.
14301 : Normally this is CODE, which is what the caller asked for,
14302 : but in some special cases we change it. */
14303 16951256 : enum tree_code resultcode = code;
14304 :
14305 : /* Data type in which the computation is to be performed.
14306 : In the simplest cases this is the common type of the arguments. */
14307 16951256 : tree result_type = NULL;
14308 :
14309 : /* When the computation is in excess precision, the type of the
14310 : final EXCESS_PRECISION_EXPR. */
14311 16951256 : tree semantic_result_type = NULL;
14312 :
14313 : /* Nonzero means operands have already been type-converted
14314 : in whatever way is necessary.
14315 : Zero means they need to be converted to RESULT_TYPE. */
14316 16951256 : int converted = 0;
14317 :
14318 : /* Nonzero means create the expression with this type, rather than
14319 : RESULT_TYPE. */
14320 16951256 : tree build_type = NULL_TREE;
14321 :
14322 : /* Nonzero means after finally constructing the expression
14323 : convert it to this type. */
14324 16951256 : tree final_type = NULL_TREE;
14325 :
14326 : /* Nonzero if this is an operation like MIN or MAX which can
14327 : safely be computed in short if both args are promoted shorts.
14328 : Also implies COMMON.
14329 : -1 indicates a bitwise operation; this makes a difference
14330 : in the exact conditions for when it is safe to do the operation
14331 : in a narrower mode. */
14332 16951256 : int shorten = 0;
14333 :
14334 : /* Nonzero if this is a comparison operation;
14335 : if both args are promoted shorts, compare the original shorts.
14336 : Also implies COMMON. */
14337 16951256 : int short_compare = 0;
14338 :
14339 : /* Nonzero if this is a right-shift operation, which can be computed on the
14340 : original short and then promoted if the operand is a promoted short. */
14341 16951256 : int short_shift = 0;
14342 :
14343 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14344 16951256 : int common = 0;
14345 :
14346 : /* True means types are compatible as far as ObjC is concerned. */
14347 16951256 : bool objc_ok;
14348 :
14349 : /* True means this is an arithmetic operation that may need excess
14350 : precision. */
14351 16951256 : bool may_need_excess_precision;
14352 :
14353 : /* True means this is a boolean operation that converts both its
14354 : operands to truth-values. */
14355 16951256 : bool boolean_op = false;
14356 :
14357 : /* Remember whether we're doing / or %. */
14358 16951256 : bool doing_div_or_mod = false;
14359 :
14360 : /* Remember whether we're doing << or >>. */
14361 16951256 : bool doing_shift = false;
14362 :
14363 : /* Tree holding instrumentation expression. */
14364 16951256 : tree instrument_expr = NULL;
14365 :
14366 16951256 : if (location == UNKNOWN_LOCATION)
14367 80 : location = input_location;
14368 :
14369 16951256 : op0 = orig_op0;
14370 16951256 : op1 = orig_op1;
14371 :
14372 16951256 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14373 : if (op0_int_operands)
14374 8206760 : op0 = remove_c_maybe_const_expr (op0);
14375 16951256 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14376 : if (op1_int_operands)
14377 11492823 : op1 = remove_c_maybe_const_expr (op1);
14378 28444079 : int_operands = (op0_int_operands && op1_int_operands);
14379 11492823 : if (int_operands)
14380 : {
14381 16056041 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14382 8043191 : && TREE_CODE (orig_op1) == INTEGER_CST);
14383 8012850 : int_const = (int_const_or_overflow
14384 8012850 : && !TREE_OVERFLOW (orig_op0)
14385 8012781 : && !TREE_OVERFLOW (orig_op1));
14386 : }
14387 : else
14388 : int_const = int_const_or_overflow = false;
14389 :
14390 : /* Do not apply default conversion in mixed vector/scalar expression. */
14391 16951256 : if (convert_p
14392 16951256 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14393 : {
14394 10188593 : op0 = default_conversion (op0);
14395 10188593 : op1 = default_conversion (op1);
14396 : }
14397 :
14398 16951256 : orig_type0 = type0 = TREE_TYPE (op0);
14399 :
14400 16951256 : orig_type1 = type1 = TREE_TYPE (op1);
14401 :
14402 : /* The expression codes of the data types of the arguments tell us
14403 : whether the arguments are integers, floating, pointers, etc. */
14404 16951256 : code0 = TREE_CODE (type0);
14405 16951256 : code1 = TREE_CODE (type1);
14406 :
14407 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14408 16951258 : STRIP_TYPE_NOPS (op0);
14409 16951259 : STRIP_TYPE_NOPS (op1);
14410 :
14411 : /* If an error was already reported for one of the arguments,
14412 : avoid reporting another error. */
14413 :
14414 16951256 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14415 710 : return error_mark_node;
14416 :
14417 16950546 : if (code0 == POINTER_TYPE
14418 16950546 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14419 11 : return error_mark_node;
14420 :
14421 16950535 : if (code1 == POINTER_TYPE
14422 16950535 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14423 11 : return error_mark_node;
14424 :
14425 33901048 : if ((invalid_op_diag
14426 16950524 : = targetm.invalid_binary_op (code, type0, type1)))
14427 : {
14428 0 : error_at (location, invalid_op_diag);
14429 0 : return error_mark_node;
14430 : }
14431 :
14432 16950524 : switch (code)
14433 : {
14434 : case PLUS_EXPR:
14435 : case MINUS_EXPR:
14436 : case MULT_EXPR:
14437 : case TRUNC_DIV_EXPR:
14438 : case CEIL_DIV_EXPR:
14439 : case FLOOR_DIV_EXPR:
14440 : case ROUND_DIV_EXPR:
14441 : case EXACT_DIV_EXPR:
14442 : may_need_excess_precision = true;
14443 : break;
14444 :
14445 2758104 : case EQ_EXPR:
14446 2758104 : case NE_EXPR:
14447 2758104 : case LE_EXPR:
14448 2758104 : case GE_EXPR:
14449 2758104 : case LT_EXPR:
14450 2758104 : case GT_EXPR:
14451 : /* Excess precision for implicit conversions of integers to
14452 : floating point in C11 and later. */
14453 2758104 : may_need_excess_precision = (flag_isoc11
14454 2758104 : && (ANY_INTEGRAL_TYPE_P (type0)
14455 487968 : || ANY_INTEGRAL_TYPE_P (type1)));
14456 : break;
14457 :
14458 : default:
14459 16950524 : may_need_excess_precision = false;
14460 : break;
14461 : }
14462 16950524 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14463 : {
14464 184 : op0 = TREE_OPERAND (op0, 0);
14465 184 : type0 = TREE_TYPE (op0);
14466 : }
14467 16950340 : else if (may_need_excess_precision
14468 16950340 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14469 : {
14470 771 : type0 = eptype;
14471 771 : op0 = convert (eptype, op0);
14472 : }
14473 16950524 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14474 : {
14475 896 : op1 = TREE_OPERAND (op1, 0);
14476 896 : type1 = TREE_TYPE (op1);
14477 : }
14478 16949628 : else if (may_need_excess_precision
14479 16949628 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14480 : {
14481 592 : type1 = eptype;
14482 592 : op1 = convert (eptype, op1);
14483 : }
14484 :
14485 16950524 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14486 :
14487 : /* In case when one of the operands of the binary operation is
14488 : a vector and another is a scalar -- convert scalar to vector. */
14489 18537596 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14490 18535664 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14491 : {
14492 2551 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14493 : orig_op1, true);
14494 :
14495 2551 : switch (convert_flag)
14496 : {
14497 12 : case stv_error:
14498 12 : return error_mark_node;
14499 606 : case stv_firstarg:
14500 606 : {
14501 606 : bool maybe_const = true;
14502 606 : tree sc;
14503 606 : sc = c_fully_fold (op0, false, &maybe_const);
14504 606 : sc = save_expr (sc);
14505 606 : sc = convert (TREE_TYPE (type1), sc);
14506 606 : op0 = build_vector_from_val (type1, sc);
14507 606 : if (!maybe_const)
14508 93 : op0 = c_wrap_maybe_const (op0, true);
14509 606 : orig_type0 = type0 = TREE_TYPE (op0);
14510 606 : code0 = TREE_CODE (type0);
14511 606 : converted = 1;
14512 606 : break;
14513 : }
14514 1091 : case stv_secondarg:
14515 1091 : {
14516 1091 : bool maybe_const = true;
14517 1091 : tree sc;
14518 1091 : sc = c_fully_fold (op1, false, &maybe_const);
14519 1091 : sc = save_expr (sc);
14520 1091 : sc = convert (TREE_TYPE (type0), sc);
14521 1091 : op1 = build_vector_from_val (type0, sc);
14522 1091 : if (!maybe_const)
14523 38 : op1 = c_wrap_maybe_const (op1, true);
14524 1091 : orig_type1 = type1 = TREE_TYPE (op1);
14525 1091 : code1 = TREE_CODE (type1);
14526 1091 : converted = 1;
14527 1091 : break;
14528 : }
14529 : default:
14530 : break;
14531 : }
14532 : }
14533 :
14534 16950512 : switch (code)
14535 : {
14536 8696214 : case PLUS_EXPR:
14537 : /* Handle the pointer + int case. */
14538 8696214 : if (code0 == POINTER_TYPE
14539 1256880 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14540 : {
14541 1256874 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14542 1256874 : goto return_build_binary_op;
14543 : }
14544 7439340 : else if (code1 == POINTER_TYPE
14545 1539 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14546 : {
14547 1533 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14548 1533 : goto return_build_binary_op;
14549 : }
14550 : else
14551 : common = 1;
14552 : break;
14553 :
14554 800141 : case MINUS_EXPR:
14555 : /* Subtraction of two similar pointers.
14556 : We must subtract them as integers, then divide by object size. */
14557 800141 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14558 800141 : && comp_target_types (location, type0, type1))
14559 : {
14560 3750 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14561 3750 : goto return_build_binary_op;
14562 : }
14563 : /* Handle pointer minus int. Just like pointer plus int. */
14564 796391 : else if (code0 == POINTER_TYPE
14565 16871 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14566 : {
14567 16859 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14568 16859 : goto return_build_binary_op;
14569 : }
14570 : else
14571 : common = 1;
14572 : break;
14573 :
14574 : case MULT_EXPR:
14575 : common = 1;
14576 : break;
14577 :
14578 275864 : case TRUNC_DIV_EXPR:
14579 275864 : case CEIL_DIV_EXPR:
14580 275864 : case FLOOR_DIV_EXPR:
14581 275864 : case ROUND_DIV_EXPR:
14582 275864 : case EXACT_DIV_EXPR:
14583 275864 : doing_div_or_mod = true;
14584 275864 : warn_for_div_by_zero (location, op1);
14585 :
14586 275863 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14587 49849 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14588 47835 : || code0 == COMPLEX_TYPE
14589 45787 : || gnu_vector_type_p (type0))
14590 325711 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14591 47416 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14592 47249 : || code1 == COMPLEX_TYPE
14593 45789 : || gnu_vector_type_p (type1)))
14594 : {
14595 275859 : enum tree_code tcode0 = code0, tcode1 = code1;
14596 :
14597 275859 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14598 47834 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14599 275859 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14600 47246 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14601 :
14602 275859 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14603 69904 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14604 205956 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14605 12988 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14606 82890 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14607 : resultcode = RDIV_EXPR;
14608 : else
14609 : /* Although it would be tempting to shorten always here, that
14610 : loses on some targets, since the modulo instruction is
14611 : undefined if the quotient can't be represented in the
14612 : computation mode. We shorten only if unsigned or if
14613 : dividing by something we know != -1. */
14614 192969 : shorten = may_shorten_divmod (op0, op1);
14615 : common = 1;
14616 : }
14617 : break;
14618 :
14619 1589352 : case BIT_AND_EXPR:
14620 1589352 : case BIT_IOR_EXPR:
14621 1589352 : case BIT_XOR_EXPR:
14622 1589352 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14623 1051499 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14624 : shorten = -1;
14625 : /* Allow vector types which are not floating point types. */
14626 537889 : else if (gnu_vector_type_p (type0)
14627 537812 : && gnu_vector_type_p (type1)
14628 537812 : && !VECTOR_FLOAT_TYPE_P (type0)
14629 1075698 : && !VECTOR_FLOAT_TYPE_P (type1))
14630 : common = 1;
14631 : break;
14632 :
14633 58816 : case TRUNC_MOD_EXPR:
14634 58816 : case FLOOR_MOD_EXPR:
14635 58816 : doing_div_or_mod = true;
14636 58816 : warn_for_div_by_zero (location, op1);
14637 :
14638 58816 : if (gnu_vector_type_p (type0)
14639 301 : && gnu_vector_type_p (type1)
14640 301 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14641 59117 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14642 : common = 1;
14643 58515 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14644 58514 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14645 : {
14646 : /* Although it would be tempting to shorten always here, that loses
14647 : on some targets, since the modulo instruction is undefined if the
14648 : quotient can't be represented in the computation mode. We shorten
14649 : only if unsigned or if dividing by something we know != -1. */
14650 58514 : shorten = may_shorten_divmod (op0, op1);
14651 58514 : common = 1;
14652 : }
14653 : break;
14654 :
14655 512145 : case TRUTH_ANDIF_EXPR:
14656 512145 : case TRUTH_ORIF_EXPR:
14657 512145 : case TRUTH_AND_EXPR:
14658 512145 : case TRUTH_OR_EXPR:
14659 512145 : case TRUTH_XOR_EXPR:
14660 512145 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14661 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14662 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14663 0 : || code0 == BITINT_TYPE)
14664 512145 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14665 165 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14666 24 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14667 22 : || code1 == BITINT_TYPE))
14668 : {
14669 : /* Result of these operations is always an int,
14670 : but that does not mean the operands should be
14671 : converted to ints! */
14672 512145 : result_type = integer_type_node;
14673 512145 : if (op0_int_operands)
14674 : {
14675 110431 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14676 110431 : op0 = remove_c_maybe_const_expr (op0);
14677 : }
14678 : else
14679 401714 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14680 512145 : if (op1_int_operands)
14681 : {
14682 71407 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14683 71407 : op1 = remove_c_maybe_const_expr (op1);
14684 : }
14685 : else
14686 440738 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14687 : converted = 1;
14688 : boolean_op = true;
14689 : }
14690 512145 : if (code == TRUTH_ANDIF_EXPR)
14691 : {
14692 250403 : int_const_or_overflow = (int_operands
14693 63221 : && TREE_CODE (orig_op0) == INTEGER_CST
14694 250419 : && (op0 == truthvalue_false_node
14695 13168 : || TREE_CODE (orig_op1) == INTEGER_CST));
14696 : int_const = (int_const_or_overflow
14697 63194 : && !TREE_OVERFLOW (orig_op0)
14698 63194 : && (op0 == truthvalue_false_node
14699 13152 : || !TREE_OVERFLOW (orig_op1)));
14700 : }
14701 324936 : else if (code == TRUTH_ORIF_EXPR)
14702 : {
14703 331616 : int_const_or_overflow = (int_operands
14704 6831 : && TREE_CODE (orig_op0) == INTEGER_CST
14705 331639 : && (op0 == truthvalue_true_node
14706 2146 : || TREE_CODE (orig_op1) == INTEGER_CST));
14707 : int_const = (int_const_or_overflow
14708 6801 : && !TREE_OVERFLOW (orig_op0)
14709 6801 : && (op0 == truthvalue_true_node
14710 2123 : || !TREE_OVERFLOW (orig_op1)));
14711 : }
14712 : break;
14713 :
14714 : /* Shift operations: result has same type as first operand;
14715 : always convert second operand to int.
14716 : Also set SHORT_SHIFT if shifting rightward. */
14717 :
14718 232898 : case RSHIFT_EXPR:
14719 232898 : if (gnu_vector_type_p (type0)
14720 814 : && gnu_vector_type_p (type1)
14721 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14722 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14723 233154 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14724 : TYPE_VECTOR_SUBPARTS (type1)))
14725 : {
14726 : result_type = type0;
14727 : converted = 1;
14728 : }
14729 232644 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14730 1119 : || code0 == BITINT_TYPE
14731 564 : || (gnu_vector_type_p (type0)
14732 560 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14733 233756 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14734 : {
14735 232635 : doing_shift = true;
14736 232635 : if (TREE_CODE (op1) == INTEGER_CST)
14737 : {
14738 203075 : if (tree_int_cst_sgn (op1) < 0)
14739 : {
14740 269 : int_const = false;
14741 269 : if (c_inhibit_evaluation_warnings == 0)
14742 129 : warning_at (location, OPT_Wshift_count_negative,
14743 : "right shift count is negative");
14744 : }
14745 202806 : else if (code0 == VECTOR_TYPE)
14746 : {
14747 458 : if (compare_tree_int (op1,
14748 458 : TYPE_PRECISION (TREE_TYPE (type0)))
14749 : >= 0)
14750 : {
14751 16 : int_const = false;
14752 16 : if (c_inhibit_evaluation_warnings == 0)
14753 16 : warning_at (location, OPT_Wshift_count_overflow,
14754 : "right shift count >= width of vector element");
14755 : }
14756 : }
14757 : else
14758 : {
14759 202348 : if (!integer_zerop (op1))
14760 201990 : short_shift = 1;
14761 :
14762 202348 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14763 : {
14764 586 : int_const = false;
14765 586 : if (c_inhibit_evaluation_warnings == 0)
14766 52 : warning_at (location, OPT_Wshift_count_overflow,
14767 : "right shift count >= width of type");
14768 : }
14769 : }
14770 : }
14771 :
14772 : /* Use the type of the value to be shifted. */
14773 : result_type = type0;
14774 : /* Avoid converting op1 to result_type later. */
14775 : converted = 1;
14776 : }
14777 : break;
14778 :
14779 807189 : case LSHIFT_EXPR:
14780 807189 : if (gnu_vector_type_p (type0)
14781 596 : && gnu_vector_type_p (type1)
14782 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14783 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14784 807502 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14785 : TYPE_VECTOR_SUBPARTS (type1)))
14786 : {
14787 : result_type = type0;
14788 : converted = 1;
14789 : }
14790 806878 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14791 791 : || code0 == BITINT_TYPE
14792 291 : || (gnu_vector_type_p (type0)
14793 285 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14794 807660 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14795 : {
14796 806864 : doing_shift = true;
14797 806864 : if (TREE_CODE (op0) == INTEGER_CST
14798 441041 : && tree_int_cst_sgn (op0) < 0
14799 807613 : && !TYPE_OVERFLOW_WRAPS (type0))
14800 : {
14801 : /* Don't reject a left shift of a negative value in a context
14802 : where a constant expression is needed in C90. */
14803 712 : if (flag_isoc99)
14804 666 : int_const = false;
14805 712 : if (c_inhibit_evaluation_warnings == 0)
14806 712 : warning_at (location, OPT_Wshift_negative_value,
14807 : "left shift of negative value");
14808 : }
14809 806864 : if (TREE_CODE (op1) == INTEGER_CST)
14810 : {
14811 734197 : if (tree_int_cst_sgn (op1) < 0)
14812 : {
14813 313 : int_const = false;
14814 313 : if (c_inhibit_evaluation_warnings == 0)
14815 135 : warning_at (location, OPT_Wshift_count_negative,
14816 : "left shift count is negative");
14817 : }
14818 733884 : else if (code0 == VECTOR_TYPE)
14819 : {
14820 216 : if (compare_tree_int (op1,
14821 216 : TYPE_PRECISION (TREE_TYPE (type0)))
14822 : >= 0)
14823 : {
14824 6 : int_const = false;
14825 6 : if (c_inhibit_evaluation_warnings == 0)
14826 6 : warning_at (location, OPT_Wshift_count_overflow,
14827 : "left shift count >= width of vector element");
14828 : }
14829 : }
14830 733668 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14831 : {
14832 29733 : int_const = false;
14833 29733 : if (c_inhibit_evaluation_warnings == 0)
14834 113 : warning_at (location, OPT_Wshift_count_overflow,
14835 : "left shift count >= width of type");
14836 : }
14837 703935 : else if (TREE_CODE (op0) == INTEGER_CST
14838 367150 : && maybe_warn_shift_overflow (location, op0, op1)
14839 704562 : && flag_isoc99)
14840 : int_const = false;
14841 : }
14842 :
14843 : /* Use the type of the value to be shifted. */
14844 : result_type = type0;
14845 : /* Avoid converting op1 to result_type later. */
14846 : converted = 1;
14847 : }
14848 : break;
14849 :
14850 1749367 : case EQ_EXPR:
14851 1749367 : case NE_EXPR:
14852 1749367 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14853 : {
14854 42089 : tree intt;
14855 42089 : if (!vector_types_compatible_elements_p (type0, type1))
14856 : {
14857 4 : error_at (location, "comparing vectors with different "
14858 : "element types");
14859 4 : return error_mark_node;
14860 : }
14861 :
14862 42085 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14863 84170 : TYPE_VECTOR_SUBPARTS (type1)))
14864 : {
14865 1 : error_at (location, "comparing vectors with different "
14866 : "number of elements");
14867 1 : return error_mark_node;
14868 : }
14869 :
14870 : /* It's not precisely specified how the usual arithmetic
14871 : conversions apply to the vector types. Here, we use
14872 : the unsigned type if one of the operands is signed and
14873 : the other one is unsigned. */
14874 42084 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14875 : {
14876 4 : if (!TYPE_UNSIGNED (type0))
14877 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14878 : else
14879 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14880 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14881 : "types %qT and %qT", type0, type1);
14882 : }
14883 :
14884 : /* Always construct signed integer vector type. */
14885 42084 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14886 : result_type = type0;
14887 : else
14888 : {
14889 42084 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14890 :
14891 126252 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14892 84168 : (SCALAR_TYPE_MODE
14893 : (TREE_TYPE (type0))), 0);
14894 42084 : if (!intt)
14895 : {
14896 0 : error_at (location, "could not find an integer type "
14897 : "of the same size as %qT",
14898 0 : TREE_TYPE (type0));
14899 0 : return error_mark_node;
14900 : }
14901 42084 : result_type = build_opaque_vector_type (intt, nelts);
14902 : }
14903 42084 : converted = 1;
14904 42084 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14905 42084 : goto return_build_binary_op;
14906 : }
14907 1707278 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14908 254842 : warning_at (location,
14909 254842 : OPT_Wfloat_equal,
14910 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14911 : /* Result of comparison is always int,
14912 : but don't convert the args to int! */
14913 1707278 : build_type = integer_type_node;
14914 1707278 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14915 159022 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14916 1583980 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14917 : || code1 == BITINT_TYPE
14918 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14919 : short_compare = 1;
14920 123564 : else if (code0 == POINTER_TYPE
14921 123564 : && (code1 == NULLPTR_TYPE
14922 123187 : || null_pointer_constant_p (orig_op1)))
14923 : {
14924 51986 : maybe_warn_for_null_address (location, op0, code);
14925 51986 : result_type = type0;
14926 : }
14927 71578 : else if (code1 == POINTER_TYPE
14928 71578 : && (code0 == NULLPTR_TYPE
14929 71476 : || null_pointer_constant_p (orig_op0)))
14930 : {
14931 294 : maybe_warn_for_null_address (location, op1, code);
14932 294 : result_type = type1;
14933 : }
14934 71284 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14935 : {
14936 71194 : tree tt0 = TREE_TYPE (type0);
14937 71194 : tree tt1 = TREE_TYPE (type1);
14938 71194 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14939 71194 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14940 71194 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14941 :
14942 : /* Anything compares with void *. void * compares with anything.
14943 : Otherwise, the targets must be compatible
14944 : and both must be object or both incomplete. */
14945 71194 : if (comp_target_types (location, type0, type1))
14946 49857 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14947 21337 : else if (!addr_space_superset (as0, as1, &as_common))
14948 : {
14949 0 : error_at (location, "comparison of pointers to "
14950 : "disjoint address spaces");
14951 0 : return error_mark_node;
14952 : }
14953 21337 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14954 : {
14955 21105 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14956 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14957 : "comparison of %<void *%> with function pointer");
14958 : }
14959 232 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14960 : {
14961 184 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14962 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14963 : "comparison of %<void *%> with function pointer");
14964 : }
14965 : else
14966 : /* Avoid warning about the volatile ObjC EH puts on decls. */
14967 48 : if (!objc_ok)
14968 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14969 : "comparison of distinct pointer types lacks a cast");
14970 :
14971 49913 : if (result_type == NULL_TREE)
14972 : {
14973 21337 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14974 21337 : result_type = c_build_pointer_type
14975 21337 : (c_build_qualified_type (void_type_node, qual));
14976 : }
14977 : }
14978 90 : else if (code0 == POINTER_TYPE
14979 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14980 : {
14981 26 : result_type = type0;
14982 26 : pedwarn (location, 0, "comparison between pointer and integer");
14983 : }
14984 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14985 21 : && code1 == POINTER_TYPE)
14986 : {
14987 8 : result_type = type1;
14988 8 : pedwarn (location, 0, "comparison between pointer and integer");
14989 : }
14990 : /* 6.5.9: One of the following shall hold:
14991 : -- both operands have type nullptr_t; */
14992 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14993 : {
14994 22 : result_type = nullptr_type_node;
14995 : /* No need to convert the operands to result_type later. */
14996 22 : converted = 1;
14997 : }
14998 : /* -- one operand has type nullptr_t and the other is a null pointer
14999 : constant. We will have to convert the former to the type of the
15000 : latter, because during gimplification we can't have mismatching
15001 : comparison operand type. We convert from nullptr_t to the other
15002 : type, since only nullptr_t can be converted to nullptr_t. Also,
15003 : even a constant 0 is a null pointer constant, so we may have to
15004 : create a pointer type from its type. */
15005 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
15006 19 : result_type = (INTEGRAL_TYPE_P (type1)
15007 19 : ? c_build_pointer_type (type1) : type1);
15008 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
15009 11 : result_type = (INTEGRAL_TYPE_P (type0)
15010 11 : ? c_build_pointer_type (type0) : type0);
15011 5046768 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15012 3339465 : || truth_value_p (TREE_CODE (orig_op0)))
15013 3407129 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15014 3407125 : || truth_value_p (TREE_CODE (orig_op1))))
15015 71956 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15016 : break;
15017 :
15018 1008733 : case LE_EXPR:
15019 1008733 : case GE_EXPR:
15020 1008733 : case LT_EXPR:
15021 1008733 : case GT_EXPR:
15022 1008733 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
15023 : {
15024 59410 : tree intt;
15025 59410 : if (!vector_types_compatible_elements_p (type0, type1))
15026 : {
15027 5 : error_at (location, "comparing vectors with different "
15028 : "element types");
15029 5 : return error_mark_node;
15030 : }
15031 :
15032 59405 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
15033 118810 : TYPE_VECTOR_SUBPARTS (type1)))
15034 : {
15035 0 : error_at (location, "comparing vectors with different "
15036 : "number of elements");
15037 0 : return error_mark_node;
15038 : }
15039 :
15040 : /* It's not precisely specified how the usual arithmetic
15041 : conversions apply to the vector types. Here, we use
15042 : the unsigned type if one of the operands is signed and
15043 : the other one is unsigned. */
15044 59405 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
15045 : {
15046 15 : if (!TYPE_UNSIGNED (type0))
15047 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
15048 : else
15049 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15050 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15051 : "types %qT and %qT", type0, type1);
15052 : }
15053 :
15054 : /* Always construct signed integer vector type. */
15055 178215 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15056 118810 : (SCALAR_TYPE_MODE
15057 : (TREE_TYPE (type0))), 0);
15058 59405 : if (!intt)
15059 : {
15060 0 : error_at (location, "could not find an integer type "
15061 : "of the same size as %qT",
15062 0 : TREE_TYPE (type0));
15063 0 : return error_mark_node;
15064 : }
15065 59405 : result_type = build_opaque_vector_type (intt,
15066 59405 : TYPE_VECTOR_SUBPARTS (type0));
15067 59405 : converted = 1;
15068 59405 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15069 59405 : goto return_build_binary_op;
15070 : }
15071 949323 : build_type = integer_type_node;
15072 949323 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15073 65124 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15074 884766 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15075 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15076 : short_compare = 1;
15077 64589 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15078 : {
15079 64522 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15080 64522 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15081 64522 : addr_space_t as_common;
15082 :
15083 64522 : if (comp_target_types (location, type0, type1))
15084 : {
15085 64357 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15086 64357 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15087 64357 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15088 32 : pedwarn_c99 (location, OPT_Wpedantic,
15089 : "comparison of complete and incomplete pointers");
15090 64325 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15091 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15092 : "ordered comparisons of pointers to functions");
15093 64325 : else if (null_pointer_constant_p (orig_op0)
15094 64325 : || null_pointer_constant_p (orig_op1))
15095 8 : warning_at (location, OPT_Wextra,
15096 : "ordered comparison of pointer with null pointer");
15097 :
15098 : }
15099 165 : else if (!addr_space_superset (as0, as1, &as_common))
15100 : {
15101 0 : error_at (location, "comparison of pointers to "
15102 : "disjoint address spaces");
15103 0 : return error_mark_node;
15104 : }
15105 : else
15106 : {
15107 165 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
15108 165 : result_type = c_build_pointer_type
15109 165 : (c_build_qualified_type (void_type_node, qual));
15110 165 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15111 : "comparison of distinct pointer types lacks a cast");
15112 : }
15113 : }
15114 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15115 : {
15116 17 : result_type = type0;
15117 17 : if (pedantic)
15118 11 : pedwarn (location, OPT_Wpedantic,
15119 : "ordered comparison of pointer with integer zero");
15120 6 : else if (extra_warnings)
15121 1 : warning_at (location, OPT_Wextra,
15122 : "ordered comparison of pointer with integer zero");
15123 : }
15124 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15125 : {
15126 12 : result_type = type1;
15127 12 : if (pedantic)
15128 2 : pedwarn (location, OPT_Wpedantic,
15129 : "ordered comparison of pointer with integer zero");
15130 10 : else if (extra_warnings)
15131 1 : warning_at (location, OPT_Wextra,
15132 : "ordered comparison of pointer with integer zero");
15133 : }
15134 38 : else if (code0 == POINTER_TYPE
15135 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15136 : {
15137 18 : result_type = type0;
15138 18 : pedwarn (location, 0, "comparison between pointer and integer");
15139 : }
15140 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15141 20 : && code1 == POINTER_TYPE)
15142 : {
15143 18 : result_type = type1;
15144 18 : pedwarn (location, 0, "comparison between pointer and integer");
15145 : }
15146 :
15147 949323 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15148 64587 : && current_function_decl != NULL_TREE
15149 1013899 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15150 : {
15151 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15152 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15153 :
15154 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15155 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15156 : }
15157 :
15158 2847877 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15159 1898554 : || truth_value_p (TREE_CODE (orig_op0)))
15160 1898571 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15161 1898571 : || truth_value_p (TREE_CODE (orig_op1))))
15162 526 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15163 : break;
15164 :
15165 43 : case MIN_EXPR:
15166 43 : case MAX_EXPR:
15167 : /* Used for OpenMP atomics. */
15168 43 : gcc_assert (flag_openmp);
15169 : common = 1;
15170 : break;
15171 :
15172 0 : default:
15173 0 : gcc_unreachable ();
15174 : }
15175 :
15176 15569996 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15177 0 : return error_mark_node;
15178 :
15179 15569996 : if (gnu_vector_type_p (type0)
15180 1486175 : && gnu_vector_type_p (type1)
15181 17055334 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15182 1485318 : || !vector_types_compatible_elements_p (type0, type1)))
15183 : {
15184 26 : gcc_rich_location richloc (location);
15185 26 : maybe_range_label_for_tree_type_mismatch
15186 26 : label_for_op0 (orig_op0, orig_op1),
15187 26 : label_for_op1 (orig_op1, orig_op0);
15188 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15189 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15190 26 : binary_op_error (&richloc, code, type0, type1);
15191 26 : return error_mark_node;
15192 26 : }
15193 :
15194 15569970 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15195 1743365 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15196 1674419 : || gnu_vector_type_p (type0))
15197 17125065 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15198 1579746 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15199 1487442 : || gnu_vector_type_p (type1)))
15200 : {
15201 15379575 : bool first_complex = (code0 == COMPLEX_TYPE);
15202 15379575 : bool second_complex = (code1 == COMPLEX_TYPE);
15203 15379575 : int none_complex = (!first_complex && !second_complex);
15204 :
15205 15379575 : if (shorten || common || short_compare)
15206 : {
15207 13829102 : result_type = c_common_type (type0, type1);
15208 13829102 : do_warn_double_promotion (result_type, type0, type1,
15209 : "implicit conversion from %qT to %qT "
15210 : "to match other operand of binary "
15211 : "expression",
15212 : location);
15213 13829102 : if (result_type == error_mark_node)
15214 : return error_mark_node;
15215 : }
15216 :
15217 15379553 : if (first_complex != second_complex
15218 82078 : && (code == PLUS_EXPR
15219 : || code == MINUS_EXPR
15220 82078 : || code == MULT_EXPR
15221 17526 : || (code == TRUNC_DIV_EXPR && first_complex))
15222 65966 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15223 15441808 : && flag_signed_zeros)
15224 : {
15225 : /* An operation on mixed real/complex operands must be
15226 : handled specially, but the language-independent code can
15227 : more easily optimize the plain complex arithmetic if
15228 : -fno-signed-zeros. */
15229 61943 : tree real_type = TREE_TYPE (result_type);
15230 61943 : tree real, imag;
15231 61943 : if (type0 != orig_type0 || type1 != orig_type1)
15232 : {
15233 94 : gcc_assert (may_need_excess_precision && common);
15234 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15235 : }
15236 61943 : if (first_complex)
15237 : {
15238 8253 : if (TREE_TYPE (op0) != result_type)
15239 1787 : op0 = convert_and_check (location, result_type, op0);
15240 8253 : if (TREE_TYPE (op1) != real_type)
15241 4601 : op1 = convert_and_check (location, real_type, op1);
15242 : }
15243 : else
15244 : {
15245 53690 : if (TREE_TYPE (op0) != real_type)
15246 2698 : op0 = convert_and_check (location, real_type, op0);
15247 53690 : if (TREE_TYPE (op1) != result_type)
15248 1866 : op1 = convert_and_check (location, result_type, op1);
15249 : }
15250 61943 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15251 0 : return error_mark_node;
15252 61943 : if (first_complex)
15253 : {
15254 8253 : op0 = save_expr (op0);
15255 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15256 : op0, true);
15257 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15258 : op0, true);
15259 8253 : switch (code)
15260 : {
15261 3488 : case MULT_EXPR:
15262 3488 : case TRUNC_DIV_EXPR:
15263 3488 : op1 = save_expr (op1);
15264 3488 : imag = build2 (resultcode, real_type, imag, op1);
15265 : /* Fall through. */
15266 8253 : case PLUS_EXPR:
15267 8253 : case MINUS_EXPR:
15268 8253 : real = build2 (resultcode, real_type, real, op1);
15269 8253 : break;
15270 : default:
15271 : gcc_unreachable();
15272 : }
15273 : }
15274 : else
15275 : {
15276 53690 : op1 = save_expr (op1);
15277 53690 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15278 : op1, true);
15279 53690 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15280 : op1, true);
15281 53690 : switch (code)
15282 : {
15283 2008 : case MULT_EXPR:
15284 2008 : op0 = save_expr (op0);
15285 2008 : imag = build2 (resultcode, real_type, op0, imag);
15286 : /* Fall through. */
15287 31958 : case PLUS_EXPR:
15288 31958 : real = build2 (resultcode, real_type, op0, real);
15289 31958 : break;
15290 21732 : case MINUS_EXPR:
15291 21732 : real = build2 (resultcode, real_type, op0, real);
15292 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15293 21732 : break;
15294 0 : default:
15295 0 : gcc_unreachable();
15296 : }
15297 : }
15298 61943 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15299 61943 : goto return_build_binary_op;
15300 : }
15301 :
15302 : /* For certain operations (which identify themselves by shorten != 0)
15303 : if both args were extended from the same smaller type,
15304 : do the arithmetic in that type and then extend.
15305 :
15306 : shorten !=0 and !=1 indicates a bitwise operation.
15307 : For them, this optimization is safe only if
15308 : both args are zero-extended or both are sign-extended.
15309 : Otherwise, we might change the result.
15310 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15311 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15312 :
15313 15317610 : if (shorten && none_complex)
15314 : {
15315 1296104 : final_type = result_type;
15316 1296104 : result_type = shorten_binary_op (result_type, op0, op1,
15317 : shorten == -1);
15318 : }
15319 :
15320 : /* Shifts can be shortened if shifting right. */
15321 :
15322 15317610 : if (short_shift)
15323 : {
15324 201990 : int unsigned_arg;
15325 201990 : tree arg0 = get_narrower (op0, &unsigned_arg);
15326 :
15327 201990 : final_type = result_type;
15328 :
15329 201990 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15330 142235 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15331 :
15332 201990 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15333 2997 : && tree_int_cst_sgn (op1) > 0
15334 : /* We can shorten only if the shift count is less than the
15335 : number of bits in the smaller type size. */
15336 2997 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15337 : /* We cannot drop an unsigned shift after sign-extension. */
15338 204847 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15339 : {
15340 : /* Do an unsigned shift if the operand was zero-extended. */
15341 2842 : result_type
15342 2842 : = c_common_signed_or_unsigned_type (unsigned_arg,
15343 2842 : TREE_TYPE (arg0));
15344 : /* Convert value-to-be-shifted to that type. */
15345 2842 : if (TREE_TYPE (op0) != result_type)
15346 2842 : op0 = convert (result_type, op0);
15347 : converted = 1;
15348 : }
15349 : }
15350 :
15351 : /* Comparison operations are shortened too but differently.
15352 : They identify themselves by setting short_compare = 1. */
15353 :
15354 15317610 : if (short_compare)
15355 : {
15356 : /* Don't write &op0, etc., because that would prevent op0
15357 : from being kept in a register.
15358 : Instead, make copies of the our local variables and
15359 : pass the copies by reference, then copy them back afterward. */
15360 2468446 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15361 2468446 : enum tree_code xresultcode = resultcode;
15362 2468446 : tree val
15363 2468446 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15364 : &xresultcode);
15365 :
15366 2468446 : if (val != NULL_TREE)
15367 : {
15368 15889 : ret = val;
15369 15889 : goto return_build_binary_op;
15370 : }
15371 :
15372 2452557 : op0 = xop0, op1 = xop1;
15373 2452557 : converted = 1;
15374 2452557 : resultcode = xresultcode;
15375 :
15376 2452557 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15377 : {
15378 2305026 : bool op0_maybe_const = true;
15379 2305026 : bool op1_maybe_const = true;
15380 2305026 : tree orig_op0_folded, orig_op1_folded;
15381 :
15382 2305026 : if (in_late_binary_op)
15383 : {
15384 : orig_op0_folded = orig_op0;
15385 : orig_op1_folded = orig_op1;
15386 : }
15387 : else
15388 : {
15389 : /* Fold for the sake of possible warnings, as in
15390 : build_conditional_expr. This requires the
15391 : "original" values to be folded, not just op0 and
15392 : op1. */
15393 2296386 : c_inhibit_evaluation_warnings++;
15394 2296386 : op0 = c_fully_fold (op0, require_constant_value,
15395 : &op0_maybe_const);
15396 2296386 : op1 = c_fully_fold (op1, require_constant_value,
15397 : &op1_maybe_const);
15398 2296386 : c_inhibit_evaluation_warnings--;
15399 2296386 : orig_op0_folded = c_fully_fold (orig_op0,
15400 : require_constant_value,
15401 : NULL);
15402 2296386 : orig_op1_folded = c_fully_fold (orig_op1,
15403 : require_constant_value,
15404 : NULL);
15405 : }
15406 :
15407 2305026 : if (warn_sign_compare)
15408 377479 : warn_for_sign_compare (location, orig_op0_folded,
15409 : orig_op1_folded, op0, op1,
15410 : result_type, resultcode);
15411 2305026 : if (!in_late_binary_op && !int_operands)
15412 : {
15413 2118784 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15414 2115274 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15415 2118784 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15416 831215 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15417 : }
15418 : }
15419 : }
15420 : }
15421 :
15422 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15423 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15424 : Then the expression will be built.
15425 : It will be given type FINAL_TYPE if that is nonzero;
15426 : otherwise, it will be given type RESULT_TYPE. */
15427 :
15428 15492116 : if (!result_type)
15429 : {
15430 : /* Favor showing any expression locations that are available. */
15431 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15432 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15433 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15434 514 : return error_mark_node;
15435 514 : }
15436 :
15437 15491602 : if (build_type == NULL_TREE)
15438 : {
15439 12850898 : build_type = result_type;
15440 12850898 : if ((type0 != orig_type0 || type1 != orig_type1)
15441 797 : && !boolean_op)
15442 : {
15443 797 : gcc_assert (may_need_excess_precision && common);
15444 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15445 : }
15446 : }
15447 :
15448 15491602 : if (!converted)
15449 : {
15450 11485569 : op0 = ep_convert_and_check (location, result_type, op0,
15451 : semantic_result_type);
15452 11485569 : op1 = ep_convert_and_check (location, result_type, op1,
15453 : semantic_result_type);
15454 :
15455 : /* This can happen if one operand has a vector type, and the other
15456 : has a different type. */
15457 11485569 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15458 3 : return error_mark_node;
15459 : }
15460 :
15461 15491599 : if (sanitize_flags_p ((SANITIZE_SHIFT
15462 : | SANITIZE_DIVIDE
15463 : | SANITIZE_FLOAT_DIVIDE
15464 : | SANITIZE_SI_OVERFLOW))
15465 23433 : && current_function_decl != NULL_TREE
15466 19963 : && (doing_div_or_mod || doing_shift)
15467 15494528 : && !require_constant_value)
15468 : {
15469 : /* OP0 and/or OP1 might have side-effects. */
15470 2873 : op0 = save_expr (op0);
15471 2873 : op1 = save_expr (op1);
15472 2873 : op0 = c_fully_fold (op0, false, NULL);
15473 2873 : op1 = c_fully_fold (op1, false, NULL);
15474 2873 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15475 : | SANITIZE_FLOAT_DIVIDE
15476 : | SANITIZE_SI_OVERFLOW))))
15477 916 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15478 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15479 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15480 : }
15481 :
15482 : /* Treat expressions in initializers specially as they can't trap. */
15483 15491599 : if (int_const_or_overflow)
15484 7997421 : ret = (require_constant_value
15485 7997421 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15486 : op0, op1)
15487 7951230 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15488 : else
15489 7494178 : ret = build2 (resultcode, build_type, op0, op1);
15490 15491599 : if (final_type != NULL_TREE)
15491 1498094 : ret = convert (final_type, ret);
15492 :
15493 13993505 : return_build_binary_op:
15494 16949936 : gcc_assert (ret != error_mark_node);
15495 16949936 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15496 1853 : ret = (int_operands
15497 1853 : ? note_integer_operands (ret)
15498 532 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15499 16948083 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15500 60311 : && !in_late_binary_op)
15501 60311 : ret = note_integer_operands (ret);
15502 16949936 : protected_set_expr_location (ret, location);
15503 :
15504 16949936 : if (instrument_expr != NULL)
15505 1424 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15506 : instrument_expr, ret);
15507 :
15508 16949936 : if (semantic_result_type)
15509 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15510 : semantic_result_type, ret);
15511 :
15512 : return ret;
15513 : }
15514 :
15515 :
15516 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15517 : purpose. LOCATION is the source location for the expression. */
15518 :
15519 : tree
15520 4154347 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15521 : {
15522 4154347 : bool int_const, int_operands;
15523 :
15524 4154347 : switch (TREE_CODE (TREE_TYPE (expr)))
15525 : {
15526 72 : case ARRAY_TYPE:
15527 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15528 72 : return error_mark_node;
15529 :
15530 86 : case RECORD_TYPE:
15531 86 : error_at (location, "used struct type value where scalar is required");
15532 86 : return error_mark_node;
15533 :
15534 83 : case UNION_TYPE:
15535 83 : error_at (location, "used union type value where scalar is required");
15536 83 : return error_mark_node;
15537 :
15538 22 : case VOID_TYPE:
15539 22 : error_at (location, "void value not ignored as it ought to be");
15540 22 : return error_mark_node;
15541 :
15542 30970 : case POINTER_TYPE:
15543 30970 : if (reject_gcc_builtin (expr))
15544 3 : return error_mark_node;
15545 : break;
15546 :
15547 0 : case FUNCTION_TYPE:
15548 0 : gcc_unreachable ();
15549 :
15550 8 : case VECTOR_TYPE:
15551 8 : error_at (location, "used vector type where scalar is required");
15552 8 : return error_mark_node;
15553 :
15554 : default:
15555 : break;
15556 : }
15557 :
15558 : /* Conversion of a floating constant to boolean goes through here
15559 : and yields an integer constant expression. Otherwise, the result
15560 : is only an integer constant expression if the argument is. */
15561 899962 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15562 4154149 : || ((TREE_CODE (expr) == REAL_CST
15563 3253813 : || TREE_CODE (expr) == COMPLEX_CST)
15564 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15565 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15566 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15567 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15568 : == BOOLEAN_TYPE)))));
15569 4154073 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15570 900122 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15571 : {
15572 319 : expr = remove_c_maybe_const_expr (expr);
15573 319 : expr = build2 (NE_EXPR, type, expr,
15574 319 : convert (TREE_TYPE (expr), integer_zero_node));
15575 319 : expr = note_integer_operands (expr);
15576 : }
15577 : else
15578 : {
15579 : /* ??? Should we also give an error for vectors rather than leaving
15580 : those to give errors later? */
15581 4153754 : expr = c_common_truthvalue_conversion (location, expr);
15582 4153754 : expr = fold_convert_loc (location, type, expr);
15583 : }
15584 :
15585 4154073 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15586 : {
15587 76 : if (TREE_OVERFLOW (expr))
15588 : return expr;
15589 : else
15590 76 : return note_integer_operands (expr);
15591 : }
15592 4153997 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15593 857 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15594 : return expr;
15595 : }
15596 :
15597 :
15598 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15599 : required. */
15600 :
15601 : tree
15602 126924001 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15603 : {
15604 126924001 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15605 : {
15606 706 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15607 : /* Executing a compound literal inside a function reinitializes
15608 : it. */
15609 706 : if (!TREE_STATIC (decl))
15610 440 : *se = true;
15611 : return decl;
15612 : }
15613 : else
15614 : return expr;
15615 : }
15616 :
15617 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15618 : statement. LOC is the location of the construct. */
15619 :
15620 : tree
15621 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15622 : tree clauses)
15623 : {
15624 2154 : body = c_end_compound_stmt (loc, body, true);
15625 :
15626 2154 : tree stmt = make_node (code);
15627 2154 : TREE_TYPE (stmt) = void_type_node;
15628 2154 : OMP_BODY (stmt) = body;
15629 2154 : OMP_CLAUSES (stmt) = clauses;
15630 2154 : SET_EXPR_LOCATION (stmt, loc);
15631 :
15632 2154 : return add_stmt (stmt);
15633 : }
15634 :
15635 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15636 : statement. LOC is the location of the OACC_DATA. */
15637 :
15638 : tree
15639 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15640 : {
15641 497 : tree stmt;
15642 :
15643 497 : block = c_end_compound_stmt (loc, block, true);
15644 :
15645 497 : stmt = make_node (OACC_DATA);
15646 497 : TREE_TYPE (stmt) = void_type_node;
15647 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15648 497 : OACC_DATA_BODY (stmt) = block;
15649 497 : SET_EXPR_LOCATION (stmt, loc);
15650 :
15651 497 : return add_stmt (stmt);
15652 : }
15653 :
15654 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15655 : statement. LOC is the location of the OACC_HOST_DATA. */
15656 :
15657 : tree
15658 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15659 : {
15660 21 : tree stmt;
15661 :
15662 21 : block = c_end_compound_stmt (loc, block, true);
15663 :
15664 21 : stmt = make_node (OACC_HOST_DATA);
15665 21 : TREE_TYPE (stmt) = void_type_node;
15666 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15667 21 : OACC_HOST_DATA_BODY (stmt) = block;
15668 21 : SET_EXPR_LOCATION (stmt, loc);
15669 :
15670 21 : return add_stmt (stmt);
15671 : }
15672 :
15673 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15674 :
15675 : tree
15676 12030 : c_begin_omp_parallel (void)
15677 : {
15678 12030 : tree block;
15679 :
15680 12030 : keep_next_level ();
15681 12030 : block = c_begin_compound_stmt (true);
15682 :
15683 12030 : return block;
15684 : }
15685 :
15686 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15687 : statement. LOC is the location of the OMP_PARALLEL. */
15688 :
15689 : tree
15690 5693 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15691 : {
15692 5693 : tree stmt;
15693 :
15694 5693 : block = c_end_compound_stmt (loc, block, true);
15695 :
15696 5693 : stmt = make_node (OMP_PARALLEL);
15697 5693 : TREE_TYPE (stmt) = void_type_node;
15698 5693 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15699 5693 : OMP_PARALLEL_BODY (stmt) = block;
15700 5693 : SET_EXPR_LOCATION (stmt, loc);
15701 :
15702 5693 : return add_stmt (stmt);
15703 : }
15704 :
15705 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15706 :
15707 : tree
15708 903 : c_begin_omp_task (void)
15709 : {
15710 903 : tree block;
15711 :
15712 903 : keep_next_level ();
15713 903 : block = c_begin_compound_stmt (true);
15714 :
15715 903 : return block;
15716 : }
15717 :
15718 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15719 : statement. LOC is the location of the #pragma. */
15720 :
15721 : tree
15722 903 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15723 : {
15724 903 : tree stmt;
15725 :
15726 903 : block = c_end_compound_stmt (loc, block, true);
15727 :
15728 903 : stmt = make_node (OMP_TASK);
15729 903 : TREE_TYPE (stmt) = void_type_node;
15730 903 : OMP_TASK_CLAUSES (stmt) = clauses;
15731 903 : OMP_TASK_BODY (stmt) = block;
15732 903 : SET_EXPR_LOCATION (stmt, loc);
15733 :
15734 903 : return add_stmt (stmt);
15735 : }
15736 :
15737 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15738 :
15739 : void
15740 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15741 : {
15742 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15743 213 : int mask = 0;
15744 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15745 : mask = 1;
15746 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15747 : mask = 2;
15748 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15749 : mask = 4;
15750 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15751 : mask = 8;
15752 : else
15753 : {
15754 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15755 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15756 : "clauses");
15757 0 : return;
15758 : }
15759 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15760 213 : if (ifc != NULL_TREE)
15761 : {
15762 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15763 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15764 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15765 : "expected %<cancel%> %<if%> clause modifier");
15766 : else
15767 : {
15768 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15769 29 : if (ifc2 != NULL_TREE)
15770 : {
15771 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15772 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15773 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15774 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15775 : "expected %<cancel%> %<if%> clause modifier");
15776 : }
15777 : }
15778 :
15779 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15780 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15781 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15782 : build_zero_cst (type));
15783 : }
15784 : else
15785 182 : ifc = boolean_true_node;
15786 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15787 213 : build_int_cst (integer_type_node, mask),
15788 : ifc);
15789 213 : add_stmt (stmt);
15790 : }
15791 :
15792 : /* Generate GOMP_cancellation_point call for
15793 : #pragma omp cancellation point. */
15794 :
15795 : void
15796 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15797 : {
15798 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15799 167 : int mask = 0;
15800 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15801 : mask = 1;
15802 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15803 : mask = 2;
15804 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15805 : mask = 4;
15806 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15807 : mask = 8;
15808 : else
15809 : {
15810 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15811 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15812 : "clauses");
15813 1 : return;
15814 : }
15815 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15816 166 : build_int_cst (integer_type_node, mask));
15817 166 : add_stmt (stmt);
15818 : }
15819 :
15820 : /* Helper function for handle_omp_array_sections. Called recursively
15821 : to handle multiple array-section-subscripts. C is the clause,
15822 : T current expression (initially OMP_CLAUSE_DECL), which is either
15823 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15824 : expression if specified, TREE_VALUE length expression if specified,
15825 : TREE_CHAIN is what it has been specified after, or some decl.
15826 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15827 : set to true if any of the array-section-subscript could have length
15828 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15829 : first array-section-subscript which is known not to have length
15830 : of one. Given say:
15831 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15832 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15833 : all are or may have length of 1, array-section-subscript [:2] is the
15834 : first one known not to have length 1. For array-section-subscript
15835 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15836 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15837 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15838 : case though, as some lengths could be zero. */
15839 :
15840 : static tree
15841 6413 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15842 : bool &maybe_zero_len, unsigned int &first_non_one,
15843 : enum c_omp_region_type ort)
15844 : {
15845 6413 : tree ret, low_bound, length, type;
15846 6413 : bool openacc = (ort & C_ORT_ACC) != 0;
15847 6413 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15848 : {
15849 2986 : if (error_operand_p (t))
15850 2 : return error_mark_node;
15851 2984 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15852 2984 : ret = t;
15853 2984 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15854 2880 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15855 5550 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15856 : {
15857 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15858 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15859 6 : return error_mark_node;
15860 : }
15861 2978 : if (!ai.check_clause (c))
15862 0 : return error_mark_node;
15863 2978 : else if (ai.component_access_p ()
15864 3281 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15865 16 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15866 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15867 303 : t = ai.get_root_term (true);
15868 : else
15869 2675 : t = ai.unconverted_ref_origin ();
15870 2978 : if (t == error_mark_node)
15871 : return error_mark_node;
15872 2978 : if (!VAR_P (t)
15873 744 : && (ort == C_ORT_ACC || !EXPR_P (t))
15874 741 : && TREE_CODE (t) != PARM_DECL)
15875 : {
15876 5 : if (DECL_P (t))
15877 5 : error_at (OMP_CLAUSE_LOCATION (c),
15878 : "%qD is not a variable in %qs clause", t,
15879 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15880 : else
15881 0 : error_at (OMP_CLAUSE_LOCATION (c),
15882 : "%qE is not a variable in %qs clause", t,
15883 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15884 5 : return error_mark_node;
15885 : }
15886 2973 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15887 2870 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15888 5530 : && TYPE_ATOMIC (TREE_TYPE (t)))
15889 : {
15890 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15891 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15892 0 : return error_mark_node;
15893 : }
15894 2973 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15895 2870 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15896 2557 : && VAR_P (t)
15897 4981 : && DECL_THREAD_LOCAL_P (t))
15898 : {
15899 3 : error_at (OMP_CLAUSE_LOCATION (c),
15900 : "%qD is threadprivate variable in %qs clause", t,
15901 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15902 3 : return error_mark_node;
15903 : }
15904 2970 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15905 2867 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15906 416 : && TYPE_ATOMIC (TREE_TYPE (t))
15907 2972 : && POINTER_TYPE_P (TREE_TYPE (t)))
15908 : {
15909 : /* If the array section is pointer based and the pointer
15910 : itself is _Atomic qualified, we need to atomically load
15911 : the pointer. */
15912 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15913 : ret, false, false);
15914 : }
15915 : return ret;
15916 2984 : }
15917 :
15918 3427 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15919 : maybe_zero_len, first_non_one, ort);
15920 3427 : if (ret == error_mark_node || ret == NULL_TREE)
15921 : return ret;
15922 :
15923 3359 : type = TREE_TYPE (ret);
15924 3359 : low_bound = TREE_OPERAND (t, 1);
15925 3359 : length = TREE_OPERAND (t, 2);
15926 :
15927 3359 : if (low_bound == error_mark_node || length == error_mark_node)
15928 : return error_mark_node;
15929 :
15930 3359 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15931 : {
15932 13 : error_at (OMP_CLAUSE_LOCATION (c),
15933 : "low bound %qE of array section does not have integral type",
15934 : low_bound);
15935 13 : return error_mark_node;
15936 : }
15937 3346 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15938 : {
15939 12 : error_at (OMP_CLAUSE_LOCATION (c),
15940 : "length %qE of array section does not have integral type",
15941 : length);
15942 12 : return error_mark_node;
15943 : }
15944 3334 : if (low_bound
15945 2747 : && TREE_CODE (low_bound) == INTEGER_CST
15946 5792 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15947 2458 : > TYPE_PRECISION (sizetype))
15948 0 : low_bound = fold_convert (sizetype, low_bound);
15949 3334 : if (length
15950 3142 : && TREE_CODE (length) == INTEGER_CST
15951 5529 : && TYPE_PRECISION (TREE_TYPE (length))
15952 2195 : > TYPE_PRECISION (sizetype))
15953 0 : length = fold_convert (sizetype, length);
15954 3334 : if (low_bound == NULL_TREE)
15955 587 : low_bound = integer_zero_node;
15956 3334 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15957 3334 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15958 1938 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15959 : {
15960 10 : if (length != integer_one_node)
15961 : {
15962 6 : error_at (OMP_CLAUSE_LOCATION (c),
15963 : "expected single pointer in %qs clause",
15964 : user_omp_clause_code_name (c, openacc));
15965 6 : return error_mark_node;
15966 : }
15967 : }
15968 3328 : if (length != NULL_TREE)
15969 : {
15970 3140 : if (!integer_nonzerop (length))
15971 : {
15972 976 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15973 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15974 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15975 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15976 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15977 : {
15978 180 : if (integer_zerop (length))
15979 : {
15980 12 : error_at (OMP_CLAUSE_LOCATION (c),
15981 : "zero length array section in %qs clause",
15982 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15983 12 : return error_mark_node;
15984 : }
15985 : }
15986 : else
15987 796 : maybe_zero_len = true;
15988 : }
15989 3128 : if (first_non_one == types.length ()
15990 3128 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15991 1600 : first_non_one++;
15992 : }
15993 3316 : if (TREE_CODE (type) == ARRAY_TYPE)
15994 : {
15995 1499 : if (length == NULL_TREE
15996 1499 : && (TYPE_DOMAIN (type) == NULL_TREE
15997 170 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15998 : {
15999 8 : error_at (OMP_CLAUSE_LOCATION (c),
16000 : "for unknown bound array type length expression must "
16001 : "be specified");
16002 8 : return error_mark_node;
16003 : }
16004 1491 : if (TREE_CODE (low_bound) == INTEGER_CST
16005 1491 : && tree_int_cst_sgn (low_bound) == -1)
16006 : {
16007 36 : error_at (OMP_CLAUSE_LOCATION (c),
16008 : "negative low bound in array section in %qs clause",
16009 36 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16010 36 : return error_mark_node;
16011 : }
16012 1455 : if (length != NULL_TREE
16013 1298 : && TREE_CODE (length) == INTEGER_CST
16014 2313 : && tree_int_cst_sgn (length) == -1)
16015 : {
16016 36 : error_at (OMP_CLAUSE_LOCATION (c),
16017 : "negative length in array section in %qs clause",
16018 36 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16019 36 : return error_mark_node;
16020 : }
16021 1419 : if (TYPE_DOMAIN (type)
16022 1408 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
16023 2827 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
16024 : == INTEGER_CST)
16025 : {
16026 1062 : tree size
16027 1062 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
16028 1062 : size = size_binop (PLUS_EXPR, size, size_one_node);
16029 1062 : if (TREE_CODE (low_bound) == INTEGER_CST)
16030 : {
16031 875 : if (tree_int_cst_lt (size, low_bound))
16032 : {
16033 11 : error_at (OMP_CLAUSE_LOCATION (c),
16034 : "low bound %qE above array section size "
16035 : "in %qs clause", low_bound,
16036 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16037 11 : return error_mark_node;
16038 : }
16039 864 : if (tree_int_cst_equal (size, low_bound))
16040 : {
16041 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16042 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16043 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16044 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16045 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16046 : {
16047 5 : error_at (OMP_CLAUSE_LOCATION (c),
16048 : "zero length array section in %qs clause",
16049 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16050 5 : return error_mark_node;
16051 : }
16052 0 : maybe_zero_len = true;
16053 : }
16054 859 : else if (length == NULL_TREE
16055 256 : && first_non_one == types.length ()
16056 914 : && tree_int_cst_equal
16057 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16058 : low_bound))
16059 30 : first_non_one++;
16060 : }
16061 187 : else if (length == NULL_TREE)
16062 : {
16063 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16064 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16065 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16066 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16067 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16068 0 : maybe_zero_len = true;
16069 6 : if (first_non_one == types.length ())
16070 1 : first_non_one++;
16071 : }
16072 1046 : if (length && TREE_CODE (length) == INTEGER_CST)
16073 : {
16074 799 : if (tree_int_cst_lt (size, length))
16075 : {
16076 11 : error_at (OMP_CLAUSE_LOCATION (c),
16077 : "length %qE above array section size "
16078 : "in %qs clause", length,
16079 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16080 11 : return error_mark_node;
16081 : }
16082 788 : if (TREE_CODE (low_bound) == INTEGER_CST)
16083 : {
16084 688 : tree lbpluslen
16085 688 : = size_binop (PLUS_EXPR,
16086 : fold_convert (sizetype, low_bound),
16087 : fold_convert (sizetype, length));
16088 688 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16089 688 : && tree_int_cst_lt (size, lbpluslen))
16090 : {
16091 10 : error_at (OMP_CLAUSE_LOCATION (c),
16092 : "high bound %qE above array section size "
16093 : "in %qs clause", lbpluslen,
16094 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16095 10 : return error_mark_node;
16096 : }
16097 : }
16098 : }
16099 : }
16100 357 : else if (length == NULL_TREE)
16101 : {
16102 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16103 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16104 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16105 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16106 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16107 0 : maybe_zero_len = true;
16108 20 : if (first_non_one == types.length ())
16109 10 : first_non_one++;
16110 : }
16111 :
16112 : /* For [lb:] we will need to evaluate lb more than once. */
16113 929 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16114 : {
16115 127 : tree lb = save_expr (low_bound);
16116 127 : if (lb != low_bound)
16117 : {
16118 2 : TREE_OPERAND (t, 1) = lb;
16119 2 : low_bound = lb;
16120 : }
16121 : }
16122 : }
16123 1817 : else if (TREE_CODE (type) == POINTER_TYPE)
16124 : {
16125 1806 : if (length == NULL_TREE)
16126 : {
16127 12 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16128 10 : error_at (OMP_CLAUSE_LOCATION (c),
16129 : "for array function parameter length expression "
16130 : "must be specified");
16131 : else
16132 2 : error_at (OMP_CLAUSE_LOCATION (c),
16133 : "for pointer type length expression must be specified");
16134 12 : return error_mark_node;
16135 : }
16136 1794 : if (length != NULL_TREE
16137 1794 : && TREE_CODE (length) == INTEGER_CST
16138 1287 : && tree_int_cst_sgn (length) == -1)
16139 : {
16140 20 : error_at (OMP_CLAUSE_LOCATION (c),
16141 : "negative length in array section in %qs clause",
16142 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16143 20 : return error_mark_node;
16144 : }
16145 : /* If there is a pointer type anywhere but in the very first
16146 : array-section-subscript, the array section could be non-contiguous. */
16147 1774 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16148 1613 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16149 3358 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16150 : {
16151 : /* If any prior dimension has a non-one length, then deem this
16152 : array section as non-contiguous. */
16153 42 : for (tree d = TREE_OPERAND (t, 0);
16154 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16155 40 : d = TREE_OPERAND (d, 0))
16156 : {
16157 44 : tree d_length = TREE_OPERAND (d, 2);
16158 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16159 : {
16160 4 : error_at (OMP_CLAUSE_LOCATION (c),
16161 : "array section is not contiguous in %qs clause",
16162 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16163 4 : return error_mark_node;
16164 : }
16165 : }
16166 : }
16167 : }
16168 : else
16169 : {
16170 11 : error_at (OMP_CLAUSE_LOCATION (c),
16171 : "%qE does not have pointer or array type", ret);
16172 11 : return error_mark_node;
16173 : }
16174 3152 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16175 2836 : types.safe_push (TREE_TYPE (ret));
16176 : /* We will need to evaluate lb more than once. */
16177 3152 : tree lb = save_expr (low_bound);
16178 3152 : if (lb != low_bound)
16179 : {
16180 273 : TREE_OPERAND (t, 1) = lb;
16181 273 : low_bound = lb;
16182 : }
16183 3152 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16184 3152 : return ret;
16185 : }
16186 :
16187 : /* Handle array sections for clause C. */
16188 :
16189 : static bool
16190 2986 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16191 : {
16192 2986 : bool maybe_zero_len = false;
16193 2986 : unsigned int first_non_one = 0;
16194 2986 : auto_vec<tree, 10> types;
16195 2986 : tree *tp = &OMP_CLAUSE_DECL (c);
16196 2986 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16197 2672 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16198 3090 : && OMP_ITERATOR_DECL_P (*tp))
16199 66 : tp = &TREE_VALUE (*tp);
16200 2986 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16201 : maybe_zero_len, first_non_one,
16202 : ort);
16203 2986 : if (first == error_mark_node)
16204 : return true;
16205 2763 : if (first == NULL_TREE)
16206 : return false;
16207 2763 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16208 2763 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16209 : {
16210 336 : tree t = *tp;
16211 336 : tree tem = NULL_TREE;
16212 : /* Need to evaluate side effects in the length expressions
16213 : if any. */
16214 336 : while (TREE_CODE (t) == TREE_LIST)
16215 : {
16216 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16217 : {
16218 0 : if (tem == NULL_TREE)
16219 : tem = TREE_VALUE (t);
16220 : else
16221 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16222 0 : TREE_VALUE (t), tem);
16223 : }
16224 0 : t = TREE_CHAIN (t);
16225 : }
16226 336 : if (tem)
16227 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16228 336 : first = c_fully_fold (first, false, NULL, true);
16229 336 : *tp = first;
16230 : }
16231 : else
16232 : {
16233 2427 : unsigned int num = types.length (), i;
16234 2427 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16235 2427 : tree condition = NULL_TREE;
16236 :
16237 2427 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16238 3 : maybe_zero_len = true;
16239 :
16240 5054 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16241 2627 : t = TREE_OPERAND (t, 0))
16242 : {
16243 2642 : tree low_bound = TREE_OPERAND (t, 1);
16244 2642 : tree length = TREE_OPERAND (t, 2);
16245 :
16246 2642 : i--;
16247 2642 : if (low_bound
16248 2178 : && TREE_CODE (low_bound) == INTEGER_CST
16249 4634 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16250 1992 : > TYPE_PRECISION (sizetype))
16251 0 : low_bound = fold_convert (sizetype, low_bound);
16252 2642 : if (length
16253 2550 : && TREE_CODE (length) == INTEGER_CST
16254 4263 : && TYPE_PRECISION (TREE_TYPE (length))
16255 1621 : > TYPE_PRECISION (sizetype))
16256 0 : length = fold_convert (sizetype, length);
16257 2642 : if (low_bound == NULL_TREE)
16258 464 : low_bound = integer_zero_node;
16259 2642 : if (!maybe_zero_len && i > first_non_one)
16260 : {
16261 109 : if (integer_nonzerop (low_bound))
16262 6 : goto do_warn_noncontiguous;
16263 103 : if (length != NULL_TREE
16264 50 : && TREE_CODE (length) == INTEGER_CST
16265 50 : && TYPE_DOMAIN (types[i])
16266 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16267 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16268 : == INTEGER_CST)
16269 : {
16270 50 : tree size;
16271 50 : size = size_binop (PLUS_EXPR,
16272 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16273 : size_one_node);
16274 50 : if (!tree_int_cst_equal (length, size))
16275 : {
16276 7 : do_warn_noncontiguous:
16277 26 : error_at (OMP_CLAUSE_LOCATION (c),
16278 : "array section is not contiguous in %qs "
16279 : "clause",
16280 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16281 2427 : return true;
16282 : }
16283 : }
16284 96 : if (length != NULL_TREE
16285 96 : && TREE_SIDE_EFFECTS (length))
16286 : {
16287 0 : if (side_effects == NULL_TREE)
16288 : side_effects = length;
16289 : else
16290 0 : side_effects = build2 (COMPOUND_EXPR,
16291 0 : TREE_TYPE (side_effects),
16292 : length, side_effects);
16293 : }
16294 : }
16295 : else
16296 : {
16297 2533 : tree l;
16298 :
16299 2533 : if (i > first_non_one
16300 2533 : && ((length && integer_nonzerop (length))
16301 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16302 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16303 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16304 0 : continue;
16305 2533 : if (length)
16306 2500 : l = fold_convert (sizetype, length);
16307 : else
16308 : {
16309 33 : l = size_binop (PLUS_EXPR,
16310 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16311 : size_one_node);
16312 33 : l = size_binop (MINUS_EXPR, l,
16313 : fold_convert (sizetype, low_bound));
16314 : }
16315 2533 : if (i > first_non_one)
16316 : {
16317 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16318 : size_zero_node);
16319 0 : if (condition == NULL_TREE)
16320 : condition = l;
16321 : else
16322 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16323 : l, condition);
16324 : }
16325 2533 : else if (size == NULL_TREE)
16326 : {
16327 2414 : size = size_in_bytes (TREE_TYPE (types[i]));
16328 2414 : tree eltype = TREE_TYPE (types[num - 1]);
16329 2434 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16330 20 : eltype = TREE_TYPE (eltype);
16331 2414 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16332 2179 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16333 4333 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16334 : {
16335 539 : if (integer_zerop (size)
16336 1076 : || integer_zerop (size_in_bytes (eltype)))
16337 : {
16338 4 : error_at (OMP_CLAUSE_LOCATION (c),
16339 : "zero length array section in %qs clause",
16340 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16341 2 : return error_mark_node;
16342 : }
16343 537 : size = size_binop (EXACT_DIV_EXPR, size,
16344 : size_in_bytes (eltype));
16345 : }
16346 2412 : size = size_binop (MULT_EXPR, size, l);
16347 2412 : if (condition)
16348 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16349 : size, size_zero_node);
16350 : }
16351 : else
16352 119 : size = size_binop (MULT_EXPR, size, l);
16353 : }
16354 : }
16355 2412 : if (side_effects)
16356 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16357 2412 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16358 2179 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16359 4331 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16360 : {
16361 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16362 537 : size = c_fully_fold (size, false, NULL);
16363 537 : size = save_expr (size);
16364 537 : tree index_type = build_index_type (size);
16365 537 : tree eltype = TREE_TYPE (first);
16366 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16367 12 : eltype = TREE_TYPE (eltype);
16368 537 : tree type = c_build_array_type (eltype, index_type);
16369 537 : tree ptype = c_build_pointer_type (eltype);
16370 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16371 296 : t = build_fold_addr_expr (t);
16372 537 : tree t2 = build_fold_addr_expr (first);
16373 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16374 : ptrdiff_type_node, t2);
16375 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16376 : ptrdiff_type_node, t2,
16377 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16378 : ptrdiff_type_node, t));
16379 537 : t2 = c_fully_fold (t2, false, NULL);
16380 537 : if (tree_fits_shwi_p (t2))
16381 398 : t = build2 (MEM_REF, type, t,
16382 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16383 : else
16384 : {
16385 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16386 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16387 139 : TREE_TYPE (t), t, t2);
16388 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16389 : }
16390 537 : OMP_CLAUSE_DECL (c) = t;
16391 537 : return false;
16392 : }
16393 1875 : first = c_fully_fold (first, false, NULL);
16394 1875 : OMP_CLAUSE_DECL (c) = first;
16395 1875 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16396 : return false;
16397 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16398 1864 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16399 1864 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16400 1723 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16401 1721 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16402 : {
16403 1860 : if (size)
16404 1860 : size = c_fully_fold (size, false, NULL);
16405 1860 : OMP_CLAUSE_SIZE (c) = size;
16406 : }
16407 :
16408 1864 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16409 : return false;
16410 :
16411 1725 : auto_vec<omp_addr_token *, 10> addr_tokens;
16412 :
16413 1725 : if (!omp_parse_expr (addr_tokens, first))
16414 1725 : return true;
16415 :
16416 1725 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16417 :
16418 1725 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16419 1725 : if (nc != error_mark_node)
16420 : {
16421 1725 : using namespace omp_addr_tokenizer;
16422 :
16423 1725 : if (ai.maybe_zero_length_array_section (c))
16424 1721 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16425 :
16426 : /* !!! If we're accessing a base decl via chained access
16427 : methods (e.g. multiple indirections), duplicate clause
16428 : detection won't work properly. Skip it in that case. */
16429 1725 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16430 1437 : || addr_tokens[0]->type == ARRAY_BASE)
16431 1725 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16432 1725 : && addr_tokens[1]->type == ACCESS_METHOD
16433 3450 : && omp_access_chain_p (addr_tokens, 1))
16434 34 : c = nc;
16435 :
16436 1725 : return false;
16437 : }
16438 : }
16439 : return false;
16440 2986 : }
16441 :
16442 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16443 : an inline call. But, remap
16444 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16445 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16446 :
16447 : static tree
16448 516 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16449 : tree decl, tree placeholder)
16450 : {
16451 516 : copy_body_data id;
16452 516 : hash_map<tree, tree> decl_map;
16453 :
16454 516 : decl_map.put (omp_decl1, placeholder);
16455 516 : decl_map.put (omp_decl2, decl);
16456 516 : memset (&id, 0, sizeof (id));
16457 516 : id.src_fn = DECL_CONTEXT (omp_decl1);
16458 516 : id.dst_fn = current_function_decl;
16459 516 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16460 516 : id.decl_map = &decl_map;
16461 :
16462 516 : id.copy_decl = copy_decl_no_change;
16463 516 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16464 516 : id.transform_new_cfg = true;
16465 516 : id.transform_return_to_modify = false;
16466 516 : id.eh_lp_nr = 0;
16467 516 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16468 516 : return stmt;
16469 516 : }
16470 :
16471 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16472 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16473 :
16474 : static tree
16475 1147 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16476 : {
16477 1147 : if (*tp == (tree) data)
16478 31 : return *tp;
16479 : return NULL_TREE;
16480 : }
16481 :
16482 : /* Similarly, but also walk aggregate fields. */
16483 :
16484 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16485 :
16486 : static tree
16487 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16488 : {
16489 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16490 : return *tp;
16491 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16492 : {
16493 2 : tree field;
16494 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16495 :
16496 2 : for (field = TYPE_FIELDS (*tp); field;
16497 0 : field = DECL_CHAIN (field))
16498 2 : if (TREE_CODE (field) == FIELD_DECL)
16499 : {
16500 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16501 : c_find_omp_var_r, data, pset);
16502 2 : if (ret)
16503 : return ret;
16504 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16505 2 : if (ret)
16506 : return ret;
16507 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16508 : pset);
16509 2 : if (ret)
16510 : return ret;
16511 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16512 2 : if (ret)
16513 : return ret;
16514 : }
16515 : }
16516 278 : else if (INTEGRAL_TYPE_P (*tp))
16517 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16518 : ((struct c_find_omp_var_s *) data)->pset);
16519 : return NULL_TREE;
16520 : }
16521 :
16522 : /* Finish OpenMP iterators ITER. Return true if they are erroneous
16523 : and clauses containing them should be removed. */
16524 :
16525 : static bool
16526 143 : c_omp_finish_iterators (tree iter)
16527 : {
16528 143 : bool ret = false;
16529 325 : for (tree it = iter; it; it = TREE_CHAIN (it))
16530 : {
16531 182 : tree var = OMP_ITERATOR_VAR (it);
16532 182 : tree begin = OMP_ITERATOR_BEGIN (it);
16533 182 : tree end = OMP_ITERATOR_END (it);
16534 182 : tree step = OMP_ITERATOR_STEP (it);
16535 182 : tree orig_step;
16536 182 : tree type = TREE_TYPE (var);
16537 182 : location_t loc = DECL_SOURCE_LOCATION (var);
16538 182 : if (type == error_mark_node)
16539 : {
16540 0 : ret = true;
16541 34 : continue;
16542 : }
16543 182 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16544 : {
16545 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16546 : var);
16547 6 : ret = true;
16548 6 : continue;
16549 : }
16550 176 : else if (TYPE_ATOMIC (type))
16551 : {
16552 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16553 2 : ret = true;
16554 2 : continue;
16555 : }
16556 174 : else if (TYPE_READONLY (type))
16557 : {
16558 4 : error_at (loc, "iterator %qD has const qualified type", var);
16559 4 : ret = true;
16560 4 : continue;
16561 : }
16562 170 : else if (step == error_mark_node
16563 170 : || TREE_TYPE (step) == error_mark_node)
16564 : {
16565 0 : ret = true;
16566 0 : continue;
16567 : }
16568 170 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16569 : {
16570 4 : error_at (EXPR_LOC_OR_LOC (step, loc),
16571 : "iterator step with non-integral type");
16572 4 : ret = true;
16573 4 : continue;
16574 : }
16575 166 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16576 166 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16577 166 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16578 166 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16579 166 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16580 166 : if (POINTER_TYPE_P (type))
16581 : {
16582 14 : begin = save_expr (begin);
16583 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16584 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16585 : fold_convert (sizetype, step),
16586 : fold_convert (sizetype, begin));
16587 14 : step = fold_convert (ssizetype, step);
16588 : }
16589 166 : if (integer_zerop (step))
16590 : {
16591 6 : error_at (loc, "iterator %qD has zero step", var);
16592 6 : ret = true;
16593 6 : continue;
16594 : }
16595 :
16596 160 : if (begin == error_mark_node
16597 158 : || end == error_mark_node
16598 156 : || step == error_mark_node
16599 156 : || orig_step == error_mark_node)
16600 : {
16601 4 : ret = true;
16602 4 : continue;
16603 : }
16604 156 : hash_set<tree> pset;
16605 156 : tree it2;
16606 197 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16607 : {
16608 49 : tree var2 = OMP_ITERATOR_VAR (it2);
16609 49 : tree begin2 = OMP_ITERATOR_BEGIN (it2);
16610 49 : tree end2 = OMP_ITERATOR_END (it2);
16611 49 : tree step2 = OMP_ITERATOR_STEP (it2);
16612 49 : tree type2 = TREE_TYPE (var2);
16613 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16614 49 : struct c_find_omp_var_s data = { var, &pset };
16615 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16616 : {
16617 2 : error_at (loc2,
16618 : "type of iterator %qD refers to outer iterator %qD",
16619 : var2, var);
16620 10 : break;
16621 : }
16622 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16623 : {
16624 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16625 : "begin expression refers to outer iterator %qD", var);
16626 2 : break;
16627 : }
16628 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16629 : {
16630 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16631 : "end expression refers to outer iterator %qD", var);
16632 2 : break;
16633 : }
16634 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16635 : {
16636 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16637 : "step expression refers to outer iterator %qD", var);
16638 2 : break;
16639 : }
16640 : }
16641 156 : if (it2)
16642 : {
16643 8 : ret = true;
16644 8 : continue;
16645 : }
16646 148 : OMP_ITERATOR_BEGIN (it) = begin;
16647 148 : OMP_ITERATOR_END (it) = end;
16648 148 : OMP_ITERATOR_STEP (it) = step;
16649 148 : OMP_ITERATOR_ORIG_STEP (it) = orig_step;
16650 156 : }
16651 143 : return ret;
16652 : }
16653 :
16654 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16655 : Return true if an error has been detected. */
16656 :
16657 : static bool
16658 9632 : c_oacc_check_attachments (tree c)
16659 : {
16660 9632 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16661 : return false;
16662 :
16663 : /* OpenACC attach / detach clauses must be pointers. */
16664 6607 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16665 6607 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16666 : {
16667 71 : tree t = OMP_CLAUSE_DECL (c);
16668 :
16669 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16670 6 : t = TREE_OPERAND (t, 0);
16671 :
16672 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16673 : {
16674 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16675 : user_omp_clause_code_name (c, true));
16676 6 : return true;
16677 : }
16678 : }
16679 :
16680 : return false;
16681 : }
16682 :
16683 : /* For all elements of CLAUSES, validate them against their constraints.
16684 : Remove any elements from the list that are invalid. */
16685 :
16686 : tree
16687 29657 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16688 : {
16689 29657 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16690 29657 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16691 29657 : bitmap_head oacc_reduction_head, is_on_device_head;
16692 29657 : tree c, t, type, *pc;
16693 29657 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16694 29657 : bool branch_seen = false;
16695 29657 : bool copyprivate_seen = false;
16696 29657 : bool mergeable_seen = false;
16697 29657 : tree *detach_seen = NULL;
16698 29657 : bool linear_variable_step_check = false;
16699 29657 : tree *nowait_clause = NULL;
16700 29657 : tree *grainsize_seen = NULL;
16701 29657 : bool num_tasks_seen = false;
16702 29657 : tree ordered_clause = NULL_TREE;
16703 29657 : tree schedule_clause = NULL_TREE;
16704 29657 : bool oacc_async = false;
16705 29657 : tree last_iterators = NULL_TREE;
16706 29657 : bool last_iterators_remove = false;
16707 29657 : tree *nogroup_seen = NULL;
16708 29657 : tree *order_clause = NULL;
16709 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16710 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16711 29657 : int reduction_seen = 0;
16712 29657 : bool allocate_seen = false;
16713 29657 : bool implicit_moved = false;
16714 29657 : bool target_in_reduction_seen = false;
16715 29657 : tree *full_seen = NULL;
16716 29657 : bool partial_seen = false;
16717 29657 : bool openacc = (ort & C_ORT_ACC) != 0;
16718 29657 : bool init_seen = false;
16719 29657 : bool init_use_destroy_seen = false;
16720 29657 : tree init_no_targetsync_clause = NULL_TREE;
16721 29657 : tree depend_clause = NULL_TREE;
16722 :
16723 29657 : if (!openacc)
16724 24420 : clauses = omp_remove_duplicate_maps (clauses, true);
16725 :
16726 29657 : bitmap_obstack_initialize (NULL);
16727 29657 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16728 29657 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16729 29657 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16730 29657 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16731 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16732 29657 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16733 29657 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16734 29657 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16735 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16736 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16737 29657 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16738 29657 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16739 :
16740 29657 : if (openacc)
16741 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16742 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16743 : {
16744 : oacc_async = true;
16745 : break;
16746 : }
16747 :
16748 29657 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16749 :
16750 77833 : for (pc = &clauses, c = clauses; c ; c = *pc)
16751 : {
16752 48176 : bool remove = false;
16753 48176 : bool need_complete = false;
16754 48176 : bool need_implicitly_determined = false;
16755 :
16756 : /* We've reached the end of a list of expanded nodes. Reset the group
16757 : start pointer. */
16758 48176 : if (c == grp_sentinel)
16759 : {
16760 2517 : if (grp_start_p
16761 2517 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16762 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16763 22 : gc = OMP_CLAUSE_CHAIN (gc))
16764 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16765 : grp_start_p = NULL;
16766 : }
16767 :
16768 48176 : switch (OMP_CLAUSE_CODE (c))
16769 : {
16770 1139 : case OMP_CLAUSE_SHARED:
16771 1139 : need_implicitly_determined = true;
16772 1139 : goto check_dup_generic;
16773 :
16774 851 : case OMP_CLAUSE_PRIVATE:
16775 851 : need_complete = true;
16776 851 : need_implicitly_determined = true;
16777 851 : goto check_dup_generic;
16778 :
16779 3539 : case OMP_CLAUSE_REDUCTION:
16780 3539 : if (reduction_seen == 0)
16781 2935 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16782 604 : else if (reduction_seen != -2
16783 1208 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16784 604 : ? -1 : 1))
16785 : {
16786 2 : error_at (OMP_CLAUSE_LOCATION (c),
16787 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16788 : "on the same construct");
16789 2 : reduction_seen = -2;
16790 : }
16791 : /* FALLTHRU */
16792 4203 : case OMP_CLAUSE_IN_REDUCTION:
16793 4203 : case OMP_CLAUSE_TASK_REDUCTION:
16794 4203 : need_implicitly_determined = true;
16795 4203 : t = OMP_CLAUSE_DECL (c);
16796 4203 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16797 : {
16798 553 : if (handle_omp_array_sections (c, ort))
16799 : {
16800 : remove = true;
16801 : break;
16802 : }
16803 :
16804 537 : t = OMP_CLAUSE_DECL (c);
16805 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16806 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16807 : {
16808 1 : error_at (OMP_CLAUSE_LOCATION (c),
16809 : "%<inscan%> %<reduction%> clause with array "
16810 : "section");
16811 1 : remove = true;
16812 1 : break;
16813 : }
16814 : }
16815 4186 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16816 4186 : if (t == error_mark_node)
16817 : {
16818 : remove = true;
16819 : break;
16820 : }
16821 4184 : if (oacc_async)
16822 3 : c_mark_addressable (t);
16823 4184 : type = TREE_TYPE (t);
16824 4184 : if (TREE_CODE (t) == MEM_REF)
16825 536 : type = TREE_TYPE (type);
16826 4184 : if (TREE_CODE (type) == ARRAY_TYPE)
16827 : {
16828 74 : tree oatype = type;
16829 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16830 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16831 74 : type = TREE_TYPE (type);
16832 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16833 : {
16834 1 : error_at (OMP_CLAUSE_LOCATION (c),
16835 : "%qD in %<reduction%> clause is a zero size array",
16836 : t);
16837 1 : remove = true;
16838 1 : break;
16839 : }
16840 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16841 : TYPE_SIZE_UNIT (type));
16842 73 : if (integer_zerop (size))
16843 : {
16844 1 : error_at (OMP_CLAUSE_LOCATION (c),
16845 : "%qD in %<reduction%> clause is a zero size array",
16846 : t);
16847 1 : remove = true;
16848 1 : break;
16849 : }
16850 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16851 72 : size = save_expr (size);
16852 72 : tree index_type = build_index_type (size);
16853 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16854 : index_type);
16855 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16856 72 : tree ptype = c_build_pointer_type (type);
16857 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16858 72 : t = build_fold_addr_expr (t);
16859 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16860 72 : OMP_CLAUSE_DECL (c) = t;
16861 : }
16862 4182 : if (TYPE_ATOMIC (type))
16863 : {
16864 3 : error_at (OMP_CLAUSE_LOCATION (c),
16865 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16866 3 : remove = true;
16867 3 : break;
16868 : }
16869 4179 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16870 4179 : || OMP_CLAUSE_REDUCTION_TASK (c))
16871 : {
16872 : /* Disallow zero sized or potentially zero sized task
16873 : reductions. */
16874 873 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16875 : {
16876 6 : error_at (OMP_CLAUSE_LOCATION (c),
16877 : "zero sized type %qT in %qs clause", type,
16878 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16879 3 : remove = true;
16880 3 : break;
16881 : }
16882 870 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16883 : {
16884 0 : error_at (OMP_CLAUSE_LOCATION (c),
16885 : "variable sized type %qT in %qs clause", type,
16886 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16887 0 : remove = true;
16888 0 : break;
16889 : }
16890 : }
16891 4176 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16892 4176 : && (FLOAT_TYPE_P (type)
16893 3621 : || TREE_CODE (type) == COMPLEX_TYPE))
16894 : {
16895 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16896 340 : const char *r_name = NULL;
16897 :
16898 340 : switch (r_code)
16899 : {
16900 : case PLUS_EXPR:
16901 : case MULT_EXPR:
16902 : case MINUS_EXPR:
16903 : case TRUTH_ANDIF_EXPR:
16904 : case TRUTH_ORIF_EXPR:
16905 : break;
16906 13 : case MIN_EXPR:
16907 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16908 : r_name = "min";
16909 : break;
16910 38 : case MAX_EXPR:
16911 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16912 : r_name = "max";
16913 : break;
16914 3 : case BIT_AND_EXPR:
16915 3 : r_name = "&";
16916 3 : break;
16917 2 : case BIT_XOR_EXPR:
16918 2 : r_name = "^";
16919 2 : break;
16920 : case BIT_IOR_EXPR:
16921 : r_name = "|";
16922 : break;
16923 0 : default:
16924 0 : gcc_unreachable ();
16925 : }
16926 5 : if (r_name)
16927 : {
16928 12 : error_at (OMP_CLAUSE_LOCATION (c),
16929 : "%qE has invalid type for %<reduction(%s)%>",
16930 : t, r_name);
16931 12 : remove = true;
16932 12 : break;
16933 : }
16934 : }
16935 3836 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16936 : {
16937 2 : error_at (OMP_CLAUSE_LOCATION (c),
16938 : "user defined reduction not found for %qE", t);
16939 2 : remove = true;
16940 2 : break;
16941 : }
16942 3834 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16943 : {
16944 280 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16945 280 : type = TYPE_MAIN_VARIANT (type);
16946 280 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16947 : VAR_DECL, NULL_TREE, type);
16948 280 : tree decl_placeholder = NULL_TREE;
16949 280 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16950 280 : DECL_ARTIFICIAL (placeholder) = 1;
16951 280 : DECL_IGNORED_P (placeholder) = 1;
16952 280 : if (TREE_CODE (t) == MEM_REF)
16953 : {
16954 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16955 : VAR_DECL, NULL_TREE, type);
16956 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16957 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16958 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16959 : }
16960 280 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16961 45 : c_mark_addressable (placeholder);
16962 280 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16963 82 : c_mark_addressable (decl_placeholder ? decl_placeholder
16964 37 : : OMP_CLAUSE_DECL (c));
16965 280 : OMP_CLAUSE_REDUCTION_MERGE (c)
16966 784 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16967 280 : TREE_VEC_ELT (list, 0),
16968 280 : TREE_VEC_ELT (list, 1),
16969 : decl_placeholder ? decl_placeholder
16970 224 : : OMP_CLAUSE_DECL (c), placeholder);
16971 280 : OMP_CLAUSE_REDUCTION_MERGE (c)
16972 280 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16973 : void_type_node, NULL_TREE,
16974 280 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16975 280 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16976 280 : if (TREE_VEC_LENGTH (list) == 6)
16977 : {
16978 236 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16979 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16980 33 : : OMP_CLAUSE_DECL (c));
16981 236 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16982 30 : c_mark_addressable (placeholder);
16983 236 : tree init = TREE_VEC_ELT (list, 5);
16984 236 : if (init == error_mark_node)
16985 200 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16986 236 : OMP_CLAUSE_REDUCTION_INIT (c)
16987 660 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16988 236 : TREE_VEC_ELT (list, 3),
16989 : decl_placeholder ? decl_placeholder
16990 188 : : OMP_CLAUSE_DECL (c), placeholder);
16991 236 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16992 : {
16993 200 : tree v = decl_placeholder ? decl_placeholder : t;
16994 200 : OMP_CLAUSE_REDUCTION_INIT (c)
16995 400 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16996 200 : OMP_CLAUSE_REDUCTION_INIT (c));
16997 : }
16998 236 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16999 : c_find_omp_placeholder_r,
17000 : placeholder, NULL))
17001 31 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
17002 : }
17003 : else
17004 : {
17005 44 : tree init;
17006 44 : tree v = decl_placeholder ? decl_placeholder : t;
17007 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
17008 34 : init = build_constructor (TREE_TYPE (v), NULL);
17009 : else
17010 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
17011 44 : OMP_CLAUSE_REDUCTION_INIT (c)
17012 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
17013 : }
17014 280 : OMP_CLAUSE_REDUCTION_INIT (c)
17015 280 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
17016 : void_type_node, NULL_TREE,
17017 280 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
17018 280 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
17019 : }
17020 4162 : if (TREE_CODE (t) == MEM_REF)
17021 : {
17022 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
17023 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
17024 : != INTEGER_CST)
17025 : {
17026 0 : sorry ("variable length element type in array "
17027 : "%<reduction%> clause");
17028 0 : remove = true;
17029 0 : break;
17030 : }
17031 607 : t = TREE_OPERAND (t, 0);
17032 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
17033 139 : t = TREE_OPERAND (t, 0);
17034 607 : if (TREE_CODE (t) == ADDR_EXPR)
17035 367 : t = TREE_OPERAND (t, 0);
17036 : }
17037 4162 : goto check_dup_generic_t;
17038 :
17039 24 : case OMP_CLAUSE_COPYPRIVATE:
17040 24 : copyprivate_seen = true;
17041 24 : if (nowait_clause)
17042 : {
17043 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
17044 : "%<nowait%> clause must not be used together "
17045 : "with %<copyprivate%> clause");
17046 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
17047 1 : nowait_clause = NULL;
17048 : }
17049 24 : goto check_dup_generic;
17050 :
17051 100 : case OMP_CLAUSE_COPYIN:
17052 100 : t = OMP_CLAUSE_DECL (c);
17053 100 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17054 : {
17055 5 : error_at (OMP_CLAUSE_LOCATION (c),
17056 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17057 5 : remove = true;
17058 5 : break;
17059 : }
17060 95 : goto check_dup_generic;
17061 :
17062 480 : case OMP_CLAUSE_LINEAR:
17063 480 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17064 327 : need_implicitly_determined = true;
17065 480 : t = OMP_CLAUSE_DECL (c);
17066 480 : if (ort != C_ORT_OMP_DECLARE_SIMD
17067 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17068 485 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17069 : {
17070 5 : error_at (OMP_CLAUSE_LOCATION (c),
17071 : "modifier should not be specified in %<linear%> "
17072 : "clause on %<simd%> or %<for%> constructs when not "
17073 : "using OpenMP 5.2 modifiers");
17074 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17075 : }
17076 960 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17077 503 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17078 : {
17079 1 : error_at (OMP_CLAUSE_LOCATION (c),
17080 : "linear clause applied to non-integral non-pointer "
17081 1 : "variable with type %qT", TREE_TYPE (t));
17082 1 : remove = true;
17083 1 : break;
17084 : }
17085 479 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17086 : {
17087 3 : error_at (OMP_CLAUSE_LOCATION (c),
17088 : "%<_Atomic%> %qD in %<linear%> clause", t);
17089 3 : remove = true;
17090 3 : break;
17091 : }
17092 476 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17093 : {
17094 152 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17095 152 : if (TREE_CODE (s) == PARM_DECL)
17096 : {
17097 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17098 : /* map_head bitmap is used as uniform_head if
17099 : declare_simd. */
17100 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17101 5 : linear_variable_step_check = true;
17102 9 : goto check_dup_generic;
17103 : }
17104 143 : if (TREE_CODE (s) != INTEGER_CST)
17105 : {
17106 7 : error_at (OMP_CLAUSE_LOCATION (c),
17107 : "%<linear%> clause step %qE is neither constant "
17108 : "nor a parameter", s);
17109 7 : remove = true;
17110 7 : break;
17111 : }
17112 : }
17113 460 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17114 : {
17115 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17116 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17117 20 : OMP_CLAUSE_DECL (c), s);
17118 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17119 : sizetype, fold_convert (sizetype, s),
17120 20 : fold_convert
17121 : (sizetype, OMP_CLAUSE_DECL (c)));
17122 20 : if (s == error_mark_node)
17123 0 : s = size_one_node;
17124 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17125 : }
17126 : else
17127 440 : OMP_CLAUSE_LINEAR_STEP (c)
17128 880 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17129 460 : goto check_dup_generic;
17130 :
17131 2892 : check_dup_generic:
17132 2892 : t = OMP_CLAUSE_DECL (c);
17133 7143 : check_dup_generic_t:
17134 7143 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17135 : {
17136 4 : error_at (OMP_CLAUSE_LOCATION (c),
17137 : "%qE is not a variable in clause %qs", t,
17138 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17139 2 : remove = true;
17140 : }
17141 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17142 6102 : || (ort == C_ORT_OMP
17143 5381 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17144 5370 : || (OMP_CLAUSE_CODE (c)
17145 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17146 13196 : || (ort == C_ORT_OMP_TARGET
17147 349 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17148 : {
17149 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17150 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17151 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17152 : {
17153 2 : error_at (OMP_CLAUSE_LOCATION (c),
17154 : "%qD appears more than once in data-sharing "
17155 : "clauses", t);
17156 2 : remove = true;
17157 2 : break;
17158 : }
17159 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17160 108 : target_in_reduction_seen = true;
17161 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17162 : {
17163 4 : error_at (OMP_CLAUSE_LOCATION (c),
17164 : openacc
17165 : ? "%qD appears more than once in reduction clauses"
17166 : : "%qD appears more than once in data clauses",
17167 : t);
17168 2 : remove = true;
17169 : }
17170 : else
17171 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17172 : }
17173 5945 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17174 5922 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17175 5921 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17176 11866 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17177 : {
17178 24 : error_at (OMP_CLAUSE_LOCATION (c),
17179 : "%qE appears more than once in data clauses", t);
17180 24 : remove = true;
17181 : }
17182 5921 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17183 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17184 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17185 1051 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17186 : {
17187 3 : if (openacc)
17188 0 : error_at (OMP_CLAUSE_LOCATION (c),
17189 : "%qD appears more than once in data clauses", t);
17190 : else
17191 3 : error_at (OMP_CLAUSE_LOCATION (c),
17192 : "%qD appears both in data and map clauses", t);
17193 : remove = true;
17194 : }
17195 : else
17196 5918 : bitmap_set_bit (&generic_head, DECL_UID (t));
17197 : break;
17198 :
17199 1342 : case OMP_CLAUSE_FIRSTPRIVATE:
17200 1342 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17201 : {
17202 153 : move_implicit:
17203 153 : implicit_moved = true;
17204 : /* Move firstprivate and map clauses with
17205 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17206 : clauses chain. */
17207 153 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17208 153 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17209 972 : while (*pc1)
17210 819 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17211 819 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17212 : {
17213 113 : *pc3 = *pc1;
17214 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17215 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17216 : }
17217 706 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17218 706 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17219 : {
17220 131 : *pc2 = *pc1;
17221 131 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17222 131 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17223 : }
17224 : else
17225 575 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17226 153 : *pc3 = NULL;
17227 153 : *pc2 = cl2;
17228 153 : *pc1 = cl1;
17229 153 : continue;
17230 153 : }
17231 1247 : t = OMP_CLAUSE_DECL (c);
17232 1247 : need_complete = true;
17233 1247 : need_implicitly_determined = true;
17234 1247 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17235 : {
17236 1 : error_at (OMP_CLAUSE_LOCATION (c),
17237 : "%qE is not a variable in clause %<firstprivate%>", t);
17238 1 : remove = true;
17239 : }
17240 1246 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17241 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17242 1352 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17243 : remove = true;
17244 1246 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17245 1244 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17246 2489 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17247 : {
17248 4 : error_at (OMP_CLAUSE_LOCATION (c),
17249 : "%qE appears more than once in data clauses", t);
17250 4 : remove = true;
17251 : }
17252 1242 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17253 1242 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17254 : {
17255 7 : if (openacc)
17256 0 : error_at (OMP_CLAUSE_LOCATION (c),
17257 : "%qD appears more than once in data clauses", t);
17258 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17259 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17260 : /* Silently drop the clause. */;
17261 : else
17262 6 : error_at (OMP_CLAUSE_LOCATION (c),
17263 : "%qD appears both in data and map clauses", t);
17264 : remove = true;
17265 : }
17266 : else
17267 1235 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17268 : break;
17269 :
17270 1524 : case OMP_CLAUSE_LASTPRIVATE:
17271 1524 : t = OMP_CLAUSE_DECL (c);
17272 1524 : need_complete = true;
17273 1524 : need_implicitly_determined = true;
17274 1524 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17275 : {
17276 0 : error_at (OMP_CLAUSE_LOCATION (c),
17277 : "%qE is not a variable in clause %<lastprivate%>", t);
17278 0 : remove = true;
17279 : }
17280 1524 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17281 1524 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17282 : {
17283 3 : error_at (OMP_CLAUSE_LOCATION (c),
17284 : "%qE appears more than once in data clauses", t);
17285 3 : remove = true;
17286 : }
17287 : else
17288 1521 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17289 : break;
17290 :
17291 253 : case OMP_CLAUSE_ALIGNED:
17292 253 : t = OMP_CLAUSE_DECL (c);
17293 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17294 : {
17295 0 : error_at (OMP_CLAUSE_LOCATION (c),
17296 : "%qE is not a variable in %<aligned%> clause", t);
17297 0 : remove = true;
17298 : }
17299 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17300 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17301 : {
17302 7 : error_at (OMP_CLAUSE_LOCATION (c),
17303 : "%qE in %<aligned%> clause is neither a pointer nor "
17304 : "an array", t);
17305 7 : remove = true;
17306 : }
17307 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17308 : {
17309 1 : error_at (OMP_CLAUSE_LOCATION (c),
17310 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17311 1 : remove = true;
17312 1 : break;
17313 : }
17314 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17315 : {
17316 0 : error_at (OMP_CLAUSE_LOCATION (c),
17317 : "%qE appears more than once in %<aligned%> clauses",
17318 : t);
17319 0 : remove = true;
17320 : }
17321 : else
17322 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17323 : break;
17324 :
17325 125 : case OMP_CLAUSE_NONTEMPORAL:
17326 125 : t = OMP_CLAUSE_DECL (c);
17327 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17328 : {
17329 0 : error_at (OMP_CLAUSE_LOCATION (c),
17330 : "%qE is not a variable in %<nontemporal%> clause", t);
17331 0 : remove = true;
17332 : }
17333 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17334 : {
17335 2 : error_at (OMP_CLAUSE_LOCATION (c),
17336 : "%qE appears more than once in %<nontemporal%> "
17337 : "clauses", t);
17338 2 : remove = true;
17339 : }
17340 : else
17341 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17342 : break;
17343 :
17344 814 : case OMP_CLAUSE_ALLOCATE:
17345 814 : t = OMP_CLAUSE_DECL (c);
17346 814 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17347 : {
17348 1 : error_at (OMP_CLAUSE_LOCATION (c),
17349 : "%qE is not a variable in %<allocate%> clause", t);
17350 1 : remove = true;
17351 : }
17352 813 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17353 : {
17354 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17355 : "%qE appears more than once in %<allocate%> clauses",
17356 : t);
17357 2 : remove = true;
17358 : }
17359 : else
17360 : {
17361 811 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17362 811 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17363 711 : allocate_seen = true;
17364 : }
17365 : break;
17366 :
17367 357 : case OMP_CLAUSE_DOACROSS:
17368 357 : t = OMP_CLAUSE_DECL (c);
17369 357 : if (t == NULL_TREE)
17370 : break;
17371 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17372 : {
17373 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17374 1315 : for (; t; t = TREE_CHAIN (t))
17375 : {
17376 1146 : tree decl = TREE_VALUE (t);
17377 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17378 : {
17379 3 : tree offset = TREE_PURPOSE (t);
17380 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17381 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17382 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17383 : neg ? MINUS_EXPR : PLUS_EXPR,
17384 : decl, offset);
17385 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17386 : sizetype,
17387 : fold_convert (sizetype, t2),
17388 : fold_convert (sizetype, decl));
17389 3 : if (t2 == error_mark_node)
17390 : {
17391 : remove = true;
17392 : break;
17393 : }
17394 3 : TREE_PURPOSE (t) = t2;
17395 : }
17396 : }
17397 : break;
17398 : }
17399 0 : gcc_unreachable ();
17400 :
17401 64 : case OMP_CLAUSE_USES_ALLOCATORS:
17402 64 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17403 64 : if (t == error_mark_node)
17404 : {
17405 : remove = true;
17406 : break;
17407 : }
17408 8 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17409 60 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17410 52 : || bitmap_bit_p (&map_head, DECL_UID (t))
17411 52 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17412 51 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17413 : {
17414 1 : error_at (OMP_CLAUSE_LOCATION (c),
17415 : "%qE appears more than once in data clauses", t);
17416 1 : remove = true;
17417 1 : break;
17418 : }
17419 : else
17420 58 : bitmap_set_bit (&generic_head, DECL_UID (t));
17421 58 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17422 113 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17423 : "omp_allocator_handle_t") != 0)
17424 : {
17425 3 : error_at (OMP_CLAUSE_LOCATION (c),
17426 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17427 : "type", t);
17428 3 : remove = true;
17429 3 : break;
17430 : }
17431 55 : tree init;
17432 55 : if (!DECL_P (t)
17433 55 : || (TREE_CODE (t) == CONST_DECL
17434 7 : && ((init = DECL_INITIAL(t)) == nullptr
17435 7 : || TREE_CODE (init) != INTEGER_CST
17436 7 : || ((wi::to_widest (init) < 0
17437 7 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17438 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17439 0 : || (wi::to_widest (init)
17440 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17441 : {
17442 0 : remove = true;
17443 0 : error_at (OMP_CLAUSE_LOCATION (c),
17444 : "allocator %qE must be either a variable or a "
17445 : "predefined allocator", t);
17446 0 : break;
17447 : }
17448 55 : else if (TREE_CODE (t) == CONST_DECL)
17449 : {
17450 : /* omp_null_allocator is ignored and for predefined allocators,
17451 : no special handling is required; thus, mark them removed. */
17452 7 : remove = true;
17453 :
17454 7 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17455 7 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17456 : {
17457 0 : error_at (OMP_CLAUSE_LOCATION (c),
17458 : "modifiers cannot be used with predefined "
17459 : "allocator %qE", t);
17460 0 : break;
17461 : }
17462 : }
17463 55 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17464 55 : if (t == error_mark_node)
17465 : {
17466 : remove = true;
17467 : break;
17468 : }
17469 55 : if (t != NULL_TREE
17470 55 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17471 11 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17472 20 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17473 : "omp_memspace_handle_t") != 0))
17474 : {
17475 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17476 : " constant enum of %<omp_memspace_handle_t%> type", t);
17477 1 : remove = true;
17478 1 : break;
17479 : }
17480 54 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17481 54 : if (t == error_mark_node)
17482 : {
17483 : remove = true;
17484 : break;
17485 : }
17486 54 : if (t != NULL_TREE
17487 : && t != error_mark_node
17488 54 : && (DECL_EXTERNAL (t)
17489 25 : || TREE_CODE (t) == PARM_DECL))
17490 : {
17491 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17492 : "defined in same scope as the construct on which the "
17493 : "clause appears", t);
17494 3 : remove = true;
17495 : }
17496 54 : if (t != NULL_TREE)
17497 : {
17498 27 : bool type_err = false;
17499 :
17500 27 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17501 26 : || DECL_SIZE (t) == NULL_TREE
17502 52 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17503 : type_err = true;
17504 : else
17505 : {
17506 25 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17507 25 : if (TREE_CODE (elem_t) != RECORD_TYPE
17508 50 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17509 : "omp_alloctrait_t") != 0
17510 50 : || !TYPE_READONLY (elem_t))
17511 : type_err = true;
17512 : }
17513 24 : if (type_err)
17514 : {
17515 3 : if (t != error_mark_node)
17516 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17517 : "be of %<const omp_alloctrait_t []%> type", t);
17518 : else
17519 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17520 : "be of %<const omp_alloctrait_t []%> type");
17521 : remove = true;
17522 : }
17523 : else
17524 : {
17525 24 : tree cst_val = decl_constant_value_1 (t, true);
17526 24 : if (cst_val == t)
17527 : {
17528 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17529 : "initialized with constants");
17530 :
17531 1 : remove = true;
17532 : }
17533 : }
17534 : }
17535 54 : if (remove)
17536 : break;
17537 43 : pc = &OMP_CLAUSE_CHAIN (c);
17538 43 : continue;
17539 713 : case OMP_CLAUSE_DEPEND:
17540 713 : depend_clause = c;
17541 : /* FALLTHRU */
17542 875 : case OMP_CLAUSE_AFFINITY:
17543 875 : t = OMP_CLAUSE_DECL (c);
17544 875 : if (OMP_ITERATOR_DECL_P (t))
17545 : {
17546 131 : if (TREE_PURPOSE (t) != last_iterators)
17547 111 : last_iterators_remove
17548 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17549 131 : last_iterators = TREE_PURPOSE (t);
17550 131 : t = TREE_VALUE (t);
17551 131 : if (last_iterators_remove)
17552 34 : t = error_mark_node;
17553 : }
17554 : else
17555 : last_iterators = NULL_TREE;
17556 875 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17557 : {
17558 418 : if (handle_omp_array_sections (c, ort))
17559 : remove = true;
17560 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17561 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17562 : {
17563 1 : error_at (OMP_CLAUSE_LOCATION (c),
17564 : "%<depend%> clause with %<depobj%> dependence "
17565 : "type on array section");
17566 1 : remove = true;
17567 : }
17568 : break;
17569 : }
17570 457 : if (t == error_mark_node)
17571 : remove = true;
17572 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17573 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17574 : {
17575 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17576 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17577 : {
17578 3 : error_at (OMP_CLAUSE_LOCATION (c),
17579 : "%<omp_all_memory%> used with %<depend%> kind "
17580 : "other than %<out%> or %<inout%>");
17581 3 : remove = true;
17582 : }
17583 : }
17584 408 : else if (!lvalue_p (t))
17585 : {
17586 6 : error_at (OMP_CLAUSE_LOCATION (c),
17587 : "%qE is not lvalue expression nor array section in "
17588 : "%qs clause", t,
17589 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17590 3 : remove = true;
17591 : }
17592 405 : else if (TREE_CODE (t) == COMPONENT_REF
17593 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17594 : {
17595 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17596 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17597 2 : error_at (OMP_CLAUSE_LOCATION (c),
17598 : "bit-field %qE in %qs clause", t,
17599 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17600 2 : remove = true;
17601 : }
17602 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17603 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17604 : {
17605 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17606 : {
17607 2 : error_at (OMP_CLAUSE_LOCATION (c),
17608 : "%qE does not have %<omp_depend_t%> type in "
17609 : "%<depend%> clause with %<depobj%> dependence "
17610 : "type", t);
17611 2 : remove = true;
17612 : }
17613 : }
17614 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17615 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17616 : {
17617 2 : error_at (OMP_CLAUSE_LOCATION (c),
17618 : "%qE should not have %<omp_depend_t%> type in "
17619 : "%<depend%> clause with dependence type other than "
17620 : "%<depobj%>", t);
17621 2 : remove = true;
17622 : }
17623 12 : if (!remove)
17624 : {
17625 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17626 12 : t = null_pointer_node;
17627 : else
17628 : {
17629 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17630 : ADDR_EXPR, t, false);
17631 399 : if (addr == error_mark_node)
17632 : {
17633 : remove = true;
17634 : break;
17635 : }
17636 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17637 : RO_UNARY_STAR);
17638 399 : if (t == error_mark_node)
17639 : {
17640 : remove = true;
17641 : break;
17642 : }
17643 : }
17644 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17645 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17646 : else
17647 380 : OMP_CLAUSE_DECL (c) = t;
17648 : }
17649 : break;
17650 :
17651 6713 : case OMP_CLAUSE_MAP:
17652 6713 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17653 58 : goto move_implicit;
17654 6655 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17655 6655 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17656 : {
17657 : remove = true;
17658 : break;
17659 : }
17660 : /* FALLTHRU */
17661 9526 : case OMP_CLAUSE_TO:
17662 9526 : case OMP_CLAUSE_FROM:
17663 9526 : if (OMP_CLAUSE_ITERATORS (c)
17664 9526 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17665 : {
17666 23015 : t = error_mark_node;
17667 : break;
17668 : }
17669 : /* FALLTHRU */
17670 9662 : case OMP_CLAUSE__CACHE_:
17671 9662 : {
17672 9662 : using namespace omp_addr_tokenizer;
17673 9662 : auto_vec<omp_addr_token *, 10> addr_tokens;
17674 :
17675 9662 : t = OMP_CLAUSE_DECL (c);
17676 9662 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17677 : {
17678 2004 : grp_start_p = pc;
17679 2004 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17680 :
17681 2004 : if (handle_omp_array_sections (c, ort))
17682 : remove = true;
17683 : else
17684 : {
17685 1864 : t = OMP_CLAUSE_DECL (c);
17686 1864 : if (!omp_mappable_type (TREE_TYPE (t)))
17687 : {
17688 2 : error_at (OMP_CLAUSE_LOCATION (c),
17689 : "array section does not have mappable type "
17690 : "in %qs clause",
17691 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17692 1 : remove = true;
17693 : }
17694 1863 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17695 : {
17696 2 : error_at (OMP_CLAUSE_LOCATION (c),
17697 : "%<_Atomic%> %qE in %qs clause", t,
17698 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17699 1 : remove = true;
17700 : }
17701 2513 : while (TREE_CODE (t) == ARRAY_REF)
17702 649 : t = TREE_OPERAND (t, 0);
17703 :
17704 1864 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17705 :
17706 1864 : if (!omp_parse_expr (addr_tokens, t))
17707 : {
17708 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17709 : "unsupported map expression %qE",
17710 0 : OMP_CLAUSE_DECL (c));
17711 0 : remove = true;
17712 0 : break;
17713 : }
17714 :
17715 : /* This check is to determine if this will be the only map
17716 : node created for this clause. Otherwise, we'll check
17717 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17718 : node on the next iteration(s) of the loop. */
17719 1864 : if (addr_tokens.length () >= 4
17720 306 : && addr_tokens[0]->type == STRUCTURE_BASE
17721 304 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17722 304 : && addr_tokens[1]->type == ACCESS_METHOD
17723 304 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17724 304 : && addr_tokens[3]->type == ACCESS_METHOD
17725 2107 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17726 190 : || (addr_tokens[3]->u.access_kind
17727 : == ACCESS_INDEXED_ARRAY)))
17728 : {
17729 55 : tree rt = addr_tokens[1]->expr;
17730 :
17731 55 : gcc_assert (DECL_P (rt));
17732 :
17733 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17734 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17735 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17736 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17737 0 : || bitmap_bit_p (&map_firstprivate_head,
17738 0 : DECL_UID (rt))))
17739 : {
17740 : remove = true;
17741 : break;
17742 : }
17743 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17744 : break;
17745 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17746 : {
17747 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17748 0 : error_at (OMP_CLAUSE_LOCATION (c),
17749 : "%qD appears more than once in motion "
17750 : "clauses", rt);
17751 0 : else if (openacc)
17752 0 : error_at (OMP_CLAUSE_LOCATION (c),
17753 : "%qD appears more than once in data "
17754 : "clauses", rt);
17755 : else
17756 0 : error_at (OMP_CLAUSE_LOCATION (c),
17757 : "%qD appears more than once in map "
17758 : "clauses", rt);
17759 : remove = true;
17760 : }
17761 : else
17762 : {
17763 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17764 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17765 : }
17766 : }
17767 : }
17768 1986 : if (c_oacc_check_attachments (c))
17769 2 : remove = true;
17770 1986 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17771 1809 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17772 1791 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17773 2011 : && !OMP_CLAUSE_SIZE (c))
17774 : /* In this case, we have a single array element which is a
17775 : pointer, and we already set OMP_CLAUSE_SIZE in
17776 : handle_omp_array_sections above. For attach/detach
17777 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17778 : to zero here. */
17779 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17780 : break;
17781 : }
17782 7658 : else if (!omp_parse_expr (addr_tokens, t))
17783 : {
17784 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17785 : "unsupported map expression %qE",
17786 0 : OMP_CLAUSE_DECL (c));
17787 0 : remove = true;
17788 0 : break;
17789 : }
17790 7658 : if (t == error_mark_node)
17791 : {
17792 : remove = true;
17793 : break;
17794 : }
17795 : /* OpenACC attach / detach clauses must be pointers. */
17796 7646 : if (c_oacc_check_attachments (c))
17797 : {
17798 : remove = true;
17799 : break;
17800 : }
17801 7642 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17802 4794 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17803 4767 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17804 7684 : && !OMP_CLAUSE_SIZE (c))
17805 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17806 : bias) to zero here, so it is not set erroneously to the pointer
17807 : size later on in gimplify.cc. */
17808 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17809 :
17810 7642 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17811 :
17812 7642 : if (!ai.check_clause (c))
17813 : {
17814 : remove = true;
17815 : break;
17816 : }
17817 :
17818 7634 : if (!ai.map_supported_p ())
17819 : {
17820 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17821 : "unsupported map expression %qE",
17822 7 : OMP_CLAUSE_DECL (c));
17823 7 : remove = true;
17824 7 : break;
17825 : }
17826 :
17827 15254 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17828 : || addr_tokens[0]->type == STRUCTURE_BASE)
17829 : && addr_tokens[1]->type == ACCESS_METHOD);
17830 :
17831 7627 : t = addr_tokens[1]->expr;
17832 :
17833 7627 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17834 0 : goto skip_decl_checks;
17835 :
17836 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17837 : mapping. OpenACC allows multiple fields of the same structure
17838 : to be written. */
17839 7627 : if (addr_tokens[0]->type == STRUCTURE_BASE
17840 7627 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17841 261 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17842 307 : goto skip_decl_checks;
17843 :
17844 7320 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17845 : {
17846 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17847 : break;
17848 :
17849 6 : error_at (OMP_CLAUSE_LOCATION (c),
17850 : "%qE is not a variable in %qs clause", t,
17851 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17852 3 : remove = true;
17853 : }
17854 7317 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17855 : {
17856 0 : error_at (OMP_CLAUSE_LOCATION (c),
17857 : "%qD is threadprivate variable in %qs clause", t,
17858 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17859 0 : remove = true;
17860 : }
17861 7317 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17862 4496 : || (OMP_CLAUSE_MAP_KIND (c)
17863 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17864 10769 : && !c_mark_addressable (t))
17865 : remove = true;
17866 7317 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17867 4496 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17868 4496 : || (OMP_CLAUSE_MAP_KIND (c)
17869 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17870 3452 : || (OMP_CLAUSE_MAP_KIND (c)
17871 : == GOMP_MAP_FORCE_DEVICEPTR)))
17872 6215 : && t == OMP_CLAUSE_DECL (c)
17873 13273 : && !omp_mappable_type (TREE_TYPE (t)))
17874 : {
17875 16 : error_at (OMP_CLAUSE_LOCATION (c),
17876 : "%qD does not have a mappable type in %qs clause", t,
17877 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17878 8 : remove = true;
17879 : }
17880 7309 : else if (TREE_TYPE (t) == error_mark_node)
17881 : remove = true;
17882 7308 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17883 : {
17884 8 : error_at (OMP_CLAUSE_LOCATION (c),
17885 : "%<_Atomic%> %qE in %qs clause", t,
17886 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17887 4 : remove = true;
17888 : }
17889 7304 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17890 4486 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17891 7435 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17892 130 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17893 130 : || bitmap_bit_p (&map_firstprivate_head,
17894 130 : DECL_UID (t))))
17895 : remove = true;
17896 7302 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17897 7302 : && (OMP_CLAUSE_MAP_KIND (c)
17898 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17899 : {
17900 1042 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17901 1042 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17902 2083 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17903 : {
17904 4 : error_at (OMP_CLAUSE_LOCATION (c),
17905 : "%qD appears more than once in data clauses", t);
17906 4 : remove = true;
17907 : }
17908 1038 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17909 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17910 1040 : && openacc)
17911 : {
17912 1 : error_at (OMP_CLAUSE_LOCATION (c),
17913 : "%qD appears more than once in data clauses", t);
17914 1 : remove = true;
17915 : }
17916 : else
17917 1037 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17918 : }
17919 6260 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17920 65 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17921 18 : && ort != C_ORT_OMP
17922 18 : && ort != C_ORT_OMP_TARGET
17923 6270 : && ort != C_ORT_OMP_EXIT_DATA)
17924 : {
17925 7 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17926 0 : error_at (OMP_CLAUSE_LOCATION (c),
17927 : "%qD appears more than once in motion clauses", t);
17928 7 : else if (openacc)
17929 7 : error_at (OMP_CLAUSE_LOCATION (c),
17930 : "%qD appears more than once in data clauses", t);
17931 : else
17932 0 : error_at (OMP_CLAUSE_LOCATION (c),
17933 : "%qD appears more than once in map clauses", t);
17934 : remove = true;
17935 : }
17936 6253 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17937 : {
17938 0 : error_at (OMP_CLAUSE_LOCATION (c),
17939 : "%qD appears more than once in data clauses", t);
17940 0 : remove = true;
17941 : }
17942 6253 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17943 6253 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17944 : {
17945 9 : if (openacc)
17946 0 : error_at (OMP_CLAUSE_LOCATION (c),
17947 : "%qD appears more than once in data clauses", t);
17948 : else
17949 9 : error_at (OMP_CLAUSE_LOCATION (c),
17950 : "%qD appears both in data and map clauses", t);
17951 : remove = true;
17952 : }
17953 6244 : else if (!omp_access_chain_p (addr_tokens, 1))
17954 : {
17955 6244 : bitmap_set_bit (&map_head, DECL_UID (t));
17956 6244 : if (t != OMP_CLAUSE_DECL (c)
17957 6244 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17958 255 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17959 : }
17960 :
17961 0 : skip_decl_checks:
17962 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17963 : the containing loop (here) iterates through the new nodes
17964 : created by that expansion. Avoid expanding those again (just
17965 : by checking the node type). */
17966 7627 : if (!remove
17967 7627 : && ort != C_ORT_DECLARE_SIMD
17968 7627 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17969 4744 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17970 3707 : && (OMP_CLAUSE_MAP_KIND (c)
17971 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17972 3707 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17973 3707 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17974 3406 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17975 3379 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17976 : {
17977 6208 : grp_start_p = pc;
17978 6208 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17979 6208 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17980 : addr_tokens, ort);
17981 6208 : if (nc != error_mark_node)
17982 6208 : c = nc;
17983 : }
17984 9677 : }
17985 7627 : break;
17986 :
17987 233 : case OMP_CLAUSE_ENTER:
17988 233 : case OMP_CLAUSE_LINK:
17989 233 : t = OMP_CLAUSE_DECL (c);
17990 233 : const char *cname;
17991 233 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17992 233 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17993 233 : && OMP_CLAUSE_ENTER_TO (c))
17994 : cname = "to";
17995 233 : if (TREE_CODE (t) == FUNCTION_DECL
17996 233 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17997 : ;
17998 151 : else if (!VAR_P (t))
17999 : {
18000 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
18001 0 : error_at (OMP_CLAUSE_LOCATION (c),
18002 : "%qE is neither a variable nor a function name in "
18003 : "clause %qs", t, cname);
18004 : else
18005 1 : error_at (OMP_CLAUSE_LOCATION (c),
18006 : "%qE is not a variable in clause %qs", t, cname);
18007 : remove = true;
18008 : }
18009 150 : else if (DECL_THREAD_LOCAL_P (t))
18010 : {
18011 2 : error_at (OMP_CLAUSE_LOCATION (c),
18012 : "%qD is threadprivate variable in %qs clause", t,
18013 : cname);
18014 2 : remove = true;
18015 : }
18016 148 : else if (!omp_mappable_type (TREE_TYPE (t)))
18017 : {
18018 6 : error_at (OMP_CLAUSE_LOCATION (c),
18019 : "%qD does not have a mappable type in %qs clause", t,
18020 : cname);
18021 6 : remove = true;
18022 : }
18023 9 : if (remove)
18024 : break;
18025 224 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18026 : {
18027 8 : error_at (OMP_CLAUSE_LOCATION (c),
18028 : "%qE appears more than once on the same "
18029 : "%<declare target%> directive", t);
18030 8 : remove = true;
18031 : }
18032 : else
18033 216 : bitmap_set_bit (&generic_head, DECL_UID (t));
18034 : break;
18035 :
18036 117 : case OMP_CLAUSE_UNIFORM:
18037 117 : t = OMP_CLAUSE_DECL (c);
18038 117 : if (TREE_CODE (t) != PARM_DECL)
18039 : {
18040 0 : if (DECL_P (t))
18041 0 : error_at (OMP_CLAUSE_LOCATION (c),
18042 : "%qD is not an argument in %<uniform%> clause", t);
18043 : else
18044 0 : error_at (OMP_CLAUSE_LOCATION (c),
18045 : "%qE is not an argument in %<uniform%> clause", t);
18046 : remove = true;
18047 : break;
18048 : }
18049 : /* map_head bitmap is used as uniform_head if declare_simd. */
18050 117 : bitmap_set_bit (&map_head, DECL_UID (t));
18051 117 : goto check_dup_generic;
18052 :
18053 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18054 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18055 161 : t = OMP_CLAUSE_DECL (c);
18056 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18057 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18058 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18059 : {
18060 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18061 21 : && !openacc)
18062 : {
18063 1 : error_at (OMP_CLAUSE_LOCATION (c),
18064 : "%qs variable is not a pointer",
18065 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18066 1 : remove = true;
18067 : }
18068 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18069 : {
18070 4 : error_at (OMP_CLAUSE_LOCATION (c),
18071 : "%qs variable is neither a pointer nor an array",
18072 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18073 4 : remove = true;
18074 : }
18075 : }
18076 161 : goto check_dup_generic;
18077 :
18078 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18079 89 : t = OMP_CLAUSE_DECL (c);
18080 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18081 : {
18082 11 : if (handle_omp_array_sections (c, ort))
18083 : remove = true;
18084 : else
18085 : {
18086 11 : t = OMP_CLAUSE_DECL (c);
18087 24 : while (TREE_CODE (t) == ARRAY_REF)
18088 13 : t = TREE_OPERAND (t, 0);
18089 : }
18090 : }
18091 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18092 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18093 89 : c_mark_addressable (t);
18094 89 : goto check_dup_generic_t;
18095 :
18096 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18097 36 : t = OMP_CLAUSE_DECL (c);
18098 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18099 36 : c_mark_addressable (t);
18100 36 : goto check_dup_generic;
18101 :
18102 4793 : case OMP_CLAUSE_NOWAIT:
18103 4793 : if (copyprivate_seen)
18104 : {
18105 1 : error_at (OMP_CLAUSE_LOCATION (c),
18106 : "%<nowait%> clause must not be used together "
18107 : "with %<copyprivate%> clause");
18108 1 : remove = true;
18109 1 : break;
18110 : }
18111 4792 : nowait_clause = pc;
18112 4792 : pc = &OMP_CLAUSE_CHAIN (c);
18113 4792 : continue;
18114 :
18115 523 : case OMP_CLAUSE_ORDER:
18116 523 : if (ordered_clause)
18117 : {
18118 2 : error_at (OMP_CLAUSE_LOCATION (c),
18119 : "%<order%> clause must not be used together "
18120 : "with %<ordered%> clause");
18121 2 : remove = true;
18122 2 : break;
18123 : }
18124 521 : else if (order_clause)
18125 : {
18126 : /* Silently remove duplicates. */
18127 : remove = true;
18128 : break;
18129 : }
18130 513 : order_clause = pc;
18131 513 : pc = &OMP_CLAUSE_CHAIN (c);
18132 513 : continue;
18133 :
18134 32 : case OMP_CLAUSE_DETACH:
18135 32 : t = OMP_CLAUSE_DECL (c);
18136 32 : if (detach_seen)
18137 : {
18138 1 : error_at (OMP_CLAUSE_LOCATION (c),
18139 : "too many %qs clauses on a task construct",
18140 : "detach");
18141 1 : remove = true;
18142 1 : break;
18143 : }
18144 31 : detach_seen = pc;
18145 31 : pc = &OMP_CLAUSE_CHAIN (c);
18146 31 : c_mark_addressable (t);
18147 31 : continue;
18148 :
18149 14600 : case OMP_CLAUSE_IF:
18150 14600 : case OMP_CLAUSE_SELF:
18151 14600 : case OMP_CLAUSE_NUM_THREADS:
18152 14600 : case OMP_CLAUSE_NUM_TEAMS:
18153 14600 : case OMP_CLAUSE_THREAD_LIMIT:
18154 14600 : case OMP_CLAUSE_DEFAULT:
18155 14600 : case OMP_CLAUSE_UNTIED:
18156 14600 : case OMP_CLAUSE_COLLAPSE:
18157 14600 : case OMP_CLAUSE_FINAL:
18158 14600 : case OMP_CLAUSE_DEVICE:
18159 14600 : case OMP_CLAUSE_DIST_SCHEDULE:
18160 14600 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18161 14600 : case OMP_CLAUSE_PARALLEL:
18162 14600 : case OMP_CLAUSE_FOR:
18163 14600 : case OMP_CLAUSE_SECTIONS:
18164 14600 : case OMP_CLAUSE_TASKGROUP:
18165 14600 : case OMP_CLAUSE_PROC_BIND:
18166 14600 : case OMP_CLAUSE_DEVICE_TYPE:
18167 14600 : case OMP_CLAUSE_PRIORITY:
18168 14600 : case OMP_CLAUSE_THREADS:
18169 14600 : case OMP_CLAUSE_SIMD:
18170 14600 : case OMP_CLAUSE_HINT:
18171 14600 : case OMP_CLAUSE_FILTER:
18172 14600 : case OMP_CLAUSE_DEFAULTMAP:
18173 14600 : case OMP_CLAUSE_BIND:
18174 14600 : case OMP_CLAUSE_NUM_GANGS:
18175 14600 : case OMP_CLAUSE_NUM_WORKERS:
18176 14600 : case OMP_CLAUSE_VECTOR_LENGTH:
18177 14600 : case OMP_CLAUSE_ASYNC:
18178 14600 : case OMP_CLAUSE_WAIT:
18179 14600 : case OMP_CLAUSE_AUTO:
18180 14600 : case OMP_CLAUSE_INDEPENDENT:
18181 14600 : case OMP_CLAUSE_SEQ:
18182 14600 : case OMP_CLAUSE_GANG:
18183 14600 : case OMP_CLAUSE_WORKER:
18184 14600 : case OMP_CLAUSE_VECTOR:
18185 14600 : case OMP_CLAUSE_TILE:
18186 14600 : case OMP_CLAUSE_IF_PRESENT:
18187 14600 : case OMP_CLAUSE_FINALIZE:
18188 14600 : case OMP_CLAUSE_NOHOST:
18189 14600 : case OMP_CLAUSE_INDIRECT:
18190 14600 : case OMP_CLAUSE_NOVARIANTS:
18191 14600 : case OMP_CLAUSE_NOCONTEXT:
18192 14600 : pc = &OMP_CLAUSE_CHAIN (c);
18193 14600 : continue;
18194 :
18195 62 : case OMP_CLAUSE_GRAINSIZE:
18196 62 : grainsize_seen = pc;
18197 62 : pc = &OMP_CLAUSE_CHAIN (c);
18198 62 : continue;
18199 :
18200 50 : case OMP_CLAUSE_NUM_TASKS:
18201 50 : num_tasks_seen = true;
18202 50 : pc = &OMP_CLAUSE_CHAIN (c);
18203 50 : continue;
18204 :
18205 79 : case OMP_CLAUSE_MERGEABLE:
18206 79 : mergeable_seen = true;
18207 79 : pc = &OMP_CLAUSE_CHAIN (c);
18208 79 : continue;
18209 :
18210 18 : case OMP_CLAUSE_NOGROUP:
18211 18 : nogroup_seen = pc;
18212 18 : pc = &OMP_CLAUSE_CHAIN (c);
18213 18 : continue;
18214 :
18215 3475 : case OMP_CLAUSE_SCHEDULE:
18216 3475 : schedule_clause = c;
18217 3475 : pc = &OMP_CLAUSE_CHAIN (c);
18218 3475 : continue;
18219 :
18220 304 : case OMP_CLAUSE_ORDERED:
18221 304 : ordered_clause = c;
18222 304 : if (order_clause)
18223 : {
18224 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18225 : "%<order%> clause must not be used together "
18226 : "with %<ordered%> clause");
18227 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18228 2 : order_clause = NULL;
18229 : }
18230 304 : pc = &OMP_CLAUSE_CHAIN (c);
18231 304 : continue;
18232 :
18233 223 : case OMP_CLAUSE_SAFELEN:
18234 223 : safelen = c;
18235 223 : pc = &OMP_CLAUSE_CHAIN (c);
18236 223 : continue;
18237 321 : case OMP_CLAUSE_SIMDLEN:
18238 321 : simdlen = c;
18239 321 : pc = &OMP_CLAUSE_CHAIN (c);
18240 321 : continue;
18241 :
18242 69 : case OMP_CLAUSE_FULL:
18243 69 : full_seen = pc;
18244 69 : pc = &OMP_CLAUSE_CHAIN (c);
18245 69 : continue;
18246 :
18247 223 : case OMP_CLAUSE_PARTIAL:
18248 223 : partial_seen = true;
18249 223 : pc = &OMP_CLAUSE_CHAIN (c);
18250 223 : continue;
18251 :
18252 0 : case OMP_CLAUSE_SIZES:
18253 0 : pc = &OMP_CLAUSE_CHAIN (c);
18254 0 : continue;
18255 :
18256 206 : case OMP_CLAUSE_INBRANCH:
18257 206 : case OMP_CLAUSE_NOTINBRANCH:
18258 206 : if (branch_seen)
18259 : {
18260 1 : error_at (OMP_CLAUSE_LOCATION (c),
18261 : "%<inbranch%> clause is incompatible with "
18262 : "%<notinbranch%>");
18263 1 : remove = true;
18264 1 : break;
18265 : }
18266 205 : branch_seen = true;
18267 205 : pc = &OMP_CLAUSE_CHAIN (c);
18268 205 : continue;
18269 :
18270 370 : case OMP_CLAUSE_INCLUSIVE:
18271 370 : case OMP_CLAUSE_EXCLUSIVE:
18272 370 : need_complete = true;
18273 370 : need_implicitly_determined = true;
18274 370 : t = OMP_CLAUSE_DECL (c);
18275 370 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18276 : {
18277 0 : error_at (OMP_CLAUSE_LOCATION (c),
18278 : "%qE is not a variable in clause %qs", t,
18279 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18280 0 : remove = true;
18281 : }
18282 : break;
18283 :
18284 152 : case OMP_CLAUSE_INIT:
18285 152 : init_seen = true;
18286 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18287 71 : init_no_targetsync_clause = c;
18288 : /* FALLTHRU */
18289 242 : case OMP_CLAUSE_DESTROY:
18290 242 : case OMP_CLAUSE_USE:
18291 242 : init_use_destroy_seen = true;
18292 242 : t = OMP_CLAUSE_DECL (c);
18293 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18294 : {
18295 3 : error_at (OMP_CLAUSE_LOCATION (c),
18296 : "%qD appears more than once in action clauses", t);
18297 3 : remove = true;
18298 3 : break;
18299 : }
18300 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18301 : /* FALLTHRU */
18302 298 : case OMP_CLAUSE_INTEROP:
18303 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18304 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18305 : {
18306 40 : error_at (OMP_CLAUSE_LOCATION (c),
18307 : "%qD must be of %<omp_interop_t%>",
18308 20 : OMP_CLAUSE_DECL (c));
18309 20 : remove = true;
18310 : }
18311 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18312 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18313 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18314 : {
18315 12 : error_at (OMP_CLAUSE_LOCATION (c),
18316 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18317 6 : remove = true;
18318 : }
18319 298 : pc = &OMP_CLAUSE_CHAIN (c);
18320 298 : break;
18321 0 : default:
18322 0 : gcc_unreachable ();
18323 25008 : }
18324 :
18325 23015 : if (!remove)
18326 : {
18327 22449 : t = OMP_CLAUSE_DECL (c);
18328 :
18329 22449 : if (need_complete)
18330 : {
18331 3970 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18332 3970 : if (t == error_mark_node)
18333 7 : remove = true;
18334 : }
18335 :
18336 22449 : if (need_implicitly_determined)
18337 : {
18338 9589 : const char *share_name = NULL;
18339 :
18340 9589 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18341 : share_name = "threadprivate";
18342 9583 : else switch (c_omp_predetermined_sharing (t))
18343 : {
18344 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18345 : break;
18346 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18347 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18348 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18349 28 : && c_omp_predefined_variable (t))
18350 : /* The __func__ variable and similar function-local
18351 : predefined variables may be listed in a shared or
18352 : firstprivate clause. */
18353 : break;
18354 : share_name = "shared";
18355 : break;
18356 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18357 : share_name = "private";
18358 : break;
18359 0 : default:
18360 0 : gcc_unreachable ();
18361 : }
18362 : if (share_name)
18363 : {
18364 20 : error_at (OMP_CLAUSE_LOCATION (c),
18365 : "%qE is predetermined %qs for %qs",
18366 : t, share_name,
18367 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18368 10 : remove = true;
18369 : }
18370 9579 : else if (TREE_READONLY (t)
18371 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18372 9592 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18373 : {
18374 4 : error_at (OMP_CLAUSE_LOCATION (c),
18375 : "%<const%> qualified %qE may appear only in "
18376 : "%<shared%> or %<firstprivate%> clauses", t);
18377 4 : remove = true;
18378 : }
18379 : }
18380 : }
18381 :
18382 22449 : if (remove)
18383 : {
18384 587 : if (grp_start_p)
18385 : {
18386 : /* If we found a clause to remove, we want to remove the whole
18387 : expanded group, otherwise gimplify
18388 : (omp_resolve_clause_dependencies) can get confused. */
18389 153 : *grp_start_p = grp_sentinel;
18390 153 : pc = grp_start_p;
18391 153 : grp_start_p = NULL;
18392 : }
18393 : else
18394 434 : *pc = OMP_CLAUSE_CHAIN (c);
18395 : }
18396 : else
18397 22428 : pc = &OMP_CLAUSE_CHAIN (c);
18398 : }
18399 :
18400 29657 : if (grp_start_p
18401 29657 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18402 52 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18403 32 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18404 :
18405 29657 : if (simdlen
18406 29657 : && safelen
18407 29774 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18408 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18409 : {
18410 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18411 : "%<simdlen%> clause value is bigger than "
18412 : "%<safelen%> clause value");
18413 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18414 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18415 : }
18416 :
18417 29657 : if (ordered_clause
18418 29657 : && schedule_clause
18419 29657 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18420 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18421 : {
18422 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18423 : "%<nonmonotonic%> schedule modifier specified together "
18424 : "with %<ordered%> clause");
18425 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18426 7 : = (enum omp_clause_schedule_kind)
18427 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18428 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18429 : }
18430 :
18431 29657 : if (reduction_seen < 0 && ordered_clause)
18432 : {
18433 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18434 : "%qs clause specified together with %<inscan%> "
18435 : "%<reduction%> clause", "ordered");
18436 2 : reduction_seen = -2;
18437 : }
18438 :
18439 29657 : if (reduction_seen < 0 && schedule_clause)
18440 : {
18441 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18442 : "%qs clause specified together with %<inscan%> "
18443 : "%<reduction%> clause", "schedule");
18444 3 : reduction_seen = -2;
18445 : }
18446 :
18447 29657 : if (linear_variable_step_check
18448 29657 : || reduction_seen == -2
18449 : || allocate_seen
18450 29645 : || target_in_reduction_seen)
18451 5225 : for (pc = &clauses, c = clauses; c ; c = *pc)
18452 : {
18453 4618 : bool remove = false;
18454 4618 : if (allocate_seen)
18455 4460 : switch (OMP_CLAUSE_CODE (c))
18456 : {
18457 430 : case OMP_CLAUSE_REDUCTION:
18458 430 : case OMP_CLAUSE_IN_REDUCTION:
18459 430 : case OMP_CLAUSE_TASK_REDUCTION:
18460 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18461 : {
18462 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18463 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18464 0 : t = TREE_OPERAND (t, 0);
18465 23 : if (TREE_CODE (t) == ADDR_EXPR
18466 10 : || INDIRECT_REF_P (t))
18467 13 : t = TREE_OPERAND (t, 0);
18468 23 : if (DECL_P (t))
18469 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18470 : break;
18471 : }
18472 : /* FALLTHRU */
18473 1329 : case OMP_CLAUSE_PRIVATE:
18474 1329 : case OMP_CLAUSE_FIRSTPRIVATE:
18475 1329 : case OMP_CLAUSE_LASTPRIVATE:
18476 1329 : case OMP_CLAUSE_LINEAR:
18477 1329 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18478 2658 : bitmap_clear_bit (&aligned_head,
18479 1329 : DECL_UID (OMP_CLAUSE_DECL (c)));
18480 : break;
18481 : default:
18482 : break;
18483 : }
18484 4618 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18485 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18486 4625 : && !bitmap_bit_p (&map_head,
18487 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18488 : {
18489 2 : error_at (OMP_CLAUSE_LOCATION (c),
18490 : "%<linear%> clause step is a parameter %qD not "
18491 : "specified in %<uniform%> clause",
18492 2 : OMP_CLAUSE_LINEAR_STEP (c));
18493 2 : remove = true;
18494 : }
18495 4616 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18496 4616 : && reduction_seen == -2)
18497 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18498 4618 : if (target_in_reduction_seen
18499 4618 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18500 : {
18501 256 : tree t = OMP_CLAUSE_DECL (c);
18502 256 : while (handled_component_p (t)
18503 : || INDIRECT_REF_P (t)
18504 : || TREE_CODE (t) == ADDR_EXPR
18505 : || TREE_CODE (t) == MEM_REF
18506 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18507 32 : t = TREE_OPERAND (t, 0);
18508 256 : if (DECL_P (t)
18509 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18510 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18511 : }
18512 :
18513 4618 : if (remove)
18514 2 : *pc = OMP_CLAUSE_CHAIN (c);
18515 : else
18516 4616 : pc = &OMP_CLAUSE_CHAIN (c);
18517 : }
18518 :
18519 607 : if (allocate_seen)
18520 5041 : for (pc = &clauses, c = clauses; c ; c = *pc)
18521 : {
18522 4460 : bool remove = false;
18523 4460 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18524 731 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18525 5171 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18526 : {
18527 3 : error_at (OMP_CLAUSE_LOCATION (c),
18528 : "%qD specified in %<allocate%> clause but not in "
18529 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18530 3 : remove = true;
18531 : }
18532 4460 : if (remove)
18533 3 : *pc = OMP_CLAUSE_CHAIN (c);
18534 : else
18535 4457 : pc = &OMP_CLAUSE_CHAIN (c);
18536 : }
18537 :
18538 29657 : if (nogroup_seen && reduction_seen)
18539 : {
18540 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18541 : "%<nogroup%> clause must not be used together with "
18542 : "%<reduction%> clause");
18543 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18544 : }
18545 :
18546 29657 : if (grainsize_seen && num_tasks_seen)
18547 : {
18548 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18549 : "%<grainsize%> clause must not be used together with "
18550 : "%<num_tasks%> clause");
18551 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18552 : }
18553 :
18554 29657 : if (full_seen && partial_seen)
18555 : {
18556 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18557 : "%<full%> clause must not be used together with "
18558 : "%<partial%> clause");
18559 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18560 : }
18561 :
18562 29657 : if (detach_seen)
18563 : {
18564 31 : if (mergeable_seen)
18565 : {
18566 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18567 : "%<detach%> clause must not be used together with "
18568 : "%<mergeable%> clause");
18569 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18570 : }
18571 : else
18572 : {
18573 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18574 :
18575 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18576 : {
18577 50 : bool remove = false;
18578 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18579 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18580 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18581 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18582 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18583 : {
18584 2 : error_at (OMP_CLAUSE_LOCATION (c),
18585 : "the event handle of a %<detach%> clause "
18586 : "should not be in a data-sharing clause");
18587 2 : remove = true;
18588 : }
18589 50 : if (remove)
18590 2 : *pc = OMP_CLAUSE_CHAIN (c);
18591 : else
18592 48 : pc = &OMP_CLAUSE_CHAIN (c);
18593 : }
18594 : }
18595 : }
18596 :
18597 29657 : if (ort == C_ORT_OMP_INTEROP
18598 29657 : && depend_clause
18599 21 : && (!init_use_destroy_seen
18600 20 : || (init_seen && init_no_targetsync_clause)))
18601 : {
18602 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18603 : "%<depend%> clause requires action clauses with "
18604 : "%<targetsync%> interop-type");
18605 3 : if (init_no_targetsync_clause)
18606 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18607 : "%<init%> clause lacks the %<targetsync%> modifier");
18608 : }
18609 :
18610 29657 : bitmap_obstack_release (NULL);
18611 29657 : return clauses;
18612 : }
18613 :
18614 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18615 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18616 :
18617 : tree
18618 15 : c_omp_finish_mapper_clauses (tree clauses)
18619 : {
18620 15 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18621 : }
18622 :
18623 : /* Return code to initialize DST with a copy constructor from SRC.
18624 : C doesn't have copy constructors nor assignment operators, only for
18625 : _Atomic vars we need to perform __atomic_load from src into a temporary
18626 : followed by __atomic_store of the temporary to dst. */
18627 :
18628 : tree
18629 18755 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18630 : {
18631 18755 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18632 18751 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18633 :
18634 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18635 4 : tree type = TREE_TYPE (dst);
18636 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18637 4 : tree tmp = create_tmp_var (nonatomic_type);
18638 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18639 4 : TREE_ADDRESSABLE (tmp) = 1;
18640 4 : suppress_warning (tmp);
18641 4 : tree src_addr = build_fold_addr_expr (src);
18642 4 : tree dst_addr = build_fold_addr_expr (dst);
18643 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18644 4 : vec<tree, va_gc> *params;
18645 : /* Expansion of a generic atomic load may require an addition
18646 : element, so allocate enough to prevent a resize. */
18647 4 : vec_alloc (params, 4);
18648 :
18649 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18650 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18651 4 : params->quick_push (src_addr);
18652 4 : params->quick_push (tmp_addr);
18653 4 : params->quick_push (seq_cst);
18654 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18655 :
18656 4 : vec_alloc (params, 4);
18657 :
18658 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18659 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18660 4 : params->quick_push (dst_addr);
18661 4 : params->quick_push (tmp_addr);
18662 4 : params->quick_push (seq_cst);
18663 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18664 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18665 : }
18666 :
18667 : /* Create a transaction node. */
18668 :
18669 : tree
18670 135 : c_finish_transaction (location_t loc, tree block, int flags)
18671 : {
18672 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18673 135 : if (flags & TM_STMT_ATTR_OUTER)
18674 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18675 135 : if (flags & TM_STMT_ATTR_RELAXED)
18676 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18677 135 : return add_stmt (stmt);
18678 : }
18679 :
18680 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18681 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18682 : NULL, then it should be used as the qualified type
18683 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18684 : preserve information about the typedef name from which an array
18685 : type was derived). */
18686 :
18687 : tree
18688 76393881 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18689 : size_t orig_qual_indirect)
18690 : {
18691 76393881 : if (type == error_mark_node)
18692 : return type;
18693 :
18694 76393729 : if (TREE_CODE (type) == ARRAY_TYPE)
18695 : {
18696 3152255 : tree t;
18697 3152255 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18698 : type_quals, orig_qual_type,
18699 : orig_qual_indirect - 1);
18700 :
18701 : /* See if we already have an identically qualified type. */
18702 3152255 : if (orig_qual_type && orig_qual_indirect == 0)
18703 : t = orig_qual_type;
18704 : else
18705 4595702 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18706 : {
18707 4455022 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18708 3032560 : && TYPE_NAME (t) == TYPE_NAME (type)
18709 3011534 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18710 7466556 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18711 3011534 : TYPE_ATTRIBUTES (type)))
18712 : break;
18713 : }
18714 3152214 : if (!t)
18715 : {
18716 140680 : tree domain = TYPE_DOMAIN (type);
18717 :
18718 140680 : t = build_variant_type_copy (type);
18719 140680 : TREE_TYPE (t) = element_type;
18720 140680 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18721 :
18722 140680 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18723 140680 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18724 196 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18725 140484 : else if (TYPE_CANONICAL (element_type) != element_type
18726 140484 : || (domain && TYPE_CANONICAL (domain) != domain))
18727 : {
18728 2835 : tree unqualified_canon
18729 5413 : = c_build_array_type (TYPE_CANONICAL (element_type),
18730 2578 : domain ? TYPE_CANONICAL (domain)
18731 : : NULL_TREE);
18732 2835 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18733 : {
18734 0 : unqualified_canon
18735 0 : = build_distinct_type_copy (unqualified_canon);
18736 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18737 : }
18738 2835 : TYPE_CANONICAL (t)
18739 5670 : = c_build_qualified_type (unqualified_canon, type_quals);
18740 : }
18741 : else
18742 137649 : TYPE_CANONICAL (t) = t;
18743 : }
18744 : return t;
18745 : }
18746 :
18747 : /* A restrict-qualified pointer type must be a pointer to object or
18748 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18749 : REFERENCE_TYPEs, which is appropriate for C++. */
18750 73241474 : if ((type_quals & TYPE_QUAL_RESTRICT)
18751 73241474 : && (!POINTER_TYPE_P (type)
18752 3487070 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18753 : {
18754 3 : error ("invalid use of %<restrict%>");
18755 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18756 : }
18757 :
18758 73241474 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18759 73241474 : ? orig_qual_type
18760 73241443 : : build_qualified_type (type, type_quals));
18761 :
18762 73241474 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18763 : == C_TYPE_VARIABLE_SIZE (type));
18764 73241474 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18765 : == c_type_variably_modified_p (type));
18766 :
18767 : /* A variant type does not inherit the list of incomplete vars from the
18768 : type main variant. */
18769 73241474 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18770 71153897 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18771 73347288 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18772 1413999 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18773 : return var_type;
18774 : }
18775 :
18776 : /* Build a VA_ARG_EXPR for the C parser. */
18777 :
18778 : tree
18779 19974 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18780 : tree type_expr)
18781 : {
18782 19974 : if (error_operand_p (type))
18783 2 : return error_mark_node;
18784 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18785 : order because it takes the address of the expression. */
18786 19972 : else if (handled_component_p (expr)
18787 31 : && reverse_storage_order_for_component_p (expr))
18788 : {
18789 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18790 0 : return error_mark_node;
18791 : }
18792 19972 : else if (!COMPLETE_TYPE_P (type))
18793 : {
18794 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18795 : "type %qT", type);
18796 11 : return error_mark_node;
18797 : }
18798 19961 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18799 : {
18800 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18801 : type);
18802 1 : return error_mark_node;
18803 : }
18804 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18805 19963 : && !flag_isoc99)
18806 : {
18807 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18808 : type);
18809 1 : return error_mark_node;
18810 : }
18811 19959 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18812 1 : warning_at (loc2, OPT_Wc___compat,
18813 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18814 :
18815 19959 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18816 : {
18817 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18818 : type);
18819 : /* We create a trap but evaluate side effects first. */
18820 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18821 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18822 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18823 : /* Return a compound literal of the right type. */
18824 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18825 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18826 3 : }
18827 : else
18828 19956 : expr = build_va_arg (loc2, expr, type);
18829 :
18830 19959 : if (type_expr)
18831 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18832 :
18833 : return expr;
18834 : }
18835 :
18836 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18837 : Return 1 if they are the same. Return false if they are different. */
18838 :
18839 : bool
18840 1854 : c_tree_equal (tree t1, tree t2)
18841 : {
18842 1883 : enum tree_code code1, code2;
18843 :
18844 1883 : if (t1 == t2)
18845 : return true;
18846 661 : if (!t1 || !t2)
18847 : return false;
18848 :
18849 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18850 20 : code1 = TREE_CODE (t1))
18851 20 : t1 = TREE_OPERAND (t1, 0);
18852 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18853 21 : code2 = TREE_CODE (t2))
18854 21 : t2 = TREE_OPERAND (t2, 0);
18855 :
18856 : /* They might have become equal now. */
18857 661 : if (t1 == t2)
18858 : return true;
18859 :
18860 640 : if (code1 != code2)
18861 : return false;
18862 :
18863 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18864 : return false;
18865 :
18866 379 : switch (code1)
18867 : {
18868 2 : case INTEGER_CST:
18869 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18870 :
18871 10 : case REAL_CST:
18872 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18873 :
18874 0 : case STRING_CST:
18875 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18876 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18877 0 : TREE_STRING_LENGTH (t1));
18878 :
18879 0 : case FIXED_CST:
18880 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18881 : TREE_FIXED_CST (t2));
18882 :
18883 0 : case COMPLEX_CST:
18884 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18885 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18886 :
18887 0 : case VECTOR_CST:
18888 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18889 :
18890 0 : case CONSTRUCTOR:
18891 : /* We need to do this when determining whether or not two
18892 : non-type pointer to member function template arguments
18893 : are the same. */
18894 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18895 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18896 : return false;
18897 : {
18898 : tree field, value;
18899 : unsigned int i;
18900 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18901 : {
18902 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18903 0 : if (!c_tree_equal (field, elt2->index)
18904 0 : || !c_tree_equal (value, elt2->value))
18905 : return false;
18906 : }
18907 : }
18908 : return true;
18909 :
18910 0 : case TREE_LIST:
18911 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18912 : return false;
18913 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18914 : return false;
18915 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18916 :
18917 0 : case SAVE_EXPR:
18918 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18919 :
18920 25 : case CALL_EXPR:
18921 25 : {
18922 25 : tree arg1, arg2;
18923 25 : call_expr_arg_iterator iter1, iter2;
18924 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18925 : return false;
18926 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18927 25 : arg2 = first_call_expr_arg (t2, &iter2);
18928 25 : arg1 && arg2;
18929 0 : arg1 = next_call_expr_arg (&iter1),
18930 0 : arg2 = next_call_expr_arg (&iter2))
18931 0 : if (!c_tree_equal (arg1, arg2))
18932 : return false;
18933 25 : if (arg1 || arg2)
18934 0 : return false;
18935 : return true;
18936 : }
18937 :
18938 0 : case TARGET_EXPR:
18939 0 : {
18940 0 : tree o1 = TREE_OPERAND (t1, 0);
18941 0 : tree o2 = TREE_OPERAND (t2, 0);
18942 :
18943 : /* Special case: if either target is an unallocated VAR_DECL,
18944 : it means that it's going to be unified with whatever the
18945 : TARGET_EXPR is really supposed to initialize, so treat it
18946 : as being equivalent to anything. */
18947 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18948 0 : && !DECL_RTL_SET_P (o1))
18949 : /*Nop*/;
18950 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18951 0 : && !DECL_RTL_SET_P (o2))
18952 : /*Nop*/;
18953 0 : else if (!c_tree_equal (o1, o2))
18954 : return false;
18955 :
18956 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18957 : }
18958 :
18959 29 : case COMPONENT_REF:
18960 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18961 : return false;
18962 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18963 :
18964 : case PARM_DECL:
18965 : case VAR_DECL:
18966 : case CONST_DECL:
18967 : case FIELD_DECL:
18968 : case FUNCTION_DECL:
18969 : case IDENTIFIER_NODE:
18970 : case SSA_NAME:
18971 : return false;
18972 :
18973 0 : case TREE_VEC:
18974 0 : {
18975 0 : unsigned ix;
18976 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18977 : return false;
18978 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18979 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18980 0 : TREE_VEC_ELT (t2, ix)))
18981 : return false;
18982 : return true;
18983 : }
18984 :
18985 22 : CASE_CONVERT:
18986 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18987 : return false;
18988 : break;
18989 :
18990 : default:
18991 : break;
18992 : }
18993 :
18994 131 : switch (TREE_CODE_CLASS (code1))
18995 : {
18996 131 : case tcc_unary:
18997 131 : case tcc_binary:
18998 131 : case tcc_comparison:
18999 131 : case tcc_expression:
19000 131 : case tcc_vl_exp:
19001 131 : case tcc_reference:
19002 131 : case tcc_statement:
19003 131 : {
19004 131 : int i, n = TREE_OPERAND_LENGTH (t1);
19005 :
19006 131 : switch (code1)
19007 : {
19008 0 : case PREINCREMENT_EXPR:
19009 0 : case PREDECREMENT_EXPR:
19010 0 : case POSTINCREMENT_EXPR:
19011 0 : case POSTDECREMENT_EXPR:
19012 0 : n = 1;
19013 0 : break;
19014 16 : case ARRAY_REF:
19015 16 : n = 2;
19016 16 : break;
19017 : default:
19018 : break;
19019 : }
19020 :
19021 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
19022 131 : && n != TREE_OPERAND_LENGTH (t2))
19023 : return false;
19024 :
19025 305 : for (i = 0; i < n; ++i)
19026 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
19027 : return false;
19028 :
19029 : return true;
19030 : }
19031 :
19032 0 : case tcc_type:
19033 0 : return comptypes (t1, t2);
19034 0 : default:
19035 0 : gcc_unreachable ();
19036 : }
19037 : }
19038 :
19039 : /* Returns true when the function declaration FNDECL is implicit,
19040 : introduced as a result of a call to an otherwise undeclared
19041 : function, and false otherwise. */
19042 :
19043 : bool
19044 2013 : c_decl_implicit (const_tree fndecl)
19045 : {
19046 2013 : return C_DECL_IMPLICIT (fndecl);
19047 : }
|