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 194608371 : 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 194608371 : 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 194608371 : if (expr == nullptr_node)
160 : return true;
161 :
162 194607669 : return (TREE_CODE (expr) == INTEGER_CST
163 19675625 : && !TREE_OVERFLOW (expr)
164 19675528 : && integer_zerop (expr)
165 198605648 : && (INTEGRAL_TYPE_P (type)
166 340941 : || (TREE_CODE (type) == POINTER_TYPE
167 340939 : && VOID_TYPE_P (TREE_TYPE (type))
168 265479 : && 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 62370 : note_integer_operands (tree expr)
178 : {
179 62370 : tree ret;
180 62370 : 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 62348 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
188 62348 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
189 : }
190 62370 : 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 35995779 : remove_c_maybe_const_expr (tree expr)
200 : {
201 35995779 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
202 61518 : 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 803036776 : require_complete_type (location_t loc, tree value)
221 : {
222 803036776 : tree type = TREE_TYPE (value);
223 :
224 803036776 : if (error_operand_p (value))
225 9092 : return error_mark_node;
226 :
227 : /* First, detect a valid value with a complete type. */
228 803027684 : 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 128990852 : c_type_promotes_to (tree type)
296 : {
297 128990852 : tree ret = NULL_TREE;
298 :
299 128990852 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 1643128 : ret = double_type_node;
301 127347724 : else if (c_promoting_integer_type_p (type))
302 : {
303 : /* Preserve unsignedness if not really getting any wider. */
304 19299613 : if (TYPE_UNSIGNED (type)
305 19299613 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 0 : ret = unsigned_type_node;
307 : else
308 19299613 : ret = integer_type_node;
309 : }
310 :
311 20942741 : if (ret != NULL_TREE)
312 20942741 : return (TYPE_ATOMIC (type)
313 20942741 : ? 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 7577829 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
325 : {
326 7577829 : if (as1 == as2)
327 : {
328 7577829 : *common = as1;
329 7577829 : 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 2752244 : qualify_type (tree type, tree like)
350 : {
351 2752244 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 2752244 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 2752244 : 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 2752244 : 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 11008976 : return c_build_qualified_type (type,
365 2752244 : {TYPE_QUALS_NO_ADDR_SPACE (type)
366 2752244 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like),
367 2752244 : 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 146041210 : c_verify_type (tree type)
379 : {
380 146041210 : switch (TREE_CODE (type))
381 : {
382 69897405 : case POINTER_TYPE:
383 69897405 : case FUNCTION_TYPE:
384 69897405 : case ARRAY_TYPE:
385 : /* Pointer, array, functions are variably modified if and only if the
386 : target, element, return type is variably modified. */
387 69897405 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
388 69897405 : && (C_TYPE_VARIABLY_MODIFIED (type)
389 69506363 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
390 : return false;
391 : /* If the target type is structural equality, the type should also. */
392 69897405 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
393 69897405 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
394 : return false;
395 146041210 : default:
396 146041210 : break;
397 : }
398 :
399 146041210 : switch (TREE_CODE (type))
400 : {
401 68972830 : case POINTER_TYPE:
402 68972830 : case FUNCTION_TYPE:
403 : /* Pointer and functions can not have variable size. */
404 68972830 : if (C_TYPE_VARIABLE_SIZE (type))
405 0 : return false;
406 : break;
407 924575 : 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 924575 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
411 924575 : || top_array_vla_p (type))
412 924575 : != 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 924575 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
417 924575 : || C_TYPE_VARIABLE_SIZE (type))
418 : {
419 27706 : if (!C_TYPE_VARIABLE_SIZE (type))
420 : return false;
421 27706 : if (!C_TYPE_VARIABLY_MODIFIED (type))
422 : return false;
423 : }
424 :
425 : /* va_list violates this, accept such types here. */
426 924575 : if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
427 : return true;
428 :
429 924569 : if (COMPLETE_TYPE_P (type))
430 : {
431 : /* If the size is unknown, it can not be complete. */
432 916306 : if (NULL_TREE == TYPE_DOMAIN (type))
433 : return false;
434 :
435 : /* Zero-length arrays must have size zero. */
436 916306 : if (zero_length_array_type_p (type)
437 916306 : && !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 8263 : if (NULL_TREE != TYPE_DOMAIN (type)
444 8263 : && !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 138748746 : c_set_type_bits (tree new_type, tree old_type)
458 : {
459 138748746 : gcc_checking_assert (c_verify_type (old_type));
460 :
461 138748746 : if (c_type_variably_modified_p (old_type))
462 28356 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
463 :
464 138748746 : 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 138748746 : return new_type;
470 : }
471 :
472 : /* Build a pointer type using the default pointer mode. */
473 :
474 : tree
475 74210657 : c_build_pointer_type (tree to_type)
476 : {
477 74210657 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
478 74210657 : : TYPE_ADDR_SPACE (to_type);
479 74210657 : machine_mode pointer_mode;
480 :
481 74210657 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
482 74210657 : pointer_mode = targetm.addr_space.pointer_mode (as);
483 : else
484 : pointer_mode = c_default_pointer_mode;
485 :
486 74210657 : 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 74462821 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
493 : {
494 74462821 : tree ret = build_pointer_type_for_mode (type, mode, m);
495 74462821 : return c_set_type_bits (ret, type);
496 : }
497 :
498 : /* Build a function type. */
499 :
500 : tree
501 55524530 : c_build_function_type (tree type, tree args, bool no)
502 : {
503 55524530 : tree ret = build_function_type (type, args, no);
504 55524530 : 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 1176462 : c_build_array_type (tree type, tree domain)
513 : {
514 1176462 : auto type_quals = TYPE_QUALS (type);
515 :
516 : /* Identify typeless storage as introduced in C2Y
517 : and supported also in earlier language modes. */
518 1748012 : bool typeless = (char_type_p (type) && !(type_quals.has (TYPE_QUAL_ATOMIC)))
519 1176462 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
520 :
521 1176462 : tree ret = build_array_type (type, domain, typeless);
522 :
523 1176462 : if (top_array_vla_p (ret))
524 : {
525 24285 : C_TYPE_VARIABLE_SIZE (ret) = 1;
526 24285 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
527 : }
528 :
529 1176462 : 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 2616 : 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 2616 : type = c_build_array_type (type, build_index_type (NULL_TREE));
549 2616 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
550 2616 : TYPE_SIZE (type) = bitsize_zero_node;
551 2616 : TYPE_SIZE_UNIT (type) = size_zero_node;
552 2616 : SET_TYPE_STRUCTURAL_EQUALITY (type);
553 2616 : return type;
554 : }
555 :
556 : tree
557 7584933 : c_build_type_attribute_qual_variant (tree type, tree attrs, qualifier_set quals)
558 : {
559 7584933 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
560 7584933 : return c_set_type_bits (ret, type);
561 : }
562 :
563 : tree
564 7392796 : c_build_type_attribute_variant (tree type, tree attrs)
565 : {
566 7392796 : 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 852320 : c_reconstruct_complex_type (tree type, tree bottom)
574 : {
575 852320 : tree inner, outer;
576 :
577 852320 : if (TREE_CODE (type) == POINTER_TYPE)
578 : {
579 177727 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
580 177727 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
581 177727 : TYPE_REF_CAN_ALIAS_ALL (type));
582 : }
583 : else if (TREE_CODE (type) == ARRAY_TYPE)
584 : {
585 7432 : bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
586 7432 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
587 7432 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
588 7432 : if (rso)
589 : {
590 1 : outer = build_distinct_type_copy (outer);
591 1 : TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
592 : }
593 :
594 7432 : 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 185761 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
610 185761 : 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 3503106 : ptr_to_tagged_member (tree field)
620 : {
621 3503106 : gcc_assert (FIELD_DECL == TREE_CODE (field));
622 3503106 : tree type = TREE_TYPE (field);
623 3503106 : bool ptr_seen = false;
624 3503106 : while (TREE_CODE (type) == ARRAY_TYPE
625 : || TREE_CODE (type) == FUNCTION_TYPE
626 4800779 : || TREE_CODE (type) == POINTER_TYPE)
627 : {
628 1297673 : if (TREE_CODE (type) == POINTER_TYPE)
629 710155 : ptr_seen = true;
630 1297673 : type = TREE_TYPE (type);
631 : }
632 :
633 3503106 : if (ptr_seen
634 689804 : && RECORD_OR_UNION_TYPE_P (type)
635 3756384 : && NULL_TREE != c_type_tag (type))
636 248931 : 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 903343 : c_type_canonical (tree type)
653 : {
654 903343 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
655 :
656 903343 : bool needs_mod = false;
657 3700473 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
658 : {
659 2880730 : if (!ptr_to_tagged_member (x))
660 2797130 : continue;
661 : needs_mod = true;
662 : break;
663 : }
664 903343 : if (!needs_mod)
665 : return type;
666 :
667 : /* Construct new version with such members replaced. */
668 83600 : tree n = build_distinct_type_copy (type);
669 83600 : tree *fields = &TYPE_FIELDS (n);
670 705976 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
671 : {
672 622376 : tree f = copy_node (x);
673 622376 : if (tree m = ptr_to_tagged_member (x))
674 : {
675 165331 : tree new_node = make_node (TREE_CODE (m));
676 165331 : TYPE_NAME (new_node) = TYPE_NAME (m);
677 165331 : SET_TYPE_STRUCTURAL_EQUALITY (new_node);
678 165331 : new_node = c_build_qualified_type
679 330662 : (new_node, {TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (m),
680 165331 : TYPE_ADDR_SPACE (m)});
681 165331 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
682 : new_node);
683 : }
684 622376 : *fields = f;
685 622376 : fields = &DECL_CHAIN (f);
686 : }
687 83600 : TYPE_CANONICAL (n) = n;
688 83600 : return n;
689 : }
690 :
691 :
692 : /* If NTYPE is a type of a non-variadic function with a prototype
693 : and OTYPE is a type of a function without a prototype and ATTRS
694 : contains attribute format, diagnosess and removes it from ATTRS.
695 : Returns the result of build_type_attribute_variant of NTYPE and
696 : the (possibly) modified ATTRS. */
697 :
698 : static tree
699 10787 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
700 : {
701 10787 : if (!prototype_p (otype)
702 10775 : && prototype_p (ntype)
703 21532 : && lookup_attribute ("format", attrs))
704 : {
705 14 : warning_at (input_location, OPT_Wattributes,
706 : "%qs attribute can only be applied to variadic functions",
707 : "format");
708 14 : attrs = remove_attribute ("format", attrs);
709 : }
710 10787 : return c_build_type_attribute_variant (ntype, attrs);
711 :
712 : }
713 :
714 : /* Given a type which could be a typedef name, make sure to return the
715 : original type. See set_underlying_type. */
716 : static const_tree
717 63184923 : c_type_original (const_tree t)
718 : {
719 : /* It may even be a typedef of a typedef...
720 : In the case of compiler-created builtin structs the TYPE_DECL
721 : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
722 63184923 : while (TYPE_NAME (t)
723 32464138 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
724 63230167 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
725 22620 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
726 63184923 : return t;
727 : }
728 :
729 : /* Return the tag for a tagged type. */
730 : tree
731 63184923 : c_type_tag (const_tree t)
732 : {
733 63184923 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
734 63184923 : const_tree orig = c_type_original (t);
735 63184923 : tree name = TYPE_NAME (orig);
736 63184923 : if (!name)
737 : return NULL_TREE;
738 32441518 : if (TREE_CODE (name) == TYPE_DECL)
739 : {
740 4 : if (!DECL_NAME (name))
741 : return NULL_TREE;
742 2 : name = DECL_NAME (name);
743 : }
744 32441516 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
745 : return name;
746 : }
747 :
748 : /* Remove qualifiers but not atomic. For arrays remove qualifiers
749 : on the element type but also do not remove atomic. */
750 : static tree
751 66387397 : remove_qualifiers (tree t)
752 : {
753 66387397 : if (!t || t == error_mark_node)
754 : return t;
755 66387372 : return TYPE_ATOMIC (strip_array_types (t))
756 132759161 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
757 66371789 : : TYPE_MAIN_VARIANT (t);
758 : }
759 :
760 :
761 : /* Helper function for composite_type_internal. Find a compatible type
762 : in a (transparent) union U compatible to T. If found, return the
763 : type of the corresponding member. Otherwise, return the union type U. */
764 : static tree
765 8749236 : transparent_union_replacement (tree u, tree t)
766 : {
767 8749231 : if (u == error_mark_node || t == error_mark_node
768 8749226 : || TREE_CODE (u) != UNION_TYPE
769 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
770 8749300 : || comptypes (u, t))
771 : return u;
772 :
773 80 : for (tree memb = TYPE_FIELDS (u); memb; memb = DECL_CHAIN (memb))
774 : {
775 80 : tree m = remove_qualifiers (TREE_TYPE (memb));
776 80 : if (comptypes (m, t))
777 : {
778 64 : pedwarn (input_location, OPT_Wpedantic,
779 : "function types not truly compatible in ISO C");
780 64 : return m;
781 : }
782 : }
783 :
784 : return u;
785 : }
786 :
787 :
788 :
789 : /* Return the composite type of two compatible types.
790 :
791 : We assume that comptypes has already been done and returned
792 : nonzero; if that isn't so, this may crash. In particular, we
793 : assume that qualifiers match. */
794 :
795 : struct composite_cache {
796 : tree t1;
797 : tree t2;
798 : tree composite;
799 : struct composite_cache* next;
800 : };
801 :
802 : tree
803 13796326 : composite_type_internal (tree t1, tree t2, tree cond,
804 : struct composite_cache* cache)
805 : {
806 13796326 : enum tree_code code1;
807 13796326 : enum tree_code code2;
808 13796326 : tree attributes;
809 :
810 : /* Save time if the two types are the same. */
811 :
812 13796326 : if (t1 == t2) return t1;
813 :
814 : /* If one type is nonsense, use the other. */
815 2794864 : if (t1 == error_mark_node)
816 : return t2;
817 2794860 : if (t2 == error_mark_node)
818 : return t1;
819 :
820 2794858 : code1 = TREE_CODE (t1);
821 2794858 : code2 = TREE_CODE (t2);
822 :
823 : /* Merge the attributes. */
824 2794858 : attributes = targetm.merge_type_attributes (t1, t2);
825 :
826 : /* If one is an enumerated type and the other is the compatible
827 : integer type, the composite type might be either of the two
828 : (DR#013 question 3). For consistency, use the enumerated type as
829 : the composite type. */
830 :
831 2794858 : if (code1 == ENUMERAL_TYPE
832 197 : && (code2 == INTEGER_TYPE
833 197 : || code2 == BOOLEAN_TYPE))
834 : return t1;
835 2794661 : if (code2 == ENUMERAL_TYPE
836 69 : && (code1 == INTEGER_TYPE
837 69 : || code1 == BOOLEAN_TYPE))
838 : return t2;
839 :
840 2794592 : gcc_assert (code1 == code2);
841 :
842 2794592 : switch (code1)
843 : {
844 6376 : case POINTER_TYPE:
845 : /* For two pointers, do this recursively on the target type. */
846 6376 : {
847 6376 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
848 6376 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
849 : cond, cache);
850 6376 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
851 6376 : return c_build_type_attribute_qual_variant (n, attributes,
852 6376 : TYPE_QUALS (t2));
853 : }
854 :
855 10996 : case ARRAY_TYPE:
856 10996 : {
857 10996 : tree d1 = TYPE_DOMAIN (t1);
858 10996 : tree d2 = TYPE_DOMAIN (t2);
859 :
860 : /* We should not have any type quals on arrays at all. */
861 10996 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
862 : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
863 :
864 10996 : bool d1_zero = zero_length_array_type_p (t1)
865 10996 : || flexible_array_member_type_p (t1);
866 10996 : bool d2_zero = zero_length_array_type_p (t2)
867 10996 : || flexible_array_member_type_p (t2);
868 :
869 10996 : bool d1_variable = top_array_vla_p (t1);
870 10996 : bool d2_variable = top_array_vla_p (t2);
871 :
872 10996 : bool use1 = d1 && (d2_variable || !d2 || d2_zero || !d1_variable);
873 10996 : bool use2 = d2 && (d1_variable || !d1 || d1_zero || !d2_variable);
874 :
875 : /* If the first is an unspecified size pick the other one. */
876 10996 : if (d2_variable && c_type_unspecified_p (t1))
877 : {
878 29 : gcc_assert (use1 && use2);
879 : use1 = false;
880 : }
881 :
882 : /* If both are VLAs but not unspecified and we are in the
883 : conditional operator, we create a conditional to select
884 : the size of the active branch. */
885 232 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
886 11225 : && d2_variable && !c_type_unspecified_p (t2);
887 :
888 10996 : tree td;
889 10996 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
890 : cond, cache);
891 :
892 10996 : if (!use0)
893 : {
894 : /* Save space: see if the result is identical to one of the args. */
895 10879 : if (elt == TREE_TYPE (t1) && use1)
896 10088 : return c_build_type_attribute_variant (t1, attributes);
897 791 : if (elt == TREE_TYPE (t2) && use2)
898 673 : return c_build_type_attribute_variant (t2, attributes);
899 :
900 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
901 10 : return c_build_type_attribute_variant (t1, attributes);
902 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
903 2 : return c_build_type_attribute_variant (t2, attributes);
904 :
905 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
906 : }
907 : else
908 : {
909 : /* Not used in C. */
910 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
911 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
912 :
913 117 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
914 117 : cond, TYPE_MAX_VALUE (d1),
915 117 : TYPE_MAX_VALUE (d2));
916 :
917 117 : td = build_index_type (d);
918 : }
919 :
920 : /* Merge the element types, and have a size if either arg has
921 : one. We may have qualifiers on the element types. To set
922 : up TYPE_MAIN_VARIANT correctly, we need to form the
923 : composite of the unqualified types and add the qualifiers
924 : back at the end. */
925 223 : auto quals = TYPE_QUALS (strip_array_types (elt));
926 223 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
927 :
928 223 : if ((!d1 || d1_zero) && (!d2 || d2_zero) && (d1 || d2))
929 1 : t1 = c_build_array_type_zero_size (unqual_elt);
930 : else
931 222 : t1 = c_build_array_type (unqual_elt, td);
932 :
933 : /* Check that a type which has a varying outermost dimension
934 : got marked as having a variable size. */
935 223 : bool varsize = (d1_variable && d2_variable)
936 97 : || (d1_variable && !d2)
937 309 : || (d2_variable && !d1);
938 157 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
939 :
940 223 : t1 = c_build_qualified_type (t1, quals);
941 223 : return c_build_type_attribute_variant (t1, attributes);
942 : }
943 :
944 253 : case RECORD_TYPE:
945 253 : case UNION_TYPE:
946 253 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
947 : {
948 : /* Go to the original type to get the right tag. */
949 150 : tree tag = c_type_tag (t1);
950 :
951 150 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
952 150 : gcc_checking_assert (!tag || comptypes (t1, t2));
953 :
954 : /* If a composite type for these two types is already under
955 : construction, return it. */
956 :
957 376 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
958 246 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
959 20 : return c->composite;
960 :
961 : /* Otherwise, create a new type node and link it into the cache. */
962 :
963 130 : tree n = make_node (code1);
964 130 : SET_TYPE_STRUCTURAL_EQUALITY (n);
965 130 : TYPE_NAME (n) = tag;
966 :
967 130 : struct composite_cache cache2 = { t1, t2, n, cache };
968 130 : cache = &cache2;
969 :
970 130 : tree f1 = TYPE_FIELDS (t1);
971 130 : tree f2 = TYPE_FIELDS (t2);
972 130 : tree fields = NULL_TREE;
973 :
974 321 : for (tree a = f1, b = f2; a && b;
975 191 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
976 : {
977 191 : tree ta = TREE_TYPE (a);
978 191 : tree tb = TREE_TYPE (b);
979 :
980 191 : if (DECL_C_BIT_FIELD (a))
981 : {
982 19 : ta = DECL_BIT_FIELD_TYPE (a);
983 19 : tb = DECL_BIT_FIELD_TYPE (b);
984 : }
985 :
986 191 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
987 191 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
988 :
989 191 : tree t = composite_type_internal (ta, tb, cond, cache);
990 191 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
991 :
992 191 : DECL_PACKED (f) = DECL_PACKED (a);
993 191 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
994 191 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
995 191 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
996 :
997 191 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
998 :
999 191 : finish_decl (f, input_location, NULL, NULL, NULL);
1000 :
1001 191 : if (DECL_C_BIT_FIELD (a))
1002 : {
1003 : /* This will be processed by finish_struct. */
1004 19 : SET_DECL_C_BIT_FIELD (f);
1005 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
1006 19 : tree_to_uhwi (DECL_SIZE (a)));
1007 19 : DECL_NONADDRESSABLE_P (f) = true;
1008 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
1009 : }
1010 :
1011 191 : DECL_CHAIN (f) = fields;
1012 191 : fields = f;
1013 : }
1014 :
1015 130 : fields = nreverse (fields);
1016 :
1017 : /* Setup the struct/union type. Because we inherit all variably
1018 : modified components, we can ignore the size expression. */
1019 130 : tree expr = NULL_TREE;
1020 :
1021 : /* Set TYPE_STUB_DECL for debugging symbols. */
1022 130 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
1023 : NULL_TREE, n));
1024 :
1025 130 : n = finish_struct (input_location, n, fields, attributes, NULL,
1026 : &expr);
1027 :
1028 130 : return qualify_type (n, t1);
1029 : }
1030 : /* FALLTHRU */
1031 103 : case ENUMERAL_TYPE:
1032 103 : if (attributes != NULL)
1033 : {
1034 : /* Try harder not to create a new aggregate type. */
1035 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1036 : return t1;
1037 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1038 : return t2;
1039 : }
1040 99 : return c_build_type_attribute_variant (t1, attributes);
1041 :
1042 2727667 : case FUNCTION_TYPE:
1043 : /* Function types: prefer the one that specified arg types.
1044 : If both do, merge the arg types. Also merge the return types. */
1045 2727667 : {
1046 2727667 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1047 : cond, cache);
1048 2727667 : tree p1 = TYPE_ARG_TYPES (t1);
1049 2727667 : tree p2 = TYPE_ARG_TYPES (t2);
1050 :
1051 : /* Save space: see if the result is identical to one of the args. */
1052 2727667 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1053 10797 : return c_build_functype_attribute_variant (t1, t2, attributes);
1054 2717405 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
1055 525 : return c_build_functype_attribute_variant (t2, t1, attributes);
1056 :
1057 : /* Simple way if one arg fails to specify argument types. */
1058 2716880 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
1059 : {
1060 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
1061 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
1062 9 : t1 = c_build_type_attribute_variant (t1, attributes);
1063 9 : return qualify_type (t1, t2);
1064 : }
1065 2716871 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
1066 : {
1067 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
1068 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
1069 1 : t1 = c_build_type_attribute_variant (t1, attributes);
1070 1 : return qualify_type (t1, t2);
1071 : }
1072 :
1073 : /* If both args specify argument types, we must merge the two
1074 : lists, argument by argument. */
1075 2716870 : tree newargs = NULL_TREE;
1076 2716870 : tree *endp = &newargs;
1077 :
1078 7091488 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1079 : {
1080 6922195 : if (p1 == void_list_node)
1081 : {
1082 2547577 : *endp = void_list_node;
1083 2547577 : break;
1084 : }
1085 4374618 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1086 4374618 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1087 :
1088 4374618 : gcc_assert (mv1);
1089 4374618 : gcc_assert (mv2);
1090 :
1091 4374618 : mv1 = transparent_union_replacement (mv1, mv2);
1092 4374618 : mv2 = transparent_union_replacement (mv2, mv1);
1093 :
1094 4374618 : *endp = tree_cons (NULL_TREE,
1095 : composite_type_internal (mv1, mv2, cond, cache),
1096 : NULL_TREE);
1097 :
1098 4374618 : endp = &TREE_CHAIN (*endp);
1099 : }
1100 :
1101 2716870 : t1 = c_build_function_type (valtype, newargs);
1102 2716870 : t1 = qualify_type (t1, t2);
1103 : }
1104 : /* FALLTHRU */
1105 :
1106 2766170 : default:
1107 2766170 : return c_build_type_attribute_variant (t1, attributes);
1108 : }
1109 : }
1110 :
1111 : tree
1112 6676478 : composite_type_cond (tree t1, tree t2, tree cond)
1113 : {
1114 6676478 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1115 :
1116 6676478 : struct composite_cache cache = { };
1117 6676478 : tree n = composite_type_internal (t1, t2, cond, &cache);
1118 :
1119 6676478 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1120 6676478 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1121 6676478 : return n;
1122 : }
1123 :
1124 :
1125 : tree
1126 6671995 : composite_type (tree t1, tree t2)
1127 : {
1128 6671995 : return composite_type_cond (t1, t2, NULL_TREE);
1129 : }
1130 :
1131 : /* Return the type of a conditional expression between pointers to
1132 : possibly differently qualified versions of compatible types.
1133 :
1134 : We assume that comp_target_types has already been done and returned
1135 : true; if that isn't so, this may crash. */
1136 :
1137 : static tree
1138 117098 : common_pointer_type (tree t1, tree t2, tree cond)
1139 : {
1140 117098 : tree attributes;
1141 117098 : qualifier_set target_quals;
1142 117098 : addr_space_t as1, as2, as_common;
1143 117098 : cv_qualifier quals1, quals2;
1144 :
1145 : /* Save time if the two types are the same. */
1146 :
1147 117098 : if (t1 == t2) return t1;
1148 :
1149 : /* If one type is nonsense, use the other. */
1150 4483 : if (t1 == error_mark_node)
1151 : return t2;
1152 4483 : if (t2 == error_mark_node)
1153 : return t1;
1154 :
1155 4483 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1156 : && TREE_CODE (t2) == POINTER_TYPE);
1157 :
1158 : /* Merge the attributes. */
1159 4483 : attributes = targetm.merge_type_attributes (t1, t2);
1160 :
1161 : /* Find the composite type of the target types, and combine the
1162 : qualifiers of the two types' targets. */
1163 4483 : tree pointed_to_1 = TREE_TYPE (t1);
1164 4483 : tree pointed_to_2 = TREE_TYPE (t2);
1165 4483 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1166 4483 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1167 :
1168 : /* Strip array types to get correct qualifier for pointers to arrays */
1169 4483 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1170 4483 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1171 :
1172 : /* For function types do not merge const qualifiers, but drop them
1173 : if used inconsistently. The middle-end uses these to mark const
1174 : and noreturn functions. */
1175 4483 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1176 2136 : target_quals = (quals1 & quals2);
1177 : else
1178 2347 : target_quals = (quals1 | quals2);
1179 :
1180 : /* If the two named address spaces are different, determine the common
1181 : superset address space. This is guaranteed to exist due to the
1182 : assumption that comp_target_type returned true. */
1183 4483 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1184 4483 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1185 4483 : if (!addr_space_superset (as1, as2, &as_common))
1186 0 : gcc_unreachable ();
1187 :
1188 4483 : target_quals.set_as (as_common);
1189 :
1190 4483 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1191 4483 : return c_build_type_attribute_variant (t1, attributes);
1192 : }
1193 :
1194 : /* Return TYPE, with the no_implicit_truncation attribute added if
1195 : NO_IMPLICIT_TRUNCATION. */
1196 :
1197 : static tree
1198 14532919 : c_maybe_no_implicit_truncation (tree type, bool no_implicit_truncation)
1199 : {
1200 14532919 : if (no_implicit_truncation
1201 14532919 : && !lookup_attribute ("no_implicit_truncation", TYPE_ATTRIBUTES (type)))
1202 : {
1203 184 : tree attrs = tree_cons (get_identifier ("no_implicit_truncation"),
1204 : NULL_TREE,
1205 184 : TYPE_ATTRIBUTES (type));
1206 184 : return c_build_type_attribute_variant (type, attrs);
1207 : }
1208 : else
1209 : return type;
1210 : }
1211 :
1212 : /* Return the common type for two arithmetic types under the usual
1213 : arithmetic conversions. The default conversions have already been
1214 : applied, and enumerated types converted to their compatible integer
1215 : types. The resulting type is unqualified and has no attributes,
1216 : except for no_implicit_truncation if both types are integer types and at
1217 : least one has that attribute.
1218 :
1219 : This is the type for the result of most arithmetic operations
1220 : if the operands have the given two types. */
1221 :
1222 : static tree
1223 14788017 : c_common_type (tree t1, tree t2)
1224 : {
1225 14788017 : enum tree_code code1;
1226 14788017 : enum tree_code code2;
1227 14788017 : bool no_implicit_truncation
1228 14788017 : = (INTEGRAL_TYPE_P (t1)
1229 12237167 : && INTEGRAL_TYPE_P (t2)
1230 26988190 : && (lookup_attribute ("no_implicit_truncation", TYPE_ATTRIBUTES (t1))
1231 12200067 : || lookup_attribute ("no_implicit_truncation",
1232 12200067 : TYPE_ATTRIBUTES (t2))));
1233 :
1234 : /* If one type is nonsense, use the other. */
1235 14788017 : if (t1 == error_mark_node)
1236 : return t2;
1237 14788017 : if (t2 == error_mark_node)
1238 : return t1;
1239 :
1240 14788017 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1241 38222 : t1 = TYPE_MAIN_VARIANT (t1);
1242 :
1243 14788017 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1244 49653 : t2 = TYPE_MAIN_VARIANT (t2);
1245 :
1246 14788017 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1247 : {
1248 190474 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1249 190474 : t1 = c_build_type_attribute_variant (t1, attrs);
1250 : }
1251 :
1252 14788017 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1253 : {
1254 189918 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1255 189918 : t2 = c_build_type_attribute_variant (t2, attrs);
1256 : }
1257 :
1258 : /* Save time if the two types are the same. */
1259 :
1260 14788017 : if (t1 == t2)
1261 12367799 : return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
1262 :
1263 2420218 : code1 = TREE_CODE (t1);
1264 2420218 : code2 = TREE_CODE (t2);
1265 :
1266 2420218 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1267 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1268 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1269 2420218 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1270 : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1271 : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1272 :
1273 : /* When one operand is a decimal float type, the other operand cannot be
1274 : a generic float type or a complex type. We also disallow vector types
1275 : here. */
1276 2420218 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1277 2422643 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1278 : {
1279 1081 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1280 : {
1281 3 : error ("cannot mix operands of decimal floating and vector types");
1282 3 : return error_mark_node;
1283 : }
1284 1078 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1285 : {
1286 5 : error ("cannot mix operands of decimal floating and complex types");
1287 5 : return error_mark_node;
1288 : }
1289 1073 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1290 : {
1291 16 : error ("cannot mix operands of decimal floating "
1292 : "and other floating types");
1293 16 : return error_mark_node;
1294 : }
1295 : }
1296 :
1297 : /* If one type is a vector type, return that type. (How the usual
1298 : arithmetic conversions apply to the vector types extension is not
1299 : precisely specified.) */
1300 2420194 : if (code1 == VECTOR_TYPE)
1301 : return t1;
1302 :
1303 2419902 : if (code2 == VECTOR_TYPE)
1304 : return t2;
1305 :
1306 : /* If one type is complex, form the common type of the non-complex
1307 : components, then make that complex. Use T1 or T2 if it is the
1308 : required type. */
1309 2419899 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1310 : {
1311 89393 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1312 89393 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1313 89393 : tree subtype = c_common_type (subtype1, subtype2);
1314 :
1315 89393 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1316 : return t1;
1317 59263 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1318 : return t2;
1319 3258 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1320 : {
1321 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1322 4 : TYPE_PRECISION (subtype));
1323 4 : return code1 == COMPLEX_TYPE ? t1 : t2;
1324 : }
1325 : else
1326 3254 : return build_complex_type (subtype);
1327 : }
1328 :
1329 : /* If only one is real, use it as the result. */
1330 :
1331 2330506 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1332 : return t1;
1333 :
1334 2201108 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1335 : return t2;
1336 :
1337 : /* If both are real and either are decimal floating point types, use
1338 : the decimal floating point type with the greater precision. */
1339 :
1340 2169663 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1341 : {
1342 107904 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1343 107904 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1344 : return dfloat128_type_node;
1345 : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1346 : implementation the same mode. */
1347 107355 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1348 107355 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1349 : return dfloat64x_type_node;
1350 107351 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1351 107351 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1352 : return dfloat64_type_node;
1353 106905 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1354 106905 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1355 : return dfloat32_type_node;
1356 : }
1357 :
1358 : /* Deal with fixed-point types. */
1359 2168319 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1360 : {
1361 0 : unsigned int unsignedp = 0, satp = 0;
1362 0 : scalar_mode m1, m2;
1363 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1364 :
1365 0 : m1 = SCALAR_TYPE_MODE (t1);
1366 0 : m2 = SCALAR_TYPE_MODE (t2);
1367 :
1368 : /* If one input type is saturating, the result type is saturating. */
1369 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1370 : satp = 1;
1371 :
1372 : /* If both fixed-point types are unsigned, the result type is unsigned.
1373 : When mixing fixed-point and integer types, follow the sign of the
1374 : fixed-point type.
1375 : Otherwise, the result type is signed. */
1376 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1377 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1378 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1379 0 : && TYPE_UNSIGNED (t1))
1380 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1381 0 : && TYPE_UNSIGNED (t2)))
1382 : unsignedp = 1;
1383 :
1384 : /* The result type is signed. */
1385 0 : if (unsignedp == 0)
1386 : {
1387 : /* If the input type is unsigned, we need to convert to the
1388 : signed type. */
1389 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1390 : {
1391 0 : enum mode_class mclass = (enum mode_class) 0;
1392 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1393 : mclass = MODE_FRACT;
1394 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1395 : mclass = MODE_ACCUM;
1396 : else
1397 0 : gcc_unreachable ();
1398 0 : m1 = as_a <scalar_mode>
1399 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1400 : }
1401 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1402 : {
1403 0 : enum mode_class mclass = (enum mode_class) 0;
1404 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1405 : mclass = MODE_FRACT;
1406 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1407 : mclass = MODE_ACCUM;
1408 : else
1409 0 : gcc_unreachable ();
1410 0 : m2 = as_a <scalar_mode>
1411 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1412 : }
1413 : }
1414 :
1415 0 : if (code1 == FIXED_POINT_TYPE)
1416 : {
1417 0 : fbit1 = GET_MODE_FBIT (m1);
1418 0 : ibit1 = GET_MODE_IBIT (m1);
1419 : }
1420 : else
1421 : {
1422 0 : fbit1 = 0;
1423 : /* Signed integers need to subtract one sign bit. */
1424 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1425 : }
1426 :
1427 0 : if (code2 == FIXED_POINT_TYPE)
1428 : {
1429 0 : fbit2 = GET_MODE_FBIT (m2);
1430 0 : ibit2 = GET_MODE_IBIT (m2);
1431 : }
1432 : else
1433 : {
1434 0 : fbit2 = 0;
1435 : /* Signed integers need to subtract one sign bit. */
1436 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1437 : }
1438 :
1439 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1440 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1441 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1442 : satp);
1443 : }
1444 :
1445 : /* Both real or both integers; use the one with greater precision. */
1446 :
1447 2168319 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1448 981483 : return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
1449 1186836 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1450 368521 : return c_maybe_no_implicit_truncation (t2, no_implicit_truncation);
1451 :
1452 : /* Same precision. Prefer long longs to longs to ints when the
1453 : same precision, following the C99 rules on integer type rank
1454 : (which are equivalent to the C90 rules for C90 types). */
1455 :
1456 818315 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1457 818315 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1458 89771 : return c_maybe_no_implicit_truncation (long_long_unsigned_type_node,
1459 89771 : no_implicit_truncation);
1460 :
1461 728544 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1462 728544 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1463 : {
1464 15427 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1465 373 : return c_maybe_no_implicit_truncation (long_long_unsigned_type_node,
1466 373 : no_implicit_truncation);
1467 : else
1468 15054 : return c_maybe_no_implicit_truncation (long_long_integer_type_node,
1469 15054 : no_implicit_truncation);
1470 : }
1471 :
1472 713117 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1473 713117 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1474 99646 : return c_maybe_no_implicit_truncation (long_unsigned_type_node,
1475 99646 : no_implicit_truncation);
1476 :
1477 613471 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1478 613471 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1479 : {
1480 : /* But preserve unsignedness from the other type,
1481 : since long cannot hold all the values of an unsigned int. */
1482 26711 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1483 129 : return c_maybe_no_implicit_truncation (long_unsigned_type_node,
1484 129 : no_implicit_truncation);
1485 : else
1486 26582 : return c_maybe_no_implicit_truncation (long_integer_type_node,
1487 26582 : no_implicit_truncation);
1488 : }
1489 :
1490 : /* For floating types of the same TYPE_PRECISION (which we here
1491 : assume means either the same set of values, or sets of values
1492 : neither a subset of the other, with behavior being undefined in
1493 : the latter case), follow the rules from TS 18661-3: prefer
1494 : interchange types _FloatN, then standard types long double,
1495 : double, float, then extended types _FloatNx. For extended types,
1496 : check them starting with _Float128x as that seems most consistent
1497 : in spirit with preferring long double to double; for interchange
1498 : types, also check in that order for consistency although it's not
1499 : possible for more than one of them to have the same
1500 : precision. */
1501 586760 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1502 586760 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1503 :
1504 2933151 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1505 2346575 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1506 : return FLOATN_TYPE_NODE (i);
1507 :
1508 : /* Likewise, prefer long double to double even if same size. */
1509 586576 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1510 : return long_double_type_node;
1511 :
1512 : /* Likewise, prefer double to float even if same size.
1513 : We got a couple of embedded targets with 32 bit doubles, and the
1514 : pdp11 might have 64 bit floats. */
1515 586318 : if (mv1 == double_type_node || mv2 == double_type_node)
1516 : return double_type_node;
1517 :
1518 585746 : if (mv1 == float_type_node || mv2 == float_type_node)
1519 : return float_type_node;
1520 :
1521 2334244 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1522 1750683 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1523 : return FLOATNX_TYPE_NODE (i);
1524 :
1525 583561 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1526 : {
1527 : /* Prefer any other integral types over bit-precise integer types. */
1528 33 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1529 22 : return c_maybe_no_implicit_truncation (code1 == BITINT_TYPE ? t2 : t1,
1530 20 : no_implicit_truncation);
1531 : /* If BITINT_TYPE is unsigned and the other type is signed
1532 : non-BITINT_TYPE with the same precision, the latter has higher rank.
1533 : In that case:
1534 : Otherwise, both operands are converted to the unsigned integer type
1535 : corresponding to the type of the operand with signed integer type. */
1536 15 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1537 : {
1538 12 : tree ret = c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1539 11 : return c_maybe_no_implicit_truncation (ret, no_implicit_truncation);
1540 : }
1541 : }
1542 :
1543 : /* Otherwise prefer the unsigned one. */
1544 :
1545 583530 : if (TYPE_UNSIGNED (t1))
1546 313218 : return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
1547 : else
1548 270312 : return c_maybe_no_implicit_truncation (t2, no_implicit_truncation);
1549 : }
1550 :
1551 : /* Wrapper around c_common_type that is used by c-common.cc and other
1552 : front end optimizations that remove promotions. ENUMERAL_TYPEs
1553 : are allowed here and are converted to their compatible integer types.
1554 : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1555 : preferably a non-Boolean type as the common type. */
1556 : tree
1557 104606 : common_type (tree t1, tree t2)
1558 : {
1559 104606 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1560 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1561 104606 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1562 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1563 :
1564 : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1565 104606 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1566 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1567 584 : return boolean_type_node;
1568 :
1569 : /* If either type is BOOLEAN_TYPE, then return the other. */
1570 104022 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1571 : return t2;
1572 104022 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1573 : return t1;
1574 :
1575 104022 : return c_common_type (t1, t2);
1576 : }
1577 :
1578 :
1579 :
1580 : /* Helper function for comptypes. For two compatible types, return 1
1581 : if they pass consistency checks. In particular we test that
1582 : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1583 :
1584 : static bool
1585 43710938 : comptypes_verify (tree type1, tree type2)
1586 : {
1587 43710938 : if (type1 == type2 || !type1 || !type2
1588 3646233 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1589 : return true;
1590 :
1591 3646232 : gcc_checking_assert (c_verify_type (type1));
1592 3646232 : gcc_checking_assert (c_verify_type (type2));
1593 :
1594 3646232 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1595 404378 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1596 4049101 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1597 : {
1598 : /* FIXME: check other types. */
1599 402558 : if (RECORD_OR_UNION_TYPE_P (type1)
1600 402558 : || TREE_CODE (type1) == ENUMERAL_TYPE
1601 402558 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1602 0 : return false;
1603 : }
1604 : return true;
1605 : }
1606 :
1607 : struct comptypes_data {
1608 :
1609 : /* output */
1610 : bool enum_and_int_p;
1611 : bool different_types_p;
1612 : bool warning_needed;
1613 :
1614 : /* context */
1615 : bool anon_field;
1616 : bool pointedto;
1617 :
1618 : /* configuration */
1619 : bool equiv;
1620 : bool ignore_promoting_args;
1621 :
1622 : const struct tagged_tu_seen_cache* cache;
1623 : };
1624 :
1625 :
1626 : /* Helper function for composite_type. This function ignores when the
1627 : function type of an old-style declaration is incompatible with a type
1628 : of a declaration with prototype because some are arguments are not
1629 : self-promoting. This is ignored only for function types but not
1630 : ignored in a nested context. */
1631 :
1632 : static bool
1633 20029434 : comptypes_check_for_composite (tree t1, tree t2)
1634 : {
1635 20029434 : struct comptypes_data data = { };
1636 20029434 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1637 20029434 : return comptypes_internal (t1, t2, &data);
1638 : }
1639 :
1640 :
1641 : /* C implementation of compatible_types_for_indirection_note_p. */
1642 :
1643 : bool
1644 762 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1645 : {
1646 762 : return comptypes (type1, type2);
1647 : }
1648 :
1649 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1650 : or various other operations. */
1651 :
1652 : bool
1653 57304200 : comptypes (tree type1, tree type2)
1654 : {
1655 57304200 : struct comptypes_data data = { };
1656 57304200 : bool ret = comptypes_internal (type1, type2, &data);
1657 :
1658 57304200 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1659 :
1660 57304200 : return ret;
1661 : }
1662 :
1663 :
1664 : /* Like comptypes, but it returns non-zero only for identical
1665 : types. */
1666 :
1667 : bool
1668 48112 : comptypes_same_p (tree type1, tree type2)
1669 : {
1670 48112 : struct comptypes_data data = { };
1671 48112 : bool ret = comptypes_internal (type1, type2, &data);
1672 :
1673 48112 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1674 :
1675 48112 : if (data.different_types_p)
1676 163 : return false;
1677 :
1678 : return ret;
1679 : }
1680 :
1681 :
1682 : /* Like comptypes, but if it returns true because enum and int are
1683 : compatible, it sets *ENUM_AND_INT_P to true. */
1684 :
1685 : bool
1686 5255477 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1687 : {
1688 5255477 : struct comptypes_data data = { };
1689 5255477 : bool ret = comptypes_internal (type1, type2, &data);
1690 5255477 : *enum_and_int_p = data.enum_and_int_p;
1691 :
1692 5255477 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1693 :
1694 5255477 : return ret;
1695 : }
1696 :
1697 :
1698 :
1699 : /* Like comptypes, but if it returns true for struct and union types
1700 : considered equivalent for aliasing purposes, i.e. for setting
1701 : TYPE_CANONICAL after completing a struct or union.
1702 :
1703 : This function must return false only for types which are not
1704 : compatible according to C language semantics (cf. comptypes),
1705 : otherwise the middle-end would make incorrect aliasing decisions.
1706 : It may return true for some similar types that are not compatible
1707 : according to those stricter rules.
1708 :
1709 : In particular, we ignore size expression in arrays so that the
1710 : following structs are in the same equivalence class:
1711 :
1712 : struct foo { char (*buf)[]; };
1713 : struct foo { char (*buf)[3]; };
1714 : struct foo { char (*buf)[4]; };
1715 :
1716 : We also treat unions / structs with members which are pointers to
1717 : structures or unions with the same tag as equivalent (if they are not
1718 : incompatible for other reasons). Although incomplete structure
1719 : or union types are not compatible to any other type, they may become
1720 : compatible to different types when completed. To avoid having to update
1721 : TYPE_CANONICAL at this point, we only consider the tag when forming
1722 : the equivalence classes. For example, the following types with tag
1723 : 'foo' are all considered equivalent:
1724 :
1725 : struct bar;
1726 : struct foo { struct bar *x };
1727 : struct foo { struct bar { int a; } *x };
1728 : struct foo { struct bar { char b; } *x }; */
1729 :
1730 : bool
1731 14843336 : comptypes_equiv_p (tree type1, tree type2)
1732 : {
1733 14843336 : struct comptypes_data data = { };
1734 14843336 : data.equiv = true;
1735 14843336 : bool ret = comptypes_internal (type1, type2, &data);
1736 :
1737 : /* check that different equivance classes are assigned only
1738 : to types that are not compatible. */
1739 14843336 : gcc_checking_assert (ret || !comptypes (type1, type2));
1740 :
1741 14843336 : return ret;
1742 : }
1743 :
1744 :
1745 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1746 : or various other operations. If they are compatible but a warning may
1747 : be needed if you use them together, 'warning_needed' in DATA is set.
1748 : If one type is an enum and the other a compatible integer type, then
1749 : this sets 'enum_and_int_p' in DATA to true (it is never set to
1750 : false). If the types are compatible but different enough not to be
1751 : permitted in C11 typedef redeclarations, then this sets
1752 : 'different_types_p' in DATA to true; it is never set to
1753 : false, but may or may not be set if the types are incompatible.
1754 : If two functions types are not compatible only because one is
1755 : an old-style definition that does not have self-promoting arguments,
1756 : then this can be ignored by setting 'ignore_promoting_args_p'.
1757 : For 'equiv' we can compute equivalency classes (see above). */
1758 :
1759 : static bool
1760 137496548 : comptypes_internal (const_tree type1, const_tree type2,
1761 : struct comptypes_data *data)
1762 : {
1763 137781403 : const_tree t1 = type1;
1764 137781403 : const_tree t2 = type2;
1765 :
1766 : /* Suppress errors caused by previously reported errors. */
1767 :
1768 137781403 : if (t1 == t2 || !t1 || !t2
1769 46162294 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1770 : return true;
1771 :
1772 : /* Qualifiers must match. C99 6.7.3p9 */
1773 :
1774 46162291 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1775 : return false;
1776 :
1777 : /* Enumerated types are compatible with integer types, but this is
1778 : not transitive: two enumerated types in the same translation unit
1779 : are compatible with each other only if they are the same type. */
1780 :
1781 46122120 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1782 1021 : && COMPLETE_TYPE_P (t1)
1783 46123097 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1784 : {
1785 849 : t1 = ENUM_UNDERLYING_TYPE (t1);
1786 849 : if (TREE_CODE (t2) != VOID_TYPE)
1787 : {
1788 845 : data->enum_and_int_p = true;
1789 845 : data->different_types_p = true;
1790 : }
1791 : }
1792 46121271 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1793 4953 : && COMPLETE_TYPE_P (t2)
1794 46126218 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1795 : {
1796 4819 : t2 = ENUM_UNDERLYING_TYPE (t2);
1797 4819 : if (TREE_CODE (t1) != VOID_TYPE)
1798 : {
1799 3920 : data->enum_and_int_p = true;
1800 3920 : data->different_types_p = true;
1801 : }
1802 : }
1803 :
1804 46122120 : if (t1 == t2)
1805 : return true;
1806 :
1807 : /* Different classes of types can't be compatible. */
1808 :
1809 46120544 : if (TREE_CODE (t1) != TREE_CODE (t2))
1810 : return false;
1811 :
1812 : /* Allow for two different type nodes which have essentially the same
1813 : definition. Note that we already checked for equality of the type
1814 : qualifiers (just above). */
1815 :
1816 38028315 : if (TREE_CODE (t1) != ARRAY_TYPE
1817 38028315 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1818 : return true;
1819 :
1820 37658572 : int attrval;
1821 :
1822 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1823 37658572 : if (!(attrval = comp_type_attributes (t1, t2)))
1824 : return false;
1825 :
1826 37658179 : if (2 == attrval)
1827 336 : data->warning_needed = true;
1828 :
1829 37658179 : switch (TREE_CODE (t1))
1830 : {
1831 3398087 : case INTEGER_TYPE:
1832 3398087 : case FIXED_POINT_TYPE:
1833 3398087 : case REAL_TYPE:
1834 3398087 : case BITINT_TYPE:
1835 : /* With these nodes, we can't determine type equivalence by
1836 : looking at what is stored in the nodes themselves, because
1837 : two nodes might have different TYPE_MAIN_VARIANTs but still
1838 : represent the same type. For example, wchar_t and int could
1839 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1840 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1841 : and are distinct types. On the other hand, int and the
1842 : following typedef
1843 :
1844 : typedef int INT __attribute((may_alias));
1845 :
1846 : have identical properties, different TYPE_MAIN_VARIANTs, but
1847 : represent the same type. The canonical type system keeps
1848 : track of equivalence in this case, so we fall back on it. */
1849 3398087 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1850 :
1851 284855 : case POINTER_TYPE:
1852 : /* Do not remove mode information. */
1853 284855 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1854 : return false;
1855 284855 : data->pointedto = true;
1856 284855 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1857 :
1858 9493654 : case FUNCTION_TYPE:
1859 9493654 : return function_types_compatible_p (t1, t2, data);
1860 :
1861 156052 : case ARRAY_TYPE:
1862 156052 : {
1863 156052 : tree d1 = TYPE_DOMAIN (t1);
1864 156052 : tree d2 = TYPE_DOMAIN (t2);
1865 :
1866 : /* Target types must match incl. qualifiers. */
1867 156052 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1868 : return false;
1869 :
1870 120939 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1871 6353 : data->different_types_p = true;
1872 : /* Ignore size mismatches when forming equivalence classes. */
1873 120939 : if (data->equiv)
1874 : return true;
1875 : /* Sizes must match unless one is missing or variable. */
1876 50410 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1877 : return true;
1878 :
1879 34135 : bool d1_variable = top_array_vla_p (t1);
1880 34135 : bool d2_variable = top_array_vla_p (t2);
1881 :
1882 34135 : if (d1_variable != d2_variable)
1883 943 : data->different_types_p = true;
1884 34135 : if (d1_variable || d2_variable)
1885 : return true;
1886 :
1887 4035 : bool d1_zero = zero_length_array_type_p (t1)
1888 4035 : || flexible_array_member_type_p (t1);
1889 :
1890 4035 : bool d2_zero = zero_length_array_type_p (t2)
1891 4035 : || flexible_array_member_type_p (t2);
1892 :
1893 93 : if (d1_zero && d2_zero)
1894 : return true;
1895 4035 : if (d1_zero || d2_zero)
1896 : return false;
1897 :
1898 3939 : if (!tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1899 3939 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1900 3939 : return false;
1901 :
1902 : return true;
1903 : }
1904 :
1905 23782930 : case ENUMERAL_TYPE:
1906 23782930 : case RECORD_TYPE:
1907 23782930 : case UNION_TYPE:
1908 :
1909 23782930 : if (!flag_isoc23)
1910 : return false;
1911 :
1912 23782883 : return tagged_types_tu_compatible_p (t1, t2, data);
1913 :
1914 541796 : case VECTOR_TYPE:
1915 1083592 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1916 541796 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1917 :
1918 : default:
1919 : return false;
1920 : }
1921 : gcc_unreachable ();
1922 : }
1923 :
1924 : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1925 : their qualifiers, except for named address spaces. If the pointers point to
1926 : different named addresses, then we must determine if one address space is a
1927 : subset of the other. */
1928 :
1929 : static bool
1930 1786027 : comp_target_types (location_t location, tree ttl, tree ttr)
1931 : {
1932 1786027 : tree mvl = TREE_TYPE (ttl);
1933 1786027 : tree mvr = TREE_TYPE (ttr);
1934 1786027 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1935 1786027 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1936 1786027 : addr_space_t as_common;
1937 :
1938 : /* Fail if pointers point to incompatible address spaces. */
1939 1786027 : if (!addr_space_superset (asl, asr, &as_common))
1940 : return 0;
1941 :
1942 : /* Qualifiers on element types of array types that are
1943 : pointer targets are also removed. */
1944 1786027 : struct comptypes_data data = { };
1945 1786027 : if (!comptypes_internal (remove_qualifiers (mvl),
1946 1786027 : remove_qualifiers (mvr), &data))
1947 : return false;
1948 :
1949 : /* For pedantic use comptypes on arrays before removing
1950 : qualifiers on the element type. */
1951 1684503 : if (TREE_CODE (mvl) == ARRAY_TYPE
1952 1892 : && TREE_CODE (mvr) == ARRAY_TYPE
1953 1686395 : && !comptypes (mvl, mvr))
1954 191 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays "
1955 : "with different qualifiers in ISO C "
1956 : "before C23");
1957 :
1958 1684503 : if (data.warning_needed)
1959 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1960 :
1961 1684503 : if (data.enum_and_int_p && warn_cxx_compat)
1962 2 : warning_at (location, OPT_Wc___compat,
1963 : "pointer target types incompatible in C++");
1964 :
1965 : return true;
1966 : }
1967 :
1968 : /* Subroutines of `comptypes'. */
1969 :
1970 : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1971 : compatible. The two types are not the same (which has been
1972 : checked earlier in comptypes_internal). */
1973 :
1974 : static bool
1975 23782883 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1976 : struct comptypes_data *data)
1977 : {
1978 23782883 : tree s1, s2;
1979 :
1980 23782883 : if (c_type_tag (t1) != c_type_tag (t2))
1981 : return false;
1982 :
1983 : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1984 : structs with the same tag as equivalent, but only when they are targets
1985 : of pointers inside other structs. */
1986 19549068 : if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1987 : return true;
1988 :
1989 : /* Different types without tag are incompatible except as an anonymous
1990 : field or when forming equivalence classes for TYPE_CANONICAL. */
1991 19549006 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1992 : return false;
1993 :
1994 14336779 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1995 14335716 : data->different_types_p = true;
1996 :
1997 : /* Incomplete types are incompatible inside a TU. */
1998 14336779 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1999 : return false;
2000 :
2001 14336769 : if (ENUMERAL_TYPE != TREE_CODE (t1)
2002 14336769 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
2003 14336712 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
2004 : return false;
2005 :
2006 14336759 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
2007 145340 : data->different_types_p = true;
2008 :
2009 : /* For types already being looked at in some active
2010 : invocation of this function, assume compatibility.
2011 : The cache is built as a linked list on the stack
2012 : with the head of the list passed downwards. */
2013 14336759 : for (const struct tagged_tu_seen_cache *t = data->cache;
2014 14346452 : t != NULL; t = t->next)
2015 9988 : if (t->t1 == t1 && t->t2 == t2)
2016 : return true;
2017 :
2018 14336464 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
2019 :
2020 14336464 : switch (TREE_CODE (t1))
2021 : {
2022 57 : case ENUMERAL_TYPE:
2023 57 : {
2024 57 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
2025 : return false;
2026 :
2027 : /* Speed up the case where the type values are in the same order. */
2028 53 : tree tv1 = TYPE_VALUES (t1);
2029 53 : tree tv2 = TYPE_VALUES (t2);
2030 :
2031 53 : if (tv1 == tv2)
2032 : return true;
2033 :
2034 103 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
2035 : {
2036 62 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
2037 : break;
2038 :
2039 55 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
2040 55 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
2041 : break;
2042 : }
2043 :
2044 53 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
2045 : return true;
2046 :
2047 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
2048 : return false;
2049 :
2050 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
2051 : return false;
2052 :
2053 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
2054 : {
2055 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
2056 :
2057 16 : if (s2 == NULL
2058 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
2059 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
2060 : return false;
2061 : }
2062 :
2063 : return true;
2064 : }
2065 :
2066 14336407 : case UNION_TYPE:
2067 14336407 : case RECORD_TYPE:
2068 :
2069 14336407 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2070 : return false;
2071 :
2072 12359778 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2073 13885203 : s1 && s2;
2074 1525425 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2075 : {
2076 13818807 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2077 13818807 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2078 :
2079 13818807 : tree ft1 = TREE_TYPE (s1);
2080 13818807 : tree ft2 = TREE_TYPE (s2);
2081 :
2082 13818807 : if (DECL_NAME (s1) != DECL_NAME (s2))
2083 : return false;
2084 :
2085 12150311 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2086 : return false;
2087 :
2088 3240548 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2089 : return false;
2090 :
2091 3240528 : if (DECL_C_BIT_FIELD (s1))
2092 : {
2093 541 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2094 : return false;
2095 :
2096 345 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2097 345 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2098 : }
2099 :
2100 3240332 : if (!ft1 || !ft2)
2101 : return false;
2102 :
2103 3240332 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2104 : return false;
2105 :
2106 3240331 : data->anon_field = !DECL_NAME (s1);
2107 3240331 : data->pointedto = false;
2108 :
2109 3240331 : const struct tagged_tu_seen_cache *cache = data->cache;
2110 3240331 : data->cache = &entry;
2111 3240331 : bool ret = comptypes_internal (ft1, ft2, data);
2112 3240331 : data->cache = cache;
2113 3240331 : if (!ret)
2114 : return false;
2115 :
2116 1595842 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2117 1595842 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2118 :
2119 1595842 : if (data->equiv
2120 1001558 : && st1 && TREE_CODE (st1) == INTEGER_CST
2121 1001076 : && st2 && TREE_CODE (st2) == INTEGER_CST
2122 2596912 : && !tree_int_cst_equal (st1, st2))
2123 : return false;
2124 :
2125 1525459 : tree counted_by1 = lookup_attribute ("counted_by",
2126 1525459 : DECL_ATTRIBUTES (s1));
2127 1525459 : tree counted_by2 = lookup_attribute ("counted_by",
2128 1525459 : DECL_ATTRIBUTES (s2));
2129 : /* If there is no counted_by attribute for both fields. */
2130 1525459 : if (!counted_by1 && !counted_by2)
2131 1525395 : continue;
2132 :
2133 : /* If only one field has counted_by attribute. */
2134 64 : if ((counted_by1 && !counted_by2)
2135 64 : || (!counted_by1 && counted_by2))
2136 : return false;
2137 :
2138 : /* Now both s1 and s2 have counted_by attributes, check
2139 : whether they are the same. */
2140 :
2141 52 : tree counted_by_field1
2142 52 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2143 52 : tree counted_by_field2
2144 52 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2145 :
2146 52 : gcc_assert (counted_by_field1 && counted_by_field2);
2147 :
2148 94 : while (TREE_CHAIN (counted_by_field1))
2149 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2150 86 : while (TREE_CHAIN (counted_by_field2))
2151 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2152 :
2153 52 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2154 52 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2155 : return false;
2156 : }
2157 : return true;
2158 :
2159 0 : default:
2160 0 : gcc_unreachable ();
2161 : }
2162 : }
2163 :
2164 : /* Return true if two function types F1 and F2 are compatible.
2165 : If either type specifies no argument types,
2166 : the other must specify a fixed number of self-promoting arg types.
2167 : Otherwise, if one type specifies only the number of arguments,
2168 : the other must specify that number of self-promoting arg types.
2169 : Otherwise, the argument types must match. */
2170 :
2171 : static bool
2172 9493654 : function_types_compatible_p (const_tree f1, const_tree f2,
2173 : struct comptypes_data *data)
2174 : {
2175 9493654 : tree ret1 = TREE_TYPE (f1);
2176 9493654 : tree ret2 = TREE_TYPE (f2);
2177 :
2178 : /* 'volatile' qualifiers on a function's return type used to mean
2179 : the function is noreturn. */
2180 9493654 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2181 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2182 9493654 : if (TYPE_VOLATILE (ret1))
2183 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2184 15 : TYPE_QUALS (ret1)
2185 : .without (TYPE_QUAL_VOLATILE));
2186 9493654 : if (TYPE_VOLATILE (ret2))
2187 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2188 18 : TYPE_QUALS (ret2)
2189 : .without (TYPE_QUAL_VOLATILE));
2190 :
2191 9493654 : bool ignore_pargs = data->ignore_promoting_args;
2192 9493654 : data->ignore_promoting_args = false;
2193 :
2194 9493654 : if (!comptypes_internal (ret1, ret2, data))
2195 : return false;
2196 :
2197 9491065 : data->ignore_promoting_args = ignore_pargs;
2198 :
2199 9491065 : tree args1 = TYPE_ARG_TYPES (f1);
2200 9491065 : tree args2 = TYPE_ARG_TYPES (f2);
2201 :
2202 9491065 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2203 45329 : data->different_types_p = true;
2204 :
2205 : /* An unspecified parmlist matches any specified parmlist
2206 : whose argument types don't need default promotions. */
2207 :
2208 9491065 : if (args1 == NULL_TREE)
2209 : {
2210 23805 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2211 : return false;
2212 23802 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2213 : return false;
2214 23330 : data->ignore_promoting_args = false;
2215 : /* If one of these types comes from a non-prototype fn definition,
2216 : compare that with the other type's arglist.
2217 : If they don't match, ask for a warning (but no error). */
2218 23330 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2219 23330 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2220 12 : data->warning_needed = true;
2221 : return true;
2222 : }
2223 9467260 : if (args2 == NULL_TREE)
2224 : {
2225 21606 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2226 : return false;
2227 21604 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2228 : return false;
2229 21543 : data->ignore_promoting_args = false;
2230 21543 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2231 21543 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2232 1 : data->warning_needed = true;
2233 : return true;
2234 : }
2235 :
2236 : /* Both types have argument lists: compare them and propagate results. */
2237 9445654 : return type_lists_compatible_p (args1, args2, data);
2238 : }
2239 :
2240 : /* Check two lists of types for compatibility, returning false for
2241 : incompatible, true for compatible. */
2242 :
2243 : static bool
2244 9445730 : type_lists_compatible_p (const_tree args1, const_tree args2,
2245 : struct comptypes_data *data)
2246 : {
2247 59142748 : while (1)
2248 : {
2249 34294239 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2250 : return true;
2251 : /* If one list is shorter than the other,
2252 : they fail to match. */
2253 24994868 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2254 : return false;
2255 24994861 : tree a1 = TREE_VALUE (args1);
2256 24994861 : tree a2 = TREE_VALUE (args2);
2257 24994861 : tree mv1 = remove_qualifiers (a1);
2258 24994861 : tree mv2 = remove_qualifiers (a2);
2259 :
2260 24994861 : gcc_assert (mv2);
2261 24994861 : gcc_assert (mv2);
2262 :
2263 : /* If one of the lists has an error marker, ignore this arg. */
2264 24994861 : if (TREE_CODE (a1) == ERROR_MARK
2265 24994857 : || TREE_CODE (a2) == ERROR_MARK)
2266 : ;
2267 24994841 : else if (!comptypes_internal (mv1, mv2, data))
2268 : {
2269 146583 : data->different_types_p = true;
2270 : /* Allow wait (union {union wait *u; int *i} *)
2271 : and wait (union wait *) to be compatible. */
2272 146583 : if (TREE_CODE (a1) == UNION_TYPE
2273 130 : && (TYPE_NAME (a1) == NULL_TREE
2274 98 : || TYPE_TRANSPARENT_AGGR (a1))
2275 130 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2276 146713 : && tree_int_cst_equal (TYPE_SIZE (a1),
2277 130 : TYPE_SIZE (a2)))
2278 : {
2279 130 : tree memb;
2280 130 : for (memb = TYPE_FIELDS (a1);
2281 166 : memb; memb = DECL_CHAIN (memb))
2282 : {
2283 164 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2284 164 : if (comptypes_internal (mv3, mv2, data))
2285 : break;
2286 : }
2287 : if (memb == NULL_TREE)
2288 : return false;
2289 : }
2290 146453 : else if (TREE_CODE (a2) == UNION_TYPE
2291 105 : && (TYPE_NAME (a2) == NULL_TREE
2292 74 : || TYPE_TRANSPARENT_AGGR (a2))
2293 105 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2294 146558 : && tree_int_cst_equal (TYPE_SIZE (a2),
2295 105 : TYPE_SIZE (a1)))
2296 : {
2297 105 : tree memb;
2298 105 : for (memb = TYPE_FIELDS (a2);
2299 133 : memb; memb = DECL_CHAIN (memb))
2300 : {
2301 131 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2302 131 : if (comptypes_internal (mv3, mv1, data))
2303 : break;
2304 : }
2305 : if (memb == NULL_TREE)
2306 : return false;
2307 : }
2308 : else
2309 : return false;
2310 : }
2311 :
2312 24848509 : args1 = TREE_CHAIN (args1);
2313 24848509 : args2 = TREE_CHAIN (args2);
2314 24848509 : }
2315 : }
2316 :
2317 : /* Compute the size to increment a pointer by. When a function type or void
2318 : type or incomplete type is passed, size_one_node is returned.
2319 : This function does not emit any diagnostics; the caller is responsible
2320 : for that. */
2321 :
2322 : static tree
2323 237254 : c_size_in_bytes (const_tree type)
2324 : {
2325 237254 : enum tree_code code = TREE_CODE (type);
2326 :
2327 237254 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2328 237254 : || !COMPLETE_TYPE_P (type))
2329 195 : return size_one_node;
2330 :
2331 : /* Convert in case a char is more than one unit. */
2332 237059 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2333 237059 : size_int (TYPE_PRECISION (char_type_node)
2334 : / BITS_PER_UNIT));
2335 : }
2336 :
2337 : /* Return either DECL or its known constant value (if it has one). */
2338 :
2339 : tree
2340 14545935 : decl_constant_value_1 (tree decl, bool in_init)
2341 : {
2342 14545935 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2343 14545935 : TREE_CODE (decl) != PARM_DECL
2344 14545935 : && !TREE_THIS_VOLATILE (decl)
2345 14198113 : && TREE_READONLY (decl)
2346 39379 : && DECL_INITIAL (decl) != NULL_TREE
2347 38365 : && !error_operand_p (DECL_INITIAL (decl))
2348 : /* This is invalid if initial value is not constant.
2349 : If it has either a function call, a memory reference,
2350 : or a variable, then re-evaluating it could give different results. */
2351 38356 : && TREE_CONSTANT (DECL_INITIAL (decl))
2352 : /* Check for cases where this is sub-optimal, even though valid. */
2353 14570982 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2354 18078 : return DECL_INITIAL (decl);
2355 : return decl;
2356 : }
2357 :
2358 : /* Return either DECL or its known constant value (if it has one).
2359 : Like the above, but always return decl outside of functions. */
2360 :
2361 : tree
2362 14545673 : decl_constant_value (tree decl)
2363 : {
2364 : /* Don't change a variable array bound or initial value to a constant
2365 : in a place where a variable is invalid. */
2366 14545673 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2367 : }
2368 :
2369 : /* Convert the array expression EXP to a pointer. */
2370 : static tree
2371 834878 : array_to_pointer_conversion (location_t loc, tree exp)
2372 : {
2373 834878 : tree orig_exp = exp;
2374 834878 : tree type = TREE_TYPE (exp);
2375 834878 : tree adr;
2376 834878 : tree restype = TREE_TYPE (type);
2377 834878 : tree ptrtype;
2378 :
2379 834878 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2380 :
2381 834878 : STRIP_TYPE_NOPS (exp);
2382 :
2383 834878 : copy_warning (exp, orig_exp);
2384 :
2385 834878 : ptrtype = c_build_pointer_type (restype);
2386 :
2387 834878 : if (INDIRECT_REF_P (exp))
2388 2916 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2389 :
2390 : /* In C++ array compound literals are temporary objects unless they are
2391 : const or appear in namespace scope, so they are destroyed too soon
2392 : to use them for much of anything (c++/53220). */
2393 831962 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2394 : {
2395 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2396 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2397 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2398 : "converting an array compound literal to a pointer "
2399 : "leads to a dangling pointer in C++");
2400 : }
2401 :
2402 831962 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2403 831962 : return convert (ptrtype, adr);
2404 : }
2405 :
2406 : /* Convert the function expression EXP to a pointer. */
2407 : static tree
2408 52640335 : function_to_pointer_conversion (location_t loc, tree exp)
2409 : {
2410 52640335 : tree orig_exp = exp;
2411 :
2412 52640335 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2413 :
2414 52640335 : STRIP_TYPE_NOPS (exp);
2415 :
2416 52640335 : copy_warning (exp, orig_exp);
2417 :
2418 52640335 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2419 :
2420 : /* If the function is known to not require a non-local
2421 : context, make sure no trampoline is generated. */
2422 52640335 : if (TREE_CODE (exp) == FUNCTION_DECL
2423 52640335 : && !DECL_STATIC_CHAIN (exp))
2424 52624169 : TREE_NO_TRAMPOLINE (exp2) = 1;
2425 :
2426 52640335 : return exp2;
2427 : }
2428 :
2429 : /* Mark EXP as read, not just set, for set but not used -Wunused
2430 : warning purposes. */
2431 :
2432 : void
2433 509050796 : mark_exp_read (tree exp)
2434 : {
2435 640107946 : switch (TREE_CODE (exp))
2436 : {
2437 209858030 : case VAR_DECL:
2438 209858030 : case PARM_DECL:
2439 209858030 : DECL_READ_P (exp) = 1;
2440 209858030 : break;
2441 20496698 : CASE_CONVERT:
2442 20496698 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2443 3808 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2444 : {
2445 : case PREINCREMENT_EXPR:
2446 : case PREDECREMENT_EXPR:
2447 : case POSTINCREMENT_EXPR:
2448 : case POSTDECREMENT_EXPR:
2449 : return;
2450 : default:
2451 : break;
2452 : }
2453 : /* FALLTHRU */
2454 128285002 : case ARRAY_REF:
2455 128285002 : case COMPONENT_REF:
2456 128285002 : case MODIFY_EXPR:
2457 128285002 : case REALPART_EXPR:
2458 128285002 : case IMAGPART_EXPR:
2459 128285002 : case ADDR_EXPR:
2460 128285002 : case VIEW_CONVERT_EXPR:
2461 128285002 : case PREINCREMENT_EXPR:
2462 128285002 : case PREDECREMENT_EXPR:
2463 128285002 : case POSTINCREMENT_EXPR:
2464 128285002 : case POSTDECREMENT_EXPR:
2465 128285002 : mark_exp_read (TREE_OPERAND (exp, 0));
2466 128285002 : break;
2467 236394 : case COMPOUND_EXPR:
2468 : /* Pattern match what build_atomic_assign produces with modifycode
2469 : NOP_EXPR. */
2470 236394 : if (VAR_P (TREE_OPERAND (exp, 1))
2471 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2472 263537 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2473 : {
2474 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2475 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2476 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2477 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2478 4410 : && TREE_CODE (t2) == CALL_EXPR)
2479 : {
2480 2200 : tree fndecl = get_callee_fndecl (t2);
2481 2200 : tree arg = NULL_TREE;
2482 2200 : if (fndecl
2483 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2484 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2485 4400 : && call_expr_nargs (t2) >= 2)
2486 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2487 : {
2488 130 : case BUILT_IN_ATOMIC_STORE:
2489 130 : arg = CALL_EXPR_ARG (t2, 1);
2490 130 : break;
2491 2070 : case BUILT_IN_ATOMIC_STORE_1:
2492 2070 : case BUILT_IN_ATOMIC_STORE_2:
2493 2070 : case BUILT_IN_ATOMIC_STORE_4:
2494 2070 : case BUILT_IN_ATOMIC_STORE_8:
2495 2070 : case BUILT_IN_ATOMIC_STORE_16:
2496 2070 : arg = CALL_EXPR_ARG (t2, 0);
2497 2070 : break;
2498 : default:
2499 : break;
2500 : }
2501 2200 : if (arg)
2502 : {
2503 2200 : STRIP_NOPS (arg);
2504 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2505 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2506 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2507 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2508 : }
2509 : }
2510 : }
2511 : /* FALLTHRU */
2512 2771324 : case C_MAYBE_CONST_EXPR:
2513 2771324 : mark_exp_read (TREE_OPERAND (exp, 1));
2514 2771324 : break;
2515 610 : case OMP_ARRAY_SECTION:
2516 610 : mark_exp_read (TREE_OPERAND (exp, 0));
2517 610 : if (TREE_OPERAND (exp, 1))
2518 388 : mark_exp_read (TREE_OPERAND (exp, 1));
2519 610 : if (TREE_OPERAND (exp, 2))
2520 575 : mark_exp_read (TREE_OPERAND (exp, 2));
2521 : break;
2522 90095954 : case CALL_EXPR:
2523 90095954 : if (is_access_with_size_p (exp))
2524 231 : mark_exp_read (get_ref_from_access_with_size (exp));
2525 : break;
2526 3554275 : case INDIRECT_REF:
2527 3554275 : if (is_access_with_size_p (TREE_OPERAND (exp, 0)))
2528 18 : mark_exp_read (TREE_OPERAND (exp, 0));
2529 : break;
2530 : default:
2531 : break;
2532 : }
2533 : }
2534 :
2535 : /* Perform the default conversion of arrays and functions to pointers.
2536 : Return the result of converting EXP. For any other expression, just
2537 : return EXP.
2538 :
2539 : LOC is the location of the expression. */
2540 :
2541 : struct c_expr
2542 375475830 : default_function_array_conversion (location_t loc, struct c_expr exp)
2543 : {
2544 375475830 : tree orig_exp = exp.value;
2545 375475830 : tree type = TREE_TYPE (exp.value);
2546 375475830 : enum tree_code code = TREE_CODE (type);
2547 :
2548 375475830 : switch (code)
2549 : {
2550 : case ARRAY_TYPE:
2551 : {
2552 : bool not_lvalue = false;
2553 : bool lvalue_array_p;
2554 :
2555 787926 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2556 787926 : || CONVERT_EXPR_P (exp.value))
2557 787926 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2558 : {
2559 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2560 0 : not_lvalue = true;
2561 0 : exp.value = TREE_OPERAND (exp.value, 0);
2562 : }
2563 :
2564 787926 : copy_warning (exp.value, orig_exp);
2565 :
2566 787926 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2567 787926 : if (!flag_isoc99 && !lvalue_array_p)
2568 : {
2569 : /* Before C99, non-lvalue arrays do not decay to pointers.
2570 : Normally, using such an array would be invalid; but it can
2571 : be used correctly inside sizeof or as a statement expression.
2572 : Thus, do not give an error here; an error will result later. */
2573 109 : return exp;
2574 : }
2575 :
2576 787817 : exp.value = array_to_pointer_conversion (loc, exp.value);
2577 : }
2578 787817 : break;
2579 763020 : case FUNCTION_TYPE:
2580 763020 : exp.value = function_to_pointer_conversion (loc, exp.value);
2581 763020 : break;
2582 : default:
2583 : break;
2584 : }
2585 :
2586 375475721 : return exp;
2587 : }
2588 :
2589 : struct c_expr
2590 12951 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2591 : {
2592 12951 : mark_exp_read (exp.value);
2593 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2594 12951 : if (TREE_CODE (exp.value) == COMPONENT_REF
2595 12951 : && handle_counted_by_p (exp.value))
2596 4 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2597 12951 : return default_function_array_conversion (loc, exp);
2598 : }
2599 :
2600 : /* Return whether EXPR should be treated as an atomic lvalue for the
2601 : purposes of load and store handling. */
2602 :
2603 : static bool
2604 376671949 : really_atomic_lvalue (tree expr)
2605 : {
2606 376671949 : if (error_operand_p (expr))
2607 : return false;
2608 376664247 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2609 : return false;
2610 57413 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2611 : return false;
2612 57410 : if (!lvalue_p (expr))
2613 : return false;
2614 :
2615 : /* Ignore _Atomic on register variables, since their addresses can't
2616 : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2617 : sequences wouldn't work. Ignore _Atomic on structures containing
2618 : bit-fields, since accessing elements of atomic structures or
2619 : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2620 : it's undefined at translation time or execution time, and the
2621 : normal atomic sequences again wouldn't work. */
2622 57536 : while (handled_component_p (expr))
2623 : {
2624 128 : if (TREE_CODE (expr) == COMPONENT_REF
2625 128 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2626 : return false;
2627 128 : expr = TREE_OPERAND (expr, 0);
2628 : }
2629 57408 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2630 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2631 112623 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2632 7 : return false;
2633 : return true;
2634 : }
2635 :
2636 : /* If EXPR is a named constant (C23) derived from a constexpr variable
2637 : - that is, a reference to such a variable, or a member extracted by
2638 : a sequence of structure and union (but not array) member accesses
2639 : (where union member accesses must access the same member as
2640 : initialized) - then return the corresponding initializer;
2641 : otherwise, return NULL_TREE. */
2642 :
2643 : static tree
2644 373360086 : maybe_get_constexpr_init (tree expr)
2645 : {
2646 373360086 : tree decl = NULL_TREE;
2647 373360086 : if (TREE_CODE (expr) == VAR_DECL)
2648 : decl = expr;
2649 361483426 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2650 831391 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2651 831391 : if (decl
2652 12708051 : && C_DECL_DECLARED_CONSTEXPR (decl)
2653 498 : && DECL_INITIAL (decl) != NULL_TREE
2654 831889 : && !error_operand_p (DECL_INITIAL (decl)))
2655 498 : return DECL_INITIAL (decl);
2656 373359588 : if (TREE_CODE (expr) != COMPONENT_REF)
2657 : return NULL_TREE;
2658 881436 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2659 881436 : if (inner == NULL_TREE)
2660 : return NULL_TREE;
2661 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2662 47 : && !error_operand_p (inner)
2663 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2664 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2665 47 : inner = TREE_OPERAND (inner, 0);
2666 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2667 : return NULL_TREE;
2668 126 : tree field = TREE_OPERAND (expr, 1);
2669 126 : unsigned HOST_WIDE_INT cidx;
2670 126 : tree cfield, cvalue;
2671 126 : bool have_other_init = false;
2672 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2673 : {
2674 250 : if (cfield == field)
2675 : return cvalue;
2676 140 : have_other_init = true;
2677 : }
2678 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2679 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2680 : return NULL_TREE;
2681 : /* Return a default initializer. */
2682 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2683 5 : return build_constructor (TREE_TYPE (expr), NULL);
2684 8 : return build_zero_cst (TREE_TYPE (expr));
2685 : }
2686 :
2687 : /* Helper function for convert_lvalue_to_rvalue called via
2688 : walk_tree_without_duplicates. Find DATA inside of the expression. */
2689 :
2690 : static tree
2691 230174 : c_find_var_r (tree *tp, int *walk_subtrees, void *data)
2692 : {
2693 230174 : if (TYPE_P (*tp))
2694 0 : *walk_subtrees = 0;
2695 230174 : else if (*tp == (tree) data)
2696 0 : return *tp;
2697 : return NULL_TREE;
2698 : }
2699 :
2700 : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2701 : including converting functions and arrays to pointers if CONVERT_P.
2702 : If READ_P, also mark the expression as having been read. If
2703 : FOR_INIT, constexpr expressions of structure and union type should
2704 : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2705 : constexpr scalars (including elements of structures and unions) are
2706 : replaced by their initializers. */
2707 :
2708 : struct c_expr
2709 372666924 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2710 : bool convert_p, bool read_p, bool for_init)
2711 : {
2712 372666924 : bool force_non_npc = false;
2713 372666924 : if (read_p)
2714 327036405 : mark_exp_read (exp.value);
2715 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2716 327036405 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2717 787594 : && handle_counted_by_p (exp.value))
2718 787565 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2719 :
2720 372666924 : if (convert_p)
2721 372653316 : exp = default_function_array_conversion (loc, exp);
2722 372666924 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2723 372666924 : || (flag_isoc2y
2724 1574 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2725 370052703 : exp.value = require_complete_type (loc, exp.value);
2726 372666924 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2727 : {
2728 372478650 : tree init = maybe_get_constexpr_init (exp.value);
2729 372478650 : if (init != NULL_TREE)
2730 : {
2731 : /* A named constant of pointer type or type nullptr_t is not
2732 : a null pointer constant even if the initializer is
2733 : one. */
2734 495 : if (TREE_CODE (init) == INTEGER_CST
2735 360 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2736 521 : && integer_zerop (init))
2737 : force_non_npc = true;
2738 : exp.value = init;
2739 : }
2740 : }
2741 372666924 : if (really_atomic_lvalue (exp.value))
2742 : {
2743 26934 : vec<tree, va_gc> *params;
2744 26934 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2745 26934 : tree expr_type = TREE_TYPE (exp.value);
2746 26934 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2747 26934 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2748 :
2749 26934 : gcc_assert (TYPE_ATOMIC (expr_type));
2750 :
2751 : /* Expansion of a generic atomic load may require an addition
2752 : element, so allocate enough to prevent a resize. */
2753 26934 : vec_alloc (params, 4);
2754 :
2755 : /* Remove the qualifiers for the rest of the expressions and
2756 : create the VAL temp variable to hold the RHS. */
2757 26934 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2758 26934 : tmp = create_tmp_var_raw (nonatomic_type);
2759 26934 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2760 26934 : TREE_ADDRESSABLE (tmp) = 1;
2761 : /* Do not disable warnings for TMP even though it's artificial.
2762 : -Winvalid-memory-model depends on it. */
2763 :
2764 : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2765 26934 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2766 26934 : params->quick_push (expr_addr);
2767 26934 : params->quick_push (tmp_addr);
2768 26934 : params->quick_push (seq_cst);
2769 26934 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2770 :
2771 : /* EXPR is always read. */
2772 26934 : mark_exp_read (exp.value);
2773 :
2774 : /* Optimize the common case where c_build_function_call_vec
2775 : immediately folds __atomic_load (&expr, &tmp, SEQ_CST); into
2776 : tmp = __atomic_load_<N> (&expr, SEQ_CST);
2777 : In that case tmp is not addressable and can be initialized
2778 : fully by the rhs of the MODIFY_EXPR. */
2779 26934 : tree tem = func_call;
2780 26934 : if (CONVERT_EXPR_P (tem) && VOID_TYPE_P (TREE_TYPE (tem)))
2781 : {
2782 25679 : tem = TREE_OPERAND (tem, 0);
2783 25679 : if (TREE_CODE (tem) == MODIFY_EXPR
2784 25679 : && TREE_OPERAND (tem, 0) == tmp
2785 51358 : && !walk_tree_without_duplicates (&TREE_OPERAND (tem, 1),
2786 : c_find_var_r, tmp))
2787 : {
2788 25679 : TREE_ADDRESSABLE (tmp) = 0;
2789 25679 : func_call = TREE_OPERAND (tem, 1);
2790 : }
2791 : }
2792 :
2793 : /* Return tmp which contains the value loaded. */
2794 26934 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2795 : NULL_TREE, NULL_TREE);
2796 : }
2797 372653316 : if (convert_p && !error_operand_p (exp.value)
2798 745312584 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2799 372645551 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2800 372666924 : if (force_non_npc)
2801 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2802 :
2803 372666924 : {
2804 372666924 : tree false_value, true_value;
2805 372653316 : if (convert_p && !error_operand_p (exp.value)
2806 745312584 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2807 : &false_value, &true_value))
2808 : {
2809 9721 : tree t = save_expr (exp.value);
2810 :
2811 9721 : mark_exp_read (exp.value);
2812 :
2813 9721 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2814 9721 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2815 9721 : expr = build_compound_expr (loc, expr, boolean_true_node);
2816 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2817 : fold_build2_loc (loc, NE_EXPR,
2818 : boolean_type_node,
2819 : t, true_value),
2820 : expr, boolean_true_node);
2821 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2822 : fold_build2_loc (loc, NE_EXPR,
2823 : boolean_type_node,
2824 : t, false_value),
2825 : expr, boolean_false_node);
2826 :
2827 9721 : exp.value = expr;
2828 : }
2829 : }
2830 :
2831 372666924 : return exp;
2832 : }
2833 :
2834 : /* Wrapper for the overload above, same arguments but for tree rather than
2835 : c_expr. This is important for hardbools to decay to bools. */
2836 :
2837 : static inline tree
2838 584535 : convert_lvalue_to_rvalue (location_t loc, tree val,
2839 : bool convert_p, bool read_p, bool for_init = false)
2840 : {
2841 584535 : struct c_expr expr;
2842 584535 : memset (&expr, 0, sizeof (expr));
2843 584535 : expr.value = val;
2844 584535 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2845 584535 : return expr.value;
2846 : }
2847 :
2848 : /* EXP is an expression of integer type. Apply the integer promotions
2849 : to it and return the promoted value. */
2850 :
2851 : tree
2852 80960604 : perform_integral_promotions (tree exp)
2853 : {
2854 80960604 : tree type = TREE_TYPE (exp);
2855 80960604 : enum tree_code code = TREE_CODE (type);
2856 :
2857 80960604 : gcc_assert (INTEGRAL_TYPE_P (type));
2858 :
2859 : /* Convert enums to the result of applying the integer promotions to
2860 : their underlying type. */
2861 80960604 : if (code == ENUMERAL_TYPE)
2862 : {
2863 659408 : type = ENUM_UNDERLYING_TYPE (type);
2864 659408 : if (c_promoting_integer_type_p (type))
2865 : {
2866 210 : if (TYPE_UNSIGNED (type)
2867 210 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2868 0 : type = unsigned_type_node;
2869 : else
2870 210 : type = integer_type_node;
2871 : }
2872 :
2873 659408 : return convert (type, exp);
2874 : }
2875 :
2876 : /* ??? This should no longer be needed now bit-fields have their
2877 : proper types. */
2878 80301196 : if (TREE_CODE (exp) == COMPONENT_REF
2879 80301196 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2880 : {
2881 62441 : if (BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))))
2882 : {
2883 584 : tree btype = DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1));
2884 584 : if (TREE_CODE (btype) == BITINT_TYPE)
2885 546 : return convert (btype, exp);
2886 : else
2887 38 : return convert (ENUM_UNDERLYING_TYPE (btype), exp);
2888 : }
2889 : /* If it's thinner than an int, promote it like a
2890 : c_promoting_integer_type_p, otherwise leave it alone. */
2891 61857 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2892 61857 : TYPE_PRECISION (integer_type_node)) < 0)
2893 53595 : return convert (integer_type_node, exp);
2894 : }
2895 :
2896 80247017 : if (c_promoting_integer_type_p (type))
2897 : {
2898 : /* Preserve unsignedness if not really getting any wider. */
2899 659176 : if (TYPE_UNSIGNED (type)
2900 659176 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2901 0 : return convert (unsigned_type_node, exp);
2902 :
2903 659176 : return convert (integer_type_node, exp);
2904 : }
2905 :
2906 : return exp;
2907 : }
2908 :
2909 :
2910 : /* Perform default promotions for C data used in expressions.
2911 : Enumeral types or short or char are converted to int.
2912 : In addition, manifest constants symbols are replaced by their values. */
2913 :
2914 : tree
2915 90597098 : default_conversion (tree exp)
2916 : {
2917 90597098 : tree orig_exp;
2918 90597098 : tree type = TREE_TYPE (exp);
2919 90597098 : enum tree_code code = TREE_CODE (type);
2920 90597098 : tree promoted_type;
2921 :
2922 90597098 : mark_exp_read (exp);
2923 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2924 90597098 : if (TREE_CODE (exp) == COMPONENT_REF
2925 90597098 : && handle_counted_by_p (exp))
2926 603226 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2927 :
2928 : /* Functions and arrays have been converted during parsing. */
2929 90597098 : gcc_assert (code != FUNCTION_TYPE);
2930 90597098 : if (code == ARRAY_TYPE)
2931 : return exp;
2932 :
2933 : /* Constants can be used directly unless they're not loadable. */
2934 90596938 : if (TREE_CODE (exp) == CONST_DECL)
2935 0 : exp = DECL_INITIAL (exp);
2936 :
2937 : /* Strip no-op conversions. */
2938 90596938 : orig_exp = exp;
2939 90961877 : STRIP_TYPE_NOPS (exp);
2940 :
2941 90596938 : copy_warning (exp, orig_exp);
2942 :
2943 90596938 : if (code == VOID_TYPE)
2944 : {
2945 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2946 : "void value not ignored as it ought to be");
2947 2 : return error_mark_node;
2948 : }
2949 :
2950 90596936 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2951 90596936 : if (exp == error_mark_node)
2952 : return error_mark_node;
2953 :
2954 90596031 : promoted_type = targetm.promoted_type (type);
2955 90596031 : if (promoted_type)
2956 0 : return convert (promoted_type, exp);
2957 :
2958 90596031 : if (INTEGRAL_TYPE_P (type))
2959 : {
2960 : /* Avoid performing integral promotion of bit-precise integer
2961 : type bit-field to its corresponding bit-precise integer if
2962 : orig_exp is a cast to it to its underlying type. */
2963 79955451 : if (orig_exp != exp
2964 13127 : && TREE_CODE (exp) == COMPONENT_REF
2965 42 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2966 79955482 : && BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))))
2967 31 : return perform_integral_promotions (orig_exp);
2968 79955420 : return perform_integral_promotions (exp);
2969 : }
2970 :
2971 : return exp;
2972 : }
2973 :
2974 : /* Look up COMPONENT in a structure or union TYPE.
2975 :
2976 : If the component name is not found, returns NULL_TREE. Otherwise,
2977 : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2978 : stepping down the chain to the component, which is in the last
2979 : TREE_VALUE of the list. Normally the list is of length one, but if
2980 : the component is embedded within (nested) anonymous structures or
2981 : unions, the list steps down the chain to the component. */
2982 :
2983 : tree
2984 2337950 : lookup_field (const_tree type, tree component)
2985 : {
2986 2337950 : tree field;
2987 :
2988 : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2989 : to the field elements. Use a binary search on this array to quickly
2990 : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2991 : will always be set for structures which have many elements.
2992 :
2993 : Duplicate field checking replaces duplicates with NULL_TREE so
2994 : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2995 : case just iterate using DECL_CHAIN. */
2996 :
2997 2641235 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2998 2641235 : && !seen_error ())
2999 : {
3000 303264 : int bot, top, half;
3001 303264 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
3002 :
3003 303264 : field = TYPE_FIELDS (type);
3004 303264 : bot = 0;
3005 303264 : top = TYPE_LANG_SPECIFIC (type)->s->len;
3006 1397029 : while (top - bot > 1)
3007 : {
3008 1367933 : half = (top - bot + 1) >> 1;
3009 1367933 : field = field_array[bot+half];
3010 :
3011 1367933 : if (DECL_NAME (field) == NULL_TREE)
3012 : {
3013 : /* Step through all anon unions in linear fashion. */
3014 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
3015 : {
3016 0 : field = field_array[bot++];
3017 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3018 : {
3019 0 : tree anon = lookup_field (TREE_TYPE (field), component);
3020 :
3021 0 : if (anon)
3022 0 : return tree_cons (NULL_TREE, field, anon);
3023 :
3024 : /* The Plan 9 compiler permits referring
3025 : directly to an anonymous struct/union field
3026 : using a typedef name. */
3027 0 : if (flag_plan9_extensions
3028 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3029 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
3030 : == TYPE_DECL)
3031 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3032 : == component))
3033 : break;
3034 : }
3035 : }
3036 :
3037 : /* Entire record is only anon unions. */
3038 0 : if (bot > top)
3039 : return NULL_TREE;
3040 :
3041 : /* Restart the binary search, with new lower bound. */
3042 0 : continue;
3043 0 : }
3044 :
3045 1367933 : if (DECL_NAME (field) == component)
3046 : break;
3047 1093765 : if (DECL_NAME (field) < component)
3048 : bot += half;
3049 : else
3050 901649 : top = bot + half;
3051 : }
3052 :
3053 303264 : if (DECL_NAME (field_array[bot]) == component)
3054 : field = field_array[bot];
3055 274168 : else if (DECL_NAME (field) != component)
3056 : return NULL_TREE;
3057 : }
3058 : else
3059 : {
3060 5305313 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3061 : {
3062 5295832 : if (DECL_NAME (field) == NULL_TREE
3063 5295832 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3064 : {
3065 12394 : tree anon = lookup_field (TREE_TYPE (field), component);
3066 :
3067 12394 : if (anon)
3068 3008 : return tree_cons (NULL_TREE, field, anon);
3069 :
3070 : /* The Plan 9 compiler permits referring directly to an
3071 : anonymous struct/union field using a typedef
3072 : name. */
3073 9386 : if (flag_plan9_extensions
3074 174 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3075 172 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
3076 9448 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3077 : == component))
3078 : break;
3079 : }
3080 :
3081 5292820 : if (DECL_NAME (field) == component)
3082 : break;
3083 : }
3084 :
3085 2031678 : if (field == NULL_TREE)
3086 : return NULL_TREE;
3087 : }
3088 :
3089 2325461 : return tree_cons (NULL_TREE, field, NULL_TREE);
3090 : }
3091 :
3092 : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
3093 :
3094 : static void
3095 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
3096 : vec<tree> *candidates)
3097 : {
3098 101 : tree field;
3099 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3100 : {
3101 157 : if (DECL_NAME (field) == NULL_TREE
3102 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3103 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
3104 : candidates);
3105 :
3106 157 : if (DECL_NAME (field))
3107 111 : candidates->safe_push (DECL_NAME (field));
3108 : }
3109 101 : }
3110 :
3111 : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
3112 : rather than returning a TREE_LIST for an exact match. */
3113 :
3114 : static tree
3115 71 : lookup_field_fuzzy (tree type, tree component)
3116 : {
3117 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
3118 :
3119 : /* First, gather a list of candidates. */
3120 71 : auto_vec <tree> candidates;
3121 :
3122 71 : lookup_field_fuzzy_find_candidates (type, component,
3123 : &candidates);
3124 :
3125 71 : return find_closest_identifier (component, &candidates);
3126 71 : }
3127 :
3128 : /* Support function for build_component_ref's error-handling.
3129 :
3130 : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
3131 : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
3132 :
3133 : static bool
3134 72 : should_suggest_deref_p (tree datum_type)
3135 : {
3136 : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
3137 : allows "." for ptrs; we could be handling a failed attempt
3138 : to access a property. */
3139 72 : if (c_dialect_objc ())
3140 : return false;
3141 :
3142 : /* Only suggest it for pointers... */
3143 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3144 : return false;
3145 :
3146 : /* ...to structs/unions. */
3147 8 : tree underlying_type = TREE_TYPE (datum_type);
3148 8 : enum tree_code code = TREE_CODE (underlying_type);
3149 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3150 : return true;
3151 : else
3152 1 : return false;
3153 : }
3154 :
3155 : /* Give a component ref REF, decide whether we should handle its counted_by
3156 : attribute based on its context:
3157 : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3158 :
3159 : static bool
3160 2218468 : handle_counted_by_p (tree ref)
3161 : {
3162 2218468 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3163 2218468 : tree datum = TREE_OPERAND (ref, 0);
3164 : /* If the component_ref is build for a offsetof, i.e., the datum
3165 : of the component_ref is a indirect_ref of null_pointer_node,
3166 : we should not generate call to .ACCESS_WITH_SIZE. */
3167 2218468 : if (TREE_CODE (datum) == INDIRECT_REF
3168 2218468 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3169 : return false;
3170 2218130 : if (in_typeof || in_alignof)
3171 8786 : return false;
3172 : return true;
3173 : }
3174 :
3175 : /* Given a component ref REF, if there is a counted_by attribute attached,
3176 : issue error when the element_type is a structure or union including a
3177 : flexible array member. */
3178 :
3179 : static void
3180 2291237 : check_counted_by_attribute (location_t loc, tree ref)
3181 : {
3182 2291237 : tree subdatum = TREE_OPERAND (ref, 1);
3183 2291237 : tree sub_type = TREE_TYPE (subdatum);
3184 :
3185 2291237 : if (!flexible_array_member_type_p (sub_type)
3186 2291237 : && TREE_CODE (sub_type) != POINTER_TYPE)
3187 : return;
3188 :
3189 392232 : tree element_type = TREE_TYPE (sub_type);
3190 :
3191 392232 : tree attr_counted_by = lookup_attribute ("counted_by",
3192 392232 : DECL_ATTRIBUTES (subdatum));
3193 392232 : if (attr_counted_by)
3194 : {
3195 : /* Issue error when the element_type is a structure or
3196 : union including a flexible array member. */
3197 889 : if (RECORD_OR_UNION_TYPE_P (element_type)
3198 889 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3199 : {
3200 2 : error_at (loc,
3201 : "%<counted_by%> attribute is not allowed for a pointer to"
3202 : " structure or union with flexible array member");
3203 2 : return;
3204 : }
3205 : }
3206 : }
3207 :
3208 : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3209 : to the object that represents its counted_by per the attribute
3210 : counted_by attached to this field if it's a flexible array member
3211 : or a pointer field, otherwise return NULL_TREE.
3212 : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3213 : For example, if:
3214 :
3215 : struct P {
3216 : int k;
3217 : int x[] __attribute__ ((counted_by (k)));
3218 : } *p;
3219 :
3220 : for:
3221 : p->x
3222 :
3223 : the ref to the object that represents its element count will be:
3224 :
3225 : &(p->k)
3226 : */
3227 :
3228 : static tree
3229 443504 : build_counted_by_ref (tree datum, tree subdatum,
3230 : tree *counted_by_type)
3231 : {
3232 443504 : tree sub_type = TREE_TYPE (subdatum);
3233 443504 : if (!flexible_array_member_type_p (sub_type)
3234 443504 : && TREE_CODE (sub_type) != POINTER_TYPE)
3235 : return NULL_TREE;
3236 :
3237 443504 : tree attr_counted_by = lookup_attribute ("counted_by",
3238 443504 : DECL_ATTRIBUTES (subdatum));
3239 443504 : if (!attr_counted_by)
3240 : return NULL_TREE;
3241 :
3242 666 : tree counted_by_ref = NULL_TREE;
3243 666 : *counted_by_type = NULL_TREE;
3244 :
3245 666 : tree type = TREE_TYPE (datum);
3246 :
3247 : /* If the type of the containing structure is an anonymous struct/union,
3248 : and this anonymous struct/union is not a root type, get the first
3249 : outer named structure/union type. */
3250 666 : while (TREE_CODE (datum) == COMPONENT_REF
3251 36 : && c_type_tag (type) == NULL_TREE
3252 736 : && DECL_NAME (TREE_OPERAND (datum, 1)) == NULL_TREE)
3253 : {
3254 35 : datum = TREE_OPERAND (datum, 0);
3255 35 : type = TREE_TYPE (datum);
3256 : }
3257 :
3258 666 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3259 666 : tree counted_by_field = lookup_field (type, field_id);
3260 666 : gcc_assert (counted_by_field);
3261 :
3262 1078 : tree counted_by_subdatum;
3263 1078 : do
3264 : {
3265 1078 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3266 : /* Get the TYPE of the counted_by field. */
3267 1078 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3268 :
3269 1078 : counted_by_ref
3270 1078 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3271 : datum, counted_by_subdatum, NULL_TREE);
3272 :
3273 1078 : datum = counted_by_ref;
3274 1078 : counted_by_field = TREE_CHAIN (counted_by_field);
3275 : }
3276 1078 : while (counted_by_field);
3277 :
3278 666 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3279 666 : return counted_by_ref;
3280 : }
3281 :
3282 : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3283 : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3284 : call to the internal function .ACCESS_WITH_SIZE.
3285 :
3286 : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3287 : the internal function .ACCESS_WITH_SIZE.
3288 :
3289 : REF
3290 :
3291 : to:
3292 :
3293 : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3294 : TYPE_SIZE_UNIT for element)
3295 :
3296 : NOTE: The return type of this function is the POINTER type pointing
3297 : to the original flexible array type. Then the type of the INDIRECT_REF
3298 : is the original flexible array type.
3299 : The type of the first argument of this function is a POINTER type
3300 : to the original flexible array type.
3301 :
3302 : B: For pointers with counted_by, generate a call to the internal function
3303 : .ACCESS_WITH_SIZE.
3304 :
3305 : REF
3306 :
3307 : to:
3308 :
3309 : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3310 : TYPE_SIZE_UNIT for element)
3311 :
3312 : NOTE: The return type of this function is the original pointer type.
3313 : The type of the first argument of this function is the original
3314 : pointer type.
3315 :
3316 : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3317 : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3318 :
3319 : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3320 : of the array.
3321 :
3322 : */
3323 : static tree
3324 666 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3325 : tree counted_by_ref,
3326 : tree counted_by_type)
3327 : {
3328 666 : gcc_assert (flexible_array_member_type_p (TREE_TYPE (ref))
3329 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3330 :
3331 666 : bool is_fam = flexible_array_member_type_p (TREE_TYPE (ref));
3332 :
3333 : /* The result type of the call is a pointer to the flexible array type;
3334 : or is the original pointer type to the pointer field with counted_by. */
3335 666 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3336 462 : : TREE_TYPE (ref);
3337 :
3338 666 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3339 666 : tree element_size = VOID_TYPE_P (element_type)
3340 666 : ? size_one_node
3341 666 : : TYPE_SIZE_UNIT (element_type);
3342 666 : if (element_size == NULL_TREE)
3343 : return ref;
3344 :
3345 652 : tree first_param = is_fam
3346 652 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3347 : false, NULL)
3348 448 : : c_fully_fold (ref, false, NULL);
3349 652 : tree second_param
3350 652 : = c_fully_fold (counted_by_ref, false, NULL);
3351 652 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3352 :
3353 652 : tree call
3354 652 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3355 : result_type, 4,
3356 : first_param,
3357 : second_param,
3358 : third_param,
3359 : element_size);
3360 :
3361 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3362 652 : if (is_fam)
3363 204 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3364 652 : SET_EXPR_LOCATION (call, loc);
3365 652 : return call;
3366 : }
3367 :
3368 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3369 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3370 : function .ACCESS_WITH_SIZE.
3371 : Otherwise, return the ref itself. */
3372 :
3373 : tree
3374 2209424 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3375 : {
3376 2209424 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3377 2209424 : tree datum = TREE_OPERAND (ref, 0);
3378 2209424 : tree subdatum = TREE_OPERAND (ref, 1);
3379 2209424 : tree counted_by_type = NULL_TREE;
3380 :
3381 2209424 : if (!(flexible_array_member_type_p (TREE_TYPE (ref))
3382 2138891 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3383 : return ref;
3384 :
3385 443504 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3386 : &counted_by_type);
3387 443504 : if (counted_by_ref)
3388 666 : ref = build_access_with_size_for_counted_by (loc, ref,
3389 : counted_by_ref,
3390 : counted_by_type);
3391 : return ref;
3392 : }
3393 :
3394 : /* Make an expression to refer to the COMPONENT field of structure or
3395 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3396 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3397 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3398 : it is from -> operator.
3399 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3400 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3401 :
3402 : tree
3403 2289118 : build_component_ref (location_t loc, tree datum, tree component,
3404 : location_t component_loc, location_t arrow_loc)
3405 : {
3406 2289118 : tree type = TREE_TYPE (datum);
3407 2289118 : enum tree_code code = TREE_CODE (type);
3408 2289118 : tree field = NULL;
3409 2289118 : tree ref;
3410 2289118 : bool datum_lvalue = lvalue_p (datum);
3411 :
3412 2289118 : if (!objc_is_public (datum, component))
3413 0 : return error_mark_node;
3414 :
3415 : /* Detect Objective-C property syntax object.property. */
3416 2289118 : if (c_dialect_objc ()
3417 2289118 : && (ref = objc_maybe_build_component_ref (datum, component)))
3418 : return ref;
3419 :
3420 : /* See if there is a field or component with name COMPONENT. */
3421 :
3422 2289118 : if (code == RECORD_TYPE || code == UNION_TYPE)
3423 : {
3424 2289046 : if (!COMPLETE_TYPE_P (type))
3425 : {
3426 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3427 19 : return error_mark_node;
3428 : }
3429 :
3430 2289027 : field = lookup_field (type, component);
3431 :
3432 2289027 : if (!field)
3433 : {
3434 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3435 63 : if (guessed_id)
3436 : {
3437 : /* Attempt to provide a fixit replacement hint, if
3438 : we have a valid range for the component. */
3439 0 : location_t reported_loc
3440 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3441 26 : gcc_rich_location rich_loc (reported_loc);
3442 26 : if (component_loc != UNKNOWN_LOCATION)
3443 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3444 26 : error_at (&rich_loc,
3445 : "%qT has no member named %qE; did you mean %qE?",
3446 : type, component, guessed_id);
3447 26 : }
3448 : else
3449 37 : error_at (loc, "%qT has no member named %qE", type, component);
3450 63 : return error_mark_node;
3451 : }
3452 :
3453 : /* Accessing elements of atomic structures or unions is undefined
3454 : behavior (C11 6.5.2.3#5). */
3455 2288964 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3456 : {
3457 18 : if (code == RECORD_TYPE)
3458 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3459 : "structure %qE", component, datum);
3460 : else
3461 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3462 : "union %qE", component, datum);
3463 : }
3464 :
3465 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3466 : This might be better solved in future the way the C++ front
3467 : end does it - by giving the anonymous entities each a
3468 : separate name and type, and then have build_component_ref
3469 : recursively call itself. We can't do that here. */
3470 2291237 : do
3471 : {
3472 2291237 : tree subdatum = TREE_VALUE (field);
3473 2291237 : qualifier_set quals;
3474 2291237 : tree subtype;
3475 2291237 : bool use_datum_quals;
3476 :
3477 2291237 : if (TREE_TYPE (subdatum) == error_mark_node)
3478 2289118 : return error_mark_node;
3479 :
3480 : /* If this is an rvalue, it does not have qualifiers in C
3481 : standard terms and we must avoid propagating such
3482 : qualifiers down to a non-lvalue array that is then
3483 : converted to a pointer. */
3484 4582474 : use_datum_quals = (datum_lvalue
3485 2291237 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3486 :
3487 2291237 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3488 2291237 : if (use_datum_quals)
3489 : {
3490 : /* SUBDATUM refers to a field, which lack their own address
3491 : space. */
3492 2290971 : gcc_assert (ADDR_SPACE_GENERIC_P (quals.addr_space ()));
3493 2290971 : addr_space_t datum_as;
3494 2290971 : cv_qualifier datum_cv;
3495 2290971 : std::tie (datum_cv, datum_as)
3496 2290971 : = TYPE_QUALS (TREE_TYPE (datum)).split ();
3497 2290971 : quals |= datum_cv;
3498 2290971 : quals.set_as (datum_as);
3499 : }
3500 2291237 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3501 :
3502 2291237 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3503 : NULL_TREE);
3504 2291237 : SET_EXPR_LOCATION (ref, loc);
3505 :
3506 2291237 : check_counted_by_attribute (loc, ref);
3507 :
3508 2291237 : if (TREE_READONLY (subdatum)
3509 2291237 : || (use_datum_quals && TREE_READONLY (datum)))
3510 30983 : TREE_READONLY (ref) = 1;
3511 2291237 : if (TREE_THIS_VOLATILE (subdatum)
3512 2290211 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3513 2476 : TREE_THIS_VOLATILE (ref) = 1;
3514 :
3515 2291237 : if (TREE_UNAVAILABLE (subdatum))
3516 10 : error_unavailable_use (subdatum, NULL_TREE);
3517 2291227 : else if (TREE_DEPRECATED (subdatum))
3518 16 : warn_deprecated_use (subdatum, NULL_TREE);
3519 :
3520 2291237 : datum = ref;
3521 :
3522 2291237 : field = TREE_CHAIN (field);
3523 : }
3524 2291237 : while (field);
3525 :
3526 : return ref;
3527 : }
3528 72 : else if (should_suggest_deref_p (type))
3529 : {
3530 : /* Special-case the error message for "ptr.field" for the case
3531 : where the user has confused "." vs "->". */
3532 7 : rich_location richloc (line_table, loc);
3533 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3534 : {
3535 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3536 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3537 2 : error_at (&richloc,
3538 : "%qE is a pointer to pointer; did you mean to dereference "
3539 : "it before applying %<->%> to it?",
3540 2 : TREE_OPERAND (datum, 0));
3541 : }
3542 : else
3543 : {
3544 : /* "loc" should be the "." token. */
3545 5 : richloc.add_fixit_replace ("->");
3546 5 : error_at (&richloc,
3547 : "%qE is a pointer; did you mean to use %<->%>?",
3548 : datum);
3549 : }
3550 7 : return error_mark_node;
3551 7 : }
3552 65 : else if (code != ERROR_MARK)
3553 1 : error_at (loc,
3554 : "request for member %qE in something not a structure or union",
3555 : component);
3556 :
3557 65 : return error_mark_node;
3558 : }
3559 :
3560 : /* Given an expression PTR for a pointer, return an expression
3561 : for the value pointed to.
3562 : ERRORSTRING is the name of the operator to appear in error messages.
3563 :
3564 : LOC is the location to use for the generated tree. */
3565 :
3566 : tree
3567 2786090 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3568 : {
3569 2786090 : tree pointer = default_conversion (ptr);
3570 2786090 : tree type = TREE_TYPE (pointer);
3571 2786090 : tree ref;
3572 :
3573 2786090 : if (TREE_CODE (type) == POINTER_TYPE)
3574 : {
3575 2785736 : if (CONVERT_EXPR_P (pointer)
3576 1999532 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3577 : {
3578 : /* If a warning is issued, mark it to avoid duplicates from
3579 : the backend. This only needs to be done at
3580 : warn_strict_aliasing > 2. */
3581 786204 : if (warn_strict_aliasing > 2)
3582 335156 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3583 335156 : type, TREE_OPERAND (pointer, 0)))
3584 8 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3585 : }
3586 :
3587 2785736 : if (TREE_CODE (pointer) == ADDR_EXPR
3588 2785736 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3589 67507 : == TREE_TYPE (type)))
3590 : {
3591 67498 : ref = TREE_OPERAND (pointer, 0);
3592 67498 : protected_set_expr_location (ref, loc);
3593 67498 : return ref;
3594 : }
3595 : else
3596 : {
3597 2718238 : tree t = TREE_TYPE (type);
3598 :
3599 2718238 : ref = build1 (INDIRECT_REF, t, pointer);
3600 :
3601 2718238 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3602 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3603 :
3604 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3605 : so that we get the proper error message if the result is used
3606 : to assign to. Also, &* is supposed to be a no-op.
3607 : And ANSI C seems to specify that the type of the result
3608 : should be the const type. */
3609 : /* A de-reference of a pointer to const is not a const. It is valid
3610 : to change it via some other pointer. */
3611 2718238 : TREE_READONLY (ref) = TYPE_READONLY (t);
3612 2718238 : TREE_SIDE_EFFECTS (ref)
3613 2718238 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3614 2718238 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3615 2718238 : protected_set_expr_location (ref, loc);
3616 2718238 : return ref;
3617 : }
3618 : }
3619 354 : else if (TREE_CODE (pointer) != ERROR_MARK)
3620 278 : invalid_indirection_error (loc, type, errstring);
3621 :
3622 354 : return error_mark_node;
3623 : }
3624 :
3625 : /* This handles expressions of the form "a[i]", which denotes
3626 : an array reference.
3627 :
3628 : This is logically equivalent in C to *(a+i), but we may do it differently.
3629 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3630 : This avoids forcing the array out of registers, and can work on
3631 : arrays that are not lvalues (for example, members of structures returned
3632 : by functions).
3633 :
3634 : For vector types, allow vector[i] but not i[vector], and create
3635 : *(((type*)&vectortype) + i) for the expression.
3636 :
3637 : LOC is the location to use for the returned expression. */
3638 :
3639 : tree
3640 3570778 : build_array_ref (location_t loc, tree array, tree index)
3641 : {
3642 3570778 : tree ret;
3643 3570778 : bool swapped = false;
3644 3570778 : if (TREE_TYPE (array) == error_mark_node
3645 3570778 : || TREE_TYPE (index) == error_mark_node)
3646 : return error_mark_node;
3647 :
3648 3570670 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3649 2021315 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3650 : /* Allow vector[index] but not index[vector]. */
3651 4606946 : && !gnu_vector_type_p (TREE_TYPE (array)))
3652 : {
3653 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3654 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3655 : {
3656 2 : error_at (loc,
3657 : "subscripted value is neither array nor pointer nor vector");
3658 :
3659 2 : return error_mark_node;
3660 : }
3661 192 : std::swap (array, index);
3662 192 : swapped = true;
3663 : }
3664 :
3665 3570668 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3666 : {
3667 2 : error_at (loc, "array subscript is not an integer");
3668 2 : return error_mark_node;
3669 : }
3670 :
3671 3570666 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3672 : {
3673 10 : error_at (loc, "subscripted value is pointer to function");
3674 10 : return error_mark_node;
3675 : }
3676 :
3677 : /* ??? Existing practice has been to warn only when the char
3678 : index is syntactically the index, not for char[array]. */
3679 3570656 : if (!swapped)
3680 3570470 : warn_array_subscript_with_type_char (loc, index);
3681 :
3682 : /* Apply default promotions *after* noticing character types. */
3683 3570656 : index = default_conversion (index);
3684 3570656 : if (index == error_mark_node)
3685 : return error_mark_node;
3686 :
3687 3570655 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3688 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3689 :
3690 3570655 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3691 3570655 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3692 :
3693 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3694 3570655 : if (TREE_CODE (array) == COMPONENT_REF
3695 3570655 : && handle_counted_by_p (array))
3696 818549 : array = handle_counted_by_for_component_ref (loc, array);
3697 :
3698 3570655 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3699 : {
3700 2585444 : tree rval, type;
3701 :
3702 : /* An array that is indexed by a non-constant
3703 : cannot be stored in a register; we must be able to do
3704 : address arithmetic on its address.
3705 : Likewise an array of elements of variable size. */
3706 2585444 : if (TREE_CODE (index) != INTEGER_CST
3707 2585444 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3708 2098099 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3709 : {
3710 488085 : if (!c_mark_addressable (array, true, true))
3711 1 : return error_mark_node;
3712 : }
3713 : /* An array that is indexed by a constant value which is not within
3714 : the array bounds cannot be stored in a register either; because we
3715 : would get a crash in store_bit_field/extract_bit_field when trying
3716 : to access a non-existent part of the register. */
3717 2585443 : if (TREE_CODE (index) == INTEGER_CST
3718 2098099 : && TYPE_DOMAIN (TREE_TYPE (array))
3719 4680183 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3720 : {
3721 60657 : if (!c_mark_addressable (array, false, true))
3722 0 : return error_mark_node;
3723 : /* ISO C2Y disallows a negative integer constant expression index
3724 : when array subscripting has an operand of array type. */
3725 60657 : if (flag_isoc2y
3726 26 : && !TREE_OVERFLOW (index)
3727 60680 : && tree_int_cst_sgn (index) < 0)
3728 10 : error_at (loc, "array subscript is negative");
3729 : }
3730 :
3731 2585443 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3732 2561854 : && ! was_vector)
3733 : {
3734 1548633 : tree foo = array;
3735 2385984 : while (TREE_CODE (foo) == COMPONENT_REF)
3736 837351 : foo = TREE_OPERAND (foo, 0);
3737 1047403 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3738 2595970 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3739 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3740 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3741 : "ISO C forbids subscripting %<register%> array "
3742 : "before C2Y");
3743 1548555 : else if (!lvalue_p (foo))
3744 122 : pedwarn_c90 (loc, OPT_Wpedantic,
3745 : "ISO C90 forbids subscripting non-lvalue "
3746 : "array");
3747 : }
3748 :
3749 2585443 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3750 2585443 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3751 4 : index = fold_convert (sizetype, index);
3752 :
3753 2585443 : type = TREE_TYPE (TREE_TYPE (array));
3754 2585443 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3755 : /* Array ref is const/volatile if the array elements are
3756 : or if the array is. */
3757 7756329 : TREE_READONLY (rval)
3758 2585443 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3759 2585443 : | TREE_READONLY (array));
3760 7756329 : TREE_SIDE_EFFECTS (rval)
3761 2585443 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3762 2585443 : | TREE_SIDE_EFFECTS (array));
3763 5170886 : TREE_THIS_VOLATILE (rval)
3764 2585443 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3765 : /* This was added by rms on 16 Nov 91.
3766 : It fixes vol struct foo *a; a->elts[1]
3767 : in an inline function.
3768 : Hope it doesn't break something else. */
3769 2585443 : | TREE_THIS_VOLATILE (array));
3770 2585443 : ret = require_complete_type (loc, rval);
3771 2585443 : protected_set_expr_location (ret, loc);
3772 2585443 : if (non_lvalue)
3773 106237 : ret = non_lvalue_loc (loc, ret);
3774 : return ret;
3775 : }
3776 : else
3777 : {
3778 985211 : tree ar = default_conversion (array);
3779 :
3780 985211 : if (ar == error_mark_node)
3781 : return ar;
3782 :
3783 985211 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3784 985211 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3785 :
3786 985211 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3787 : index, false),
3788 : RO_ARRAY_INDEXING);
3789 985211 : if (non_lvalue)
3790 0 : ret = non_lvalue_loc (loc, ret);
3791 : return ret;
3792 : }
3793 : }
3794 :
3795 : /* Build an OpenMP array section reference, creating an exact type for the
3796 : resulting expression based on the element type and bounds if possible. If
3797 : we have variable bounds, create an incomplete array type for the result
3798 : instead. */
3799 :
3800 : tree
3801 4034 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3802 : {
3803 4034 : tree type = TREE_TYPE (array);
3804 4034 : gcc_assert (type);
3805 :
3806 4034 : tree sectype, eltype = TREE_TYPE (type);
3807 :
3808 : /* It's not an array or pointer type. Just reuse the type of the original
3809 : expression as the type of the array section (an error will be raised
3810 : anyway, later). */
3811 4034 : if (eltype == NULL_TREE || error_operand_p (eltype))
3812 12 : sectype = TREE_TYPE (array);
3813 : else
3814 : {
3815 4022 : tree idxtype = NULL_TREE;
3816 :
3817 4022 : if (index != NULL_TREE
3818 4022 : && length != NULL_TREE
3819 3032 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3820 7049 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3821 : {
3822 3020 : tree low = fold_convert (sizetype, index);
3823 3020 : tree high = fold_convert (sizetype, length);
3824 3020 : high = size_binop (PLUS_EXPR, low, high);
3825 3020 : high = size_binop (MINUS_EXPR, high, size_one_node);
3826 3020 : idxtype = build_range_type (sizetype, low, high);
3827 : }
3828 137 : else if ((index == NULL_TREE || integer_zerop (index))
3829 890 : && length != NULL_TREE
3830 1738 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3831 719 : idxtype = build_index_type (length);
3832 :
3833 4022 : gcc_assert (!error_operand_p (idxtype));
3834 :
3835 4022 : sectype = c_build_array_type (eltype, idxtype);
3836 : }
3837 :
3838 4034 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3839 : }
3840 :
3841 :
3842 : /* Record that REF is used. This is relevant for undeclared static
3843 : function and declarations referenced from a non-local context.
3844 : ADDRESS indicates whether the address is taken. */
3845 :
3846 : void
3847 492156761 : mark_decl_used (tree ref, bool address)
3848 : {
3849 492156761 : if (!ref || !DECL_P (ref) || in_alignof)
3850 : return;
3851 :
3852 : /* Non-file-scope and non-local reference in nested function. */
3853 360980318 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3854 125751599 : && DECL_CONTEXT (current_function_decl)
3855 492122581 : && DECL_CONTEXT (ref) != current_function_decl;
3856 :
3857 : /* An undeclared static function. */
3858 492112436 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3859 106004308 : && DECL_INITIAL (ref) == NULL_TREE
3860 72443234 : && DECL_EXTERNAL (ref)
3861 564552948 : && !TREE_PUBLIC (ref);
3862 :
3863 491978020 : if (!static_p && !nonloc_p)
3864 : return;
3865 :
3866 : /* If we may be in an unevaluated context, delay the decision. */
3867 137091 : if (in_sizeof || in_typeof || in_countof || in_generic)
3868 273 : return record_maybe_used_decl (ref, address);
3869 :
3870 136818 : if (static_p)
3871 134160 : C_DECL_USED (ref) = 1;
3872 :
3873 136818 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3874 : || TREE_CODE (ref) == PARM_DECL))
3875 2076 : DECL_NONLOCAL (ref) = 1;
3876 :
3877 : /* Nothing to do anymore. */
3878 2658 : if (!nonloc_p)
3879 : return;
3880 :
3881 : /* Even when this function is already marked as requiring a non-local
3882 : context we need to do the check because we may need to mark more
3883 : parents. */
3884 :
3885 : /* Filter out the cases where referencing a non-local variable does not
3886 : require a non-local context passed via the static chain. */
3887 2658 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3888 2542 : switch (TREE_CODE (ref))
3889 : {
3890 441 : case FUNCTION_DECL:
3891 : /* Use of another local function that requires no context is ok. */
3892 441 : if (!DECL_STATIC_CHAIN (ref) || ref == current_function_decl)
3893 : return;
3894 : break;
3895 1380 : case VAR_DECL:
3896 : /* Static variables and constexpr are ok, but for the later only
3897 : when the address is not taken. */
3898 1380 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3899 : return;
3900 : break;
3901 : case TYPE_DECL:
3902 : /* A typedef is ok when not for a variably-modified type. */
3903 : return;
3904 : case CONST_DECL:
3905 : /* An enumeration constant is ok. */
3906 : return;
3907 : case PARM_DECL:
3908 : break;
3909 : case LABEL_DECL:
3910 : break;
3911 0 : default:
3912 0 : gcc_unreachable ();
3913 : }
3914 :
3915 : /* Mark all parent functions up to the nesting level of the variable as
3916 : as needing the non-local context. */
3917 4656 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3918 : {
3919 4656 : if (cont == DECL_CONTEXT (ref))
3920 : break;
3921 :
3922 : /* There should not be any other type of context used for function
3923 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3924 2379 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3925 :
3926 2379 : if (TREE_CODE (cont) == FUNCTION_DECL)
3927 2379 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3928 : }
3929 : }
3930 :
3931 :
3932 : /* Build an external reference to identifier ID. FUN indicates
3933 : whether this will be used for a function call. LOC is the source
3934 : location of the identifier. This sets *TYPE to the type of the
3935 : identifier, which is not the same as the type of the returned value
3936 : for CONST_DECLs defined as enum constants. If the type of the
3937 : identifier is not available, *TYPE is set to NULL. */
3938 : tree
3939 180646869 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3940 : {
3941 180646869 : tree ref;
3942 180646869 : tree decl = lookup_name (id);
3943 :
3944 : /* In Objective-C, an instance variable (ivar) may be preferred to
3945 : whatever lookup_name() found. */
3946 180646869 : decl = objc_lookup_ivar (decl, id);
3947 :
3948 180646869 : *type = NULL;
3949 180646869 : if (decl && decl != error_mark_node)
3950 : {
3951 180640639 : ref = decl;
3952 180640639 : *type = TREE_TYPE (ref);
3953 180640639 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3954 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3955 : decl);
3956 : }
3957 6230 : else if (fun)
3958 : /* Implicit function declaration. */
3959 4730 : ref = implicitly_declare (loc, id);
3960 1500 : else if (decl == error_mark_node)
3961 : /* Don't complain about something that's already been
3962 : complained about. */
3963 : return error_mark_node;
3964 : else
3965 : {
3966 1264 : undeclared_variable (loc, id);
3967 1264 : return error_mark_node;
3968 : }
3969 :
3970 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3971 : unmappable types if we return the decl with an error_mark_node type,
3972 : rather than returning error_mark_node for the decl itself. */
3973 180645369 : if (TREE_TYPE (ref) == error_mark_node
3974 180645369 : && !c_omp_array_section_p)
3975 : return error_mark_node;
3976 :
3977 180645351 : if (TREE_UNAVAILABLE (ref))
3978 14 : error_unavailable_use (ref, NULL_TREE);
3979 180645337 : else if (TREE_DEPRECATED (ref))
3980 100 : warn_deprecated_use (ref, NULL_TREE);
3981 :
3982 : /* Recursive call does not count as usage. */
3983 180645351 : if (ref != current_function_decl)
3984 : {
3985 180642631 : TREE_USED (ref) = 1;
3986 : }
3987 :
3988 180645351 : mark_decl_used (ref, false);
3989 :
3990 180645351 : if (TREE_CODE (ref) == CONST_DECL)
3991 : {
3992 425889 : used_types_insert (TREE_TYPE (ref));
3993 :
3994 425889 : if (warn_cxx_compat
3995 22855 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3996 442630 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3997 : {
3998 1 : warning_at (loc, OPT_Wc___compat,
3999 : "enum constant defined in struct or union "
4000 : "is not visible in C++");
4001 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
4002 : }
4003 :
4004 425889 : ref = DECL_INITIAL (ref);
4005 425889 : TREE_CONSTANT (ref) = 1;
4006 : }
4007 : /* C99 6.7.4p3: An inline definition of a function with external
4008 : linkage ... shall not contain a reference to an identifier with
4009 : internal linkage. */
4010 180219462 : else if (current_function_decl != NULL_TREE
4011 178703273 : && DECL_DECLARED_INLINE_P (current_function_decl)
4012 164740902 : && DECL_EXTERNAL (current_function_decl)
4013 163927590 : && VAR_OR_FUNCTION_DECL_P (ref)
4014 58601296 : && (!VAR_P (ref) || TREE_STATIC (ref))
4015 49352794 : && ! TREE_PUBLIC (ref)
4016 180219496 : && DECL_CONTEXT (ref) != current_function_decl)
4017 25 : record_inline_static (loc, current_function_decl, ref,
4018 : csi_internal);
4019 :
4020 : return ref;
4021 : }
4022 :
4023 : static struct maybe_used_decl *maybe_used_decls;
4024 :
4025 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
4026 : _Generic, might be used if the operand of sizeof is a VLA type or the
4027 : operand of typeof is a variably modified type or the operand of _Countof has
4028 : a variable number of elements or the operand of _Generic is the one selected
4029 : as the result. */
4030 :
4031 : static void
4032 273 : record_maybe_used_decl (tree decl, bool address)
4033 : {
4034 273 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
4035 273 : t->decl = decl;
4036 273 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
4037 273 : t->address = address;
4038 273 : t->next = maybe_used_decls;
4039 273 : maybe_used_decls = t;
4040 273 : }
4041 :
4042 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
4043 : _Generic. If USED is false, just discard them. If it is true, mark them
4044 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
4045 : them to the next level up (if still inside sizeof or typeof or _Countof or
4046 : _Generic). */
4047 :
4048 : void
4049 1477572 : pop_maybe_used (bool used)
4050 : {
4051 1477572 : struct maybe_used_decl *p = maybe_used_decls;
4052 1477572 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
4053 1477869 : while (p && p->level > cur_level)
4054 : {
4055 297 : if (used)
4056 : {
4057 77 : if (cur_level == 0)
4058 53 : mark_decl_used (p->decl, p->address);
4059 : else
4060 24 : p->level = cur_level;
4061 : }
4062 297 : p = p->next;
4063 : }
4064 1477572 : if (!used || cur_level == 0)
4065 1477491 : maybe_used_decls = p;
4066 1477572 : }
4067 :
4068 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
4069 : _Generic, without acting on them, and return the pointer to the previous top
4070 : of the stack. This for use at the end of a default generic association when
4071 : it is not yet known whether the expression is used. If it later turns out
4072 : the expression is used (or treated as used before C23), restore_maybe_used
4073 : should be called on the return value followed by pop_maybe_used (true);
4074 : otherwise, the return value can be discarded. */
4075 :
4076 : struct maybe_used_decl *
4077 1217 : save_maybe_used ()
4078 : {
4079 1217 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
4080 1217 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
4081 1249 : while (p && p->level > cur_level)
4082 32 : p = p->next;
4083 1217 : maybe_used_decls = p;
4084 1217 : return orig;
4085 : }
4086 :
4087 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
4088 : or _Generic returned by save_maybe_used. It is required that the stack is
4089 : at exactly the point where it was left by save_maybe_used. */
4090 :
4091 : void
4092 283 : restore_maybe_used (struct maybe_used_decl *stack)
4093 : {
4094 283 : maybe_used_decls = stack;
4095 283 : }
4096 :
4097 : /* Return the result of sizeof applied to EXPR. */
4098 :
4099 : struct c_expr
4100 316235 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4101 : {
4102 316235 : struct c_expr ret;
4103 316235 : if (expr.value == error_mark_node)
4104 : {
4105 111 : ret.value = error_mark_node;
4106 111 : ret.original_code = ERROR_MARK;
4107 111 : ret.original_type = NULL;
4108 111 : ret.m_decimal = 0;
4109 111 : pop_maybe_used (false);
4110 : }
4111 : else
4112 : {
4113 316124 : bool expr_const_operands = true;
4114 :
4115 316124 : if (TREE_CODE (expr.value) == PARM_DECL
4116 316124 : && C_ARRAY_PARAMETER (expr.value))
4117 : {
4118 55 : auto_diagnostic_group d;
4119 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4120 : "%<sizeof%> on array function parameter %qE will "
4121 : "return size of %qT", expr.value,
4122 55 : TREE_TYPE (expr.value)))
4123 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4124 55 : }
4125 316124 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4126 : &expr_const_operands);
4127 316124 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4128 316124 : c_last_sizeof_arg = expr.value;
4129 316124 : c_last_sizeof_loc = loc;
4130 316124 : ret.original_code = SIZEOF_EXPR;
4131 316124 : ret.original_type = NULL;
4132 316124 : ret.m_decimal = 0;
4133 316124 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4134 : {
4135 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4136 292 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4137 : folded_expr, ret.value);
4138 292 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4139 292 : SET_EXPR_LOCATION (ret.value, loc);
4140 : }
4141 316124 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4142 : }
4143 316235 : return ret;
4144 : }
4145 :
4146 : /* Return the result of sizeof applied to T, a structure for the type
4147 : name passed to sizeof (rather than the type itself). LOC is the
4148 : location of the original expression. */
4149 :
4150 : struct c_expr
4151 334743 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4152 : {
4153 334743 : tree type;
4154 334743 : struct c_expr ret;
4155 334743 : tree type_expr = NULL_TREE;
4156 334743 : bool type_expr_const = true;
4157 334743 : type = groktypename (t, &type_expr, &type_expr_const);
4158 334743 : ret.value = c_sizeof (loc, type);
4159 334743 : c_last_sizeof_arg = type;
4160 334743 : c_last_sizeof_loc = loc;
4161 334743 : ret.original_code = SIZEOF_EXPR;
4162 334743 : ret.original_type = NULL;
4163 334743 : ret.m_decimal = 0;
4164 334743 : if (type == error_mark_node)
4165 : {
4166 5 : ret.value = error_mark_node;
4167 5 : ret.original_code = ERROR_MARK;
4168 : }
4169 : else
4170 334696 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4171 669349 : && C_TYPE_VARIABLE_SIZE (type))
4172 : {
4173 : /* If the type is a [*] array, it is a VLA but is represented as
4174 : having a size of zero. In such a case we must ensure that
4175 : the result of sizeof does not get folded to a constant by
4176 : c_fully_fold, because if the size is evaluated the result is
4177 : not constant and so constraints on zero or negative size
4178 : arrays must not be applied when this sizeof call is inside
4179 : another array declarator. */
4180 41 : if (!type_expr)
4181 16 : type_expr = integer_zero_node;
4182 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4183 : type_expr, ret.value);
4184 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4185 : }
4186 334743 : pop_maybe_used (type != error_mark_node
4187 334743 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4188 334743 : return ret;
4189 : }
4190 :
4191 : static bool
4192 2175154 : top_array_vla_p (const_tree type)
4193 : {
4194 2175154 : if (TREE_CODE (type) != ARRAY_TYPE)
4195 : return false;
4196 :
4197 2175136 : tree d = TYPE_DOMAIN (type);
4198 :
4199 2175136 : if (!d)
4200 : return false;
4201 2080408 : if (!TYPE_MAX_VALUE (d))
4202 : return false;
4203 :
4204 1988039 : return TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST
4205 1988039 : || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST;
4206 : }
4207 :
4208 : /* Return the result of countof applied to EXPR. */
4209 :
4210 : struct c_expr
4211 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4212 : {
4213 54 : struct c_expr ret;
4214 54 : if (expr.value == error_mark_node)
4215 : {
4216 1 : ret.value = error_mark_node;
4217 1 : ret.original_code = ERROR_MARK;
4218 1 : ret.original_type = NULL;
4219 1 : ret.m_decimal = 0;
4220 1 : pop_maybe_used (false);
4221 : }
4222 : else
4223 : {
4224 53 : bool expr_const_operands = true;
4225 :
4226 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4227 : &expr_const_operands);
4228 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4229 53 : c_last_sizeof_arg = expr.value;
4230 53 : c_last_sizeof_loc = loc;
4231 53 : ret.original_code = COUNTOF_EXPR;
4232 53 : ret.original_type = NULL;
4233 53 : ret.m_decimal = 0;
4234 53 : if (top_array_vla_p (TREE_TYPE (folded_expr)))
4235 : {
4236 : /* countof is evaluated when given a vla. */
4237 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4238 : folded_expr, ret.value);
4239 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4240 11 : SET_EXPR_LOCATION (ret.value, loc);
4241 : }
4242 53 : pop_maybe_used (top_array_vla_p (TREE_TYPE (folded_expr)));
4243 : }
4244 54 : return ret;
4245 : }
4246 :
4247 : /* Return the result of countof applied to T, a structure for the type
4248 : name passed to countof (rather than the type itself). LOC is the
4249 : location of the original expression. */
4250 :
4251 : struct c_expr
4252 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4253 : {
4254 18 : tree type;
4255 18 : struct c_expr ret;
4256 18 : tree type_expr = NULL_TREE;
4257 18 : bool type_expr_const = true;
4258 18 : type = groktypename (t, &type_expr, &type_expr_const);
4259 18 : ret.value = c_countof_type (loc, type);
4260 18 : c_last_sizeof_arg = type;
4261 18 : c_last_sizeof_loc = loc;
4262 18 : ret.original_code = COUNTOF_EXPR;
4263 18 : ret.original_type = NULL;
4264 18 : ret.m_decimal = 0;
4265 18 : if (type == error_mark_node)
4266 : {
4267 0 : ret.value = error_mark_node;
4268 0 : ret.original_code = ERROR_MARK;
4269 : }
4270 : else
4271 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4272 22 : && top_array_vla_p (type))
4273 : {
4274 : /* If the type is a [*] array, it is a VLA but is represented as
4275 : having a size of zero. In such a case we must ensure that
4276 : the result of countof does not get folded to a constant by
4277 : c_fully_fold, because if the number of elements is evaluated
4278 : the result is not constant and so
4279 : constraints on zero or negative size arrays must not be applied
4280 : when this countof call is inside another array declarator. */
4281 6 : if (!type_expr)
4282 0 : type_expr = integer_zero_node;
4283 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4284 : type_expr, ret.value);
4285 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4286 : }
4287 18 : pop_maybe_used (type != error_mark_node ? top_array_vla_p (type) : false);
4288 18 : return ret;
4289 : }
4290 :
4291 : /* Return the result of maxof applied to T, a structure for the type
4292 : name passed to maxof (rather than the type itself). LOC is the
4293 : location of the original expression. */
4294 :
4295 : struct c_expr
4296 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4297 : {
4298 46 : tree type;
4299 46 : struct c_expr ret;
4300 46 : tree type_expr = NULL_TREE;
4301 46 : bool type_expr_const = true;
4302 46 : type = groktypename (t, &type_expr, &type_expr_const);
4303 46 : ret.value = c_maxof_type (loc, type);
4304 46 : c_last_sizeof_arg = type;
4305 46 : c_last_sizeof_loc = loc;
4306 46 : ret.original_code = MAXOF_EXPR;
4307 46 : ret.original_type = NULL;
4308 46 : ret.m_decimal = 0;
4309 46 : if (type == error_mark_node)
4310 : {
4311 0 : ret.value = error_mark_node;
4312 0 : ret.original_code = ERROR_MARK;
4313 : }
4314 46 : pop_maybe_used (type != error_mark_node);
4315 46 : return ret;
4316 : }
4317 :
4318 : /* Return the result of minof applied to T, a structure for the type
4319 : name passed to minof (rather than the type itself). LOC is the
4320 : location of the original expression. */
4321 :
4322 : struct c_expr
4323 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4324 : {
4325 46 : tree type;
4326 46 : struct c_expr ret;
4327 46 : tree type_expr = NULL_TREE;
4328 46 : bool type_expr_const = true;
4329 46 : type = groktypename (t, &type_expr, &type_expr_const);
4330 46 : ret.value = c_minof_type (loc, type);
4331 46 : c_last_sizeof_arg = type;
4332 46 : c_last_sizeof_loc = loc;
4333 46 : ret.original_code = MINOF_EXPR;
4334 46 : ret.original_type = NULL;
4335 46 : ret.m_decimal = 0;
4336 46 : if (type == error_mark_node)
4337 : {
4338 0 : ret.value = error_mark_node;
4339 0 : ret.original_code = ERROR_MARK;
4340 : }
4341 46 : pop_maybe_used (type != error_mark_node);
4342 46 : return ret;
4343 : }
4344 :
4345 : /* Build a function call to function FUNCTION with parameters PARAMS.
4346 : The function call is at LOC.
4347 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4348 : TREE_VALUE of each node is a parameter-expression.
4349 : FUNCTION's data type may be a function type or a pointer-to-function. */
4350 :
4351 : tree
4352 0 : build_function_call (location_t loc, tree function, tree params)
4353 : {
4354 0 : vec<tree, va_gc> *v;
4355 0 : tree ret;
4356 :
4357 0 : vec_alloc (v, list_length (params));
4358 0 : for (; params; params = TREE_CHAIN (params))
4359 0 : v->quick_push (TREE_VALUE (params));
4360 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4361 0 : vec_free (v);
4362 0 : return ret;
4363 : }
4364 :
4365 : /* Give a note about the location of the declaration of DECL,
4366 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4367 :
4368 : static void
4369 299 : inform_declaration (tree decl, tree function_expr)
4370 : {
4371 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4372 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4373 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4374 92 : else if (function_expr)
4375 92 : switch (TREE_CODE (function_expr))
4376 : {
4377 : default:
4378 : break;
4379 13 : case COMPONENT_REF:
4380 : /* Show the decl of the pertinent field (e.g. for callback
4381 : fields in a struct. */
4382 13 : {
4383 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4384 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4385 13 : inform (loc, "declared here");
4386 : }
4387 : break;
4388 : }
4389 299 : }
4390 :
4391 :
4392 : /* Build a function call to function FUNCTION with parameters PARAMS.
4393 : If FUNCTION is the result of resolving an overloaded target built-in,
4394 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4395 : ORIGTYPES, if not NULL, is a vector of types; each element is
4396 : either NULL or the original type of the corresponding element in
4397 : PARAMS. The original type may differ from TREE_TYPE of the
4398 : parameter for enums. FUNCTION's data type may be a function type
4399 : or pointer-to-function. This function changes the elements of
4400 : PARAMS. */
4401 :
4402 : tree
4403 51940610 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4404 : tree function, vec<tree, va_gc> *params,
4405 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4406 : {
4407 51940610 : tree fntype, fundecl = NULL_TREE;
4408 51940610 : tree name = NULL_TREE, result;
4409 51940610 : tree tem;
4410 51940610 : int nargs;
4411 51940610 : tree *argarray;
4412 :
4413 :
4414 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4415 51940610 : STRIP_TYPE_NOPS (function);
4416 :
4417 : /* Convert anything with function type to a pointer-to-function. */
4418 51940610 : if (TREE_CODE (function) == FUNCTION_DECL)
4419 : {
4420 51862607 : name = DECL_NAME (function);
4421 :
4422 51862607 : if (flag_tm)
4423 422 : tm_malloc_replacement (function);
4424 51862607 : fundecl = function;
4425 51862607 : if (!orig_fundecl)
4426 51862607 : orig_fundecl = fundecl;
4427 : /* Atomic functions have type checking/casting already done. They are
4428 : often rewritten and don't match the original parameter list. */
4429 103725214 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4430 : origtypes = NULL;
4431 : }
4432 51940610 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4433 51877315 : function = function_to_pointer_conversion (loc, function);
4434 :
4435 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4436 : expressions, like those used for ObjC messenger dispatches. */
4437 51940610 : if (params && !params->is_empty ())
4438 39575790 : function = objc_rewrite_function_call (function, (*params)[0]);
4439 :
4440 51940610 : function = c_fully_fold (function, false, NULL);
4441 :
4442 51940610 : fntype = TREE_TYPE (function);
4443 :
4444 51940610 : if (TREE_CODE (fntype) == ERROR_MARK)
4445 5 : return error_mark_node;
4446 :
4447 51940605 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4448 51940571 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4449 : {
4450 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4451 33 : error_at (loc,
4452 : "called object %qE is not a function or function pointer",
4453 : function);
4454 4 : else if (DECL_P (function))
4455 : {
4456 1 : auto_diagnostic_group d;
4457 1 : error_at (loc,
4458 : "called object %qD is not a function or function pointer",
4459 : function);
4460 1 : inform_declaration (function, NULL_TREE);
4461 1 : }
4462 : else
4463 3 : error_at (loc,
4464 : "called object is not a function or function pointer");
4465 37 : return error_mark_node;
4466 : }
4467 :
4468 51940568 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4469 388235 : current_function_returns_abnormally = 1;
4470 :
4471 : /* fntype now gets the type of function pointed to. */
4472 51940568 : fntype = TREE_TYPE (fntype);
4473 51940568 : tree return_type = TREE_TYPE (fntype);
4474 :
4475 : /* Convert the parameters to the types declared in the
4476 : function prototype, or apply default promotions. */
4477 :
4478 51940568 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4479 : origtypes, function, fundecl);
4480 51940568 : if (nargs < 0)
4481 177 : return error_mark_node;
4482 :
4483 : /* Check that the function is called through a compatible prototype.
4484 : If it is not, warn. */
4485 51939476 : if (CONVERT_EXPR_P (function)
4486 931 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4487 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4488 51940576 : && !comptypes (fntype, TREE_TYPE (tem)))
4489 : {
4490 : /* This situation leads to run-time undefined behavior. We can't,
4491 : therefore, simply error unless we can prove that all possible
4492 : executions of the program must execute the code. */
4493 20 : warning_at (loc, 0, "function called through a non-compatible type");
4494 :
4495 20 : if (VOID_TYPE_P (return_type)
4496 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4497 1 : pedwarn (loc, 0,
4498 : "function with qualified void return type called");
4499 : }
4500 :
4501 51940391 : argarray = vec_safe_address (params);
4502 :
4503 : /* Check that arguments to builtin functions match the expectations. */
4504 51940391 : if (fundecl
4505 51862445 : && fndecl_built_in_p (fundecl)
4506 86536360 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4507 : orig_fundecl, nargs, argarray))
4508 338 : return error_mark_node;
4509 :
4510 : /* Check that the arguments to the function are valid. */
4511 51940053 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4512 : nargs, argarray, &arg_loc,
4513 : comptypes);
4514 :
4515 51940053 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4516 13 : && !VOID_TYPE_P (return_type))
4517 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4518 51940053 : if (name != NULL_TREE
4519 103802160 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4520 : {
4521 33860500 : if (require_constant_value)
4522 3516 : result
4523 3516 : = fold_build_call_array_initializer_loc (loc, return_type,
4524 : function, nargs, argarray);
4525 : else
4526 33856984 : result = fold_build_call_array_loc (loc, return_type,
4527 : function, nargs, argarray);
4528 33860500 : if (TREE_CODE (result) == NOP_EXPR
4529 33860500 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4530 26692 : STRIP_TYPE_NOPS (result);
4531 : }
4532 : else
4533 18079553 : result = build_call_array_loc (loc, return_type,
4534 : function, nargs, argarray);
4535 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4536 : later. */
4537 51940053 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4538 215 : suppress_warning (result, OPT_Wnonnull);
4539 :
4540 : /* In this improbable scenario, a nested function returns a VM type.
4541 : Create a TARGET_EXPR so that the call always has a LHS, much as
4542 : what the C++ FE does for functions returning non-PODs. */
4543 51940053 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4544 : {
4545 83 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4546 83 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4547 : NULL_TREE, NULL_TREE);
4548 : }
4549 :
4550 51940053 : if (VOID_TYPE_P (TREE_TYPE (result)))
4551 : {
4552 2523792 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4553 2 : pedwarn (loc, 0,
4554 : "function with qualified void return type called");
4555 : return result;
4556 : }
4557 49416261 : return require_complete_type (loc, result);
4558 : }
4559 :
4560 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4561 :
4562 : tree
4563 51940165 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4564 : tree function, vec<tree, va_gc> *params,
4565 : vec<tree, va_gc> *origtypes)
4566 : {
4567 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4568 51940176 : STRIP_TYPE_NOPS (function);
4569 :
4570 : /* Convert anything with function type to a pointer-to-function. */
4571 51940165 : if (TREE_CODE (function) == FUNCTION_DECL)
4572 : {
4573 : /* Implement type-directed function overloading for builtins.
4574 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4575 : handle all the type checking. The result is a complete expression
4576 : that implements this function call. */
4577 51862162 : tree tem = resolve_overloaded_builtin (loc, function, params);
4578 51862162 : if (tem)
4579 : return tem;
4580 : }
4581 51852800 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4582 : }
4583 :
4584 : /* Helper for convert_arguments called to convert the VALue of argument
4585 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4586 : PARMNUM and TYPE.
4587 : PLOC is the location where the conversion is being performed.
4588 : FUNCTION and FUNDECL are the same as in convert_arguments.
4589 : VALTYPE is the original type of VAL before the conversion and,
4590 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4591 : NPC is true if VAL represents the null pointer constant (VAL itself
4592 : will have been folded to an integer constant).
4593 : RNAME is the same as FUNCTION except in Objective C when it's
4594 : the function selector.
4595 : EXCESS_PRECISION is true when VAL was originally represented
4596 : as EXCESS_PRECISION_EXPR.
4597 : WARNOPT is the same as in convert_for_assignment. */
4598 :
4599 : static tree
4600 129916116 : convert_argument (location_t ploc, tree function, tree fundecl,
4601 : tree type, tree origtype, tree val, tree valtype,
4602 : bool npc, tree rname, int parmnum, int argnum,
4603 : bool excess_precision, int warnopt)
4604 : {
4605 : /* Formal parm type is specified by a function prototype. */
4606 :
4607 129916116 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4608 : {
4609 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4610 : parmnum + 1);
4611 3 : return error_mark_node;
4612 : }
4613 :
4614 : /* Optionally warn about conversions that differ from the default
4615 : conversions. */
4616 129916113 : if (warn_traditional_conversion || warn_traditional)
4617 : {
4618 193 : if (INTEGRAL_TYPE_P (type)
4619 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4620 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4621 : "passing argument %d of %qE as integer rather "
4622 : "than floating due to prototype",
4623 : argnum, rname);
4624 193 : if (INTEGRAL_TYPE_P (type)
4625 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4626 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4627 : "passing argument %d of %qE as integer rather "
4628 : "than complex due to prototype",
4629 : argnum, rname);
4630 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4631 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4632 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4633 : "passing argument %d of %qE as complex rather "
4634 : "than floating due to prototype",
4635 : argnum, rname);
4636 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4637 71 : && INTEGRAL_TYPE_P (valtype))
4638 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4639 : "passing argument %d of %qE as floating rather "
4640 : "than integer due to prototype",
4641 : argnum, rname);
4642 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4643 7 : && INTEGRAL_TYPE_P (valtype))
4644 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4645 : "passing argument %d of %qE as complex rather "
4646 : "than integer due to prototype",
4647 : argnum, rname);
4648 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4649 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4650 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4651 : "passing argument %d of %qE as floating rather "
4652 : "than complex due to prototype",
4653 : argnum, rname);
4654 : /* ??? At some point, messages should be written about
4655 : conversions between complex types, but that's too messy
4656 : to do now. */
4657 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4658 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4659 : {
4660 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4661 :
4662 : /* Warn if any argument is passed as `float',
4663 : since without a prototype it would be `double'. */
4664 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4665 45 : && type != dfloat32_type_node)
4666 7 : warning_at (ploc, 0,
4667 : "passing argument %d of %qE as %<float%> "
4668 : "rather than %<double%> due to prototype",
4669 : argnum, rname);
4670 :
4671 : /* Warn if mismatch between argument and prototype
4672 : for decimal float types. Warn of conversions with
4673 : binary float types and of precision narrowing due to
4674 : prototype. */
4675 38 : else if (type != valtype
4676 32 : && (type == dfloat32_type_node
4677 22 : || type == dfloat64_type_node
4678 12 : || type == dfloat128_type_node
4679 2 : || valtype == dfloat32_type_node
4680 2 : || valtype == dfloat64_type_node
4681 2 : || valtype == dfloat128_type_node)
4682 38 : && (formal_prec
4683 30 : <= TYPE_PRECISION (valtype)
4684 14 : || (type == dfloat128_type_node
4685 10 : && (valtype
4686 10 : != dfloat64_type_node
4687 8 : && (valtype
4688 : != dfloat32_type_node)))
4689 8 : || (type == dfloat64_type_node
4690 4 : && (valtype
4691 : != dfloat32_type_node))))
4692 24 : warning_at (ploc, 0,
4693 : "passing argument %d of %qE as %qT "
4694 : "rather than %qT due to prototype",
4695 : argnum, rname, type, valtype);
4696 :
4697 : }
4698 : /* Detect integer changing in width or signedness.
4699 : These warnings are only activated with
4700 : -Wtraditional-conversion, not with -Wtraditional. */
4701 105 : else if (warn_traditional_conversion
4702 105 : && INTEGRAL_TYPE_P (type)
4703 102 : && INTEGRAL_TYPE_P (valtype))
4704 : {
4705 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4706 83 : tree would_have_been = default_conversion (val);
4707 83 : tree type1 = TREE_TYPE (would_have_been);
4708 :
4709 83 : if (val == error_mark_node)
4710 : /* VAL could have been of incomplete type. */;
4711 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4712 83 : && (TYPE_MAIN_VARIANT (type)
4713 2 : == TYPE_MAIN_VARIANT (valtype)))
4714 : /* No warning if function asks for enum
4715 : and the actual arg is that enum type. */
4716 : ;
4717 81 : else if (formal_prec != TYPE_PRECISION (type1))
4718 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4719 : "passing argument %d of %qE "
4720 : "with different width due to prototype",
4721 : argnum, rname);
4722 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4723 : ;
4724 : /* Don't complain if the formal parameter type
4725 : is an enum, because we can't tell now whether
4726 : the value was an enum--even the same enum. */
4727 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4728 : ;
4729 32 : else if (TREE_CODE (val) == INTEGER_CST
4730 5 : && int_fits_type_p (val, type))
4731 : /* Change in signedness doesn't matter
4732 : if a constant value is unaffected. */
4733 : ;
4734 : /* If the value is extended from a narrower
4735 : unsigned type, it doesn't matter whether we
4736 : pass it as signed or unsigned; the value
4737 : certainly is the same either way. */
4738 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4739 32 : && TYPE_UNSIGNED (valtype))
4740 : ;
4741 32 : else if (TYPE_UNSIGNED (type))
4742 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4743 : "passing argument %d of %qE "
4744 : "as unsigned due to prototype",
4745 : argnum, rname);
4746 : else
4747 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4748 : "passing argument %d of %qE "
4749 : "as signed due to prototype",
4750 : argnum, rname);
4751 : }
4752 : }
4753 :
4754 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4755 : sake of better warnings from convert_and_check. */
4756 129916113 : if (excess_precision)
4757 380 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4758 :
4759 129916113 : tree parmval = convert_for_assignment (ploc, ploc, type,
4760 : val, origtype, ic_argpass,
4761 : npc, fundecl, function,
4762 : parmnum + 1, warnopt);
4763 129916113 : return parmval;
4764 : }
4765 :
4766 : /* Convert the argument expressions in the vector VALUES
4767 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4768 :
4769 : If the list is exhausted, or when an element has NULL as its type,
4770 : perform the default conversions.
4771 :
4772 : ORIGTYPES is the original types of the expressions in VALUES. This
4773 : holds the type of enum values which have been converted to integral
4774 : types. It may be NULL.
4775 :
4776 : FUNCTION is a tree for the called function. It is used only for
4777 : error messages, where it is formatted with %qE.
4778 :
4779 : This is also where warnings about wrong number of args are generated.
4780 :
4781 : ARG_LOC are locations of function arguments (if any).
4782 :
4783 : Returns the actual number of arguments processed (which may be less
4784 : than the length of VALUES in some error situations), or -1 on
4785 : failure. */
4786 :
4787 : static int
4788 51940568 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4789 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4790 : tree function, tree fundecl)
4791 : {
4792 51940568 : tree typelist = TYPE_ARG_TYPES (fntype);
4793 51940568 : unsigned int parmnum;
4794 51940568 : bool error_args = false;
4795 51940568 : const bool type_generic = fundecl
4796 51940568 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4797 51940568 : bool type_generic_remove_excess_precision = false;
4798 51940568 : bool type_generic_overflow_p = false;
4799 51940568 : bool type_generic_bit_query = false;
4800 51940568 : tree selector;
4801 :
4802 : /* Change pointer to function to the function itself for
4803 : diagnostics. */
4804 51940568 : if (TREE_CODE (function) == ADDR_EXPR
4805 51940568 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4806 51864705 : function = TREE_OPERAND (function, 0);
4807 :
4808 : /* Handle an ObjC selector specially for diagnostics. */
4809 51940568 : selector = objc_message_selector ();
4810 :
4811 : /* For a call to a built-in function declared without a prototype,
4812 : set to the built-in function's argument list. */
4813 51940568 : tree builtin_typelist = NULL_TREE;
4814 :
4815 : /* For type-generic built-in functions, determine whether excess
4816 : precision should be removed (classification) or not
4817 : (comparison). */
4818 51940568 : if (fundecl
4819 51940568 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4820 : {
4821 1424624 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4822 1424624 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4823 : {
4824 : /* For a call to a built-in function declared without a prototype
4825 : use the types of the parameters of the internal built-in to
4826 : match those of the arguments to. */
4827 702908 : if (tree bdecl = builtin_decl_explicit (code))
4828 702908 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4829 : }
4830 :
4831 : /* For type-generic built-in functions, determine whether excess
4832 : precision should be removed (classification) or not
4833 : (comparison). */
4834 1424624 : if (type_generic)
4835 61629 : switch (code)
4836 : {
4837 5444 : case BUILT_IN_ISFINITE:
4838 5444 : case BUILT_IN_ISINF:
4839 5444 : case BUILT_IN_ISINF_SIGN:
4840 5444 : case BUILT_IN_ISNAN:
4841 5444 : case BUILT_IN_ISNORMAL:
4842 5444 : case BUILT_IN_ISSIGNALING:
4843 5444 : case BUILT_IN_FPCLASSIFY:
4844 5444 : type_generic_remove_excess_precision = true;
4845 5444 : break;
4846 :
4847 21047 : case BUILT_IN_ADD_OVERFLOW_P:
4848 21047 : case BUILT_IN_SUB_OVERFLOW_P:
4849 21047 : case BUILT_IN_MUL_OVERFLOW_P:
4850 : /* The last argument of these type-generic builtins
4851 : should not be promoted. */
4852 21047 : type_generic_overflow_p = true;
4853 21047 : break;
4854 :
4855 1557 : case BUILT_IN_CLZG:
4856 1557 : case BUILT_IN_CTZG:
4857 1557 : case BUILT_IN_CLRSBG:
4858 1557 : case BUILT_IN_FFSG:
4859 1557 : case BUILT_IN_PARITYG:
4860 1557 : case BUILT_IN_POPCOUNTG:
4861 : /* The first argument of these type-generic builtins
4862 : should not be promoted. */
4863 1557 : type_generic_bit_query = true;
4864 1557 : break;
4865 :
4866 : default:
4867 : break;
4868 : }
4869 : }
4870 :
4871 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4872 : individual converted arguments. */
4873 :
4874 51940568 : tree typetail, builtin_typetail, val;
4875 130857247 : for (typetail = typelist,
4876 51940568 : builtin_typetail = builtin_typelist,
4877 51940568 : parmnum = 0;
4878 182797815 : values && values->iterate (parmnum, &val);
4879 : ++parmnum)
4880 : {
4881 : /* The type of the function parameter (if it was declared with one). */
4882 260771513 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4883 : /* The type of the built-in function parameter (if the function
4884 : is a built-in). Used to detect type incompatibilities in
4885 : calls to built-ins declared without a prototype. */
4886 130857283 : tree builtin_type = (builtin_typetail
4887 131905988 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4888 : /* The original type of the argument being passed to the function. */
4889 130857283 : tree valtype = TREE_TYPE (val);
4890 : /* The called function (or function selector in Objective C). */
4891 130857283 : tree rname = function;
4892 130857283 : int argnum = parmnum + 1;
4893 130857283 : const char *invalid_func_diag;
4894 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4895 130857283 : bool excess_precision = false;
4896 : /* The value of the argument after conversion to the type
4897 : of the function parameter it is passed to. */
4898 130857283 : tree parmval;
4899 : /* Some __atomic_* builtins have additional hidden argument at
4900 : position 0. */
4901 130857283 : location_t ploc
4902 130584777 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4903 130584177 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4904 130857283 : : input_location;
4905 :
4906 130857283 : if (type == void_type_node)
4907 : {
4908 33 : auto_diagnostic_group d;
4909 33 : int num_expected = parmnum;
4910 33 : int num_actual = values->length ();
4911 33 : gcc_rich_location rich_loc (loc);
4912 33 : if (ploc != input_location)
4913 32 : rich_loc.add_range (ploc);
4914 33 : if (selector)
4915 0 : error_at (&rich_loc,
4916 : "too many arguments to method %qE; expected %i, have %i",
4917 : selector, num_expected, num_actual);
4918 : else
4919 33 : error_at (&rich_loc,
4920 : "too many arguments to function %qE; expected %i, have %i",
4921 : function, num_expected, num_actual);
4922 33 : inform_declaration (fundecl, function);
4923 65 : return error_args ? -1 : (int) parmnum;
4924 33 : }
4925 :
4926 130857250 : if (builtin_type == void_type_node)
4927 : {
4928 12 : auto_diagnostic_group d;
4929 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4930 : "too many arguments to built-in function %qE "
4931 : "expecting %d", function, parmnum))
4932 12 : inform_declaration (fundecl, function);
4933 12 : builtin_typetail = NULL_TREE;
4934 12 : }
4935 :
4936 91386 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4937 130948517 : && !(fundecl && fndecl_built_in_p (fundecl)))
4938 : {
4939 6962 : auto_diagnostic_group d;
4940 6962 : bool warned;
4941 6962 : if (selector)
4942 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4943 : "ISO C23 does not allow arguments"
4944 : " for method %qE declared without parameters",
4945 : function);
4946 : else
4947 6962 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4948 : "ISO C23 does not allow arguments"
4949 : " for function %qE declared without parameters",
4950 : function);
4951 6962 : if (warned)
4952 4 : inform_declaration (fundecl, function);
4953 6962 : }
4954 :
4955 130857250 : if (selector && argnum > 2)
4956 : {
4957 0 : rname = selector;
4958 0 : argnum -= 2;
4959 : }
4960 :
4961 : /* Determine if VAL is a null pointer constant before folding it. */
4962 130857250 : bool npc = null_pointer_constant_p (val);
4963 :
4964 : /* If there is excess precision and a prototype, convert once to
4965 : the required type rather than converting via the semantic
4966 : type. Likewise without a prototype a float value represented
4967 : as long double should be converted once to double. But for
4968 : type-generic classification functions excess precision must
4969 : be removed here. */
4970 130857250 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4971 433 : && (type || !type_generic || !type_generic_remove_excess_precision))
4972 : {
4973 413 : val = TREE_OPERAND (val, 0);
4974 413 : excess_precision = true;
4975 : }
4976 130857250 : val = c_fully_fold (val, false, NULL);
4977 261714501 : STRIP_TYPE_NOPS (val);
4978 :
4979 130857250 : val = require_complete_type (ploc, val);
4980 :
4981 : /* Some floating-point arguments must be promoted to double when
4982 : no type is specified by a prototype. This applies to
4983 : arguments of type float, and to architecture-specific types
4984 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4985 130857250 : bool promote_float_arg = false;
4986 130857250 : if (type == NULL_TREE
4987 943053 : && TREE_CODE (valtype) == REAL_TYPE
4988 270028 : && (TYPE_PRECISION (valtype)
4989 270028 : <= TYPE_PRECISION (double_type_node))
4990 262770 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4991 131514 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4992 130988764 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4993 : {
4994 : /* Promote this argument, unless it has a _FloatN or
4995 : _FloatNx type. */
4996 1028614 : promote_float_arg = true;
4997 1028614 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4998 901110 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4999 : {
5000 : promote_float_arg = false;
5001 : break;
5002 : }
5003 : /* Don't promote __bf16 either. */
5004 130781 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
5005 130726539 : promote_float_arg = false;
5006 : }
5007 :
5008 130857250 : if (type != NULL_TREE)
5009 : {
5010 129914197 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5011 129914197 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
5012 : val, valtype, npc, rname, parmnum, argnum,
5013 : excess_precision, 0);
5014 : }
5015 : /* A NULLPTR type is just a nullptr always. */
5016 943053 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
5017 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
5018 943044 : else if (promote_float_arg)
5019 : {
5020 127434 : if (type_generic)
5021 : parmval = val;
5022 : else
5023 : {
5024 : /* Convert `float' to `double'. */
5025 124528 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5026 6 : warning_at (ploc, OPT_Wdouble_promotion,
5027 : "implicit conversion from %qT to %qT when passing "
5028 : "argument to function",
5029 : valtype, double_type_node);
5030 124528 : parmval = convert (double_type_node, val);
5031 : }
5032 : }
5033 815610 : else if ((excess_precision && !type_generic)
5034 815608 : || (type_generic_overflow_p && parmnum == 2)
5035 794565 : || (type_generic_bit_query && parmnum == 0))
5036 : /* A "double" argument with excess precision being passed
5037 : without a prototype or in variable arguments.
5038 : The last argument of __builtin_*_overflow_p should not be
5039 : promoted, similarly the first argument of
5040 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
5041 22596 : parmval = convert (valtype, val);
5042 1586028 : else if ((invalid_func_diag =
5043 793014 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
5044 : {
5045 0 : error (invalid_func_diag);
5046 0 : return -1;
5047 : }
5048 793014 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
5049 : {
5050 : return -1;
5051 : }
5052 : else
5053 : /* Convert `short' and `char' to full-size `int'. */
5054 793011 : parmval = default_conversion (val);
5055 :
5056 130857247 : (*values)[parmnum] = parmval;
5057 130857247 : if (parmval == error_mark_node)
5058 104 : error_args = true;
5059 :
5060 130857247 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
5061 : {
5062 : /* For a call to a built-in function declared without a prototype,
5063 : perform the conversions from the argument to the expected type
5064 : but issue warnings rather than errors for any mismatches.
5065 : Ignore the converted argument and use the PARMVAL obtained
5066 : above by applying default conversions instead. */
5067 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5068 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
5069 : val, valtype, npc, rname, parmnum, argnum,
5070 : excess_precision,
5071 : OPT_Wbuiltin_declaration_mismatch);
5072 : }
5073 :
5074 130857247 : if (typetail)
5075 129914197 : typetail = TREE_CHAIN (typetail);
5076 :
5077 130857247 : if (builtin_typetail)
5078 1048688 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5079 : }
5080 :
5081 91516275 : gcc_assert (parmnum == vec_safe_length (values));
5082 :
5083 103546331 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5084 : {
5085 : /* Not enough args.
5086 : Determine minimum number of arguments required. */
5087 : int min_expected_num = 0;
5088 231 : bool at_least_p = false;
5089 : tree iter = typelist;
5090 386 : while (true)
5091 : {
5092 231 : if (!iter)
5093 : {
5094 : /* Variadic arguments; stop iterating. */
5095 : at_least_p = true;
5096 : break;
5097 : }
5098 222 : if (iter == void_list_node)
5099 : /* End of arguments; stop iterating. */
5100 : break;
5101 155 : ++min_expected_num;
5102 155 : iter = TREE_CHAIN (iter);
5103 : }
5104 76 : auto_diagnostic_group d;
5105 76 : int actual_num = vec_safe_length (values);
5106 143 : error_at (loc,
5107 : at_least_p
5108 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5109 : : G_("too few arguments to function %qE; expected %i, have %i"),
5110 : function, min_expected_num, actual_num);
5111 76 : inform_declaration (fundecl, function);
5112 76 : return -1;
5113 76 : }
5114 :
5115 52606846 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5116 : {
5117 616 : unsigned nargs = parmnum;
5118 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5119 428 : ++nargs;
5120 :
5121 188 : auto_diagnostic_group d;
5122 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5123 : "too few arguments to built-in function %qE "
5124 : "expecting %u", function, nargs - 1))
5125 173 : inform_declaration (fundecl, function);
5126 188 : }
5127 :
5128 51940456 : return error_args ? -1 : (int) parmnum;
5129 : }
5130 :
5131 : /* This is the entry point used by the parser to build unary operators
5132 : in the input. CODE, a tree_code, specifies the unary operator, and
5133 : ARG is the operand. For unary plus, the C parser currently uses
5134 : CONVERT_EXPR for code.
5135 :
5136 : LOC is the location to use for the tree generated.
5137 : */
5138 :
5139 : struct c_expr
5140 8509941 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5141 : {
5142 8509941 : struct c_expr result;
5143 :
5144 8509941 : result.original_code = code;
5145 8509941 : result.original_type = NULL;
5146 8509941 : result.m_decimal = 0;
5147 :
5148 8509941 : if (reject_gcc_builtin (arg.value))
5149 : {
5150 2 : result.value = error_mark_node;
5151 : }
5152 : else
5153 : {
5154 8509939 : result.value = build_unary_op (loc, code, arg.value, false);
5155 :
5156 8509939 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5157 5 : overflow_warning (loc, result.value, arg.value);
5158 : }
5159 :
5160 : /* We are typically called when parsing a prefix token at LOC acting on
5161 : ARG. Reflect this by updating the source range of the result to
5162 : start at LOC and end at the end of ARG. */
5163 8509941 : set_c_expr_source_range (&result,
5164 : loc, arg.get_finish ());
5165 :
5166 8509941 : return result;
5167 : }
5168 :
5169 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5170 :
5171 : bool
5172 1288424 : char_type_p (tree type)
5173 : {
5174 1288424 : return (type == char_type_node
5175 993566 : || type == unsigned_char_type_node
5176 974100 : || type == signed_char_type_node
5177 973132 : || type == char16_type_node
5178 2037648 : || type == char32_type_node);
5179 : }
5180 :
5181 : /* This is the entry point used by the parser to build binary operators
5182 : in the input. CODE, a tree_code, specifies the binary operator, and
5183 : ARG1 and ARG2 are the operands. In addition to constructing the
5184 : expression, we check for operands that were written with other binary
5185 : operators in a way that is likely to confuse the user. ORIG_ARG1 is
5186 : the original first operand for TRUTH_{AND,OR}IF_EXPR before it is
5187 : converted to truth value, otherwise NULL_TREE.
5188 :
5189 : LOCATION is the location of the binary operator. */
5190 :
5191 : struct c_expr
5192 10051976 : parser_build_binary_op (location_t location, enum tree_code code,
5193 : struct c_expr arg1, struct c_expr arg2, tree orig_arg1)
5194 : {
5195 10051976 : struct c_expr result;
5196 10051976 : result.m_decimal = 0;
5197 :
5198 10051976 : enum tree_code code1 = arg1.original_code;
5199 10051976 : enum tree_code code2 = arg2.original_code;
5200 10051976 : tree type1 = (arg1.original_type
5201 10051976 : ? arg1.original_type
5202 6648527 : : TREE_TYPE (arg1.value));
5203 10051976 : tree type2 = (arg2.original_type
5204 10051976 : ? arg2.original_type
5205 7569566 : : TREE_TYPE (arg2.value));
5206 :
5207 10051976 : result.value = build_binary_op (location, code,
5208 : arg1.value, arg2.value, true);
5209 10051975 : result.original_code = code;
5210 10051975 : result.original_type = NULL;
5211 10051975 : result.m_decimal = 0;
5212 :
5213 10051975 : if (TREE_CODE (result.value) == ERROR_MARK)
5214 : {
5215 1297 : set_c_expr_source_range (&result,
5216 : arg1.get_start (),
5217 : arg2.get_finish ());
5218 1297 : return result;
5219 : }
5220 :
5221 10050678 : if (location != UNKNOWN_LOCATION)
5222 10050678 : protected_set_expr_location (result.value, location);
5223 :
5224 10050678 : set_c_expr_source_range (&result,
5225 : arg1.get_start (),
5226 : arg2.get_finish ());
5227 :
5228 : /* Check for cases such as x+y<<z which users are likely
5229 : to misinterpret. */
5230 10050678 : if (warn_parentheses)
5231 2018141 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5232 : arg2.value);
5233 :
5234 10050678 : if (warn_logical_op)
5235 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5236 : code1, arg1.value, code2, arg2.value);
5237 :
5238 10050678 : if (warn_constant_logical_operand
5239 2016556 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5240 74934 : && INTEGRAL_NB_TYPE_P (type1)
5241 73104 : && INTEGRAL_NB_TYPE_P (type2))
5242 : {
5243 71499 : const char *name = code == TRUTH_ANDIF_EXPR ? "&&" : "||";
5244 71499 : if (orig_arg1 == NULL_TREE)
5245 0 : orig_arg1 = arg1.value;
5246 76892 : auto enum_other_than_0_1 = [] (tree type) {
5247 5393 : if (TREE_CODE (type) != ENUMERAL_TYPE)
5248 : return false;
5249 48 : for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
5250 : {
5251 40 : tree v = DECL_INITIAL (TREE_VALUE (l));
5252 40 : if (!integer_zerop (v) && !integer_onep (v))
5253 : return true;
5254 : }
5255 : return false;
5256 : };
5257 214481 : auto diagnose_constant_logical_operand = [=] (tree val, tree type) {
5258 142982 : if (TREE_CODE (val) != INTEGER_CST || integer_zerop (val))
5259 : return false;
5260 5417 : if (integer_onep (val) && !enum_other_than_0_1 (type))
5261 : return false;
5262 32 : gcc_rich_location richloc (location);
5263 32 : richloc.add_fixit_replace (name + 1);
5264 32 : auto_diagnostic_group d;
5265 32 : if (warning_at (location, OPT_Wconstant_logical_operand,
5266 : "use of logical %qs with constant operand %qE",
5267 : name, val))
5268 32 : inform (&richloc, "use %qs for bitwise operation", name + 1);
5269 32 : return true;
5270 32 : };
5271 71499 : if (!diagnose_constant_logical_operand (arg2.value, type2))
5272 71483 : diagnose_constant_logical_operand (orig_arg1, type1);
5273 : }
5274 :
5275 10050678 : if (warn_tautological_compare)
5276 : {
5277 2016654 : tree lhs = arg1.value;
5278 2016654 : tree rhs = arg2.value;
5279 2016654 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5280 : {
5281 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5282 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5283 : lhs = NULL_TREE;
5284 : else
5285 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5286 : }
5287 2016654 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5288 : {
5289 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5290 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5291 : rhs = NULL_TREE;
5292 : else
5293 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5294 : }
5295 2016654 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5296 2016654 : warn_tautological_cmp (location, code, lhs, rhs);
5297 : }
5298 :
5299 10050678 : if (warn_logical_not_paren
5300 2016737 : && TREE_CODE_CLASS (code) == tcc_comparison
5301 412377 : && code1 == TRUTH_NOT_EXPR
5302 412377 : && code2 != TRUTH_NOT_EXPR
5303 : /* Avoid warning for !!x == y. */
5304 10050812 : && (TREE_CODE (arg1.value) != NE_EXPR
5305 27 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5306 : {
5307 : /* Avoid warning for !b == y where b has _Bool type. */
5308 107 : tree t = integer_zero_node;
5309 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5310 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5311 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5312 : {
5313 90 : t = TREE_OPERAND (arg1.value, 0);
5314 199 : do
5315 : {
5316 199 : if (TREE_TYPE (t) != integer_type_node)
5317 : break;
5318 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5319 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5320 90 : else if (CONVERT_EXPR_P (t))
5321 19 : t = TREE_OPERAND (t, 0);
5322 : else
5323 : break;
5324 : }
5325 : while (1);
5326 : }
5327 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5328 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5329 : }
5330 :
5331 : /* Warn about comparisons against string literals, with the exception
5332 : of testing for equality or inequality of a string literal with NULL. */
5333 10050678 : if (code == EQ_EXPR || code == NE_EXPR)
5334 : {
5335 1194068 : if ((code1 == STRING_CST
5336 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5337 1194079 : || (code2 == STRING_CST
5338 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5339 36 : warning_at (location, OPT_Waddress,
5340 : "comparison with string literal results in unspecified behavior");
5341 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5342 1194068 : if (POINTER_TYPE_P (type1)
5343 92797 : && null_pointer_constant_p (arg2.value)
5344 1215509 : && char_type_p (type2))
5345 : {
5346 18 : auto_diagnostic_group d;
5347 18 : if (warning_at (location, OPT_Wpointer_compare,
5348 : "comparison between pointer and zero character "
5349 : "constant"))
5350 10 : inform (arg1.get_start (),
5351 : "did you mean to dereference the pointer?");
5352 18 : }
5353 1194050 : else if (POINTER_TYPE_P (type2)
5354 90115 : && null_pointer_constant_p (arg1.value)
5355 1194322 : && char_type_p (type1))
5356 : {
5357 10 : auto_diagnostic_group d;
5358 10 : if (warning_at (location, OPT_Wpointer_compare,
5359 : "comparison between pointer and zero character "
5360 : "constant"))
5361 10 : inform (arg2.get_start (),
5362 : "did you mean to dereference the pointer?");
5363 10 : }
5364 : }
5365 8856610 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5366 1020045 : && (code1 == STRING_CST || code2 == STRING_CST))
5367 0 : warning_at (location, OPT_Waddress,
5368 : "comparison with string literal results in unspecified "
5369 : "behavior");
5370 :
5371 10050678 : if (warn_zero_as_null_pointer_constant
5372 24 : && c_inhibit_evaluation_warnings == 0
5373 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5374 : {
5375 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5376 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5377 9 : && INTEGRAL_TYPE_P (type2)
5378 33 : && null_pointer_constant_p (arg2.value))
5379 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5380 : "zero as null pointer constant");
5381 :
5382 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5383 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5384 11 : && INTEGRAL_TYPE_P (type1)
5385 35 : && null_pointer_constant_p (arg1.value))
5386 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5387 : "zero as null pointer constant");
5388 : }
5389 :
5390 10050678 : if (warn_array_compare
5391 2016492 : && TREE_CODE_CLASS (code) == tcc_comparison
5392 412162 : && TREE_CODE (type1) == ARRAY_TYPE
5393 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5394 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5395 :
5396 2789906 : if (TREE_OVERFLOW_P (result.value)
5397 299 : && !TREE_OVERFLOW_P (arg1.value)
5398 10050970 : && !TREE_OVERFLOW_P (arg2.value))
5399 249 : overflow_warning (location, result.value);
5400 :
5401 : /* Warn about comparisons of different enum types. */
5402 10050677 : if (warn_enum_compare
5403 2037549 : && TREE_CODE_CLASS (code) == tcc_comparison
5404 417246 : && TREE_CODE (type1) == ENUMERAL_TYPE
5405 7917 : && TREE_CODE (type2) == ENUMERAL_TYPE
5406 10058538 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5407 2 : warning_at (location, OPT_Wenum_compare,
5408 : "comparison between %qT and %qT",
5409 : type1, type2);
5410 :
5411 10050677 : if (warn_xor_used_as_pow
5412 10050677 : && code == BIT_XOR_EXPR
5413 85096 : && arg1.m_decimal
5414 737 : && arg2.m_decimal)
5415 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5416 : location,
5417 : arg2.get_location (), arg2.value);
5418 :
5419 : return result;
5420 : }
5421 :
5422 : /* Return a tree for the difference of pointers OP0 and OP1.
5423 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5424 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5425 :
5426 : static tree
5427 3780 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5428 : {
5429 3780 : tree restype = ptrdiff_type_node;
5430 3780 : tree result, inttype;
5431 :
5432 3780 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5433 3780 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5434 3780 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5435 3780 : tree orig_op0 = op0;
5436 3780 : tree orig_op1 = op1;
5437 :
5438 : /* If the operands point into different address spaces, we need to
5439 : explicitly convert them to pointers into the common address space
5440 : before we can subtract the numerical address values. */
5441 3780 : if (as0 != as1)
5442 : {
5443 0 : addr_space_t as_common;
5444 0 : tree common_type;
5445 :
5446 : /* Determine the common superset address space. This is guaranteed
5447 : to exist because the caller verified that comp_target_types
5448 : returned non-zero. */
5449 0 : if (!addr_space_superset (as0, as1, &as_common))
5450 0 : gcc_unreachable ();
5451 :
5452 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5453 0 : op0 = convert (common_type, op0);
5454 0 : op1 = convert (common_type, op1);
5455 : }
5456 :
5457 : /* Determine integer type result of the subtraction. This will usually
5458 : be the same as the result type (ptrdiff_t), but may need to be a wider
5459 : type if pointers for the address space are wider than ptrdiff_t. */
5460 3780 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5461 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5462 : else
5463 : inttype = restype;
5464 :
5465 3780 : if (VOID_TYPE_P (target_type))
5466 150 : pedwarn (loc, OPT_Wpointer_arith,
5467 : "pointer of type %<void *%> used in subtraction");
5468 3780 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5469 4 : pedwarn (loc, OPT_Wpointer_arith,
5470 : "pointer to a function used in subtraction");
5471 :
5472 3780 : if (current_function_decl != NULL_TREE
5473 3780 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5474 : {
5475 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5476 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5477 :
5478 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5479 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5480 : }
5481 :
5482 : /* First do the subtraction, then build the divide operator
5483 : and only convert at the very end.
5484 : Do not do default conversions in case restype is a short type. */
5485 :
5486 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5487 : pointers. If some platform cannot provide that, or has a larger
5488 : ptrdiff_type to support differences larger than half the address
5489 : space, cast the pointers to some larger integer type and do the
5490 : computations in that type. */
5491 3780 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5492 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5493 : convert (inttype, op1), false);
5494 : else
5495 : {
5496 : /* Cast away qualifiers. */
5497 3780 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5498 3780 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5499 3780 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5500 : }
5501 :
5502 : /* This generates an error if op1 is pointer to incomplete type. */
5503 3780 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5504 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5505 3776 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5506 3776 : TREE_TYPE (TREE_TYPE (orig_op0))))
5507 7552 : verify_type_context (loc, TCTX_POINTER_ARITH,
5508 3776 : TREE_TYPE (TREE_TYPE (orig_op1)));
5509 :
5510 3780 : op1 = c_size_in_bytes (target_type);
5511 :
5512 3780 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5513 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5514 :
5515 : /* Divide by the size, in easiest possible way. */
5516 3780 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5517 : op0, convert (inttype, op1));
5518 :
5519 : /* Convert to final result type if necessary. */
5520 3780 : return convert (restype, result);
5521 : }
5522 :
5523 : /* Expand atomic compound assignments into an appropriate sequence as
5524 : specified by the C11 standard section 6.5.16.2.
5525 :
5526 : _Atomic T1 E1
5527 : T2 E2
5528 : E1 op= E2
5529 :
5530 : This sequence is used for all types for which these operations are
5531 : supported.
5532 :
5533 : In addition, built-in versions of the 'fe' prefixed routines may
5534 : need to be invoked for floating point (real, complex or vector) when
5535 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5536 :
5537 : T1 newval;
5538 : T1 old;
5539 : T1 *addr
5540 : T2 val
5541 : fenv_t fenv
5542 :
5543 : addr = &E1;
5544 : val = (E2);
5545 : __atomic_load (addr, &old, SEQ_CST);
5546 : feholdexcept (&fenv);
5547 : loop:
5548 : newval = old op val;
5549 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5550 : SEQ_CST))
5551 : goto done;
5552 : feclearexcept (FE_ALL_EXCEPT);
5553 : goto loop:
5554 : done:
5555 : feupdateenv (&fenv);
5556 :
5557 : The compiler will issue the __atomic_fetch_* built-in when possible,
5558 : otherwise it will generate the generic form of the atomic operations.
5559 : This requires temp(s) and has their address taken. The atomic processing
5560 : is smart enough to figure out when the size of an object can utilize
5561 : a lock-free version, and convert the built-in call to the appropriate
5562 : lock-free routine. The optimizers will then dispose of any temps that
5563 : are no longer required, and lock-free implementations are utilized as
5564 : long as there is target support for the required size.
5565 :
5566 : If the operator is NOP_EXPR, then this is a simple assignment, and
5567 : an __atomic_store is issued to perform the assignment rather than
5568 : the above loop. */
5569 :
5570 : /* Build an atomic assignment at LOC, expanding into the proper
5571 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5572 : the result of the operation, unless RETURN_OLD_P, in which case
5573 : return the old value of LHS (this is only for postincrement and
5574 : postdecrement). */
5575 :
5576 : static tree
5577 30457 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5578 : tree rhs, bool return_old_p)
5579 : {
5580 30457 : tree fndecl, func_call;
5581 30457 : vec<tree, va_gc> *params;
5582 30457 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5583 30457 : tree old, old_addr;
5584 30457 : tree compound_stmt = NULL_TREE;
5585 30457 : tree stmt, goto_stmt;
5586 30457 : tree loop_label, loop_decl, done_label, done_decl;
5587 :
5588 30457 : tree lhs_type = TREE_TYPE (lhs);
5589 30457 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5590 30457 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5591 30457 : tree rhs_semantic_type = TREE_TYPE (rhs);
5592 30457 : tree nonatomic_rhs_semantic_type;
5593 30457 : tree rhs_type;
5594 :
5595 30457 : gcc_assert (TYPE_ATOMIC (lhs_type));
5596 :
5597 30457 : if (return_old_p)
5598 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5599 :
5600 : /* Allocate enough vector items for a compare_exchange. */
5601 30457 : vec_alloc (params, 6);
5602 :
5603 : /* Create a compound statement to hold the sequence of statements
5604 : with a loop. */
5605 30457 : if (modifycode != NOP_EXPR)
5606 : {
5607 19954 : compound_stmt = c_begin_compound_stmt (false);
5608 :
5609 : /* For consistency with build_modify_expr on non-_Atomic,
5610 : mark the lhs as read. Also, it would be very hard to match
5611 : such expressions in mark_exp_read. */
5612 19954 : mark_exp_read (lhs);
5613 : }
5614 :
5615 : /* Remove any excess precision (which is only present here in the
5616 : case of compound assignments). */
5617 30457 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5618 : {
5619 0 : gcc_assert (modifycode != NOP_EXPR);
5620 0 : rhs = TREE_OPERAND (rhs, 0);
5621 : }
5622 30457 : rhs_type = TREE_TYPE (rhs);
5623 :
5624 : /* Fold the RHS if it hasn't already been folded. */
5625 30457 : if (modifycode != NOP_EXPR)
5626 19954 : rhs = c_fully_fold (rhs, false, NULL);
5627 :
5628 : /* Remove the qualifiers for the rest of the expressions and create
5629 : the VAL temp variable to hold the RHS. */
5630 30457 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5631 30457 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5632 30457 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5633 : TYPE_UNQUALIFIED);
5634 30457 : val = create_tmp_var_raw (nonatomic_rhs_type);
5635 30457 : TREE_ADDRESSABLE (val) = 1;
5636 30457 : suppress_warning (val);
5637 30457 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5638 : NULL_TREE);
5639 30457 : TREE_SIDE_EFFECTS (rhs) = 1;
5640 30457 : SET_EXPR_LOCATION (rhs, loc);
5641 30457 : if (modifycode != NOP_EXPR)
5642 19954 : add_stmt (rhs);
5643 :
5644 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5645 : an atomic_store. */
5646 19954 : if (modifycode == NOP_EXPR)
5647 : {
5648 10503 : compound_stmt = rhs;
5649 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5650 10503 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5651 10503 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5652 10503 : params->quick_push (lhs_addr);
5653 10503 : params->quick_push (rhs);
5654 10503 : params->quick_push (seq_cst);
5655 10503 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5656 :
5657 10503 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5658 : compound_stmt, func_call);
5659 :
5660 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5661 : the statement and that value. */
5662 10503 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5663 : }
5664 :
5665 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5666 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5667 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5668 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5669 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5670 : {
5671 11713 : built_in_function fncode;
5672 11713 : switch (modifycode)
5673 : {
5674 3075 : case PLUS_EXPR:
5675 3075 : case POINTER_PLUS_EXPR:
5676 2124 : fncode = (return_old_p
5677 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5678 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5679 : break;
5680 2889 : case MINUS_EXPR:
5681 2094 : fncode = (return_old_p
5682 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5683 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5684 : break;
5685 609 : case BIT_AND_EXPR:
5686 609 : fncode = (return_old_p
5687 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5688 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5689 : break;
5690 609 : case BIT_IOR_EXPR:
5691 609 : fncode = (return_old_p
5692 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5693 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5694 : break;
5695 609 : case BIT_XOR_EXPR:
5696 609 : fncode = (return_old_p
5697 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5698 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5699 : break;
5700 3922 : default:
5701 3922 : goto cas_loop;
5702 : }
5703 :
5704 : /* We can only use "_1" through "_16" variants of the atomic fetch
5705 : built-ins. */
5706 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5707 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5708 0 : goto cas_loop;
5709 :
5710 : /* If this is a pointer type, we need to multiply by the size of
5711 : the pointer target type. */
5712 7791 : if (POINTER_TYPE_P (lhs_type))
5713 : {
5714 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5715 : /* ??? This would introduce -Wdiscarded-qualifiers
5716 : warning: __atomic_fetch_* expect volatile void *
5717 : type as the first argument. (Assignments between
5718 : atomic and non-atomic objects are OK.) */
5719 1018 : || TYPE_RESTRICT (lhs_type))
5720 17 : goto cas_loop;
5721 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5722 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5723 : convert (ptrdiff_type_node, rhs),
5724 : convert (ptrdiff_type_node, sz));
5725 : }
5726 :
5727 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5728 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5729 7774 : fndecl = builtin_decl_explicit (fncode);
5730 7774 : params->quick_push (lhs_addr);
5731 7774 : params->quick_push (rhs);
5732 7774 : params->quick_push (seq_cst);
5733 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5734 :
5735 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5736 7774 : TREE_ADDRESSABLE (newval) = 1;
5737 7774 : suppress_warning (newval);
5738 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5739 : NULL_TREE, NULL_TREE);
5740 7774 : SET_EXPR_LOCATION (rhs, loc);
5741 7774 : add_stmt (rhs);
5742 :
5743 : /* Finish the compound statement. */
5744 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5745 :
5746 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5747 : the statement and that value. */
5748 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5749 : }
5750 :
5751 8241 : cas_loop:
5752 : /* Create the variables and labels required for the op= form. */
5753 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5754 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5755 12180 : TREE_ADDRESSABLE (old) = 1;
5756 12180 : suppress_warning (old);
5757 :
5758 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5759 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5760 12180 : TREE_ADDRESSABLE (newval) = 1;
5761 12180 : suppress_warning (newval);
5762 :
5763 12180 : loop_decl = create_artificial_label (loc);
5764 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5765 :
5766 12180 : done_decl = create_artificial_label (loc);
5767 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5768 :
5769 : /* __atomic_load (addr, &old, SEQ_CST). */
5770 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5771 12180 : params->quick_push (lhs_addr);
5772 12180 : params->quick_push (old_addr);
5773 12180 : params->quick_push (seq_cst);
5774 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5775 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5776 : NULL_TREE);
5777 12180 : add_stmt (old);
5778 12180 : params->truncate (0);
5779 :
5780 : /* Create the expressions for floating-point environment
5781 : manipulation, if required. */
5782 12180 : bool need_fenv = (flag_trapping_math
5783 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5784 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5785 12180 : if (need_fenv)
5786 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5787 :
5788 12180 : if (hold_call)
5789 7054 : add_stmt (hold_call);
5790 :
5791 : /* loop: */
5792 12180 : add_stmt (loop_label);
5793 :
5794 : /* newval = old + val; */
5795 12180 : if (rhs_type != rhs_semantic_type)
5796 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5797 12180 : rhs = build_binary_op (loc, modifycode,
5798 : convert_lvalue_to_rvalue (loc, old, true, true),
5799 : val, true);
5800 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5801 : {
5802 0 : tree eptype = TREE_TYPE (rhs);
5803 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5804 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5805 : }
5806 : else
5807 12180 : rhs = c_fully_fold (rhs, false, NULL);
5808 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5809 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5810 : NULL_TREE, 0);
5811 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5812 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5813 : subsequent ICEs in nested function processing after an error, ensure such
5814 : a TARGET_EXPR is built even after an error. */
5815 12180 : if (rhs == error_mark_node)
5816 10 : rhs = old;
5817 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5818 : NULL_TREE);
5819 12180 : SET_EXPR_LOCATION (rhs, loc);
5820 12180 : add_stmt (rhs);
5821 :
5822 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5823 : goto done; */
5824 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5825 12180 : params->quick_push (lhs_addr);
5826 12180 : params->quick_push (old_addr);
5827 12180 : params->quick_push (newval_addr);
5828 12180 : params->quick_push (integer_zero_node);
5829 12180 : params->quick_push (seq_cst);
5830 12180 : params->quick_push (seq_cst);
5831 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5832 :
5833 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5834 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5835 :
5836 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5837 12180 : SET_EXPR_LOCATION (stmt, loc);
5838 12180 : add_stmt (stmt);
5839 :
5840 12180 : if (clear_call)
5841 7054 : add_stmt (clear_call);
5842 :
5843 : /* goto loop; */
5844 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5845 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5846 12180 : add_stmt (goto_stmt);
5847 :
5848 : /* done: */
5849 12180 : add_stmt (done_label);
5850 :
5851 12180 : if (update_call)
5852 7054 : add_stmt (update_call);
5853 :
5854 : /* Finish the compound statement. */
5855 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5856 :
5857 : /* NEWVAL is the value that was successfully stored, return a
5858 : COMPOUND_EXPR of the statement and the appropriate value. */
5859 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5860 12180 : return_old_p ? old : newval);
5861 : }
5862 :
5863 : /* Construct and perhaps optimize a tree representation
5864 : for a unary operation. CODE, a tree_code, specifies the operation
5865 : and XARG is the operand.
5866 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5867 : promotions (such as from short to int).
5868 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5869 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5870 : to pointers in C99.
5871 :
5872 : LOCATION is the location of the operator. */
5873 :
5874 : tree
5875 62941646 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5876 : bool noconvert)
5877 : {
5878 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5879 62941646 : tree arg = xarg;
5880 62941646 : tree argtype = NULL_TREE;
5881 62941646 : enum tree_code typecode;
5882 62941646 : tree val;
5883 62941646 : tree ret = error_mark_node;
5884 62941646 : tree eptype = NULL_TREE;
5885 62941646 : const char *invalid_op_diag;
5886 :
5887 : /* If ARG is wrapped in a call to .ACCESS_WITH_SIZE — created when the
5888 : C parser rvalue-converts a counted_by-annotated member access (see
5889 : PR123569) — unwrap it here. Unary operators that consume an rvalue
5890 : (!, -, +) read the value itself rather than dereferencing into the
5891 : pointed-to data, so the bounds-checking wrapper is unnecessary and
5892 : would otherwise reach build_unary_op while it is not equipped to
5893 : handle the wrapped form. PR123569 fixed the ++/-- side of this by
5894 : suppressing wrapper creation in the parser; the rvalue-consuming
5895 : ops still receive the wrapper and need to strip it here. */
5896 62941646 : if (is_access_with_size_p (arg))
5897 1 : xarg = arg = get_ref_from_access_with_size (arg);
5898 :
5899 62941646 : gcc_checking_assert (!is_access_with_size_p (arg));
5900 :
5901 62941646 : bool int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5902 : if (int_operands)
5903 6787017 : arg = remove_c_maybe_const_expr (arg);
5904 :
5905 62941646 : if (code != ADDR_EXPR)
5906 8892272 : arg = require_complete_type (location, arg);
5907 :
5908 62941646 : typecode = TREE_CODE (TREE_TYPE (arg));
5909 62941646 : if (typecode == ERROR_MARK)
5910 72 : return error_mark_node;
5911 62941574 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5912 32484 : typecode = INTEGER_TYPE;
5913 :
5914 125883148 : if ((invalid_op_diag
5915 62941574 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5916 : {
5917 0 : error_at (location, invalid_op_diag);
5918 0 : return error_mark_node;
5919 : }
5920 :
5921 62941574 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5922 : {
5923 251 : eptype = TREE_TYPE (arg);
5924 251 : arg = TREE_OPERAND (arg, 0);
5925 : }
5926 :
5927 62941574 : switch (code)
5928 : {
5929 28807 : case CONVERT_EXPR:
5930 : /* This is used for unary plus, because a CONVERT_EXPR
5931 : is enough to prevent anybody from looking inside for
5932 : associativity, but won't generate any code. */
5933 28812 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5934 53 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5935 50 : || typecode == BITINT_TYPE
5936 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5937 : {
5938 1 : error_at (location, "wrong type argument to unary plus");
5939 1 : return error_mark_node;
5940 : }
5941 28806 : else if (!noconvert)
5942 28806 : arg = default_conversion (arg);
5943 28806 : arg = non_lvalue_loc (location, arg);
5944 28806 : break;
5945 :
5946 7026157 : case NEGATE_EXPR:
5947 7478529 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5948 489135 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5949 457860 : || typecode == BITINT_TYPE
5950 452372 : || gnu_vector_type_p (TREE_TYPE (arg))))
5951 : {
5952 1 : error_at (location, "wrong type argument to unary minus");
5953 1 : return error_mark_node;
5954 : }
5955 7026156 : else if (!noconvert)
5956 7026097 : arg = default_conversion (arg);
5957 : break;
5958 :
5959 300004 : case BIT_NOT_EXPR:
5960 : /* ~ works on integer types and non float vectors. */
5961 300004 : if (typecode == INTEGER_TYPE
5962 300004 : || typecode == BITINT_TYPE
5963 300004 : || (gnu_vector_type_p (TREE_TYPE (arg))
5964 2140 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5965 : {
5966 : tree e = arg;
5967 :
5968 : /* Warn if the expression has boolean value. */
5969 299398 : while (TREE_CODE (e) == COMPOUND_EXPR)
5970 11 : e = TREE_OPERAND (e, 1);
5971 :
5972 598758 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5973 598758 : || truth_value_p (TREE_CODE (e))))
5974 : {
5975 149 : auto_diagnostic_group d;
5976 149 : if (warning_at (location, OPT_Wbool_operation,
5977 : "%<~%> on a boolean expression"))
5978 : {
5979 12 : gcc_rich_location richloc (location);
5980 12 : richloc.add_fixit_insert_before (location, "!");
5981 12 : inform (&richloc, "did you mean to use logical not?");
5982 12 : }
5983 149 : }
5984 299387 : if (!noconvert)
5985 299382 : arg = default_conversion (arg);
5986 : }
5987 617 : else if (typecode == COMPLEX_TYPE)
5988 : {
5989 612 : code = CONJ_EXPR;
5990 612 : pedwarn (location, OPT_Wpedantic,
5991 : "ISO C does not support %<~%> for complex conjugation");
5992 612 : if (!noconvert)
5993 612 : arg = default_conversion (arg);
5994 : }
5995 : else
5996 : {
5997 5 : error_at (location, "wrong type argument to bit-complement");
5998 5 : return error_mark_node;
5999 : }
6000 : break;
6001 :
6002 7 : case ABS_EXPR:
6003 7 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
6004 : {
6005 0 : error_at (location, "wrong type argument to abs");
6006 0 : return error_mark_node;
6007 : }
6008 7 : else if (!noconvert)
6009 0 : arg = default_conversion (arg);
6010 : break;
6011 :
6012 7 : case ABSU_EXPR:
6013 7 : if (!(typecode == INTEGER_TYPE))
6014 : {
6015 0 : error_at (location, "wrong type argument to absu");
6016 0 : return error_mark_node;
6017 : }
6018 7 : else if (!noconvert)
6019 0 : arg = default_conversion (arg);
6020 : break;
6021 :
6022 0 : case CONJ_EXPR:
6023 : /* Conjugating a real value is a no-op, but allow it anyway. */
6024 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
6025 : || typecode == COMPLEX_TYPE))
6026 : {
6027 0 : error_at (location, "wrong type argument to conjugation");
6028 0 : return error_mark_node;
6029 : }
6030 0 : else if (!noconvert)
6031 0 : arg = default_conversion (arg);
6032 : break;
6033 :
6034 379725 : case TRUTH_NOT_EXPR:
6035 379725 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
6036 7363 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
6037 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
6038 18 : && typecode != BITINT_TYPE)
6039 : {
6040 10 : error_at (location,
6041 : "wrong type argument to unary exclamation mark");
6042 10 : return error_mark_node;
6043 : }
6044 379715 : if (int_operands)
6045 : {
6046 139892 : arg = c_objc_common_truthvalue_conversion (location, xarg);
6047 139892 : arg = remove_c_maybe_const_expr (arg);
6048 : }
6049 : else
6050 239823 : arg = c_objc_common_truthvalue_conversion (location, arg);
6051 379715 : ret = invert_truthvalue_loc (location, arg);
6052 379715 : eptype = NULL_TREE;
6053 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
6054 379715 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
6055 239416 : location = EXPR_LOCATION (ret);
6056 379715 : goto return_build_unary_op;
6057 :
6058 203190 : case REALPART_EXPR:
6059 203190 : case IMAGPART_EXPR:
6060 203190 : ret = build_real_imag_expr (location, code, arg);
6061 203190 : if (ret == error_mark_node)
6062 : return error_mark_node;
6063 203182 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
6064 2 : eptype = TREE_TYPE (eptype);
6065 203182 : goto return_build_unary_op;
6066 :
6067 954332 : case PREINCREMENT_EXPR:
6068 954332 : case POSTINCREMENT_EXPR:
6069 954332 : case PREDECREMENT_EXPR:
6070 954332 : case POSTDECREMENT_EXPR:
6071 :
6072 954332 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6073 : {
6074 2 : tree inner = build_unary_op (location, code,
6075 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
6076 : noconvert);
6077 2 : if (inner == error_mark_node)
6078 : return error_mark_node;
6079 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6080 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6081 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6082 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
6083 2 : goto return_build_unary_op;
6084 : }
6085 :
6086 : /* Complain about anything that is not a true lvalue. In
6087 : Objective-C, skip this check for property_refs. */
6088 954330 : if (!objc_is_property_ref (arg)
6089 1908660 : && !lvalue_or_else (location,
6090 954330 : arg, ((code == PREINCREMENT_EXPR
6091 954330 : || code == POSTINCREMENT_EXPR)
6092 : ? lv_increment
6093 : : lv_decrement)))
6094 22 : return error_mark_node;
6095 :
6096 954308 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
6097 : {
6098 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6099 2 : warning_at (location, OPT_Wc___compat,
6100 : "increment of enumeration value is invalid in C++");
6101 : else
6102 2 : warning_at (location, OPT_Wc___compat,
6103 : "decrement of enumeration value is invalid in C++");
6104 : }
6105 :
6106 954308 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6107 : {
6108 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6109 328 : warning_at (location, OPT_Wbool_operation,
6110 : "increment of a boolean expression");
6111 : else
6112 336 : warning_at (location, OPT_Wbool_operation,
6113 : "decrement of a boolean expression");
6114 : }
6115 :
6116 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
6117 954308 : arg = c_fully_fold (arg, false, NULL, true);
6118 :
6119 954308 : bool atomic_op;
6120 954308 : atomic_op = really_atomic_lvalue (arg);
6121 :
6122 : /* Increment or decrement the real part of the value,
6123 : and don't change the imaginary part. */
6124 954308 : if (typecode == COMPLEX_TYPE)
6125 : {
6126 57 : tree real, imag;
6127 :
6128 57 : pedwarn_c23 (location, OPT_Wpedantic,
6129 : "ISO C does not support %<++%> and %<--%> on complex "
6130 : "types before C2Y");
6131 :
6132 57 : if (!atomic_op)
6133 : {
6134 53 : arg = stabilize_reference (arg);
6135 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6136 : true);
6137 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6138 : true);
6139 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6140 53 : if (real == error_mark_node || imag == error_mark_node)
6141 : return error_mark_node;
6142 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6143 : real, imag);
6144 53 : goto return_build_unary_op;
6145 : }
6146 : }
6147 :
6148 : /* Report invalid types. */
6149 :
6150 954255 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6151 720781 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6152 209 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6153 954311 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6154 : {
6155 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6156 3 : error_at (location, "wrong type argument to increment");
6157 : else
6158 2 : error_at (location, "wrong type argument to decrement");
6159 :
6160 5 : return error_mark_node;
6161 : }
6162 :
6163 954250 : {
6164 954250 : tree inc;
6165 :
6166 954250 : argtype = TREE_TYPE (arg);
6167 :
6168 : /* Compute the increment. */
6169 :
6170 954250 : if (typecode == POINTER_TYPE)
6171 : {
6172 : /* If pointer target is an incomplete type,
6173 : we just cannot know how to do the arithmetic. */
6174 233474 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6175 : {
6176 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6177 17 : error_at (location,
6178 : "increment of pointer to an incomplete type %qT",
6179 17 : TREE_TYPE (argtype));
6180 : else
6181 10 : error_at (location,
6182 : "decrement of pointer to an incomplete type %qT",
6183 10 : TREE_TYPE (argtype));
6184 : }
6185 233447 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6186 233447 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6187 : {
6188 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6189 6 : pedwarn (location, OPT_Wpointer_arith,
6190 : "wrong type argument to increment");
6191 : else
6192 4 : pedwarn (location, OPT_Wpointer_arith,
6193 : "wrong type argument to decrement");
6194 : }
6195 : else
6196 466874 : verify_type_context (location, TCTX_POINTER_ARITH,
6197 233437 : TREE_TYPE (argtype));
6198 :
6199 233474 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6200 233474 : inc = convert_to_ptrofftype_loc (location, inc);
6201 : }
6202 720776 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6203 : {
6204 : /* For signed fract types, we invert ++ to -- or
6205 : -- to ++, and change inc from 1 to -1, because
6206 : it is not possible to represent 1 in signed fract constants.
6207 : For unsigned fract types, the result always overflows and
6208 : we get an undefined (original) or the maximum value. */
6209 0 : if (code == PREINCREMENT_EXPR)
6210 : code = PREDECREMENT_EXPR;
6211 : else if (code == PREDECREMENT_EXPR)
6212 : code = PREINCREMENT_EXPR;
6213 : else if (code == POSTINCREMENT_EXPR)
6214 : code = POSTDECREMENT_EXPR;
6215 : else /* code == POSTDECREMENT_EXPR */
6216 0 : code = POSTINCREMENT_EXPR;
6217 :
6218 0 : inc = integer_minus_one_node;
6219 0 : inc = convert (argtype, inc);
6220 : }
6221 : else
6222 : {
6223 720725 : inc = VECTOR_TYPE_P (argtype)
6224 720776 : ? build_one_cst (argtype)
6225 : : integer_one_node;
6226 720776 : inc = convert (argtype, inc);
6227 : }
6228 :
6229 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6230 : need to ask Objective-C to build the increment or decrement
6231 : expression for it. */
6232 954250 : if (objc_is_property_ref (arg))
6233 0 : return objc_build_incr_expr_for_property_ref (location, code,
6234 20 : arg, inc);
6235 :
6236 : /* Report a read-only lvalue. */
6237 954250 : if (TYPE_READONLY (argtype))
6238 : {
6239 20 : readonly_error (location, arg,
6240 20 : ((code == PREINCREMENT_EXPR
6241 20 : || code == POSTINCREMENT_EXPR)
6242 : ? lv_increment : lv_decrement));
6243 20 : return error_mark_node;
6244 : }
6245 954230 : else if (TREE_READONLY (arg))
6246 4 : readonly_warning (arg,
6247 4 : ((code == PREINCREMENT_EXPR
6248 4 : || code == POSTINCREMENT_EXPR)
6249 : ? lv_increment : lv_decrement));
6250 :
6251 : /* If the argument is atomic, use the special code sequences for
6252 : atomic compound assignment. */
6253 954230 : if (atomic_op)
6254 : {
6255 4469 : arg = stabilize_reference (arg);
6256 8938 : ret = build_atomic_assign (location, arg,
6257 4469 : ((code == PREINCREMENT_EXPR
6258 4469 : || code == POSTINCREMENT_EXPR)
6259 : ? PLUS_EXPR
6260 : : MINUS_EXPR),
6261 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6262 : ? inc
6263 : : integer_one_node),
6264 : (code == POSTINCREMENT_EXPR
6265 4469 : || code == POSTDECREMENT_EXPR));
6266 954230 : goto return_build_unary_op;
6267 : }
6268 :
6269 949761 : tree true_res;
6270 949761 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6271 : {
6272 364 : tree larg = stabilize_reference (arg);
6273 364 : tree sarg = save_expr (larg);
6274 364 : switch (code)
6275 : {
6276 91 : case PREINCREMENT_EXPR:
6277 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6278 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6279 91 : break;
6280 91 : case POSTINCREMENT_EXPR:
6281 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6282 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6283 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6284 91 : break;
6285 91 : case PREDECREMENT_EXPR:
6286 91 : {
6287 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6288 : true, true);
6289 91 : rarg = invert_truthvalue_loc (location, rarg);
6290 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6291 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6292 : }
6293 91 : break;
6294 91 : case POSTDECREMENT_EXPR:
6295 91 : {
6296 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6297 : true, true);
6298 91 : rarg = invert_truthvalue_loc (location, rarg);
6299 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6300 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6301 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6302 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6303 : }
6304 91 : break;
6305 : default:
6306 : gcc_unreachable ();
6307 : }
6308 364 : TREE_SIDE_EFFECTS (val) = 1;
6309 : }
6310 949397 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6311 64 : val = boolean_increment (code, arg);
6312 : else
6313 949333 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6314 949761 : TREE_SIDE_EFFECTS (val) = 1;
6315 949761 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6316 3974 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6317 : TYPE_UNQUALIFIED);
6318 949761 : ret = val;
6319 949761 : goto return_build_unary_op;
6320 : }
6321 :
6322 54049333 : case ADDR_EXPR:
6323 : /* Note that this operation never does default_conversion. */
6324 :
6325 : /* The operand of unary '&' must be an lvalue (which excludes
6326 : expressions of type void), or, in C99, the result of a [] or
6327 : unary '*' operator. */
6328 54049333 : if (VOID_TYPE_P (TREE_TYPE (arg))
6329 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6330 54049352 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6331 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6332 :
6333 : /* Let &* cancel out to simplify resulting code. */
6334 54049333 : if (INDIRECT_REF_P (arg))
6335 : {
6336 : /* Don't let this be an lvalue. */
6337 22602 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6338 17034 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6339 5568 : ret = TREE_OPERAND (arg, 0);
6340 5568 : goto return_build_unary_op;
6341 : }
6342 :
6343 : /* Anything not already handled and not a true memory reference
6344 : or a non-lvalue array is an error. */
6345 54026731 : if (typecode != FUNCTION_TYPE && !noconvert
6346 54026731 : && !lvalue_or_else (location, arg, lv_addressof))
6347 17 : return error_mark_node;
6348 :
6349 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6350 : folding later. */
6351 54026714 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6352 : {
6353 14 : tree inner = build_unary_op (location, code,
6354 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6355 : noconvert);
6356 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6357 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6358 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6359 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6360 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6361 14 : goto return_build_unary_op;
6362 : }
6363 :
6364 : /* Ordinary case; arg is a COMPONENT_REF or a decl. */
6365 :
6366 54026700 : argtype = TREE_TYPE (arg);
6367 :
6368 : /* If the lvalue is const or volatile, merge that into the type
6369 : to which the address will point. This is only needed
6370 : for function types. */
6371 54026700 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6372 53531992 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6373 85298018 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6374 : {
6375 31190267 : auto quals = TYPE_QUALS (strip_array_types (argtype));
6376 :
6377 31190267 : if (TREE_READONLY (arg))
6378 30803631 : quals |= TYPE_QUAL_CONST;
6379 31190267 : if (TREE_THIS_VOLATILE (arg))
6380 388423 : quals |= TYPE_QUAL_VOLATILE;
6381 :
6382 31190267 : argtype = c_build_qualified_type (argtype, quals);
6383 : }
6384 :
6385 54026700 : switch (TREE_CODE (arg))
6386 : {
6387 66877 : case COMPONENT_REF:
6388 66877 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6389 : {
6390 8 : error_at (location, "cannot take address of bit-field %qD",
6391 8 : TREE_OPERAND (arg, 1));
6392 8 : return error_mark_node;
6393 : }
6394 :
6395 : /* fall through */
6396 :
6397 142883 : case ARRAY_REF:
6398 142883 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6399 : {
6400 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6401 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6402 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6403 : {
6404 6 : error_at (location, "cannot take address of scalar with "
6405 : "reverse storage order");
6406 6 : return error_mark_node;
6407 : }
6408 :
6409 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6410 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6411 58 : warning_at (location, OPT_Wscalar_storage_order,
6412 : "address of array with reverse scalar storage "
6413 : "order requested");
6414 : }
6415 :
6416 54026686 : default:
6417 54026686 : break;
6418 : }
6419 :
6420 54026686 : if (!c_mark_addressable (arg))
6421 20 : return error_mark_node;
6422 :
6423 54026666 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6424 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6425 :
6426 54026666 : argtype = c_build_pointer_type (argtype);
6427 :
6428 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6429 : when we have proper support for integer constant expressions. */
6430 54026666 : val = get_base_address (arg);
6431 54026666 : if (val && INDIRECT_REF_P (val)
6432 54051886 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6433 : {
6434 541 : ret = fold_offsetof (arg, argtype);
6435 541 : goto return_build_unary_op;
6436 : }
6437 :
6438 54026125 : val = build1 (ADDR_EXPR, argtype, arg);
6439 :
6440 54026125 : ret = val;
6441 54026125 : goto return_build_unary_op;
6442 :
6443 12 : case PAREN_EXPR:
6444 12 : ret = build1 (code, TREE_TYPE (arg), arg);
6445 12 : goto return_build_unary_op;
6446 :
6447 0 : default:
6448 0 : gcc_unreachable ();
6449 : }
6450 :
6451 7354975 : if (argtype == NULL_TREE)
6452 7354975 : argtype = TREE_TYPE (arg);
6453 7354975 : if (TREE_CODE (arg) == INTEGER_CST)
6454 6642782 : ret = (require_constant_value
6455 6642782 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6456 6623143 : : fold_build1_loc (location, code, argtype, arg));
6457 : else
6458 712193 : ret = build1 (code, argtype, arg);
6459 62924417 : return_build_unary_op:
6460 62924417 : gcc_assert (ret != error_mark_node);
6461 6787494 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6462 69711902 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6463 563 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6464 62923854 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6465 74 : ret = note_integer_operands (ret);
6466 62924417 : if (eptype)
6467 244 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6468 62924417 : protected_set_expr_location (ret, location);
6469 62924417 : return ret;
6470 : }
6471 :
6472 : /* Return nonzero if REF is an lvalue valid for this language.
6473 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6474 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6475 :
6476 : bool
6477 184413115 : lvalue_p (const_tree ref)
6478 : {
6479 185932668 : const enum tree_code code = TREE_CODE (ref);
6480 :
6481 185932668 : switch (code)
6482 : {
6483 1494424 : case REALPART_EXPR:
6484 1494424 : case IMAGPART_EXPR:
6485 1494424 : case COMPONENT_REF:
6486 1494424 : return lvalue_p (TREE_OPERAND (ref, 0));
6487 :
6488 25129 : case C_MAYBE_CONST_EXPR:
6489 25129 : return lvalue_p (TREE_OPERAND (ref, 1));
6490 :
6491 : case COMPOUND_LITERAL_EXPR:
6492 : case STRING_CST:
6493 : return true;
6494 :
6495 25968867 : case MEM_REF:
6496 25968867 : case TARGET_MEM_REF:
6497 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6498 : here as well. */
6499 25968867 : case INDIRECT_REF:
6500 25968867 : case ARRAY_REF:
6501 25968867 : case VAR_DECL:
6502 25968867 : case PARM_DECL:
6503 25968867 : case RESULT_DECL:
6504 25968867 : case ERROR_MARK:
6505 25968867 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6506 25968867 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6507 :
6508 6 : case BIND_EXPR:
6509 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6510 :
6511 7268571 : case CALL_EXPR:
6512 7268571 : return is_access_with_size_p (ref);
6513 :
6514 : default:
6515 : return false;
6516 : }
6517 : }
6518 :
6519 : /* Give a warning for storing in something that is read-only in GCC
6520 : terms but not const in ISO C terms. */
6521 :
6522 : static void
6523 5 : readonly_warning (tree arg, enum lvalue_use use)
6524 : {
6525 5 : switch (use)
6526 : {
6527 1 : case lv_assign:
6528 1 : warning (0, "assignment of read-only location %qE", arg);
6529 1 : break;
6530 2 : case lv_increment:
6531 2 : warning (0, "increment of read-only location %qE", arg);
6532 2 : break;
6533 2 : case lv_decrement:
6534 2 : warning (0, "decrement of read-only location %qE", arg);
6535 2 : break;
6536 0 : default:
6537 0 : gcc_unreachable ();
6538 : }
6539 5 : return;
6540 : }
6541 :
6542 :
6543 : /* Return nonzero if REF is an lvalue valid for this language;
6544 : otherwise, print an error message and return zero. USE says
6545 : how the lvalue is being used and so selects the error message.
6546 : LOCATION is the location at which any error should be reported. */
6547 :
6548 : static int
6549 4861157 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6550 : {
6551 4861157 : int win = lvalue_p (ref);
6552 :
6553 4861157 : if (!win)
6554 75 : lvalue_error (loc, use);
6555 :
6556 4861157 : return win;
6557 : }
6558 :
6559 : /* Mark EXP saying that we need to be able to take the
6560 : address of it; it should not be allocated in a register.
6561 : Returns true if successful. ARRAY_REF_P is true if this
6562 : is for ARRAY_REF construction - in that case we don't want
6563 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6564 : it is fine to use ARRAY_REFs for vector subscripts on vector
6565 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6566 : than producing an error for taking the address of a register. */
6567 :
6568 : bool
6569 54586506 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6570 : {
6571 54586506 : tree x = exp;
6572 :
6573 55171568 : while (1)
6574 55171568 : switch (TREE_CODE (x))
6575 : {
6576 11583 : case VIEW_CONVERT_EXPR:
6577 11583 : if (array_ref_p
6578 11473 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6579 23056 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6580 : return true;
6581 110 : x = TREE_OPERAND (x, 0);
6582 110 : break;
6583 :
6584 398744 : case COMPONENT_REF:
6585 398744 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6586 : {
6587 6 : error ("cannot take address of bit-field %qD",
6588 3 : TREE_OPERAND (x, 1));
6589 3 : return false;
6590 : }
6591 : /* FALLTHRU */
6592 584952 : case ADDR_EXPR:
6593 584952 : case ARRAY_REF:
6594 584952 : case REALPART_EXPR:
6595 584952 : case IMAGPART_EXPR:
6596 584952 : x = TREE_OPERAND (x, 0);
6597 584952 : break;
6598 :
6599 582 : case COMPOUND_LITERAL_EXPR:
6600 582 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6601 : {
6602 16 : if (override_register)
6603 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6604 : else
6605 : {
6606 4 : error ("address of register compound literal requested");
6607 4 : return false;
6608 : }
6609 : }
6610 578 : TREE_ADDRESSABLE (x) = 1;
6611 578 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6612 578 : return true;
6613 :
6614 0 : case CONSTRUCTOR:
6615 0 : TREE_ADDRESSABLE (x) = 1;
6616 0 : return true;
6617 :
6618 1348476 : case VAR_DECL:
6619 1348476 : case CONST_DECL:
6620 1348476 : case PARM_DECL:
6621 1348476 : case RESULT_DECL:
6622 1348476 : if (C_DECL_REGISTER (x)
6623 1348476 : && DECL_NONLOCAL (x))
6624 : {
6625 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6626 : {
6627 0 : error
6628 0 : ("global register variable %qD used in nested function", x);
6629 0 : return false;
6630 : }
6631 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6632 : }
6633 1348476 : else if (C_DECL_REGISTER (x))
6634 : {
6635 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6636 4 : error ("address of global register variable %qD requested", x);
6637 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6638 : {
6639 54 : DECL_REGISTER (x) = 0;
6640 54 : TREE_ADDRESSABLE (x) = 1;
6641 54 : mark_decl_used (x, true);
6642 54 : return true;
6643 : }
6644 : else
6645 15 : error ("address of register variable %qD requested", x);
6646 : return false;
6647 : }
6648 :
6649 : /* FALLTHRU */
6650 54003211 : case FUNCTION_DECL:
6651 54003211 : TREE_ADDRESSABLE (x) = 1;
6652 54003211 : mark_decl_used (x, true);
6653 : /* FALLTHRU */
6654 : default:
6655 : return true;
6656 : }
6657 : }
6658 :
6659 : /* Convert EXPR to TYPE, warning about conversion problems with
6660 : constants. SEMANTIC_TYPE is the type this conversion would use
6661 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6662 : is equivalent to convert_and_check. This function is a wrapper that
6663 : handles conversions that may be different than
6664 : the usual ones because of excess precision. */
6665 :
6666 : static tree
6667 25177418 : ep_convert_and_check (location_t loc, tree type, tree expr,
6668 : tree semantic_type)
6669 : {
6670 25177418 : if (TREE_TYPE (expr) == type)
6671 : return expr;
6672 :
6673 : /* For C11, integer conversions may have results with excess
6674 : precision. */
6675 2144478 : if (flag_isoc11 || !semantic_type)
6676 2144474 : return convert_and_check (loc, type, expr);
6677 :
6678 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6679 4 : && TREE_TYPE (expr) != semantic_type)
6680 : {
6681 : /* For integers, we need to check the real conversion, not
6682 : the conversion to the excess precision type. */
6683 4 : expr = convert_and_check (loc, semantic_type, expr);
6684 : }
6685 : /* Result type is the excess precision type, which should be
6686 : large enough, so do not check. */
6687 4 : return convert (type, expr);
6688 : }
6689 :
6690 : /* If EXPR refers to a built-in declared without a prototype returns
6691 : the actual type of the built-in and, if non-null, set *BLTIN to
6692 : a pointer to the built-in. Otherwise return the type of EXPR
6693 : and clear *BLTIN if non-null. */
6694 :
6695 : static tree
6696 2857858 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6697 : {
6698 2857858 : tree dummy;
6699 2857858 : if (!bltin)
6700 0 : bltin = &dummy;
6701 :
6702 2857858 : *bltin = NULL_TREE;
6703 :
6704 2857858 : tree type = TREE_TYPE (expr);
6705 2857858 : if (TREE_CODE (expr) != ADDR_EXPR)
6706 : return type;
6707 :
6708 211679 : tree oper = TREE_OPERAND (expr, 0);
6709 211679 : if (!DECL_P (oper)
6710 171969 : || TREE_CODE (oper) != FUNCTION_DECL
6711 248120 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6712 : return type;
6713 :
6714 2086 : built_in_function code = DECL_FUNCTION_CODE (oper);
6715 2086 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6716 : return type;
6717 :
6718 1977 : if ((*bltin = builtin_decl_implicit (code)))
6719 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6720 :
6721 : return type;
6722 : }
6723 :
6724 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6725 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6726 : if folded to an integer constant then the unselected half may
6727 : contain arbitrary operations not normally permitted in constant
6728 : expressions. Set the location of the expression to LOC. */
6729 :
6730 : tree
6731 410040 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6732 : tree op1, tree op1_original_type, location_t op1_loc,
6733 : tree op2, tree op2_original_type, location_t op2_loc)
6734 : {
6735 410040 : tree type1;
6736 410040 : tree type2;
6737 410040 : tree result_type = NULL;
6738 410040 : tree semantic_result_type = NULL;
6739 410040 : tree orig_op1 = op1, orig_op2 = op2;
6740 410040 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6741 410040 : bool ifexp_int_operands;
6742 410040 : tree ret;
6743 :
6744 410040 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6745 : if (op1_int_operands)
6746 114227 : op1 = remove_c_maybe_const_expr (op1);
6747 410040 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6748 : if (op2_int_operands)
6749 149396 : op2 = remove_c_maybe_const_expr (op2);
6750 410040 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6751 : if (ifexp_int_operands)
6752 244981 : ifexp = remove_c_maybe_const_expr (ifexp);
6753 :
6754 : /* Promote both alternatives. */
6755 :
6756 410040 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6757 406115 : op1 = default_conversion (op1);
6758 410040 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6759 380192 : op2 = default_conversion (op2);
6760 :
6761 410040 : if (TREE_CODE (ifexp) == ERROR_MARK
6762 409971 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6763 819997 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6764 83 : return error_mark_node;
6765 :
6766 409957 : tree bltin1 = NULL_TREE;
6767 409957 : tree bltin2 = NULL_TREE;
6768 409957 : type1 = type_or_builtin_type (op1, &bltin1);
6769 409957 : const enum tree_code code1 = TREE_CODE (type1);
6770 409957 : type2 = type_or_builtin_type (op2, &bltin2);
6771 409957 : const enum tree_code code2 = TREE_CODE (type2);
6772 :
6773 409957 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6774 1 : return error_mark_node;
6775 :
6776 409956 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6777 1 : return error_mark_node;
6778 :
6779 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6780 : In C99 they will be pointers by now. */
6781 409955 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6782 : {
6783 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6784 1 : return error_mark_node;
6785 : }
6786 :
6787 409954 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6788 409947 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6789 8 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6790 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6791 8 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6792 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6793 : {
6794 8 : semantic_result_type = c_common_type (type1, type2);
6795 8 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6796 : {
6797 7 : op1 = TREE_OPERAND (op1, 0);
6798 7 : type1 = TREE_TYPE (op1);
6799 7 : gcc_assert (TREE_CODE (type1) == code1);
6800 : }
6801 8 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6802 : {
6803 1 : op2 = TREE_OPERAND (op2, 0);
6804 1 : type2 = TREE_TYPE (op2);
6805 1 : gcc_assert (TREE_CODE (type2) == code2);
6806 : }
6807 : }
6808 :
6809 409954 : if (warn_cxx_compat)
6810 : {
6811 2944 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6812 2944 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6813 :
6814 2944 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6815 614 : && TREE_CODE (t2) == ENUMERAL_TYPE
6816 3558 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6817 2 : warning_at (colon_loc, OPT_Wc___compat,
6818 : "different enum types in conditional is "
6819 : "invalid in C++: %qT vs %qT", t1, t2);
6820 : }
6821 :
6822 409954 : if (warn_zero_as_null_pointer_constant
6823 20 : && c_inhibit_evaluation_warnings == 0)
6824 : {
6825 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6826 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6827 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6828 : "zero as null pointer constant");
6829 :
6830 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6831 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6832 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6833 : "zero as null pointer constant");
6834 : }
6835 :
6836 : /* Quickly detect the usual case where op1 and op2 have the same type
6837 : after promotion. */
6838 409954 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6839 : {
6840 274583 : if (type1 == type2)
6841 : result_type = type1;
6842 : else
6843 6443 : result_type = TYPE_MAIN_VARIANT (type1);
6844 : }
6845 135371 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6846 80493 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6847 54951 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6848 26163 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6849 : {
6850 : /* In C11, a conditional expression between a floating-point
6851 : type and an integer type should convert the integer type to
6852 : the evaluation format of the floating-point type, with
6853 : possible excess precision. */
6854 28899 : tree eptype1 = type1;
6855 28899 : tree eptype2 = type2;
6856 28899 : if (flag_isoc11)
6857 : {
6858 28586 : tree eptype;
6859 11693 : if (ANY_INTEGRAL_TYPE_P (type1)
6860 28586 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6861 : {
6862 2 : eptype2 = eptype;
6863 2 : if (!semantic_result_type)
6864 2 : semantic_result_type = c_common_type (type1, type2);
6865 : }
6866 506 : else if (ANY_INTEGRAL_TYPE_P (type2)
6867 28584 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6868 : {
6869 4646 : eptype1 = eptype;
6870 4646 : if (!semantic_result_type)
6871 4646 : semantic_result_type = c_common_type (type1, type2);
6872 : }
6873 : }
6874 28899 : result_type = c_common_type (eptype1, eptype2);
6875 28899 : if (result_type == error_mark_node)
6876 : return error_mark_node;
6877 28897 : do_warn_double_promotion (result_type, type1, type2,
6878 : "implicit conversion from %qT to %qT to "
6879 : "match other result of conditional",
6880 : colon_loc);
6881 :
6882 : /* If -Wsign-compare, warn here if type1 and type2 have
6883 : different signedness. We'll promote the signed to unsigned
6884 : and later code won't know it used to be different.
6885 : Do this check on the original types, so that explicit casts
6886 : will be considered, but default promotions won't. */
6887 28897 : if (c_inhibit_evaluation_warnings == 0)
6888 : {
6889 28706 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6890 28706 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6891 :
6892 28706 : if (unsigned_op1 ^ unsigned_op2)
6893 : {
6894 : /* Do not warn if the result type is signed, since the
6895 : signed type will only be chosen if it can represent
6896 : all the values of the unsigned type. */
6897 13123 : if (!TYPE_UNSIGNED (result_type))
6898 : /* OK */;
6899 : else
6900 : {
6901 13018 : bool op1_maybe_const = true;
6902 13018 : bool op2_maybe_const = true;
6903 :
6904 : /* Do not warn if the signed quantity is an
6905 : unsuffixed integer literal (or some static
6906 : constant expression involving such literals) and
6907 : it is non-negative. This warning requires the
6908 : operands to be folded for best results, so do
6909 : that folding in this case even without
6910 : warn_sign_compare to avoid warning options
6911 : possibly affecting code generation. */
6912 13018 : c_inhibit_evaluation_warnings
6913 13018 : += (ifexp == truthvalue_false_node);
6914 13018 : op1 = c_fully_fold (op1, require_constant_value,
6915 : &op1_maybe_const);
6916 13018 : c_inhibit_evaluation_warnings
6917 13018 : -= (ifexp == truthvalue_false_node);
6918 :
6919 13018 : c_inhibit_evaluation_warnings
6920 13018 : += (ifexp == truthvalue_true_node);
6921 13018 : op2 = c_fully_fold (op2, require_constant_value,
6922 : &op2_maybe_const);
6923 13018 : c_inhibit_evaluation_warnings
6924 13018 : -= (ifexp == truthvalue_true_node);
6925 :
6926 13018 : if (warn_sign_compare)
6927 : {
6928 1397 : if ((unsigned_op2
6929 693 : && tree_expr_nonnegative_p (op1))
6930 1400 : || (unsigned_op1
6931 704 : && tree_expr_nonnegative_p (op2)))
6932 : /* OK */;
6933 10 : else if (unsigned_op2)
6934 3 : warning_at (op1_loc, OPT_Wsign_compare,
6935 : "operand of %<?:%> changes signedness from "
6936 : "%qT to %qT due to unsignedness of other "
6937 3 : "operand", TREE_TYPE (orig_op1),
6938 3 : TREE_TYPE (orig_op2));
6939 : else
6940 7 : warning_at (op2_loc, OPT_Wsign_compare,
6941 : "operand of %<?:%> changes signedness from "
6942 : "%qT to %qT due to unsignedness of other "
6943 7 : "operand", TREE_TYPE (orig_op2),
6944 7 : TREE_TYPE (orig_op1));
6945 : }
6946 13018 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6947 7655 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6948 13018 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6949 4399 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6950 : }
6951 : }
6952 : }
6953 : }
6954 106472 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6955 : {
6956 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6957 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6958 : "ISO C forbids conditional expr with only one void side");
6959 25995 : result_type = void_type_node;
6960 : }
6961 80477 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6962 : {
6963 80059 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6964 80059 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6965 80059 : addr_space_t as_common;
6966 :
6967 80059 : if (comp_target_types (colon_loc, type1, type2))
6968 : {
6969 2623 : ifexp = save_expr (ifexp);
6970 2623 : result_type = common_pointer_type (type1, type2, ifexp);
6971 : }
6972 77436 : else if (null_pointer_constant_p (orig_op1))
6973 : result_type = type2;
6974 73274 : else if (null_pointer_constant_p (orig_op2))
6975 : result_type = type1;
6976 35282 : else if (!addr_space_superset (as1, as2, &as_common))
6977 : {
6978 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6979 : "used in conditional expression");
6980 0 : return error_mark_node;
6981 : }
6982 35282 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6983 326 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6984 35284 : || (VOID_TYPE_P (TREE_TYPE (type2))
6985 34911 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6986 : {
6987 35234 : tree t1 = TREE_TYPE (type1);
6988 35234 : tree t2 = TREE_TYPE (type2);
6989 35234 : if (!(VOID_TYPE_P (t1)
6990 325 : && !TYPE_ATOMIC (t1)))
6991 : {
6992 : /* roles are swapped */
6993 34910 : t1 = t2;
6994 34910 : t2 = TREE_TYPE (type1);
6995 : }
6996 35234 : tree t2_stripped = strip_array_types (t2);
6997 35234 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6998 35234 : && !TYPE_QUALS (t1).superset_of (TYPE_QUALS (t2_stripped)))
6999 : {
7000 18 : if (!flag_isoc23)
7001 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
7002 : "pointer to array loses qualifier "
7003 : "in conditional expression");
7004 12 : else if (warn_c11_c23_compat > 0)
7005 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
7006 : "pointer to array loses qualifier "
7007 : "in conditional expression in ISO C before C23");
7008 : }
7009 35234 : if (TREE_CODE (t2) == FUNCTION_TYPE)
7010 4 : pedwarn (colon_loc, OPT_Wpedantic,
7011 : "ISO C forbids conditional expr between "
7012 : "%<void *%> and function pointer");
7013 : /* for array, use qualifiers of element type */
7014 35234 : if (flag_isoc23)
7015 11453 : t2 = t2_stripped;
7016 35234 : result_type = c_build_pointer_type (qualify_type (t1, t2));
7017 : }
7018 : /* Objective-C pointer comparisons are a bit more lenient. */
7019 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
7020 0 : result_type = objc_common_type (type1, type2);
7021 : else
7022 : {
7023 48 : qualifier_set qual {TYPE_UNQUALIFIED, as_common};
7024 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
7025 48 : if (!flag_isoc99)
7026 : /* This downgrade to a warning ensures that -std=gnu89
7027 : -pedantic-errors does not flag these mismatches between
7028 : builtins as errors (as diagnostics::kind::permerror would)
7029 : ISO C99 and later do not have implicit function declarations,
7030 : so the mismatch cannot occur naturally there. */
7031 5 : kind = (bltin1 && bltin2
7032 5 : ? diagnostics::kind::warning
7033 : : diagnostics::kind::pedwarn);
7034 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
7035 : "pointer type mismatch "
7036 : "in conditional expression"))
7037 : {
7038 46 : inform (op1_loc, "first expression has type %qT", type1);
7039 46 : inform (op2_loc, "second expression has type %qT", type2);
7040 : }
7041 48 : result_type = c_build_pointer_type
7042 48 : (c_build_qualified_type (void_type_node, qual));
7043 : }
7044 : }
7045 418 : else if (code1 == POINTER_TYPE
7046 298 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
7047 : {
7048 294 : if (!null_pointer_constant_p (orig_op2))
7049 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
7050 : "pointer/integer type mismatch "
7051 : "in conditional expression");
7052 : else
7053 : {
7054 280 : op2 = null_pointer_node;
7055 : }
7056 : result_type = type1;
7057 : }
7058 124 : else if (code2 == POINTER_TYPE
7059 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
7060 : {
7061 86 : if (!null_pointer_constant_p (orig_op1))
7062 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
7063 : "pointer/integer type mismatch "
7064 : "in conditional expression");
7065 : else
7066 : {
7067 72 : op1 = null_pointer_node;
7068 : }
7069 : result_type = type2;
7070 : }
7071 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
7072 : type nullptr_t and the other is a pointer, the result type is the pointer
7073 : type." */
7074 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
7075 : result_type = type2;
7076 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
7077 : result_type = type1;
7078 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
7079 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
7080 8 : TYPE_MAIN_VARIANT (type2)))
7081 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
7082 8 : TYPE_MAIN_VARIANT (type2));
7083 :
7084 409930 : if (!result_type)
7085 : {
7086 22 : if (flag_cond_mismatch)
7087 0 : result_type = void_type_node;
7088 : else
7089 : {
7090 22 : error_at (colon_loc, "type mismatch in conditional expression");
7091 22 : return error_mark_node;
7092 : }
7093 : }
7094 :
7095 : /* Merge const and volatile flags of the incoming types. */
7096 409930 : result_type
7097 409930 : = build_type_variant (result_type,
7098 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
7099 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
7100 :
7101 409930 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
7102 : semantic_result_type);
7103 409930 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
7104 : semantic_result_type);
7105 :
7106 409930 : if (ifexp_bcp && ifexp == truthvalue_true_node)
7107 : {
7108 10 : op2_int_operands = true;
7109 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
7110 : }
7111 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
7112 : {
7113 49 : op1_int_operands = true;
7114 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
7115 : }
7116 919135 : int_const = int_operands = (ifexp_int_operands
7117 409930 : && op1_int_operands
7118 409930 : && op2_int_operands);
7119 99275 : if (int_operands)
7120 : {
7121 99275 : int_const = ((ifexp == truthvalue_true_node
7122 58867 : && TREE_CODE (orig_op1) == INTEGER_CST
7123 58853 : && !TREE_OVERFLOW (orig_op1))
7124 99289 : || (ifexp == truthvalue_false_node
7125 40362 : && TREE_CODE (orig_op2) == INTEGER_CST
7126 40346 : && !TREE_OVERFLOW (orig_op2)));
7127 : }
7128 :
7129 : /* Need to convert condition operand into a vector mask. */
7130 409930 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7131 : {
7132 0 : tree vectype = TREE_TYPE (ifexp);
7133 0 : tree elem_type = TREE_TYPE (vectype);
7134 0 : tree zero = build_int_cst (elem_type, 0);
7135 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7136 0 : tree cmp_type = truth_type_for (vectype);
7137 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7138 : }
7139 :
7140 409930 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7141 99231 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7142 : else
7143 : {
7144 310699 : if (int_operands)
7145 : {
7146 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7147 : nested inside of the expression. */
7148 76 : op1 = c_fully_fold (op1, false, NULL);
7149 76 : op2 = c_fully_fold (op2, false, NULL);
7150 : }
7151 310699 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7152 310699 : if (int_operands)
7153 76 : ret = note_integer_operands (ret);
7154 : }
7155 409930 : if (semantic_result_type)
7156 4656 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7157 :
7158 409930 : protected_set_expr_location (ret, colon_loc);
7159 :
7160 : /* If the OP1 and OP2 are the same and don't have side-effects,
7161 : warn here, because the COND_EXPR will be turned into OP1. */
7162 409930 : if (warn_duplicated_branches
7163 34 : && TREE_CODE (ret) == COND_EXPR
7164 409964 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7165 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7166 : "this condition has identical branches");
7167 :
7168 : return ret;
7169 : }
7170 :
7171 : /* EXPR is an expression, location LOC, whose result is discarded.
7172 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7173 : whose right-hand operand is such a call, possibly recursively). */
7174 :
7175 : static void
7176 6741957 : maybe_warn_nodiscard (location_t loc, tree expr)
7177 : {
7178 6741957 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7179 : return;
7180 4095071 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7181 : {
7182 52678 : expr = TREE_OPERAND (expr, 1);
7183 52678 : if (EXPR_HAS_LOCATION (expr))
7184 52678 : loc = EXPR_LOCATION (expr);
7185 : }
7186 4042393 : if (TREE_CODE (expr) != CALL_EXPR)
7187 : return;
7188 349905 : tree fn = CALL_EXPR_FN (expr);
7189 349905 : if (!fn)
7190 : return;
7191 349888 : tree attr;
7192 349888 : if (TREE_CODE (fn) == ADDR_EXPR
7193 349221 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7194 699109 : && (attr = lookup_attribute ("nodiscard",
7195 349221 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7196 : {
7197 3 : fn = TREE_OPERAND (fn, 0);
7198 3 : tree args = TREE_VALUE (attr);
7199 3 : if (args)
7200 1 : args = TREE_VALUE (args);
7201 3 : auto_diagnostic_group d;
7202 3 : auto_urlify_attributes sentinel;
7203 3 : int warned;
7204 3 : if (args)
7205 1 : warned = warning_at (loc, OPT_Wunused_result,
7206 : "ignoring return value of %qD, declared with "
7207 : "attribute %<nodiscard%>: %E", fn, args);
7208 : else
7209 2 : warned = warning_at (loc, OPT_Wunused_result,
7210 : "ignoring return value of %qD, declared with "
7211 : "attribute %<nodiscard%>", fn);
7212 3 : if (warned)
7213 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7214 3 : }
7215 : else
7216 : {
7217 349885 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7218 349885 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7219 349885 : if (!attr)
7220 349873 : return;
7221 12 : tree args = TREE_VALUE (attr);
7222 12 : if (args)
7223 5 : args = TREE_VALUE (args);
7224 12 : auto_diagnostic_group d;
7225 12 : auto_urlify_attributes sentinel;
7226 12 : int warned;
7227 12 : if (args)
7228 5 : warned = warning_at (loc, OPT_Wunused_result,
7229 : "ignoring return value of type %qT, declared "
7230 : "with attribute %<nodiscard%>: %E",
7231 : rettype, args);
7232 : else
7233 7 : warned = warning_at (loc, OPT_Wunused_result,
7234 : "ignoring return value of type %qT, declared "
7235 : "with attribute %<nodiscard%>", rettype);
7236 12 : if (warned)
7237 : {
7238 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7239 : {
7240 11 : fn = TREE_OPERAND (fn, 0);
7241 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7242 11 : inform (DECL_SOURCE_LOCATION (fn),
7243 : "in call to %qD, declared here", fn);
7244 : }
7245 : }
7246 12 : }
7247 : }
7248 :
7249 : /* Return a compound expression that performs two expressions and
7250 : returns the value of the second of them.
7251 :
7252 : LOC is the location of the COMPOUND_EXPR. */
7253 :
7254 : tree
7255 114915 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7256 : {
7257 114915 : bool expr1_int_operands, expr2_int_operands;
7258 114915 : tree eptype = NULL_TREE;
7259 114915 : tree ret;
7260 :
7261 114915 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7262 : if (expr1_int_operands)
7263 326 : expr1 = remove_c_maybe_const_expr (expr1);
7264 114915 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7265 : if (expr2_int_operands)
7266 66140 : expr2 = remove_c_maybe_const_expr (expr2);
7267 :
7268 114915 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7269 0 : expr1 = TREE_OPERAND (expr1, 0);
7270 114915 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7271 : {
7272 1 : eptype = TREE_TYPE (expr2);
7273 1 : expr2 = TREE_OPERAND (expr2, 0);
7274 : }
7275 :
7276 114915 : if (!TREE_SIDE_EFFECTS (expr1))
7277 : {
7278 : /* The left-hand operand of a comma expression is like an expression
7279 : statement: with -Wunused, we should warn if it doesn't have
7280 : any side-effects, unless it was explicitly cast to (void). */
7281 8468 : if (warn_unused_value)
7282 : {
7283 1288 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7284 1288 : && CONVERT_EXPR_P (expr1))
7285 : ; /* (void) a, b */
7286 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7287 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7288 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7289 : ; /* (void) a, (void) b, c */
7290 : else
7291 11 : warning_at (loc, OPT_Wunused_value,
7292 : "left-hand operand of comma expression has no effect");
7293 : }
7294 : }
7295 106447 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7296 13189 : && warn_unused_value)
7297 : {
7298 : tree r = expr1;
7299 : location_t cloc = loc;
7300 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7301 : {
7302 2417 : if (EXPR_HAS_LOCATION (r))
7303 2417 : cloc = EXPR_LOCATION (r);
7304 2417 : r = TREE_OPERAND (r, 1);
7305 : }
7306 2416 : if (!TREE_SIDE_EFFECTS (r)
7307 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7308 2420 : && !CONVERT_EXPR_P (r))
7309 4 : warning_at (cloc, OPT_Wunused_value,
7310 : "right-hand operand of comma expression has no effect");
7311 : }
7312 :
7313 : /* With -Wunused, we should also warn if the left-hand operand does have
7314 : side-effects, but computes a value which is not used. For example, in
7315 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7316 : so we should issue a warning. */
7317 104031 : else if (warn_unused_value)
7318 35475 : warn_if_unused_value (expr1, loc);
7319 :
7320 114915 : maybe_warn_nodiscard (loc, expr1);
7321 :
7322 114915 : if (expr2 == error_mark_node)
7323 : return error_mark_node;
7324 :
7325 114902 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7326 :
7327 114902 : if (flag_isoc99
7328 : && expr1_int_operands
7329 114442 : && expr2_int_operands)
7330 153 : ret = note_integer_operands (ret);
7331 :
7332 114902 : if (eptype)
7333 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7334 :
7335 114902 : protected_set_expr_location (ret, loc);
7336 114902 : return ret;
7337 : }
7338 :
7339 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7340 : which we are casting. OTYPE is the type of the expression being
7341 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7342 : of the cast. -Wcast-qual appeared on the command line. Named
7343 : address space qualifiers are not handled here, because they result
7344 : in different warnings. */
7345 :
7346 : static void
7347 14107 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7348 : {
7349 14107 : tree in_type = type;
7350 14107 : tree in_otype = otype;
7351 14107 : int added = 0;
7352 14107 : int discarded = 0;
7353 14277 : bool is_const;
7354 :
7355 : /* Check that the qualifiers on IN_TYPE are a superset of the
7356 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7357 : nodes is uninteresting and we stop as soon as we hit a
7358 : non-POINTER_TYPE node on either type. */
7359 14277 : do
7360 : {
7361 14277 : in_otype = TREE_TYPE (in_otype);
7362 14277 : in_type = TREE_TYPE (in_type);
7363 :
7364 : /* GNU C allows cv-qualified function types. 'const' means the
7365 : function is very pure, 'volatile' means it can't return. We
7366 : need to warn when such qualifiers are added, not when they're
7367 : taken away. */
7368 14277 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7369 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7370 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7371 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7372 : else
7373 14259 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7374 14259 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7375 : }
7376 14277 : while (TREE_CODE (in_type) == POINTER_TYPE
7377 14277 : && TREE_CODE (in_otype) == POINTER_TYPE);
7378 :
7379 14107 : if (added)
7380 2 : warning_at (loc, OPT_Wcast_qual,
7381 : "cast adds %q#v qualifier to function type", added);
7382 :
7383 14107 : if (discarded)
7384 : /* There are qualifiers present in IN_OTYPE that are not present
7385 : in IN_TYPE. */
7386 1203 : warning_at (loc, OPT_Wcast_qual,
7387 : "cast discards %qv qualifier from pointer target type",
7388 : discarded);
7389 :
7390 14107 : if (added || discarded)
7391 : return;
7392 :
7393 : /* A cast from **T to const **T is unsafe, because it can cause a
7394 : const value to be changed with no additional warning. We only
7395 : issue this warning if T is the same on both sides, and we only
7396 : issue the warning if there are the same number of pointers on
7397 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7398 : cast is unsafe when a qualifier is added at one level and const
7399 : is not present at all outer levels.
7400 :
7401 : To issue this warning, we check at each level whether the cast
7402 : adds new qualifiers not already seen. We don't need to special
7403 : case function types, as they won't have the same
7404 : TYPE_MAIN_VARIANT. */
7405 :
7406 12902 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7407 : return;
7408 227 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7409 : return;
7410 :
7411 48 : in_type = type;
7412 48 : in_otype = otype;
7413 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7414 130 : do
7415 : {
7416 130 : in_type = TREE_TYPE (in_type);
7417 130 : in_otype = TREE_TYPE (in_otype);
7418 130 : if (!TYPE_QUALS (in_otype).superset_of (TYPE_QUALS (in_type))
7419 130 : && !is_const)
7420 : {
7421 27 : warning_at (loc, OPT_Wcast_qual,
7422 : "to be safe all intermediate pointers in cast from "
7423 : "%qT to %qT must be %<const%> qualified",
7424 : otype, type);
7425 27 : break;
7426 : }
7427 103 : if (is_const)
7428 72 : is_const = TYPE_READONLY (in_type);
7429 : }
7430 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7431 : }
7432 :
7433 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7434 :
7435 : static bool
7436 1316 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7437 : {
7438 1316 : if (error_operand_p (t1) || error_operand_p (t2))
7439 : return true;
7440 :
7441 1315 : t1 = TYPE_MAIN_VARIANT (t1);
7442 1315 : t2 = TYPE_MAIN_VARIANT (t2);
7443 :
7444 1315 : if (TREE_CODE (t1) == POINTER_TYPE
7445 229 : && TREE_CODE (t2) == POINTER_TYPE)
7446 : return true;
7447 :
7448 : /* Only the precision of the parameter matters. This check should
7449 : make sure that the callee does not see undefined values in argument
7450 : registers. */
7451 1107 : if (INTEGRAL_TYPE_P (t1)
7452 917 : && INTEGRAL_TYPE_P (t2)
7453 1452 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7454 : return true;
7455 :
7456 942 : return comptypes (t1, t2);
7457 : }
7458 :
7459 : /* Check if a type cast between two function types can be considered safe. */
7460 :
7461 : static bool
7462 7563 : c_safe_function_type_cast_p (tree t1, tree t2)
7463 : {
7464 7563 : if (TREE_TYPE (t1) == void_type_node &&
7465 5561 : TYPE_ARG_TYPES (t1) == void_list_node)
7466 : return true;
7467 :
7468 3726 : if (TREE_TYPE (t2) == void_type_node &&
7469 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7470 : return true;
7471 :
7472 896 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7473 : return false;
7474 :
7475 197 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7476 513 : t1 && t2;
7477 316 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7478 420 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7479 : return false;
7480 :
7481 : return true;
7482 : }
7483 :
7484 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7485 : LOC is the location of the cast-- typically the open paren of the cast. */
7486 :
7487 : tree
7488 125226880 : build_c_cast (location_t loc, tree type, tree expr)
7489 : {
7490 125226880 : tree value;
7491 :
7492 125226880 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7493 :
7494 125226880 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7495 66 : expr = TREE_OPERAND (expr, 0);
7496 :
7497 125226880 : value = expr;
7498 125226880 : if (int_operands)
7499 7252273 : value = remove_c_maybe_const_expr (value);
7500 :
7501 125226880 : if (type == error_mark_node || expr == error_mark_node)
7502 : return error_mark_node;
7503 :
7504 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7505 : only in <protocol> qualifications. But when constructing cast expressions,
7506 : the protocols do matter and must be kept around. */
7507 125225860 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7508 0 : return build1 (NOP_EXPR, type, expr);
7509 :
7510 125225860 : type = TYPE_MAIN_VARIANT (type);
7511 :
7512 125225860 : if (TREE_CODE (type) == ARRAY_TYPE)
7513 : {
7514 13 : error_at (loc, "cast specifies array type");
7515 13 : return error_mark_node;
7516 : }
7517 :
7518 125225847 : if (TREE_CODE (type) == FUNCTION_TYPE)
7519 : {
7520 6 : error_at (loc, "cast specifies function type");
7521 6 : return error_mark_node;
7522 : }
7523 :
7524 125225841 : if (!VOID_TYPE_P (type))
7525 : {
7526 125181113 : value = require_complete_type (loc, value);
7527 125181113 : if (value == error_mark_node)
7528 : return error_mark_node;
7529 : }
7530 :
7531 125225840 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7532 : {
7533 18978140 : if (RECORD_OR_UNION_TYPE_P (type)
7534 18978140 : && pedwarn (loc, OPT_Wpedantic,
7535 : "ISO C forbids casting nonscalar to the same type"))
7536 : ;
7537 18978136 : else if (warn_useless_cast && c_inhibit_evaluation_warnings == 0)
7538 8 : warning_at (loc, OPT_Wuseless_cast,
7539 : "useless cast to type %qT", type);
7540 :
7541 : /* Convert to remove any qualifiers from VALUE's type. */
7542 18978140 : value = convert (type, value);
7543 : }
7544 106247700 : else if (TREE_CODE (type) == UNION_TYPE)
7545 : {
7546 137 : tree field;
7547 :
7548 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7549 185 : if (TREE_TYPE (field) != error_mark_node
7550 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7551 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7552 : break;
7553 :
7554 137 : if (field)
7555 : {
7556 130 : tree t;
7557 130 : bool maybe_const = true;
7558 :
7559 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7560 130 : t = c_fully_fold (value, false, &maybe_const);
7561 130 : t = build_constructor_single (type, field, t);
7562 130 : if (!maybe_const)
7563 15 : t = c_wrap_maybe_const (t, true);
7564 130 : t = digest_init (loc, field, type, t,
7565 : NULL_TREE, false, false, false, true, false, false);
7566 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7567 130 : return t;
7568 : }
7569 7 : error_at (loc, "cast to union type from type not present in union");
7570 7 : return error_mark_node;
7571 : }
7572 : else
7573 : {
7574 106247563 : tree otype, ovalue;
7575 :
7576 106247563 : if (type == void_type_node)
7577 : {
7578 18758 : tree t = build1 (CONVERT_EXPR, type, value);
7579 18758 : SET_EXPR_LOCATION (t, loc);
7580 18758 : return t;
7581 : }
7582 :
7583 106228805 : otype = TREE_TYPE (value);
7584 :
7585 : /* Optionally warn about potentially worrisome casts. */
7586 106228805 : if (warn_cast_qual
7587 204404 : && TREE_CODE (type) == POINTER_TYPE
7588 27455 : && TREE_CODE (otype) == POINTER_TYPE)
7589 14107 : handle_warn_cast_qual (loc, type, otype);
7590 :
7591 : /* Warn about conversions between pointers to disjoint
7592 : address spaces. */
7593 106228805 : if (TREE_CODE (type) == POINTER_TYPE
7594 3381666 : && TREE_CODE (otype) == POINTER_TYPE
7595 109261625 : && !null_pointer_constant_p (value))
7596 : {
7597 2978234 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7598 2978234 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7599 2978234 : addr_space_t as_common;
7600 :
7601 2978234 : if (!addr_space_superset (as_to, as_from, &as_common))
7602 : {
7603 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7604 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7605 : "from disjoint generic address space pointer",
7606 : c_addr_space_name (as_to));
7607 :
7608 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7609 0 : warning_at (loc, 0, "cast to generic address space pointer "
7610 : "from disjoint %qs address space pointer",
7611 : c_addr_space_name (as_from));
7612 :
7613 : else
7614 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7615 : "from disjoint %qs address space pointer",
7616 : c_addr_space_name (as_to),
7617 : c_addr_space_name (as_from));
7618 : }
7619 :
7620 : /* Warn of new allocations that are not big enough for the target
7621 : type. */
7622 2978234 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7623 1069 : if (tree fndecl = get_callee_fndecl (value))
7624 1065 : if (DECL_IS_MALLOC (fndecl))
7625 : {
7626 573 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7627 573 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7628 573 : if (alloc_size)
7629 215 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7630 : alloc_size);
7631 : }
7632 : }
7633 :
7634 : /* Warn about possible alignment problems. */
7635 106228805 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7636 7 : && TREE_CODE (type) == POINTER_TYPE
7637 7 : && TREE_CODE (otype) == POINTER_TYPE
7638 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7639 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7640 : /* Don't warn about opaque types, where the actual alignment
7641 : restriction is unknown. */
7642 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7643 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7644 106228819 : && min_align_of_type (TREE_TYPE (type))
7645 7 : > min_align_of_type (TREE_TYPE (otype)))
7646 5 : warning_at (loc, OPT_Wcast_align,
7647 : "cast increases required alignment of target type");
7648 :
7649 106228805 : if ((TREE_CODE (type) == INTEGER_TYPE
7650 106228805 : || TREE_CODE (type) == BITINT_TYPE)
7651 7416022 : && TREE_CODE (otype) == POINTER_TYPE
7652 106254390 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7653 : /* Unlike conversion of integers to pointers, where the
7654 : warning is disabled for converting constants because
7655 : of cases such as SIG_*, warn about converting constant
7656 : pointers to integers. In some cases it may cause unwanted
7657 : sign extension, and a warning is appropriate. */
7658 1464 : warning_at (loc, OPT_Wpointer_to_int_cast,
7659 : "cast from pointer to integer of different size");
7660 :
7661 106228805 : if ((TREE_CODE (value) == CALL_EXPR
7662 36375665 : && !is_access_with_size_p (value))
7663 142604466 : && TREE_CODE (type) != TREE_CODE (otype))
7664 2304 : warning_at (loc, OPT_Wbad_function_cast,
7665 : "cast from function call of type %qT "
7666 : "to non-matching type %qT", otype, type);
7667 :
7668 106228805 : if (TREE_CODE (type) == POINTER_TYPE
7669 3381666 : && (TREE_CODE (otype) == INTEGER_TYPE
7670 3381666 : || TREE_CODE (otype) == BITINT_TYPE)
7671 348757 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7672 : /* Don't warn about converting any constant. */
7673 106539361 : && !TREE_CONSTANT (value))
7674 407 : warning_at (loc,
7675 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7676 : "of different size");
7677 :
7678 106228805 : if (warn_strict_aliasing <= 2)
7679 97449379 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7680 :
7681 : /* If pedantic, warn for conversions between function and object
7682 : pointer types, except for converting a null pointer constant
7683 : to function pointer type. */
7684 106228805 : if (pedantic
7685 261509 : && TREE_CODE (type) == POINTER_TYPE
7686 144380 : && TREE_CODE (otype) == POINTER_TYPE
7687 3035 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7688 106228826 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7689 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7690 : "conversion of function pointer to object pointer type");
7691 :
7692 106228805 : if (pedantic
7693 261509 : && TREE_CODE (type) == POINTER_TYPE
7694 144380 : && TREE_CODE (otype) == POINTER_TYPE
7695 3035 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7696 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7697 106228815 : && !null_pointer_constant_p (value))
7698 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7699 : "conversion of object pointer to function pointer type");
7700 :
7701 106228805 : if (TREE_CODE (type) == POINTER_TYPE
7702 3381666 : && TREE_CODE (otype) == POINTER_TYPE
7703 3032820 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7704 8927 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7705 106236368 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7706 7563 : TREE_TYPE (otype)))
7707 803 : warning_at (loc, OPT_Wcast_function_type,
7708 : "cast between incompatible function types"
7709 : " from %qT to %qT", otype, type);
7710 :
7711 106228805 : ovalue = value;
7712 : /* If converting to boolean a value with integer operands that
7713 : is not itself represented as an INTEGER_CST, the call below
7714 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7715 : build_binary_op as called by c_common_truthvalue_conversion
7716 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7717 : subexpression has been fully folded. To avoid nested
7718 : C_MAYBE_CONST_EXPR, ensure that
7719 : c_objc_common_truthvalue_conversion receives an argument
7720 : properly marked as having integer operands in that case. */
7721 106228805 : if (int_operands
7722 7002910 : && TREE_CODE (value) != INTEGER_CST
7723 106228847 : && (TREE_CODE (type) == BOOLEAN_TYPE
7724 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7725 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7726 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7727 28 : value = note_integer_operands (value);
7728 106228805 : value = convert (type, value);
7729 :
7730 : /* Ignore any integer overflow caused by the cast. */
7731 106228805 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7732 : {
7733 6949760 : if (TREE_OVERFLOW_P (ovalue))
7734 : {
7735 9 : if (!TREE_OVERFLOW (value))
7736 : {
7737 : /* Avoid clobbering a shared constant. */
7738 0 : value = copy_node (value);
7739 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7740 : }
7741 : }
7742 6949751 : else if (TREE_OVERFLOW (value))
7743 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7744 3133 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7745 : }
7746 : }
7747 :
7748 : /* Don't let a cast be an lvalue. */
7749 125206945 : if (lvalue_p (value)
7750 : /* Also, don't let a cast be a bit-field of a bit-precise integer type,
7751 : otherwise it could be incorrectly subject of integer promotions. */
7752 125206945 : || (TREE_CODE (value) == COMPONENT_REF
7753 24 : && DECL_C_BIT_FIELD (TREE_OPERAND (value, 1))
7754 24 : && BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (value, 1)))))
7755 91737 : value = non_lvalue_loc (loc, value);
7756 :
7757 : /* Don't allow the results of casting to floating-point or complex
7758 : types be confused with actual constants, or casts involving
7759 : integer and pointer types other than direct integer-to-integer
7760 : and integer-to-pointer be confused with integer constant
7761 : expressions and null pointer constants. */
7762 125206945 : if (TREE_CODE (value) == REAL_CST
7763 125156975 : || TREE_CODE (value) == COMPLEX_CST
7764 250341294 : || (TREE_CODE (value) == INTEGER_CST
7765 7258498 : && !((TREE_CODE (expr) == INTEGER_CST
7766 7184170 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7767 66924 : || TREE_CODE (expr) == REAL_CST
7768 : || TREE_CODE (expr) == COMPLEX_CST)))
7769 135153 : value = build1 (NOP_EXPR, type, value);
7770 :
7771 : /* If the expression has integer operands and so can occur in an
7772 : unevaluated part of an integer constant expression, ensure the
7773 : return value reflects this. */
7774 125206945 : if (int_operands
7775 7238260 : && INTEGRAL_TYPE_P (type)
7776 6789434 : && value != error_mark_node
7777 131996378 : && !EXPR_INT_CONST_OPERANDS (value))
7778 11 : value = note_integer_operands (value);
7779 :
7780 125206945 : protected_set_expr_location (value, loc);
7781 125206945 : return value;
7782 : }
7783 :
7784 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7785 : location of the open paren of the cast, or the position of the cast
7786 : expr. */
7787 : tree
7788 125226375 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7789 : {
7790 125226375 : tree type;
7791 125226375 : tree type_expr = NULL_TREE;
7792 125226375 : bool type_expr_const = true;
7793 125226375 : tree ret;
7794 125226375 : int saved_wsp = warn_strict_prototypes;
7795 :
7796 : /* This avoids warnings about unprototyped casts on
7797 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7798 125226375 : if (TREE_CODE (expr) == INTEGER_CST)
7799 7306964 : warn_strict_prototypes = 0;
7800 125226375 : type = groktypename (type_name, &type_expr, &type_expr_const);
7801 125226375 : warn_strict_prototypes = saved_wsp;
7802 :
7803 765705 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7804 125991825 : && reject_gcc_builtin (expr))
7805 2 : return error_mark_node;
7806 :
7807 125226373 : ret = build_c_cast (loc, type, expr);
7808 125226373 : if (ret == error_mark_node)
7809 : return error_mark_node;
7810 :
7811 125225213 : if (type_expr)
7812 : {
7813 250 : bool inner_expr_const = true;
7814 250 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7815 250 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7816 500 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7817 145 : && inner_expr_const);
7818 250 : SET_EXPR_LOCATION (ret, loc);
7819 : }
7820 :
7821 125225213 : if (!EXPR_HAS_LOCATION (ret))
7822 7184485 : protected_set_expr_location (ret, loc);
7823 :
7824 : /* C++ does not permits types to be defined in a cast, but it
7825 : allows references to incomplete types. */
7826 125225213 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7827 1 : warning_at (loc, OPT_Wc___compat,
7828 : "defining a type in a cast is invalid in C++");
7829 :
7830 : return ret;
7831 : }
7832 :
7833 : /* Build an assignment expression of lvalue LHS from value RHS.
7834 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7835 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7836 : MODIFYCODE is the code for a binary operator that we use
7837 : to combine the old value of LHS with RHS to get the new value.
7838 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7839 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7840 : which may differ from TREE_TYPE (RHS) for an enum value.
7841 :
7842 : LOCATION is the location of the MODIFYCODE operator.
7843 : RHS_LOC is the location of the RHS. */
7844 :
7845 : tree
7846 3012255 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7847 : enum tree_code modifycode,
7848 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7849 : {
7850 3012255 : tree result;
7851 3012255 : tree newrhs;
7852 3012255 : tree rhseval = NULL_TREE;
7853 3012255 : tree lhstype = TREE_TYPE (lhs);
7854 3012255 : tree olhstype = lhstype;
7855 3012255 : bool npc;
7856 3012255 : bool is_atomic_op;
7857 :
7858 : /* Types that aren't fully specified cannot be used in assignments. */
7859 3012255 : lhs = require_complete_type (location, lhs);
7860 3012255 : rhs = require_complete_type (location, rhs);
7861 :
7862 : /* Avoid duplicate error messages from operands that had errors. */
7863 3012255 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7864 473 : return error_mark_node;
7865 :
7866 : /* Ensure an error for assigning a non-lvalue array to an array in
7867 : C90. */
7868 3011782 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7869 : {
7870 1 : error_at (location, "assignment to expression with array type");
7871 1 : return error_mark_node;
7872 : }
7873 :
7874 : /* For ObjC properties, defer this check. */
7875 3011781 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7876 32 : return error_mark_node;
7877 :
7878 3011749 : is_atomic_op = really_atomic_lvalue (lhs);
7879 :
7880 3011749 : newrhs = rhs;
7881 :
7882 3011749 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7883 : {
7884 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7885 : lhs_origtype, modifycode, rhs_loc, rhs,
7886 : rhs_origtype);
7887 2 : if (inner == error_mark_node)
7888 : return error_mark_node;
7889 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7890 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7891 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7892 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7893 2 : protected_set_expr_location (result, location);
7894 2 : return result;
7895 : }
7896 :
7897 : /* If a binary op has been requested, combine the old LHS value with the RHS
7898 : producing the value we should actually store into the LHS. */
7899 :
7900 3011747 : if (modifycode != NOP_EXPR)
7901 : {
7902 219594 : lhs = c_fully_fold (lhs, false, NULL, true);
7903 219594 : lhs = stabilize_reference (lhs);
7904 :
7905 : /* Construct the RHS for any non-atomic compound assignment. */
7906 219594 : if (!is_atomic_op)
7907 : {
7908 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7909 : are preevaluated before the rest of the assignment expression's
7910 : side-effects, because RHS could contain e.g. function calls
7911 : that modify LHS. */
7912 204109 : if (TREE_SIDE_EFFECTS (rhs))
7913 : {
7914 9007 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7915 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7916 : else
7917 8979 : newrhs = save_expr (rhs);
7918 9007 : rhseval = newrhs;
7919 9007 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7920 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7921 : newrhs);
7922 : }
7923 204109 : bool clear_decl_read = false;
7924 90480 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7925 157264 : && !DECL_READ_P (lhs)
7926 257272 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7927 : : warn_unused_but_set_parameter) > 2)
7928 : {
7929 20971 : mark_exp_read (newrhs);
7930 20971 : if (!DECL_READ_P (lhs))
7931 204109 : clear_decl_read = true;
7932 : }
7933 :
7934 204109 : newrhs = build_binary_op (location, modifycode,
7935 : convert_lvalue_to_rvalue (location, lhs,
7936 : true, true),
7937 : newrhs, true);
7938 204109 : if (clear_decl_read)
7939 20971 : DECL_READ_P (lhs) = 0;
7940 :
7941 : /* The original type of the right hand side is no longer
7942 : meaningful. */
7943 : rhs_origtype = NULL_TREE;
7944 : }
7945 : }
7946 :
7947 3011747 : if (c_dialect_objc ())
7948 : {
7949 : /* Check if we are modifying an Objective-C property reference;
7950 : if so, we need to generate setter calls. */
7951 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7952 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7953 : else
7954 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7955 0 : if (result)
7956 0 : goto return_result;
7957 :
7958 : /* Else, do the check that we postponed for Objective-C. */
7959 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7960 0 : return error_mark_node;
7961 : }
7962 :
7963 : /* Give an error for storing in something that is 'const'. */
7964 :
7965 3011747 : if (TYPE_READONLY (lhstype)
7966 3011747 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7967 25866 : && C_TYPE_FIELDS_READONLY (lhstype)))
7968 : {
7969 88 : readonly_error (location, lhs, lv_assign);
7970 88 : return error_mark_node;
7971 : }
7972 3011659 : else if (TREE_READONLY (lhs))
7973 1 : readonly_warning (lhs, lv_assign);
7974 :
7975 : /* If storing into a structure or union member,
7976 : it has probably been given type `int'.
7977 : Compute the type that would go with
7978 : the actual amount of storage the member occupies. */
7979 :
7980 3011659 : if (TREE_CODE (lhs) == COMPONENT_REF
7981 253315 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7982 253315 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7983 111320 : || SCALAR_FLOAT_TYPE_P (lhstype)
7984 87831 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7985 172488 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7986 :
7987 : /* If storing in a field that is in actuality a short or narrower than one,
7988 : we must store in the field in its actual type. */
7989 :
7990 3011659 : if (lhstype != TREE_TYPE (lhs))
7991 : {
7992 0 : lhs = copy_node (lhs);
7993 0 : TREE_TYPE (lhs) = lhstype;
7994 : }
7995 :
7996 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7997 : when LHS does not have its original type. This happens for,
7998 : e.g., an enum bitfield in a struct. */
7999 3011659 : if (warn_cxx_compat
8000 32139 : && lhs_origtype != NULL_TREE
8001 32139 : && lhs_origtype != lhstype
8002 95 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
8003 : {
8004 32 : tree checktype = (rhs_origtype != NULL_TREE
8005 32 : ? rhs_origtype
8006 0 : : TREE_TYPE (rhs));
8007 32 : if (checktype != error_mark_node
8008 32 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
8009 30 : || (is_atomic_op && modifycode != NOP_EXPR)))
8010 2 : warning_at (location, OPT_Wc___compat,
8011 : "enum conversion in assignment is invalid in C++");
8012 : }
8013 :
8014 : /* Remove qualifiers. */
8015 3011659 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
8016 3011659 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
8017 :
8018 : /* Convert new value to destination type. Fold it first, then
8019 : restore any excess precision information, for the sake of
8020 : conversion warnings. */
8021 :
8022 3011659 : if (!(is_atomic_op && modifycode != NOP_EXPR))
8023 : {
8024 2996174 : tree rhs_semantic_type = NULL_TREE;
8025 2996174 : if (!c_in_omp_for)
8026 : {
8027 2978866 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
8028 : {
8029 5097 : rhs_semantic_type = TREE_TYPE (newrhs);
8030 5097 : newrhs = TREE_OPERAND (newrhs, 0);
8031 : }
8032 2978866 : npc = null_pointer_constant_p (newrhs);
8033 2978866 : newrhs = c_fully_fold (newrhs, false, NULL);
8034 2978866 : if (rhs_semantic_type)
8035 5097 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
8036 : }
8037 : else
8038 17308 : npc = null_pointer_constant_p (newrhs);
8039 2996174 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
8040 : rhs_origtype, ic_assign, npc,
8041 : NULL_TREE, NULL_TREE, 0);
8042 2996174 : if (TREE_CODE (newrhs) == ERROR_MARK)
8043 127 : return error_mark_node;
8044 : }
8045 :
8046 : /* Emit ObjC write barrier, if necessary. */
8047 3011532 : if (c_dialect_objc () && flag_objc_gc)
8048 : {
8049 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8050 0 : if (result)
8051 : {
8052 0 : protected_set_expr_location (result, location);
8053 0 : goto return_result;
8054 : }
8055 : }
8056 :
8057 : /* Scan operands. */
8058 :
8059 3011532 : if (is_atomic_op)
8060 25988 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
8061 : else
8062 : {
8063 2985544 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
8064 2985544 : TREE_SIDE_EFFECTS (result) = 1;
8065 2985544 : protected_set_expr_location (result, location);
8066 : }
8067 :
8068 : /* If we got the LHS in a different type for storing in,
8069 : convert the result back to the nominal type of LHS
8070 : so that the value we return always has the same type
8071 : as the LHS argument. */
8072 :
8073 3011532 : if (olhstype == TREE_TYPE (result))
8074 3011532 : goto return_result;
8075 :
8076 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
8077 : rhs_origtype, ic_assign, false, NULL_TREE,
8078 : NULL_TREE, 0);
8079 0 : protected_set_expr_location (result, location);
8080 :
8081 3011532 : return_result:
8082 3011532 : if (rhseval)
8083 9007 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
8084 : return result;
8085 : }
8086 :
8087 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
8088 : This is used to implement -fplan9-extensions. */
8089 :
8090 : static bool
8091 26 : find_anonymous_field_with_type (tree struct_type, tree type)
8092 : {
8093 26 : tree field;
8094 26 : bool found;
8095 :
8096 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
8097 26 : found = false;
8098 26 : for (field = TYPE_FIELDS (struct_type);
8099 68 : field != NULL_TREE;
8100 42 : field = TREE_CHAIN (field))
8101 : {
8102 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8103 42 : if (DECL_NAME (field) == NULL
8104 42 : && comptypes (type, fieldtype))
8105 : {
8106 4 : if (found)
8107 : return false;
8108 : found = true;
8109 : }
8110 38 : else if (DECL_NAME (field) == NULL
8111 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
8112 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
8113 : {
8114 0 : if (found)
8115 : return false;
8116 : found = true;
8117 : }
8118 : }
8119 : return found;
8120 : }
8121 :
8122 : /* RHS is an expression whose type is pointer to struct. If there is
8123 : an anonymous field in RHS with type TYPE, then return a pointer to
8124 : that field in RHS. This is used with -fplan9-extensions. This
8125 : returns NULL if no conversion could be found. */
8126 :
8127 : static tree
8128 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
8129 : {
8130 28 : tree rhs_struct_type, lhs_main_type;
8131 28 : tree field, found_field;
8132 28 : bool found_sub_field;
8133 28 : tree ret;
8134 :
8135 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8136 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8137 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8138 :
8139 28 : gcc_assert (POINTER_TYPE_P (type));
8140 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8141 :
8142 28 : found_field = NULL_TREE;
8143 28 : found_sub_field = false;
8144 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8145 172 : field != NULL_TREE;
8146 144 : field = TREE_CHAIN (field))
8147 : {
8148 148 : if (DECL_NAME (field) != NULL_TREE
8149 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8150 96 : continue;
8151 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8152 52 : if (comptypes (lhs_main_type, fieldtype))
8153 : {
8154 28 : if (found_field != NULL_TREE)
8155 : return NULL_TREE;
8156 : found_field = field;
8157 : }
8158 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8159 : lhs_main_type))
8160 : {
8161 4 : if (found_field != NULL_TREE)
8162 : return NULL_TREE;
8163 : found_field = field;
8164 : found_sub_field = true;
8165 : }
8166 : }
8167 :
8168 24 : if (found_field == NULL_TREE)
8169 : return NULL_TREE;
8170 :
8171 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8172 : build_fold_indirect_ref (rhs), found_field,
8173 : NULL_TREE);
8174 24 : ret = build_fold_addr_expr_loc (location, ret);
8175 :
8176 24 : if (found_sub_field)
8177 : {
8178 2 : ret = convert_to_anonymous_field (location, type, ret);
8179 2 : gcc_assert (ret != NULL_TREE);
8180 : }
8181 :
8182 : return ret;
8183 : }
8184 :
8185 : /* Issue an error message for a bad initializer component.
8186 : GMSGID identifies the message.
8187 : The component name is taken from the spelling stack. */
8188 :
8189 : static void ATTRIBUTE_GCC_DIAG (2,0)
8190 951 : error_init (location_t loc, const char *gmsgid, ...)
8191 : {
8192 951 : char *ofwhat;
8193 :
8194 951 : auto_diagnostic_group d;
8195 :
8196 : /* The gmsgid may be a format string with %< and %>. */
8197 951 : va_list ap;
8198 951 : va_start (ap, gmsgid);
8199 951 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8200 951 : loc, -1, gmsgid, &ap);
8201 951 : va_end (ap);
8202 :
8203 951 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8204 951 : if (*ofwhat && warned)
8205 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8206 951 : }
8207 :
8208 : /* Used to implement pedwarn_init and permerror_init. */
8209 :
8210 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8211 2484 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8212 : va_list *ap, enum diagnostics::kind kind)
8213 : {
8214 : /* Use the location where a macro was expanded rather than where
8215 : it was defined to make sure macros defined in system headers
8216 : but used incorrectly elsewhere are diagnosed. */
8217 2484 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8218 2484 : auto_diagnostic_group d;
8219 2484 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8220 2484 : if (warned)
8221 : {
8222 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8223 1815 : if (*ofwhat)
8224 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8225 : }
8226 4968 : return warned;
8227 2484 : }
8228 :
8229 : /* Issue a pedantic warning for a bad initializer component. OPT is
8230 : the option OPT_* (from options.h) controlling this warning or 0 if
8231 : it is unconditionally given. GMSGID identifies the message. The
8232 : component name is taken from the spelling stack. */
8233 :
8234 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8235 2133 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8236 : {
8237 2133 : va_list ap;
8238 2133 : va_start (ap, gmsgid);
8239 2133 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8240 : diagnostics::kind::pedwarn);
8241 2133 : va_end (ap);
8242 2133 : return warned;
8243 : }
8244 :
8245 : /* Like pedwarn_init, but issue a permerror. */
8246 :
8247 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8248 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8249 : {
8250 351 : va_list ap;
8251 351 : va_start (ap, gmsgid);
8252 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8253 : diagnostics::kind::permerror);
8254 351 : va_end (ap);
8255 351 : return warned;
8256 : }
8257 :
8258 : /* Issue a warning for a bad initializer component.
8259 :
8260 : OPT is the OPT_W* value corresponding to the warning option that
8261 : controls this warning. GMSGID identifies the message. The
8262 : component name is taken from the spelling stack. */
8263 :
8264 : static void
8265 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8266 : {
8267 146 : char *ofwhat;
8268 146 : bool warned;
8269 :
8270 146 : auto_diagnostic_group d;
8271 :
8272 : /* Use the location where a macro was expanded rather than where
8273 : it was defined to make sure macros defined in system headers
8274 : but used incorrectly elsewhere are diagnosed. */
8275 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8276 :
8277 : /* The gmsgid may be a format string with %< and %>. */
8278 146 : warned = warning_at (exploc, opt, gmsgid);
8279 146 : if (warned)
8280 : {
8281 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8282 133 : if (*ofwhat)
8283 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8284 : }
8285 146 : }
8286 :
8287 : /* If TYPE is an array type and EXPR is a parenthesized string
8288 : constant, warn if pedantic that EXPR is being used to initialize an
8289 : object of type TYPE. */
8290 :
8291 : void
8292 7374255 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8293 : {
8294 7374255 : if (pedantic
8295 81880 : && TREE_CODE (type) == ARRAY_TYPE
8296 1207 : && TREE_CODE (expr.value) == STRING_CST
8297 472 : && expr.original_code != STRING_CST)
8298 13 : pedwarn_init (loc, OPT_Wpedantic,
8299 : "array initialized from parenthesized string constant");
8300 7374255 : }
8301 :
8302 : /* Attempt to locate the parameter with the given index within FNDECL,
8303 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8304 :
8305 : static location_t
8306 935 : get_fndecl_argument_location (tree fndecl, int argnum)
8307 : {
8308 935 : int i;
8309 935 : tree param;
8310 :
8311 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8312 935 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8313 1014 : i < argnum && param;
8314 79 : i++, param = TREE_CHAIN (param))
8315 : ;
8316 :
8317 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8318 : return DECL_SOURCE_LOCATION (FNDECL). */
8319 935 : if (param == NULL)
8320 653 : return DECL_SOURCE_LOCATION (fndecl);
8321 :
8322 282 : return DECL_SOURCE_LOCATION (param);
8323 : }
8324 :
8325 : /* Issue a note about a mismatching argument for parameter PARMNUM
8326 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8327 : Attempt to issue the note at the pertinent parameter of the decl;
8328 : failing that issue it at the location of FUNDECL; failing that
8329 : issue it at PLOC.
8330 : Use highlight_colors::actual for the ACTUAL_TYPE
8331 : and highlight_colors::expected for EXPECTED_TYPE and the
8332 : parameter of FUNDECL*/
8333 :
8334 : static void
8335 1060 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8336 : tree expected_type, tree actual_type)
8337 : {
8338 1060 : location_t loc;
8339 1060 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8340 935 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8341 : else
8342 : loc = ploc;
8343 :
8344 1060 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8345 :
8346 1060 : pp_markup::element_expected_type elem_expected_type (expected_type);
8347 1060 : pp_markup::element_actual_type elem_actual_type (actual_type);
8348 1060 : inform (&richloc,
8349 : "expected %e but argument is of type %e",
8350 : &elem_expected_type, &elem_actual_type);
8351 1060 : }
8352 :
8353 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8354 : function FUNDECL declared without prototype to parameter PARMNUM of
8355 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8356 :
8357 : static void
8358 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8359 : tree parmtype, tree argtype)
8360 : {
8361 418 : tree_code parmcode = TREE_CODE (parmtype);
8362 418 : tree_code argcode = TREE_CODE (argtype);
8363 418 : tree promoted = c_type_promotes_to (argtype);
8364 :
8365 : /* Avoid warning for enum arguments that promote to an integer type
8366 : of the same size/mode. */
8367 418 : if (parmcode == INTEGER_TYPE
8368 418 : && argcode == ENUMERAL_TYPE
8369 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8370 : return;
8371 :
8372 417 : if ((parmcode == argcode
8373 222 : || (parmcode == INTEGER_TYPE
8374 : && argcode == ENUMERAL_TYPE))
8375 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8376 : return;
8377 :
8378 : /* This diagnoses even signed/unsigned mismatches. Those might be
8379 : safe in many cases but GCC may emit suboptimal code for them so
8380 : warning on those cases drives efficiency improvements. */
8381 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8382 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8383 : ? G_("%qD argument %d type is %qT where %qT is expected "
8384 : "in a call to built-in function declared without "
8385 : "prototype")
8386 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8387 : "in a call to built-in function declared without "
8388 : "prototype"),
8389 : fundecl, parmnum, promoted, parmtype))
8390 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8391 : "built-in %qD declared here",
8392 : fundecl);
8393 : }
8394 :
8395 : /* Print a declaration in quotes, with the given highlight_color.
8396 : Analogous to handler for %qD, but with a specific highlight color. */
8397 :
8398 199 : class pp_element_quoted_decl : public pp_element
8399 : {
8400 : public:
8401 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8402 199 : : m_decl (decl),
8403 199 : m_highlight_color (highlight_color)
8404 : {
8405 : }
8406 :
8407 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8408 : {
8409 199 : ctxt.begin_quote ();
8410 199 : ctxt.begin_highlight_color (m_highlight_color);
8411 :
8412 199 : print_decl (ctxt);
8413 :
8414 199 : ctxt.end_highlight_color ();
8415 199 : ctxt.end_quote ();
8416 199 : }
8417 :
8418 199 : void print_decl (pp_markup::context &ctxt)
8419 : {
8420 199 : pretty_printer *const pp = &ctxt.m_pp;
8421 199 : pp->set_padding (pp_none);
8422 199 : if (DECL_NAME (m_decl))
8423 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8424 : else
8425 0 : pp_string (pp, _("({anonymous})"));
8426 199 : }
8427 :
8428 : private:
8429 : tree m_decl;
8430 : const char *m_highlight_color;
8431 : };
8432 :
8433 : /* If TYPE is from a typedef, issue a note showing the location
8434 : to the user.
8435 : Use HIGHLIGHT_COLOR as the highlight color. */
8436 :
8437 : static void
8438 1180 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8439 : {
8440 1180 : if (!typedef_variant_p (type))
8441 1146 : return;
8442 :
8443 34 : tree typedef_decl = TYPE_NAME (type);
8444 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8445 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8446 34 : nullptr, highlight_color);
8447 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8448 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8449 34 : }
8450 :
8451 : /* Convert value RHS to type TYPE as preparation for an assignment to
8452 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8453 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8454 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8455 : constant before any folding.
8456 : The real work of conversion is done by `convert'.
8457 : The purpose of this function is to generate error messages
8458 : for assignments that are not allowed in C.
8459 : ERRTYPE says whether it is argument passing, assignment,
8460 : initialization or return.
8461 :
8462 : In the following example, '~' denotes where EXPR_LOC and '^' where
8463 : LOCATION point to:
8464 :
8465 : f (var); [ic_argpass]
8466 : ^ ~~~
8467 : x = var; [ic_assign]
8468 : ^ ~~~;
8469 : int x = var; [ic_init]
8470 : ^^^
8471 : return x; [ic_return]
8472 : ^
8473 :
8474 : FUNCTION is a tree for the function being called.
8475 : PARMNUM is the number of the argument, for printing in error messages.
8476 : WARNOPT may be set to a warning option to issue the corresponding warning
8477 : rather than an error for invalid conversions. Used for calls to built-in
8478 : functions declared without a prototype. */
8479 :
8480 : static tree
8481 172782021 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8482 : tree rhs, tree origtype, enum impl_conv errtype,
8483 : bool null_pointer_constant, tree fundecl,
8484 : tree function, int parmnum, int warnopt /* = 0 */)
8485 : {
8486 172782021 : enum tree_code codel = TREE_CODE (type);
8487 172782021 : tree orig_rhs = rhs;
8488 172782021 : tree rhstype;
8489 172782021 : enum tree_code coder;
8490 172782021 : tree rname = NULL_TREE;
8491 172782021 : bool objc_ok = false;
8492 :
8493 : /* Use the expansion point location to handle cases such as user's
8494 : function returning a wrong-type macro defined in a system header. */
8495 172782021 : location = expansion_point_location_if_in_system_header (location);
8496 :
8497 172782021 : if (errtype == ic_argpass)
8498 : {
8499 129916403 : tree selector;
8500 : /* Change pointer to function to the function itself for
8501 : diagnostics. */
8502 129916403 : if (TREE_CODE (function) == ADDR_EXPR
8503 129916403 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8504 0 : function = TREE_OPERAND (function, 0);
8505 :
8506 : /* Handle an ObjC selector specially for diagnostics. */
8507 129916403 : selector = objc_message_selector ();
8508 129916403 : rname = function;
8509 129916403 : if (selector && parmnum > 2)
8510 : {
8511 0 : rname = selector;
8512 0 : parmnum -= 2;
8513 : }
8514 : }
8515 :
8516 : /* This macro is used to emit diagnostics to ensure that all format
8517 : strings are complete sentences, visible to gettext and checked at
8518 : compile time. */
8519 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8520 : do { \
8521 : switch (errtype) \
8522 : { \
8523 : case ic_argpass: \
8524 : { \
8525 : auto_diagnostic_group d; \
8526 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8527 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8528 : } \
8529 : break; \
8530 : case ic_assign: \
8531 : pedwarn (LOCATION, OPT, AS); \
8532 : break; \
8533 : case ic_init: \
8534 : case ic_init_const: \
8535 : pedwarn_init (LOCATION, OPT, IN); \
8536 : break; \
8537 : case ic_return: \
8538 : pedwarn (LOCATION, OPT, RE); \
8539 : break; \
8540 : default: \
8541 : gcc_unreachable (); \
8542 : } \
8543 : } while (0)
8544 :
8545 : /* This macro is used to emit diagnostics to ensure that all format
8546 : strings are complete sentences, visible to gettext and checked at
8547 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8548 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8549 : do { \
8550 : switch (errtype) \
8551 : { \
8552 : case ic_argpass: \
8553 : { \
8554 : auto_diagnostic_group d; \
8555 : if (PEDWARN) { \
8556 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8557 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8558 : } else { \
8559 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8560 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8561 : } \
8562 : } \
8563 : break; \
8564 : case ic_assign: \
8565 : if (PEDWARN) \
8566 : pedwarn (LOCATION, OPT, AS, QUALS); \
8567 : else \
8568 : warning_at (LOCATION, OPT, AS, QUALS); \
8569 : break; \
8570 : case ic_init: \
8571 : case ic_init_const: \
8572 : if (PEDWARN) \
8573 : pedwarn (LOCATION, OPT, IN, QUALS); \
8574 : else \
8575 : warning_at (LOCATION, OPT, IN, QUALS); \
8576 : break; \
8577 : case ic_return: \
8578 : if (PEDWARN) \
8579 : pedwarn (LOCATION, OPT, RE, QUALS); \
8580 : else \
8581 : warning_at (LOCATION, OPT, RE, QUALS); \
8582 : break; \
8583 : default: \
8584 : gcc_unreachable (); \
8585 : } \
8586 : } while (0)
8587 :
8588 : /* This macro is used to emit diagnostics to ensure that all format
8589 : strings are complete sentences, visible to gettext and checked at
8590 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8591 : extra parameter to enumerate qualifiers. */
8592 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8593 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8594 :
8595 :
8596 172782021 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8597 6031 : rhs = TREE_OPERAND (rhs, 0);
8598 :
8599 172782021 : rhstype = TREE_TYPE (rhs);
8600 172782021 : coder = TREE_CODE (rhstype);
8601 :
8602 172782021 : if (coder == ERROR_MARK)
8603 347 : return error_mark_node;
8604 :
8605 172781674 : if (c_dialect_objc ())
8606 : {
8607 0 : int parmno;
8608 :
8609 0 : switch (errtype)
8610 : {
8611 : case ic_return:
8612 : parmno = 0;
8613 : break;
8614 :
8615 : case ic_assign:
8616 : parmno = -1;
8617 : break;
8618 :
8619 : case ic_init:
8620 : case ic_init_const:
8621 : parmno = -2;
8622 : break;
8623 :
8624 : default:
8625 0 : parmno = parmnum;
8626 : break;
8627 : }
8628 :
8629 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8630 : }
8631 :
8632 172781674 : if (warn_cxx_compat)
8633 : {
8634 167899 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8635 167899 : if (checktype != error_mark_node
8636 167899 : && TREE_CODE (type) == ENUMERAL_TYPE
8637 174029 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8638 45 : switch (errtype)
8639 : {
8640 8 : case ic_argpass:
8641 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8642 : "passing argument %d of %qE is invalid in C++",
8643 : parmnum, rname))
8644 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8645 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8646 : "expected %qT but argument is of type %qT",
8647 : type, rhstype);
8648 : break;
8649 10 : case ic_assign:
8650 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8651 : "%qT in assignment is invalid in C++", rhstype, type);
8652 10 : break;
8653 18 : case ic_init:
8654 18 : case ic_init_const:
8655 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8656 : "%qT to %qT in initialization is invalid in C++",
8657 : rhstype, type);
8658 18 : break;
8659 9 : case ic_return:
8660 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8661 : "%qT in return is invalid in C++", rhstype, type);
8662 9 : break;
8663 0 : default:
8664 0 : gcc_unreachable ();
8665 : }
8666 : }
8667 :
8668 172781674 : if (warn_enum_conversion)
8669 : {
8670 14903878 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8671 14903878 : if (checktype != error_mark_node
8672 14903878 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8673 12859 : && TREE_CODE (type) == ENUMERAL_TYPE
8674 14912190 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8675 : {
8676 7 : gcc_rich_location loc (location);
8677 7 : warning_at (&loc, OPT_Wenum_conversion,
8678 : "implicit conversion from %qT to %qT",
8679 : checktype, type);
8680 7 : }
8681 : }
8682 :
8683 172781674 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8684 : {
8685 153360273 : warn_for_address_of_packed_member (type, orig_rhs);
8686 153360273 : if (type != rhstype)
8687 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8688 38108163 : rhs = convert (type, rhs);
8689 : return rhs;
8690 : }
8691 :
8692 19421401 : if (coder == VOID_TYPE)
8693 : {
8694 : /* Except for passing an argument to an unprototyped function,
8695 : this is a constraint violation. When passing an argument to
8696 : an unprototyped function, it is compile-time undefined;
8697 : making it a constraint in that case was rejected in
8698 : DR#252. */
8699 8 : const char msg[] = "void value not ignored as it ought to be";
8700 8 : if (warnopt)
8701 0 : warning_at (location, warnopt, msg);
8702 : else
8703 8 : error_at (location, msg);
8704 8 : return error_mark_node;
8705 : }
8706 19421393 : rhs = require_complete_type (location, rhs);
8707 19421393 : if (rhs == error_mark_node)
8708 : return error_mark_node;
8709 :
8710 19421393 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8711 6 : return error_mark_node;
8712 :
8713 : /* A non-reference type can convert to a reference. This handles
8714 : va_start, va_copy and possibly port built-ins. */
8715 19421387 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8716 : {
8717 290 : if (!lvalue_p (rhs))
8718 : {
8719 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8720 0 : if (warnopt)
8721 0 : warning_at (location, warnopt, msg);
8722 : else
8723 0 : error_at (location, msg);
8724 0 : return error_mark_node;
8725 : }
8726 290 : if (!c_mark_addressable (rhs))
8727 0 : return error_mark_node;
8728 290 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8729 290 : SET_EXPR_LOCATION (rhs, location);
8730 :
8731 290 : rhs = convert_for_assignment (location, expr_loc,
8732 290 : c_build_pointer_type (TREE_TYPE (type)),
8733 : rhs, origtype, errtype,
8734 : null_pointer_constant, fundecl, function,
8735 : parmnum, warnopt);
8736 290 : if (rhs == error_mark_node)
8737 : return error_mark_node;
8738 :
8739 290 : rhs = build1 (NOP_EXPR, type, rhs);
8740 290 : SET_EXPR_LOCATION (rhs, location);
8741 290 : return rhs;
8742 : }
8743 : /* Some types can interconvert without explicit casts. */
8744 19421097 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8745 19421097 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8746 9389276 : return convert (type, rhs);
8747 : /* Arithmetic types all interconvert, and enum is treated like int. */
8748 10031821 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8749 2306381 : || codel == FIXED_POINT_TYPE
8750 2306381 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8751 2219737 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8752 7914815 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8753 84475 : || coder == FIXED_POINT_TYPE
8754 84475 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8755 40881 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8756 : {
8757 7913431 : if (warnopt && errtype == ic_argpass)
8758 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8759 : rhstype);
8760 :
8761 7913431 : bool save = in_late_binary_op;
8762 7875527 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8763 15731770 : || (coder == REAL_TYPE
8764 278216 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8765 24242 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8766 101836 : in_late_binary_op = true;
8767 11337098 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8768 : ? expr_loc : location, type, orig_rhs,
8769 : errtype == ic_init_const);
8770 7913431 : in_late_binary_op = save;
8771 7913431 : return ret;
8772 : }
8773 :
8774 : /* Aggregates in different TUs might need conversion. */
8775 2118390 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8776 105 : && codel == coder
8777 2118412 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8778 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8779 9 : ? expr_loc : location, type, rhs);
8780 :
8781 : /* Conversion to a transparent union or record from its member types.
8782 : This applies only to function arguments. */
8783 2118381 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8784 96 : && TYPE_TRANSPARENT_AGGR (type))
8785 2118420 : && errtype == ic_argpass)
8786 : {
8787 39 : tree memb, marginal_memb = NULL_TREE;
8788 :
8789 50 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8790 : {
8791 50 : tree memb_type = TREE_TYPE (memb);
8792 :
8793 50 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8794 50 : TYPE_MAIN_VARIANT (rhstype)))
8795 : break;
8796 :
8797 27 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8798 1 : continue;
8799 :
8800 26 : if (coder == POINTER_TYPE)
8801 : {
8802 26 : tree ttl = TREE_TYPE (memb_type);
8803 26 : tree ttr = TREE_TYPE (rhstype);
8804 :
8805 : /* Any non-function converts to a [const][volatile] void *
8806 : and vice versa; otherwise, targets must be the same.
8807 : Meanwhile, the lhs target must have all the qualifiers of
8808 : the rhs. */
8809 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8810 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8811 45 : || comp_target_types (location, memb_type, rhstype))
8812 : {
8813 16 : auto lquals = TYPE_QUALS (ttl).without (TYPE_QUAL_ATOMIC);
8814 16 : auto rquals = TYPE_QUALS (ttr).without (TYPE_QUAL_ATOMIC);
8815 : /* If this type won't generate any warnings, use it. */
8816 32 : if (lquals == rquals
8817 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8818 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8819 16 : ? (rquals.superset_of (lquals))
8820 16 : : (lquals.superset_of (rquals))))
8821 : break;
8822 :
8823 : /* Keep looking for a better type, but remember this one. */
8824 0 : if (!marginal_memb)
8825 0 : marginal_memb = memb;
8826 : }
8827 : }
8828 :
8829 : /* Can convert integer zero to any pointer type. */
8830 10 : if (null_pointer_constant)
8831 : {
8832 0 : rhs = null_pointer_node;
8833 0 : break;
8834 : }
8835 : }
8836 :
8837 39 : if (memb || marginal_memb)
8838 : {
8839 39 : if (!memb)
8840 : {
8841 : /* We have only a marginally acceptable member type;
8842 : it needs a warning. */
8843 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8844 0 : tree ttr = TREE_TYPE (rhstype);
8845 :
8846 : /* Const and volatile mean something different for function
8847 : types, so the usual warnings are not appropriate. */
8848 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8849 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8850 : {
8851 : /* Because const and volatile on functions are
8852 : restrictions that say the function will not do
8853 : certain things, it is okay to use a const or volatile
8854 : function where an ordinary one is wanted, but not
8855 : vice-versa. */
8856 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8857 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8858 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8859 : OPT_Wdiscarded_qualifiers,
8860 : G_("passing argument %d of %qE "
8861 : "makes %q#v qualified function "
8862 : "pointer from unqualified"),
8863 : G_("assignment makes %q#v qualified "
8864 : "function pointer from "
8865 : "unqualified"),
8866 : G_("initialization makes %q#v qualified "
8867 : "function pointer from "
8868 : "unqualified"),
8869 : G_("return makes %q#v qualified function "
8870 : "pointer from unqualified"),
8871 : TYPE_QUALS_NO_ADDR_SPACE (ttl)
8872 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr));
8873 : }
8874 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8875 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8876 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8877 : OPT_Wdiscarded_qualifiers,
8878 : G_("passing argument %d of %qE discards "
8879 : "%qv qualifier from pointer target type"),
8880 : G_("assignment discards %qv qualifier "
8881 : "from pointer target type"),
8882 : G_("initialization discards %qv qualifier "
8883 : "from pointer target type"),
8884 : G_("return discards %qv qualifier from "
8885 : "pointer target type"),
8886 : TYPE_QUALS_NO_ADDR_SPACE (ttr)
8887 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl));
8888 :
8889 : memb = marginal_memb;
8890 : }
8891 :
8892 39 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8893 31 : pedwarn (location, OPT_Wpedantic,
8894 : "ISO C prohibits argument conversion to union type");
8895 :
8896 39 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8897 39 : return build_constructor_single (type, memb, rhs);
8898 : }
8899 : }
8900 :
8901 : /* Conversions among pointers */
8902 2118342 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8903 2116802 : && (coder == codel))
8904 : {
8905 : /* If RHS refers to a built-in declared without a prototype
8906 : BLTIN is the declaration of the built-in with a prototype
8907 : and RHSTYPE is set to the actual type of the built-in. */
8908 2037944 : tree bltin;
8909 2037944 : rhstype = type_or_builtin_type (rhs, &bltin);
8910 :
8911 2037944 : tree ttl = TREE_TYPE (type);
8912 2037944 : tree ttr = TREE_TYPE (rhstype);
8913 2037944 : bool is_opaque_pointer;
8914 2037944 : bool target_cmp = false; /* Cache comp_target_types () result. */
8915 2037944 : addr_space_t asl;
8916 2037944 : addr_space_t asr;
8917 :
8918 2037944 : tree mvl = remove_qualifiers (ttl);
8919 2037944 : tree mvr = remove_qualifiers (ttr);
8920 :
8921 : /* Opaque pointers are treated like void pointers. */
8922 2037944 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8923 :
8924 : /* The Plan 9 compiler permits a pointer to a struct to be
8925 : automatically converted into a pointer to an anonymous field
8926 : within the struct. */
8927 2037944 : if (flag_plan9_extensions
8928 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8929 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8930 26 : && mvl != mvr)
8931 : {
8932 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8933 26 : if (new_rhs != NULL_TREE)
8934 : {
8935 22 : rhs = new_rhs;
8936 22 : rhstype = TREE_TYPE (rhs);
8937 22 : coder = TREE_CODE (rhstype);
8938 22 : ttr = TREE_TYPE (rhstype);
8939 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8940 : }
8941 : }
8942 :
8943 : /* C++ does not allow the implicit conversion void* -> T*. However,
8944 : for the purpose of reducing the number of false positives, we
8945 : tolerate the special case of
8946 :
8947 : int *p = NULL;
8948 :
8949 : where NULL is typically defined in C to be '(void *) 0'. */
8950 2037944 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8951 25605 : warning_at (errtype == ic_argpass ? expr_loc : location,
8952 13608 : OPT_Wc___compat,
8953 : "request for implicit conversion "
8954 : "from %qT to %qT not permitted in C++", rhstype, type);
8955 :
8956 : /* Warn of new allocations that are not big enough for the target
8957 : type. */
8958 2037944 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8959 5463 : if (tree fndecl = get_callee_fndecl (rhs))
8960 5463 : if (DECL_IS_MALLOC (fndecl))
8961 : {
8962 4614 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8963 4614 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8964 4614 : if (alloc_size)
8965 451 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8966 : }
8967 :
8968 : /* See if the pointers point to incompatible address spaces. */
8969 2037944 : asl = TYPE_ADDR_SPACE (ttl);
8970 2037944 : asr = TYPE_ADDR_SPACE (ttr);
8971 2037944 : if (!null_pointer_constant_p (rhs)
8972 2037944 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8973 : {
8974 3 : auto_diagnostic_group d;
8975 3 : bool diagnosed = true;
8976 3 : switch (errtype)
8977 : {
8978 1 : case ic_argpass:
8979 1 : {
8980 1 : const char msg[] = G_("passing argument %d of %qE from "
8981 : "pointer to non-enclosed address space");
8982 1 : if (warnopt)
8983 0 : diagnosed
8984 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8985 : else
8986 1 : error_at (expr_loc, msg, parmnum, rname);
8987 0 : break;
8988 : }
8989 0 : case ic_assign:
8990 0 : {
8991 0 : const char msg[] = G_("assignment from pointer to "
8992 : "non-enclosed address space");
8993 0 : if (warnopt)
8994 0 : diagnosed = warning_at (location, warnopt, msg);
8995 : else
8996 0 : error_at (location, msg);
8997 0 : break;
8998 : }
8999 0 : case ic_init:
9000 0 : case ic_init_const:
9001 0 : {
9002 0 : const char msg[] = G_("initialization from pointer to "
9003 : "non-enclosed address space");
9004 0 : if (warnopt)
9005 0 : diagnosed = warning_at (location, warnopt, msg);
9006 : else
9007 0 : error_at (location, msg);
9008 0 : break;
9009 : }
9010 2 : case ic_return:
9011 2 : {
9012 2 : const char msg[] = G_("return from pointer to "
9013 : "non-enclosed address space");
9014 2 : if (warnopt)
9015 0 : diagnosed = warning_at (location, warnopt, msg);
9016 : else
9017 2 : error_at (location, msg);
9018 0 : break;
9019 : }
9020 0 : default:
9021 0 : gcc_unreachable ();
9022 : }
9023 3 : if (diagnosed)
9024 : {
9025 3 : if (errtype == ic_argpass)
9026 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9027 : else
9028 2 : inform (location, "expected %qT but pointer is of type %qT",
9029 : type, rhstype);
9030 : }
9031 3 : return error_mark_node;
9032 3 : }
9033 :
9034 : /* Check if the right-hand side has a format attribute but the
9035 : left-hand side doesn't. */
9036 2037941 : if (warn_suggest_attribute_format
9037 2037941 : && check_missing_format_attribute (type, rhstype))
9038 : {
9039 16 : switch (errtype)
9040 : {
9041 4 : case ic_argpass:
9042 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
9043 : "argument %d of %qE might be "
9044 : "a candidate for a format attribute",
9045 : parmnum, rname);
9046 4 : break;
9047 4 : case ic_assign:
9048 4 : warning_at (location, OPT_Wsuggest_attribute_format,
9049 : "assignment left-hand side might be "
9050 : "a candidate for a format attribute");
9051 4 : break;
9052 4 : case ic_init:
9053 4 : case ic_init_const:
9054 4 : warning_at (location, OPT_Wsuggest_attribute_format,
9055 : "initialization left-hand side might be "
9056 : "a candidate for a format attribute");
9057 4 : break;
9058 4 : case ic_return:
9059 4 : warning_at (location, OPT_Wsuggest_attribute_format,
9060 : "return type might be "
9061 : "a candidate for a format attribute");
9062 4 : break;
9063 0 : default:
9064 0 : gcc_unreachable ();
9065 : }
9066 : }
9067 :
9068 : /* See if the pointers point to incompatible scalar storage orders. */
9069 2037941 : if (warn_scalar_storage_order
9070 2035025 : && !null_pointer_constant_p (rhs)
9071 6038379 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
9072 2000219 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
9073 : {
9074 18 : tree t;
9075 :
9076 18 : switch (errtype)
9077 : {
9078 11 : case ic_argpass:
9079 : /* Do not warn for built-in functions, for example memcpy, since we
9080 : control how they behave and they can be useful in this area. */
9081 11 : if (TREE_CODE (rname) != FUNCTION_DECL
9082 11 : || !fndecl_built_in_p (rname))
9083 1 : warning_at (location, OPT_Wscalar_storage_order,
9084 : "passing argument %d of %qE from incompatible "
9085 : "scalar storage order", parmnum, rname);
9086 : break;
9087 3 : case ic_assign:
9088 : /* Do not warn if the RHS is a call to a function that returns a
9089 : pointer that is not an alias. */
9090 3 : if (TREE_CODE (rhs) != CALL_EXPR
9091 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9092 5 : || !DECL_IS_MALLOC (t))
9093 1 : warning_at (location, OPT_Wscalar_storage_order,
9094 : "assignment to %qT from pointer type %qT with "
9095 : "incompatible scalar storage order", type, rhstype);
9096 : break;
9097 3 : case ic_init:
9098 3 : case ic_init_const:
9099 : /* Likewise. */
9100 3 : if (TREE_CODE (rhs) != CALL_EXPR
9101 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9102 5 : || !DECL_IS_MALLOC (t))
9103 1 : warning_at (location, OPT_Wscalar_storage_order,
9104 : "initialization of %qT from pointer type %qT with "
9105 : "incompatible scalar storage order", type, rhstype);
9106 : break;
9107 1 : case ic_return:
9108 1 : warning_at (location, OPT_Wscalar_storage_order,
9109 : "returning %qT from pointer type with incompatible "
9110 : "scalar storage order %qT", rhstype, type);
9111 1 : break;
9112 0 : default:
9113 0 : gcc_unreachable ();
9114 : }
9115 : }
9116 :
9117 : /* Any non-function converts to a [const][volatile] void *
9118 : and vice versa; otherwise, targets must be the same.
9119 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
9120 423544 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
9121 1614398 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
9122 1566123 : || (target_cmp = comp_target_types (location, type, rhstype))
9123 2507 : || is_opaque_pointer
9124 2042955 : || ((c_common_unsigned_type (mvl)
9125 2507 : == c_common_unsigned_type (mvr))
9126 3460 : && (c_common_signed_type (mvl)
9127 1730 : == c_common_signed_type (mvr))
9128 1715 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
9129 : {
9130 : /* Warn about loss of qualifers from pointers to arrays with
9131 : qualifiers on the element type. */
9132 2037141 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9133 : {
9134 2132 : ttr = strip_array_types (ttr);
9135 2132 : ttl = strip_array_types (ttl);
9136 :
9137 2132 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9138 2132 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9139 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9140 : location, expr_loc,
9141 : OPT_Wdiscarded_array_qualifiers,
9142 : G_("passing argument %d of %qE discards "
9143 : "%qv qualifier from pointer target type"),
9144 : G_("assignment discards %qv qualifier "
9145 : "from pointer target type"),
9146 : G_("initialization discards %qv qualifier "
9147 : "from pointer target type"),
9148 : G_("return discards %qv qualifier from "
9149 : "pointer target type"),
9150 : TYPE_QUALS_NO_ADDR_SPACE (ttr)
9151 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl));
9152 : }
9153 2035009 : else if (pedantic
9154 146628 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9155 146608 : ||
9156 : (VOID_TYPE_P (ttr)
9157 2923 : && !null_pointer_constant
9158 184 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9159 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9160 : G_("ISO C forbids passing argument %d of "
9161 : "%qE between function pointer "
9162 : "and %<void *%>"),
9163 : G_("ISO C forbids assignment between "
9164 : "function pointer and %<void *%>"),
9165 : G_("ISO C forbids initialization between "
9166 : "function pointer and %<void *%>"),
9167 : G_("ISO C forbids return between function "
9168 : "pointer and %<void *%>"));
9169 : /* Const and volatile mean something different for function types,
9170 : so the usual warnings are not appropriate. */
9171 2034975 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9172 2002536 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9173 : {
9174 : /* Assignments between atomic and non-atomic objects are OK. */
9175 2002041 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9176 2002041 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9177 2002041 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9178 2002041 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9179 :
9180 : /* Don't warn about loss of qualifier for conversions from
9181 : qualified void* to pointers to arrays with corresponding
9182 : qualifier on the element type (except for pedantic before C23). */
9183 2002041 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9184 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9185 : OPT_Wdiscarded_qualifiers,
9186 : G_("passing argument %d of %qE discards "
9187 : "%qv qualifier from pointer target type"),
9188 : G_("assignment discards %qv qualifier "
9189 : "from pointer target type"),
9190 : G_("initialization discards %qv qualifier "
9191 : "from pointer target type"),
9192 : G_("return discards %qv qualifier from "
9193 : "pointer target type"),
9194 : TYPE_QUALS_NO_ADDR_SPACE (ttr)
9195 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl));
9196 11 : else if (warn_quals_ped)
9197 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9198 : "array with qualifier on the element is not qualified before C23");
9199 :
9200 : /* If this is not a case of ignoring a mismatch in signedness,
9201 : no warning. */
9202 2001333 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9203 1561196 : || target_cmp)
9204 : ;
9205 : /* If there is a mismatch, do warn. */
9206 1707 : else if (warn_pointer_sign)
9207 82 : switch (errtype)
9208 : {
9209 33 : case ic_argpass:
9210 33 : {
9211 33 : auto_diagnostic_group d;
9212 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9213 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9214 33 : highlight_colors::actual);
9215 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9216 : "pointer targets in passing argument %d of "
9217 : "%qE differ in signedness", parmnum, rname))
9218 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9219 : rhstype);
9220 33 : }
9221 33 : break;
9222 21 : case ic_assign:
9223 21 : pedwarn (location, OPT_Wpointer_sign,
9224 : "pointer targets in assignment from %qT to %qT "
9225 : "differ in signedness", rhstype, type);
9226 21 : break;
9227 14 : case ic_init:
9228 14 : case ic_init_const:
9229 14 : pedwarn_init (location, OPT_Wpointer_sign,
9230 : "pointer targets in initialization of %qT "
9231 : "from %qT differ in signedness", type,
9232 : rhstype);
9233 14 : break;
9234 14 : case ic_return:
9235 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9236 : "returning %qT from a function with return type "
9237 : "%qT differ in signedness", rhstype, type);
9238 14 : break;
9239 0 : default:
9240 0 : gcc_unreachable ();
9241 : }
9242 : }
9243 32934 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9244 3950 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9245 : {
9246 : /* Because const and volatile on functions are restrictions
9247 : that say the function will not do certain things,
9248 : it is okay to use a const or volatile function
9249 : where an ordinary one is wanted, but not vice-versa. */
9250 3455 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9251 3455 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9252 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9253 : OPT_Wdiscarded_qualifiers,
9254 : G_("passing argument %d of %qE makes "
9255 : "%q#v qualified function pointer "
9256 : "from unqualified"),
9257 : G_("assignment makes %q#v qualified function "
9258 : "pointer from unqualified"),
9259 : G_("initialization makes %q#v qualified "
9260 : "function pointer from unqualified"),
9261 : G_("return makes %q#v qualified function "
9262 : "pointer from unqualified"),
9263 : TYPE_QUALS_NO_ADDR_SPACE (ttl)
9264 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr));
9265 : }
9266 : }
9267 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9268 800 : else if (!objc_ok)
9269 : {
9270 800 : auto_diagnostic_group d;
9271 800 : bool warned = false;
9272 800 : pp_markup::element_expected_type e_type (type);
9273 800 : pp_markup::element_actual_type e_rhstype (rhstype);
9274 800 : switch (errtype)
9275 : {
9276 307 : case ic_argpass:
9277 307 : {
9278 307 : range_label_for_type_mismatch rhs_label (rhstype, type);
9279 307 : gcc_rich_location richloc (expr_loc, &rhs_label,
9280 307 : highlight_colors::actual);
9281 307 : warned
9282 307 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9283 : "passing argument %d of %qE from "
9284 : "incompatible pointer type",
9285 : parmnum, rname);
9286 307 : if (warned)
9287 175 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9288 307 : }
9289 307 : break;
9290 267 : case ic_assign:
9291 267 : if (bltin)
9292 11 : warned
9293 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9294 : "assignment to %e from pointer to "
9295 : "%qD with incompatible type %e",
9296 : &e_type, bltin, &e_rhstype);
9297 : else
9298 256 : warned
9299 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9300 : "assignment to %e from incompatible "
9301 : "pointer type %e",
9302 : &e_type, &e_rhstype);
9303 : break;
9304 167 : case ic_init:
9305 167 : case ic_init_const:
9306 167 : if (bltin)
9307 13 : warned
9308 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9309 : "initialization of %e from pointer to "
9310 : "%qD with incompatible type %e",
9311 : &e_type, bltin, &e_rhstype);
9312 : else
9313 154 : warned
9314 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9315 : "initialization of %e from incompatible "
9316 : "pointer type %e",
9317 : &e_type, &e_rhstype);
9318 : break;
9319 59 : case ic_return:
9320 59 : if (bltin)
9321 11 : warned
9322 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9323 : "returning pointer to %qD of type %e from "
9324 : "a function with incompatible type %e",
9325 : bltin, &e_rhstype, &e_type);
9326 : else
9327 48 : warned
9328 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9329 : "returning %e from a function with "
9330 : "incompatible return type %e",
9331 : &e_rhstype, &e_type);
9332 : break;
9333 0 : default:
9334 0 : gcc_unreachable ();
9335 : }
9336 800 : if (warned)
9337 : {
9338 : /* If the mismatching function type is a pointer to a function,
9339 : try to show the decl of the function. */
9340 590 : if (TREE_CODE (rhs) == ADDR_EXPR
9341 590 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9342 : {
9343 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9344 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9345 : {
9346 165 : gcc_rich_location richloc
9347 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9348 165 : highlight_colors::actual);
9349 165 : pp_element_quoted_decl e_rhs_fndecl
9350 165 : (rhs_fndecl, highlight_colors::actual);
9351 165 : inform (&richloc,
9352 : "%e declared here", &e_rhs_fndecl);
9353 165 : }
9354 : }
9355 : /* If either/both of the types are typedefs, show the decl. */
9356 590 : maybe_inform_typedef_location (type,
9357 : highlight_colors::expected);
9358 590 : maybe_inform_typedef_location (rhstype,
9359 : highlight_colors::actual);
9360 : }
9361 800 : }
9362 :
9363 : /* If RHS isn't an address, check pointer or array of packed
9364 : struct or union. */
9365 2037941 : warn_for_address_of_packed_member (type, orig_rhs);
9366 :
9367 2037941 : return convert (type, rhs);
9368 : }
9369 80398 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9370 : {
9371 : /* ??? This should not be an error when inlining calls to
9372 : unprototyped functions. */
9373 4 : const char msg[] = "invalid use of non-lvalue array";
9374 4 : if (warnopt)
9375 0 : warning_at (location, warnopt, msg);
9376 : else
9377 4 : error_at (location, msg);
9378 4 : return error_mark_node;
9379 : }
9380 80394 : else if (codel == POINTER_TYPE
9381 78854 : && (coder == INTEGER_TYPE
9382 78854 : || coder == ENUMERAL_TYPE
9383 626 : || coder == BOOLEAN_TYPE
9384 626 : || coder == NULLPTR_TYPE
9385 25 : || coder == BITINT_TYPE))
9386 : {
9387 78843 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9388 77845 : && coder != NULLPTR_TYPE)
9389 77273 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9390 : "zero as null pointer constant");
9391 : /* A NULLPTR type is just a nullptr always. */
9392 78271 : if (coder == NULLPTR_TYPE)
9393 587 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9394 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9395 : or one that results from arithmetic, even including a cast to
9396 : integer type. */
9397 78256 : else if (!null_pointer_constant)
9398 972 : switch (errtype)
9399 : {
9400 800 : case ic_argpass:
9401 800 : {
9402 800 : auto_diagnostic_group d;
9403 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9404 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9405 800 : highlight_colors::actual);
9406 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9407 : "passing argument %d of %qE makes pointer "
9408 : "from integer without a cast", parmnum, rname))
9409 : {
9410 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9411 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9412 : }
9413 800 : }
9414 800 : break;
9415 93 : case ic_assign:
9416 93 : permerror_opt (location, OPT_Wint_conversion,
9417 : "assignment to %qT from %qT makes pointer from "
9418 : "integer without a cast", type, rhstype);
9419 93 : break;
9420 56 : case ic_init:
9421 56 : case ic_init_const:
9422 56 : permerror_init (location, OPT_Wint_conversion,
9423 : "initialization of %qT from %qT makes pointer "
9424 : "from integer without a cast", type, rhstype);
9425 56 : break;
9426 23 : case ic_return:
9427 23 : permerror_init (location, OPT_Wint_conversion,
9428 : "returning %qT from a function with return type "
9429 : "%qT makes pointer from integer without a cast",
9430 : rhstype, type);
9431 23 : break;
9432 0 : default:
9433 0 : gcc_unreachable ();
9434 : }
9435 :
9436 78256 : return convert (type, rhs);
9437 : }
9438 1551 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9439 540 : && coder == POINTER_TYPE)
9440 : {
9441 514 : switch (errtype)
9442 : {
9443 344 : case ic_argpass:
9444 344 : {
9445 344 : auto_diagnostic_group d;
9446 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9447 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9448 344 : highlight_colors::actual);
9449 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9450 : "passing argument %d of %qE makes integer from "
9451 : "pointer without a cast", parmnum, rname))
9452 : {
9453 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9454 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9455 : }
9456 344 : }
9457 344 : break;
9458 52 : case ic_assign:
9459 52 : permerror_opt (location, OPT_Wint_conversion,
9460 : "assignment to %qT from %qT makes integer from "
9461 : "pointer without a cast", type, rhstype);
9462 52 : break;
9463 83 : case ic_init:
9464 83 : case ic_init_const:
9465 83 : permerror_init (location, OPT_Wint_conversion,
9466 : "initialization of %qT from %qT makes integer "
9467 : "from pointer without a cast", type, rhstype);
9468 83 : break;
9469 35 : case ic_return:
9470 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9471 : "function with return type %qT makes integer from "
9472 : "pointer without a cast", rhstype, type);
9473 35 : break;
9474 0 : default:
9475 0 : gcc_unreachable ();
9476 : }
9477 :
9478 514 : return convert (type, rhs);
9479 : }
9480 624 : else if (C_BOOLEAN_TYPE_P (type)
9481 : /* The type nullptr_t may be converted to bool. The
9482 : result is false. */
9483 1039 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9484 : {
9485 415 : tree ret;
9486 415 : bool save = in_late_binary_op;
9487 415 : in_late_binary_op = true;
9488 415 : ret = convert (type, rhs);
9489 415 : in_late_binary_op = save;
9490 415 : return ret;
9491 : }
9492 622 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9493 6 : return convert (type, rhs);
9494 :
9495 616 : switch (errtype)
9496 : {
9497 33 : case ic_argpass:
9498 33 : {
9499 33 : auto_diagnostic_group d;
9500 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9501 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9502 33 : highlight_colors::actual);
9503 33 : const char msg[] = G_("incompatible type for argument %d of %qE");
9504 33 : if (warnopt)
9505 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9506 : else
9507 25 : error_at (&richloc, msg, parmnum, rname);
9508 33 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9509 33 : }
9510 33 : break;
9511 108 : case ic_assign:
9512 108 : {
9513 108 : const char msg[]
9514 : = G_("incompatible types when assigning to type %qT from type %qT");
9515 108 : if (warnopt)
9516 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9517 : else
9518 108 : error_at (expr_loc, msg, type, rhstype);
9519 108 : break;
9520 : }
9521 460 : case ic_init:
9522 460 : case ic_init_const:
9523 460 : {
9524 460 : const char msg[]
9525 : = G_("incompatible types when initializing type %qT using type %qT");
9526 460 : if (warnopt)
9527 0 : warning_at (location, 0, msg, type, rhstype);
9528 : else
9529 460 : error_at (location, msg, type, rhstype);
9530 460 : break;
9531 : }
9532 15 : case ic_return:
9533 15 : {
9534 15 : const char msg[]
9535 : = G_("incompatible types when returning type %qT but %qT was expected");
9536 15 : if (warnopt)
9537 0 : warning_at (location, 0, msg, rhstype, type);
9538 : else
9539 15 : error_at (location, msg, rhstype, type);
9540 15 : break;
9541 : }
9542 0 : default:
9543 0 : gcc_unreachable ();
9544 : }
9545 :
9546 616 : return error_mark_node;
9547 : }
9548 :
9549 : /* If VALUE is a compound expr all of whose expressions are constant, then
9550 : return its value. Otherwise, return error_mark_node.
9551 :
9552 : This is for handling COMPOUND_EXPRs as initializer elements
9553 : which is allowed with a warning when -pedantic is specified. */
9554 :
9555 : static tree
9556 2 : valid_compound_expr_initializer (tree value, tree endtype)
9557 : {
9558 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9559 : {
9560 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9561 1 : == error_mark_node)
9562 : return error_mark_node;
9563 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9564 0 : endtype);
9565 : }
9566 1 : else if (!initializer_constant_valid_p (value, endtype))
9567 1 : return error_mark_node;
9568 : else
9569 : return value;
9570 : }
9571 :
9572 : /* Perform appropriate conversions on the initial value of a variable,
9573 : store it in the declaration DECL,
9574 : and print any error messages that are appropriate.
9575 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9576 : If the init is invalid, store an ERROR_MARK.
9577 :
9578 : INIT_LOC is the location of the initial value. */
9579 :
9580 : void
9581 7365348 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9582 : {
9583 7365348 : tree value, type;
9584 7365348 : bool npc = false;
9585 7365348 : bool int_const_expr = false;
9586 7365348 : bool arith_const_expr = false;
9587 :
9588 : /* If variable's type was invalidly declared, just ignore it. */
9589 :
9590 7365348 : type = TREE_TYPE (decl);
9591 7365348 : if (TREE_CODE (type) == ERROR_MARK)
9592 : return;
9593 :
9594 : /* Digest the specified initializer into an expression. */
9595 :
9596 7365340 : if (init)
9597 : {
9598 7365337 : npc = null_pointer_constant_p (init);
9599 7365337 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9600 430899 : && !TREE_OVERFLOW (init)
9601 7796200 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9602 : /* Not fully determined before folding. */
9603 : arith_const_expr = true;
9604 : }
9605 7365340 : bool constexpr_p = (VAR_P (decl)
9606 7365340 : && C_DECL_DECLARED_CONSTEXPR (decl));
9607 7365340 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9608 : int_const_expr, arith_const_expr, true,
9609 7365340 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9610 :
9611 : /* Store the expression if valid; else report error. */
9612 :
9613 7365340 : if (!in_system_header_at (input_location)
9614 7365340 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9615 29651 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9616 : "aggregate initialization");
9617 :
9618 7365340 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9619 7365339 : DECL_INITIAL (decl) = value;
9620 :
9621 : /* ANSI wants warnings about out-of-range constant initializers. */
9622 7365351 : STRIP_TYPE_NOPS (value);
9623 7365340 : if (TREE_STATIC (decl))
9624 182667 : constant_expression_warning (value);
9625 :
9626 : /* Check if we need to set array size from compound literal size. */
9627 7365340 : if (TREE_CODE (type) == ARRAY_TYPE
9628 27953 : && TYPE_DOMAIN (type) == NULL_TREE
9629 7378549 : && value != error_mark_node)
9630 : {
9631 : tree inside_init = init;
9632 :
9633 12337 : STRIP_TYPE_NOPS (inside_init);
9634 12337 : inside_init = fold (inside_init);
9635 :
9636 12337 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9637 : {
9638 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9639 :
9640 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9641 : {
9642 : /* For int foo[] = (int [3]){1}; we need to set array size
9643 : now since later on array initializer will be just the
9644 : brace enclosed list of the compound literal. */
9645 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9646 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9647 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9648 13 : layout_type (type);
9649 13 : layout_decl (cldecl, 0);
9650 13 : TREE_TYPE (decl)
9651 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9652 : }
9653 : }
9654 : }
9655 : }
9656 :
9657 : /* Methods for storing and printing names for error messages. */
9658 :
9659 : /* Implement a spelling stack that allows components of a name to be pushed
9660 : and popped. Each element on the stack is this structure. */
9661 :
9662 : struct spelling
9663 : {
9664 : int kind;
9665 : union
9666 : {
9667 : unsigned HOST_WIDE_INT i;
9668 : const char *s;
9669 : } u;
9670 : };
9671 :
9672 : #define SPELLING_STRING 1
9673 : #define SPELLING_MEMBER 2
9674 : #define SPELLING_BOUNDS 3
9675 :
9676 : static struct spelling *spelling; /* Next stack element (unused). */
9677 : static struct spelling *spelling_base; /* Spelling stack base. */
9678 : static int spelling_size; /* Size of the spelling stack. */
9679 :
9680 : /* Macros to save and restore the spelling stack around push_... functions.
9681 : Alternative to SAVE_SPELLING_STACK. */
9682 :
9683 : #define SPELLING_DEPTH() (spelling - spelling_base)
9684 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9685 :
9686 : /* Push an element on the spelling stack with type KIND and assign VALUE
9687 : to MEMBER. */
9688 :
9689 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9690 : { \
9691 : int depth = SPELLING_DEPTH (); \
9692 : \
9693 : if (depth >= spelling_size) \
9694 : { \
9695 : spelling_size += 10; \
9696 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9697 : spelling_size); \
9698 : RESTORE_SPELLING_DEPTH (depth); \
9699 : } \
9700 : \
9701 : spelling->kind = (KIND); \
9702 : spelling->MEMBER = (VALUE); \
9703 : spelling++; \
9704 : }
9705 :
9706 : /* Push STRING on the stack. Printed literally. */
9707 :
9708 : static void
9709 7362528 : push_string (const char *string)
9710 : {
9711 7362528 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9712 7362528 : }
9713 :
9714 : /* Push a member name on the stack. Printed as '.' STRING. */
9715 :
9716 : static void
9717 1233255 : push_member_name (tree decl)
9718 : {
9719 1233255 : const char *const string
9720 1233255 : = (DECL_NAME (decl)
9721 1233255 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9722 210 : : _("<anonymous>"));
9723 1233255 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9724 1233255 : }
9725 :
9726 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9727 :
9728 : static void
9729 2618878 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9730 : {
9731 2618878 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9732 2618878 : }
9733 :
9734 : /* Compute the maximum size in bytes of the printed spelling. */
9735 :
9736 : static int
9737 2899 : spelling_length (void)
9738 : {
9739 2899 : int size = 0;
9740 2899 : struct spelling *p;
9741 :
9742 6423 : for (p = spelling_base; p < spelling; p++)
9743 : {
9744 3524 : if (p->kind == SPELLING_BOUNDS)
9745 1479 : size += 25;
9746 : else
9747 2045 : size += strlen (p->u.s) + 1;
9748 : }
9749 :
9750 2899 : return size;
9751 : }
9752 :
9753 : /* Print the spelling to BUFFER and return it. */
9754 :
9755 : static char *
9756 2899 : print_spelling (char *buffer)
9757 : {
9758 2899 : char *d = buffer;
9759 2899 : struct spelling *p;
9760 :
9761 6423 : for (p = spelling_base; p < spelling; p++)
9762 3524 : if (p->kind == SPELLING_BOUNDS)
9763 : {
9764 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9765 1479 : d += strlen (d);
9766 : }
9767 : else
9768 : {
9769 2045 : const char *s;
9770 2045 : if (p->kind == SPELLING_MEMBER)
9771 198 : *d++ = '.';
9772 16715 : for (s = p->u.s; (*d = *s++); d++)
9773 : ;
9774 : }
9775 2899 : *d++ = '\0';
9776 2899 : return buffer;
9777 : }
9778 :
9779 : /* Check whether INIT, a floating or integer constant, is
9780 : representable in TYPE, a real floating type with the same radix or
9781 : a decimal floating type initialized with a binary floating
9782 : constant. Return true if OK, false if not. */
9783 : static bool
9784 362 : constexpr_init_fits_real_type (tree type, tree init)
9785 : {
9786 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9787 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9788 362 : if (TREE_CODE (init) == REAL_CST
9789 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9790 : {
9791 : /* Same mode, no conversion required except for the case of
9792 : signaling NaNs if the types are incompatible (e.g. double and
9793 : long double with the same mode). */
9794 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9795 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9796 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9797 0 : return false;
9798 : return true;
9799 : }
9800 185 : if (TREE_CODE (init) == INTEGER_CST)
9801 : {
9802 54 : tree converted = build_real_from_int_cst (type, init);
9803 54 : bool fail = false;
9804 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9805 54 : TYPE_PRECISION (TREE_TYPE (init)));
9806 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9807 54 : }
9808 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9809 : return false;
9810 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9811 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9812 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9813 44 : && MODE_HAS_NANS (TYPE_MODE (type))))
9814 : return true;
9815 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9816 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9817 : {
9818 : /* This is valid if the real number represented by the
9819 : initializer can be exactly represented in the decimal
9820 : type. Compare the values using MPFR. */
9821 8 : REAL_VALUE_TYPE t;
9822 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9823 8 : mpfr_t bin_val, dec_val;
9824 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9825 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9826 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9827 8 : char string[256];
9828 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9829 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9830 8 : && mpfr_equal_p (bin_val, dec_val));
9831 8 : mpfr_clear (bin_val);
9832 8 : mpfr_clear (dec_val);
9833 8 : return res;
9834 : }
9835 : /* exact_real_truncate is not quite right here, since it doesn't
9836 : allow even an exact conversion to subnormal values. */
9837 86 : REAL_VALUE_TYPE t;
9838 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9839 86 : return real_identical (&t, &TREE_REAL_CST (init));
9840 : }
9841 :
9842 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9843 : initializer for type TYPE, and give an error if not. INIT has
9844 : already been folded and verified to be constant. INT_CONST_EXPR
9845 : and ARITH_CONST_EXPR say whether it is an integer constant
9846 : expression or arithmetic constant expression, respectively. If
9847 : TYPE is not a scalar type, this function does nothing. */
9848 :
9849 : static void
9850 942 : check_constexpr_init (location_t loc, tree type, tree init,
9851 : bool int_const_expr, bool arith_const_expr)
9852 : {
9853 942 : if (POINTER_TYPE_P (type))
9854 : {
9855 : /* The initializer must be null. */
9856 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9857 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9858 : return;
9859 : }
9860 856 : if (INTEGRAL_TYPE_P (type))
9861 : {
9862 : /* The initializer must be an integer constant expression,
9863 : representable in the target type. */
9864 346 : if (!int_const_expr)
9865 : {
9866 13 : if (TREE_CODE (init) == RAW_DATA_CST
9867 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9868 : {
9869 4 : if (!TYPE_UNSIGNED (type))
9870 137 : for (unsigned int i = 0;
9871 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9872 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9873 : {
9874 1 : error_at (loc, "%<constexpr%> initializer not "
9875 : "representable in type of object");
9876 1 : break;
9877 : }
9878 : }
9879 : else
9880 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9881 : "integer constant expression");
9882 : }
9883 333 : else if (!int_fits_type_p (init, type))
9884 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9885 : "type of object");
9886 : return;
9887 : }
9888 : /* We don't apply any extra checks to extension types such as vector
9889 : or fixed-point types. */
9890 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9891 : return;
9892 353 : if (!arith_const_expr)
9893 : {
9894 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9895 : "constant expression");
9896 5 : return;
9897 : }
9898 : /* We don't apply any extra checks to complex integers. */
9899 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9900 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9901 : return;
9902 : /* Following N3082, a real type cannot be initialized from a complex
9903 : type and a binary type cannot be initialized from a decimal type
9904 : (but initializing a decimal type from a binary type is OK).
9905 : Signaling NaN initializers are OK only if the types are
9906 : compatible (not just the same mode); all quiet NaN and infinity
9907 : initializations are considered to preserve the value. */
9908 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9909 348 : && SCALAR_FLOAT_TYPE_P (type))
9910 : {
9911 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9912 : "complex type");
9913 6 : return;
9914 : }
9915 342 : if (SCALAR_FLOAT_TYPE_P (type)
9916 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9917 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9918 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9919 : {
9920 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9921 : "floating-point type is of decimal type");
9922 6 : return;
9923 : }
9924 336 : bool fits;
9925 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9926 : {
9927 42 : switch (TREE_CODE (init))
9928 : {
9929 10 : case INTEGER_CST:
9930 10 : case REAL_CST:
9931 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9932 10 : break;
9933 32 : case COMPLEX_CST:
9934 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9935 32 : TREE_REALPART (init))
9936 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9937 26 : TREE_IMAGPART (init)));
9938 : break;
9939 0 : default:
9940 0 : gcc_unreachable ();
9941 : }
9942 : }
9943 : else
9944 294 : fits = constexpr_init_fits_real_type (type, init);
9945 304 : if (!fits)
9946 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9947 : "type of object");
9948 : }
9949 :
9950 : /* Digest the parser output INIT as an initializer for type TYPE
9951 : initializing DECL.
9952 : Return a C expression of type TYPE to represent the initial value.
9953 :
9954 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9955 :
9956 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9957 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9958 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9959 : constant expression, false if it has already been determined in the
9960 : caller that it is not (but folding may have made the value passed here
9961 : indistinguishable from an arithmetic constant expression).
9962 :
9963 : If INIT is a string constant, STRICT_STRING is true if it is
9964 : unparenthesized or we should not warn here for it being parenthesized.
9965 : For other types of INIT, STRICT_STRING is not used.
9966 :
9967 : INIT_LOC is the location of the INIT.
9968 :
9969 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9970 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9971 : on initializers for 'constexpr' objects apply. */
9972 :
9973 : static tree
9974 17089386 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9975 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9976 : bool arith_const_expr, bool strict_string,
9977 : bool require_constant, bool require_constexpr)
9978 : {
9979 17089386 : enum tree_code code = TREE_CODE (type);
9980 17089386 : tree inside_init = init;
9981 17089386 : tree semantic_type = NULL_TREE;
9982 17089386 : bool maybe_const = true;
9983 :
9984 17089386 : if (type == error_mark_node
9985 17089386 : || !init
9986 34178769 : || error_operand_p (init))
9987 : return error_mark_node;
9988 :
9989 17096875 : STRIP_TYPE_NOPS (inside_init);
9990 :
9991 : /* If require_constant is TRUE, when the initializer is a call to
9992 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9993 : For example:
9994 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9995 : will be converted to
9996 : y = &static_annotated.c. */
9997 :
9998 17088218 : if (require_constant
9999 2909949 : && TREE_CODE (inside_init) == NOP_EXPR
10000 736281 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
10001 17088220 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
10002 2 : inside_init
10003 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
10004 :
10005 17088218 : if (!c_in_omp_for)
10006 : {
10007 17082749 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
10008 : {
10009 458 : semantic_type = TREE_TYPE (inside_init);
10010 458 : inside_init = TREE_OPERAND (inside_init, 0);
10011 : }
10012 17082749 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
10013 : }
10014 : /* TODO: this may not detect all cases of expressions folding to
10015 : constants that are not arithmetic constant expressions. */
10016 17088218 : if (!maybe_const)
10017 : arith_const_expr = false;
10018 25500011 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
10019 5704142 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
10020 17004581 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
10021 : arith_const_expr = false;
10022 8524423 : else if (TREE_CODE (inside_init) != INTEGER_CST
10023 : && TREE_CODE (inside_init) != REAL_CST
10024 : && TREE_CODE (inside_init) != COMPLEX_CST
10025 : && TREE_CODE (inside_init) != RAW_DATA_CST)
10026 : arith_const_expr = false;
10027 5143341 : else if (TREE_OVERFLOW (inside_init))
10028 11944921 : arith_const_expr = false;
10029 :
10030 : /* Initialization of an array of chars from a string constant
10031 : optionally enclosed in braces. */
10032 :
10033 17088218 : if (code == ARRAY_TYPE && inside_init
10034 191073 : && TREE_CODE (inside_init) == STRING_CST)
10035 : {
10036 11832 : tree typ1
10037 11832 : = (TYPE_ATOMIC (TREE_TYPE (type))
10038 11832 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
10039 : TYPE_QUAL_ATOMIC)
10040 11818 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
10041 : /* Note that an array could be both an array of character type
10042 : and an array of wchar_t if wchar_t is signed char or unsigned
10043 : char. */
10044 23664 : bool char_array = (typ1 == char_type_node
10045 520 : || typ1 == signed_char_type_node
10046 12317 : || typ1 == unsigned_char_type_node);
10047 11832 : bool wchar_array = comptypes (typ1, wchar_type_node);
10048 11832 : bool char16_array = comptypes (typ1, char16_type_node);
10049 11832 : bool char32_array = comptypes (typ1, char32_type_node);
10050 :
10051 11832 : if (char_array || wchar_array || char16_array || char32_array)
10052 : {
10053 11803 : struct c_expr expr;
10054 11803 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
10055 11803 : bool incompat_string_cst = false;
10056 11803 : expr.value = inside_init;
10057 11803 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
10058 11803 : expr.original_type = NULL;
10059 11803 : expr.m_decimal = 0;
10060 11803 : maybe_warn_string_init (init_loc, type, expr);
10061 :
10062 11803 : if (flexible_array_member_type_p (type))
10063 63 : pedwarn_init (init_loc, OPT_Wpedantic,
10064 : "initialization of a flexible array member");
10065 :
10066 11803 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10067 11803 : TYPE_MAIN_VARIANT (type)))
10068 : return inside_init;
10069 :
10070 4198 : if (char_array)
10071 : {
10072 4087 : if (typ2 != char_type_node && typ2 != char8_type_node)
10073 : incompat_string_cst = true;
10074 : }
10075 111 : else if (!comptypes (typ1, typ2))
10076 : incompat_string_cst = true;
10077 :
10078 : if (incompat_string_cst)
10079 : {
10080 72 : error_init (init_loc, "cannot initialize array of %qT from "
10081 : "a string literal with type array of %qT",
10082 : typ1, typ2);
10083 72 : return error_mark_node;
10084 : }
10085 :
10086 4126 : if (require_constexpr
10087 4126 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
10088 : {
10089 : /* Check if all characters of the string can be
10090 : represented in the type of the constexpr object being
10091 : initialized. */
10092 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10093 24 : const unsigned char *p =
10094 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
10095 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
10096 70 : for (unsigned i = 0; i < len; i++)
10097 58 : if (p[i] > 127)
10098 : {
10099 12 : error_init (init_loc, "%<constexpr%> initializer not "
10100 : "representable in type of object");
10101 12 : break;
10102 : }
10103 : }
10104 :
10105 4126 : if (TYPE_DOMAIN (type) != NULL_TREE
10106 4030 : && TYPE_SIZE (type) != NULL_TREE
10107 8095 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10108 : {
10109 3969 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10110 :
10111 3969 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
10112 : {
10113 393 : unsigned HOST_WIDE_INT avail
10114 393 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10115 393 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
10116 393 : const char *p = TREE_STRING_POINTER (inside_init);
10117 :
10118 : /* Construct truncated string. */
10119 393 : inside_init = build_string (avail, p);
10120 :
10121 : /* Subtract the size of a single (possibly wide) character
10122 : because it may be ok to ignore the terminating NUL char
10123 : that is counted in the length of the constant. */
10124 393 : if (len - unit > avail)
10125 63 : pedwarn_init (init_loc, 0,
10126 : "initializer-string for array of %qT "
10127 : "is too long (%wu chars into %wu "
10128 : "available)", typ1, len, avail);
10129 330 : else if (warn_cxx_compat)
10130 9 : warning_at (init_loc, OPT_Wc___compat,
10131 : "initializer-string for array of %qT "
10132 : "is too long for C++ (%wu chars into %wu "
10133 : "available)", typ1, len, avail);
10134 321 : else if (warn_unterminated_string_initialization
10135 321 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10136 22 : warning_at (init_loc,
10137 22 : OPT_Wunterminated_string_initialization,
10138 : "initializer-string for array of %qT "
10139 : "truncates NUL terminator but destination "
10140 : "lacks %qs attribute (%wu chars into %wu "
10141 : "available)", typ1, "nonstring", len, avail);
10142 : }
10143 : }
10144 :
10145 4126 : TREE_TYPE (inside_init) = type;
10146 4126 : return inside_init;
10147 : }
10148 29 : else if (INTEGRAL_TYPE_P (typ1))
10149 : {
10150 29 : error_init (init_loc, "array of inappropriate type initialized "
10151 : "from string constant");
10152 29 : return error_mark_node;
10153 : }
10154 : }
10155 :
10156 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10157 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10158 : below and handle as a constructor. */
10159 17076386 : if (code == VECTOR_TYPE
10160 6380855 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10161 6380853 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10162 23457201 : && TREE_CONSTANT (inside_init))
10163 : {
10164 640087 : if (TREE_CODE (inside_init) == VECTOR_CST
10165 640184 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10166 97 : TYPE_MAIN_VARIANT (type)))
10167 : return inside_init;
10168 :
10169 639990 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10170 : {
10171 : unsigned HOST_WIDE_INT ix;
10172 : tree value;
10173 4217260 : bool constant_p = true;
10174 :
10175 : /* Iterate through elements and check if all constructor
10176 : elements are *_CSTs. */
10177 4217260 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10178 3577327 : if (!CONSTANT_CLASS_P (value))
10179 : {
10180 : constant_p = false;
10181 : break;
10182 : }
10183 :
10184 639990 : if (constant_p)
10185 639933 : return build_vector_from_ctor (type,
10186 639933 : CONSTRUCTOR_ELTS (inside_init));
10187 : }
10188 : }
10189 :
10190 16436356 : if (warn_sequence_point)
10191 2452599 : verify_sequence_points (inside_init);
10192 :
10193 : /* Any type can be initialized
10194 : from an expression of the same type, optionally with braces. */
10195 :
10196 16436356 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10197 32872712 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10198 16436356 : TYPE_MAIN_VARIANT (type))
10199 3171754 : || (code == ARRAY_TYPE
10200 477 : && comptypes (TREE_TYPE (inside_init), type))
10201 3171754 : || (gnu_vector_type_p (type)
10202 193 : && comptypes (TREE_TYPE (inside_init), type))
10203 3171754 : || (code == POINTER_TYPE
10204 112367 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10205 9302 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10206 9302 : TREE_TYPE (type)))))
10207 : {
10208 13266588 : if (code == POINTER_TYPE)
10209 : {
10210 787123 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10211 : {
10212 1986 : if (TREE_CODE (inside_init) == STRING_CST
10213 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10214 1983 : inside_init = array_to_pointer_conversion
10215 1983 : (init_loc, inside_init);
10216 : else
10217 : {
10218 3 : error_init (init_loc, "invalid use of non-lvalue array");
10219 3 : return error_mark_node;
10220 : }
10221 : }
10222 : }
10223 :
10224 13266585 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10225 : /* Although the types are compatible, we may require a
10226 : conversion. */
10227 5740765 : inside_init = convert (type, inside_init);
10228 :
10229 13266585 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10230 13266585 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10231 : {
10232 0 : error_init (init_loc, "invalid initializer");
10233 0 : return error_mark_node;
10234 : }
10235 :
10236 13266585 : if (require_constant
10237 2308315 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10238 : {
10239 : /* As an extension, allow initializing objects with static storage
10240 : duration with compound literals (which are then treated just as
10241 : the brace enclosed list they contain). Also allow this for
10242 : vectors, as we can only assign them with compound literals. */
10243 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10244 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10245 : "is not constant");
10246 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10247 31 : inside_init = DECL_INITIAL (decl);
10248 : }
10249 :
10250 13266585 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10251 178764 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10252 : {
10253 2 : error_init (init_loc, "array initialized from non-constant array "
10254 : "expression");
10255 2 : return error_mark_node;
10256 : }
10257 :
10258 : /* Compound expressions can only occur here if -Wpedantic or
10259 : -pedantic-errors is specified. In the later case, we always want
10260 : an error. In the former case, we simply want a warning. */
10261 13266583 : if (require_constant && pedantic
10262 20366 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10263 : {
10264 1 : inside_init
10265 1 : = valid_compound_expr_initializer (inside_init,
10266 1 : TREE_TYPE (inside_init));
10267 1 : if (inside_init == error_mark_node)
10268 1 : error_init (init_loc, "initializer element is not constant");
10269 : else
10270 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10271 : "initializer element is not constant");
10272 1 : if (flag_pedantic_errors)
10273 0 : inside_init = error_mark_node;
10274 : }
10275 2308314 : else if (require_constant
10276 2308314 : && !initializer_constant_valid_p (inside_init,
10277 2308314 : TREE_TYPE (inside_init)))
10278 : {
10279 101 : error_init (init_loc, "initializer element is not constant");
10280 101 : inside_init = error_mark_node;
10281 : }
10282 13266481 : else if (require_constant && !maybe_const)
10283 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10284 : "initializer element is not a constant expression");
10285 13266262 : else if (require_constexpr)
10286 610 : check_constexpr_init (init_loc, type, inside_init,
10287 : int_const_expr, arith_const_expr);
10288 :
10289 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10290 13266583 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10291 858045 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10292 : type, inside_init, origtype,
10293 : (require_constant
10294 : ? ic_init_const
10295 : : ic_init), null_pointer_constant,
10296 : NULL_TREE, NULL_TREE, 0);
10297 13266583 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10298 260 : && c_inhibit_evaluation_warnings == 0
10299 260 : && warn_conversion
10300 1 : && !TYPE_UNSIGNED (type)
10301 13266584 : && TYPE_PRECISION (type) == CHAR_BIT)
10302 303 : for (unsigned int i = 0;
10303 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10304 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10305 10 : warning_at (init_loc, OPT_Wconversion,
10306 : "conversion from %qT to %qT changes value from "
10307 : "%qd to %qd",
10308 : integer_type_node, type,
10309 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10310 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10311 : return inside_init;
10312 : }
10313 :
10314 : /* Handle scalar types, including conversions. */
10315 :
10316 3169768 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10317 130452 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10318 12344 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10319 10254 : || code == BITINT_TYPE)
10320 : {
10321 3169280 : tree unconverted_init = inside_init;
10322 3169280 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10323 3169280 : && (TREE_CODE (init) == STRING_CST
10324 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10325 7317 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10326 3169280 : if (semantic_type)
10327 453 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10328 : inside_init);
10329 3169280 : inside_init
10330 5751794 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10331 : inside_init, origtype,
10332 : require_constant ? ic_init_const : ic_init,
10333 : null_pointer_constant, NULL_TREE, NULL_TREE,
10334 : 0);
10335 :
10336 : /* Check to see if we have already given an error message. */
10337 3169280 : if (inside_init == error_mark_node)
10338 : ;
10339 3168820 : else if (require_constant && !TREE_CONSTANT (inside_init))
10340 : {
10341 36 : error_init (init_loc, "initializer element is not constant");
10342 36 : inside_init = error_mark_node;
10343 : }
10344 3168784 : else if (require_constant
10345 3755122 : && !initializer_constant_valid_p (inside_init,
10346 586338 : TREE_TYPE (inside_init)))
10347 : {
10348 10 : error_init (init_loc, "initializer element is not computable at "
10349 : "load time");
10350 10 : inside_init = error_mark_node;
10351 : }
10352 3168774 : else if (require_constant && !maybe_const)
10353 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10354 : "initializer element is not a constant expression");
10355 3168769 : else if (require_constexpr)
10356 332 : check_constexpr_init (init_loc, type, unconverted_init,
10357 : int_const_expr, arith_const_expr);
10358 :
10359 : return inside_init;
10360 : }
10361 :
10362 : /* Come here only for records and arrays. */
10363 :
10364 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10365 : {
10366 0 : error_init (init_loc,
10367 : "variable-sized object may not be initialized except "
10368 : "with an empty initializer");
10369 0 : return error_mark_node;
10370 : }
10371 :
10372 488 : error_init (init_loc, "invalid initializer");
10373 488 : return error_mark_node;
10374 : }
10375 :
10376 : /* Handle initializers that use braces. */
10377 :
10378 : /* Type of object we are accumulating a constructor for.
10379 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10380 : static tree constructor_type;
10381 :
10382 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10383 : left to fill. */
10384 : static tree constructor_fields;
10385 :
10386 : /* For an ARRAY_TYPE, this is the specified index
10387 : at which to store the next element we get. */
10388 : static tree constructor_index;
10389 :
10390 : /* For an ARRAY_TYPE, this is the maximum index. */
10391 : static tree constructor_max_index;
10392 :
10393 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10394 : static tree constructor_unfilled_fields;
10395 :
10396 : /* For an ARRAY_TYPE, this is the index of the first element
10397 : not yet written out. */
10398 : static tree constructor_unfilled_index;
10399 :
10400 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10401 : This is so we can generate gaps between fields, when appropriate. */
10402 : static tree constructor_bit_index;
10403 :
10404 : /* If we are saving up the elements rather than allocating them,
10405 : this is the list of elements so far (in reverse order,
10406 : most recent first). */
10407 : static vec<constructor_elt, va_gc> *constructor_elements;
10408 :
10409 : /* 1 if constructor should be incrementally stored into a constructor chain,
10410 : 0 if all the elements should be kept in AVL tree. */
10411 : static int constructor_incremental;
10412 :
10413 : /* 1 if so far this constructor's elements are all compile-time constants. */
10414 : static int constructor_constant;
10415 :
10416 : /* 1 if so far this constructor's elements are all valid address constants. */
10417 : static int constructor_simple;
10418 :
10419 : /* 1 if this constructor has an element that cannot be part of a
10420 : constant expression. */
10421 : static int constructor_nonconst;
10422 :
10423 : /* 1 if this constructor is erroneous so far. */
10424 : static int constructor_erroneous;
10425 :
10426 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10427 : static int constructor_zeroinit;
10428 :
10429 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10430 : static bool constructor_zero_padding_bits;
10431 :
10432 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10433 : of braces are an error). */
10434 : static bool constructor_braced_scalar;
10435 :
10436 : /* Structure for managing pending initializer elements, organized as an
10437 : AVL tree. */
10438 :
10439 : struct init_node
10440 : {
10441 : struct init_node *left, *right;
10442 : struct init_node *parent;
10443 : int balance;
10444 : tree purpose;
10445 : tree value;
10446 : tree origtype;
10447 : };
10448 :
10449 : /* Tree of pending elements at this constructor level.
10450 : These are elements encountered out of order
10451 : which belong at places we haven't reached yet in actually
10452 : writing the output.
10453 : Will never hold tree nodes across GC runs. */
10454 : static struct init_node *constructor_pending_elts;
10455 :
10456 : /* The SPELLING_DEPTH of this constructor. */
10457 : static int constructor_depth;
10458 :
10459 : /* DECL node for which an initializer is being read.
10460 : 0 means we are reading a constructor expression
10461 : such as (struct foo) {...}. */
10462 : static tree constructor_decl;
10463 :
10464 : /* Nonzero if there were any member designators in this initializer. */
10465 : static int constructor_designated;
10466 :
10467 : /* Nesting depth of designator list. */
10468 : static int designator_depth;
10469 :
10470 : /* Nonzero if there were diagnosed errors in this designator list. */
10471 : static int designator_erroneous;
10472 :
10473 :
10474 : /* This stack has a level for each implicit or explicit level of
10475 : structuring in the initializer, including the outermost one. It
10476 : saves the values of most of the variables above. */
10477 :
10478 : struct constructor_range_stack;
10479 :
10480 : struct constructor_stack
10481 : {
10482 : struct constructor_stack *next;
10483 : tree type;
10484 : tree fields;
10485 : tree index;
10486 : tree max_index;
10487 : tree unfilled_index;
10488 : tree unfilled_fields;
10489 : tree bit_index;
10490 : vec<constructor_elt, va_gc> *elements;
10491 : struct init_node *pending_elts;
10492 : int offset;
10493 : int depth;
10494 : /* If value nonzero, this value should replace the entire
10495 : constructor at this level. */
10496 : struct c_expr replacement_value;
10497 : struct constructor_range_stack *range_stack;
10498 : char constant;
10499 : char simple;
10500 : char nonconst;
10501 : char implicit;
10502 : char erroneous;
10503 : char outer;
10504 : char incremental;
10505 : char designated;
10506 : bool zero_padding_bits;
10507 : bool braced_scalar;
10508 : int designator_depth;
10509 : };
10510 :
10511 : static struct constructor_stack *constructor_stack;
10512 :
10513 : /* This stack represents designators from some range designator up to
10514 : the last designator in the list. */
10515 :
10516 : struct constructor_range_stack
10517 : {
10518 : struct constructor_range_stack *next, *prev;
10519 : struct constructor_stack *stack;
10520 : tree range_start;
10521 : tree index;
10522 : tree range_end;
10523 : tree fields;
10524 : };
10525 :
10526 : static struct constructor_range_stack *constructor_range_stack;
10527 :
10528 : /* This stack records separate initializers that are nested.
10529 : Nested initializers can't happen in ANSI C, but GNU C allows them
10530 : in cases like { ... (struct foo) { ... } ... }. */
10531 :
10532 : struct initializer_stack
10533 : {
10534 : struct initializer_stack *next;
10535 : tree decl;
10536 : struct constructor_stack *constructor_stack;
10537 : struct constructor_range_stack *constructor_range_stack;
10538 : vec<constructor_elt, va_gc> *elements;
10539 : struct spelling *spelling;
10540 : struct spelling *spelling_base;
10541 : int spelling_size;
10542 : char require_constant_value;
10543 : char require_constant_elements;
10544 : char require_constexpr_value;
10545 : char designated;
10546 : rich_location *missing_brace_richloc;
10547 : };
10548 :
10549 : static struct initializer_stack *initializer_stack;
10550 :
10551 : /* Prepare to parse and output the initializer for variable DECL. */
10552 :
10553 : void
10554 7362528 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10555 : bool init_require_constant, bool init_require_constexpr,
10556 : rich_location *richloc)
10557 : {
10558 7362528 : const char *locus;
10559 7362528 : struct initializer_stack *p = XNEW (struct initializer_stack);
10560 :
10561 7362528 : p->decl = constructor_decl;
10562 7362528 : p->require_constant_value = require_constant_value;
10563 7362528 : p->require_constant_elements = require_constant_elements;
10564 7362528 : p->require_constexpr_value = require_constexpr_value;
10565 7362528 : p->constructor_stack = constructor_stack;
10566 7362528 : p->constructor_range_stack = constructor_range_stack;
10567 7362528 : p->elements = constructor_elements;
10568 7362528 : p->spelling = spelling;
10569 7362528 : p->spelling_base = spelling_base;
10570 7362528 : p->spelling_size = spelling_size;
10571 7362528 : p->next = initializer_stack;
10572 7362528 : p->missing_brace_richloc = richloc;
10573 7362528 : p->designated = constructor_designated;
10574 7362528 : initializer_stack = p;
10575 :
10576 7362528 : constructor_decl = decl;
10577 7362528 : constructor_designated = 0;
10578 :
10579 7362528 : require_constant_value = init_require_constant;
10580 7362528 : require_constexpr_value = init_require_constexpr;
10581 7362528 : if (decl != NULL_TREE && decl != error_mark_node)
10582 : {
10583 6430260 : require_constant_elements
10584 6250809 : = ((init_require_constant || (pedantic && !flag_isoc99))
10585 : /* For a scalar, you can always use any value to initialize,
10586 : even within braces. */
10587 6443400 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10588 6430260 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10589 : }
10590 : else
10591 : {
10592 932268 : require_constant_elements = false;
10593 932268 : locus = _("(anonymous)");
10594 : }
10595 :
10596 7362528 : constructor_stack = 0;
10597 7362528 : constructor_range_stack = 0;
10598 :
10599 7362528 : found_missing_braces = 0;
10600 :
10601 7362528 : spelling_base = 0;
10602 7362528 : spelling_size = 0;
10603 7362528 : RESTORE_SPELLING_DEPTH (0);
10604 :
10605 7362528 : if (locus)
10606 7362528 : push_string (locus);
10607 7362528 : }
10608 :
10609 : void
10610 7362526 : finish_init (void)
10611 : {
10612 7362526 : struct initializer_stack *p = initializer_stack;
10613 :
10614 : /* Free the whole constructor stack of this initializer. */
10615 7362526 : while (constructor_stack)
10616 : {
10617 0 : struct constructor_stack *q = constructor_stack;
10618 0 : constructor_stack = q->next;
10619 0 : XDELETE (q);
10620 : }
10621 :
10622 7362526 : gcc_assert (!constructor_range_stack);
10623 :
10624 : /* Pop back to the data of the outer initializer (if any). */
10625 7362526 : XDELETE (spelling_base);
10626 :
10627 7362526 : constructor_decl = p->decl;
10628 7362526 : require_constant_value = p->require_constant_value;
10629 7362526 : require_constant_elements = p->require_constant_elements;
10630 7362526 : require_constexpr_value = p->require_constexpr_value;
10631 7362526 : constructor_stack = p->constructor_stack;
10632 7362526 : constructor_designated = p->designated;
10633 7362526 : constructor_range_stack = p->constructor_range_stack;
10634 7362526 : constructor_elements = p->elements;
10635 7362526 : spelling = p->spelling;
10636 7362526 : spelling_base = p->spelling_base;
10637 7362526 : spelling_size = p->spelling_size;
10638 7362526 : initializer_stack = p->next;
10639 7362526 : XDELETE (p);
10640 7362526 : }
10641 :
10642 : /* Call here when we see the initializer is surrounded by braces.
10643 : This is instead of a call to push_init_level;
10644 : it is matched by a call to pop_init_level.
10645 :
10646 : TYPE is the type to initialize, for a constructor expression.
10647 : For an initializer for a decl, TYPE is zero. */
10648 :
10649 : void
10650 1038556 : really_start_incremental_init (tree type)
10651 : {
10652 1038556 : struct constructor_stack *p = XNEW (struct constructor_stack);
10653 :
10654 1038556 : if (type == NULL_TREE)
10655 108261 : type = TREE_TYPE (constructor_decl);
10656 :
10657 1038556 : if (VECTOR_TYPE_P (type)
10658 1038556 : && TYPE_VECTOR_OPAQUE (type))
10659 0 : error ("opaque vector types cannot be initialized");
10660 :
10661 1038556 : p->type = constructor_type;
10662 1038556 : p->fields = constructor_fields;
10663 1038556 : p->index = constructor_index;
10664 1038556 : p->max_index = constructor_max_index;
10665 1038556 : p->unfilled_index = constructor_unfilled_index;
10666 1038556 : p->unfilled_fields = constructor_unfilled_fields;
10667 1038556 : p->bit_index = constructor_bit_index;
10668 1038556 : p->elements = constructor_elements;
10669 1038556 : p->constant = constructor_constant;
10670 1038556 : p->simple = constructor_simple;
10671 1038556 : p->nonconst = constructor_nonconst;
10672 1038556 : p->erroneous = constructor_erroneous;
10673 1038556 : p->pending_elts = constructor_pending_elts;
10674 1038556 : p->depth = constructor_depth;
10675 1038556 : p->replacement_value.value = 0;
10676 1038556 : p->replacement_value.original_code = ERROR_MARK;
10677 1038556 : p->replacement_value.original_type = NULL;
10678 1038556 : p->implicit = 0;
10679 1038556 : p->range_stack = 0;
10680 1038556 : p->outer = 0;
10681 1038556 : p->incremental = constructor_incremental;
10682 1038556 : p->designated = constructor_designated;
10683 1038556 : p->zero_padding_bits = constructor_zero_padding_bits;
10684 1038556 : p->braced_scalar = constructor_braced_scalar;
10685 1038556 : p->designator_depth = designator_depth;
10686 1038556 : p->next = 0;
10687 1038556 : constructor_stack = p;
10688 :
10689 1038556 : constructor_constant = 1;
10690 1038556 : constructor_simple = 1;
10691 1038556 : constructor_nonconst = 0;
10692 1038556 : constructor_depth = SPELLING_DEPTH ();
10693 1038556 : constructor_elements = NULL;
10694 1038556 : constructor_pending_elts = 0;
10695 1038556 : constructor_type = type;
10696 1038556 : constructor_incremental = 1;
10697 1038556 : constructor_designated = 0;
10698 1038556 : constructor_zero_padding_bits = false;
10699 1038556 : constructor_zeroinit = 1;
10700 1038556 : constructor_braced_scalar = false;
10701 1038556 : designator_depth = 0;
10702 1038556 : designator_erroneous = 0;
10703 :
10704 1038556 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10705 : {
10706 82796 : constructor_fields = TYPE_FIELDS (constructor_type);
10707 : /* Skip any nameless bit fields at the beginning. */
10708 82796 : while (constructor_fields != NULL_TREE
10709 82825 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10710 29 : constructor_fields = DECL_CHAIN (constructor_fields);
10711 :
10712 82796 : constructor_unfilled_fields = constructor_fields;
10713 82796 : constructor_bit_index = bitsize_zero_node;
10714 : }
10715 955760 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10716 : {
10717 19371 : if (TYPE_DOMAIN (constructor_type))
10718 : {
10719 10254 : constructor_max_index
10720 10254 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10721 :
10722 : /* Detect non-empty initializations of zero-length arrays. */
10723 10254 : if (constructor_max_index == NULL_TREE
10724 10254 : && TYPE_SIZE (constructor_type))
10725 210 : constructor_max_index = integer_minus_one_node;
10726 :
10727 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10728 : to initialize VLAs with a nonempty initializer will cause a
10729 : proper error; avoid tree checking errors as well by setting a
10730 : safe value. */
10731 10254 : if (constructor_max_index
10732 10254 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10733 73 : constructor_max_index = integer_minus_one_node;
10734 :
10735 10254 : constructor_index
10736 10254 : = convert (bitsizetype,
10737 10254 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10738 : }
10739 : else
10740 : {
10741 9117 : constructor_index = bitsize_zero_node;
10742 9117 : constructor_max_index = NULL_TREE;
10743 : }
10744 :
10745 19371 : constructor_unfilled_index = constructor_index;
10746 : }
10747 936389 : else if (gnu_vector_type_p (constructor_type))
10748 : {
10749 : /* Vectors are like simple fixed-size arrays. */
10750 1871666 : constructor_max_index =
10751 935833 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10752 935833 : constructor_index = bitsize_zero_node;
10753 935833 : constructor_unfilled_index = constructor_index;
10754 : }
10755 : else
10756 : {
10757 : /* Handle the case of int x = {5}; */
10758 556 : constructor_fields = constructor_type;
10759 556 : constructor_unfilled_fields = constructor_type;
10760 556 : constructor_braced_scalar = true;
10761 : }
10762 1038556 : }
10763 :
10764 : extern location_t last_init_list_comma;
10765 :
10766 : /* Called when we see an open brace for a nested initializer. Finish
10767 : off any pending levels with implicit braces. */
10768 : void
10769 342972 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10770 : {
10771 342974 : while (constructor_stack->implicit)
10772 : {
10773 1158 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10774 208 : && constructor_fields == NULL_TREE)
10775 0 : process_init_element (input_location,
10776 : pop_init_level (loc, 1, braced_init_obstack,
10777 : last_init_list_comma),
10778 : true, braced_init_obstack);
10779 1158 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10780 950 : && constructor_max_index
10781 2108 : && tree_int_cst_lt (constructor_max_index,
10782 : constructor_index))
10783 2 : process_init_element (input_location,
10784 : pop_init_level (loc, 1, braced_init_obstack,
10785 : last_init_list_comma),
10786 : true, braced_init_obstack);
10787 : else
10788 : break;
10789 : }
10790 342972 : }
10791 :
10792 : /* Push down into a subobject, for initialization.
10793 : If this is for an explicit set of braces, IMPLICIT is 0.
10794 : If it is because the next element belongs at a lower level,
10795 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10796 :
10797 : void
10798 1045652 : push_init_level (location_t loc, int implicit,
10799 : struct obstack *braced_init_obstack)
10800 : {
10801 1045652 : struct constructor_stack *p;
10802 1045652 : tree value = NULL_TREE;
10803 :
10804 : /* Unless this is an explicit brace, we need to preserve previous
10805 : content if any. */
10806 1045652 : if (implicit)
10807 : {
10808 705331 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10809 1823 : value = find_init_member (constructor_fields, braced_init_obstack);
10810 703508 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10811 703508 : value = find_init_member (constructor_index, braced_init_obstack);
10812 : }
10813 :
10814 1045652 : p = XNEW (struct constructor_stack);
10815 1045652 : p->type = constructor_type;
10816 1045652 : p->fields = constructor_fields;
10817 1045652 : p->index = constructor_index;
10818 1045652 : p->max_index = constructor_max_index;
10819 1045652 : p->unfilled_index = constructor_unfilled_index;
10820 1045652 : p->unfilled_fields = constructor_unfilled_fields;
10821 1045652 : p->bit_index = constructor_bit_index;
10822 1045652 : p->elements = constructor_elements;
10823 1045652 : p->constant = constructor_constant;
10824 1045652 : p->simple = constructor_simple;
10825 1045652 : p->nonconst = constructor_nonconst;
10826 1045652 : p->erroneous = constructor_erroneous;
10827 1045652 : p->pending_elts = constructor_pending_elts;
10828 1045652 : p->depth = constructor_depth;
10829 1045652 : p->replacement_value.value = NULL_TREE;
10830 1045652 : p->replacement_value.original_code = ERROR_MARK;
10831 1045652 : p->replacement_value.original_type = NULL;
10832 1045652 : p->implicit = implicit;
10833 1045652 : p->outer = 0;
10834 1045652 : p->incremental = constructor_incremental;
10835 1045652 : p->designated = constructor_designated;
10836 1045652 : p->zero_padding_bits = constructor_zero_padding_bits;
10837 1045652 : p->braced_scalar = constructor_braced_scalar;
10838 1045652 : p->designator_depth = designator_depth;
10839 1045652 : p->next = constructor_stack;
10840 1045652 : p->range_stack = 0;
10841 1045652 : constructor_stack = p;
10842 :
10843 1045652 : constructor_constant = 1;
10844 1045652 : constructor_simple = 1;
10845 1045652 : constructor_nonconst = 0;
10846 1045652 : constructor_depth = SPELLING_DEPTH ();
10847 1045652 : constructor_elements = NULL;
10848 1045652 : constructor_incremental = 1;
10849 : /* If the upper initializer is designated, then mark this as
10850 : designated too to prevent bogus warnings. */
10851 1045652 : constructor_designated = p->designated;
10852 : /* If the upper initializer has padding bits zeroed, that includes
10853 : all nested initializers as well. */
10854 1045652 : constructor_zero_padding_bits = p->zero_padding_bits;
10855 1045652 : constructor_braced_scalar = false;
10856 1045652 : constructor_pending_elts = 0;
10857 1045652 : if (!implicit)
10858 : {
10859 340321 : p->range_stack = constructor_range_stack;
10860 340321 : constructor_range_stack = 0;
10861 340321 : designator_depth = 0;
10862 340321 : designator_erroneous = 0;
10863 : }
10864 :
10865 : /* Don't die if an entire brace-pair level is superfluous
10866 : in the containing level. */
10867 1045652 : if (constructor_type == NULL_TREE)
10868 : ;
10869 1045652 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10870 : {
10871 : /* Don't die if there are extra init elts at the end. */
10872 160185 : if (constructor_fields == NULL_TREE)
10873 51 : constructor_type = NULL_TREE;
10874 : else
10875 : {
10876 160134 : constructor_type = TREE_TYPE (constructor_fields);
10877 160134 : push_member_name (constructor_fields);
10878 160134 : constructor_depth++;
10879 : }
10880 : }
10881 885467 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10882 : {
10883 885416 : constructor_type = TREE_TYPE (constructor_type);
10884 885416 : push_array_bounds (tree_to_uhwi (constructor_index));
10885 885416 : constructor_depth++;
10886 : }
10887 :
10888 1045652 : if (constructor_type == NULL_TREE)
10889 : {
10890 51 : error_init (loc, "extra brace group at end of initializer");
10891 51 : constructor_fields = NULL_TREE;
10892 51 : constructor_unfilled_fields = NULL_TREE;
10893 51 : return;
10894 : }
10895 :
10896 1045601 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10897 : {
10898 1686 : constructor_constant = TREE_CONSTANT (value);
10899 1686 : constructor_simple = TREE_STATIC (value);
10900 1686 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10901 1686 : constructor_elements = CONSTRUCTOR_ELTS (value);
10902 1686 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10903 1686 : if (!vec_safe_is_empty (constructor_elements)
10904 1462 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10905 1462 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10906 1416 : set_nonincremental_init (braced_init_obstack);
10907 : }
10908 :
10909 1045601 : if (implicit == 1)
10910 : {
10911 702680 : found_missing_braces = 1;
10912 702680 : if (initializer_stack->missing_brace_richloc)
10913 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10914 702680 : (loc, "{");
10915 : }
10916 :
10917 1045601 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10918 : {
10919 881183 : constructor_fields = TYPE_FIELDS (constructor_type);
10920 : /* Skip any nameless bit fields at the beginning. */
10921 881183 : while (constructor_fields != NULL_TREE
10922 881247 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10923 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10924 :
10925 881183 : constructor_unfilled_fields = constructor_fields;
10926 881183 : constructor_bit_index = bitsize_zero_node;
10927 : }
10928 164418 : else if (gnu_vector_type_p (constructor_type))
10929 : {
10930 : /* Vectors are like simple fixed-size arrays. */
10931 9626 : constructor_max_index =
10932 4813 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10933 4813 : constructor_index = bitsize_int (0);
10934 4813 : constructor_unfilled_index = constructor_index;
10935 : }
10936 159605 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10937 : {
10938 159525 : if (TYPE_DOMAIN (constructor_type))
10939 : {
10940 159525 : constructor_max_index
10941 159525 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10942 :
10943 : /* Detect non-empty initializations of zero-length arrays. */
10944 159525 : if (constructor_max_index == NULL_TREE
10945 159525 : && TYPE_SIZE (constructor_type))
10946 135 : constructor_max_index = integer_minus_one_node;
10947 :
10948 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10949 : to initialize VLAs will cause a proper error; avoid tree
10950 : checking errors as well by setting a safe value. */
10951 159525 : if (constructor_max_index
10952 159251 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10953 2 : constructor_max_index = integer_minus_one_node;
10954 :
10955 159525 : constructor_index
10956 159525 : = convert (bitsizetype,
10957 159525 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10958 : }
10959 : else
10960 0 : constructor_index = bitsize_zero_node;
10961 :
10962 159525 : constructor_unfilled_index = constructor_index;
10963 159525 : if (value && TREE_CODE (value) == STRING_CST)
10964 : {
10965 : /* We need to split the char/wchar array into individual
10966 : characters, so that we don't have to special case it
10967 : everywhere. */
10968 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10969 : }
10970 : }
10971 : else
10972 : {
10973 80 : if (constructor_type != error_mark_node)
10974 : {
10975 51 : if (p->braced_scalar)
10976 22 : permerror_init (input_location, 0,
10977 : "braces around scalar initializer");
10978 : else
10979 29 : warning_init (input_location, 0,
10980 : "braces around scalar initializer");
10981 51 : constructor_braced_scalar = true;
10982 : }
10983 80 : constructor_fields = constructor_type;
10984 80 : constructor_unfilled_fields = constructor_type;
10985 : }
10986 : }
10987 :
10988 : /* At the end of an implicit or explicit brace level,
10989 : finish up that level of constructor. If a single expression
10990 : with redundant braces initialized that level, return the
10991 : c_expr structure for that expression. Otherwise, the original_code
10992 : element is set to ERROR_MARK.
10993 : If we were outputting the elements as they are read, return 0 as the value
10994 : from inner levels (process_init_element ignores that),
10995 : but return error_mark_node as the value from the outermost level
10996 : (that's what we want to put in DECL_INITIAL).
10997 : Otherwise, return a CONSTRUCTOR expression as the value. */
10998 :
10999 : struct c_expr
11000 2084208 : pop_init_level (location_t loc, int implicit,
11001 : struct obstack *braced_init_obstack,
11002 : location_t insert_before)
11003 : {
11004 2084208 : struct constructor_stack *p;
11005 2084208 : struct c_expr ret;
11006 2084208 : ret.value = NULL_TREE;
11007 2084208 : ret.original_code = ERROR_MARK;
11008 2084208 : ret.original_type = NULL;
11009 2084208 : ret.m_decimal = 0;
11010 :
11011 2084208 : if (implicit == 0)
11012 : {
11013 : /* When we come to an explicit close brace,
11014 : pop any inner levels that didn't have explicit braces. */
11015 1380227 : while (constructor_stack->implicit)
11016 1350 : process_init_element (input_location,
11017 : pop_init_level (loc, 1, braced_init_obstack,
11018 : insert_before),
11019 : true, braced_init_obstack);
11020 1378877 : gcc_assert (!constructor_range_stack);
11021 : }
11022 : else
11023 705331 : if (initializer_stack->missing_brace_richloc)
11024 705331 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
11025 705331 : (insert_before, "}");
11026 :
11027 : /* Now output all pending elements. */
11028 2084208 : constructor_incremental = 1;
11029 2084208 : output_pending_init_elements (1, braced_init_obstack);
11030 :
11031 2084208 : p = constructor_stack;
11032 :
11033 : /* Error for initializing a flexible array member. */
11034 2084157 : if (constructor_type && constructor_fields
11035 2245363 : && flexible_array_member_type_p (constructor_type))
11036 : {
11037 : /* Silently discard empty initializations. The parser will
11038 : already have pedwarned for empty brackets for C17 and earlier. */
11039 274 : if (integer_zerop (constructor_unfilled_index))
11040 54 : constructor_type = NULL_TREE;
11041 274 : if (constructor_type || flag_isoc23)
11042 : {
11043 270 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
11044 :
11045 270 : if (constructor_depth <= 2)
11046 223 : pedwarn_init (loc, OPT_Wpedantic,
11047 : "initialization of a flexible array member");
11048 47 : else if (constructor_type)
11049 33 : error_init (loc, "initialization of flexible array member "
11050 : "in a nested context");
11051 : else
11052 14 : pedwarn_init (loc, OPT_Wpedantic,
11053 : "initialization of flexible array member "
11054 : "in a nested context");
11055 :
11056 : /* We have already issued an error message for the existence
11057 : of a flexible array member not at the end of the structure.
11058 : Discard the initializer so that we do not die later. */
11059 270 : if (constructor_type
11060 220 : && DECL_CHAIN (constructor_fields) != NULL_TREE
11061 276 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
11062 0 : constructor_type = NULL_TREE;
11063 : }
11064 : }
11065 :
11066 2084208 : switch (vec_safe_length (constructor_elements))
11067 : {
11068 5169 : case 0:
11069 : /* Initialization with { } counts as zeroinit. */
11070 5169 : constructor_zeroinit = 1;
11071 5169 : break;
11072 916881 : case 1:
11073 : /* This might be zeroinit as well. */
11074 916881 : if (integer_zerop ((*constructor_elements)[0].value))
11075 2200 : constructor_zeroinit = 1;
11076 : break;
11077 1162158 : default:
11078 : /* If the constructor has more than one element, it can't be { 0 }. */
11079 1162158 : constructor_zeroinit = 0;
11080 1162158 : break;
11081 : }
11082 :
11083 : /* Warn when some structs are initialized with direct aggregation. */
11084 2084208 : if (!implicit && found_missing_braces && warn_missing_braces
11085 37 : && !constructor_zeroinit)
11086 : {
11087 18 : gcc_assert (initializer_stack->missing_brace_richloc);
11088 18 : warning_at (initializer_stack->missing_brace_richloc,
11089 18 : OPT_Wmissing_braces,
11090 : "missing braces around initializer");
11091 : }
11092 :
11093 : /* Warn when some struct elements are implicitly initialized to zero. */
11094 2084208 : if (warn_missing_field_initializers
11095 425477 : && constructor_type
11096 425463 : && TREE_CODE (constructor_type) == RECORD_TYPE
11097 193409 : && constructor_unfilled_fields)
11098 : {
11099 : /* Do not warn for flexible array members or zero-length arrays. */
11100 43 : while (constructor_unfilled_fields
11101 43 : && (!DECL_SIZE (constructor_unfilled_fields)
11102 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
11103 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
11104 :
11105 43 : if (constructor_unfilled_fields
11106 : /* Do not warn if this level of the initializer uses member
11107 : designators; it is likely to be deliberate. */
11108 43 : && !constructor_designated
11109 : /* Do not warn about initializing with { 0 } or with { }. */
11110 24 : && !constructor_zeroinit)
11111 : {
11112 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
11113 : "missing initializer for field %qD of %qT",
11114 : constructor_unfilled_fields,
11115 : constructor_type))
11116 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
11117 : "%qD declared here", constructor_unfilled_fields);
11118 : }
11119 : }
11120 :
11121 : /* Pad out the end of the structure. */
11122 2084208 : if (p->replacement_value.value)
11123 : /* If this closes a superfluous brace pair,
11124 : just pass out the element between them. */
11125 138 : ret = p->replacement_value;
11126 2084070 : else if (constructor_type == NULL_TREE)
11127 : ;
11128 2083973 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
11129 2083973 : && TREE_CODE (constructor_type) != ARRAY_TYPE
11130 2083973 : && !gnu_vector_type_p (constructor_type))
11131 : {
11132 : /* A nonincremental scalar initializer--just return
11133 : the element, after verifying there is just one.
11134 : Empty scalar initializers are supported in C23. */
11135 636 : if (vec_safe_is_empty (constructor_elements))
11136 : {
11137 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11138 85 : ret.value = error_mark_node;
11139 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11140 : {
11141 2 : error_init (loc, "invalid initializer");
11142 2 : ret.value = error_mark_node;
11143 : }
11144 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11145 : /* Ensure this is a null pointer constant in the case of a
11146 : 'constexpr' object initialized with {}. */
11147 33 : ret.value = build_zero_cst (ptr_type_node);
11148 : else
11149 81 : ret.value = build_zero_cst (constructor_type);
11150 : }
11151 435 : else if (vec_safe_length (constructor_elements) != 1)
11152 : {
11153 0 : error_init (loc, "extra elements in scalar initializer");
11154 0 : ret.value = (*constructor_elements)[0].value;
11155 : }
11156 : else
11157 435 : ret.value = (*constructor_elements)[0].value;
11158 : }
11159 : else
11160 : {
11161 2083337 : if (constructor_erroneous)
11162 347 : ret.value = error_mark_node;
11163 : else
11164 : {
11165 2082990 : ret.value = build_constructor (constructor_type,
11166 : constructor_elements);
11167 2082990 : if (constructor_constant)
11168 1748681 : TREE_CONSTANT (ret.value) = 1;
11169 2082990 : if (constructor_constant && constructor_simple)
11170 1748642 : TREE_STATIC (ret.value) = 1;
11171 2082990 : if (constructor_nonconst)
11172 673 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11173 2082990 : if (constructor_zero_padding_bits)
11174 687 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11175 : }
11176 : }
11177 :
11178 2084208 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11179 : {
11180 1121 : if (constructor_nonconst)
11181 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11182 1106 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11183 0 : ret.original_code = ERROR_MARK;
11184 : }
11185 :
11186 2084208 : constructor_type = p->type;
11187 2084208 : constructor_fields = p->fields;
11188 2084208 : constructor_index = p->index;
11189 2084208 : constructor_max_index = p->max_index;
11190 2084208 : constructor_unfilled_index = p->unfilled_index;
11191 2084208 : constructor_unfilled_fields = p->unfilled_fields;
11192 2084208 : constructor_bit_index = p->bit_index;
11193 2084208 : constructor_elements = p->elements;
11194 2084208 : constructor_constant = p->constant;
11195 2084208 : constructor_simple = p->simple;
11196 2084208 : constructor_nonconst = p->nonconst;
11197 2084208 : constructor_erroneous = p->erroneous;
11198 2084208 : constructor_incremental = p->incremental;
11199 2084208 : constructor_designated = p->designated;
11200 2084208 : constructor_zero_padding_bits = p->zero_padding_bits;
11201 2084208 : constructor_braced_scalar = p->braced_scalar;
11202 2084208 : designator_depth = p->designator_depth;
11203 2084208 : constructor_pending_elts = p->pending_elts;
11204 2084208 : constructor_depth = p->depth;
11205 2084208 : if (!p->implicit)
11206 1378877 : constructor_range_stack = p->range_stack;
11207 2084208 : RESTORE_SPELLING_DEPTH (constructor_depth);
11208 :
11209 2084208 : constructor_stack = p->next;
11210 2084208 : XDELETE (p);
11211 :
11212 2084208 : if (ret.value == NULL_TREE && constructor_stack == 0)
11213 0 : ret.value = error_mark_node;
11214 2084208 : return ret;
11215 : }
11216 :
11217 : /* Common handling for both array range and field name designators.
11218 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11219 :
11220 : static bool
11221 37362 : set_designator (location_t loc, bool array,
11222 : struct obstack *braced_init_obstack)
11223 : {
11224 37362 : tree subtype;
11225 37362 : enum tree_code subcode;
11226 :
11227 : /* Don't die if an entire brace-pair level is superfluous
11228 : in the containing level, or for an erroneous type. */
11229 37362 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11230 : return true;
11231 :
11232 : /* If there were errors in this designator list already, bail out
11233 : silently. */
11234 37353 : if (designator_erroneous)
11235 : return true;
11236 :
11237 : /* Likewise for an initializer for a variable-size type. Those are
11238 : diagnosed in the parser, except for empty initializer braces. */
11239 37353 : if (COMPLETE_TYPE_P (constructor_type)
11240 37353 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11241 : return true;
11242 :
11243 37343 : if (!designator_depth)
11244 : {
11245 35016 : gcc_assert (!constructor_range_stack);
11246 :
11247 : /* Designator list starts at the level of closest explicit
11248 : braces. */
11249 36691 : while (constructor_stack->implicit)
11250 1675 : process_init_element (input_location,
11251 : pop_init_level (loc, 1, braced_init_obstack,
11252 : last_init_list_comma),
11253 : true, braced_init_obstack);
11254 35016 : constructor_designated = 1;
11255 35016 : return false;
11256 : }
11257 :
11258 2327 : switch (TREE_CODE (constructor_type))
11259 : {
11260 1446 : case RECORD_TYPE:
11261 1446 : case UNION_TYPE:
11262 1446 : subtype = TREE_TYPE (constructor_fields);
11263 1446 : if (subtype != error_mark_node)
11264 1446 : subtype = TYPE_MAIN_VARIANT (subtype);
11265 : break;
11266 881 : case ARRAY_TYPE:
11267 881 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11268 881 : break;
11269 0 : default:
11270 0 : gcc_unreachable ();
11271 : }
11272 :
11273 2327 : subcode = TREE_CODE (subtype);
11274 2327 : if (array && subcode != ARRAY_TYPE)
11275 : {
11276 1 : error_init (loc, "array index in non-array initializer");
11277 1 : return true;
11278 : }
11279 2326 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11280 : {
11281 0 : error_init (loc, "field name not in record or union initializer");
11282 0 : return true;
11283 : }
11284 :
11285 2326 : constructor_designated = 1;
11286 2326 : finish_implicit_inits (loc, braced_init_obstack);
11287 2326 : push_init_level (loc, 2, braced_init_obstack);
11288 2326 : return false;
11289 : }
11290 :
11291 : /* If there are range designators in designator list, push a new designator
11292 : to constructor_range_stack. RANGE_END is end of such stack range or
11293 : NULL_TREE if there is no range designator at this level. */
11294 :
11295 : static void
11296 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11297 : {
11298 390 : struct constructor_range_stack *p;
11299 :
11300 780 : p = (struct constructor_range_stack *)
11301 390 : obstack_alloc (braced_init_obstack,
11302 : sizeof (struct constructor_range_stack));
11303 390 : p->prev = constructor_range_stack;
11304 390 : p->next = 0;
11305 390 : p->fields = constructor_fields;
11306 390 : p->range_start = constructor_index;
11307 390 : p->index = constructor_index;
11308 390 : p->stack = constructor_stack;
11309 390 : p->range_end = range_end;
11310 390 : if (constructor_range_stack)
11311 19 : constructor_range_stack->next = p;
11312 390 : constructor_range_stack = p;
11313 390 : }
11314 :
11315 : /* Within an array initializer, specify the next index to be initialized.
11316 : FIRST is that index. If LAST is nonzero, then initialize a range
11317 : of indices, running from FIRST through LAST. */
11318 :
11319 : void
11320 1940 : set_init_index (location_t loc, tree first, tree last,
11321 : struct obstack *braced_init_obstack)
11322 : {
11323 1940 : if (set_designator (loc, true, braced_init_obstack))
11324 : return;
11325 :
11326 1937 : designator_erroneous = 1;
11327 :
11328 3874 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11329 3863 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11330 : {
11331 12 : error_init (loc, "array index in initializer not of integer type");
11332 12 : return;
11333 : }
11334 :
11335 1925 : if (TREE_CODE (first) != INTEGER_CST)
11336 : {
11337 6 : first = c_fully_fold (first, false, NULL);
11338 6 : if (TREE_CODE (first) == INTEGER_CST)
11339 5 : pedwarn_init (loc, OPT_Wpedantic,
11340 : "array index in initializer is not "
11341 : "an integer constant expression");
11342 : }
11343 :
11344 1925 : if (last && TREE_CODE (last) != INTEGER_CST)
11345 : {
11346 2 : last = c_fully_fold (last, false, NULL);
11347 2 : if (TREE_CODE (last) == INTEGER_CST)
11348 1 : pedwarn_init (loc, OPT_Wpedantic,
11349 : "array index in initializer is not "
11350 : "an integer constant expression");
11351 : }
11352 :
11353 1925 : if (TREE_CODE (first) != INTEGER_CST)
11354 1 : error_init (loc, "nonconstant array index in initializer");
11355 1924 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11356 1 : error_init (loc, "nonconstant array index in initializer");
11357 1923 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11358 9 : error_init (loc, "array index in non-array initializer");
11359 1914 : else if (tree_int_cst_sgn (first) == -1)
11360 15 : error_init (loc, "array index in initializer exceeds array bounds");
11361 1899 : else if (constructor_max_index
11362 1899 : && tree_int_cst_lt (constructor_max_index, first))
11363 7 : error_init (loc, "array index in initializer exceeds array bounds");
11364 : else
11365 : {
11366 1892 : constant_expression_warning (first);
11367 1892 : if (last)
11368 385 : constant_expression_warning (last);
11369 1892 : constructor_index = convert (bitsizetype, first);
11370 1892 : if (tree_int_cst_lt (constructor_index, first))
11371 : {
11372 0 : constructor_index = copy_node (constructor_index);
11373 0 : TREE_OVERFLOW (constructor_index) = 1;
11374 : }
11375 :
11376 1892 : if (last)
11377 : {
11378 385 : if (tree_int_cst_equal (first, last))
11379 : last = NULL_TREE;
11380 384 : else if (tree_int_cst_lt (last, first))
11381 : {
11382 2 : error_init (loc, "empty index range in initializer");
11383 2 : last = NULL_TREE;
11384 : }
11385 : else
11386 : {
11387 382 : last = convert (bitsizetype, last);
11388 382 : if (constructor_max_index != NULL_TREE
11389 382 : && tree_int_cst_lt (constructor_max_index, last))
11390 : {
11391 3 : error_init (loc, "array index range in initializer exceeds "
11392 : "array bounds");
11393 3 : last = NULL_TREE;
11394 : }
11395 : }
11396 : }
11397 :
11398 1892 : designator_depth++;
11399 1892 : designator_erroneous = 0;
11400 1892 : if (constructor_range_stack || last)
11401 379 : push_range_stack (last, braced_init_obstack);
11402 : }
11403 : }
11404 :
11405 : /* Within a struct initializer, specify the next field to be initialized. */
11406 :
11407 : void
11408 35376 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11409 : struct obstack *braced_init_obstack)
11410 : {
11411 35376 : tree field;
11412 :
11413 35376 : if (set_designator (loc, false, braced_init_obstack))
11414 : return;
11415 :
11416 35359 : designator_erroneous = 1;
11417 :
11418 35359 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11419 : {
11420 3 : error_init (loc, "field name not in record or union initializer");
11421 3 : return;
11422 : }
11423 :
11424 35356 : field = lookup_field (constructor_type, fieldname);
11425 :
11426 35356 : if (field == NULL_TREE)
11427 : {
11428 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11429 8 : if (guessed_id)
11430 : {
11431 4 : gcc_rich_location rich_loc (fieldname_loc);
11432 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11433 4 : error_at (&rich_loc,
11434 : "%qT has no member named %qE; did you mean %qE?",
11435 : constructor_type, fieldname, guessed_id);
11436 4 : }
11437 : else
11438 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11439 : constructor_type, fieldname);
11440 : }
11441 : else
11442 35394 : do
11443 : {
11444 35394 : constructor_fields = TREE_VALUE (field);
11445 35394 : designator_depth++;
11446 35394 : designator_erroneous = 0;
11447 35394 : if (constructor_range_stack)
11448 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11449 35394 : field = TREE_CHAIN (field);
11450 35394 : if (field)
11451 : {
11452 46 : if (set_designator (loc, false, braced_init_obstack))
11453 : return;
11454 : }
11455 : }
11456 35394 : while (field != NULL_TREE);
11457 : }
11458 :
11459 : /* Helper function for add_pending_init. Find inorder successor of P
11460 : in AVL tree. */
11461 : static struct init_node *
11462 129 : init_node_successor (struct init_node *p)
11463 : {
11464 129 : struct init_node *r;
11465 129 : if (p->right)
11466 : {
11467 : r = p->right;
11468 58 : while (r->left)
11469 : r = r->left;
11470 : return r;
11471 : }
11472 75 : r = p->parent;
11473 114 : while (r && p == r->right)
11474 : {
11475 39 : p = r;
11476 39 : r = r->parent;
11477 : }
11478 : return r;
11479 : }
11480 :
11481 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11482 : identifies the initializer, either array index or field in a structure.
11483 : VALUE is the value of that index or field. If ORIGTYPE is not
11484 : NULL_TREE, it is the original type of VALUE.
11485 :
11486 : IMPLICIT is true if value comes from pop_init_level (1),
11487 : the new initializer has been merged with the existing one
11488 : and thus no warnings should be emitted about overriding an
11489 : existing initializer. */
11490 :
11491 : static void
11492 7997 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11493 : bool implicit, struct obstack *braced_init_obstack)
11494 : {
11495 7997 : struct init_node *p, **q, *r;
11496 :
11497 12550 : q = &constructor_pending_elts;
11498 7997 : p = 0;
11499 :
11500 7997 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11501 : {
11502 7225 : while (*q != 0)
11503 : {
11504 4386 : p = *q;
11505 4386 : if (tree_int_cst_lt (purpose, p->purpose))
11506 419 : q = &p->left;
11507 3967 : else if (tree_int_cst_lt (p->purpose, purpose))
11508 : {
11509 3356 : if (TREE_CODE (p->value) != RAW_DATA_CST
11510 3356 : || (p->right
11511 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11512 3226 : q = &p->right;
11513 : else
11514 : {
11515 130 : widest_int pp = wi::to_widest (p->purpose);
11516 130 : widest_int pw = wi::to_widest (purpose);
11517 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11518 98 : q = &p->right;
11519 : else
11520 : {
11521 : /* Override which should split the old RAW_DATA_CST
11522 : into 2 or 3 pieces. */
11523 32 : if (!implicit && warn_override_init)
11524 9 : warning_init (loc, OPT_Woverride_init,
11525 : "initialized field overwritten");
11526 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11527 32 : unsigned HOST_WIDE_INT len = 1;
11528 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11529 0 : len = RAW_DATA_LENGTH (value);
11530 32 : unsigned HOST_WIDE_INT end = 0;
11531 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11532 32 : gcc_checking_assert (start < plen && start);
11533 32 : if (plen - start > len)
11534 30 : end = plen - start - len;
11535 32 : tree v = p->value;
11536 32 : tree origtype = p->origtype;
11537 32 : if (start == 1)
11538 2 : p->value = build_int_cst (TREE_TYPE (v),
11539 2 : RAW_DATA_UCHAR_ELT (v, 0));
11540 : else
11541 : {
11542 30 : p->value = v;
11543 30 : if (end > 1)
11544 26 : v = copy_node (v);
11545 30 : RAW_DATA_LENGTH (p->value) = start;
11546 : }
11547 32 : if (end)
11548 : {
11549 30 : tree epurpose
11550 30 : = size_binop (PLUS_EXPR, purpose,
11551 : bitsize_int (len));
11552 30 : if (end > 1)
11553 : {
11554 28 : RAW_DATA_LENGTH (v) -= plen - end;
11555 28 : RAW_DATA_POINTER (v) += plen - end;
11556 : }
11557 : else
11558 2 : v = build_int_cst (TREE_TYPE (v),
11559 2 : RAW_DATA_UCHAR_ELT (v, plen
11560 : - end));
11561 30 : add_pending_init (loc, epurpose, v, origtype,
11562 : implicit, braced_init_obstack);
11563 : }
11564 32 : q = &constructor_pending_elts;
11565 32 : continue;
11566 32 : }
11567 130 : }
11568 : }
11569 : else
11570 : {
11571 611 : if (TREE_CODE (p->value) == RAW_DATA_CST
11572 621 : && (RAW_DATA_LENGTH (p->value)
11573 10 : > (TREE_CODE (value) == RAW_DATA_CST
11574 10 : ? RAW_DATA_LENGTH (value) : 1)))
11575 : {
11576 : /* Override which should split the old RAW_DATA_CST
11577 : into 2 pieces. */
11578 6 : if (!implicit && warn_override_init)
11579 3 : warning_init (loc, OPT_Woverride_init,
11580 : "initialized field overwritten");
11581 6 : unsigned HOST_WIDE_INT len = 1;
11582 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11583 2 : len = RAW_DATA_LENGTH (value);
11584 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11585 : {
11586 6 : RAW_DATA_LENGTH (p->value) -= len;
11587 6 : RAW_DATA_POINTER (p->value) += len;
11588 : }
11589 : else
11590 : {
11591 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11592 0 : p->value
11593 0 : = build_int_cst (TREE_TYPE (p->value),
11594 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11595 : }
11596 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11597 : bitsize_int (len));
11598 6 : continue;
11599 6 : }
11600 605 : if (TREE_CODE (value) == RAW_DATA_CST)
11601 : {
11602 8 : handle_raw_data:
11603 : /* RAW_DATA_CST value might overlap various further
11604 : prior initval entries. Find out how many. */
11605 13 : unsigned cnt = 0;
11606 13 : widest_int w
11607 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11608 13 : struct init_node *r = p, *last = NULL;
11609 13 : bool override_init = warn_override_init;
11610 13 : while ((r = init_node_successor (r))
11611 53 : && wi::to_widest (r->purpose) < w)
11612 : {
11613 40 : ++cnt;
11614 40 : if (TREE_SIDE_EFFECTS (r->value))
11615 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11616 : "initialized field with side-effects "
11617 : "overwritten");
11618 38 : else if (override_init)
11619 : {
11620 6 : warning_init (loc, OPT_Woverride_init,
11621 : "initialized field overwritten");
11622 6 : override_init = false;
11623 : }
11624 40 : last = r;
11625 : }
11626 13 : if (cnt)
11627 : {
11628 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11629 13 : && (wi::to_widest (last->purpose)
11630 13 : + RAW_DATA_LENGTH (last->value) > w))
11631 : {
11632 : /* The last overlapping prior initval overlaps
11633 : only partially. Shrink it and decrease cnt. */
11634 0 : unsigned int l = (wi::to_widest (last->purpose)
11635 0 : + RAW_DATA_LENGTH (last->value)
11636 0 : - w).to_uhwi ();
11637 0 : --cnt;
11638 0 : RAW_DATA_LENGTH (last->value) -= l;
11639 0 : RAW_DATA_POINTER (last->value) += l;
11640 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11641 0 : last->value
11642 0 : = build_int_cst (TREE_TYPE (last->value),
11643 0 : RAW_DATA_UCHAR_ELT (last->value,
11644 : 0));
11645 0 : last->purpose
11646 0 : = size_binop (PLUS_EXPR, last->purpose,
11647 : bitsize_int (l));
11648 : }
11649 : /* Instead of deleting cnt nodes from the AVL tree
11650 : and rebalancing, peel of last cnt bytes from the
11651 : RAW_DATA_CST. Overriding thousands of previously
11652 : initialized array elements with #embed needs to work,
11653 : but doesn't need to be super efficient. */
11654 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11655 : > cnt);
11656 13 : RAW_DATA_LENGTH (value) -= cnt;
11657 13 : const unsigned char *s
11658 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11659 13 : unsigned int o = RAW_DATA_LENGTH (value);
11660 53 : for (r = p; cnt--; ++o, ++s)
11661 : {
11662 40 : r = init_node_successor (r);
11663 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11664 : bitsize_int (o));
11665 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11666 40 : r->origtype = origtype;
11667 : }
11668 13 : if (RAW_DATA_LENGTH (value) == 1)
11669 0 : value = build_int_cst (TREE_TYPE (value),
11670 0 : RAW_DATA_UCHAR_ELT (value, 0));
11671 : }
11672 : }
11673 610 : if (!implicit)
11674 : {
11675 54 : if (TREE_SIDE_EFFECTS (p->value))
11676 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11677 : "initialized field with side-effects "
11678 : "overwritten");
11679 48 : else if (warn_override_init)
11680 13 : warning_init (loc, OPT_Woverride_init,
11681 : "initialized field overwritten");
11682 : }
11683 610 : p->value = value;
11684 610 : p->origtype = origtype;
11685 610 : return;
11686 : }
11687 : }
11688 2839 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11689 : {
11690 61 : struct init_node *r;
11691 61 : if (q == &p->left)
11692 : r = p;
11693 : else
11694 36 : r = init_node_successor (p);
11695 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11696 131 : + RAW_DATA_LENGTH (value)))
11697 : {
11698 : /* Overlap with at least one prior initval in the range but
11699 : not at the start. */
11700 5 : p = r;
11701 5 : p->purpose = purpose;
11702 5 : goto handle_raw_data;
11703 : }
11704 : }
11705 : }
11706 : else
11707 : {
11708 4553 : tree bitpos;
11709 :
11710 4553 : bitpos = bit_position (purpose);
11711 8191 : while (*q != NULL)
11712 : {
11713 4881 : p = *q;
11714 4881 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11715 81 : q = &p->left;
11716 4800 : else if (p->purpose != purpose)
11717 3557 : q = &p->right;
11718 : else
11719 : {
11720 1243 : if (!implicit)
11721 : {
11722 101 : if (TREE_SIDE_EFFECTS (p->value))
11723 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11724 : "initialized field with side-effects "
11725 : "overwritten");
11726 97 : else if (warn_override_init)
11727 6 : warning_init (loc, OPT_Woverride_init,
11728 : "initialized field overwritten");
11729 : }
11730 1243 : p->value = value;
11731 1243 : p->origtype = origtype;
11732 1243 : return;
11733 : }
11734 : }
11735 : }
11736 :
11737 6144 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11738 : sizeof (struct init_node));
11739 6144 : r->purpose = purpose;
11740 6144 : r->value = value;
11741 6144 : r->origtype = origtype;
11742 :
11743 6144 : *q = r;
11744 6144 : r->parent = p;
11745 6144 : r->left = 0;
11746 6144 : r->right = 0;
11747 6144 : r->balance = 0;
11748 :
11749 9970 : while (p)
11750 : {
11751 5214 : struct init_node *s;
11752 :
11753 5214 : if (r == p->left)
11754 : {
11755 343 : if (p->balance == 0)
11756 246 : p->balance = -1;
11757 97 : else if (p->balance < 0)
11758 : {
11759 45 : if (r->balance < 0)
11760 : {
11761 : /* L rotation. */
11762 40 : p->left = r->right;
11763 40 : if (p->left)
11764 2 : p->left->parent = p;
11765 40 : r->right = p;
11766 :
11767 40 : p->balance = 0;
11768 40 : r->balance = 0;
11769 :
11770 40 : s = p->parent;
11771 40 : p->parent = r;
11772 40 : r->parent = s;
11773 40 : if (s)
11774 : {
11775 31 : if (s->left == p)
11776 10 : s->left = r;
11777 : else
11778 21 : s->right = r;
11779 : }
11780 : else
11781 9 : constructor_pending_elts = r;
11782 : }
11783 : else
11784 : {
11785 : /* LR rotation. */
11786 5 : struct init_node *t = r->right;
11787 :
11788 5 : r->right = t->left;
11789 5 : if (r->right)
11790 0 : r->right->parent = r;
11791 5 : t->left = r;
11792 :
11793 5 : p->left = t->right;
11794 5 : if (p->left)
11795 0 : p->left->parent = p;
11796 5 : t->right = p;
11797 :
11798 5 : p->balance = t->balance < 0;
11799 5 : r->balance = -(t->balance > 0);
11800 5 : t->balance = 0;
11801 :
11802 5 : s = p->parent;
11803 5 : p->parent = t;
11804 5 : r->parent = t;
11805 5 : t->parent = s;
11806 5 : if (s)
11807 : {
11808 0 : if (s->left == p)
11809 0 : s->left = t;
11810 : else
11811 0 : s->right = t;
11812 : }
11813 : else
11814 5 : constructor_pending_elts = t;
11815 : }
11816 : break;
11817 : }
11818 : else
11819 : {
11820 : /* p->balance == +1; growth of left side balances the node. */
11821 52 : p->balance = 0;
11822 52 : break;
11823 : }
11824 : }
11825 : else /* r == p->right */
11826 : {
11827 4871 : if (p->balance == 0)
11828 : /* Growth propagation from right side. */
11829 3580 : p->balance++;
11830 1291 : else if (p->balance > 0)
11831 : {
11832 1228 : if (r->balance > 0)
11833 : {
11834 : /* R rotation. */
11835 1222 : p->right = r->left;
11836 1222 : if (p->right)
11837 193 : p->right->parent = p;
11838 1222 : r->left = p;
11839 :
11840 1222 : p->balance = 0;
11841 1222 : r->balance = 0;
11842 :
11843 1222 : s = p->parent;
11844 1222 : p->parent = r;
11845 1222 : r->parent = s;
11846 1222 : if (s)
11847 : {
11848 414 : if (s->left == p)
11849 2 : s->left = r;
11850 : else
11851 412 : s->right = r;
11852 : }
11853 : else
11854 808 : constructor_pending_elts = r;
11855 : }
11856 : else /* r->balance == -1 */
11857 : {
11858 : /* RL rotation */
11859 6 : struct init_node *t = r->left;
11860 :
11861 6 : r->left = t->right;
11862 6 : if (r->left)
11863 2 : r->left->parent = r;
11864 6 : t->right = r;
11865 :
11866 6 : p->right = t->left;
11867 6 : if (p->right)
11868 2 : p->right->parent = p;
11869 6 : t->left = p;
11870 :
11871 6 : r->balance = (t->balance < 0);
11872 6 : p->balance = -(t->balance > 0);
11873 6 : t->balance = 0;
11874 :
11875 6 : s = p->parent;
11876 6 : p->parent = t;
11877 6 : r->parent = t;
11878 6 : t->parent = s;
11879 6 : if (s)
11880 : {
11881 4 : if (s->left == p)
11882 0 : s->left = t;
11883 : else
11884 4 : s->right = t;
11885 : }
11886 : else
11887 2 : constructor_pending_elts = t;
11888 : }
11889 : break;
11890 : }
11891 : else
11892 : {
11893 : /* p->balance == -1; growth of right side balances the node. */
11894 63 : p->balance = 0;
11895 63 : break;
11896 : }
11897 : }
11898 :
11899 3826 : r = p;
11900 3826 : p = p->parent;
11901 : }
11902 : }
11903 :
11904 : /* Build AVL tree from a sorted chain. */
11905 :
11906 : static void
11907 1812 : set_nonincremental_init (struct obstack * braced_init_obstack)
11908 : {
11909 1812 : unsigned HOST_WIDE_INT ix;
11910 1812 : tree index, value;
11911 :
11912 1812 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11913 1812 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11914 : return;
11915 :
11916 5511 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11917 3699 : add_pending_init (input_location, index, value, NULL_TREE, true,
11918 : braced_init_obstack);
11919 1812 : constructor_elements = NULL;
11920 1812 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11921 : {
11922 948 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11923 : /* Skip any nameless bit fields at the beginning. */
11924 948 : while (constructor_unfilled_fields != NULL_TREE
11925 948 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11926 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11927 :
11928 : }
11929 864 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11930 : {
11931 864 : if (TYPE_DOMAIN (constructor_type))
11932 806 : constructor_unfilled_index
11933 806 : = convert (bitsizetype,
11934 806 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11935 : else
11936 58 : constructor_unfilled_index = bitsize_zero_node;
11937 : }
11938 1812 : constructor_incremental = 0;
11939 : }
11940 :
11941 : /* Build AVL tree from a string constant. */
11942 :
11943 : static void
11944 7 : set_nonincremental_init_from_string (tree str,
11945 : struct obstack * braced_init_obstack)
11946 : {
11947 7 : tree value, purpose, type;
11948 7 : HOST_WIDE_INT val[2];
11949 7 : const char *p, *end;
11950 7 : int byte, wchar_bytes, charwidth, bitpos;
11951 :
11952 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11953 :
11954 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11955 7 : charwidth = TYPE_PRECISION (char_type_node);
11956 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11957 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11958 7 : type = TREE_TYPE (constructor_type);
11959 7 : p = TREE_STRING_POINTER (str);
11960 7 : end = p + TREE_STRING_LENGTH (str);
11961 :
11962 7 : for (purpose = bitsize_zero_node;
11963 : p < end
11964 52 : && !(constructor_max_index
11965 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11966 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11967 : {
11968 25 : if (wchar_bytes == 1)
11969 : {
11970 9 : val[0] = (unsigned char) *p++;
11971 9 : val[1] = 0;
11972 : }
11973 : else
11974 : {
11975 16 : val[1] = 0;
11976 16 : val[0] = 0;
11977 64 : for (byte = 0; byte < wchar_bytes; byte++)
11978 : {
11979 48 : if (BYTES_BIG_ENDIAN)
11980 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11981 : else
11982 48 : bitpos = byte * charwidth;
11983 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11984 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11985 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11986 : }
11987 : }
11988 :
11989 25 : if (!TYPE_UNSIGNED (type))
11990 : {
11991 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11992 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11993 : {
11994 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11995 : {
11996 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11997 0 : val[1] = -1;
11998 : }
11999 : }
12000 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
12001 : {
12002 0 : if (val[0] < 0)
12003 0 : val[1] = -1;
12004 : }
12005 0 : else if (val[1] & (HOST_WIDE_INT_1
12006 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
12007 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
12008 : }
12009 :
12010 25 : value = wide_int_to_tree (type,
12011 25 : wide_int::from_array (val, 2,
12012 : HOST_BITS_PER_WIDE_INT * 2));
12013 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
12014 : braced_init_obstack);
12015 : }
12016 :
12017 7 : constructor_incremental = 0;
12018 7 : }
12019 :
12020 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
12021 : not initialized yet. */
12022 :
12023 : static tree
12024 705331 : find_init_member (tree field, struct obstack * braced_init_obstack)
12025 : {
12026 705331 : struct init_node *p;
12027 :
12028 705331 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12029 : {
12030 703508 : if (constructor_incremental
12031 703508 : && tree_int_cst_lt (field, constructor_unfilled_index))
12032 16 : set_nonincremental_init (braced_init_obstack);
12033 :
12034 703508 : p = constructor_pending_elts;
12035 704107 : while (p)
12036 : {
12037 1151 : if (tree_int_cst_lt (field, p->purpose))
12038 93 : p = p->left;
12039 1058 : else if (tree_int_cst_lt (p->purpose, field))
12040 506 : p = p->right;
12041 : else
12042 552 : return p->value;
12043 : }
12044 : }
12045 1823 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12046 : {
12047 1700 : tree bitpos = bit_position (field);
12048 :
12049 1700 : if (constructor_incremental
12050 1700 : && (!constructor_unfilled_fields
12051 613 : || tree_int_cst_lt (bitpos,
12052 613 : bit_position (constructor_unfilled_fields))))
12053 289 : set_nonincremental_init (braced_init_obstack);
12054 :
12055 1700 : p = constructor_pending_elts;
12056 2656 : while (p)
12057 : {
12058 2047 : if (field == p->purpose)
12059 1091 : return p->value;
12060 956 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
12061 7 : p = p->left;
12062 : else
12063 949 : p = p->right;
12064 : }
12065 : }
12066 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12067 : {
12068 123 : if (!vec_safe_is_empty (constructor_elements)
12069 50 : && (constructor_elements->last ().index == field))
12070 50 : return constructor_elements->last ().value;
12071 : }
12072 : return NULL_TREE;
12073 : }
12074 :
12075 : /* "Output" the next constructor element.
12076 : At top level, really output it to assembler code now.
12077 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
12078 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
12079 : TYPE is the data type that the containing data type wants here.
12080 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
12081 : If VALUE is a string constant, STRICT_STRING is true if it is
12082 : unparenthesized or we should not warn here for it being parenthesized.
12083 : For other types of VALUE, STRICT_STRING is not used.
12084 :
12085 : PENDING if true means output pending elements that belong
12086 : right after this element. (PENDING is normally true;
12087 : it is false while outputting pending elements, to avoid recursion.)
12088 :
12089 : IMPLICIT is true if value comes from pop_init_level (1),
12090 : the new initializer has been merged with the existing one
12091 : and thus no warnings should be emitted about overriding an
12092 : existing initializer. */
12093 :
12094 : static void
12095 9724246 : output_init_element (location_t loc, tree value, tree origtype,
12096 : bool strict_string, tree type, tree field, bool pending,
12097 : bool implicit, struct obstack * braced_init_obstack)
12098 : {
12099 9724246 : tree semantic_type = NULL_TREE;
12100 9724246 : bool maybe_const = true;
12101 9724246 : bool npc, int_const_expr, arith_const_expr;
12102 :
12103 9724246 : if (type == error_mark_node || value == error_mark_node)
12104 : {
12105 319 : constructor_erroneous = 1;
12106 4666 : return;
12107 : }
12108 9723927 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
12109 201241 : && (TREE_CODE (value) == STRING_CST
12110 159789 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
12111 41460 : && !(TREE_CODE (value) == STRING_CST
12112 41452 : && TREE_CODE (type) == ARRAY_TYPE
12113 3903 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
12114 9761484 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
12115 37557 : TYPE_MAIN_VARIANT (type)))
12116 37557 : value = array_to_pointer_conversion (input_location, value);
12117 :
12118 9723927 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
12119 182 : && require_constant_value && pending)
12120 : {
12121 : /* As an extension, allow initializing objects with static storage
12122 : duration with compound literals (which are then treated just as
12123 : the brace enclosed list they contain). */
12124 114 : if (flag_isoc99)
12125 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
12126 : "constant");
12127 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
12128 114 : value = DECL_INITIAL (decl);
12129 : }
12130 :
12131 9723927 : npc = null_pointer_constant_p (value);
12132 19447854 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12133 4273018 : && !TREE_OVERFLOW (value)
12134 13996944 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12135 : /* Not fully determined before folding. */
12136 9723927 : arith_const_expr = true;
12137 9723927 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12138 : {
12139 17 : semantic_type = TREE_TYPE (value);
12140 17 : value = TREE_OPERAND (value, 0);
12141 : }
12142 9723927 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12143 : /* TODO: this may not detect all cases of expressions folding to
12144 : constants that are not arithmetic constant expressions. */
12145 9723927 : if (!maybe_const)
12146 : arith_const_expr = false;
12147 19445193 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12148 3205750 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12149 11570099 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12150 : arith_const_expr = false;
12151 7890677 : else if (TREE_CODE (value) != INTEGER_CST
12152 : && TREE_CODE (value) != REAL_CST
12153 : && TREE_CODE (value) != COMPLEX_CST
12154 : && TREE_CODE (value) != RAW_DATA_CST)
12155 : arith_const_expr = false;
12156 4666490 : else if (TREE_OVERFLOW (value))
12157 5057438 : arith_const_expr = false;
12158 :
12159 9723927 : if (value == error_mark_node)
12160 0 : constructor_erroneous = 1;
12161 9723927 : else if (!TREE_CONSTANT (value))
12162 3244828 : constructor_constant = 0;
12163 6479099 : else if (!initializer_constant_valid_p (value,
12164 6479099 : TREE_TYPE (value),
12165 6479099 : AGGREGATE_TYPE_P (constructor_type)
12166 2774638 : && TYPE_REVERSE_STORAGE_ORDER
12167 : (constructor_type))
12168 6479099 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12169 1042867 : && DECL_C_BIT_FIELD (field)
12170 7983 : && TREE_CODE (value) != INTEGER_CST))
12171 77 : constructor_simple = 0;
12172 9723927 : if (!maybe_const)
12173 1174 : constructor_nonconst = 1;
12174 :
12175 : /* Digest the initializer and issue any errors about incompatible
12176 : types before issuing errors about non-constant initializers. */
12177 9723927 : tree new_value = value;
12178 9723927 : if (semantic_type)
12179 17 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12180 : /* In the case of braces around a scalar initializer, the result of
12181 : this initializer processing goes through digest_init again at the
12182 : outer level. In the case of a constexpr initializer for a
12183 : pointer, avoid converting a null pointer constant to something
12184 : that is not a null pointer constant to avoid a spurious error
12185 : from that second processing. */
12186 9723927 : if (!require_constexpr_value
12187 398 : || !npc
12188 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12189 9723916 : new_value = digest_init (loc,
12190 9723916 : RECORD_OR_UNION_TYPE_P (constructor_type)
12191 8647446 : ? field : constructor_fields
12192 8647446 : ? constructor_fields : constructor_decl,
12193 : type, new_value, origtype, npc,
12194 : int_const_expr, arith_const_expr, strict_string,
12195 : require_constant_value, require_constexpr_value);
12196 9723927 : if (new_value == error_mark_node)
12197 : {
12198 76 : constructor_erroneous = 1;
12199 76 : return;
12200 : }
12201 9723851 : if (require_constant_value || require_constant_elements)
12202 2728120 : constant_expression_warning (new_value);
12203 :
12204 : /* Proceed to check the constness of the original initializer. */
12205 9723851 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12206 : {
12207 3244854 : if (require_constant_value)
12208 : {
12209 0 : error_init (loc, "initializer element is not constant");
12210 0 : value = error_mark_node;
12211 : }
12212 3244854 : else if (require_constant_elements)
12213 21 : pedwarn (loc, OPT_Wpedantic,
12214 : "initializer element is not computable at load time");
12215 : }
12216 6478997 : else if (!maybe_const
12217 54 : && (require_constant_value || require_constant_elements))
12218 28 : pedwarn_init (loc, OPT_Wpedantic,
12219 : "initializer element is not a constant expression");
12220 : /* digest_init has already carried out the additional checks
12221 : required for 'constexpr' initializers (using the information
12222 : passed to it about whether the original initializer was certain
12223 : kinds of constant expression), so that check does not need to be
12224 : repeated here. */
12225 :
12226 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12227 : enum type. */
12228 9723851 : if (warn_cxx_compat
12229 10198 : && field != NULL_TREE
12230 10198 : && TREE_CODE (field) == FIELD_DECL
12231 3014 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12232 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12233 21 : != TYPE_MAIN_VARIANT (type))
12234 9723872 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12235 : {
12236 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12237 21 : if (checktype != error_mark_node
12238 21 : && (TYPE_MAIN_VARIANT (checktype)
12239 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12240 9 : warning_init (loc, OPT_Wc___compat,
12241 : "enum conversion in initialization is invalid in C++");
12242 : }
12243 :
12244 : /* If this field is empty and does not have side effects (and is not at
12245 : the end of structure), don't do anything other than checking the
12246 : initializer. */
12247 9723851 : if (field
12248 9723851 : && (TREE_TYPE (field) == error_mark_node
12249 9723416 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12250 9723101 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12251 92 : && !TREE_SIDE_EFFECTS (new_value)
12252 63 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12253 63 : || DECL_CHAIN (field)))))
12254 : return;
12255 :
12256 : /* Finally, set VALUE to the initializer value digested above. */
12257 9723823 : value = new_value;
12258 :
12259 : /* If this element doesn't come next in sequence,
12260 : put it on constructor_pending_elts. */
12261 9723823 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12262 9723823 : && (!constructor_incremental
12263 1735080 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12264 1734637 : || (TREE_CODE (value) == RAW_DATA_CST
12265 241 : && constructor_pending_elts
12266 66 : && pending)))
12267 : {
12268 1350 : if (constructor_incremental
12269 1350 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12270 414 : || (TREE_CODE (value) == RAW_DATA_CST
12271 14 : && constructor_pending_elts
12272 14 : && pending)))
12273 48 : set_nonincremental_init (braced_init_obstack);
12274 :
12275 1350 : add_pending_init (loc, field, value, origtype, implicit,
12276 : braced_init_obstack);
12277 1350 : return;
12278 : }
12279 9722473 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12280 1044774 : && (!constructor_incremental
12281 1043108 : || field != constructor_unfilled_fields))
12282 : {
12283 : /* We do this for records but not for unions. In a union,
12284 : no matter which field is specified, it can be initialized
12285 : right away since it starts at the beginning of the union. */
12286 2893 : if (constructor_incremental)
12287 : {
12288 1227 : if (!constructor_unfilled_fields)
12289 39 : set_nonincremental_init (braced_init_obstack);
12290 : else
12291 : {
12292 1188 : tree bitpos, unfillpos;
12293 :
12294 1188 : bitpos = bit_position (field);
12295 1188 : unfillpos = bit_position (constructor_unfilled_fields);
12296 :
12297 1188 : if (tree_int_cst_lt (bitpos, unfillpos))
12298 4 : set_nonincremental_init (braced_init_obstack);
12299 : }
12300 : }
12301 :
12302 2893 : add_pending_init (loc, field, value, origtype, implicit,
12303 : braced_init_obstack);
12304 2893 : return;
12305 : }
12306 9719580 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12307 9719580 : && !vec_safe_is_empty (constructor_elements))
12308 : {
12309 62 : if (!implicit)
12310 : {
12311 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12312 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12313 : "initialized field with side-effects overwritten");
12314 9 : else if (warn_override_init)
12315 6 : warning_init (loc, OPT_Woverride_init,
12316 : "initialized field overwritten");
12317 : }
12318 :
12319 : /* We can have just one union field set. */
12320 62 : constructor_elements = NULL;
12321 : }
12322 :
12323 : /* Otherwise, output this element either to
12324 : constructor_elements or to the assembler file. */
12325 :
12326 9719580 : constructor_elt celt = {field, value};
12327 9719580 : vec_safe_push (constructor_elements, celt);
12328 :
12329 : /* Advance the variable that indicates sequential elements output. */
12330 9719580 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12331 : {
12332 1734632 : tree inc = bitsize_one_node;
12333 1734632 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12334 236 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12335 1734632 : constructor_unfilled_index
12336 1734632 : = size_binop_loc (input_location, PLUS_EXPR,
12337 : constructor_unfilled_index, inc);
12338 : }
12339 7984948 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12340 : {
12341 1041881 : constructor_unfilled_fields
12342 1041881 : = DECL_CHAIN (constructor_unfilled_fields);
12343 :
12344 : /* Skip any nameless bit fields. */
12345 1041881 : while (constructor_unfilled_fields != NULL_TREE
12346 1041986 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12347 105 : constructor_unfilled_fields
12348 105 : = DECL_CHAIN (constructor_unfilled_fields);
12349 : }
12350 6943067 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12351 31654 : constructor_unfilled_fields = NULL_TREE;
12352 :
12353 : /* Now output any pending elements which have become next. */
12354 9719580 : if (pending)
12355 9713381 : output_pending_init_elements (0, braced_init_obstack);
12356 : }
12357 :
12358 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12359 : comes before field2, 1 if field1 comes after field2 and
12360 : 0 if field1 == field2. */
12361 :
12362 : static int
12363 7112 : init_field_decl_cmp (tree field1, tree field2)
12364 : {
12365 7112 : if (field1 == field2)
12366 : return 0;
12367 :
12368 3346 : tree bitpos1 = bit_position (field1);
12369 3346 : tree bitpos2 = bit_position (field2);
12370 3346 : if (tree_int_cst_equal (bitpos1, bitpos2))
12371 : {
12372 : /* If one of the fields has non-zero bitsize, then that
12373 : field must be the last one in a sequence of zero
12374 : sized fields, fields after it will have bigger
12375 : bit_position. */
12376 32 : if (TREE_TYPE (field1) != error_mark_node
12377 32 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12378 64 : && integer_nonzerop (TREE_TYPE (field1)))
12379 : return 1;
12380 32 : if (TREE_TYPE (field2) != error_mark_node
12381 32 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12382 57 : && integer_nonzerop (TREE_TYPE (field2)))
12383 : return -1;
12384 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12385 : which field comes earlier. Walk chains of both
12386 : fields, so that if field1 and field2 are close to each
12387 : other in either order, it is found soon even for large
12388 : sequences of zero sized fields. */
12389 : tree f1 = field1, f2 = field2;
12390 32 : while (1)
12391 : {
12392 32 : f1 = DECL_CHAIN (f1);
12393 32 : f2 = DECL_CHAIN (f2);
12394 32 : if (f1 == NULL_TREE)
12395 : {
12396 7 : gcc_assert (f2);
12397 : return 1;
12398 : }
12399 25 : if (f2 == NULL_TREE)
12400 : return -1;
12401 9 : if (f1 == field2)
12402 : return -1;
12403 0 : if (f2 == field1)
12404 : return 1;
12405 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12406 : return 1;
12407 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12408 : return -1;
12409 : }
12410 : }
12411 3314 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12412 : return -1;
12413 : else
12414 1948 : return 1;
12415 : }
12416 :
12417 : /* Output any pending elements which have become next.
12418 : As we output elements, constructor_unfilled_{fields,index}
12419 : advances, which may cause other elements to become next;
12420 : if so, they too are output.
12421 :
12422 : If ALL is 0, we return when there are
12423 : no more pending elements to output now.
12424 :
12425 : If ALL is 1, we output space as necessary so that
12426 : we can output all the pending elements. */
12427 : static void
12428 11797589 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12429 : {
12430 11797589 : struct init_node *elt = constructor_pending_elts;
12431 11798764 : tree next;
12432 :
12433 11798764 : retry:
12434 :
12435 : /* Look through the whole pending tree.
12436 : If we find an element that should be output now,
12437 : output it. Otherwise, set NEXT to the element
12438 : that comes first among those still pending. */
12439 :
12440 11798764 : next = NULL_TREE;
12441 11810955 : while (elt)
12442 : {
12443 15082 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12444 : {
12445 6839 : if (tree_int_cst_equal (elt->purpose,
12446 : constructor_unfilled_index))
12447 2838 : output_init_element (input_location, elt->value, elt->origtype,
12448 2838 : true, TREE_TYPE (constructor_type),
12449 : constructor_unfilled_index, false, false,
12450 : braced_init_obstack);
12451 4001 : else if (tree_int_cst_lt (constructor_unfilled_index,
12452 4001 : elt->purpose))
12453 : {
12454 : /* Advance to the next smaller node. */
12455 1142 : if (elt->left)
12456 : elt = elt->left;
12457 : else
12458 : {
12459 : /* We have reached the smallest node bigger than the
12460 : current unfilled index. Fill the space first. */
12461 354 : next = elt->purpose;
12462 354 : break;
12463 : }
12464 : }
12465 : else
12466 : {
12467 : /* Advance to the next bigger node. */
12468 2859 : if (elt->right)
12469 : elt = elt->right;
12470 : else
12471 : {
12472 : /* We have reached the biggest node in a subtree. Find
12473 : the parent of it, which is the next bigger node. */
12474 2859 : while (elt->parent && elt->parent->right == elt)
12475 : elt = elt->parent;
12476 1903 : elt = elt->parent;
12477 2686 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12478 783 : elt->purpose))
12479 : {
12480 22 : next = elt->purpose;
12481 22 : break;
12482 : }
12483 : }
12484 : }
12485 : }
12486 8243 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12487 : {
12488 : /* If the current record is complete we are done. */
12489 8243 : if (constructor_unfilled_fields == NULL_TREE)
12490 : break;
12491 :
12492 6636 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12493 : elt->purpose);
12494 6636 : if (cmp == 0)
12495 3361 : output_init_element (input_location, elt->value, elt->origtype,
12496 3361 : true, TREE_TYPE (elt->purpose),
12497 : elt->purpose, false, false,
12498 : braced_init_obstack);
12499 3275 : else if (cmp < 0)
12500 : {
12501 : /* Advance to the next smaller node. */
12502 1320 : if (elt->left)
12503 : elt = elt->left;
12504 : else
12505 : {
12506 : /* We have reached the smallest node bigger than the
12507 : current unfilled field. Fill the space first. */
12508 837 : next = elt->purpose;
12509 837 : break;
12510 : }
12511 : }
12512 : else
12513 : {
12514 : /* Advance to the next bigger node. */
12515 1955 : if (elt->right)
12516 : elt = elt->right;
12517 : else
12518 : {
12519 : /* We have reached the biggest node in a subtree. Find
12520 : the parent of it, which is the next bigger node. */
12521 1025 : while (elt->parent && elt->parent->right == elt)
12522 : elt = elt->parent;
12523 789 : elt = elt->parent;
12524 789 : if (elt
12525 789 : && init_field_decl_cmp (constructor_unfilled_fields,
12526 : elt->purpose) < 0)
12527 : {
12528 71 : next = elt->purpose;
12529 71 : break;
12530 : }
12531 : }
12532 : }
12533 : }
12534 : }
12535 :
12536 : /* Ordinarily return, but not if we want to output all
12537 : and there are elements left. */
12538 11798764 : if (!(all && next != NULL_TREE))
12539 11797589 : return;
12540 :
12541 : /* If it's not incremental, just skip over the gap, so that after
12542 : jumping to retry we will output the next successive element. */
12543 1175 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12544 838 : constructor_unfilled_fields = next;
12545 337 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12546 337 : constructor_unfilled_index = next;
12547 :
12548 : /* ELT now points to the node in the pending tree with the next
12549 : initializer to output. */
12550 1175 : goto retry;
12551 : }
12552 :
12553 : /* Expression VALUE coincides with the start of type TYPE in a braced
12554 : initializer. Return true if we should treat VALUE as initializing
12555 : the first element of TYPE, false if we should treat it as initializing
12556 : TYPE as a whole.
12557 :
12558 : If the initializer is clearly invalid, the question becomes:
12559 : which choice gives the best error message? */
12560 :
12561 : static bool
12562 3505551 : initialize_elementwise_p (tree type, tree value)
12563 : {
12564 3505551 : if (type == error_mark_node || value == error_mark_node)
12565 : return false;
12566 :
12567 3505253 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12568 :
12569 3505253 : tree value_type = TREE_TYPE (value);
12570 3505253 : if (value_type == error_mark_node)
12571 : return false;
12572 :
12573 : /* GNU vectors can be initialized elementwise. However, treat any
12574 : kind of vector value as initializing the vector type as a whole,
12575 : regardless of whether the value is a GNU vector. Such initializers
12576 : are valid if and only if they would have been valid in a non-braced
12577 : initializer like:
12578 :
12579 : TYPE foo = VALUE;
12580 :
12581 : so recursing into the vector type would be at best confusing or at
12582 : worst wrong. For example, when -flax-vector-conversions is in effect,
12583 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12584 : have different element types and different numbers of elements. */
12585 3505253 : if (gnu_vector_type_p (type))
12586 19704 : return !VECTOR_TYPE_P (value_type);
12587 :
12588 3485549 : if (AGGREGATE_TYPE_P (type))
12589 1743783 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12590 :
12591 : return false;
12592 : }
12593 :
12594 : /* Helper function for process_init_element. Split first element of
12595 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12596 :
12597 : static inline tree
12598 9717891 : maybe_split_raw_data (tree value, tree *raw_data)
12599 : {
12600 9717891 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12601 : return value;
12602 211 : *raw_data = value;
12603 211 : value = build_int_cst (integer_type_node,
12604 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12605 211 : ++RAW_DATA_POINTER (*raw_data);
12606 211 : --RAW_DATA_LENGTH (*raw_data);
12607 211 : return value;
12608 : }
12609 :
12610 : /* Return non-zero if c_parser_initval should attempt to optimize
12611 : large initializers into RAW_DATA_CST. In that case return how
12612 : many elements to optimize at most. */
12613 :
12614 : unsigned
12615 3033828 : c_maybe_optimize_large_byte_initializer (void)
12616 : {
12617 3033828 : if (!constructor_type
12618 3033758 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12619 220909 : || constructor_stack->implicit)
12620 : return 0;
12621 218144 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12622 218144 : if (TREE_CODE (elttype) != INTEGER_TYPE
12623 218144 : && TREE_CODE (elttype) != BITINT_TYPE)
12624 : return 0;
12625 195165 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12626 29189 : || constructor_stack->replacement_value.value
12627 29189 : || (COMPLETE_TYPE_P (constructor_type)
12628 27314 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12629 224354 : || constructor_range_stack)
12630 : return 0;
12631 29189 : if (constructor_max_index == NULL_TREE)
12632 : return INT_MAX;
12633 27314 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12634 27314 : || integer_all_onesp (constructor_max_index))
12635 : return 0;
12636 22520 : widest_int w = wi::to_widest (constructor_max_index);
12637 22520 : w -= wi::to_widest (constructor_index);
12638 22520 : w += 1;
12639 22520 : if (w < 64)
12640 : return 0;
12641 6894 : if (w > INT_MAX)
12642 : return INT_MAX;
12643 6894 : return w.to_uhwi ();
12644 3033828 : }
12645 :
12646 : /* Add one non-braced element to the current constructor level.
12647 : This adjusts the current position within the constructor's type.
12648 : This may also start or terminate implicit levels
12649 : to handle a partly-braced initializer.
12650 :
12651 : Once this has found the correct level for the new element,
12652 : it calls output_init_element.
12653 :
12654 : IMPLICIT is true if value comes from pop_init_level (1),
12655 : the new initializer has been merged with the existing one
12656 : and thus no warnings should be emitted about overriding an
12657 : existing initializer. */
12658 :
12659 : void
12660 9708191 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12661 : struct obstack * braced_init_obstack)
12662 : {
12663 9708191 : tree orig_value = value.value;
12664 19416382 : int string_flag
12665 9708191 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12666 9708191 : bool strict_string = value.original_code == STRING_CST;
12667 9708191 : bool was_designated = designator_depth != 0;
12668 9708191 : tree raw_data = NULL_TREE;
12669 :
12670 9708403 : retry:
12671 9708403 : designator_depth = 0;
12672 9708403 : designator_erroneous = 0;
12673 :
12674 9708403 : if (!implicit && value.value && !integer_zerop (value.value))
12675 7666902 : constructor_zeroinit = 0;
12676 :
12677 : /* Handle superfluous braces around string cst as in
12678 : char x[] = {"foo"}; */
12679 9708403 : if (constructor_type
12680 9708275 : && !was_designated
12681 9670988 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12682 1720615 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12683 10458731 : && integer_zerop (constructor_unfilled_index))
12684 : {
12685 164476 : if (constructor_stack->replacement_value.value)
12686 : {
12687 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12688 409 : return;
12689 : }
12690 164468 : else if (string_flag)
12691 : {
12692 138 : constructor_stack->replacement_value = value;
12693 138 : return;
12694 : }
12695 : }
12696 :
12697 9708257 : if (constructor_stack->replacement_value.value != NULL_TREE)
12698 : {
12699 0 : error_init (loc, "excess elements in struct initializer");
12700 0 : return;
12701 : }
12702 :
12703 : /* Ignore elements of a brace group if it is entirely superfluous
12704 : and has already been diagnosed, or if the type is erroneous. */
12705 9708257 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12706 : return;
12707 :
12708 : /* Ignore elements of an initializer for a variable-size type.
12709 : Those are diagnosed in the parser (empty initializer braces are OK). */
12710 9708060 : if (COMPLETE_TYPE_P (constructor_type)
12711 9708060 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12712 : return;
12713 :
12714 9002671 : if (!implicit && warn_designated_init && !was_designated
12715 8967710 : && TREE_CODE (constructor_type) == RECORD_TYPE
12716 10744931 : && lookup_attribute ("designated_init",
12717 1036929 : TYPE_ATTRIBUTES (constructor_type)))
12718 50 : warning_init (loc,
12719 : OPT_Wdesignated_init,
12720 : "positional initialization of field "
12721 : "in %<struct%> declared with %<designated_init%> attribute");
12722 :
12723 : /* If we've exhausted any levels that didn't have braces,
12724 : pop them now. */
12725 10409981 : while (constructor_stack->implicit)
12726 : {
12727 708558 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12728 703715 : && constructor_fields == NULL_TREE)
12729 701738 : process_init_element (loc,
12730 : pop_init_level (loc, 1, braced_init_obstack,
12731 : last_init_list_comma),
12732 : true, braced_init_obstack);
12733 6820 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12734 2621 : || gnu_vector_type_p (constructor_type))
12735 4843 : && constructor_max_index
12736 11636 : && tree_int_cst_lt (constructor_max_index,
12737 : constructor_index))
12738 241 : process_init_element (loc,
12739 : pop_init_level (loc, 1, braced_init_obstack,
12740 : last_init_list_comma),
12741 : true, braced_init_obstack);
12742 : else
12743 : break;
12744 : }
12745 :
12746 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12747 9708002 : if (constructor_range_stack)
12748 : {
12749 : /* If value is a compound literal and we'll be just using its
12750 : content, don't put it into a SAVE_EXPR. */
12751 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12752 3 : || !require_constant_value)
12753 : {
12754 368 : tree semantic_type = NULL_TREE;
12755 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12756 : {
12757 0 : semantic_type = TREE_TYPE (value.value);
12758 0 : value.value = TREE_OPERAND (value.value, 0);
12759 : }
12760 368 : value.value = save_expr (value.value);
12761 368 : if (semantic_type)
12762 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12763 : value.value);
12764 : }
12765 : }
12766 :
12767 10422246 : while (1)
12768 : {
12769 10422246 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12770 : {
12771 1043133 : tree fieldtype;
12772 1043133 : enum tree_code fieldcode;
12773 :
12774 1043133 : if (constructor_fields == NULL_TREE)
12775 : {
12776 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12777 1280 : break;
12778 : }
12779 :
12780 1041853 : fieldtype = TREE_TYPE (constructor_fields);
12781 1041853 : if (fieldtype != error_mark_node)
12782 1041852 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12783 1041853 : fieldcode = TREE_CODE (fieldtype);
12784 :
12785 : /* Error for non-static initialization of a flexible array member. */
12786 1041853 : if (fieldcode == ARRAY_TYPE
12787 156140 : && !require_constant_value
12788 1899 : && TYPE_SIZE (fieldtype) == NULL_TREE
12789 1041863 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12790 : {
12791 10 : error_init (loc, "non-static initialization of a flexible "
12792 : "array member");
12793 10 : break;
12794 : }
12795 :
12796 : /* Error for initialization of a flexible array member with
12797 : a string constant if the structure is in an array. E.g.:
12798 : struct S { int x; char y[]; };
12799 : struct S s[] = { { 1, "foo" } };
12800 : is invalid. */
12801 1041843 : if (string_flag
12802 1041843 : && fieldcode == ARRAY_TYPE
12803 3358 : && constructor_depth > 1
12804 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12805 1041890 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12806 : {
12807 47 : bool in_array_p = false;
12808 47 : for (struct constructor_stack *p = constructor_stack;
12809 73 : p && p->type; p = p->next)
12810 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12811 : {
12812 : in_array_p = true;
12813 : break;
12814 : }
12815 47 : if (in_array_p)
12816 : {
12817 33 : error_init (loc, "initialization of flexible array "
12818 : "member in a nested context");
12819 33 : break;
12820 : }
12821 : }
12822 :
12823 : /* Accept a string constant to initialize a subarray. */
12824 1041810 : if (value.value != NULL_TREE
12825 1041767 : && fieldcode == ARRAY_TYPE
12826 156054 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12827 1196547 : && string_flag)
12828 : value.value = orig_value;
12829 : /* Otherwise, if we have come to a subaggregate,
12830 : and we don't have an element of its type, push into it. */
12831 1038789 : else if (value.value != NULL_TREE
12832 1038485 : && initialize_elementwise_p (fieldtype, value.value))
12833 : {
12834 304 : push_init_level (loc, 1, braced_init_obstack);
12835 304 : continue;
12836 : }
12837 :
12838 1041506 : value.value = maybe_split_raw_data (value.value, &raw_data);
12839 1041506 : if (value.value)
12840 : {
12841 1041463 : push_member_name (constructor_fields);
12842 1041463 : output_init_element (loc, value.value, value.original_type,
12843 : strict_string, fieldtype,
12844 : constructor_fields, true, implicit,
12845 : braced_init_obstack);
12846 1041463 : RESTORE_SPELLING_DEPTH (constructor_depth);
12847 : }
12848 : else
12849 : /* Do the bookkeeping for an element that was
12850 : directly output as a constructor. */
12851 : {
12852 : /* For a record, keep track of end position of last field. */
12853 43 : if (DECL_SIZE (constructor_fields))
12854 0 : constructor_bit_index
12855 0 : = size_binop_loc (input_location, PLUS_EXPR,
12856 : bit_position (constructor_fields),
12857 0 : DECL_SIZE (constructor_fields));
12858 :
12859 : /* If the current field was the first one not yet written out,
12860 : it isn't now, so update. */
12861 43 : if (constructor_unfilled_fields == constructor_fields)
12862 : {
12863 34 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12864 : /* Skip any nameless bit fields. */
12865 34 : while (constructor_unfilled_fields != 0
12866 34 : && (DECL_UNNAMED_BIT_FIELD
12867 : (constructor_unfilled_fields)))
12868 0 : constructor_unfilled_fields =
12869 0 : DECL_CHAIN (constructor_unfilled_fields);
12870 : }
12871 : }
12872 :
12873 1041506 : constructor_fields = DECL_CHAIN (constructor_fields);
12874 : /* Skip any nameless bit fields at the beginning. */
12875 1041506 : while (constructor_fields != NULL_TREE
12876 1041611 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12877 105 : constructor_fields = DECL_CHAIN (constructor_fields);
12878 : }
12879 9379113 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12880 : {
12881 31717 : tree fieldtype;
12882 31717 : enum tree_code fieldcode;
12883 :
12884 31717 : if (constructor_fields == NULL_TREE)
12885 : {
12886 3 : pedwarn_init (loc, 0,
12887 : "excess elements in union initializer");
12888 3 : break;
12889 : }
12890 :
12891 31714 : fieldtype = TREE_TYPE (constructor_fields);
12892 31714 : if (fieldtype != error_mark_node)
12893 31714 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12894 31714 : fieldcode = TREE_CODE (fieldtype);
12895 :
12896 : /* Warn that traditional C rejects initialization of unions.
12897 : We skip the warning if the value is zero. This is done
12898 : under the assumption that the zero initializer in user
12899 : code appears conditioned on e.g. __STDC__ to avoid
12900 : "missing initializer" warnings and relies on default
12901 : initialization to zero in the traditional C case.
12902 : We also skip the warning if the initializer is designated,
12903 : again on the assumption that this must be conditional on
12904 : __STDC__ anyway (and we've already complained about the
12905 : member-designator already). */
12906 34186 : if (!in_system_header_at (input_location) && !constructor_designated
12907 33269 : && !(value.value && (integer_zerop (value.value)
12908 1456 : || real_zerop (value.value))))
12909 1449 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12910 : "of unions");
12911 :
12912 : /* Error for non-static initialization of a flexible array member. */
12913 31714 : if (fieldcode == ARRAY_TYPE
12914 767 : && !require_constant_value
12915 31772 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12916 : {
12917 3 : error_init (loc, "non-static initialization of a flexible "
12918 : "array member");
12919 3 : break;
12920 : }
12921 :
12922 : /* Error for initialization of a flexible array member with
12923 : a string constant if the structure is in an array. E.g.:
12924 : union U { int x; char y[]; };
12925 : union U s[] = { { 1, "foo" } };
12926 : is invalid. */
12927 31711 : if (string_flag
12928 31711 : && fieldcode == ARRAY_TYPE
12929 7 : && constructor_depth > 1
12930 31715 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12931 : {
12932 3 : bool in_array_p = false;
12933 3 : for (struct constructor_stack *p = constructor_stack;
12934 3 : p && p->type; p = p->next)
12935 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12936 : {
12937 : in_array_p = true;
12938 : break;
12939 : }
12940 3 : if (in_array_p)
12941 : {
12942 3 : error_init (loc, "initialization of flexible array "
12943 : "member in a nested context");
12944 3 : break;
12945 : }
12946 : }
12947 :
12948 : /* Accept a string constant to initialize a subarray. */
12949 31708 : if (value.value != NULL_TREE
12950 31708 : && fieldcode == ARRAY_TYPE
12951 761 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12952 32464 : && string_flag)
12953 : value.value = orig_value;
12954 : /* Otherwise, if we have come to a subaggregate,
12955 : and we don't have an element of its type, push into it. */
12956 31754 : else if (value.value != NULL_TREE
12957 31704 : && initialize_elementwise_p (fieldtype, value.value))
12958 : {
12959 50 : push_init_level (loc, 1, braced_init_obstack);
12960 50 : continue;
12961 : }
12962 :
12963 31658 : value.value = maybe_split_raw_data (value.value, &raw_data);
12964 31658 : if (value.value)
12965 : {
12966 31658 : push_member_name (constructor_fields);
12967 31658 : output_init_element (loc, value.value, value.original_type,
12968 : strict_string, fieldtype,
12969 : constructor_fields, true, implicit,
12970 : braced_init_obstack);
12971 31658 : RESTORE_SPELLING_DEPTH (constructor_depth);
12972 : }
12973 : else
12974 : /* Do the bookkeeping for an element that was
12975 : directly output as a constructor. */
12976 : {
12977 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12978 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12979 : }
12980 :
12981 31658 : constructor_fields = NULL_TREE;
12982 : }
12983 9347396 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12984 : {
12985 2435901 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12986 2435901 : enum tree_code eltcode = TREE_CODE (elttype);
12987 :
12988 : /* Accept a string constant to initialize a subarray. */
12989 2435901 : if (value.value != NULL_TREE
12990 2435901 : && eltcode == ARRAY_TYPE
12991 7119 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12992 2441537 : && string_flag)
12993 : value.value = orig_value;
12994 : /* Otherwise, if we have come to a subaggregate,
12995 : and we don't have an element of its type, push into it. */
12996 3137731 : else if (value.value != NULL_TREE
12997 2435405 : && initialize_elementwise_p (elttype, value.value))
12998 : {
12999 702326 : push_init_level (loc, 1, braced_init_obstack);
13000 702326 : continue;
13001 : }
13002 :
13003 1733575 : if (constructor_max_index != NULL_TREE
13004 684068 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
13005 683953 : || integer_all_onesp (constructor_max_index))
13006 : /* For VLA we got an error already. */
13007 1733690 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
13008 : {
13009 113 : pedwarn_init (loc, 0,
13010 : "excess elements in array initializer");
13011 113 : break;
13012 : }
13013 :
13014 1733462 : if (value.value
13015 1733462 : && TREE_CODE (value.value) == RAW_DATA_CST
13016 199 : && RAW_DATA_LENGTH (value.value) > 1
13017 199 : && (TREE_CODE (elttype) == INTEGER_TYPE
13018 199 : || TREE_CODE (elttype) == BITINT_TYPE)
13019 199 : && TYPE_PRECISION (elttype) == CHAR_BIT
13020 1733661 : && (constructor_max_index == NULL_TREE
13021 59 : || tree_int_cst_lt (constructor_index,
13022 : constructor_max_index)))
13023 : {
13024 199 : unsigned int len = RAW_DATA_LENGTH (value.value);
13025 199 : if (constructor_max_index)
13026 : {
13027 59 : widest_int w = wi::to_widest (constructor_max_index);
13028 59 : w -= wi::to_widest (constructor_index);
13029 59 : w += 1;
13030 59 : if (w < len)
13031 3 : len = w.to_uhwi ();
13032 59 : }
13033 199 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
13034 : {
13035 3 : raw_data = copy_node (value.value);
13036 3 : RAW_DATA_LENGTH (raw_data) -= len;
13037 3 : RAW_DATA_POINTER (raw_data) += len;
13038 3 : RAW_DATA_LENGTH (value.value) = len;
13039 : }
13040 199 : TREE_TYPE (value.value) = elttype;
13041 199 : push_array_bounds (tree_to_uhwi (constructor_index));
13042 199 : output_init_element (loc, value.value, value.original_type,
13043 : false, elttype, constructor_index, true,
13044 : implicit, braced_init_obstack);
13045 199 : RESTORE_SPELLING_DEPTH (constructor_depth);
13046 199 : constructor_index
13047 199 : = size_binop_loc (input_location, PLUS_EXPR,
13048 199 : constructor_index, bitsize_int (len));
13049 : }
13050 : else
13051 : {
13052 1733263 : value.value = maybe_split_raw_data (value.value, &raw_data);
13053 : /* Now output the actual element. */
13054 1733263 : if (value.value)
13055 : {
13056 1733263 : push_array_bounds (tree_to_uhwi (constructor_index));
13057 1733263 : output_init_element (loc, value.value, value.original_type,
13058 : strict_string, elttype,
13059 : constructor_index, true, implicit,
13060 : braced_init_obstack);
13061 1733263 : RESTORE_SPELLING_DEPTH (constructor_depth);
13062 : }
13063 :
13064 1733263 : constructor_index
13065 1733263 : = size_binop_loc (input_location, PLUS_EXPR,
13066 : constructor_index, bitsize_one_node);
13067 :
13068 1733263 : if (!value.value)
13069 : /* If we are doing the bookkeeping for an element that was
13070 : directly output as a constructor, we must update
13071 : constructor_unfilled_index. */
13072 0 : constructor_unfilled_index = constructor_index;
13073 : }
13074 : }
13075 6911495 : else if (gnu_vector_type_p (constructor_type))
13076 : {
13077 6911003 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
13078 :
13079 : /* Do a basic check of initializer size. Note that vectors
13080 : may not always have a fixed size derived from their type. */
13081 6911003 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
13082 6911003 : tree_to_poly_uint64 (constructor_index)))
13083 : {
13084 : /* Diagose VLA out-of-bounds as errors. */
13085 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
13086 2 : pedwarn_init (loc, 0,
13087 : "excess elements in vector initializer");
13088 : else
13089 : error_init (loc, "excess elements in vector initializer");
13090 :
13091 : break;
13092 : }
13093 :
13094 6911001 : value.value = maybe_split_raw_data (value.value, &raw_data);
13095 : /* Now output the actual element. */
13096 6911001 : if (value.value)
13097 : {
13098 6911001 : if (TREE_CODE (value.value) == VECTOR_CST)
13099 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
13100 6911001 : output_init_element (loc, value.value, value.original_type,
13101 : strict_string, elttype,
13102 : constructor_index, true, implicit,
13103 : braced_init_obstack);
13104 : }
13105 :
13106 6911001 : constructor_index
13107 6911001 : = size_binop_loc (input_location,
13108 : PLUS_EXPR, constructor_index, bitsize_one_node);
13109 :
13110 6911001 : if (!value.value)
13111 : /* If we are doing the bookkeeping for an element that was
13112 : directly output as a constructor, we must update
13113 : constructor_unfilled_index. */
13114 0 : constructor_unfilled_index = constructor_index;
13115 : }
13116 :
13117 : /* Handle the sole element allowed in a braced initializer
13118 : for a scalar variable. */
13119 492 : else if (constructor_type != error_mark_node
13120 492 : && constructor_fields == NULL_TREE)
13121 : {
13122 29 : pedwarn_init (loc, 0,
13123 : "excess elements in scalar initializer");
13124 29 : break;
13125 : }
13126 : else
13127 : {
13128 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
13129 463 : if (value.value)
13130 463 : output_init_element (loc, value.value, value.original_type,
13131 : strict_string, constructor_type,
13132 : NULL_TREE, true, implicit,
13133 : braced_init_obstack);
13134 463 : constructor_fields = NULL_TREE;
13135 : }
13136 :
13137 : /* Handle range initializers either at this level or anywhere higher
13138 : in the designator stack. */
13139 9718090 : if (constructor_range_stack)
13140 : {
13141 11564 : struct constructor_range_stack *p, *range_stack;
13142 11564 : int finish = 0;
13143 :
13144 11564 : range_stack = constructor_range_stack;
13145 11564 : constructor_range_stack = 0;
13146 11564 : while (constructor_stack != range_stack->stack)
13147 : {
13148 0 : gcc_assert (constructor_stack->implicit);
13149 0 : process_init_element (loc,
13150 : pop_init_level (loc, 1,
13151 : braced_init_obstack,
13152 : last_init_list_comma),
13153 : true, braced_init_obstack);
13154 : }
13155 325 : for (p = range_stack;
13156 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13157 325 : p = p->prev)
13158 : {
13159 325 : gcc_assert (constructor_stack->implicit);
13160 325 : process_init_element (loc,
13161 : pop_init_level (loc, 1,
13162 : braced_init_obstack,
13163 : last_init_list_comma),
13164 : true, braced_init_obstack);
13165 : }
13166 :
13167 11564 : p->index = size_binop_loc (input_location,
13168 : PLUS_EXPR, p->index, bitsize_one_node);
13169 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13170 11564 : finish = 1;
13171 :
13172 11889 : while (1)
13173 : {
13174 11889 : constructor_index = p->index;
13175 11889 : constructor_fields = p->fields;
13176 11889 : if (finish && p->range_end && p->index == p->range_start)
13177 : {
13178 8 : finish = 0;
13179 8 : p->prev = 0;
13180 : }
13181 11889 : p = p->next;
13182 11889 : if (!p)
13183 : break;
13184 325 : finish_implicit_inits (loc, braced_init_obstack);
13185 325 : push_init_level (loc, 2, braced_init_obstack);
13186 325 : p->stack = constructor_stack;
13187 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13188 33 : p->index = p->range_start;
13189 : }
13190 :
13191 11564 : if (!finish)
13192 11193 : constructor_range_stack = range_stack;
13193 11564 : continue;
13194 11564 : }
13195 :
13196 : break;
13197 : }
13198 :
13199 9708002 : constructor_range_stack = 0;
13200 :
13201 9708002 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13202 : {
13203 212 : gcc_assert (!string_flag && !was_designated);
13204 212 : value.value = raw_data;
13205 212 : raw_data = NULL_TREE;
13206 212 : goto retry;
13207 : }
13208 : }
13209 :
13210 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13211 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13212 : an ASM_EXPR node). */
13213 : tree
13214 196670 : build_asm_stmt (bool is_volatile, tree args)
13215 : {
13216 196670 : if (is_volatile)
13217 156779 : ASM_VOLATILE_P (args) = 1;
13218 196670 : return add_stmt (args);
13219 : }
13220 :
13221 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13222 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13223 : SIMPLE indicates whether there was anything at all after the
13224 : string in the asm expression -- asm("blah") and asm("blah" : )
13225 : are subtly different. We use a ASM_EXPR node to represent this.
13226 : LOC is the location of the asm, and IS_INLINE says whether this
13227 : is asm inline. */
13228 : tree
13229 196722 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13230 : tree clobbers, tree labels, bool simple, bool is_inline)
13231 : {
13232 196722 : tree tail;
13233 196722 : tree args;
13234 196722 : int i;
13235 196722 : const char *constraint;
13236 196722 : const char **oconstraints;
13237 196722 : bool allows_mem, allows_reg, is_inout;
13238 196722 : int ninputs, noutputs;
13239 :
13240 196722 : ninputs = list_length (inputs);
13241 196722 : noutputs = list_length (outputs);
13242 196722 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13243 :
13244 196722 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13245 :
13246 : /* Remove output conversions that change the type but not the mode. */
13247 748608 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13248 : {
13249 355164 : tree output = TREE_VALUE (tail);
13250 :
13251 355164 : output = c_fully_fold (output, false, NULL, true);
13252 :
13253 : /* ??? Really, this should not be here. Users should be using a
13254 : proper lvalue, dammit. But there's a long history of using casts
13255 : in the output operands. In cases like longlong.h, this becomes a
13256 : primitive form of typechecking -- if the cast can be removed, then
13257 : the output operand had a type of the proper width; otherwise we'll
13258 : get an error. Gross, but ... */
13259 355164 : STRIP_NOPS (output);
13260 :
13261 355164 : if (!lvalue_or_else (loc, output, lv_asm))
13262 4 : output = error_mark_node;
13263 :
13264 355164 : if (output != error_mark_node
13265 355164 : && (TREE_READONLY (output)
13266 355157 : || TYPE_READONLY (TREE_TYPE (output))
13267 355157 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13268 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13269 2 : readonly_error (loc, output, lv_asm);
13270 :
13271 355164 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13272 355164 : oconstraints[i] = constraint;
13273 :
13274 355164 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13275 : &allows_mem, &allows_reg, &is_inout,
13276 : nullptr))
13277 : {
13278 : /* If the operand is going to end up in memory,
13279 : mark it addressable. */
13280 355156 : if (!allows_reg && !c_mark_addressable (output))
13281 3 : output = error_mark_node;
13282 1423 : if (!(!allows_reg && allows_mem)
13283 353733 : && output != error_mark_node
13284 708887 : && VOID_TYPE_P (TREE_TYPE (output)))
13285 : {
13286 4 : error_at (loc, "invalid use of void expression");
13287 4 : output = error_mark_node;
13288 : }
13289 355156 : if (allows_reg && current_function_decl == NULL_TREE)
13290 : {
13291 1 : error_at (loc, "constraint allows registers outside of "
13292 : "a function");
13293 1 : output = error_mark_node;
13294 : }
13295 : }
13296 : else
13297 8 : output = error_mark_node;
13298 :
13299 355164 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13300 : {
13301 7 : if (TREE_SIDE_EFFECTS (output))
13302 : {
13303 0 : error_at (loc, "side-effects in output operand outside "
13304 : "of a function");
13305 0 : output = error_mark_node;
13306 : }
13307 : else
13308 : {
13309 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13310 7 : if (addr == error_mark_node)
13311 : output = error_mark_node;
13312 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13313 : {
13314 1 : error_at (loc, "output operand outside of a function is not "
13315 : "constant");
13316 1 : output = error_mark_node;
13317 : }
13318 : }
13319 : }
13320 355157 : else if (output != error_mark_node && strstr (constraint, "-"))
13321 : {
13322 1 : error_at (loc, "%<-%> modifier used inside of a function");
13323 1 : output = error_mark_node;
13324 : }
13325 :
13326 355164 : TREE_VALUE (tail) = output;
13327 : }
13328 :
13329 566164 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13330 : {
13331 369442 : tree input;
13332 :
13333 369442 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13334 369442 : input = TREE_VALUE (tail);
13335 :
13336 369442 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13337 : oconstraints, &allows_mem, &allows_reg,
13338 : nullptr))
13339 : {
13340 : /* If the operand is going to end up in memory,
13341 : mark it addressable. */
13342 369434 : if (!allows_reg && allows_mem)
13343 : {
13344 1372 : input = c_fully_fold (input, false, NULL, true);
13345 :
13346 : /* Strip the nops as we allow this case. FIXME, this really
13347 : should be rejected or made deprecated. */
13348 1372 : STRIP_NOPS (input);
13349 1372 : if (!c_mark_addressable (input))
13350 2 : input = error_mark_node;
13351 : }
13352 : else
13353 : {
13354 368062 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13355 : true, false),
13356 : false, NULL);
13357 :
13358 368062 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13359 : {
13360 4 : error_at (loc, "invalid use of void expression");
13361 4 : input = error_mark_node;
13362 : }
13363 : }
13364 369434 : if (allows_reg && current_function_decl == NULL_TREE)
13365 : {
13366 1 : error_at (loc, "constraint allows registers outside of "
13367 : "a function");
13368 1 : input = error_mark_node;
13369 : }
13370 369434 : if (constraint[0] == ':' && input != error_mark_node)
13371 : {
13372 35 : tree t = input;
13373 35 : STRIP_NOPS (t);
13374 35 : if (TREE_CODE (t) != ADDR_EXPR
13375 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13376 23 : || (VAR_P (TREE_OPERAND (t, 0))
13377 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13378 : {
13379 5 : error_at (loc, "%<:%> constraint operand is not address "
13380 : "of a function or non-automatic variable");
13381 5 : input = error_mark_node;
13382 : }
13383 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13384 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13385 : }
13386 : }
13387 : else
13388 8 : input = error_mark_node;
13389 :
13390 369442 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13391 : {
13392 58 : if (TREE_SIDE_EFFECTS (input))
13393 : {
13394 2 : error_at (loc, "side-effects in input operand outside "
13395 : "of a function");
13396 2 : input = error_mark_node;
13397 : }
13398 : else
13399 : {
13400 56 : tree tem = input;
13401 56 : if (allows_mem && lvalue_p (input))
13402 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13403 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13404 : {
13405 2 : error_at (loc, "input operand outside of a function is not "
13406 : "constant");
13407 2 : input = error_mark_node;
13408 : }
13409 : }
13410 : }
13411 369384 : else if (input != error_mark_node && strstr (constraint, "-"))
13412 : {
13413 3 : error_at (loc, "%<-%> modifier used inside of a function");
13414 3 : input = error_mark_node;
13415 : }
13416 :
13417 369442 : TREE_VALUE (tail) = input;
13418 : }
13419 :
13420 196722 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13421 :
13422 : /* asm statements without outputs, including simple ones, are treated
13423 : as volatile. */
13424 196722 : ASM_BASIC_P (args) = simple;
13425 196722 : ASM_VOLATILE_P (args) = (noutputs == 0);
13426 196722 : ASM_INLINE_P (args) = is_inline;
13427 :
13428 196722 : return args;
13429 : }
13430 :
13431 : /* Generate a goto statement to LABEL. LOC is the location of the
13432 : GOTO. */
13433 :
13434 : tree
13435 83672 : c_finish_goto_label (location_t loc, tree label)
13436 : {
13437 83672 : tree decl = lookup_label_for_goto (loc, label);
13438 83672 : if (!decl)
13439 : return NULL_TREE;
13440 83672 : TREE_USED (decl) = 1;
13441 83672 : mark_decl_used (decl, false);
13442 83672 : {
13443 83672 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13444 83672 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13445 83672 : SET_EXPR_LOCATION (t, loc);
13446 83672 : return add_stmt (t);
13447 : }
13448 : }
13449 :
13450 : /* Generate a computed goto statement to EXPR. LOC is the location of
13451 : the GOTO. */
13452 :
13453 : tree
13454 1073 : c_finish_goto_ptr (location_t loc, c_expr val)
13455 : {
13456 1073 : tree expr = val.value;
13457 1073 : tree t;
13458 1073 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13459 1073 : if (expr != error_mark_node
13460 1072 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13461 1078 : && !null_pointer_constant_p (expr))
13462 : {
13463 2 : error_at (val.get_location (),
13464 : "computed goto must be pointer type");
13465 2 : expr = build_zero_cst (ptr_type_node);
13466 : }
13467 1073 : expr = c_fully_fold (expr, false, NULL);
13468 1073 : expr = convert (ptr_type_node, expr);
13469 1073 : t = build1 (GOTO_EXPR, void_type_node, expr);
13470 1073 : SET_EXPR_LOCATION (t, loc);
13471 1073 : return add_stmt (t);
13472 : }
13473 :
13474 : /* Generate a C `return' statement. RETVAL is the expression for what
13475 : to return, or a null pointer for `return;' with no value. LOC is
13476 : the location of the return statement, or the location of the expression,
13477 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13478 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13479 : attribute. */
13480 :
13481 : tree
13482 35922987 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13483 : {
13484 35922987 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13485 35922987 : bool no_warning = false;
13486 35922987 : bool npc = false;
13487 :
13488 : /* Use the expansion point to handle cases such as returning NULL
13489 : in a function returning void. */
13490 35922987 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13491 :
13492 35922987 : if (TREE_THIS_VOLATILE (current_function_decl))
13493 23 : warning_at (xloc, 0,
13494 : "function declared %<noreturn%> has a %<return%> statement");
13495 :
13496 35922987 : set_musttail_on_return (retval, xloc, musttail_p);
13497 :
13498 35922987 : if (retval)
13499 : {
13500 35901197 : tree semantic_type = NULL_TREE;
13501 35901197 : npc = null_pointer_constant_p (retval);
13502 35901197 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13503 : {
13504 101 : semantic_type = TREE_TYPE (retval);
13505 101 : retval = TREE_OPERAND (retval, 0);
13506 : }
13507 35901197 : retval = c_fully_fold (retval, false, NULL);
13508 35901197 : if (semantic_type
13509 35901197 : && valtype != NULL_TREE
13510 101 : && TREE_CODE (valtype) != VOID_TYPE)
13511 101 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13512 : }
13513 :
13514 35901197 : if (!retval)
13515 : {
13516 21790 : current_function_returns_null = 1;
13517 21790 : if ((warn_return_type >= 0 || flag_isoc99)
13518 21665 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13519 : {
13520 46 : no_warning = true;
13521 47 : if (emit_diagnostic (flag_isoc99
13522 : ? diagnostics::kind::permerror
13523 : : diagnostics::kind::warning,
13524 46 : loc, OPT_Wreturn_mismatch,
13525 : "%<return%> with no value,"
13526 : " in function returning non-void"))
13527 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13528 : "declared here");
13529 : }
13530 : }
13531 35901197 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13532 : {
13533 324 : current_function_returns_null = 1;
13534 324 : bool warned_here;
13535 324 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13536 64 : warned_here = permerror_opt
13537 64 : (xloc, OPT_Wreturn_mismatch,
13538 : "%<return%> with a value, in function returning void");
13539 : else
13540 260 : warned_here = pedwarn
13541 260 : (xloc, OPT_Wpedantic, "ISO C forbids "
13542 : "%<return%> with expression, in function returning void");
13543 324 : if (warned_here)
13544 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13545 : "declared here");
13546 : }
13547 : else
13548 : {
13549 35900873 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13550 : retval, origtype, ic_return,
13551 : npc, NULL_TREE, NULL_TREE, 0);
13552 35900873 : tree res = DECL_RESULT (current_function_decl);
13553 35900873 : tree inner;
13554 35900873 : bool save;
13555 :
13556 35900873 : current_function_returns_value = 1;
13557 35900873 : if (t == error_mark_node)
13558 : {
13559 : /* Suppress -Wreturn-type for this function. */
13560 299 : if (warn_return_type)
13561 298 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13562 : return NULL_TREE;
13563 : }
13564 :
13565 35900574 : save = in_late_binary_op;
13566 71790854 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13567 35889475 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13568 71788746 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13569 336244 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13570 336244 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13571 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13572 12402 : in_late_binary_op = true;
13573 35900574 : inner = t = convert (TREE_TYPE (res), t);
13574 35900574 : in_late_binary_op = save;
13575 :
13576 : /* Strip any conversions, additions, and subtractions, and see if
13577 : we are returning the address of a local variable. Warn if so. */
13578 39217079 : while (1)
13579 : {
13580 39217079 : switch (TREE_CODE (inner))
13581 : {
13582 3314860 : CASE_CONVERT:
13583 3314860 : case NON_LVALUE_EXPR:
13584 3314860 : case PLUS_EXPR:
13585 3314860 : case POINTER_PLUS_EXPR:
13586 3314860 : inner = TREE_OPERAND (inner, 0);
13587 3314860 : continue;
13588 :
13589 1650 : case MINUS_EXPR:
13590 : /* If the second operand of the MINUS_EXPR has a pointer
13591 : type (or is converted from it), this may be valid, so
13592 : don't give a warning. */
13593 1650 : {
13594 1650 : tree op1 = TREE_OPERAND (inner, 1);
13595 :
13596 3573 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13597 3851 : && (CONVERT_EXPR_P (op1)
13598 1645 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13599 278 : op1 = TREE_OPERAND (op1, 0);
13600 :
13601 1650 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13602 : break;
13603 :
13604 1645 : inner = TREE_OPERAND (inner, 0);
13605 1645 : continue;
13606 1645 : }
13607 :
13608 2991 : case ADDR_EXPR:
13609 2991 : inner = TREE_OPERAND (inner, 0);
13610 :
13611 2991 : while (REFERENCE_CLASS_P (inner)
13612 3900 : && !INDIRECT_REF_P (inner))
13613 909 : inner = TREE_OPERAND (inner, 0);
13614 :
13615 2991 : if (DECL_P (inner)
13616 1661 : && !DECL_EXTERNAL (inner)
13617 1002 : && DECL_CONTEXT (inner) == current_function_decl
13618 3214 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13619 : {
13620 183 : if (TREE_CODE (inner) == LABEL_DECL)
13621 4 : warning_at (loc, OPT_Wreturn_local_addr,
13622 : "function returns address of label");
13623 179 : else if (TREE_CODE (inner) == FUNCTION_DECL
13624 179 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13625 10 : || !DECL_INITIAL (inner)))
13626 14 : warning_at (loc, OPT_Wreturn_local_addr,
13627 : "function returns address of nested function "
13628 : "referencing local context");
13629 165 : else if (TREE_CODE (inner) != FUNCTION_DECL
13630 156 : && !TREE_STATIC (inner))
13631 : {
13632 9 : warning_at (loc, OPT_Wreturn_local_addr,
13633 : "function returns address of local variable");
13634 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13635 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13636 : }
13637 : }
13638 : break;
13639 :
13640 : default:
13641 : break;
13642 3314860 : }
13643 :
13644 35900574 : break;
13645 : }
13646 :
13647 35900574 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13648 35900574 : SET_EXPR_LOCATION (retval, loc);
13649 :
13650 35900574 : if (warn_sequence_point)
13651 3040710 : verify_sequence_points (retval);
13652 : }
13653 :
13654 35922688 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13655 35922688 : if (no_warning)
13656 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13657 35922688 : return add_stmt (ret_stmt);
13658 : }
13659 :
13660 : struct c_switch {
13661 : /* The SWITCH_STMT being built. */
13662 : tree switch_stmt;
13663 :
13664 : /* The original type of the testing expression, i.e. before the
13665 : default conversion is applied. */
13666 : tree orig_type;
13667 :
13668 : /* A splay-tree mapping the low element of a case range to the high
13669 : element, or NULL_TREE if there is no high element. Used to
13670 : determine whether or not a new case label duplicates an old case
13671 : label. We need a tree, rather than simply a hash table, because
13672 : of the GNU case range extension. */
13673 : splay_tree cases;
13674 :
13675 : /* The bindings at the point of the switch. This is used for
13676 : warnings crossing decls when branching to a case label. */
13677 : struct c_spot_bindings *bindings;
13678 :
13679 : /* Whether the switch includes any break statements. */
13680 : bool break_stmt_seen_p;
13681 :
13682 : /* The next node on the stack. */
13683 : struct c_switch *next;
13684 :
13685 : /* Remember whether the controlling expression had boolean type
13686 : before integer promotions for the sake of -Wswitch-bool. */
13687 : bool bool_cond_p;
13688 : };
13689 :
13690 : /* A stack of the currently active switch statements. The innermost
13691 : switch statement is on the top of the stack. There is no need to
13692 : mark the stack for garbage collection because it is only active
13693 : during the processing of the body of a function, and we never
13694 : collect at that point. */
13695 :
13696 : struct c_switch *c_switch_stack;
13697 :
13698 : /* Start a C switch statement, testing expression EXP. Return the new
13699 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13700 : SWITCH_COND_LOC is the location of the switch's condition.
13701 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13702 :
13703 : tree
13704 38874 : c_start_switch (location_t switch_loc,
13705 : location_t switch_cond_loc,
13706 : tree exp, bool explicit_cast_p, tree switch_name)
13707 : {
13708 38874 : tree orig_type = error_mark_node;
13709 38874 : bool bool_cond_p = false;
13710 38874 : struct c_switch *cs;
13711 :
13712 38874 : if (exp != error_mark_node)
13713 : {
13714 38831 : orig_type = TREE_TYPE (exp);
13715 :
13716 38831 : if (!INTEGRAL_TYPE_P (orig_type))
13717 : {
13718 12 : if (orig_type != error_mark_node)
13719 : {
13720 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13721 12 : orig_type = error_mark_node;
13722 : }
13723 12 : exp = integer_zero_node;
13724 : }
13725 : else
13726 : {
13727 38819 : tree type = TYPE_MAIN_VARIANT (orig_type);
13728 38819 : tree e = exp;
13729 :
13730 : /* Warn if the condition has boolean value. */
13731 38822 : while (TREE_CODE (e) == COMPOUND_EXPR)
13732 3 : e = TREE_OPERAND (e, 1);
13733 :
13734 38794 : if ((C_BOOLEAN_TYPE_P (type)
13735 38794 : || truth_value_p (TREE_CODE (e)))
13736 : /* Explicit cast to int suppresses this warning. */
13737 38841 : && !(TREE_CODE (type) == INTEGER_TYPE
13738 : && explicit_cast_p))
13739 : bool_cond_p = true;
13740 :
13741 38819 : if (!in_system_header_at (input_location)
13742 38819 : && (type == long_integer_type_node
13743 13213 : || type == long_unsigned_type_node))
13744 306 : warning_at (switch_cond_loc,
13745 306 : OPT_Wtraditional, "%<long%> switch expression not "
13746 : "converted to %<int%> in ISO C");
13747 :
13748 38819 : exp = c_fully_fold (exp, false, NULL);
13749 38819 : exp = default_conversion (exp);
13750 :
13751 38819 : if (warn_sequence_point)
13752 6994 : verify_sequence_points (exp);
13753 : }
13754 : }
13755 :
13756 : /* Add this new SWITCH_STMT to the stack. */
13757 38874 : cs = XNEW (struct c_switch);
13758 38874 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13759 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13760 38874 : cs->orig_type = orig_type;
13761 38874 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13762 38874 : cs->bindings = c_get_switch_bindings ();
13763 38874 : cs->break_stmt_seen_p = false;
13764 38874 : cs->bool_cond_p = bool_cond_p;
13765 38874 : cs->next = c_switch_stack;
13766 38874 : c_switch_stack = cs;
13767 :
13768 38874 : return add_stmt (cs->switch_stmt);
13769 : }
13770 :
13771 : /* Process a case label at location LOC, with attributes ATTRS. */
13772 :
13773 : tree
13774 1039791 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13775 : {
13776 1039791 : tree label = NULL_TREE;
13777 :
13778 1039791 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13779 : {
13780 87 : low_value = c_fully_fold (low_value, false, NULL);
13781 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13782 9 : pedwarn (loc, OPT_Wpedantic,
13783 : "case label is not an integer constant expression");
13784 : }
13785 :
13786 1039791 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13787 : {
13788 0 : high_value = c_fully_fold (high_value, false, NULL);
13789 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13790 0 : pedwarn (input_location, OPT_Wpedantic,
13791 : "case label is not an integer constant expression");
13792 : }
13793 :
13794 1039791 : if (c_switch_stack == NULL)
13795 : {
13796 3 : if (low_value)
13797 2 : error_at (loc, "case label not within a switch statement");
13798 : else
13799 1 : error_at (loc, "%<default%> label not within a switch statement");
13800 : return NULL_TREE;
13801 : }
13802 :
13803 1039788 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13804 1039788 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13805 : loc))
13806 : return NULL_TREE;
13807 :
13808 1039776 : label = c_add_case_label (loc, c_switch_stack->cases,
13809 1039776 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13810 : low_value, high_value, attrs);
13811 1039776 : if (label == error_mark_node)
13812 144 : label = NULL_TREE;
13813 : return label;
13814 : }
13815 :
13816 : /* Finish the switch statement. TYPE is the original type of the
13817 : controlling expression of the switch, or NULL_TREE. */
13818 :
13819 : void
13820 38874 : c_finish_switch (tree body, tree type)
13821 : {
13822 38874 : struct c_switch *cs = c_switch_stack;
13823 38874 : location_t switch_location;
13824 :
13825 38874 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13826 :
13827 : /* Emit warnings as needed. */
13828 38874 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13829 45799 : c_do_switch_warnings (cs->cases, switch_location,
13830 6925 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13831 38874 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13832 38874 : if (c_switch_covers_all_cases_p (cs->cases,
13833 38874 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13834 34866 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13835 38874 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13836 :
13837 : /* Pop the stack. */
13838 38874 : c_switch_stack = cs->next;
13839 38874 : splay_tree_delete (cs->cases);
13840 38874 : c_release_switch_bindings (cs->bindings);
13841 38874 : XDELETE (cs);
13842 38874 : }
13843 :
13844 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13845 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13846 : may be null. */
13847 :
13848 : void
13849 1294459 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13850 : tree else_block)
13851 : {
13852 1294459 : tree stmt;
13853 :
13854 1294459 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13855 1294459 : SET_EXPR_LOCATION (stmt, if_locus);
13856 1294459 : add_stmt (stmt);
13857 1294459 : }
13858 :
13859 : tree
13860 192016 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13861 : {
13862 : /* In switch statements break is sometimes stylistically used after
13863 : a return statement. This can lead to spurious warnings about
13864 : control reaching the end of a non-void function when it is
13865 : inlined. Note that we are calling block_may_fallthru with
13866 : language specific tree nodes; this works because
13867 : block_may_fallthru returns true when given something it does not
13868 : understand. */
13869 192016 : bool skip = !block_may_fallthru (cur_stmt_list);
13870 :
13871 192016 : if (is_break)
13872 178615 : switch (in_statement & ~IN_NAMED_STMT)
13873 : {
13874 9 : case 0:
13875 9 : error_at (loc, "break statement not within loop or switch");
13876 9 : return NULL_TREE;
13877 4 : case IN_OMP_BLOCK:
13878 4 : error_at (loc, "invalid exit from OpenMP structured block");
13879 4 : return NULL_TREE;
13880 12 : case IN_OMP_FOR:
13881 12 : error_at (loc, "break statement used with OpenMP for loop");
13882 12 : return NULL_TREE;
13883 : case IN_ITERATION_STMT:
13884 : case IN_OBJC_FOREACH:
13885 : break;
13886 162493 : default:
13887 162493 : gcc_assert (in_statement & IN_SWITCH_STMT);
13888 162493 : c_switch_stack->break_stmt_seen_p = true;
13889 162493 : break;
13890 : }
13891 : else
13892 13401 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13893 : {
13894 7 : case 0:
13895 7 : error_at (loc, "continue statement not within a loop");
13896 7 : return NULL_TREE;
13897 4 : case IN_OMP_BLOCK:
13898 4 : error_at (loc, "invalid exit from OpenMP structured block");
13899 4 : return NULL_TREE;
13900 : case IN_ITERATION_STMT:
13901 : case IN_OMP_FOR:
13902 : case IN_OBJC_FOREACH:
13903 : break;
13904 0 : default:
13905 0 : gcc_unreachable ();
13906 : }
13907 :
13908 191980 : if (skip)
13909 : return NULL_TREE;
13910 191444 : else if ((in_statement & IN_OBJC_FOREACH)
13911 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13912 0 : && name == NULL_TREE)
13913 : {
13914 : /* The foreach expander produces low-level code using gotos instead
13915 : of a structured loop construct. */
13916 0 : gcc_assert (label);
13917 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13918 : }
13919 191541 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13920 : {
13921 0 : label = DECL_CHAIN (name);
13922 0 : if (!is_break)
13923 0 : label = DECL_CHAIN (label);
13924 : /* Foreach expander from some outer level. */
13925 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13926 : }
13927 204834 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13928 191444 : name));
13929 : }
13930 :
13931 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13932 :
13933 : static void
13934 6627042 : emit_side_effect_warnings (location_t loc, tree expr)
13935 : {
13936 6627042 : maybe_warn_nodiscard (loc, expr);
13937 6627042 : if (!warn_unused_value)
13938 : return;
13939 1320218 : if (expr == error_mark_node)
13940 : ;
13941 1320143 : else if (!TREE_SIDE_EFFECTS (expr))
13942 : {
13943 6983 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13944 6983 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13945 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13946 : }
13947 1313160 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13948 : {
13949 : tree r = expr;
13950 : location_t cloc = loc;
13951 12107 : while (TREE_CODE (r) == COMPOUND_EXPR)
13952 : {
13953 6069 : if (EXPR_HAS_LOCATION (r))
13954 5527 : cloc = EXPR_LOCATION (r);
13955 6069 : r = TREE_OPERAND (r, 1);
13956 : }
13957 6038 : if (!TREE_SIDE_EFFECTS (r)
13958 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13959 24 : && !CONVERT_EXPR_P (r)
13960 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13961 6050 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13962 8 : warning_at (cloc, OPT_Wunused_value,
13963 : "right-hand operand of comma expression has no effect");
13964 : }
13965 : else
13966 1307122 : warn_if_unused_value (expr, loc);
13967 : }
13968 :
13969 : /* Process an expression as if it were a complete statement. Emit
13970 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13971 : statement. */
13972 :
13973 : tree
13974 6503330 : c_process_expr_stmt (location_t loc, tree expr)
13975 : {
13976 6503330 : tree exprv;
13977 :
13978 6503330 : if (!expr)
13979 : return NULL_TREE;
13980 :
13981 6498730 : expr = c_fully_fold (expr, false, NULL);
13982 :
13983 6498730 : if (warn_sequence_point)
13984 1315499 : verify_sequence_points (expr);
13985 :
13986 6498730 : if (TREE_TYPE (expr) != error_mark_node
13987 6496003 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13988 6498730 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13989 0 : error_at (loc, "expression statement has incomplete type");
13990 :
13991 : /* If we're not processing a statement expression, warn about unused values.
13992 : Warnings for statement expressions will be emitted later, once we figure
13993 : out which is the result. */
13994 6498730 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13995 6498730 : && (warn_unused_value || warn_unused_result))
13996 6375883 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13997 :
13998 6498729 : exprv = expr;
13999 6540745 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
14000 42016 : exprv = TREE_OPERAND (exprv, 1);
14001 6510758 : while (CONVERT_EXPR_P (exprv))
14002 12029 : exprv = TREE_OPERAND (exprv, 0);
14003 6498729 : if (DECL_P (exprv)
14004 6499321 : || handled_component_p (exprv)
14005 12977184 : || TREE_CODE (exprv) == ADDR_EXPR)
14006 20866 : mark_exp_read (exprv);
14007 :
14008 : /* If the expression is not of a type to which we cannot assign a line
14009 : number, wrap the thing in a no-op NOP_EXPR. */
14010 6498729 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
14011 : {
14012 14903 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
14013 14903 : SET_EXPR_LOCATION (expr, loc);
14014 : }
14015 :
14016 : return expr;
14017 : }
14018 :
14019 : /* Emit an expression as a statement. LOC is the location of the
14020 : expression. */
14021 :
14022 : tree
14023 6191321 : c_finish_expr_stmt (location_t loc, tree expr)
14024 : {
14025 6191321 : if (expr)
14026 6176858 : return add_stmt (c_process_expr_stmt (loc, expr));
14027 : else
14028 : return NULL;
14029 : }
14030 :
14031 : /* Do the opposite and emit a statement as an expression. To begin,
14032 : create a new binding level and return it. */
14033 :
14034 : tree
14035 35444 : c_begin_stmt_expr (void)
14036 : {
14037 35444 : tree ret;
14038 :
14039 : /* We must force a BLOCK for this level so that, if it is not expanded
14040 : later, there is a way to turn off the entire subtree of blocks that
14041 : are contained in it. */
14042 35444 : keep_next_level ();
14043 35444 : ret = c_begin_compound_stmt (true);
14044 :
14045 35444 : c_bindings_start_stmt_expr (c_switch_stack == NULL
14046 : ? NULL
14047 : : c_switch_stack->bindings);
14048 :
14049 : /* Mark the current statement list as belonging to a statement list. */
14050 35444 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
14051 :
14052 35444 : return ret;
14053 : }
14054 :
14055 : /* LOC is the location of the compound statement to which this body
14056 : belongs. */
14057 :
14058 : tree
14059 35444 : c_finish_stmt_expr (location_t loc, tree body)
14060 : {
14061 35444 : tree last, type, tmp, val;
14062 35444 : tree *last_p;
14063 :
14064 35444 : body = c_end_compound_stmt (loc, body, true);
14065 :
14066 35444 : c_bindings_end_stmt_expr (c_switch_stack == NULL
14067 : ? NULL
14068 : : c_switch_stack->bindings);
14069 :
14070 : /* Locate the last statement in BODY. See c_end_compound_stmt
14071 : about always returning a BIND_EXPR. */
14072 35444 : last_p = &BIND_EXPR_BODY (body);
14073 35444 : last = BIND_EXPR_BODY (body);
14074 :
14075 35444 : continue_searching:
14076 35444 : if (TREE_CODE (last) == STATEMENT_LIST)
14077 : {
14078 26608 : tree_stmt_iterator l = tsi_last (last);
14079 :
14080 26614 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
14081 6 : tsi_prev (&l);
14082 :
14083 : /* This can happen with degenerate cases like ({ }). No value. */
14084 26608 : if (tsi_end_p (l))
14085 389 : return body;
14086 :
14087 : /* If we're supposed to generate side effects warnings, process
14088 : all of the statements except the last. */
14089 26219 : if (warn_unused_value || warn_unused_result)
14090 : {
14091 26219 : for (tree_stmt_iterator i = tsi_start (last);
14092 277378 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
14093 : {
14094 251159 : location_t tloc;
14095 251159 : tree t = tsi_stmt (i);
14096 :
14097 251159 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
14098 251159 : emit_side_effect_warnings (tloc, t);
14099 : }
14100 : }
14101 26219 : last_p = tsi_stmt_ptr (l);
14102 26219 : last = *last_p;
14103 : }
14104 :
14105 : /* If the end of the list is exception related, then the list was split
14106 : by a call to push_cleanup. Continue searching. */
14107 35055 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
14108 35055 : || TREE_CODE (last) == TRY_CATCH_EXPR)
14109 : {
14110 0 : last_p = &TREE_OPERAND (last, 0);
14111 0 : last = *last_p;
14112 0 : goto continue_searching;
14113 : }
14114 :
14115 35055 : if (last == error_mark_node)
14116 : return last;
14117 :
14118 : /* In the case that the BIND_EXPR is not necessary, return the
14119 : expression out from inside it. */
14120 35046 : if ((last == BIND_EXPR_BODY (body)
14121 : /* Skip nested debug stmts. */
14122 26218 : || last == expr_first (BIND_EXPR_BODY (body)))
14123 46110 : && BIND_EXPR_VARS (body) == NULL)
14124 : {
14125 : /* Even if this looks constant, do not allow it in a constant
14126 : expression. */
14127 11052 : last = c_wrap_maybe_const (last, true);
14128 : /* Do not warn if the return value of a statement expression is
14129 : unused. */
14130 11052 : suppress_warning (last, OPT_Wunused);
14131 11052 : return last;
14132 : }
14133 :
14134 : /* Extract the type of said expression. */
14135 23994 : type = TREE_TYPE (last);
14136 :
14137 : /* If we're not returning a value at all, then the BIND_EXPR that
14138 : we already have is a fine expression to return. */
14139 23994 : if (!type || VOID_TYPE_P (type))
14140 : return body;
14141 :
14142 : /* Now that we've located the expression containing the value, it seems
14143 : silly to make voidify_wrapper_expr repeat the process. Create a
14144 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14145 18666 : tmp = create_tmp_var_raw (type);
14146 :
14147 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14148 : tree_expr_nonnegative_p giving up immediately. */
14149 18666 : val = last;
14150 18666 : if (TREE_CODE (val) == NOP_EXPR
14151 18666 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14152 3799 : val = TREE_OPERAND (val, 0);
14153 :
14154 18666 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14155 18666 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14156 :
14157 18666 : {
14158 18666 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14159 18666 : SET_EXPR_LOCATION (t, loc);
14160 18666 : return t;
14161 : }
14162 : }
14163 :
14164 : /* Begin and end compound statements. This is as simple as pushing
14165 : and popping new statement lists from the tree. */
14166 :
14167 : tree
14168 42670068 : c_begin_compound_stmt (bool do_scope)
14169 : {
14170 42670068 : tree stmt = push_stmt_list ();
14171 42670068 : if (do_scope)
14172 42573872 : push_scope ();
14173 42670068 : return stmt;
14174 : }
14175 :
14176 : /* End a compound statement. STMT is the statement. LOC is the
14177 : location of the compound statement-- this is usually the location
14178 : of the opening brace. */
14179 :
14180 : tree
14181 42670067 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14182 : {
14183 42670067 : tree block = NULL;
14184 :
14185 42670067 : if (do_scope)
14186 : {
14187 42573871 : if (c_dialect_objc ())
14188 0 : objc_clear_super_receiver ();
14189 42573871 : block = pop_scope ();
14190 : }
14191 :
14192 42670067 : stmt = pop_stmt_list (stmt);
14193 42670067 : stmt = c_build_bind_expr (loc, block, stmt);
14194 :
14195 : /* If this compound statement is nested immediately inside a statement
14196 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14197 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14198 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14199 : was really last. */
14200 85340134 : if (building_stmt_list_p ()
14201 42669982 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14202 42792793 : && TREE_CODE (stmt) != BIND_EXPR)
14203 : {
14204 120983 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14205 120983 : TREE_SIDE_EFFECTS (stmt) = 1;
14206 120983 : SET_EXPR_LOCATION (stmt, loc);
14207 : }
14208 :
14209 42670067 : return stmt;
14210 : }
14211 :
14212 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14213 : when the current scope is exited. EH_ONLY is true when this is not
14214 : meant to apply to normal control flow transfer. */
14215 :
14216 : void
14217 140 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14218 : {
14219 140 : enum tree_code code;
14220 140 : tree stmt, list;
14221 140 : bool stmt_expr;
14222 :
14223 140 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14224 140 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14225 140 : add_stmt (stmt);
14226 140 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14227 140 : list = push_stmt_list ();
14228 140 : TREE_OPERAND (stmt, 0) = list;
14229 140 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14230 140 : }
14231 :
14232 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14233 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14234 :
14235 : static tree
14236 102136 : build_vec_cmp (tree_code code, tree type,
14237 : tree arg0, tree arg1)
14238 : {
14239 102136 : tree zero_vec = build_zero_cst (type);
14240 102136 : tree minus_one_vec = build_minus_one_cst (type);
14241 102136 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14242 102136 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14243 102136 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14244 : }
14245 :
14246 : /* Possibly warn about an address of OP never being NULL in a comparison
14247 : operation CODE involving null. */
14248 :
14249 : static void
14250 52882 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14251 : {
14252 : /* Prevent warnings issued for macro expansion. */
14253 52882 : if (!warn_address
14254 30816 : || warning_suppressed_p (op, OPT_Waddress)
14255 83318 : || from_macro_expansion_at (loc))
14256 : return;
14257 :
14258 27676 : if (TREE_CODE (op) == NOP_EXPR)
14259 : {
14260 : /* Allow casts to intptr_t to suppress the warning. */
14261 1135 : tree type = TREE_TYPE (op);
14262 1135 : if (TREE_CODE (type) == INTEGER_TYPE)
14263 : return;
14264 1135 : op = TREE_OPERAND (op, 0);
14265 : }
14266 :
14267 27676 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14268 : {
14269 : /* Allow a cast to void* to suppress the warning. */
14270 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14271 24 : if (VOID_TYPE_P (type))
14272 : return;
14273 :
14274 : /* Adding any value to a null pointer, including zero, is undefined
14275 : in C. This includes the expression &p[0] where p is the null
14276 : pointer, although &p[0] will have been folded to p by this point
14277 : and so not diagnosed. */
14278 24 : if (code == EQ_EXPR)
14279 22 : warning_at (loc, OPT_Waddress,
14280 : "the comparison will always evaluate as %<false%> "
14281 : "for the pointer operand in %qE must not be NULL",
14282 : op);
14283 : else
14284 2 : warning_at (loc, OPT_Waddress,
14285 : "the comparison will always evaluate as %<true%> "
14286 : "for the pointer operand in %qE must not be NULL",
14287 : op);
14288 :
14289 : return;
14290 : }
14291 :
14292 27652 : if (TREE_CODE (op) != ADDR_EXPR)
14293 : return;
14294 :
14295 254 : op = TREE_OPERAND (op, 0);
14296 :
14297 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14298 254 : || TREE_CODE (op) == REALPART_EXPR)
14299 : {
14300 : /* The address of either complex part may not be null. */
14301 14 : if (code == EQ_EXPR)
14302 9 : warning_at (loc, OPT_Waddress,
14303 : "the comparison will always evaluate as %<false%> "
14304 : "for the address of %qE will never be NULL",
14305 : op);
14306 : else
14307 5 : warning_at (loc, OPT_Waddress,
14308 : "the comparison will always evaluate as %<true%> "
14309 : "for the address of %qE will never be NULL",
14310 : op);
14311 : return;
14312 : }
14313 :
14314 : /* Set to true in the loop below if OP dereferences is operand.
14315 : In such a case the ultimate target need not be a decl for
14316 : the null [in]equality test to be constant. */
14317 : bool deref = false;
14318 :
14319 : /* Get the outermost array or object, or member. */
14320 294 : while (handled_component_p (op))
14321 : {
14322 72 : if (TREE_CODE (op) == COMPONENT_REF)
14323 : {
14324 : /* Get the member (its address is never null). */
14325 18 : op = TREE_OPERAND (op, 1);
14326 18 : break;
14327 : }
14328 :
14329 : /* Get the outer array/object to refer to in the warning. */
14330 54 : op = TREE_OPERAND (op, 0);
14331 54 : deref = true;
14332 : }
14333 :
14334 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14335 371 : || from_macro_expansion_at (loc))
14336 : return;
14337 :
14338 131 : bool w;
14339 131 : if (code == EQ_EXPR)
14340 93 : w = warning_at (loc, OPT_Waddress,
14341 : "the comparison will always evaluate as %<false%> "
14342 : "for the address of %qE will never be NULL",
14343 : op);
14344 : else
14345 38 : w = warning_at (loc, OPT_Waddress,
14346 : "the comparison will always evaluate as %<true%> "
14347 : "for the address of %qE will never be NULL",
14348 : op);
14349 :
14350 131 : if (w && DECL_P (op))
14351 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14352 : }
14353 :
14354 : /* Build a binary-operation expression without default conversions.
14355 : CODE is the kind of expression to build.
14356 : LOCATION is the operator's location.
14357 : This function differs from `build' in several ways:
14358 : the data type of the result is computed and recorded in it,
14359 : warnings are generated if arg data types are invalid,
14360 : special handling for addition and subtraction of pointers is known,
14361 : and some optimization is done (operations on narrow ints
14362 : are done in the narrower type when that gives the same result).
14363 : Constant folding is also done before the result is returned.
14364 :
14365 : Note that the operands will never have enumeral types, or function
14366 : or array types, because either they will have the default conversions
14367 : performed or they have both just been converted to some other type in which
14368 : the arithmetic is to be done. */
14369 :
14370 : tree
14371 17696965 : build_binary_op (location_t location, enum tree_code code,
14372 : tree orig_op0, tree orig_op1, bool convert_p)
14373 : {
14374 17696965 : tree type0, type1, orig_type0, orig_type1;
14375 17696965 : tree eptype;
14376 17696965 : enum tree_code code0, code1;
14377 17696965 : tree op0, op1;
14378 17696965 : tree ret = error_mark_node;
14379 17696965 : const char *invalid_op_diag;
14380 17696965 : bool op0_int_operands, op1_int_operands;
14381 17696965 : bool int_const, int_const_or_overflow, int_operands;
14382 :
14383 : /* Expression code to give to the expression when it is built.
14384 : Normally this is CODE, which is what the caller asked for,
14385 : but in some special cases we change it. */
14386 17696965 : enum tree_code resultcode = code;
14387 :
14388 : /* Data type in which the computation is to be performed.
14389 : In the simplest cases this is the common type of the arguments. */
14390 17696965 : tree result_type = NULL;
14391 :
14392 : /* When the computation is in excess precision, the type of the
14393 : final EXCESS_PRECISION_EXPR. */
14394 17696965 : tree semantic_result_type = NULL;
14395 :
14396 : /* Nonzero means operands have already been type-converted
14397 : in whatever way is necessary.
14398 : Zero means they need to be converted to RESULT_TYPE. */
14399 17696965 : int converted = 0;
14400 :
14401 : /* Nonzero means create the expression with this type, rather than
14402 : RESULT_TYPE. */
14403 17696965 : tree build_type = NULL_TREE;
14404 :
14405 : /* Nonzero means after finally constructing the expression
14406 : convert it to this type. */
14407 17696965 : tree final_type = NULL_TREE;
14408 :
14409 : /* Nonzero if this is an operation like MIN or MAX which can
14410 : safely be computed in short if both args are promoted shorts.
14411 : Also implies COMMON.
14412 : -1 indicates a bitwise operation; this makes a difference
14413 : in the exact conditions for when it is safe to do the operation
14414 : in a narrower mode. */
14415 17696965 : int shorten = 0;
14416 :
14417 : /* Nonzero if this is a comparison operation;
14418 : if both args are promoted shorts, compare the original shorts.
14419 : Also implies COMMON. */
14420 17696965 : int short_compare = 0;
14421 :
14422 : /* Nonzero if this is a right-shift operation, which can be computed on the
14423 : original short and then promoted if the operand is a promoted short. */
14424 17696965 : int short_shift = 0;
14425 :
14426 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14427 17696965 : int common = 0;
14428 :
14429 : /* True means types are compatible as far as ObjC is concerned. */
14430 17696965 : bool objc_ok;
14431 :
14432 : /* True means this is an arithmetic operation that may need excess
14433 : precision. */
14434 17696965 : bool may_need_excess_precision;
14435 :
14436 : /* True means this is a boolean operation that converts both its
14437 : operands to truth-values. */
14438 17696965 : bool boolean_op = false;
14439 :
14440 : /* Remember whether we're doing / or %. */
14441 17696965 : bool doing_div_or_mod = false;
14442 :
14443 : /* Remember whether we're doing << or >>. */
14444 17696965 : bool doing_shift = false;
14445 :
14446 : /* Tree holding instrumentation expression. */
14447 17696965 : tree instrument_expr = NULL;
14448 :
14449 17696965 : if (location == UNKNOWN_LOCATION)
14450 80 : location = input_location;
14451 :
14452 17696965 : op0 = orig_op0;
14453 17696965 : op1 = orig_op1;
14454 :
14455 17696965 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14456 : if (op0_int_operands)
14457 8863131 : op0 = remove_c_maybe_const_expr (op0);
14458 17696965 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14459 : if (op1_int_operands)
14460 12195707 : op1 = remove_c_maybe_const_expr (op1);
14461 29892672 : int_operands = (op0_int_operands && op1_int_operands);
14462 12195707 : if (int_operands)
14463 : {
14464 17365701 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14465 8698021 : && TREE_CODE (orig_op1) == INTEGER_CST);
14466 8667680 : int_const = (int_const_or_overflow
14467 8667680 : && !TREE_OVERFLOW (orig_op0)
14468 8667611 : && !TREE_OVERFLOW (orig_op1));
14469 : }
14470 : else
14471 : int_const = int_const_or_overflow = false;
14472 :
14473 : /* Do not apply default conversion in mixed vector/scalar expression. */
14474 17696965 : if (convert_p
14475 17696965 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14476 : {
14477 10862099 : op0 = default_conversion (op0);
14478 10862099 : op1 = default_conversion (op1);
14479 : }
14480 :
14481 17696965 : orig_type0 = type0 = TREE_TYPE (op0);
14482 :
14483 17696965 : orig_type1 = type1 = TREE_TYPE (op1);
14484 :
14485 : /* The expression codes of the data types of the arguments tell us
14486 : whether the arguments are integers, floating, pointers, etc. */
14487 17696965 : code0 = TREE_CODE (type0);
14488 17696965 : code1 = TREE_CODE (type1);
14489 :
14490 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14491 17696967 : STRIP_TYPE_NOPS (op0);
14492 17696968 : STRIP_TYPE_NOPS (op1);
14493 :
14494 : /* If an error was already reported for one of the arguments,
14495 : avoid reporting another error. */
14496 :
14497 17696965 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14498 710 : return error_mark_node;
14499 :
14500 17696255 : if (code0 == POINTER_TYPE
14501 17696255 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14502 11 : return error_mark_node;
14503 :
14504 17696244 : if (code1 == POINTER_TYPE
14505 17696244 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14506 11 : return error_mark_node;
14507 :
14508 35392466 : if ((invalid_op_diag
14509 17696233 : = targetm.invalid_binary_op (code, type0, type1)))
14510 : {
14511 0 : error_at (location, invalid_op_diag);
14512 0 : return error_mark_node;
14513 : }
14514 :
14515 17696233 : switch (code)
14516 : {
14517 : case PLUS_EXPR:
14518 : case MINUS_EXPR:
14519 : case MULT_EXPR:
14520 : case TRUNC_DIV_EXPR:
14521 : case CEIL_DIV_EXPR:
14522 : case FLOOR_DIV_EXPR:
14523 : case ROUND_DIV_EXPR:
14524 : case EXACT_DIV_EXPR:
14525 : may_need_excess_precision = true;
14526 : break;
14527 :
14528 2790669 : case EQ_EXPR:
14529 2790669 : case NE_EXPR:
14530 2790669 : case LE_EXPR:
14531 2790669 : case GE_EXPR:
14532 2790669 : case LT_EXPR:
14533 2790669 : case GT_EXPR:
14534 : /* Excess precision for implicit conversions of integers to
14535 : floating point in C11 and later. */
14536 2790669 : may_need_excess_precision = (flag_isoc11
14537 2790669 : && (ANY_INTEGRAL_TYPE_P (type0)
14538 490332 : || ANY_INTEGRAL_TYPE_P (type1)));
14539 : break;
14540 :
14541 : default:
14542 17696233 : may_need_excess_precision = false;
14543 : break;
14544 : }
14545 17696233 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14546 : {
14547 184 : op0 = TREE_OPERAND (op0, 0);
14548 184 : type0 = TREE_TYPE (op0);
14549 : }
14550 17696049 : else if (may_need_excess_precision
14551 17696049 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14552 : {
14553 788 : type0 = eptype;
14554 788 : op0 = convert (eptype, op0);
14555 : }
14556 17696233 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14557 : {
14558 897 : op1 = TREE_OPERAND (op1, 0);
14559 897 : type1 = TREE_TYPE (op1);
14560 : }
14561 17695336 : else if (may_need_excess_precision
14562 17695336 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14563 : {
14564 608 : type1 = eptype;
14565 608 : op1 = convert (eptype, op1);
14566 : }
14567 :
14568 17696233 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14569 :
14570 : /* In case when one of the operands of the binary operation is
14571 : a vector and another is a scalar -- convert scalar to vector. */
14572 19293967 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14573 19291824 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14574 : {
14575 2769 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14576 : orig_op1, true);
14577 :
14578 2769 : switch (convert_flag)
14579 : {
14580 12 : case stv_error:
14581 12 : return error_mark_node;
14582 613 : case stv_firstarg:
14583 613 : {
14584 613 : bool maybe_const = true;
14585 613 : tree sc;
14586 613 : sc = c_fully_fold (op0, false, &maybe_const);
14587 613 : sc = save_expr (sc);
14588 613 : sc = convert (TREE_TYPE (type1), sc);
14589 613 : op0 = build_vector_from_val (type1, sc);
14590 613 : if (!maybe_const)
14591 93 : op0 = c_wrap_maybe_const (op0, true);
14592 613 : orig_type0 = type0 = TREE_TYPE (op0);
14593 613 : code0 = TREE_CODE (type0);
14594 613 : converted = 1;
14595 613 : break;
14596 : }
14597 1265 : case stv_secondarg:
14598 1265 : {
14599 1265 : bool maybe_const = true;
14600 1265 : tree sc;
14601 1265 : sc = c_fully_fold (op1, false, &maybe_const);
14602 1265 : sc = save_expr (sc);
14603 1265 : sc = convert (TREE_TYPE (type0), sc);
14604 1265 : op1 = build_vector_from_val (type0, sc);
14605 1265 : if (!maybe_const)
14606 38 : op1 = c_wrap_maybe_const (op1, true);
14607 1265 : orig_type1 = type1 = TREE_TYPE (op1);
14608 1265 : code1 = TREE_CODE (type1);
14609 1265 : converted = 1;
14610 1265 : break;
14611 : }
14612 : default:
14613 : break;
14614 : }
14615 : }
14616 :
14617 17696221 : switch (code)
14618 : {
14619 9240085 : case PLUS_EXPR:
14620 : /* Handle the pointer + int case. */
14621 9240085 : if (code0 == POINTER_TYPE
14622 1265240 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14623 : {
14624 1265234 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14625 1265234 : goto return_build_binary_op;
14626 : }
14627 7974851 : else if (code1 == POINTER_TYPE
14628 1522 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14629 : {
14630 1516 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14631 1516 : goto return_build_binary_op;
14632 : }
14633 : else
14634 : common = 1;
14635 : break;
14636 :
14637 806647 : case MINUS_EXPR:
14638 : /* Subtraction of two similar pointers.
14639 : We must subtract them as integers, then divide by object size. */
14640 806647 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14641 806647 : && comp_target_types (location, type0, type1))
14642 : {
14643 3780 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14644 3780 : goto return_build_binary_op;
14645 : }
14646 : /* Handle pointer minus int. Just like pointer plus int. */
14647 802867 : else if (code0 == POINTER_TYPE
14648 17009 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14649 : {
14650 16997 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14651 16997 : goto return_build_binary_op;
14652 : }
14653 : else
14654 : common = 1;
14655 : break;
14656 :
14657 : case MULT_EXPR:
14658 : common = 1;
14659 : break;
14660 :
14661 278589 : case TRUNC_DIV_EXPR:
14662 278589 : case CEIL_DIV_EXPR:
14663 278589 : case FLOOR_DIV_EXPR:
14664 278589 : case ROUND_DIV_EXPR:
14665 278589 : case EXACT_DIV_EXPR:
14666 278589 : doing_div_or_mod = true;
14667 278589 : warn_for_div_by_zero (location, op1);
14668 :
14669 278588 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14670 50304 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14671 48233 : || code0 == COMPLEX_TYPE
14672 46164 : || gnu_vector_type_p (type0))
14673 328891 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14674 47831 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14675 47640 : || code1 == COMPLEX_TYPE
14676 46166 : || gnu_vector_type_p (type1)))
14677 : {
14678 278584 : enum tree_code tcode0 = code0, tcode1 = code1;
14679 :
14680 278584 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14681 48232 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14682 278584 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14683 47637 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14684 :
14685 278584 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14686 70305 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14687 208280 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14688 12988 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14689 83291 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14690 : resultcode = RDIV_EXPR;
14691 : else
14692 : /* Although it would be tempting to shorten always here, that
14693 : loses on some targets, since the modulo instruction is
14694 : undefined if the quotient can't be represented in the
14695 : computation mode. We shorten only if unsigned or if
14696 : dividing by something we know != -1. */
14697 195293 : shorten = may_shorten_divmod (op0, op1);
14698 : common = 1;
14699 : }
14700 : break;
14701 :
14702 1669459 : case BIT_AND_EXPR:
14703 1669459 : case BIT_IOR_EXPR:
14704 1669459 : case BIT_XOR_EXPR:
14705 1669459 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14706 1127988 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14707 : shorten = -1;
14708 : /* Allow vector types which are not floating point types. */
14709 541507 : else if (gnu_vector_type_p (type0)
14710 541430 : && gnu_vector_type_p (type1)
14711 541430 : && !VECTOR_FLOAT_TYPE_P (type0)
14712 1082934 : && !VECTOR_FLOAT_TYPE_P (type1))
14713 : common = 1;
14714 : break;
14715 :
14716 59526 : case TRUNC_MOD_EXPR:
14717 59526 : case FLOOR_MOD_EXPR:
14718 59526 : doing_div_or_mod = true;
14719 59526 : warn_for_div_by_zero (location, op1);
14720 :
14721 59526 : if (gnu_vector_type_p (type0)
14722 393 : && gnu_vector_type_p (type1)
14723 393 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14724 59919 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14725 : common = 1;
14726 59133 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14727 59132 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14728 : {
14729 : /* Although it would be tempting to shorten always here, that loses
14730 : on some targets, since the modulo instruction is undefined if the
14731 : quotient can't be represented in the computation mode. We shorten
14732 : only if unsigned or if dividing by something we know != -1. */
14733 59132 : shorten = may_shorten_divmod (op0, op1);
14734 59132 : common = 1;
14735 : }
14736 : break;
14737 :
14738 516802 : case TRUTH_ANDIF_EXPR:
14739 516802 : case TRUTH_ORIF_EXPR:
14740 516802 : case TRUTH_AND_EXPR:
14741 516802 : case TRUTH_OR_EXPR:
14742 516802 : case TRUTH_XOR_EXPR:
14743 516802 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14744 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14745 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14746 0 : || code0 == BITINT_TYPE)
14747 516802 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14748 181 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14749 24 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14750 22 : || code1 == BITINT_TYPE))
14751 : {
14752 : /* Result of these operations is always an int,
14753 : but that does not mean the operands should be
14754 : converted to ints! */
14755 516802 : result_type = integer_type_node;
14756 516802 : if (op0_int_operands)
14757 : {
14758 110721 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14759 110721 : op0 = remove_c_maybe_const_expr (op0);
14760 : }
14761 : else
14762 406081 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14763 516802 : if (op1_int_operands)
14764 : {
14765 71649 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14766 71649 : op1 = remove_c_maybe_const_expr (op1);
14767 : }
14768 : else
14769 445153 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14770 : converted = 1;
14771 : boolean_op = true;
14772 : }
14773 516802 : if (code == TRUTH_ANDIF_EXPR)
14774 : {
14775 251754 : int_const_or_overflow = (int_operands
14776 63456 : && TREE_CODE (orig_op0) == INTEGER_CST
14777 251770 : && (op0 == truthvalue_false_node
14778 13345 : || TREE_CODE (orig_op1) == INTEGER_CST));
14779 : int_const = (int_const_or_overflow
14780 63429 : && !TREE_OVERFLOW (orig_op0)
14781 63429 : && (op0 == truthvalue_false_node
14782 13329 : || !TREE_OVERFLOW (orig_op1)));
14783 : }
14784 328477 : else if (code == TRUTH_ORIF_EXPR)
14785 : {
14786 335158 : int_const_or_overflow = (int_operands
14787 6832 : && TREE_CODE (orig_op0) == INTEGER_CST
14788 335181 : && (op0 == truthvalue_true_node
14789 2147 : || TREE_CODE (orig_op1) == INTEGER_CST));
14790 : int_const = (int_const_or_overflow
14791 6802 : && !TREE_OVERFLOW (orig_op0)
14792 6802 : && (op0 == truthvalue_true_node
14793 2124 : || !TREE_OVERFLOW (orig_op1)));
14794 : }
14795 : break;
14796 :
14797 : /* Shift operations: result has same type as first operand;
14798 : always convert second operand to int.
14799 : Also set SHORT_SHIFT if shifting rightward. */
14800 :
14801 235488 : case RSHIFT_EXPR:
14802 235488 : if (gnu_vector_type_p (type0)
14803 848 : && gnu_vector_type_p (type1)
14804 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14805 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14806 235744 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14807 : TYPE_VECTOR_SUBPARTS (type1)))
14808 : {
14809 : result_type = type0;
14810 : converted = 1;
14811 : }
14812 235234 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14813 1314 : || code0 == BITINT_TYPE
14814 598 : || (gnu_vector_type_p (type0)
14815 594 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14816 236541 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14817 : {
14818 235225 : doing_shift = true;
14819 235225 : if (TREE_CODE (op1) == INTEGER_CST)
14820 : {
14821 205468 : if (tree_int_cst_sgn (op1) < 0)
14822 : {
14823 269 : int_const = false;
14824 269 : if (c_inhibit_evaluation_warnings == 0)
14825 129 : warning_at (location, OPT_Wshift_count_negative,
14826 : "right shift count is negative");
14827 : }
14828 205199 : else if (code0 == VECTOR_TYPE)
14829 : {
14830 490 : if (compare_tree_int (op1,
14831 490 : TYPE_PRECISION (TREE_TYPE (type0)))
14832 : >= 0)
14833 : {
14834 16 : int_const = false;
14835 16 : if (c_inhibit_evaluation_warnings == 0)
14836 16 : warning_at (location, OPT_Wshift_count_overflow,
14837 : "right shift count >= width of vector element");
14838 : }
14839 : }
14840 : else
14841 : {
14842 204709 : if (!integer_zerop (op1))
14843 204346 : short_shift = 1;
14844 :
14845 204709 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14846 : {
14847 586 : int_const = false;
14848 586 : if (c_inhibit_evaluation_warnings == 0)
14849 52 : warning_at (location, OPT_Wshift_count_overflow,
14850 : "right shift count >= width of type");
14851 : }
14852 : }
14853 : }
14854 :
14855 : /* Use the type of the value to be shifted. */
14856 : result_type = type0;
14857 : /* Avoid converting op1 to result_type later. */
14858 : converted = 1;
14859 : }
14860 : break;
14861 :
14862 812096 : case LSHIFT_EXPR:
14863 812096 : if (gnu_vector_type_p (type0)
14864 599 : && gnu_vector_type_p (type1)
14865 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14866 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14867 812409 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14868 : TYPE_VECTOR_SUBPARTS (type1)))
14869 : {
14870 : result_type = type0;
14871 : converted = 1;
14872 : }
14873 811785 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14874 976 : || code0 == BITINT_TYPE
14875 294 : || (gnu_vector_type_p (type0)
14876 288 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14877 812752 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14878 : {
14879 811771 : doing_shift = true;
14880 811771 : if (TREE_CODE (op0) == INTEGER_CST
14881 443330 : && tree_int_cst_sgn (op0) < 0
14882 812520 : && !TYPE_OVERFLOW_WRAPS (type0))
14883 : {
14884 : /* Don't reject a left shift of a negative value in a context
14885 : where a constant expression is needed in C90. */
14886 712 : if (flag_isoc99)
14887 666 : int_const = false;
14888 712 : if (c_inhibit_evaluation_warnings == 0)
14889 712 : warning_at (location, OPT_Wshift_negative_value,
14890 : "left shift of negative value");
14891 : }
14892 811771 : if (TREE_CODE (op1) == INTEGER_CST)
14893 : {
14894 738691 : if (tree_int_cst_sgn (op1) < 0)
14895 : {
14896 313 : int_const = false;
14897 313 : if (c_inhibit_evaluation_warnings == 0)
14898 135 : warning_at (location, OPT_Wshift_count_negative,
14899 : "left shift count is negative");
14900 : }
14901 738378 : else if (code0 == VECTOR_TYPE)
14902 : {
14903 216 : if (compare_tree_int (op1,
14904 216 : TYPE_PRECISION (TREE_TYPE (type0)))
14905 : >= 0)
14906 : {
14907 6 : int_const = false;
14908 6 : if (c_inhibit_evaluation_warnings == 0)
14909 6 : warning_at (location, OPT_Wshift_count_overflow,
14910 : "left shift count >= width of vector element");
14911 : }
14912 : }
14913 738162 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14914 : {
14915 29733 : int_const = false;
14916 29733 : if (c_inhibit_evaluation_warnings == 0)
14917 113 : warning_at (location, OPT_Wshift_count_overflow,
14918 : "left shift count >= width of type");
14919 : }
14920 708429 : else if (TREE_CODE (op0) == INTEGER_CST
14921 369198 : && maybe_warn_shift_overflow (location, op0, op1)
14922 709057 : && flag_isoc99)
14923 : int_const = false;
14924 : }
14925 :
14926 : /* Use the type of the value to be shifted. */
14927 : result_type = type0;
14928 : /* Avoid converting op1 to result_type later. */
14929 : converted = 1;
14930 : }
14931 : break;
14932 :
14933 1770568 : case EQ_EXPR:
14934 1770568 : case NE_EXPR:
14935 1770568 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14936 : {
14937 42349 : tree intt;
14938 42349 : if (!vector_types_compatible_elements_p (type0, type1))
14939 : {
14940 4 : error_at (location, "comparing vectors with different "
14941 : "element types");
14942 4 : return error_mark_node;
14943 : }
14944 :
14945 42345 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14946 84690 : TYPE_VECTOR_SUBPARTS (type1)))
14947 : {
14948 1 : error_at (location, "comparing vectors with different "
14949 : "number of elements");
14950 1 : return error_mark_node;
14951 : }
14952 :
14953 : /* It's not precisely specified how the usual arithmetic
14954 : conversions apply to the vector types. Here, we use
14955 : the unsigned type if one of the operands is signed and
14956 : the other one is unsigned. */
14957 42344 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14958 : {
14959 4 : if (!TYPE_UNSIGNED (type0))
14960 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14961 : else
14962 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14963 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14964 : "types %qT and %qT", type0, type1);
14965 : }
14966 :
14967 : /* Always construct signed integer vector type. */
14968 42344 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14969 : result_type = type0;
14970 : else
14971 : {
14972 42344 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14973 :
14974 127032 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14975 84688 : (SCALAR_TYPE_MODE
14976 : (TREE_TYPE (type0))), 0);
14977 42344 : if (!intt)
14978 : {
14979 0 : error_at (location, "could not find an integer type "
14980 : "of the same size as %qT",
14981 0 : TREE_TYPE (type0));
14982 0 : return error_mark_node;
14983 : }
14984 42344 : result_type = build_opaque_vector_type (intt, nelts);
14985 : }
14986 42344 : converted = 1;
14987 42344 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14988 42344 : goto return_build_binary_op;
14989 : }
14990 1728219 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14991 255640 : warning_at (location,
14992 255640 : OPT_Wfloat_equal,
14993 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14994 : /* Result of comparison is always int,
14995 : but don't convert the args to int! */
14996 1728219 : build_type = integer_type_node;
14997 1728219 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14998 160512 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14999 1604137 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15000 : || code1 == BITINT_TYPE
15001 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
15002 : short_compare = 1;
15003 124348 : else if (code0 == POINTER_TYPE
15004 124348 : && (code1 == NULLPTR_TYPE
15005 123965 : || null_pointer_constant_p (orig_op1)))
15006 : {
15007 52588 : maybe_warn_for_null_address (location, op0, code);
15008 52588 : result_type = type0;
15009 : }
15010 71760 : else if (code1 == POINTER_TYPE
15011 71760 : && (code0 == NULLPTR_TYPE
15012 71658 : || null_pointer_constant_p (orig_op0)))
15013 : {
15014 294 : maybe_warn_for_null_address (location, op1, code);
15015 294 : result_type = type1;
15016 : }
15017 71466 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15018 : {
15019 71376 : tree tt0 = TREE_TYPE (type0);
15020 71376 : tree tt1 = TREE_TYPE (type1);
15021 71376 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
15022 71376 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
15023 71376 : addr_space_t as_common = ADDR_SPACE_GENERIC;
15024 :
15025 : /* Anything compares with void *. void * compares with anything.
15026 : Otherwise, the targets must be compatible
15027 : and both must be object or both incomplete. */
15028 71376 : if (comp_target_types (location, type0, type1))
15029 49982 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15030 21394 : else if (!addr_space_superset (as0, as1, &as_common))
15031 : {
15032 0 : error_at (location, "comparison of pointers to "
15033 : "disjoint address spaces");
15034 0 : return error_mark_node;
15035 : }
15036 21394 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
15037 : {
15038 21115 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
15039 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15040 : "comparison of %<void *%> with function pointer");
15041 : }
15042 279 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
15043 : {
15044 231 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
15045 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15046 : "comparison of %<void *%> with function pointer");
15047 : }
15048 : else
15049 : /* Avoid warning about the volatile ObjC EH puts on decls. */
15050 48 : if (!objc_ok)
15051 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15052 : "comparison of distinct pointer types lacks a cast");
15053 :
15054 50038 : if (result_type == NULL_TREE)
15055 : {
15056 21394 : qualifier_set qual {TYPE_UNQUALIFIED, as_common};
15057 21394 : result_type = c_build_pointer_type
15058 21394 : (c_build_qualified_type (void_type_node, qual));
15059 : }
15060 : }
15061 90 : else if (code0 == POINTER_TYPE
15062 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15063 : {
15064 26 : result_type = type0;
15065 26 : pedwarn (location, 0, "comparison between pointer and integer");
15066 : }
15067 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15068 21 : && code1 == POINTER_TYPE)
15069 : {
15070 8 : result_type = type1;
15071 8 : pedwarn (location, 0, "comparison between pointer and integer");
15072 : }
15073 : /* 6.5.9: One of the following shall hold:
15074 : -- both operands have type nullptr_t; */
15075 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
15076 : {
15077 22 : result_type = nullptr_type_node;
15078 : /* No need to convert the operands to result_type later. */
15079 22 : converted = 1;
15080 : }
15081 : /* -- one operand has type nullptr_t and the other is a null pointer
15082 : constant. We will have to convert the former to the type of the
15083 : latter, because during gimplification we can't have mismatching
15084 : comparison operand type. We convert from nullptr_t to the other
15085 : type, since only nullptr_t can be converted to nullptr_t. Also,
15086 : even a constant 0 is a null pointer constant, so we may have to
15087 : create a pointer type from its type. */
15088 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
15089 19 : result_type = (INTEGRAL_TYPE_P (type1)
15090 19 : ? c_build_pointer_type (type1) : type1);
15091 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
15092 11 : result_type = (INTEGRAL_TYPE_P (type0)
15093 11 : ? c_build_pointer_type (type0) : type0);
15094 5108641 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15095 3380397 : || truth_value_p (TREE_CODE (orig_op0)))
15096 3448949 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15097 3448945 : || truth_value_p (TREE_CODE (orig_op1))))
15098 72882 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15099 : break;
15100 :
15101 1020097 : case LE_EXPR:
15102 1020097 : case GE_EXPR:
15103 1020097 : case LT_EXPR:
15104 1020097 : case GT_EXPR:
15105 1020097 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
15106 : {
15107 59797 : tree intt;
15108 59797 : if (!vector_types_compatible_elements_p (type0, type1))
15109 : {
15110 5 : error_at (location, "comparing vectors with different "
15111 : "element types");
15112 5 : return error_mark_node;
15113 : }
15114 :
15115 59792 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
15116 119584 : TYPE_VECTOR_SUBPARTS (type1)))
15117 : {
15118 0 : error_at (location, "comparing vectors with different "
15119 : "number of elements");
15120 0 : return error_mark_node;
15121 : }
15122 :
15123 : /* It's not precisely specified how the usual arithmetic
15124 : conversions apply to the vector types. Here, we use
15125 : the unsigned type if one of the operands is signed and
15126 : the other one is unsigned. */
15127 59792 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
15128 : {
15129 15 : if (!TYPE_UNSIGNED (type0))
15130 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
15131 : else
15132 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15133 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15134 : "types %qT and %qT", type0, type1);
15135 : }
15136 :
15137 : /* Always construct signed integer vector type. */
15138 179376 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15139 119584 : (SCALAR_TYPE_MODE
15140 : (TREE_TYPE (type0))), 0);
15141 59792 : if (!intt)
15142 : {
15143 0 : error_at (location, "could not find an integer type "
15144 : "of the same size as %qT",
15145 0 : TREE_TYPE (type0));
15146 0 : return error_mark_node;
15147 : }
15148 59792 : result_type = build_opaque_vector_type (intt,
15149 59792 : TYPE_VECTOR_SUBPARTS (type0));
15150 59792 : converted = 1;
15151 59792 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15152 59792 : goto return_build_binary_op;
15153 : }
15154 960300 : build_type = integer_type_node;
15155 960300 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15156 65365 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15157 895607 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15158 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15159 : short_compare = 1;
15160 64725 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15161 : {
15162 64658 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15163 64658 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15164 64658 : addr_space_t as_common;
15165 :
15166 64658 : if (comp_target_types (location, type0, type1))
15167 : {
15168 64493 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15169 64493 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15170 64493 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15171 32 : pedwarn_c99 (location, OPT_Wpedantic,
15172 : "comparison of complete and incomplete pointers");
15173 64461 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15174 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15175 : "ordered comparisons of pointers to functions");
15176 64461 : else if (null_pointer_constant_p (orig_op0)
15177 64461 : || null_pointer_constant_p (orig_op1))
15178 8 : warning_at (location, OPT_Wextra,
15179 : "ordered comparison of pointer with null pointer");
15180 :
15181 : }
15182 165 : else if (!addr_space_superset (as0, as1, &as_common))
15183 : {
15184 0 : error_at (location, "comparison of pointers to "
15185 : "disjoint address spaces");
15186 0 : return error_mark_node;
15187 : }
15188 : else
15189 : {
15190 165 : qualifier_set qual {TYPE_UNQUALIFIED, as_common};
15191 165 : result_type = c_build_pointer_type
15192 165 : (c_build_qualified_type (void_type_node, qual));
15193 165 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15194 : "comparison of distinct pointer types lacks a cast");
15195 : }
15196 : }
15197 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15198 : {
15199 17 : result_type = type0;
15200 17 : if (pedantic)
15201 11 : pedwarn (location, OPT_Wpedantic,
15202 : "ordered comparison of pointer with integer zero");
15203 6 : else if (extra_warnings)
15204 1 : warning_at (location, OPT_Wextra,
15205 : "ordered comparison of pointer with integer zero");
15206 : }
15207 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15208 : {
15209 12 : result_type = type1;
15210 12 : if (pedantic)
15211 2 : pedwarn (location, OPT_Wpedantic,
15212 : "ordered comparison of pointer with integer zero");
15213 10 : else if (extra_warnings)
15214 1 : warning_at (location, OPT_Wextra,
15215 : "ordered comparison of pointer with integer zero");
15216 : }
15217 38 : else if (code0 == POINTER_TYPE
15218 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15219 : {
15220 18 : result_type = type0;
15221 18 : pedwarn (location, 0, "comparison between pointer and integer");
15222 : }
15223 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15224 20 : && code1 == POINTER_TYPE)
15225 : {
15226 18 : result_type = type1;
15227 18 : pedwarn (location, 0, "comparison between pointer and integer");
15228 : }
15229 :
15230 960300 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15231 64723 : && current_function_decl != NULL_TREE
15232 1025012 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15233 : {
15234 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15235 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15236 :
15237 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15238 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15239 : }
15240 :
15241 2880797 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15242 1920497 : || truth_value_p (TREE_CODE (orig_op0)))
15243 1920517 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15244 1920517 : || truth_value_p (TREE_CODE (orig_op1))))
15245 527 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15246 : break;
15247 :
15248 43 : case MIN_EXPR:
15249 43 : case MAX_EXPR:
15250 : /* Used for OpenMP atomics. */
15251 43 : gcc_assert (flag_openmp);
15252 : common = 1;
15253 : break;
15254 :
15255 0 : default:
15256 0 : gcc_unreachable ();
15257 : }
15258 :
15259 16306547 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15260 0 : return error_mark_node;
15261 :
15262 16306547 : if (gnu_vector_type_p (type0)
15263 1496197 : && gnu_vector_type_p (type1)
15264 17801870 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15265 1495303 : || !vector_types_compatible_elements_p (type0, type1)))
15266 : {
15267 26 : gcc_rich_location richloc (location);
15268 26 : maybe_range_label_for_tree_type_mismatch
15269 26 : label_for_op0 (orig_op0, orig_op1),
15270 26 : label_for_op1 (orig_op1, orig_op0);
15271 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15272 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15273 26 : binary_op_error (&richloc, code, type0, type1);
15274 26 : return error_mark_node;
15275 26 : }
15276 :
15277 16306521 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15278 1756340 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15279 1685361 : || gnu_vector_type_p (type0))
15280 17873671 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15281 1590781 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15282 1497434 : || gnu_vector_type_p (type1)))
15283 : {
15284 16115199 : bool first_complex = (code0 == COMPLEX_TYPE);
15285 16115199 : bool second_complex = (code1 == COMPLEX_TYPE);
15286 16115199 : int none_complex = (!first_complex && !second_complex);
15287 :
15288 16115199 : if (shorten || common || short_compare)
15289 : {
15290 14552579 : result_type = c_common_type (type0, type1);
15291 14552579 : do_warn_double_promotion (result_type, type0, type1,
15292 : "implicit conversion from %qT to %qT "
15293 : "to match other operand of binary "
15294 : "expression",
15295 : location);
15296 14552579 : if (result_type == error_mark_node)
15297 : return error_mark_node;
15298 : }
15299 :
15300 16115177 : if (first_complex != second_complex
15301 82109 : && (code == PLUS_EXPR
15302 : || code == MINUS_EXPR
15303 82109 : || code == MULT_EXPR
15304 17533 : || (code == TRUNC_DIV_EXPR && first_complex))
15305 65997 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15306 16177456 : && flag_signed_zeros)
15307 : {
15308 : /* An operation on mixed real/complex operands must be
15309 : handled specially, but the language-independent code can
15310 : more easily optimize the plain complex arithmetic if
15311 : -fno-signed-zeros. */
15312 61951 : tree real_type = TREE_TYPE (result_type);
15313 61951 : tree real, imag;
15314 61951 : if (type0 != orig_type0 || type1 != orig_type1)
15315 : {
15316 94 : gcc_assert (may_need_excess_precision && common);
15317 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15318 : }
15319 61951 : if (first_complex)
15320 : {
15321 8257 : if (TREE_TYPE (op0) != result_type)
15322 1787 : op0 = convert_and_check (location, result_type, op0);
15323 8257 : if (TREE_TYPE (op1) != real_type)
15324 4605 : op1 = convert_and_check (location, real_type, op1);
15325 : }
15326 : else
15327 : {
15328 53694 : if (TREE_TYPE (op0) != real_type)
15329 2702 : op0 = convert_and_check (location, real_type, op0);
15330 53694 : if (TREE_TYPE (op1) != result_type)
15331 1866 : op1 = convert_and_check (location, result_type, op1);
15332 : }
15333 61951 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15334 0 : return error_mark_node;
15335 61951 : if (first_complex)
15336 : {
15337 8257 : op0 = save_expr (op0);
15338 8257 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15339 : op0, true);
15340 8257 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15341 : op0, true);
15342 8257 : switch (code)
15343 : {
15344 3492 : case MULT_EXPR:
15345 3492 : case TRUNC_DIV_EXPR:
15346 3492 : op1 = save_expr (op1);
15347 3492 : imag = build2 (resultcode, real_type, imag, op1);
15348 : /* Fall through. */
15349 8257 : case PLUS_EXPR:
15350 8257 : case MINUS_EXPR:
15351 8257 : real = build2 (resultcode, real_type, real, op1);
15352 8257 : break;
15353 : default:
15354 : gcc_unreachable();
15355 : }
15356 : }
15357 : else
15358 : {
15359 53694 : op1 = save_expr (op1);
15360 53694 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15361 : op1, true);
15362 53694 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15363 : op1, true);
15364 53694 : switch (code)
15365 : {
15366 2008 : case MULT_EXPR:
15367 2008 : op0 = save_expr (op0);
15368 2008 : imag = build2 (resultcode, real_type, op0, imag);
15369 : /* Fall through. */
15370 31962 : case PLUS_EXPR:
15371 31962 : real = build2 (resultcode, real_type, op0, real);
15372 31962 : break;
15373 21732 : case MINUS_EXPR:
15374 21732 : real = build2 (resultcode, real_type, op0, real);
15375 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15376 21732 : break;
15377 0 : default:
15378 0 : gcc_unreachable();
15379 : }
15380 : }
15381 61951 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15382 61951 : goto return_build_binary_op;
15383 : }
15384 :
15385 : /* For certain operations (which identify themselves by shorten != 0)
15386 : if both args were extended from the same smaller type,
15387 : do the arithmetic in that type and then extend.
15388 :
15389 : shorten !=0 and !=1 indicates a bitwise operation.
15390 : For them, this optimization is safe only if
15391 : both args are zero-extended or both are sign-extended.
15392 : Otherwise, we might change the result.
15393 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15394 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15395 :
15396 16053226 : if (shorten && none_complex)
15397 : {
15398 1375223 : final_type = result_type;
15399 1375223 : result_type = shorten_binary_op (result_type, op0, op1,
15400 : shorten == -1);
15401 : }
15402 :
15403 : /* Shifts can be shortened if shifting right. */
15404 :
15405 16053226 : if (short_shift)
15406 : {
15407 204346 : int unsigned_arg;
15408 204346 : tree arg0 = get_narrower (op0, &unsigned_arg);
15409 :
15410 204346 : final_type = result_type;
15411 :
15412 204346 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15413 144090 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15414 :
15415 204346 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15416 3106 : && tree_int_cst_sgn (op1) > 0
15417 : /* We can shorten only if the shift count is less than the
15418 : number of bits in the smaller type size. */
15419 3106 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15420 : /* We cannot drop an unsigned shift after sign-extension. */
15421 207311 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15422 : {
15423 : /* Do an unsigned shift if the operand was zero-extended. */
15424 2949 : result_type
15425 2949 : = c_common_signed_or_unsigned_type (unsigned_arg,
15426 2949 : TREE_TYPE (arg0));
15427 : /* Convert value-to-be-shifted to that type. */
15428 2949 : if (TREE_TYPE (op0) != result_type)
15429 2949 : op0 = convert (result_type, op0);
15430 : converted = 1;
15431 : }
15432 : }
15433 :
15434 : /* Comparison operations are shortened too but differently.
15435 : They identify themselves by setting short_compare = 1. */
15436 :
15437 16053226 : if (short_compare)
15438 : {
15439 : /* These conversions do not go through
15440 : warnings_for_convert_and_check, so warn here for widening
15441 : conversions to types with no_implicit_truncation attribute. */
15442 4998888 : if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
15443 2147585 : && INTEGRAL_TYPE_P (TREE_TYPE (op1))
15444 4646476 : && (TYPE_PRECISION (TREE_TYPE (op0))
15445 2147032 : != TYPE_PRECISION (TREE_TYPE (op1))))
15446 : {
15447 448740 : tree t0 = op0, t1 = op1;
15448 448740 : if (TYPE_PRECISION (TREE_TYPE (op0))
15449 448740 : < TYPE_PRECISION (TREE_TYPE (op1)))
15450 : {
15451 184352 : t0 = op1;
15452 184352 : t1 = op0;
15453 : }
15454 448740 : if (TREE_CODE (t1) != INTEGER_CST
15455 550110 : && lookup_attribute ("no_implicit_truncation",
15456 101370 : TYPE_ATTRIBUTES (TREE_TYPE (t0))))
15457 24 : warning_at (location, OPT_Wconversion_attribute,
15458 : "conversion to %qT from narrower integer type",
15459 24 : TREE_TYPE (t0));
15460 : }
15461 : /* Don't write &op0, etc., because that would prevent op0
15462 : from being kept in a register.
15463 : Instead, make copies of the our local variables and
15464 : pass the copies by reference, then copy them back afterward. */
15465 2499444 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15466 2499444 : enum tree_code xresultcode = resultcode;
15467 2499444 : tree val
15468 2499444 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15469 : &xresultcode);
15470 :
15471 2499444 : if (val != NULL_TREE)
15472 : {
15473 15934 : ret = val;
15474 15934 : goto return_build_binary_op;
15475 : }
15476 :
15477 2483510 : op0 = xop0, op1 = xop1;
15478 2483510 : converted = 1;
15479 2483510 : resultcode = xresultcode;
15480 :
15481 2483510 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15482 : {
15483 2334934 : bool op0_maybe_const = true;
15484 2334934 : bool op1_maybe_const = true;
15485 2334934 : tree orig_op0_folded, orig_op1_folded;
15486 :
15487 2334934 : if (in_late_binary_op)
15488 : {
15489 : orig_op0_folded = orig_op0;
15490 : orig_op1_folded = orig_op1;
15491 : }
15492 : else
15493 : {
15494 : /* Fold for the sake of possible warnings, as in
15495 : build_conditional_expr. This requires the
15496 : "original" values to be folded, not just op0 and
15497 : op1. */
15498 2326263 : c_inhibit_evaluation_warnings++;
15499 2326263 : op0 = c_fully_fold (op0, require_constant_value,
15500 : &op0_maybe_const);
15501 2326263 : op1 = c_fully_fold (op1, require_constant_value,
15502 : &op1_maybe_const);
15503 2326263 : c_inhibit_evaluation_warnings--;
15504 2326263 : orig_op0_folded = c_fully_fold (orig_op0,
15505 : require_constant_value,
15506 : NULL);
15507 2326263 : orig_op1_folded = c_fully_fold (orig_op1,
15508 : require_constant_value,
15509 : NULL);
15510 : }
15511 :
15512 2334934 : if (warn_sign_compare)
15513 406350 : warn_for_sign_compare (location, orig_op0_folded,
15514 : orig_op1_folded, op0, op1,
15515 : result_type, resultcode);
15516 2334934 : if (!in_late_binary_op && !int_operands)
15517 : {
15518 2145217 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15519 2141707 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15520 2145217 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15521 839231 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15522 : }
15523 : }
15524 : }
15525 : }
15526 :
15527 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15528 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15529 : Then the expression will be built.
15530 : It will be given type FINAL_TYPE if that is nonzero;
15531 : otherwise, it will be given type RESULT_TYPE. */
15532 :
15533 16228614 : if (!result_type)
15534 : {
15535 : /* Favor showing any expression locations that are available. */
15536 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15537 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15538 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15539 514 : return error_mark_node;
15540 514 : }
15541 :
15542 16228100 : if (build_type == NULL_TREE)
15543 : {
15544 13555523 : build_type = result_type;
15545 13555523 : if ((type0 != orig_type0 || type1 != orig_type1)
15546 814 : && !boolean_op)
15547 : {
15548 814 : gcc_assert (may_need_excess_precision && common);
15549 814 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15550 : }
15551 : }
15552 :
15553 16228100 : if (!converted)
15554 : {
15555 12178779 : int save_warn_conversion_attribute = warn_conversion_attribute;
15556 12178779 : if ((common || shorten == -1) && !short_compare)
15557 11989734 : warn_conversion_attribute = 0;
15558 12178779 : op0 = ep_convert_and_check (location, result_type, op0,
15559 : semantic_result_type);
15560 12178779 : op1 = ep_convert_and_check (location, result_type, op1,
15561 : semantic_result_type);
15562 12178779 : if ((common || shorten == -1) && !short_compare)
15563 11989734 : warn_conversion_attribute = save_warn_conversion_attribute;
15564 :
15565 : /* This can happen if one operand has a vector type, and the other
15566 : has a different type. */
15567 12178779 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15568 3 : return error_mark_node;
15569 : }
15570 :
15571 16228097 : if (sanitize_flags_p ((SANITIZE_SHIFT
15572 : | SANITIZE_DIVIDE
15573 : | SANITIZE_FLOAT_DIVIDE
15574 : | SANITIZE_SI_OVERFLOW))
15575 23564 : && current_function_decl != NULL_TREE
15576 20094 : && (doing_div_or_mod || doing_shift)
15577 16231030 : && !require_constant_value)
15578 : {
15579 : /* OP0 and/or OP1 might have side-effects. */
15580 2877 : op0 = save_expr (op0);
15581 2877 : op1 = save_expr (op1);
15582 2877 : op0 = c_fully_fold (op0, false, NULL);
15583 2877 : op1 = c_fully_fold (op1, false, NULL);
15584 2877 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15585 : | SANITIZE_FLOAT_DIVIDE
15586 : | SANITIZE_SI_OVERFLOW))))
15587 916 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15588 1961 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15589 1778 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15590 : }
15591 :
15592 : /* Treat expressions in initializers specially as they can't trap. */
15593 16228097 : if (int_const_or_overflow)
15594 8652211 : ret = (require_constant_value
15595 8652211 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15596 : op0, op1)
15597 8605821 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15598 : else
15599 7575886 : ret = build2 (resultcode, build_type, op0, op1);
15600 16228097 : if (final_type != NULL_TREE)
15601 1579569 : ret = convert (final_type, ret);
15602 :
15603 14648528 : return_build_binary_op:
15604 17695645 : gcc_assert (ret != error_mark_node);
15605 17695645 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15606 1859 : ret = (int_operands
15607 1859 : ? note_integer_operands (ret)
15608 537 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15609 17693786 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15610 60311 : && !in_late_binary_op)
15611 60311 : ret = note_integer_operands (ret);
15612 17695645 : protected_set_expr_location (ret, location);
15613 :
15614 17695645 : if (instrument_expr != NULL)
15615 1425 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15616 : instrument_expr, ret);
15617 :
15618 17695645 : if (semantic_result_type)
15619 908 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15620 : semantic_result_type, ret);
15621 :
15622 : return ret;
15623 : }
15624 :
15625 :
15626 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15627 : purpose. LOCATION is the source location for the expression. */
15628 :
15629 : tree
15630 4195223 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15631 : {
15632 4195223 : bool int_const, int_operands;
15633 :
15634 4195223 : switch (TREE_CODE (TREE_TYPE (expr)))
15635 : {
15636 72 : case ARRAY_TYPE:
15637 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15638 72 : return error_mark_node;
15639 :
15640 86 : case RECORD_TYPE:
15641 86 : error_at (location, "used struct type value where scalar is required");
15642 86 : return error_mark_node;
15643 :
15644 83 : case UNION_TYPE:
15645 83 : error_at (location, "used union type value where scalar is required");
15646 83 : return error_mark_node;
15647 :
15648 22 : case VOID_TYPE:
15649 22 : error_at (location, "void value not ignored as it ought to be");
15650 22 : return error_mark_node;
15651 :
15652 31450 : case POINTER_TYPE:
15653 31450 : if (reject_gcc_builtin (expr))
15654 3 : return error_mark_node;
15655 : break;
15656 :
15657 0 : case FUNCTION_TYPE:
15658 0 : gcc_unreachable ();
15659 :
15660 8 : case VECTOR_TYPE:
15661 8 : error_at (location, "used vector type where scalar is required");
15662 8 : return error_mark_node;
15663 :
15664 : default:
15665 : break;
15666 : }
15667 :
15668 : /* Conversion of a floating constant to boolean goes through here
15669 : and yields an integer constant expression. Otherwise, the result
15670 : is only an integer constant expression if the argument is. */
15671 904688 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15672 4195025 : || ((TREE_CODE (expr) == REAL_CST
15673 3289963 : || TREE_CODE (expr) == COMPLEX_CST)
15674 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15675 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15676 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15677 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15678 : == BOOLEAN_TYPE)))));
15679 4194949 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15680 904850 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15681 : {
15682 319 : expr = remove_c_maybe_const_expr (expr);
15683 319 : expr = build2 (NE_EXPR, type, expr,
15684 319 : convert (TREE_TYPE (expr), integer_zero_node));
15685 319 : expr = note_integer_operands (expr);
15686 : }
15687 : else
15688 : {
15689 : /* ??? Should we also give an error for vectors rather than leaving
15690 : those to give errors later? */
15691 4194630 : expr = c_common_truthvalue_conversion (location, expr);
15692 4194630 : expr = fold_convert_loc (location, type, expr);
15693 : }
15694 :
15695 4194949 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15696 : {
15697 76 : if (TREE_OVERFLOW (expr))
15698 : return expr;
15699 : else
15700 76 : return note_integer_operands (expr);
15701 : }
15702 4194873 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15703 864 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15704 : return expr;
15705 : }
15706 :
15707 :
15708 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15709 : required. */
15710 :
15711 : tree
15712 123234692 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15713 : {
15714 123234692 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15715 : {
15716 694 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15717 : /* Executing a compound literal inside a function reinitializes
15718 : it. */
15719 694 : if (!TREE_STATIC (decl))
15720 428 : *se = true;
15721 : return decl;
15722 : }
15723 : else
15724 : return expr;
15725 : }
15726 :
15727 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15728 : statement. LOC is the location of the construct. */
15729 :
15730 : tree
15731 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15732 : tree clauses)
15733 : {
15734 2154 : body = c_end_compound_stmt (loc, body, true);
15735 :
15736 2154 : tree stmt = make_node (code);
15737 2154 : TREE_TYPE (stmt) = void_type_node;
15738 2154 : OMP_BODY (stmt) = body;
15739 2154 : OMP_CLAUSES (stmt) = clauses;
15740 2154 : SET_EXPR_LOCATION (stmt, loc);
15741 :
15742 2154 : return add_stmt (stmt);
15743 : }
15744 :
15745 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15746 : statement. LOC is the location of the OACC_DATA. */
15747 :
15748 : tree
15749 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15750 : {
15751 497 : tree stmt;
15752 :
15753 497 : block = c_end_compound_stmt (loc, block, true);
15754 :
15755 497 : stmt = make_node (OACC_DATA);
15756 497 : TREE_TYPE (stmt) = void_type_node;
15757 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15758 497 : OACC_DATA_BODY (stmt) = block;
15759 497 : SET_EXPR_LOCATION (stmt, loc);
15760 :
15761 497 : return add_stmt (stmt);
15762 : }
15763 :
15764 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15765 : statement. LOC is the location of the OACC_HOST_DATA. */
15766 :
15767 : tree
15768 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15769 : {
15770 21 : tree stmt;
15771 :
15772 21 : block = c_end_compound_stmt (loc, block, true);
15773 :
15774 21 : stmt = make_node (OACC_HOST_DATA);
15775 21 : TREE_TYPE (stmt) = void_type_node;
15776 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15777 21 : OACC_HOST_DATA_BODY (stmt) = block;
15778 21 : SET_EXPR_LOCATION (stmt, loc);
15779 :
15780 21 : return add_stmt (stmt);
15781 : }
15782 :
15783 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15784 :
15785 : tree
15786 12673 : c_begin_omp_parallel (void)
15787 : {
15788 12673 : tree block;
15789 :
15790 12673 : keep_next_level ();
15791 12673 : block = c_begin_compound_stmt (true);
15792 :
15793 12673 : return block;
15794 : }
15795 :
15796 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15797 : statement. LOC is the location of the OMP_PARALLEL. */
15798 :
15799 : tree
15800 6033 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15801 : {
15802 6033 : tree stmt;
15803 :
15804 6033 : block = c_end_compound_stmt (loc, block, true);
15805 :
15806 6033 : stmt = make_node (OMP_PARALLEL);
15807 6033 : TREE_TYPE (stmt) = void_type_node;
15808 6033 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15809 6033 : OMP_PARALLEL_BODY (stmt) = block;
15810 6033 : SET_EXPR_LOCATION (stmt, loc);
15811 :
15812 6033 : return add_stmt (stmt);
15813 : }
15814 :
15815 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15816 :
15817 : tree
15818 918 : c_begin_omp_task (void)
15819 : {
15820 918 : tree block;
15821 :
15822 918 : keep_next_level ();
15823 918 : block = c_begin_compound_stmt (true);
15824 :
15825 918 : return block;
15826 : }
15827 :
15828 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15829 : statement. LOC is the location of the #pragma. */
15830 :
15831 : tree
15832 918 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15833 : {
15834 918 : tree stmt;
15835 :
15836 918 : block = c_end_compound_stmt (loc, block, true);
15837 :
15838 918 : stmt = make_node (OMP_TASK);
15839 918 : TREE_TYPE (stmt) = void_type_node;
15840 918 : OMP_TASK_CLAUSES (stmt) = clauses;
15841 918 : OMP_TASK_BODY (stmt) = block;
15842 918 : SET_EXPR_LOCATION (stmt, loc);
15843 :
15844 918 : return add_stmt (stmt);
15845 : }
15846 :
15847 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15848 :
15849 : void
15850 221 : c_finish_omp_cancel (location_t loc, tree clauses)
15851 : {
15852 221 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15853 221 : int mask = 0;
15854 221 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15855 : mask = 1;
15856 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15857 : mask = 2;
15858 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15859 : mask = 4;
15860 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15861 : mask = 8;
15862 : else
15863 : {
15864 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15865 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15866 : "clauses");
15867 0 : return;
15868 : }
15869 221 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15870 221 : if (ifc != NULL_TREE)
15871 : {
15872 35 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15873 35 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15874 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15875 : "expected %<cancel%> %<if%> clause modifier");
15876 : else
15877 : {
15878 33 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15879 33 : if (ifc2 != NULL_TREE)
15880 : {
15881 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15882 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15883 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15884 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15885 : "expected %<cancel%> %<if%> clause modifier");
15886 : }
15887 : }
15888 :
15889 35 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15890 70 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15891 35 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15892 : build_zero_cst (type));
15893 : }
15894 : else
15895 186 : ifc = boolean_true_node;
15896 221 : tree stmt = build_call_expr_loc (loc, fn, 2,
15897 221 : build_int_cst (integer_type_node, mask),
15898 : ifc);
15899 221 : add_stmt (stmt);
15900 : }
15901 :
15902 : /* Generate GOMP_cancellation_point call for
15903 : #pragma omp cancellation point. */
15904 :
15905 : void
15906 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15907 : {
15908 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15909 167 : int mask = 0;
15910 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15911 : mask = 1;
15912 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15913 : mask = 2;
15914 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15915 : mask = 4;
15916 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15917 : mask = 8;
15918 : else
15919 : {
15920 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15921 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15922 : "clauses");
15923 1 : return;
15924 : }
15925 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15926 166 : build_int_cst (integer_type_node, mask));
15927 166 : add_stmt (stmt);
15928 : }
15929 :
15930 : /* Helper function for handle_omp_array_sections. Called recursively
15931 : to handle multiple array-section-subscripts. C is the clause,
15932 : T current expression (initially OMP_CLAUSE_DECL), which is either
15933 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15934 : expression if specified, TREE_VALUE length expression if specified,
15935 : TREE_CHAIN is what it has been specified after, or some decl.
15936 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15937 : set to true if any of the array-section-subscript could have length
15938 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15939 : first array-section-subscript which is known not to have length
15940 : of one. Given say:
15941 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15942 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15943 : all are or may have length of 1, array-section-subscript [:2] is the
15944 : first one known not to have length 1. For array-section-subscript
15945 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15946 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15947 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15948 : case though, as some lengths could be zero. */
15949 :
15950 : static tree
15951 6507 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15952 : bool &maybe_zero_len, unsigned int &first_non_one,
15953 : enum c_omp_region_type ort)
15954 : {
15955 6507 : tree ret, low_bound, length, type;
15956 6507 : bool openacc = (ort & C_ORT_ACC) != 0;
15957 6507 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15958 : {
15959 3033 : if (error_operand_p (t))
15960 2 : return error_mark_node;
15961 3031 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15962 3031 : ret = t;
15963 3031 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15964 2927 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15965 5644 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15966 : {
15967 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15968 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15969 6 : return error_mark_node;
15970 : }
15971 3025 : if (!ai.check_clause (c))
15972 0 : return error_mark_node;
15973 3025 : else if (ai.component_access_p ()
15974 3328 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15975 16 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15976 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15977 303 : t = ai.get_root_term (true);
15978 : else
15979 2722 : t = ai.unconverted_ref_origin ();
15980 3025 : if (t == error_mark_node)
15981 : return error_mark_node;
15982 3025 : if (!VAR_P (t)
15983 775 : && (ort == C_ORT_ACC || !EXPR_P (t))
15984 768 : && TREE_CODE (t) != PARM_DECL)
15985 : {
15986 5 : if (DECL_P (t))
15987 5 : error_at (OMP_CLAUSE_LOCATION (c),
15988 : "%qD is not a variable in %qs clause", t,
15989 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15990 : else
15991 0 : error_at (OMP_CLAUSE_LOCATION (c),
15992 : "%qE is not a variable in %qs clause", t,
15993 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15994 5 : return error_mark_node;
15995 : }
15996 3020 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15997 2917 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15998 5624 : && TYPE_ATOMIC (TREE_TYPE (t)))
15999 : {
16000 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
16001 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16002 0 : return error_mark_node;
16003 : }
16004 3020 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16005 2917 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16006 2604 : && VAR_P (t)
16007 5044 : && DECL_THREAD_LOCAL_P (t))
16008 : {
16009 3 : error_at (OMP_CLAUSE_LOCATION (c),
16010 : "%qD is threadprivate variable in %qs clause", t,
16011 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16012 3 : return error_mark_node;
16013 : }
16014 3017 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16015 2914 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
16016 416 : && TYPE_ATOMIC (TREE_TYPE (t))
16017 3019 : && POINTER_TYPE_P (TREE_TYPE (t)))
16018 : {
16019 : /* If the array section is pointer based and the pointer
16020 : itself is _Atomic qualified, we need to atomically load
16021 : the pointer. */
16022 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
16023 : ret, false, false);
16024 : }
16025 : return ret;
16026 3031 : }
16027 :
16028 3474 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
16029 : maybe_zero_len, first_non_one, ort);
16030 3474 : if (ret == error_mark_node || ret == NULL_TREE)
16031 : return ret;
16032 :
16033 3406 : type = TREE_TYPE (ret);
16034 3406 : low_bound = TREE_OPERAND (t, 1);
16035 3406 : length = TREE_OPERAND (t, 2);
16036 :
16037 3406 : if (low_bound == error_mark_node || length == error_mark_node)
16038 : return error_mark_node;
16039 :
16040 3406 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
16041 : {
16042 13 : error_at (OMP_CLAUSE_LOCATION (c),
16043 : "low bound %qE of array section does not have integral type",
16044 : low_bound);
16045 13 : return error_mark_node;
16046 : }
16047 3393 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
16048 : {
16049 12 : error_at (OMP_CLAUSE_LOCATION (c),
16050 : "length %qE of array section does not have integral type",
16051 : length);
16052 12 : return error_mark_node;
16053 : }
16054 3381 : if (low_bound
16055 2758 : && TREE_CODE (low_bound) == INTEGER_CST
16056 5847 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16057 2466 : > TYPE_PRECISION (sizetype))
16058 0 : low_bound = fold_convert (sizetype, low_bound);
16059 3381 : if (length
16060 3189 : && TREE_CODE (length) == INTEGER_CST
16061 5623 : && TYPE_PRECISION (TREE_TYPE (length))
16062 2242 : > TYPE_PRECISION (sizetype))
16063 0 : length = fold_convert (sizetype, length);
16064 3381 : if (low_bound == NULL_TREE)
16065 623 : low_bound = integer_zero_node;
16066 3381 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
16067 3381 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16068 1974 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
16069 : {
16070 10 : if (length != integer_one_node)
16071 : {
16072 6 : error_at (OMP_CLAUSE_LOCATION (c),
16073 : "expected single pointer in %qs clause",
16074 : user_omp_clause_code_name (c, openacc));
16075 6 : return error_mark_node;
16076 : }
16077 : }
16078 3375 : if (length != NULL_TREE)
16079 : {
16080 3187 : if (!integer_nonzerop (length))
16081 : {
16082 978 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16083 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16084 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16085 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16086 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16087 : {
16088 180 : if (integer_zerop (length))
16089 : {
16090 12 : error_at (OMP_CLAUSE_LOCATION (c),
16091 : "zero length array section in %qs clause",
16092 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16093 12 : return error_mark_node;
16094 : }
16095 : }
16096 : else
16097 798 : maybe_zero_len = true;
16098 : }
16099 3175 : if (first_non_one == types.length ()
16100 3175 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
16101 1630 : first_non_one++;
16102 : }
16103 3363 : if (TREE_CODE (type) == ARRAY_TYPE)
16104 : {
16105 1511 : if (length == NULL_TREE
16106 1511 : && (TYPE_DOMAIN (type) == NULL_TREE
16107 170 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
16108 : {
16109 8 : error_at (OMP_CLAUSE_LOCATION (c),
16110 : "for unknown bound array type length expression must "
16111 : "be specified");
16112 8 : return error_mark_node;
16113 : }
16114 1503 : if (TREE_CODE (low_bound) == INTEGER_CST
16115 1503 : && tree_int_cst_sgn (low_bound) == -1)
16116 : {
16117 36 : error_at (OMP_CLAUSE_LOCATION (c),
16118 : "negative low bound in array section in %qs clause",
16119 36 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16120 36 : return error_mark_node;
16121 : }
16122 1467 : if (length != NULL_TREE
16123 1310 : && TREE_CODE (length) == INTEGER_CST
16124 2337 : && tree_int_cst_sgn (length) == -1)
16125 : {
16126 36 : error_at (OMP_CLAUSE_LOCATION (c),
16127 : "negative length in array section in %qs clause",
16128 36 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16129 36 : return error_mark_node;
16130 : }
16131 1431 : if (TYPE_DOMAIN (type)
16132 1420 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
16133 2851 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
16134 : == INTEGER_CST)
16135 : {
16136 1074 : tree size
16137 1074 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
16138 1074 : size = size_binop (PLUS_EXPR, size, size_one_node);
16139 1074 : if (TREE_CODE (low_bound) == INTEGER_CST)
16140 : {
16141 887 : if (tree_int_cst_lt (size, low_bound))
16142 : {
16143 11 : error_at (OMP_CLAUSE_LOCATION (c),
16144 : "low bound %qE above array section size "
16145 : "in %qs clause", low_bound,
16146 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16147 11 : return error_mark_node;
16148 : }
16149 876 : if (tree_int_cst_equal (size, low_bound))
16150 : {
16151 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16152 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16153 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16154 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16155 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16156 : {
16157 5 : error_at (OMP_CLAUSE_LOCATION (c),
16158 : "zero length array section in %qs clause",
16159 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16160 5 : return error_mark_node;
16161 : }
16162 0 : maybe_zero_len = true;
16163 : }
16164 871 : else if (length == NULL_TREE
16165 256 : && first_non_one == types.length ()
16166 926 : && tree_int_cst_equal
16167 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16168 : low_bound))
16169 30 : first_non_one++;
16170 : }
16171 187 : else if (length == NULL_TREE)
16172 : {
16173 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16174 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16175 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16176 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16177 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16178 0 : maybe_zero_len = true;
16179 6 : if (first_non_one == types.length ())
16180 1 : first_non_one++;
16181 : }
16182 1058 : if (length && TREE_CODE (length) == INTEGER_CST)
16183 : {
16184 811 : if (tree_int_cst_lt (size, length))
16185 : {
16186 11 : error_at (OMP_CLAUSE_LOCATION (c),
16187 : "length %qE above array section size "
16188 : "in %qs clause", length,
16189 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16190 11 : return error_mark_node;
16191 : }
16192 800 : if (TREE_CODE (low_bound) == INTEGER_CST)
16193 : {
16194 700 : tree lbpluslen
16195 700 : = size_binop (PLUS_EXPR,
16196 : fold_convert (sizetype, low_bound),
16197 : fold_convert (sizetype, length));
16198 700 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16199 700 : && tree_int_cst_lt (size, lbpluslen))
16200 : {
16201 10 : error_at (OMP_CLAUSE_LOCATION (c),
16202 : "high bound %qE above array section size "
16203 : "in %qs clause", lbpluslen,
16204 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16205 10 : return error_mark_node;
16206 : }
16207 : }
16208 : }
16209 : }
16210 357 : else if (length == NULL_TREE)
16211 : {
16212 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16213 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16214 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16215 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16216 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16217 0 : maybe_zero_len = true;
16218 20 : if (first_non_one == types.length ())
16219 10 : first_non_one++;
16220 : }
16221 :
16222 : /* For [lb:] we will need to evaluate lb more than once. */
16223 941 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16224 : {
16225 127 : tree lb = save_expr (low_bound);
16226 127 : if (lb != low_bound)
16227 : {
16228 2 : TREE_OPERAND (t, 1) = lb;
16229 2 : low_bound = lb;
16230 : }
16231 : }
16232 : }
16233 1852 : else if (TREE_CODE (type) == POINTER_TYPE)
16234 : {
16235 1841 : if (length == NULL_TREE)
16236 : {
16237 12 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16238 10 : error_at (OMP_CLAUSE_LOCATION (c),
16239 : "for array function parameter length expression "
16240 : "must be specified");
16241 : else
16242 2 : error_at (OMP_CLAUSE_LOCATION (c),
16243 : "for pointer type length expression must be specified");
16244 12 : return error_mark_node;
16245 : }
16246 1829 : if (length != NULL_TREE
16247 1829 : && TREE_CODE (length) == INTEGER_CST
16248 1322 : && tree_int_cst_sgn (length) == -1)
16249 : {
16250 20 : error_at (OMP_CLAUSE_LOCATION (c),
16251 : "negative length in array section in %qs clause",
16252 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16253 20 : return error_mark_node;
16254 : }
16255 : /* If there is a pointer type anywhere but in the very first
16256 : array-section-subscript, the array section could be non-contiguous. */
16257 1809 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16258 1648 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16259 3428 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16260 : {
16261 : /* If any prior dimension has a non-one length, then deem this
16262 : array section as non-contiguous. */
16263 42 : for (tree d = TREE_OPERAND (t, 0);
16264 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16265 40 : d = TREE_OPERAND (d, 0))
16266 : {
16267 44 : tree d_length = TREE_OPERAND (d, 2);
16268 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16269 : {
16270 4 : error_at (OMP_CLAUSE_LOCATION (c),
16271 : "array section is not contiguous in %qs clause",
16272 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16273 4 : return error_mark_node;
16274 : }
16275 : }
16276 : }
16277 : }
16278 : else
16279 : {
16280 11 : error_at (OMP_CLAUSE_LOCATION (c),
16281 : "%qE does not have pointer or array type", ret);
16282 11 : return error_mark_node;
16283 : }
16284 3199 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16285 2883 : types.safe_push (TREE_TYPE (ret));
16286 : /* We will need to evaluate lb more than once. */
16287 3199 : tree lb = save_expr (low_bound);
16288 3199 : if (lb != low_bound)
16289 : {
16290 276 : TREE_OPERAND (t, 1) = lb;
16291 276 : low_bound = lb;
16292 : }
16293 3199 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16294 3199 : return ret;
16295 : }
16296 :
16297 : /* Handle array sections for clause C. */
16298 :
16299 : static bool
16300 3033 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16301 : {
16302 3033 : bool maybe_zero_len = false;
16303 3033 : unsigned int first_non_one = 0;
16304 3033 : auto_vec<tree, 10> types;
16305 3033 : tree *tp = &OMP_CLAUSE_DECL (c);
16306 3033 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16307 2719 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16308 3137 : && OMP_ITERATOR_DECL_P (*tp))
16309 66 : tp = &TREE_VALUE (*tp);
16310 3033 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16311 : maybe_zero_len, first_non_one,
16312 : ort);
16313 3033 : if (first == error_mark_node)
16314 : return true;
16315 2810 : if (first == NULL_TREE)
16316 : return false;
16317 2810 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16318 2810 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16319 : {
16320 336 : tree t = *tp;
16321 336 : tree tem = NULL_TREE;
16322 : /* Need to evaluate side effects in the length expressions
16323 : if any. */
16324 336 : while (TREE_CODE (t) == TREE_LIST)
16325 : {
16326 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16327 : {
16328 0 : if (tem == NULL_TREE)
16329 : tem = TREE_VALUE (t);
16330 : else
16331 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16332 0 : TREE_VALUE (t), tem);
16333 : }
16334 0 : t = TREE_CHAIN (t);
16335 : }
16336 336 : if (tem)
16337 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16338 336 : first = c_fully_fold (first, false, NULL, true);
16339 336 : *tp = first;
16340 : }
16341 : else
16342 : {
16343 2474 : unsigned int num = types.length (), i;
16344 2474 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16345 2474 : tree condition = NULL_TREE;
16346 :
16347 2474 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16348 3 : maybe_zero_len = true;
16349 :
16350 5148 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16351 2674 : t = TREE_OPERAND (t, 0))
16352 : {
16353 2689 : tree low_bound = TREE_OPERAND (t, 1);
16354 2689 : tree length = TREE_OPERAND (t, 2);
16355 :
16356 2689 : i--;
16357 2689 : if (low_bound
16358 2189 : && TREE_CODE (low_bound) == INTEGER_CST
16359 4689 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16360 2000 : > TYPE_PRECISION (sizetype))
16361 0 : low_bound = fold_convert (sizetype, low_bound);
16362 2689 : if (length
16363 2597 : && TREE_CODE (length) == INTEGER_CST
16364 4357 : && TYPE_PRECISION (TREE_TYPE (length))
16365 1668 : > TYPE_PRECISION (sizetype))
16366 0 : length = fold_convert (sizetype, length);
16367 2689 : if (low_bound == NULL_TREE)
16368 500 : low_bound = integer_zero_node;
16369 2689 : if (!maybe_zero_len && i > first_non_one)
16370 : {
16371 109 : if (integer_nonzerop (low_bound))
16372 6 : goto do_warn_noncontiguous;
16373 103 : if (length != NULL_TREE
16374 50 : && TREE_CODE (length) == INTEGER_CST
16375 50 : && TYPE_DOMAIN (types[i])
16376 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16377 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16378 : == INTEGER_CST)
16379 : {
16380 50 : tree size;
16381 50 : size = size_binop (PLUS_EXPR,
16382 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16383 : size_one_node);
16384 50 : if (!tree_int_cst_equal (length, size))
16385 : {
16386 7 : do_warn_noncontiguous:
16387 26 : error_at (OMP_CLAUSE_LOCATION (c),
16388 : "array section is not contiguous in %qs "
16389 : "clause",
16390 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16391 2474 : return true;
16392 : }
16393 : }
16394 96 : if (length != NULL_TREE
16395 96 : && TREE_SIDE_EFFECTS (length))
16396 : {
16397 0 : if (side_effects == NULL_TREE)
16398 : side_effects = length;
16399 : else
16400 0 : side_effects = build2 (COMPOUND_EXPR,
16401 0 : TREE_TYPE (side_effects),
16402 : length, side_effects);
16403 : }
16404 : }
16405 : else
16406 : {
16407 2580 : tree l;
16408 :
16409 2580 : if (i > first_non_one
16410 2580 : && ((length && integer_nonzerop (length))
16411 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16412 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16413 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16414 0 : continue;
16415 2580 : if (length)
16416 2547 : l = fold_convert (sizetype, length);
16417 : else
16418 : {
16419 33 : l = size_binop (PLUS_EXPR,
16420 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16421 : size_one_node);
16422 33 : l = size_binop (MINUS_EXPR, l,
16423 : fold_convert (sizetype, low_bound));
16424 : }
16425 2580 : if (i > first_non_one)
16426 : {
16427 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16428 : size_zero_node);
16429 0 : if (condition == NULL_TREE)
16430 : condition = l;
16431 : else
16432 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16433 : l, condition);
16434 : }
16435 2580 : else if (size == NULL_TREE)
16436 : {
16437 2461 : size = size_in_bytes (TREE_TYPE (types[i]));
16438 2461 : tree eltype = TREE_TYPE (types[num - 1]);
16439 2481 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16440 20 : eltype = TREE_TYPE (eltype);
16441 2461 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16442 2220 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16443 4421 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16444 : {
16445 545 : if (integer_zerop (size)
16446 1088 : || integer_zerop (size_in_bytes (eltype)))
16447 : {
16448 4 : error_at (OMP_CLAUSE_LOCATION (c),
16449 : "zero length array section in %qs clause",
16450 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16451 2 : return error_mark_node;
16452 : }
16453 543 : size = size_binop (EXACT_DIV_EXPR, size,
16454 : size_in_bytes (eltype));
16455 : }
16456 2459 : size = size_binop (MULT_EXPR, size, l);
16457 2459 : if (condition)
16458 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16459 : size, size_zero_node);
16460 : }
16461 : else
16462 119 : size = size_binop (MULT_EXPR, size, l);
16463 : }
16464 : }
16465 2459 : if (side_effects)
16466 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16467 2459 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16468 2220 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16469 4419 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16470 : {
16471 543 : size = size_binop (MINUS_EXPR, size, size_one_node);
16472 543 : size = c_fully_fold (size, false, NULL);
16473 543 : size = save_expr (size);
16474 543 : tree index_type = build_index_type (size);
16475 543 : tree eltype = TREE_TYPE (first);
16476 555 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16477 12 : eltype = TREE_TYPE (eltype);
16478 543 : tree type = c_build_array_type (eltype, index_type);
16479 543 : tree ptype = c_build_pointer_type (eltype);
16480 543 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16481 302 : t = build_fold_addr_expr (t);
16482 543 : tree t2 = build_fold_addr_expr (first);
16483 543 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16484 : ptrdiff_type_node, t2);
16485 543 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16486 : ptrdiff_type_node, t2,
16487 543 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16488 : ptrdiff_type_node, t));
16489 543 : t2 = c_fully_fold (t2, false, NULL);
16490 543 : if (tree_fits_shwi_p (t2))
16491 404 : t = build2 (MEM_REF, type, t,
16492 404 : build_int_cst (ptype, tree_to_shwi (t2)));
16493 : else
16494 : {
16495 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16496 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16497 139 : TREE_TYPE (t), t, t2);
16498 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16499 : }
16500 543 : OMP_CLAUSE_DECL (c) = t;
16501 543 : return false;
16502 : }
16503 1916 : first = c_fully_fold (first, false, NULL);
16504 1916 : OMP_CLAUSE_DECL (c) = first;
16505 1916 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16506 : return false;
16507 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16508 1905 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16509 1905 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16510 1759 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16511 1757 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16512 : {
16513 1901 : if (size)
16514 1901 : size = c_fully_fold (size, false, NULL);
16515 1901 : OMP_CLAUSE_SIZE (c) = size;
16516 : }
16517 :
16518 1905 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16519 : return false;
16520 :
16521 1761 : auto_vec<omp_addr_token *, 10> addr_tokens;
16522 :
16523 1761 : if (!omp_parse_expr (addr_tokens, first))
16524 1761 : return true;
16525 :
16526 1761 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16527 :
16528 1761 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16529 1761 : if (nc != error_mark_node)
16530 : {
16531 1761 : using namespace omp_addr_tokenizer;
16532 :
16533 1761 : if (ai.maybe_zero_length_array_section (c))
16534 1757 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16535 :
16536 : /* !!! If we're accessing a base decl via chained access
16537 : methods (e.g. multiple indirections), duplicate clause
16538 : detection won't work properly. Skip it in that case. */
16539 1761 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16540 1471 : || addr_tokens[0]->type == ARRAY_BASE)
16541 1761 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16542 1761 : && addr_tokens[1]->type == ACCESS_METHOD
16543 3522 : && omp_access_chain_p (addr_tokens, 1))
16544 36 : c = nc;
16545 :
16546 1761 : return false;
16547 : }
16548 : }
16549 : return false;
16550 3033 : }
16551 :
16552 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16553 : an inline call. But, remap
16554 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16555 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16556 :
16557 : static tree
16558 528 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16559 : tree decl, tree placeholder)
16560 : {
16561 528 : copy_body_data id;
16562 528 : hash_map<tree, tree> decl_map;
16563 :
16564 528 : decl_map.put (omp_decl1, placeholder);
16565 528 : decl_map.put (omp_decl2, decl);
16566 528 : memset (&id, 0, sizeof (id));
16567 528 : id.src_fn = DECL_CONTEXT (omp_decl1);
16568 528 : id.dst_fn = current_function_decl;
16569 528 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16570 528 : id.decl_map = &decl_map;
16571 :
16572 528 : id.copy_decl = copy_decl_no_change;
16573 528 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16574 528 : id.transform_new_cfg = true;
16575 528 : id.transform_return_to_modify = false;
16576 528 : id.eh_lp_nr = 0;
16577 528 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16578 528 : return stmt;
16579 528 : }
16580 :
16581 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16582 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16583 :
16584 : static tree
16585 1165 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16586 : {
16587 1165 : if (*tp == (tree) data)
16588 31 : return *tp;
16589 : return NULL_TREE;
16590 : }
16591 :
16592 : /* Similarly, but also walk aggregate fields. */
16593 :
16594 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16595 :
16596 : static tree
16597 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16598 : {
16599 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16600 : return *tp;
16601 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16602 : {
16603 2 : tree field;
16604 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16605 :
16606 2 : for (field = TYPE_FIELDS (*tp); field;
16607 0 : field = DECL_CHAIN (field))
16608 2 : if (TREE_CODE (field) == FIELD_DECL)
16609 : {
16610 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16611 : c_find_omp_var_r, data, pset);
16612 2 : if (ret)
16613 : return ret;
16614 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16615 2 : if (ret)
16616 : return ret;
16617 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16618 : pset);
16619 2 : if (ret)
16620 : return ret;
16621 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16622 2 : if (ret)
16623 : return ret;
16624 : }
16625 : }
16626 278 : else if (INTEGRAL_TYPE_P (*tp))
16627 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16628 : ((struct c_find_omp_var_s *) data)->pset);
16629 : return NULL_TREE;
16630 : }
16631 :
16632 : /* Finish OpenMP iterators ITER. Return true if they are erroneous
16633 : and clauses containing them should be removed. */
16634 :
16635 : static bool
16636 146 : c_omp_finish_iterators (tree iter)
16637 : {
16638 146 : bool ret = false;
16639 331 : for (tree it = iter; it; it = TREE_CHAIN (it))
16640 : {
16641 185 : tree var = OMP_ITERATOR_VAR (it);
16642 185 : tree begin = OMP_ITERATOR_BEGIN (it);
16643 185 : tree end = OMP_ITERATOR_END (it);
16644 185 : tree step = OMP_ITERATOR_STEP (it);
16645 185 : tree orig_step;
16646 185 : tree type = TREE_TYPE (var);
16647 185 : location_t loc = DECL_SOURCE_LOCATION (var);
16648 185 : if (type == error_mark_node)
16649 : {
16650 0 : ret = true;
16651 34 : continue;
16652 : }
16653 185 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16654 : {
16655 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16656 : var);
16657 6 : ret = true;
16658 6 : continue;
16659 : }
16660 179 : else if (TYPE_ATOMIC (type))
16661 : {
16662 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16663 2 : ret = true;
16664 2 : continue;
16665 : }
16666 177 : else if (TYPE_READONLY (type))
16667 : {
16668 4 : error_at (loc, "iterator %qD has const qualified type", var);
16669 4 : ret = true;
16670 4 : continue;
16671 : }
16672 173 : else if (step == error_mark_node
16673 173 : || TREE_TYPE (step) == error_mark_node)
16674 : {
16675 0 : ret = true;
16676 0 : continue;
16677 : }
16678 173 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16679 : {
16680 4 : error_at (EXPR_LOC_OR_LOC (step, loc),
16681 : "iterator step with non-integral type");
16682 4 : ret = true;
16683 4 : continue;
16684 : }
16685 169 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16686 169 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16687 169 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16688 169 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16689 169 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16690 169 : if (POINTER_TYPE_P (type))
16691 : {
16692 14 : begin = save_expr (begin);
16693 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16694 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16695 : fold_convert (sizetype, step),
16696 : fold_convert (sizetype, begin));
16697 14 : step = fold_convert (ssizetype, step);
16698 : }
16699 169 : if (integer_zerop (step))
16700 : {
16701 6 : error_at (loc, "iterator %qD has zero step", var);
16702 6 : ret = true;
16703 6 : continue;
16704 : }
16705 :
16706 163 : if (begin == error_mark_node
16707 161 : || end == error_mark_node
16708 159 : || step == error_mark_node
16709 159 : || orig_step == error_mark_node)
16710 : {
16711 4 : ret = true;
16712 4 : continue;
16713 : }
16714 159 : hash_set<tree> pset;
16715 159 : tree it2;
16716 200 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16717 : {
16718 49 : tree var2 = OMP_ITERATOR_VAR (it2);
16719 49 : tree begin2 = OMP_ITERATOR_BEGIN (it2);
16720 49 : tree end2 = OMP_ITERATOR_END (it2);
16721 49 : tree step2 = OMP_ITERATOR_STEP (it2);
16722 49 : tree type2 = TREE_TYPE (var2);
16723 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16724 49 : struct c_find_omp_var_s data = { var, &pset };
16725 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16726 : {
16727 2 : error_at (loc2,
16728 : "type of iterator %qD refers to outer iterator %qD",
16729 : var2, var);
16730 10 : break;
16731 : }
16732 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16733 : {
16734 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16735 : "begin expression refers to outer iterator %qD", var);
16736 2 : break;
16737 : }
16738 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16739 : {
16740 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16741 : "end expression refers to outer iterator %qD", var);
16742 2 : break;
16743 : }
16744 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16745 : {
16746 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16747 : "step expression refers to outer iterator %qD", var);
16748 2 : break;
16749 : }
16750 : }
16751 159 : if (it2)
16752 : {
16753 8 : ret = true;
16754 8 : continue;
16755 : }
16756 151 : OMP_ITERATOR_BEGIN (it) = begin;
16757 151 : OMP_ITERATOR_END (it) = end;
16758 151 : OMP_ITERATOR_STEP (it) = step;
16759 151 : OMP_ITERATOR_ORIG_STEP (it) = orig_step;
16760 159 : }
16761 146 : return ret;
16762 : }
16763 :
16764 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16765 : Return true if an error has been detected. */
16766 :
16767 : static bool
16768 10919 : c_oacc_check_attachments (tree c)
16769 : {
16770 10919 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16771 : return false;
16772 :
16773 : /* OpenACC attach / detach clauses must be pointers. */
16774 7206 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16775 7206 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16776 : {
16777 75 : tree t = OMP_CLAUSE_DECL (c);
16778 :
16779 81 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16780 6 : t = TREE_OPERAND (t, 0);
16781 :
16782 75 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16783 : {
16784 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16785 : user_omp_clause_code_name (c, true));
16786 6 : return true;
16787 : }
16788 : }
16789 :
16790 : return false;
16791 : }
16792 :
16793 : /* For all elements of CLAUSES, validate them against their constraints.
16794 : Remove any elements from the list that are invalid. */
16795 :
16796 : tree
16797 31862 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16798 : {
16799 31862 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16800 31862 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16801 31862 : bitmap_head oacc_reduction_head, is_on_device_head;
16802 31862 : tree c, t, type, *pc;
16803 31862 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16804 31862 : bool branch_seen = false;
16805 31862 : bool copyprivate_seen = false;
16806 31862 : bool mergeable_seen = false;
16807 31862 : tree *detach_seen = NULL;
16808 31862 : bool linear_variable_step_check = false;
16809 31862 : tree *nowait_clause = NULL;
16810 31862 : tree *grainsize_seen = NULL;
16811 31862 : bool num_tasks_seen = false;
16812 31862 : tree ordered_clause = NULL_TREE;
16813 31862 : tree schedule_clause = NULL_TREE;
16814 31862 : bool oacc_async = false;
16815 31862 : tree last_iterators = NULL_TREE;
16816 31862 : bool last_iterators_remove = false;
16817 31862 : tree *nogroup_seen = NULL;
16818 31862 : tree *order_clause = NULL;
16819 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16820 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16821 31862 : int reduction_seen = 0;
16822 31862 : bool allocate_seen = false;
16823 31862 : bool implicit_moved = false;
16824 31862 : bool target_in_reduction_seen = false;
16825 31862 : tree *full_seen = NULL;
16826 31862 : bool partial_seen = false;
16827 31862 : bool openacc = (ort & C_ORT_ACC) != 0;
16828 31862 : bool init_seen = false;
16829 31862 : bool init_use_destroy_seen = false;
16830 31862 : tree init_no_targetsync_clause = NULL_TREE;
16831 31862 : tree depend_clause = NULL_TREE;
16832 :
16833 31862 : if (!openacc)
16834 26625 : clauses = omp_remove_duplicate_maps (clauses, true);
16835 :
16836 31862 : bitmap_obstack_initialize (NULL);
16837 31862 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16838 31862 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16839 31862 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16840 31862 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16841 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16842 31862 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16843 31862 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16844 31862 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16845 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16846 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16847 31862 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16848 31862 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16849 :
16850 31862 : if (openacc)
16851 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16852 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16853 : {
16854 : oacc_async = true;
16855 : break;
16856 : }
16857 :
16858 31862 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16859 :
16860 83838 : for (pc = &clauses, c = clauses; c ; c = *pc)
16861 : {
16862 51976 : bool remove = false;
16863 51976 : bool need_complete = false;
16864 51976 : bool need_implicitly_determined = false;
16865 :
16866 : /* We've reached the end of a list of expanded nodes. Reset the group
16867 : start pointer. */
16868 51976 : if (c == grp_sentinel)
16869 : {
16870 3225 : if (grp_start_p
16871 3225 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16872 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16873 22 : gc = OMP_CLAUSE_CHAIN (gc))
16874 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16875 : grp_start_p = NULL;
16876 : }
16877 :
16878 51976 : switch (OMP_CLAUSE_CODE (c))
16879 : {
16880 1174 : case OMP_CLAUSE_SHARED:
16881 1174 : need_implicitly_determined = true;
16882 1174 : goto check_dup_generic;
16883 :
16884 881 : case OMP_CLAUSE_PRIVATE:
16885 881 : need_complete = true;
16886 881 : need_implicitly_determined = true;
16887 881 : goto check_dup_generic;
16888 :
16889 3880 : case OMP_CLAUSE_REDUCTION:
16890 3880 : if (reduction_seen == 0)
16891 3270 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16892 610 : else if (reduction_seen != -2
16893 1220 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16894 610 : ? -1 : 1))
16895 : {
16896 2 : error_at (OMP_CLAUSE_LOCATION (c),
16897 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16898 : "on the same construct");
16899 2 : reduction_seen = -2;
16900 : }
16901 : /* FALLTHRU */
16902 4545 : case OMP_CLAUSE_IN_REDUCTION:
16903 4545 : case OMP_CLAUSE_TASK_REDUCTION:
16904 4545 : need_implicitly_determined = true;
16905 4545 : t = OMP_CLAUSE_DECL (c);
16906 4545 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16907 : {
16908 559 : if (handle_omp_array_sections (c, ort))
16909 : {
16910 : remove = true;
16911 : break;
16912 : }
16913 :
16914 543 : t = OMP_CLAUSE_DECL (c);
16915 543 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16916 543 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16917 : {
16918 1 : error_at (OMP_CLAUSE_LOCATION (c),
16919 : "%<inscan%> %<reduction%> clause with array "
16920 : "section");
16921 1 : remove = true;
16922 1 : break;
16923 : }
16924 : }
16925 4528 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16926 4528 : if (t == error_mark_node)
16927 : {
16928 : remove = true;
16929 : break;
16930 : }
16931 4526 : if (oacc_async)
16932 3 : c_mark_addressable (t);
16933 4526 : type = TREE_TYPE (t);
16934 4526 : if (TREE_CODE (t) == MEM_REF)
16935 542 : type = TREE_TYPE (type);
16936 4526 : if (TREE_CODE (type) == ARRAY_TYPE)
16937 : {
16938 74 : tree oatype = type;
16939 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16940 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16941 74 : type = TREE_TYPE (type);
16942 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16943 : {
16944 1 : error_at (OMP_CLAUSE_LOCATION (c),
16945 : "%qD in %<reduction%> clause is a zero size array",
16946 : t);
16947 1 : remove = true;
16948 1 : break;
16949 : }
16950 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16951 : TYPE_SIZE_UNIT (type));
16952 73 : if (integer_zerop (size))
16953 : {
16954 1 : error_at (OMP_CLAUSE_LOCATION (c),
16955 : "%qD in %<reduction%> clause is a zero size array",
16956 : t);
16957 1 : remove = true;
16958 1 : break;
16959 : }
16960 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16961 72 : size = save_expr (size);
16962 72 : tree index_type = build_index_type (size);
16963 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16964 : index_type);
16965 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16966 72 : tree ptype = c_build_pointer_type (type);
16967 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16968 72 : t = build_fold_addr_expr (t);
16969 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16970 72 : OMP_CLAUSE_DECL (c) = t;
16971 : }
16972 4524 : if (TYPE_ATOMIC (type))
16973 : {
16974 3 : error_at (OMP_CLAUSE_LOCATION (c),
16975 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16976 3 : remove = true;
16977 3 : break;
16978 : }
16979 4521 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16980 4521 : || OMP_CLAUSE_REDUCTION_TASK (c))
16981 : {
16982 : /* Disallow zero sized or potentially zero sized task
16983 : reductions. */
16984 875 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16985 : {
16986 6 : error_at (OMP_CLAUSE_LOCATION (c),
16987 : "zero sized type %qT in %qs clause", type,
16988 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16989 3 : remove = true;
16990 3 : break;
16991 : }
16992 872 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16993 : {
16994 0 : error_at (OMP_CLAUSE_LOCATION (c),
16995 : "variable sized type %qT in %qs clause", type,
16996 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16997 0 : remove = true;
16998 0 : break;
16999 : }
17000 : }
17001 4518 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
17002 4518 : && (FLOAT_TYPE_P (type)
17003 3855 : || TREE_CODE (type) == COMPLEX_TYPE))
17004 : {
17005 442 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
17006 442 : const char *r_name = NULL;
17007 :
17008 442 : switch (r_code)
17009 : {
17010 : case PLUS_EXPR:
17011 : case MULT_EXPR:
17012 : case MINUS_EXPR:
17013 : case TRUTH_ANDIF_EXPR:
17014 : case TRUTH_ORIF_EXPR:
17015 : break;
17016 25 : case MIN_EXPR:
17017 25 : if (TREE_CODE (type) == COMPLEX_TYPE)
17018 : r_name = "min";
17019 : break;
17020 56 : case MAX_EXPR:
17021 56 : if (TREE_CODE (type) == COMPLEX_TYPE)
17022 : r_name = "max";
17023 : break;
17024 3 : case BIT_AND_EXPR:
17025 3 : r_name = "&";
17026 3 : break;
17027 2 : case BIT_XOR_EXPR:
17028 2 : r_name = "^";
17029 2 : break;
17030 : case BIT_IOR_EXPR:
17031 : r_name = "|";
17032 : break;
17033 0 : default:
17034 0 : gcc_unreachable ();
17035 : }
17036 5 : if (r_name)
17037 : {
17038 12 : error_at (OMP_CLAUSE_LOCATION (c),
17039 : "%qE has invalid type for %<reduction(%s)%>",
17040 : t, r_name);
17041 12 : remove = true;
17042 12 : break;
17043 : }
17044 : }
17045 4076 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
17046 : {
17047 2 : error_at (OMP_CLAUSE_LOCATION (c),
17048 : "user defined reduction not found for %qE", t);
17049 2 : remove = true;
17050 2 : break;
17051 : }
17052 4074 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
17053 : {
17054 286 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
17055 286 : type = TYPE_MAIN_VARIANT (type);
17056 286 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
17057 : VAR_DECL, NULL_TREE, type);
17058 286 : tree decl_placeholder = NULL_TREE;
17059 286 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
17060 286 : DECL_ARTIFICIAL (placeholder) = 1;
17061 286 : DECL_IGNORED_P (placeholder) = 1;
17062 286 : if (TREE_CODE (t) == MEM_REF)
17063 : {
17064 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
17065 : VAR_DECL, NULL_TREE, type);
17066 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
17067 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
17068 56 : DECL_IGNORED_P (decl_placeholder) = 1;
17069 : }
17070 286 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
17071 45 : c_mark_addressable (placeholder);
17072 286 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
17073 82 : c_mark_addressable (decl_placeholder ? decl_placeholder
17074 37 : : OMP_CLAUSE_DECL (c));
17075 286 : OMP_CLAUSE_REDUCTION_MERGE (c)
17076 802 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
17077 286 : TREE_VEC_ELT (list, 0),
17078 286 : TREE_VEC_ELT (list, 1),
17079 : decl_placeholder ? decl_placeholder
17080 230 : : OMP_CLAUSE_DECL (c), placeholder);
17081 286 : OMP_CLAUSE_REDUCTION_MERGE (c)
17082 286 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
17083 : void_type_node, NULL_TREE,
17084 286 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
17085 286 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
17086 286 : if (TREE_VEC_LENGTH (list) == 6)
17087 : {
17088 242 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
17089 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
17090 33 : : OMP_CLAUSE_DECL (c));
17091 242 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
17092 30 : c_mark_addressable (placeholder);
17093 242 : tree init = TREE_VEC_ELT (list, 5);
17094 242 : if (init == error_mark_node)
17095 206 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
17096 242 : OMP_CLAUSE_REDUCTION_INIT (c)
17097 678 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
17098 242 : TREE_VEC_ELT (list, 3),
17099 : decl_placeholder ? decl_placeholder
17100 194 : : OMP_CLAUSE_DECL (c), placeholder);
17101 242 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
17102 : {
17103 206 : tree v = decl_placeholder ? decl_placeholder : t;
17104 206 : OMP_CLAUSE_REDUCTION_INIT (c)
17105 412 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
17106 206 : OMP_CLAUSE_REDUCTION_INIT (c));
17107 : }
17108 242 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
17109 : c_find_omp_placeholder_r,
17110 : placeholder, NULL))
17111 31 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
17112 : }
17113 : else
17114 : {
17115 44 : tree init;
17116 44 : tree v = decl_placeholder ? decl_placeholder : t;
17117 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
17118 34 : init = build_constructor (TREE_TYPE (v), NULL);
17119 : else
17120 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
17121 44 : OMP_CLAUSE_REDUCTION_INIT (c)
17122 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
17123 : }
17124 286 : OMP_CLAUSE_REDUCTION_INIT (c)
17125 286 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
17126 : void_type_node, NULL_TREE,
17127 286 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
17128 286 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
17129 : }
17130 4504 : if (TREE_CODE (t) == MEM_REF)
17131 : {
17132 613 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
17133 613 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
17134 : != INTEGER_CST)
17135 : {
17136 0 : sorry ("variable length element type in array "
17137 : "%<reduction%> clause");
17138 0 : remove = true;
17139 0 : break;
17140 : }
17141 613 : t = TREE_OPERAND (t, 0);
17142 613 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
17143 139 : t = TREE_OPERAND (t, 0);
17144 613 : if (TREE_CODE (t) == ADDR_EXPR)
17145 373 : t = TREE_OPERAND (t, 0);
17146 : }
17147 4504 : goto check_dup_generic_t;
17148 :
17149 27 : case OMP_CLAUSE_COPYPRIVATE:
17150 27 : copyprivate_seen = true;
17151 27 : if (nowait_clause)
17152 : {
17153 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
17154 : "%<nowait%> clause must not be used together "
17155 : "with %<copyprivate%> clause");
17156 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
17157 1 : nowait_clause = NULL;
17158 : }
17159 27 : goto check_dup_generic;
17160 :
17161 100 : case OMP_CLAUSE_COPYIN:
17162 100 : t = OMP_CLAUSE_DECL (c);
17163 100 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17164 : {
17165 5 : error_at (OMP_CLAUSE_LOCATION (c),
17166 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17167 5 : remove = true;
17168 5 : break;
17169 : }
17170 95 : goto check_dup_generic;
17171 :
17172 485 : case OMP_CLAUSE_LINEAR:
17173 485 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17174 330 : need_implicitly_determined = true;
17175 485 : t = OMP_CLAUSE_DECL (c);
17176 485 : if (ort != C_ORT_OMP_DECLARE_SIMD
17177 330 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17178 490 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17179 : {
17180 5 : error_at (OMP_CLAUSE_LOCATION (c),
17181 : "modifier should not be specified in %<linear%> "
17182 : "clause on %<simd%> or %<for%> constructs when not "
17183 : "using OpenMP 5.2 modifiers");
17184 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17185 : }
17186 970 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17187 510 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17188 : {
17189 1 : error_at (OMP_CLAUSE_LOCATION (c),
17190 : "linear clause applied to non-integral non-pointer "
17191 1 : "variable with type %qT", TREE_TYPE (t));
17192 1 : remove = true;
17193 1 : break;
17194 : }
17195 484 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17196 : {
17197 3 : error_at (OMP_CLAUSE_LOCATION (c),
17198 : "%<_Atomic%> %qD in %<linear%> clause", t);
17199 3 : remove = true;
17200 3 : break;
17201 : }
17202 481 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17203 : {
17204 154 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17205 154 : if (TREE_CODE (s) == PARM_DECL)
17206 : {
17207 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17208 : /* map_head bitmap is used as uniform_head if
17209 : declare_simd. */
17210 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17211 5 : linear_variable_step_check = true;
17212 9 : goto check_dup_generic;
17213 : }
17214 145 : if (TREE_CODE (s) != INTEGER_CST)
17215 : {
17216 7 : error_at (OMP_CLAUSE_LOCATION (c),
17217 : "%<linear%> clause step %qE is neither constant "
17218 : "nor a parameter", s);
17219 7 : remove = true;
17220 7 : break;
17221 : }
17222 : }
17223 465 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17224 : {
17225 22 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17226 22 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17227 22 : OMP_CLAUSE_DECL (c), s);
17228 22 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17229 : sizetype, fold_convert (sizetype, s),
17230 22 : fold_convert
17231 : (sizetype, OMP_CLAUSE_DECL (c)));
17232 22 : if (s == error_mark_node)
17233 0 : s = size_one_node;
17234 22 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17235 : }
17236 : else
17237 443 : OMP_CLAUSE_LINEAR_STEP (c)
17238 886 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17239 465 : goto check_dup_generic;
17240 :
17241 2967 : check_dup_generic:
17242 2967 : t = OMP_CLAUSE_DECL (c);
17243 7560 : check_dup_generic_t:
17244 7560 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17245 : {
17246 4 : error_at (OMP_CLAUSE_LOCATION (c),
17247 : "%qE is not a variable in clause %qs", t,
17248 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17249 2 : remove = true;
17250 : }
17251 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17252 6519 : || (ort == C_ORT_OMP
17253 5794 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17254 5783 : || (OMP_CLAUSE_CODE (c)
17255 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17256 14030 : || (ort == C_ORT_OMP_TARGET
17257 349 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17258 : {
17259 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17260 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17261 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17262 : {
17263 2 : error_at (OMP_CLAUSE_LOCATION (c),
17264 : "%qD appears more than once in data-sharing "
17265 : "clauses", t);
17266 2 : remove = true;
17267 2 : break;
17268 : }
17269 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17270 108 : target_in_reduction_seen = true;
17271 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17272 : {
17273 4 : error_at (OMP_CLAUSE_LOCATION (c),
17274 : openacc
17275 : ? "%qD appears more than once in reduction clauses"
17276 : : "%qD appears more than once in data clauses",
17277 : t);
17278 2 : remove = true;
17279 : }
17280 : else
17281 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17282 : }
17283 6362 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17284 6339 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17285 6338 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17286 12700 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17287 : {
17288 24 : error_at (OMP_CLAUSE_LOCATION (c),
17289 : "%qE appears more than once in data clauses", t);
17290 24 : remove = true;
17291 : }
17292 6338 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17293 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17294 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17295 1081 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17296 : {
17297 3 : if (openacc)
17298 0 : error_at (OMP_CLAUSE_LOCATION (c),
17299 : "%qD appears more than once in data clauses", t);
17300 : else
17301 3 : error_at (OMP_CLAUSE_LOCATION (c),
17302 : "%qD appears both in data and map clauses", t);
17303 : remove = true;
17304 : }
17305 : else
17306 6335 : bitmap_set_bit (&generic_head, DECL_UID (t));
17307 : break;
17308 :
17309 1641 : case OMP_CLAUSE_FIRSTPRIVATE:
17310 1641 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17311 : {
17312 292 : move_implicit:
17313 292 : implicit_moved = true;
17314 : /* Move firstprivate and map clauses with
17315 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17316 : clauses chain. */
17317 292 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17318 292 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17319 1298 : while (*pc1)
17320 1006 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17321 1006 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17322 : {
17323 181 : *pc3 = *pc1;
17324 181 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17325 181 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17326 : }
17327 825 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17328 825 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17329 : {
17330 244 : *pc2 = *pc1;
17331 244 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17332 244 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17333 : }
17334 : else
17335 581 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17336 292 : *pc3 = NULL;
17337 292 : *pc2 = cl2;
17338 292 : *pc1 = cl1;
17339 292 : continue;
17340 292 : }
17341 1518 : t = OMP_CLAUSE_DECL (c);
17342 1518 : need_complete = true;
17343 1518 : need_implicitly_determined = true;
17344 1518 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17345 : {
17346 1 : error_at (OMP_CLAUSE_LOCATION (c),
17347 : "%qE is not a variable in clause %<firstprivate%>", t);
17348 1 : remove = true;
17349 : }
17350 1517 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17351 181 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17352 1669 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17353 : remove = true;
17354 1517 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17355 1515 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17356 3031 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17357 : {
17358 4 : error_at (OMP_CLAUSE_LOCATION (c),
17359 : "%qE appears more than once in data clauses", t);
17360 4 : remove = true;
17361 : }
17362 1513 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17363 1513 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17364 : {
17365 7 : if (openacc)
17366 0 : error_at (OMP_CLAUSE_LOCATION (c),
17367 : "%qD appears more than once in data clauses", t);
17368 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17369 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17370 : /* Silently drop the clause. */;
17371 : else
17372 6 : error_at (OMP_CLAUSE_LOCATION (c),
17373 : "%qD appears both in data and map clauses", t);
17374 : remove = true;
17375 : }
17376 : else
17377 1506 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17378 : break;
17379 :
17380 1619 : case OMP_CLAUSE_LASTPRIVATE:
17381 1619 : t = OMP_CLAUSE_DECL (c);
17382 1619 : need_complete = true;
17383 1619 : need_implicitly_determined = true;
17384 1619 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17385 : {
17386 0 : error_at (OMP_CLAUSE_LOCATION (c),
17387 : "%qE is not a variable in clause %<lastprivate%>", t);
17388 0 : remove = true;
17389 : }
17390 1619 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17391 1619 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17392 : {
17393 3 : error_at (OMP_CLAUSE_LOCATION (c),
17394 : "%qE appears more than once in data clauses", t);
17395 3 : remove = true;
17396 : }
17397 : else
17398 1616 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17399 : break;
17400 :
17401 253 : case OMP_CLAUSE_ALIGNED:
17402 253 : t = OMP_CLAUSE_DECL (c);
17403 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17404 : {
17405 0 : error_at (OMP_CLAUSE_LOCATION (c),
17406 : "%qE is not a variable in %<aligned%> clause", t);
17407 0 : remove = true;
17408 : }
17409 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17410 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17411 : {
17412 7 : error_at (OMP_CLAUSE_LOCATION (c),
17413 : "%qE in %<aligned%> clause is neither a pointer nor "
17414 : "an array", t);
17415 7 : remove = true;
17416 : }
17417 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17418 : {
17419 1 : error_at (OMP_CLAUSE_LOCATION (c),
17420 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17421 1 : remove = true;
17422 1 : break;
17423 : }
17424 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17425 : {
17426 0 : error_at (OMP_CLAUSE_LOCATION (c),
17427 : "%qE appears more than once in %<aligned%> clauses",
17428 : t);
17429 0 : remove = true;
17430 : }
17431 : else
17432 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17433 : break;
17434 :
17435 125 : case OMP_CLAUSE_NONTEMPORAL:
17436 125 : t = OMP_CLAUSE_DECL (c);
17437 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17438 : {
17439 0 : error_at (OMP_CLAUSE_LOCATION (c),
17440 : "%qE is not a variable in %<nontemporal%> clause", t);
17441 0 : remove = true;
17442 : }
17443 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17444 : {
17445 2 : error_at (OMP_CLAUSE_LOCATION (c),
17446 : "%qE appears more than once in %<nontemporal%> "
17447 : "clauses", t);
17448 2 : remove = true;
17449 : }
17450 : else
17451 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17452 : break;
17453 :
17454 814 : case OMP_CLAUSE_ALLOCATE:
17455 814 : t = OMP_CLAUSE_DECL (c);
17456 814 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17457 : {
17458 1 : error_at (OMP_CLAUSE_LOCATION (c),
17459 : "%qE is not a variable in %<allocate%> clause", t);
17460 1 : remove = true;
17461 : }
17462 813 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17463 : {
17464 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17465 : "%qE appears more than once in %<allocate%> clauses",
17466 : t);
17467 2 : remove = true;
17468 : }
17469 : else
17470 : {
17471 811 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17472 811 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17473 711 : allocate_seen = true;
17474 : }
17475 : break;
17476 :
17477 357 : case OMP_CLAUSE_DOACROSS:
17478 357 : t = OMP_CLAUSE_DECL (c);
17479 357 : if (t == NULL_TREE)
17480 : break;
17481 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17482 : {
17483 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17484 1315 : for (; t; t = TREE_CHAIN (t))
17485 : {
17486 1146 : tree decl = TREE_VALUE (t);
17487 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17488 : {
17489 3 : tree offset = TREE_PURPOSE (t);
17490 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17491 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17492 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17493 : neg ? MINUS_EXPR : PLUS_EXPR,
17494 : decl, offset);
17495 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17496 : sizetype,
17497 : fold_convert (sizetype, t2),
17498 : fold_convert (sizetype, decl));
17499 3 : if (t2 == error_mark_node)
17500 : {
17501 : remove = true;
17502 : break;
17503 : }
17504 3 : TREE_PURPOSE (t) = t2;
17505 : }
17506 : }
17507 : break;
17508 : }
17509 0 : gcc_unreachable ();
17510 :
17511 64 : case OMP_CLAUSE_USES_ALLOCATORS:
17512 64 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17513 64 : if (t == error_mark_node)
17514 : {
17515 : remove = true;
17516 : break;
17517 : }
17518 8 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17519 60 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17520 52 : || bitmap_bit_p (&map_head, DECL_UID (t))
17521 52 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17522 51 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17523 : {
17524 1 : error_at (OMP_CLAUSE_LOCATION (c),
17525 : "%qE appears more than once in data clauses", t);
17526 1 : remove = true;
17527 1 : break;
17528 : }
17529 : else
17530 58 : bitmap_set_bit (&generic_head, DECL_UID (t));
17531 58 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17532 113 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17533 : "omp_allocator_handle_t") != 0)
17534 : {
17535 3 : error_at (OMP_CLAUSE_LOCATION (c),
17536 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17537 : "type", t);
17538 3 : remove = true;
17539 3 : break;
17540 : }
17541 55 : tree init;
17542 55 : if (!DECL_P (t)
17543 55 : || (TREE_CODE (t) == CONST_DECL
17544 7 : && ((init = DECL_INITIAL(t)) == nullptr
17545 7 : || TREE_CODE (init) != INTEGER_CST
17546 7 : || ((wi::to_widest (init) < 0
17547 7 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17548 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17549 0 : || (wi::to_widest (init)
17550 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17551 : {
17552 0 : remove = true;
17553 0 : error_at (OMP_CLAUSE_LOCATION (c),
17554 : "allocator %qE must be either a variable or a "
17555 : "predefined allocator", t);
17556 0 : break;
17557 : }
17558 55 : else if (TREE_CODE (t) == CONST_DECL)
17559 : {
17560 : /* omp_null_allocator is ignored and for predefined allocators,
17561 : no special handling is required; thus, mark them removed. */
17562 7 : remove = true;
17563 :
17564 7 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17565 7 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17566 : {
17567 0 : error_at (OMP_CLAUSE_LOCATION (c),
17568 : "modifiers cannot be used with predefined "
17569 : "allocator %qE", t);
17570 0 : break;
17571 : }
17572 : }
17573 55 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17574 55 : if (t == error_mark_node)
17575 : {
17576 : remove = true;
17577 : break;
17578 : }
17579 55 : if (t != NULL_TREE
17580 55 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17581 11 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17582 20 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17583 : "omp_memspace_handle_t") != 0))
17584 : {
17585 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17586 : " constant enum of %<omp_memspace_handle_t%> type", t);
17587 1 : remove = true;
17588 1 : break;
17589 : }
17590 54 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17591 54 : if (t == error_mark_node)
17592 : {
17593 : remove = true;
17594 : break;
17595 : }
17596 54 : if (t != NULL_TREE
17597 : && t != error_mark_node
17598 54 : && (DECL_EXTERNAL (t)
17599 25 : || TREE_CODE (t) == PARM_DECL))
17600 : {
17601 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17602 : "defined in same scope as the construct on which the "
17603 : "clause appears", t);
17604 3 : remove = true;
17605 : }
17606 54 : if (t != NULL_TREE)
17607 : {
17608 27 : bool type_err = false;
17609 :
17610 27 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17611 26 : || DECL_SIZE (t) == NULL_TREE
17612 52 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17613 : type_err = true;
17614 : else
17615 : {
17616 25 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17617 25 : if (TREE_CODE (elem_t) != RECORD_TYPE
17618 50 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17619 : "omp_alloctrait_t") != 0
17620 50 : || !TYPE_READONLY (elem_t))
17621 : type_err = true;
17622 : }
17623 24 : if (type_err)
17624 : {
17625 3 : if (t != error_mark_node)
17626 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17627 : "be of %<const omp_alloctrait_t []%> type", t);
17628 : else
17629 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17630 : "be of %<const omp_alloctrait_t []%> type");
17631 : remove = true;
17632 : }
17633 : else
17634 : {
17635 24 : tree cst_val = decl_constant_value_1 (t, true);
17636 24 : if (cst_val == t)
17637 : {
17638 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17639 : "initialized with constants");
17640 :
17641 1 : remove = true;
17642 : }
17643 : }
17644 : }
17645 54 : if (remove)
17646 : break;
17647 43 : pc = &OMP_CLAUSE_CHAIN (c);
17648 43 : continue;
17649 713 : case OMP_CLAUSE_DEPEND:
17650 713 : depend_clause = c;
17651 : /* FALLTHRU */
17652 875 : case OMP_CLAUSE_AFFINITY:
17653 875 : t = OMP_CLAUSE_DECL (c);
17654 875 : if (OMP_ITERATOR_DECL_P (t))
17655 : {
17656 131 : if (TREE_PURPOSE (t) != last_iterators)
17657 111 : last_iterators_remove
17658 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17659 131 : last_iterators = TREE_PURPOSE (t);
17660 131 : t = TREE_VALUE (t);
17661 131 : if (last_iterators_remove)
17662 34 : t = error_mark_node;
17663 : }
17664 : else
17665 : last_iterators = NULL_TREE;
17666 875 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17667 : {
17668 418 : if (handle_omp_array_sections (c, ort))
17669 : remove = true;
17670 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17671 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17672 : {
17673 1 : error_at (OMP_CLAUSE_LOCATION (c),
17674 : "%<depend%> clause with %<depobj%> dependence "
17675 : "type on array section");
17676 1 : remove = true;
17677 : }
17678 : break;
17679 : }
17680 457 : if (t == error_mark_node)
17681 : remove = true;
17682 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17683 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17684 : {
17685 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17686 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17687 : {
17688 3 : error_at (OMP_CLAUSE_LOCATION (c),
17689 : "%<omp_all_memory%> used with %<depend%> kind "
17690 : "other than %<out%> or %<inout%>");
17691 3 : remove = true;
17692 : }
17693 : }
17694 408 : else if (!lvalue_p (t))
17695 : {
17696 6 : error_at (OMP_CLAUSE_LOCATION (c),
17697 : "%qE is not lvalue expression nor array section in "
17698 : "%qs clause", t,
17699 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17700 3 : remove = true;
17701 : }
17702 405 : else if (TREE_CODE (t) == COMPONENT_REF
17703 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17704 : {
17705 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17706 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17707 2 : error_at (OMP_CLAUSE_LOCATION (c),
17708 : "bit-field %qE in %qs clause", t,
17709 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17710 2 : remove = true;
17711 : }
17712 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17713 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17714 : {
17715 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17716 : {
17717 2 : error_at (OMP_CLAUSE_LOCATION (c),
17718 : "%qE does not have %<omp_depend_t%> type in "
17719 : "%<depend%> clause with %<depobj%> dependence "
17720 : "type", t);
17721 2 : remove = true;
17722 : }
17723 : }
17724 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17725 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17726 : {
17727 2 : error_at (OMP_CLAUSE_LOCATION (c),
17728 : "%qE should not have %<omp_depend_t%> type in "
17729 : "%<depend%> clause with dependence type other than "
17730 : "%<depobj%>", t);
17731 2 : remove = true;
17732 : }
17733 12 : if (!remove)
17734 : {
17735 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17736 12 : t = null_pointer_node;
17737 : else
17738 : {
17739 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17740 : ADDR_EXPR, t, false);
17741 399 : if (addr == error_mark_node)
17742 : {
17743 : remove = true;
17744 : break;
17745 : }
17746 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17747 : RO_UNARY_STAR);
17748 399 : if (t == error_mark_node)
17749 : {
17750 : remove = true;
17751 : break;
17752 : }
17753 : }
17754 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17755 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17756 : else
17757 380 : OMP_CLAUSE_DECL (c) = t;
17758 : }
17759 : break;
17760 :
17761 7423 : case OMP_CLAUSE_MAP:
17762 7423 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17763 169 : goto move_implicit;
17764 7254 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17765 7254 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17766 : {
17767 : remove = true;
17768 : break;
17769 : }
17770 : /* FALLTHRU */
17771 10813 : case OMP_CLAUSE_TO:
17772 10813 : case OMP_CLAUSE_FROM:
17773 10813 : if (OMP_CLAUSE_ITERATORS (c)
17774 10813 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17775 : {
17776 25129 : t = error_mark_node;
17777 : break;
17778 : }
17779 : /* FALLTHRU */
17780 10949 : case OMP_CLAUSE__CACHE_:
17781 10949 : {
17782 10949 : using namespace omp_addr_tokenizer;
17783 10949 : auto_vec<omp_addr_token *, 10> addr_tokens;
17784 :
17785 10949 : t = OMP_CLAUSE_DECL (c);
17786 10949 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17787 : {
17788 2045 : grp_start_p = pc;
17789 2045 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17790 :
17791 2045 : if (handle_omp_array_sections (c, ort))
17792 : remove = true;
17793 : else
17794 : {
17795 1905 : t = OMP_CLAUSE_DECL (c);
17796 1905 : if (!omp_mappable_type (TREE_TYPE (t)))
17797 : {
17798 2 : error_at (OMP_CLAUSE_LOCATION (c),
17799 : "array section does not have mappable type "
17800 : "in %qs clause",
17801 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17802 1 : remove = true;
17803 : }
17804 1904 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17805 : {
17806 2 : error_at (OMP_CLAUSE_LOCATION (c),
17807 : "%<_Atomic%> %qE in %qs clause", t,
17808 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17809 1 : remove = true;
17810 : }
17811 2562 : while (TREE_CODE (t) == ARRAY_REF)
17812 657 : t = TREE_OPERAND (t, 0);
17813 :
17814 1905 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17815 :
17816 1905 : if (!omp_parse_expr (addr_tokens, t))
17817 : {
17818 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17819 : "unsupported map expression %qE",
17820 0 : OMP_CLAUSE_DECL (c));
17821 0 : remove = true;
17822 0 : break;
17823 : }
17824 :
17825 : /* This check is to determine if this will be the only map
17826 : node created for this clause. Otherwise, we'll check
17827 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17828 : node on the next iteration(s) of the loop. */
17829 1905 : if (addr_tokens.length () >= 4
17830 308 : && addr_tokens[0]->type == STRUCTURE_BASE
17831 306 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17832 306 : && addr_tokens[1]->type == ACCESS_METHOD
17833 306 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17834 306 : && addr_tokens[3]->type == ACCESS_METHOD
17835 2150 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17836 192 : || (addr_tokens[3]->u.access_kind
17837 : == ACCESS_INDEXED_ARRAY)))
17838 : {
17839 57 : tree rt = addr_tokens[1]->expr;
17840 :
17841 57 : gcc_assert (DECL_P (rt));
17842 :
17843 57 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17844 52 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17845 57 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17846 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17847 0 : || bitmap_bit_p (&map_firstprivate_head,
17848 0 : DECL_UID (rt))))
17849 : {
17850 : remove = true;
17851 : break;
17852 : }
17853 57 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17854 : break;
17855 39 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17856 : {
17857 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17858 0 : error_at (OMP_CLAUSE_LOCATION (c),
17859 : "%qD appears more than once in motion "
17860 : "clauses", rt);
17861 0 : else if (openacc)
17862 0 : error_at (OMP_CLAUSE_LOCATION (c),
17863 : "%qD appears more than once in data "
17864 : "clauses", rt);
17865 : else
17866 0 : error_at (OMP_CLAUSE_LOCATION (c),
17867 : "%qD appears more than once in map "
17868 : "clauses", rt);
17869 : remove = true;
17870 : }
17871 : else
17872 : {
17873 39 : bitmap_set_bit (&map_head, DECL_UID (rt));
17874 39 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17875 : }
17876 : }
17877 : }
17878 2027 : if (c_oacc_check_attachments (c))
17879 2 : remove = true;
17880 2027 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17881 1845 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17882 1825 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17883 2054 : && !OMP_CLAUSE_SIZE (c))
17884 : /* In this case, we have a single array element which is a
17885 : pointer, and we already set OMP_CLAUSE_SIZE in
17886 : handle_omp_array_sections above. For attach/detach
17887 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17888 : to zero here. */
17889 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17890 : break;
17891 : }
17892 8904 : else if (!omp_parse_expr (addr_tokens, t))
17893 : {
17894 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17895 : "unsupported map expression %qE",
17896 0 : OMP_CLAUSE_DECL (c));
17897 0 : remove = true;
17898 0 : break;
17899 : }
17900 8904 : if (t == error_mark_node)
17901 : {
17902 : remove = true;
17903 : break;
17904 : }
17905 :
17906 8892 : tree attr;
17907 8892 : if (DECL_P (t)
17908 8303 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17909 4813 : && (attr = lookup_attribute ("omp declare target",
17910 4813 : DECL_ATTRIBUTES (t)))
17911 9132 : && value_member (get_identifier ("local"),
17912 240 : TREE_VALUE (attr)))
17913 : {
17914 0 : error_at (OMP_CLAUSE_LOCATION (c),
17915 : "device-local variable %qD cannot appear "
17916 : "in map clause", t);
17917 0 : remove = true;
17918 0 : break;
17919 : }
17920 :
17921 : /* OpenACC attach / detach clauses must be pointers. */
17922 8892 : if (c_oacc_check_attachments (c))
17923 : {
17924 : remove = true;
17925 : break;
17926 : }
17927 8888 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17928 5357 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17929 5328 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17930 8932 : && !OMP_CLAUSE_SIZE (c))
17931 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17932 : bias) to zero here, so it is not set erroneously to the pointer
17933 : size later on in gimplify.cc. */
17934 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17935 :
17936 8888 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17937 :
17938 8888 : if (!ai.check_clause (c))
17939 : {
17940 : remove = true;
17941 : break;
17942 : }
17943 :
17944 8880 : if (!ai.map_supported_p ())
17945 : {
17946 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17947 : "unsupported map expression %qE",
17948 7 : OMP_CLAUSE_DECL (c));
17949 7 : remove = true;
17950 7 : break;
17951 : }
17952 :
17953 17746 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17954 : || addr_tokens[0]->type == STRUCTURE_BASE)
17955 : && addr_tokens[1]->type == ACCESS_METHOD);
17956 :
17957 8873 : t = addr_tokens[1]->expr;
17958 :
17959 8873 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17960 0 : goto skip_decl_checks;
17961 :
17962 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17963 : mapping. OpenACC allows multiple fields of the same structure
17964 : to be written. */
17965 8873 : if (addr_tokens[0]->type == STRUCTURE_BASE
17966 8873 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17967 267 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17968 309 : goto skip_decl_checks;
17969 :
17970 8564 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17971 : {
17972 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17973 : break;
17974 :
17975 6 : error_at (OMP_CLAUSE_LOCATION (c),
17976 : "%qE is not a variable in %qs clause", t,
17977 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17978 3 : remove = true;
17979 : }
17980 8561 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17981 : {
17982 0 : error_at (OMP_CLAUSE_LOCATION (c),
17983 : "%qD is threadprivate variable in %qs clause", t,
17984 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17985 0 : remove = true;
17986 : }
17987 8561 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17988 5057 : || (OMP_CLAUSE_MAP_KIND (c)
17989 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17990 12548 : && !c_mark_addressable (t))
17991 : remove = true;
17992 8561 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17993 5057 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17994 5057 : || (OMP_CLAUSE_MAP_KIND (c)
17995 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17996 3987 : || (OMP_CLAUSE_MAP_KIND (c)
17997 : == GOMP_MAP_FORCE_DEVICEPTR)))
17998 7433 : && t == OMP_CLAUSE_DECL (c)
17999 15729 : && !omp_mappable_type (TREE_TYPE (t)))
18000 : {
18001 16 : error_at (OMP_CLAUSE_LOCATION (c),
18002 : "%qD does not have a mappable type in %qs clause", t,
18003 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18004 8 : remove = true;
18005 : }
18006 8553 : else if (TREE_TYPE (t) == error_mark_node)
18007 : remove = true;
18008 8552 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
18009 : {
18010 8 : error_at (OMP_CLAUSE_LOCATION (c),
18011 : "%<_Atomic%> %qE in %qs clause", t,
18012 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18013 4 : remove = true;
18014 : }
18015 8548 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
18016 5047 : && OMP_CLAUSE_MAP_IMPLICIT (c)
18017 8792 : && (bitmap_bit_p (&map_head, DECL_UID (t))
18018 243 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
18019 243 : || bitmap_bit_p (&map_firstprivate_head,
18020 243 : DECL_UID (t))))
18021 : remove = true;
18022 8546 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
18023 8546 : && (OMP_CLAUSE_MAP_KIND (c)
18024 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
18025 : {
18026 1068 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
18027 1068 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
18028 2135 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
18029 : {
18030 4 : error_at (OMP_CLAUSE_LOCATION (c),
18031 : "%qD appears more than once in data clauses", t);
18032 4 : remove = true;
18033 : }
18034 1064 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
18035 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
18036 1066 : && openacc)
18037 : {
18038 1 : error_at (OMP_CLAUSE_LOCATION (c),
18039 : "%qD appears more than once in data clauses", t);
18040 1 : remove = true;
18041 : }
18042 : else
18043 1063 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
18044 : }
18045 7478 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
18046 67 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
18047 20 : && ort != C_ORT_OMP
18048 20 : && ort != C_ORT_OMP_TARGET
18049 7488 : && ort != C_ORT_OMP_EXIT_DATA)
18050 : {
18051 7 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
18052 0 : error_at (OMP_CLAUSE_LOCATION (c),
18053 : "%qD appears more than once in motion clauses", t);
18054 7 : else if (openacc)
18055 7 : error_at (OMP_CLAUSE_LOCATION (c),
18056 : "%qD appears more than once in data clauses", t);
18057 : else
18058 0 : error_at (OMP_CLAUSE_LOCATION (c),
18059 : "%qD appears more than once in map clauses", t);
18060 : remove = true;
18061 : }
18062 7471 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
18063 : {
18064 0 : error_at (OMP_CLAUSE_LOCATION (c),
18065 : "%qD appears more than once in data clauses", t);
18066 0 : remove = true;
18067 : }
18068 7471 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
18069 7471 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
18070 : {
18071 9 : if (openacc)
18072 0 : error_at (OMP_CLAUSE_LOCATION (c),
18073 : "%qD appears more than once in data clauses", t);
18074 : else
18075 9 : error_at (OMP_CLAUSE_LOCATION (c),
18076 : "%qD appears both in data and map clauses", t);
18077 : remove = true;
18078 : }
18079 7462 : else if (!omp_access_chain_p (addr_tokens, 1))
18080 : {
18081 7462 : bitmap_set_bit (&map_head, DECL_UID (t));
18082 7462 : if (t != OMP_CLAUSE_DECL (c)
18083 7462 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
18084 261 : bitmap_set_bit (&map_field_head, DECL_UID (t));
18085 : }
18086 :
18087 0 : skip_decl_checks:
18088 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
18089 : the containing loop (here) iterates through the new nodes
18090 : created by that expansion. Avoid expanding those again (just
18091 : by checking the node type). */
18092 8873 : if (!remove
18093 8873 : && ort != C_ORT_DECLARE_SIMD
18094 8873 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
18095 5307 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
18096 4244 : && (OMP_CLAUSE_MAP_KIND (c)
18097 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
18098 4244 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
18099 4244 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
18100 3939 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
18101 3910 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
18102 : {
18103 7422 : grp_start_p = pc;
18104 7422 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
18105 7422 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
18106 : addr_tokens, ort);
18107 7422 : if (nc != error_mark_node)
18108 7422 : c = nc;
18109 : }
18110 10964 : }
18111 8873 : break;
18112 :
18113 276 : case OMP_CLAUSE_ENTER:
18114 276 : case OMP_CLAUSE_LINK:
18115 276 : case OMP_CLAUSE_LOCAL:
18116 276 : t = OMP_CLAUSE_DECL (c);
18117 276 : const char *cname;
18118 276 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
18119 276 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
18120 276 : && OMP_CLAUSE_ENTER_TO (c))
18121 : cname = "to";
18122 276 : if (TREE_CODE (t) == FUNCTION_DECL
18123 276 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
18124 : ;
18125 194 : else if (!VAR_P (t))
18126 : {
18127 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
18128 0 : error_at (OMP_CLAUSE_LOCATION (c),
18129 : "%qE is neither a variable nor a function name in "
18130 : "clause %qs", t, cname);
18131 : else
18132 1 : error_at (OMP_CLAUSE_LOCATION (c),
18133 : "%qE is not a variable in clause %qs", t, cname);
18134 : remove = true;
18135 : }
18136 193 : else if (DECL_THREAD_LOCAL_P (t))
18137 : {
18138 2 : error_at (OMP_CLAUSE_LOCATION (c),
18139 : "%qD is threadprivate variable in %qs clause", t,
18140 : cname);
18141 2 : remove = true;
18142 : }
18143 191 : else if (!omp_mappable_type (TREE_TYPE (t)))
18144 : {
18145 6 : error_at (OMP_CLAUSE_LOCATION (c),
18146 : "%qD does not have a mappable type in %qs clause", t,
18147 : cname);
18148 6 : remove = true;
18149 : }
18150 9 : if (remove)
18151 : break;
18152 267 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18153 : {
18154 10 : error_at (OMP_CLAUSE_LOCATION (c),
18155 : "%qE appears more than once on the same "
18156 : "%<declare target%> directive", t);
18157 10 : remove = true;
18158 : }
18159 : else
18160 257 : bitmap_set_bit (&generic_head, DECL_UID (t));
18161 : break;
18162 :
18163 119 : case OMP_CLAUSE_UNIFORM:
18164 119 : t = OMP_CLAUSE_DECL (c);
18165 119 : if (TREE_CODE (t) != PARM_DECL)
18166 : {
18167 0 : if (DECL_P (t))
18168 0 : error_at (OMP_CLAUSE_LOCATION (c),
18169 : "%qD is not an argument in %<uniform%> clause", t);
18170 : else
18171 0 : error_at (OMP_CLAUSE_LOCATION (c),
18172 : "%qE is not an argument in %<uniform%> clause", t);
18173 : remove = true;
18174 : break;
18175 : }
18176 : /* map_head bitmap is used as uniform_head if declare_simd. */
18177 119 : bitmap_set_bit (&map_head, DECL_UID (t));
18178 119 : goto check_dup_generic;
18179 :
18180 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18181 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18182 161 : t = OMP_CLAUSE_DECL (c);
18183 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18184 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18185 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18186 : {
18187 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18188 21 : && !openacc)
18189 : {
18190 1 : error_at (OMP_CLAUSE_LOCATION (c),
18191 : "%qs variable is not a pointer",
18192 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18193 1 : remove = true;
18194 : }
18195 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18196 : {
18197 4 : error_at (OMP_CLAUSE_LOCATION (c),
18198 : "%qs variable is neither a pointer nor an array",
18199 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18200 4 : remove = true;
18201 : }
18202 : }
18203 161 : goto check_dup_generic;
18204 :
18205 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18206 89 : t = OMP_CLAUSE_DECL (c);
18207 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18208 : {
18209 11 : if (handle_omp_array_sections (c, ort))
18210 : remove = true;
18211 : else
18212 : {
18213 11 : t = OMP_CLAUSE_DECL (c);
18214 24 : while (TREE_CODE (t) == ARRAY_REF)
18215 13 : t = TREE_OPERAND (t, 0);
18216 : }
18217 : }
18218 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18219 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18220 89 : c_mark_addressable (t);
18221 89 : goto check_dup_generic_t;
18222 :
18223 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18224 36 : t = OMP_CLAUSE_DECL (c);
18225 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18226 36 : c_mark_addressable (t);
18227 36 : goto check_dup_generic;
18228 :
18229 5107 : case OMP_CLAUSE_NOWAIT:
18230 5107 : if (copyprivate_seen)
18231 : {
18232 1 : error_at (OMP_CLAUSE_LOCATION (c),
18233 : "%<nowait%> clause must not be used together "
18234 : "with %<copyprivate%> clause");
18235 1 : remove = true;
18236 1 : break;
18237 : }
18238 5106 : nowait_clause = pc;
18239 5106 : pc = &OMP_CLAUSE_CHAIN (c);
18240 5106 : continue;
18241 :
18242 523 : case OMP_CLAUSE_ORDER:
18243 523 : if (ordered_clause)
18244 : {
18245 2 : error_at (OMP_CLAUSE_LOCATION (c),
18246 : "%<order%> clause must not be used together "
18247 : "with %<ordered%> clause");
18248 2 : remove = true;
18249 2 : break;
18250 : }
18251 521 : else if (order_clause)
18252 : {
18253 : /* Silently remove duplicates. */
18254 : remove = true;
18255 : break;
18256 : }
18257 513 : order_clause = pc;
18258 513 : pc = &OMP_CLAUSE_CHAIN (c);
18259 513 : continue;
18260 :
18261 32 : case OMP_CLAUSE_DETACH:
18262 32 : t = OMP_CLAUSE_DECL (c);
18263 32 : if (detach_seen)
18264 : {
18265 1 : error_at (OMP_CLAUSE_LOCATION (c),
18266 : "too many %qs clauses on a task construct",
18267 : "detach");
18268 1 : remove = true;
18269 1 : break;
18270 : }
18271 31 : detach_seen = pc;
18272 31 : pc = &OMP_CLAUSE_CHAIN (c);
18273 31 : c_mark_addressable (t);
18274 31 : continue;
18275 :
18276 15731 : case OMP_CLAUSE_IF:
18277 15731 : case OMP_CLAUSE_SELF:
18278 15731 : case OMP_CLAUSE_NUM_THREADS:
18279 15731 : case OMP_CLAUSE_NUM_TEAMS:
18280 15731 : case OMP_CLAUSE_THREAD_LIMIT:
18281 15731 : case OMP_CLAUSE_DEFAULT:
18282 15731 : case OMP_CLAUSE_UNTIED:
18283 15731 : case OMP_CLAUSE_COLLAPSE:
18284 15731 : case OMP_CLAUSE_FINAL:
18285 15731 : case OMP_CLAUSE_DEVICE:
18286 15731 : case OMP_CLAUSE_DIST_SCHEDULE:
18287 15731 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18288 15731 : case OMP_CLAUSE_PARALLEL:
18289 15731 : case OMP_CLAUSE_FOR:
18290 15731 : case OMP_CLAUSE_SECTIONS:
18291 15731 : case OMP_CLAUSE_TASKGROUP:
18292 15731 : case OMP_CLAUSE_PROC_BIND:
18293 15731 : case OMP_CLAUSE_DEVICE_TYPE:
18294 15731 : case OMP_CLAUSE_PRIORITY:
18295 15731 : case OMP_CLAUSE_THREADS:
18296 15731 : case OMP_CLAUSE_SIMD:
18297 15731 : case OMP_CLAUSE_HINT:
18298 15731 : case OMP_CLAUSE_FILTER:
18299 15731 : case OMP_CLAUSE_DEFAULTMAP:
18300 15731 : case OMP_CLAUSE_BIND:
18301 15731 : case OMP_CLAUSE_NUM_GANGS:
18302 15731 : case OMP_CLAUSE_NUM_WORKERS:
18303 15731 : case OMP_CLAUSE_VECTOR_LENGTH:
18304 15731 : case OMP_CLAUSE_ASYNC:
18305 15731 : case OMP_CLAUSE_WAIT:
18306 15731 : case OMP_CLAUSE_AUTO:
18307 15731 : case OMP_CLAUSE_INDEPENDENT:
18308 15731 : case OMP_CLAUSE_SEQ:
18309 15731 : case OMP_CLAUSE_GANG:
18310 15731 : case OMP_CLAUSE_WORKER:
18311 15731 : case OMP_CLAUSE_VECTOR:
18312 15731 : case OMP_CLAUSE_TILE:
18313 15731 : case OMP_CLAUSE_IF_PRESENT:
18314 15731 : case OMP_CLAUSE_FINALIZE:
18315 15731 : case OMP_CLAUSE_NOHOST:
18316 15731 : case OMP_CLAUSE_INDIRECT:
18317 15731 : case OMP_CLAUSE_NOVARIANTS:
18318 15731 : case OMP_CLAUSE_NOCONTEXT:
18319 15731 : pc = &OMP_CLAUSE_CHAIN (c);
18320 15731 : continue;
18321 :
18322 62 : case OMP_CLAUSE_GRAINSIZE:
18323 62 : grainsize_seen = pc;
18324 62 : pc = &OMP_CLAUSE_CHAIN (c);
18325 62 : continue;
18326 :
18327 50 : case OMP_CLAUSE_NUM_TASKS:
18328 50 : num_tasks_seen = true;
18329 50 : pc = &OMP_CLAUSE_CHAIN (c);
18330 50 : continue;
18331 :
18332 79 : case OMP_CLAUSE_MERGEABLE:
18333 79 : mergeable_seen = true;
18334 79 : pc = &OMP_CLAUSE_CHAIN (c);
18335 79 : continue;
18336 :
18337 18 : case OMP_CLAUSE_NOGROUP:
18338 18 : nogroup_seen = pc;
18339 18 : pc = &OMP_CLAUSE_CHAIN (c);
18340 18 : continue;
18341 :
18342 3483 : case OMP_CLAUSE_SCHEDULE:
18343 3483 : schedule_clause = c;
18344 3483 : pc = &OMP_CLAUSE_CHAIN (c);
18345 3483 : continue;
18346 :
18347 304 : case OMP_CLAUSE_ORDERED:
18348 304 : ordered_clause = c;
18349 304 : if (order_clause)
18350 : {
18351 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18352 : "%<order%> clause must not be used together "
18353 : "with %<ordered%> clause");
18354 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18355 2 : order_clause = NULL;
18356 : }
18357 304 : pc = &OMP_CLAUSE_CHAIN (c);
18358 304 : continue;
18359 :
18360 263 : case OMP_CLAUSE_SAFELEN:
18361 263 : safelen = c;
18362 263 : pc = &OMP_CLAUSE_CHAIN (c);
18363 263 : continue;
18364 358 : case OMP_CLAUSE_SIMDLEN:
18365 358 : simdlen = c;
18366 358 : pc = &OMP_CLAUSE_CHAIN (c);
18367 358 : continue;
18368 :
18369 69 : case OMP_CLAUSE_FULL:
18370 69 : full_seen = pc;
18371 69 : pc = &OMP_CLAUSE_CHAIN (c);
18372 69 : continue;
18373 :
18374 223 : case OMP_CLAUSE_PARTIAL:
18375 223 : partial_seen = true;
18376 223 : pc = &OMP_CLAUSE_CHAIN (c);
18377 223 : continue;
18378 :
18379 0 : case OMP_CLAUSE_SIZES:
18380 0 : pc = &OMP_CLAUSE_CHAIN (c);
18381 0 : continue;
18382 :
18383 223 : case OMP_CLAUSE_INBRANCH:
18384 223 : case OMP_CLAUSE_NOTINBRANCH:
18385 223 : if (branch_seen)
18386 : {
18387 1 : error_at (OMP_CLAUSE_LOCATION (c),
18388 : "%<inbranch%> clause is incompatible with "
18389 : "%<notinbranch%>");
18390 1 : remove = true;
18391 1 : break;
18392 : }
18393 222 : branch_seen = true;
18394 222 : pc = &OMP_CLAUSE_CHAIN (c);
18395 222 : continue;
18396 :
18397 371 : case OMP_CLAUSE_INCLUSIVE:
18398 371 : case OMP_CLAUSE_EXCLUSIVE:
18399 371 : need_complete = true;
18400 371 : need_implicitly_determined = true;
18401 371 : t = OMP_CLAUSE_DECL (c);
18402 371 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18403 : {
18404 0 : error_at (OMP_CLAUSE_LOCATION (c),
18405 : "%qE is not a variable in clause %qs", t,
18406 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18407 0 : remove = true;
18408 : }
18409 : break;
18410 :
18411 152 : case OMP_CLAUSE_INIT:
18412 152 : init_seen = true;
18413 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18414 71 : init_no_targetsync_clause = c;
18415 : /* FALLTHRU */
18416 242 : case OMP_CLAUSE_DESTROY:
18417 242 : case OMP_CLAUSE_USE:
18418 242 : init_use_destroy_seen = true;
18419 242 : t = OMP_CLAUSE_DECL (c);
18420 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18421 : {
18422 3 : error_at (OMP_CLAUSE_LOCATION (c),
18423 : "%qD appears more than once in action clauses", t);
18424 3 : remove = true;
18425 3 : break;
18426 : }
18427 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18428 : /* FALLTHRU */
18429 298 : case OMP_CLAUSE_INTEROP:
18430 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18431 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18432 : {
18433 40 : error_at (OMP_CLAUSE_LOCATION (c),
18434 : "%qD must be of %<omp_interop_t%>",
18435 20 : OMP_CLAUSE_DECL (c));
18436 20 : remove = true;
18437 : }
18438 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18439 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18440 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18441 : {
18442 12 : error_at (OMP_CLAUSE_LOCATION (c),
18443 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18444 6 : remove = true;
18445 : }
18446 298 : pc = &OMP_CLAUSE_CHAIN (c);
18447 298 : break;
18448 0 : default:
18449 0 : gcc_unreachable ();
18450 26555 : }
18451 :
18452 25129 : if (!remove)
18453 : {
18454 24561 : t = OMP_CLAUSE_DECL (c);
18455 :
18456 24561 : if (need_complete)
18457 : {
18458 4367 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18459 4367 : if (t == error_mark_node)
18460 7 : remove = true;
18461 : }
18462 :
18463 24561 : if (need_implicitly_determined)
18464 : {
18465 10366 : const char *share_name = NULL;
18466 :
18467 10366 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18468 : share_name = "threadprivate";
18469 10360 : else switch (c_omp_predetermined_sharing (t))
18470 : {
18471 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18472 : break;
18473 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18474 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18475 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18476 28 : && c_omp_predefined_variable (t))
18477 : /* The __func__ variable and similar function-local
18478 : predefined variables may be listed in a shared or
18479 : firstprivate clause. */
18480 : break;
18481 : share_name = "shared";
18482 : break;
18483 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18484 : share_name = "private";
18485 : break;
18486 0 : default:
18487 0 : gcc_unreachable ();
18488 : }
18489 : if (share_name)
18490 : {
18491 20 : error_at (OMP_CLAUSE_LOCATION (c),
18492 : "%qE is predetermined %qs for %qs",
18493 : t, share_name,
18494 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18495 10 : remove = true;
18496 : }
18497 10356 : else if (TREE_READONLY (t)
18498 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18499 10369 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18500 : {
18501 4 : error_at (OMP_CLAUSE_LOCATION (c),
18502 : "%<const%> qualified %qE may appear only in "
18503 : "%<shared%> or %<firstprivate%> clauses", t);
18504 4 : remove = true;
18505 : }
18506 : }
18507 : }
18508 :
18509 24561 : if (remove)
18510 : {
18511 589 : if (grp_start_p)
18512 : {
18513 : /* If we found a clause to remove, we want to remove the whole
18514 : expanded group, otherwise gimplify
18515 : (omp_resolve_clause_dependencies) can get confused. */
18516 153 : *grp_start_p = grp_sentinel;
18517 153 : pc = grp_start_p;
18518 153 : grp_start_p = NULL;
18519 : }
18520 : else
18521 436 : *pc = OMP_CLAUSE_CHAIN (c);
18522 : }
18523 : else
18524 24540 : pc = &OMP_CLAUSE_CHAIN (c);
18525 : }
18526 :
18527 31862 : if (grp_start_p
18528 31862 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18529 58 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18530 35 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18531 :
18532 31862 : if (simdlen
18533 31862 : && safelen
18534 31979 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18535 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18536 : {
18537 : /* Note: How a scaling factor affects this, is underspecified in OpenMP;
18538 : cf. OpenMP spec issue #5160. Presumably, it means:
18539 : savelen >= simdlen * scale_factor
18540 : with
18541 : scale_factor = MAX (1, FLOOR_DIV_EXPR (VL(type), divisor))
18542 : and OpenMP states that VL(type) might be only known at runtime.
18543 : Hence scale_factor >= 1 and, conservatively, assume == 1 for this
18544 : error check. */
18545 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18546 : "%<simdlen%> clause value is bigger than "
18547 : "%<safelen%> clause value");
18548 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18549 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18550 : }
18551 :
18552 31862 : if (ordered_clause
18553 31862 : && schedule_clause
18554 31862 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18555 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18556 : {
18557 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18558 : "%<nonmonotonic%> schedule modifier specified together "
18559 : "with %<ordered%> clause");
18560 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18561 7 : = (enum omp_clause_schedule_kind)
18562 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18563 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18564 : }
18565 :
18566 31862 : if (reduction_seen < 0 && ordered_clause)
18567 : {
18568 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18569 : "%qs clause specified together with %<inscan%> "
18570 : "%<reduction%> clause", "ordered");
18571 2 : reduction_seen = -2;
18572 : }
18573 :
18574 31862 : if (reduction_seen < 0 && schedule_clause)
18575 : {
18576 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18577 : "%qs clause specified together with %<inscan%> "
18578 : "%<reduction%> clause", "schedule");
18579 3 : reduction_seen = -2;
18580 : }
18581 :
18582 31862 : if (linear_variable_step_check
18583 31862 : || reduction_seen == -2
18584 : || allocate_seen
18585 31850 : || target_in_reduction_seen)
18586 5225 : for (pc = &clauses, c = clauses; c ; c = *pc)
18587 : {
18588 4618 : bool remove = false;
18589 4618 : if (allocate_seen)
18590 4460 : switch (OMP_CLAUSE_CODE (c))
18591 : {
18592 430 : case OMP_CLAUSE_REDUCTION:
18593 430 : case OMP_CLAUSE_IN_REDUCTION:
18594 430 : case OMP_CLAUSE_TASK_REDUCTION:
18595 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18596 : {
18597 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18598 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18599 0 : t = TREE_OPERAND (t, 0);
18600 23 : if (TREE_CODE (t) == ADDR_EXPR
18601 10 : || INDIRECT_REF_P (t))
18602 13 : t = TREE_OPERAND (t, 0);
18603 23 : if (DECL_P (t))
18604 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18605 : break;
18606 : }
18607 : /* FALLTHRU */
18608 1329 : case OMP_CLAUSE_PRIVATE:
18609 1329 : case OMP_CLAUSE_FIRSTPRIVATE:
18610 1329 : case OMP_CLAUSE_LASTPRIVATE:
18611 1329 : case OMP_CLAUSE_LINEAR:
18612 1329 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18613 2658 : bitmap_clear_bit (&aligned_head,
18614 1329 : DECL_UID (OMP_CLAUSE_DECL (c)));
18615 : break;
18616 : default:
18617 : break;
18618 : }
18619 4618 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18620 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18621 4625 : && !bitmap_bit_p (&map_head,
18622 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18623 : {
18624 2 : error_at (OMP_CLAUSE_LOCATION (c),
18625 : "%<linear%> clause step is a parameter %qD not "
18626 : "specified in %<uniform%> clause",
18627 2 : OMP_CLAUSE_LINEAR_STEP (c));
18628 2 : remove = true;
18629 : }
18630 4616 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18631 4616 : && reduction_seen == -2)
18632 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18633 4618 : if (target_in_reduction_seen
18634 4618 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18635 : {
18636 256 : tree t = OMP_CLAUSE_DECL (c);
18637 256 : while (handled_component_p (t)
18638 : || INDIRECT_REF_P (t)
18639 : || TREE_CODE (t) == ADDR_EXPR
18640 : || TREE_CODE (t) == MEM_REF
18641 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18642 32 : t = TREE_OPERAND (t, 0);
18643 256 : if (DECL_P (t)
18644 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18645 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18646 : }
18647 :
18648 4618 : if (remove)
18649 2 : *pc = OMP_CLAUSE_CHAIN (c);
18650 : else
18651 4616 : pc = &OMP_CLAUSE_CHAIN (c);
18652 : }
18653 :
18654 607 : if (allocate_seen)
18655 5041 : for (pc = &clauses, c = clauses; c ; c = *pc)
18656 : {
18657 4460 : bool remove = false;
18658 4460 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18659 731 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18660 5171 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18661 : {
18662 3 : error_at (OMP_CLAUSE_LOCATION (c),
18663 : "%qD specified in %<allocate%> clause but not in "
18664 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18665 3 : remove = true;
18666 : }
18667 4460 : if (remove)
18668 3 : *pc = OMP_CLAUSE_CHAIN (c);
18669 : else
18670 4457 : pc = &OMP_CLAUSE_CHAIN (c);
18671 : }
18672 :
18673 31862 : if (nogroup_seen && reduction_seen)
18674 : {
18675 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18676 : "%<nogroup%> clause must not be used together with "
18677 : "%<reduction%> clause");
18678 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18679 : }
18680 :
18681 31862 : if (grainsize_seen && num_tasks_seen)
18682 : {
18683 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18684 : "%<grainsize%> clause must not be used together with "
18685 : "%<num_tasks%> clause");
18686 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18687 : }
18688 :
18689 31862 : if (full_seen && partial_seen)
18690 : {
18691 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18692 : "%<full%> clause must not be used together with "
18693 : "%<partial%> clause");
18694 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18695 : }
18696 :
18697 31862 : if (detach_seen)
18698 : {
18699 31 : if (mergeable_seen)
18700 : {
18701 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18702 : "%<detach%> clause must not be used together with "
18703 : "%<mergeable%> clause");
18704 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18705 : }
18706 : else
18707 : {
18708 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18709 :
18710 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18711 : {
18712 50 : bool remove = false;
18713 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18714 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18715 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18716 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18717 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18718 : {
18719 2 : error_at (OMP_CLAUSE_LOCATION (c),
18720 : "the event handle of a %<detach%> clause "
18721 : "should not be in a data-sharing clause");
18722 2 : remove = true;
18723 : }
18724 50 : if (remove)
18725 2 : *pc = OMP_CLAUSE_CHAIN (c);
18726 : else
18727 48 : pc = &OMP_CLAUSE_CHAIN (c);
18728 : }
18729 : }
18730 : }
18731 :
18732 31862 : if (ort == C_ORT_OMP_INTEROP
18733 31862 : && depend_clause
18734 21 : && (!init_use_destroy_seen
18735 20 : || (init_seen && init_no_targetsync_clause)))
18736 : {
18737 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18738 : "%<depend%> clause requires action clauses with "
18739 : "%<targetsync%> interop-type");
18740 3 : if (init_no_targetsync_clause)
18741 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18742 : "%<init%> clause lacks the %<targetsync%> modifier");
18743 : }
18744 :
18745 31862 : bitmap_obstack_release (NULL);
18746 31862 : return clauses;
18747 : }
18748 :
18749 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18750 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18751 :
18752 : tree
18753 15 : c_omp_finish_mapper_clauses (tree clauses)
18754 : {
18755 15 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18756 : }
18757 :
18758 : /* Return code to initialize DST with a copy constructor from SRC.
18759 : C doesn't have copy constructors nor assignment operators, only for
18760 : _Atomic vars we need to perform __atomic_load from src into a temporary
18761 : followed by __atomic_store of the temporary to dst. */
18762 :
18763 : tree
18764 19486 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18765 : {
18766 19486 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18767 19482 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18768 :
18769 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18770 4 : tree type = TREE_TYPE (dst);
18771 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18772 4 : tree tmp = create_tmp_var (nonatomic_type);
18773 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18774 4 : TREE_ADDRESSABLE (tmp) = 1;
18775 4 : suppress_warning (tmp);
18776 4 : tree src_addr = build_fold_addr_expr (src);
18777 4 : tree dst_addr = build_fold_addr_expr (dst);
18778 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18779 4 : vec<tree, va_gc> *params;
18780 : /* Expansion of a generic atomic load may require an addition
18781 : element, so allocate enough to prevent a resize. */
18782 4 : vec_alloc (params, 4);
18783 :
18784 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18785 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18786 4 : params->quick_push (src_addr);
18787 4 : params->quick_push (tmp_addr);
18788 4 : params->quick_push (seq_cst);
18789 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18790 :
18791 4 : vec_alloc (params, 4);
18792 :
18793 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18794 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18795 4 : params->quick_push (dst_addr);
18796 4 : params->quick_push (tmp_addr);
18797 4 : params->quick_push (seq_cst);
18798 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18799 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18800 : }
18801 :
18802 : /* Create a transaction node. */
18803 :
18804 : tree
18805 135 : c_finish_transaction (location_t loc, tree block, int flags)
18806 : {
18807 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18808 135 : if (flags & TM_STMT_ATTR_OUTER)
18809 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18810 135 : if (flags & TM_STMT_ATTR_RELAXED)
18811 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18812 135 : return add_stmt (stmt);
18813 : }
18814 :
18815 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18816 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18817 : NULL, then it should be used as the qualified type
18818 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18819 : preserve information about the typedef name from which an array
18820 : type was derived). */
18821 :
18822 : tree
18823 77162052 : c_build_qualified_type (tree type, qualifier_set type_quals,
18824 : tree orig_qual_type, size_t orig_qual_indirect)
18825 : {
18826 77162052 : if (type == error_mark_node)
18827 : return type;
18828 :
18829 77161900 : if (TREE_CODE (type) == ARRAY_TYPE)
18830 : {
18831 3181684 : tree t;
18832 3181684 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18833 : type_quals, orig_qual_type,
18834 : orig_qual_indirect - 1);
18835 :
18836 : /* See if we already have an identically qualified type. */
18837 3181684 : if (orig_qual_type && orig_qual_indirect == 0)
18838 : t = orig_qual_type;
18839 : else
18840 4641790 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18841 : {
18842 4517346 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18843 3057189 : && TYPE_NAME (t) == TYPE_NAME (type)
18844 3035941 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18845 3035941 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18846 3035941 : TYPE_ATTRIBUTES (type)))
18847 : break;
18848 : }
18849 3181643 : if (!t)
18850 : {
18851 145702 : tree domain = TYPE_DOMAIN (type);
18852 :
18853 145702 : t = build_variant_type_copy (type);
18854 145702 : TREE_TYPE (t) = element_type;
18855 145702 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18856 :
18857 145702 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18858 145702 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18859 200 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18860 145502 : else if (TYPE_CANONICAL (element_type) != element_type
18861 145502 : || (domain && TYPE_CANONICAL (domain) != domain))
18862 : {
18863 2848 : tree unqualified_canon
18864 5437 : = c_build_array_type (TYPE_CANONICAL (element_type),
18865 2589 : domain ? TYPE_CANONICAL (domain)
18866 : : NULL_TREE);
18867 2848 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18868 : {
18869 0 : unqualified_canon
18870 0 : = build_distinct_type_copy (unqualified_canon);
18871 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18872 : }
18873 2848 : TYPE_CANONICAL (t)
18874 5696 : = c_build_qualified_type (unqualified_canon, type_quals);
18875 : }
18876 : else
18877 142654 : TYPE_CANONICAL (t) = t;
18878 : }
18879 : return t;
18880 : }
18881 :
18882 : /* A restrict-qualified pointer type must be a pointer to object or
18883 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18884 : REFERENCE_TYPEs, which is appropriate for C++. */
18885 73980216 : if ((type_quals.has (TYPE_QUAL_RESTRICT))
18886 73980216 : && (!POINTER_TYPE_P (type)
18887 3507574 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18888 : {
18889 3 : error ("invalid use of %<restrict%>");
18890 3 : type_quals.remove (TYPE_QUAL_RESTRICT);
18891 : }
18892 :
18893 73980216 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18894 73980216 : ? orig_qual_type
18895 73980185 : : build_qualified_type (type, type_quals));
18896 :
18897 73980216 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18898 : == C_TYPE_VARIABLE_SIZE (type));
18899 73980216 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18900 : == c_type_variably_modified_p (type));
18901 :
18902 : /* A variant type does not inherit the list of incomplete vars from the
18903 : type main variant. */
18904 73980216 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18905 71878756 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18906 74087751 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18907 1419635 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18908 : return var_type;
18909 : }
18910 :
18911 : /* Build a VA_ARG_EXPR for the C parser. */
18912 :
18913 : tree
18914 20024 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18915 : tree type_expr)
18916 : {
18917 20024 : if (error_operand_p (type))
18918 2 : return error_mark_node;
18919 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18920 : order because it takes the address of the expression. */
18921 20022 : else if (handled_component_p (expr)
18922 31 : && reverse_storage_order_for_component_p (expr))
18923 : {
18924 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18925 0 : return error_mark_node;
18926 : }
18927 20022 : else if (!COMPLETE_TYPE_P (type))
18928 : {
18929 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18930 : "type %qT", type);
18931 11 : return error_mark_node;
18932 : }
18933 20011 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18934 : {
18935 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18936 : type);
18937 1 : return error_mark_node;
18938 : }
18939 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18940 20013 : && !flag_isoc99)
18941 : {
18942 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18943 : type);
18944 1 : return error_mark_node;
18945 : }
18946 20009 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18947 1 : warning_at (loc2, OPT_Wc___compat,
18948 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18949 :
18950 20009 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18951 : {
18952 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18953 : type);
18954 : /* We create a trap but evaluate side effects first. */
18955 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18956 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18957 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18958 : /* Return a compound literal of the right type. */
18959 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18960 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18961 3 : }
18962 : else
18963 20006 : expr = build_va_arg (loc2, expr, type);
18964 :
18965 20009 : if (type_expr)
18966 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18967 :
18968 : return expr;
18969 : }
18970 :
18971 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18972 : Return 1 if they are the same. Return false if they are different. */
18973 :
18974 : bool
18975 1854 : c_tree_equal (tree t1, tree t2)
18976 : {
18977 1883 : enum tree_code code1, code2;
18978 :
18979 1883 : if (t1 == t2)
18980 : return true;
18981 661 : if (!t1 || !t2)
18982 : return false;
18983 :
18984 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18985 20 : code1 = TREE_CODE (t1))
18986 20 : t1 = TREE_OPERAND (t1, 0);
18987 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18988 21 : code2 = TREE_CODE (t2))
18989 21 : t2 = TREE_OPERAND (t2, 0);
18990 :
18991 : /* They might have become equal now. */
18992 661 : if (t1 == t2)
18993 : return true;
18994 :
18995 640 : if (code1 != code2)
18996 : return false;
18997 :
18998 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18999 : return false;
19000 :
19001 379 : switch (code1)
19002 : {
19003 2 : case INTEGER_CST:
19004 2 : return wi::to_wide (t1) == wi::to_wide (t2);
19005 :
19006 10 : case REAL_CST:
19007 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
19008 :
19009 0 : case STRING_CST:
19010 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
19011 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
19012 0 : TREE_STRING_LENGTH (t1));
19013 :
19014 0 : case FIXED_CST:
19015 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
19016 : TREE_FIXED_CST (t2));
19017 :
19018 0 : case COMPLEX_CST:
19019 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
19020 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
19021 :
19022 0 : case VECTOR_CST:
19023 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
19024 :
19025 0 : case CONSTRUCTOR:
19026 : /* We need to do this when determining whether or not two
19027 : non-type pointer to member function template arguments
19028 : are the same. */
19029 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
19030 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
19031 : return false;
19032 : {
19033 : tree field, value;
19034 : unsigned int i;
19035 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
19036 : {
19037 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
19038 0 : if (!c_tree_equal (field, elt2->index)
19039 0 : || !c_tree_equal (value, elt2->value))
19040 : return false;
19041 : }
19042 : }
19043 : return true;
19044 :
19045 0 : case TREE_LIST:
19046 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
19047 : return false;
19048 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
19049 : return false;
19050 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
19051 :
19052 0 : case SAVE_EXPR:
19053 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
19054 :
19055 25 : case CALL_EXPR:
19056 25 : {
19057 25 : tree arg1, arg2;
19058 25 : call_expr_arg_iterator iter1, iter2;
19059 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
19060 : return false;
19061 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
19062 25 : arg2 = first_call_expr_arg (t2, &iter2);
19063 25 : arg1 && arg2;
19064 0 : arg1 = next_call_expr_arg (&iter1),
19065 0 : arg2 = next_call_expr_arg (&iter2))
19066 0 : if (!c_tree_equal (arg1, arg2))
19067 : return false;
19068 25 : if (arg1 || arg2)
19069 0 : return false;
19070 : return true;
19071 : }
19072 :
19073 0 : case TARGET_EXPR:
19074 0 : {
19075 0 : tree o1 = TREE_OPERAND (t1, 0);
19076 0 : tree o2 = TREE_OPERAND (t2, 0);
19077 :
19078 : /* Special case: if either target is an unallocated VAR_DECL,
19079 : it means that it's going to be unified with whatever the
19080 : TARGET_EXPR is really supposed to initialize, so treat it
19081 : as being equivalent to anything. */
19082 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
19083 0 : && !DECL_RTL_SET_P (o1))
19084 : /*Nop*/;
19085 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
19086 0 : && !DECL_RTL_SET_P (o2))
19087 : /*Nop*/;
19088 0 : else if (!c_tree_equal (o1, o2))
19089 : return false;
19090 :
19091 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
19092 : }
19093 :
19094 29 : case COMPONENT_REF:
19095 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
19096 : return false;
19097 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
19098 :
19099 : case PARM_DECL:
19100 : case VAR_DECL:
19101 : case CONST_DECL:
19102 : case FIELD_DECL:
19103 : case FUNCTION_DECL:
19104 : case IDENTIFIER_NODE:
19105 : case SSA_NAME:
19106 : return false;
19107 :
19108 0 : case TREE_VEC:
19109 0 : {
19110 0 : unsigned ix;
19111 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
19112 : return false;
19113 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
19114 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
19115 0 : TREE_VEC_ELT (t2, ix)))
19116 : return false;
19117 : return true;
19118 : }
19119 :
19120 22 : CASE_CONVERT:
19121 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
19122 : return false;
19123 : break;
19124 :
19125 : default:
19126 : break;
19127 : }
19128 :
19129 131 : switch (TREE_CODE_CLASS (code1))
19130 : {
19131 131 : case tcc_unary:
19132 131 : case tcc_binary:
19133 131 : case tcc_comparison:
19134 131 : case tcc_expression:
19135 131 : case tcc_vl_exp:
19136 131 : case tcc_reference:
19137 131 : case tcc_statement:
19138 131 : {
19139 131 : int i, n = TREE_OPERAND_LENGTH (t1);
19140 :
19141 131 : switch (code1)
19142 : {
19143 0 : case PREINCREMENT_EXPR:
19144 0 : case PREDECREMENT_EXPR:
19145 0 : case POSTINCREMENT_EXPR:
19146 0 : case POSTDECREMENT_EXPR:
19147 0 : n = 1;
19148 0 : break;
19149 16 : case ARRAY_REF:
19150 16 : n = 2;
19151 16 : break;
19152 : default:
19153 : break;
19154 : }
19155 :
19156 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
19157 131 : && n != TREE_OPERAND_LENGTH (t2))
19158 : return false;
19159 :
19160 305 : for (i = 0; i < n; ++i)
19161 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
19162 : return false;
19163 :
19164 : return true;
19165 : }
19166 :
19167 0 : case tcc_type:
19168 0 : return comptypes (t1, t2);
19169 0 : default:
19170 0 : gcc_unreachable ();
19171 : }
19172 : }
19173 :
19174 : /* Returns true when the function declaration FNDECL is implicit,
19175 : introduced as a result of a call to an otherwise undeclared
19176 : function, and false otherwise. */
19177 :
19178 : bool
19179 2013 : c_decl_implicit (const_tree fndecl)
19180 : {
19181 2013 : return C_DECL_IMPLICIT (fndecl);
19182 : }
|