Line data Source code
1 : /* Build expressions with type checking for C compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 :
21 : /* This file is part of the C front end.
22 : It contains routines to build C expressions given their operands,
23 : including computing the types of the result, C-specific error checks,
24 : and some optimization. */
25 :
26 : #include "config.h"
27 : #include "system.h"
28 : #include "coretypes.h"
29 : #include "memmodel.h"
30 : #include "target.h"
31 : #include "function.h"
32 : #include "bitmap.h"
33 : #include "c-tree.h"
34 : #include "gimple-expr.h"
35 : #include "predict.h"
36 : #include "stor-layout.h"
37 : #include "trans-mem.h"
38 : #include "varasm.h"
39 : #include "stmt.h"
40 : #include "langhooks.h"
41 : #include "c-lang.h"
42 : #include "intl.h"
43 : #include "tree-iterator.h"
44 : #include "gimplify.h"
45 : #include "tree-inline.h"
46 : #include "omp-general.h"
47 : #include "c-family/c-objc.h"
48 : #include "c-family/c-ubsan.h"
49 : #include "gomp-constants.h"
50 : #include "spellcheck-tree.h"
51 : #include "c-family/c-type-mismatch.h"
52 : #include "stringpool.h"
53 : #include "attribs.h"
54 : #include "asan.h"
55 : #include "realmpfr.h"
56 : #include "tree-pretty-print-markup.h"
57 : #include "gcc-urlifier.h"
58 :
59 : /* Possible cases of implicit conversions. Used to select diagnostic messages
60 : and control folding initializers in convert_for_assignment. */
61 : enum impl_conv {
62 : ic_argpass,
63 : ic_assign,
64 : ic_init,
65 : ic_init_const,
66 : ic_return
67 : };
68 :
69 : /* The level of nesting inside "__alignof__". */
70 : int in_alignof;
71 :
72 : /* The level of nesting inside "sizeof". */
73 : int in_sizeof;
74 :
75 : /* The level of nesting inside "countof". */
76 : int in_countof;
77 :
78 : /* The level of nesting inside "typeof". */
79 : int in_typeof;
80 :
81 : /* The level of nesting inside "_Generic". */
82 : int in_generic;
83 :
84 : /* True when parsing OpenMP loop expressions. */
85 : bool c_in_omp_for;
86 :
87 : /* True when parsing OpenMP map clause. */
88 : bool c_omp_array_section_p;
89 :
90 : /* The argument of last parsed sizeof expression, only to be tested
91 : if expr.original_code == SIZEOF_EXPR. */
92 : tree c_last_sizeof_arg;
93 : location_t c_last_sizeof_loc;
94 :
95 : /* Nonzero if we might need to print a "missing braces around
96 : initializer" message within this initializer. */
97 : static int found_missing_braces;
98 :
99 : static bool require_constant_value;
100 : static bool require_constant_elements;
101 : static bool require_constexpr_value;
102 :
103 : static tree qualify_type (tree, tree);
104 : struct comptypes_data;
105 : static bool tagged_types_tu_compatible_p (const_tree, const_tree,
106 : struct comptypes_data *);
107 : static bool comp_target_types (location_t, tree, tree);
108 : static bool function_types_compatible_p (const_tree, const_tree,
109 : struct comptypes_data *);
110 : static bool type_lists_compatible_p (const_tree, const_tree,
111 : struct comptypes_data *);
112 : static int convert_arguments (location_t, vec<location_t>, tree,
113 : vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 : tree);
115 : static tree pointer_diff (location_t, tree, tree, tree *);
116 : static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
117 : enum impl_conv, bool, tree, tree, int,
118 : int = 0);
119 : static tree valid_compound_expr_initializer (tree, tree);
120 : static void push_string (const char *);
121 : static void push_member_name (tree);
122 : static int spelling_length (void);
123 : static char *print_spelling (char *);
124 : static void warning_init (location_t, int, const char *);
125 : static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
126 : bool, bool, bool);
127 : static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
128 : bool, struct obstack *);
129 : static void output_pending_init_elements (int, struct obstack *);
130 : static bool set_designator (location_t, bool, struct obstack *);
131 : static void push_range_stack (tree, struct obstack *);
132 : static void add_pending_init (location_t, tree, tree, tree, bool,
133 : struct obstack *);
134 : static void set_nonincremental_init (struct obstack *);
135 : static void set_nonincremental_init_from_string (tree, struct obstack *);
136 : static tree find_init_member (tree, struct obstack *);
137 : static void readonly_warning (tree, enum lvalue_use);
138 : static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
139 : static void record_maybe_used_decl (tree, bool address);
140 : static bool comptypes_internal (const_tree, const_tree,
141 : struct comptypes_data *data);
142 : static bool comptypes_check_for_composite (tree t1, tree t2);
143 : static bool handle_counted_by_p (tree);
144 :
145 : /* Return true if EXP is a null pointer constant, false otherwise. */
146 :
147 : bool
148 188651035 : null_pointer_constant_p (const_tree expr)
149 : {
150 : /* This should really operate on c_expr structures, but they aren't
151 : yet available everywhere required. */
152 188651035 : tree type = TREE_TYPE (expr);
153 :
154 : /* An integer constant expression with the value 0, such an expression
155 : cast to type void*, or the predefined constant nullptr, are a null
156 : pointer constant. */
157 188651035 : if (expr == nullptr_node)
158 : return true;
159 :
160 188650352 : return (TREE_CODE (expr) == INTEGER_CST
161 19103897 : && !TREE_OVERFLOW (expr)
162 19103800 : && integer_zerop (expr)
163 192583417 : && (INTEGRAL_TYPE_P (type)
164 333864 : || (TREE_CODE (type) == POINTER_TYPE
165 333862 : && VOID_TYPE_P (TREE_TYPE (type))
166 258579 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
167 : }
168 :
169 : /* EXPR may appear in an unevaluated part of an integer constant
170 : expression, but not in an evaluated part. Wrap it in a
171 : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
172 : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
173 :
174 : static tree
175 62357 : note_integer_operands (tree expr)
176 : {
177 62357 : tree ret;
178 62357 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
179 : {
180 22 : ret = copy_node (expr);
181 22 : TREE_OVERFLOW (ret) = 1;
182 : }
183 : else
184 : {
185 62335 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
186 62335 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
187 : }
188 62357 : return ret;
189 : }
190 :
191 : /* Having checked whether EXPR may appear in an unevaluated part of an
192 : integer constant expression and found that it may, remove any
193 : C_MAYBE_CONST_EXPR noting this fact and return the resulting
194 : expression. */
195 :
196 : static inline tree
197 33749217 : remove_c_maybe_const_expr (tree expr)
198 : {
199 33749217 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
200 61507 : return C_MAYBE_CONST_EXPR_EXPR (expr);
201 : else
202 : return expr;
203 : }
204 :
205 : /* This is a cache to hold if two types are seen. */
206 :
207 : struct tagged_tu_seen_cache {
208 : const struct tagged_tu_seen_cache * next;
209 : const_tree t1;
210 : const_tree t2;
211 : };
212 :
213 : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
214 : does not have an incomplete type. (That includes void types.)
215 : LOC is the location of the use. */
216 :
217 : tree
218 773608105 : require_complete_type (location_t loc, tree value)
219 : {
220 773608105 : tree type = TREE_TYPE (value);
221 :
222 773608105 : if (error_operand_p (value))
223 8925 : return error_mark_node;
224 :
225 : /* First, detect a valid value with a complete type. */
226 773599180 : if (COMPLETE_TYPE_P (type))
227 : return value;
228 :
229 164 : c_incomplete_type_error (loc, value, type);
230 164 : return error_mark_node;
231 : }
232 :
233 : /* Print an error message for invalid use of an incomplete type.
234 : VALUE is the expression that was used (or 0 if that isn't known)
235 : and TYPE is the type that was invalid. LOC is the location for
236 : the error. */
237 :
238 : void
239 224 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
240 : {
241 : /* Avoid duplicate error message. */
242 224 : if (TREE_CODE (type) == ERROR_MARK)
243 : return;
244 :
245 224 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
246 98 : error_at (loc, "%qD has an incomplete type %qT", value, type);
247 : else
248 : {
249 126 : retry:
250 : /* We must print an error message. Be clever about what it says. */
251 :
252 126 : switch (TREE_CODE (type))
253 : {
254 82 : case RECORD_TYPE:
255 82 : case UNION_TYPE:
256 82 : case ENUMERAL_TYPE:
257 82 : break;
258 :
259 38 : case VOID_TYPE:
260 38 : error_at (loc, "invalid use of void expression");
261 38 : return;
262 :
263 6 : case ARRAY_TYPE:
264 6 : if (TYPE_DOMAIN (type))
265 : {
266 3 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
267 : {
268 3 : error_at (loc, "invalid use of flexible array member");
269 3 : return;
270 : }
271 0 : type = TREE_TYPE (type);
272 0 : goto retry;
273 : }
274 3 : error_at (loc, "invalid use of array with unspecified bounds");
275 3 : return;
276 :
277 0 : default:
278 0 : gcc_unreachable ();
279 : }
280 :
281 82 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
282 80 : error_at (loc, "invalid use of undefined type %qT", type);
283 : else
284 : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
285 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
286 : }
287 : }
288 :
289 : /* Given a type, apply default promotions wrt unnamed function
290 : arguments and return the new type. */
291 :
292 : tree
293 124547226 : c_type_promotes_to (tree type)
294 : {
295 124547226 : tree ret = NULL_TREE;
296 :
297 124547226 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
298 1504317 : ret = double_type_node;
299 123042909 : else if (c_promoting_integer_type_p (type))
300 : {
301 : /* Preserve unsignedness if not really getting any wider. */
302 18662172 : if (TYPE_UNSIGNED (type)
303 18662172 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
304 0 : ret = unsigned_type_node;
305 : else
306 18662172 : ret = integer_type_node;
307 : }
308 :
309 20166489 : if (ret != NULL_TREE)
310 20166489 : return (TYPE_ATOMIC (type)
311 20166489 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
312 : : ret);
313 :
314 : return type;
315 : }
316 :
317 : /* Return true if between two named address spaces, whether there is a superset
318 : named address space that encompasses both address spaces. If there is a
319 : superset, return which address space is the superset. */
320 :
321 : static bool
322 7357282 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
323 : {
324 7357282 : if (as1 == as2)
325 : {
326 7357282 : *common = as1;
327 7357282 : return true;
328 : }
329 0 : else if (targetm.addr_space.subset_p (as1, as2))
330 : {
331 0 : *common = as2;
332 0 : return true;
333 : }
334 0 : else if (targetm.addr_space.subset_p (as2, as1))
335 : {
336 0 : *common = as1;
337 0 : return true;
338 : }
339 : else
340 : return false;
341 : }
342 :
343 : /* Return a variant of TYPE which has all the type qualifiers of LIKE
344 : as well as those of TYPE. */
345 :
346 : static tree
347 2598244 : qualify_type (tree type, tree like)
348 : {
349 2598244 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
350 2598244 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
351 2598244 : addr_space_t as_common;
352 :
353 : /* If the two named address spaces are different, determine the common
354 : superset address space. If there isn't one, raise an error. */
355 2598244 : if (!addr_space_superset (as_type, as_like, &as_common))
356 : {
357 0 : as_common = as_type;
358 0 : error ("%qT and %qT are in disjoint named address spaces",
359 : type, like);
360 : }
361 :
362 5196488 : return c_build_qualified_type (type,
363 2598244 : TYPE_QUALS_NO_ADDR_SPACE (type)
364 2598244 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
365 2598244 : | ENCODE_QUAL_ADDR_SPACE (as_common));
366 : }
367 :
368 :
369 : /* Check consistency of type TYPE. For derived types, we test that
370 : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
371 : the requirements of the base type. We also check that arrays with a
372 : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
373 : inconsistency is detected false is returned and true otherwise. */
374 :
375 : static bool
376 140447578 : c_verify_type (tree type)
377 : {
378 140447578 : switch (TREE_CODE (type))
379 : {
380 67154309 : case POINTER_TYPE:
381 67154309 : case FUNCTION_TYPE:
382 67154309 : case ARRAY_TYPE:
383 : /* Pointer, array, functions are variably modified if and only if the
384 : target, element, return type is variably modified. */
385 67154309 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
386 67154309 : && (C_TYPE_VARIABLY_MODIFIED (type)
387 66971795 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
388 : return false;
389 : /* If the target type is structural equality, the type should also. */
390 67154309 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
391 67154309 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
392 : return false;
393 :
394 140447578 : default:
395 140447578 : break;
396 : }
397 :
398 140447578 : switch (TREE_CODE (type))
399 : {
400 66242840 : case POINTER_TYPE:
401 66242840 : case FUNCTION_TYPE:
402 : /* Pointer and functions can not have variable size. */
403 66242840 : if (C_TYPE_VARIABLE_SIZE (type))
404 0 : return false;
405 : break;
406 911469 : case ARRAY_TYPE:
407 : /* An array has variable size if and only if it has a non-constant
408 : dimensions or its element type has variable size. */
409 911469 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
410 911469 : || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
411 885778 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
412 : != INTEGER_CST))
413 911469 : != C_TYPE_VARIABLE_SIZE (type))
414 : return false;
415 : /* If the element type or the array has variable size, then the
416 : array has variable size and is variably modified. */
417 911469 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
418 911469 : || C_TYPE_VARIABLE_SIZE (type))
419 : {
420 28698 : if (!C_TYPE_VARIABLE_SIZE (type))
421 : return false;
422 28698 : if (!C_TYPE_VARIABLY_MODIFIED (type))
423 0 : return false;
424 : }
425 : default:
426 : break;
427 : }
428 : return true;
429 : }
430 :
431 : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
432 : from a base type to a newly built derived or qualified type. */
433 :
434 : static tree
435 133461940 : c_set_type_bits (tree new_type, tree old_type)
436 : {
437 133461940 : gcc_checking_assert (c_verify_type (old_type));
438 :
439 133461940 : if (c_type_variably_modified_p (old_type))
440 28316 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
441 :
442 133461940 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
443 : {
444 16804 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
445 16804 : C_TYPE_VARIABLE_SIZE (new_type) = true;
446 : }
447 133461940 : return new_type;
448 : }
449 :
450 : /* Build a pointer type using the default pointer mode. */
451 :
452 : tree
453 71530989 : c_build_pointer_type (tree to_type)
454 : {
455 71530989 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
456 71530989 : : TYPE_ADDR_SPACE (to_type);
457 71530989 : machine_mode pointer_mode;
458 :
459 71530989 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
460 71530989 : pointer_mode = targetm.addr_space.pointer_mode (as);
461 : else
462 : pointer_mode = c_default_pointer_mode;
463 :
464 71530989 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
465 : }
466 :
467 : /* Build a pointer type using the given mode. */
468 :
469 : tree
470 71779227 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
471 : {
472 71779227 : tree ret = build_pointer_type_for_mode (type, mode, m);
473 71779227 : return c_set_type_bits (ret, type);
474 : }
475 :
476 : /* Build a function type. */
477 :
478 : tree
479 53298361 : c_build_function_type (tree type, tree args, bool no)
480 : {
481 53298361 : tree ret = build_function_type (type, args, no);
482 53298361 : return c_set_type_bits (ret, type);
483 : }
484 :
485 : /* Build an array type. This sets typeless storage as required
486 : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
487 : based on the element type and domain. */
488 :
489 : tree
490 1171759 : c_build_array_type (tree type, tree domain)
491 : {
492 1171759 : int type_quals = TYPE_QUALS (type);
493 :
494 : /* Identify typeless storage as introduced in C2Y
495 : and supported also in earlier language modes. */
496 1748898 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
497 1171759 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
498 :
499 1171759 : tree ret = build_array_type (type, domain, typeless);
500 :
501 1086035 : if (domain && TYPE_MAX_VALUE (domain)
502 2168502 : && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
503 : {
504 24259 : C_TYPE_VARIABLE_SIZE (ret) = 1;
505 24259 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
506 : }
507 :
508 1171759 : return c_set_type_bits (ret, type);
509 : }
510 :
511 :
512 : /* Build an array type of unspecified size. */
513 : tree
514 150 : c_build_array_type_unspecified (tree type)
515 : {
516 150 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
517 : integer_zero_node, size_zero_node);
518 150 : return c_build_array_type (type, build_index_type (upper));
519 : }
520 :
521 :
522 : tree
523 7212593 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
524 : {
525 7212593 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
526 7212593 : return c_set_type_bits (ret, type);
527 : }
528 :
529 : tree
530 7023489 : c_build_type_attribute_variant (tree type, tree attrs)
531 : {
532 7023489 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
533 : }
534 :
535 : /* Reconstruct a complex derived type. This is used to re-construct types
536 : with the vector attribute. It is called via a langhook. */
537 :
538 : tree
539 840031 : c_reconstruct_complex_type (tree type, tree bottom)
540 : {
541 840031 : tree inner, outer;
542 :
543 840031 : if (TREE_CODE (type) == POINTER_TYPE)
544 : {
545 174872 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
546 174872 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
547 174872 : TYPE_REF_CAN_ALIAS_ALL (type));
548 : }
549 : else if (TREE_CODE (type) == ARRAY_TYPE)
550 : {
551 7281 : bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
552 7281 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
553 7281 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
554 7281 : if (rso)
555 : {
556 1 : outer = build_distinct_type_copy (outer);
557 1 : TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
558 : }
559 :
560 7281 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
561 : == C_TYPE_VARIABLE_SIZE (outer));
562 : }
563 : else if (TREE_CODE (type) == FUNCTION_TYPE)
564 : {
565 585 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
566 585 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
567 585 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
568 : }
569 : else if (TREE_CODE (type) == REFERENCE_TYPE
570 : || TREE_CODE (type) == OFFSET_TYPE)
571 0 : gcc_unreachable ();
572 : else
573 : return bottom;
574 :
575 182738 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
576 365476 : TYPE_QUALS (type));
577 : }
578 :
579 :
580 : /* Helper function for c_canonical_type. Check whether FIELD
581 : contains a pointer to a structure or union with tag,
582 : possibly nested in other type derivations. */
583 : static bool
584 3441883 : ptr_to_tagged_member (tree field)
585 : {
586 3441883 : gcc_assert (FIELD_DECL == TREE_CODE (field));
587 3441883 : tree type = TREE_TYPE (field);
588 3441883 : bool ptr_seen = false;
589 3441883 : while (TREE_CODE (type) == ARRAY_TYPE
590 : || TREE_CODE (type) == FUNCTION_TYPE
591 4728922 : || TREE_CODE (type) == POINTER_TYPE)
592 : {
593 1287039 : if (TREE_CODE (type) == POINTER_TYPE)
594 698865 : ptr_seen = true;
595 1287039 : type = TREE_TYPE (type);
596 : }
597 :
598 3441883 : if (ptr_seen
599 677817 : && RECORD_OR_UNION_TYPE_P (type)
600 3690688 : && NULL_TREE != c_type_tag (type))
601 : return true;
602 :
603 : return false;
604 : }
605 :
606 : /* For a record or union type, make necessary adaptations so that the
607 : type can be used as TYPE_CANONICAL.
608 :
609 : If the TYPE contains a pointer (possibly nested in other type
610 : derivations) to a structure or union as a member, create a copy
611 : and change such pointers to void pointers. Otherwise, the middle-end
612 : gets confused when recording component aliases in the case where we
613 : have formed equivalency classes that include types for which these
614 : member pointers end up pointing to other structure or unions types
615 : which have the same tag but are not compatible. */
616 : tree
617 884315 : c_type_canonical (tree type)
618 : {
619 884315 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
620 :
621 884315 : bool needs_mod = false;
622 3633666 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
623 : {
624 2831014 : if (!ptr_to_tagged_member (x))
625 2749351 : continue;
626 : needs_mod = true;
627 : break;
628 : }
629 884315 : if (!needs_mod)
630 : return type;
631 :
632 : /* Construct new version with such members replaced. */
633 81663 : tree n = build_distinct_type_copy (type);
634 81663 : tree *fields = &TYPE_FIELDS (n);
635 692532 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
636 : {
637 610869 : tree f = copy_node (x);
638 610869 : if (ptr_to_tagged_member (x))
639 161943 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
640 : ptr_type_node);
641 610869 : *fields = f;
642 610869 : fields = &DECL_CHAIN (f);
643 : }
644 81663 : TYPE_CANONICAL (n) = n;
645 81663 : return n;
646 : }
647 :
648 :
649 : /* If NTYPE is a type of a non-variadic function with a prototype
650 : and OTYPE is a type of a function without a prototype and ATTRS
651 : contains attribute format, diagnosess and removes it from ATTRS.
652 : Returns the result of build_type_attribute_variant of NTYPE and
653 : the (possibly) modified ATTRS. */
654 :
655 : static tree
656 9833 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
657 : {
658 9833 : if (!prototype_p (otype)
659 9821 : && prototype_p (ntype)
660 19624 : && lookup_attribute ("format", attrs))
661 : {
662 14 : warning_at (input_location, OPT_Wattributes,
663 : "%qs attribute can only be applied to variadic functions",
664 : "format");
665 14 : attrs = remove_attribute ("format", attrs);
666 : }
667 9833 : return c_build_type_attribute_variant (ntype, attrs);
668 :
669 : }
670 :
671 : /* Given a type which could be a typedef name, make sure to return the
672 : original type. See set_underlying_type. */
673 : static const_tree
674 62379677 : c_type_original (const_tree t)
675 : {
676 : /* It may even be a typedef of a typedef...
677 : In the case of compiler-created builtin structs the TYPE_DECL
678 : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
679 62379677 : while (TYPE_NAME (t)
680 32165983 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
681 62427961 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
682 24140 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
683 62379677 : return t;
684 : }
685 :
686 : /* Return the tag for a tagged type. */
687 : tree
688 62379677 : c_type_tag (const_tree t)
689 : {
690 62379677 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
691 62379677 : const_tree orig = c_type_original (t);
692 62379677 : tree name = TYPE_NAME (orig);
693 62379677 : if (!name)
694 : return NULL_TREE;
695 32141843 : if (TREE_CODE (name) == TYPE_DECL)
696 : {
697 4 : if (!DECL_NAME (name))
698 : return NULL_TREE;
699 2 : name = DECL_NAME (name);
700 : }
701 32141841 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
702 : return name;
703 : }
704 :
705 : /* Remove qualifiers but not atomic. For arrays remove qualifiers
706 : on the element type but also do not remove atomic. */
707 : static tree
708 62330693 : remove_qualifiers (tree t)
709 : {
710 62330693 : if (!t || t == error_mark_node)
711 : return t;
712 62330668 : return TYPE_ATOMIC (strip_array_types (t))
713 62330668 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
714 62315093 : : TYPE_MAIN_VARIANT (t);
715 : }
716 :
717 :
718 : /* Helper function for composite_type_internal. Find a compatible type
719 : in a (transparent) union U compatible to T. If found, return the
720 : type of the corresponding member. Otherwise, return the union type U. */
721 : static tree
722 8208416 : transparent_union_replacement (tree u, tree t)
723 : {
724 8208411 : if (u == error_mark_node || t == error_mark_node
725 8208406 : || TREE_CODE (u) != UNION_TYPE
726 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
727 8208480 : || comptypes (u, t))
728 8208352 : return u;
729 :
730 80 : for (tree memb = TYPE_FIELDS (u); memb; memb = DECL_CHAIN (memb))
731 : {
732 80 : tree m = remove_qualifiers (TREE_TYPE (memb));
733 80 : if (comptypes (m, t))
734 : {
735 64 : pedwarn (input_location, OPT_Wpedantic,
736 : "function types not truly compatible in ISO C");
737 64 : return m;
738 : }
739 : }
740 :
741 : return u;
742 : }
743 :
744 :
745 :
746 : /* Return the composite type of two compatible types.
747 :
748 : We assume that comptypes has already been done and returned
749 : nonzero; if that isn't so, this may crash. In particular, we
750 : assume that qualifiers match. */
751 :
752 : struct composite_cache {
753 : tree t1;
754 : tree t2;
755 : tree composite;
756 : struct composite_cache* next;
757 : };
758 :
759 : tree
760 13014726 : composite_type_internal (tree t1, tree t2, tree cond,
761 : struct composite_cache* cache)
762 : {
763 13014726 : enum tree_code code1;
764 13014726 : enum tree_code code2;
765 13014726 : tree attributes;
766 :
767 : /* Save time if the two types are the same. */
768 :
769 13014726 : if (t1 == t2) return t1;
770 :
771 : /* If one type is nonsense, use the other. */
772 2631992 : if (t1 == error_mark_node)
773 : return t2;
774 2631988 : if (t2 == error_mark_node)
775 : return t1;
776 :
777 2631986 : code1 = TREE_CODE (t1);
778 2631986 : code2 = TREE_CODE (t2);
779 :
780 : /* Merge the attributes. */
781 2631986 : attributes = targetm.merge_type_attributes (t1, t2);
782 :
783 : /* If one is an enumerated type and the other is the compatible
784 : integer type, the composite type might be either of the two
785 : (DR#013 question 3). For consistency, use the enumerated type as
786 : the composite type. */
787 :
788 2631986 : if (code1 == ENUMERAL_TYPE
789 196 : && (code2 == INTEGER_TYPE
790 196 : || code2 == BOOLEAN_TYPE))
791 : return t1;
792 2631790 : if (code2 == ENUMERAL_TYPE
793 69 : && (code1 == INTEGER_TYPE
794 69 : || code1 == BOOLEAN_TYPE))
795 : return t2;
796 :
797 2631721 : gcc_assert (code1 == code2);
798 :
799 2631721 : switch (code1)
800 : {
801 6366 : case POINTER_TYPE:
802 : /* For two pointers, do this recursively on the target type. */
803 6366 : {
804 6366 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
805 6366 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
806 : cond, cache);
807 6366 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
808 12732 : return c_build_type_attribute_qual_variant (n, attributes,
809 6366 : TYPE_QUALS (t2));
810 : }
811 :
812 10994 : case ARRAY_TYPE:
813 10994 : {
814 10994 : tree d1 = TYPE_DOMAIN (t1);
815 10994 : tree d2 = TYPE_DOMAIN (t2);
816 :
817 : /* We should not have any type quals on arrays at all. */
818 10994 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
819 : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
820 :
821 10994 : bool t1_complete = COMPLETE_TYPE_P (t1);
822 10994 : bool t2_complete = COMPLETE_TYPE_P (t2);
823 :
824 10994 : bool d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
825 10994 : bool d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
826 :
827 10994 : bool d1_variable, d2_variable;
828 :
829 21988 : d1_variable = (!d1_zero
830 10994 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
831 10399 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
832 21988 : d2_variable = (!d2_zero
833 10994 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
834 10652 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
835 :
836 10994 : bool use1 = d1 && (d2_variable || d2_zero || !d1_variable);
837 10994 : bool use2 = d2 && (d1_variable || d1_zero || !d2_variable);
838 :
839 : /* If the first is an unspecified size pick the other one. */
840 9012 : if (d2_variable && c_type_unspecified_p (t1))
841 : {
842 28 : gcc_assert (use1 && use2);
843 : use1 = false;
844 : }
845 :
846 : /* If both are VLAs but not unspecified and we are in the
847 : conditional operator, we create a conditional to select
848 : the size of the active branch. */
849 232 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
850 11223 : && d2_variable && !c_type_unspecified_p (t2);
851 :
852 10994 : tree td;
853 10994 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
854 : cond, cache);
855 :
856 10994 : if (!use0)
857 : {
858 : /* Save space: see if the result is identical to one of the args. */
859 10877 : if (elt == TREE_TYPE (t1) && use1)
860 10087 : return c_build_type_attribute_variant (t1, attributes);
861 790 : if (elt == TREE_TYPE (t2) && use2)
862 672 : return c_build_type_attribute_variant (t2, attributes);
863 :
864 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
865 10 : return c_build_type_attribute_variant (t1, attributes);
866 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
867 2 : return c_build_type_attribute_variant (t2, attributes);
868 :
869 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
870 : }
871 : else
872 : {
873 : /* Not used in C. */
874 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
875 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
876 :
877 117 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
878 117 : cond, TYPE_MAX_VALUE (d1),
879 117 : TYPE_MAX_VALUE (d2));
880 :
881 117 : td = build_index_type (d);
882 : }
883 :
884 : /* Merge the element types, and have a size if either arg has
885 : one. We may have qualifiers on the element types. To set
886 : up TYPE_MAIN_VARIANT correctly, we need to form the
887 : composite of the unqualified types and add the qualifiers
888 : back at the end. */
889 223 : int quals = TYPE_QUALS (strip_array_types (elt));
890 223 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
891 :
892 223 : t1 = c_build_array_type (unqual_elt, td);
893 :
894 : /* Check that a type which has a varying outermost dimension
895 : got marked has having a variable size. */
896 223 : bool varsize = (d1_variable && d2_variable)
897 97 : || (d1_variable && !t2_complete)
898 309 : || (d2_variable && !t1_complete);
899 157 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
900 :
901 : /* Ensure a composite type involving a zero-length array type
902 : is a zero-length type not an incomplete type. */
903 223 : if (d1_zero && d2_zero
904 16 : && (t1_complete || t2_complete)
905 224 : && !COMPLETE_TYPE_P (t1))
906 : {
907 1 : TYPE_SIZE (t1) = bitsize_zero_node;
908 1 : TYPE_SIZE_UNIT (t1) = size_zero_node;
909 : }
910 223 : t1 = c_build_qualified_type (t1, quals);
911 223 : return c_build_type_attribute_variant (t1, attributes);
912 : }
913 :
914 253 : case RECORD_TYPE:
915 253 : case UNION_TYPE:
916 253 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
917 : {
918 : /* Go to the original type to get the right tag. */
919 150 : tree tag = c_type_tag (t1);
920 :
921 150 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
922 150 : gcc_checking_assert (!tag || comptypes (t1, t2));
923 :
924 : /* If a composite type for these two types is already under
925 : construction, return it. */
926 :
927 376 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
928 246 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
929 20 : return c->composite;
930 :
931 : /* Otherwise, create a new type node and link it into the cache. */
932 :
933 130 : tree n = make_node (code1);
934 130 : SET_TYPE_STRUCTURAL_EQUALITY (n);
935 130 : TYPE_NAME (n) = tag;
936 :
937 130 : struct composite_cache cache2 = { t1, t2, n, cache };
938 130 : cache = &cache2;
939 :
940 130 : tree f1 = TYPE_FIELDS (t1);
941 130 : tree f2 = TYPE_FIELDS (t2);
942 130 : tree fields = NULL_TREE;
943 :
944 321 : for (tree a = f1, b = f2; a && b;
945 191 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
946 : {
947 191 : tree ta = TREE_TYPE (a);
948 191 : tree tb = TREE_TYPE (b);
949 :
950 191 : if (DECL_C_BIT_FIELD (a))
951 : {
952 19 : ta = DECL_BIT_FIELD_TYPE (a);
953 19 : tb = DECL_BIT_FIELD_TYPE (b);
954 : }
955 :
956 191 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
957 191 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
958 :
959 191 : tree t = composite_type_internal (ta, tb, cond, cache);
960 191 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
961 :
962 191 : DECL_PACKED (f) = DECL_PACKED (a);
963 191 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
964 191 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
965 191 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
966 :
967 191 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
968 :
969 191 : finish_decl (f, input_location, NULL, NULL, NULL);
970 :
971 191 : if (DECL_C_BIT_FIELD (a))
972 : {
973 : /* This will be processed by finish_struct. */
974 19 : SET_DECL_C_BIT_FIELD (f);
975 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
976 19 : tree_to_uhwi (DECL_SIZE (a)));
977 19 : DECL_NONADDRESSABLE_P (f) = true;
978 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
979 : }
980 :
981 191 : DECL_CHAIN (f) = fields;
982 191 : fields = f;
983 : }
984 :
985 130 : fields = nreverse (fields);
986 :
987 : /* Setup the struct/union type. Because we inherit all variably
988 : modified components, we can ignore the size expression. */
989 130 : tree expr = NULL_TREE;
990 :
991 : /* Set TYPE_STUB_DECL for debugging symbols. */
992 130 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
993 : NULL_TREE, n));
994 :
995 130 : n = finish_struct (input_location, n, fields, attributes, NULL,
996 : &expr);
997 :
998 130 : return qualify_type (n, t1);
999 : }
1000 : /* FALLTHRU */
1001 103 : case ENUMERAL_TYPE:
1002 103 : if (attributes != NULL)
1003 : {
1004 : /* Try harder not to create a new aggregate type. */
1005 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1006 : return t1;
1007 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1008 : return t2;
1009 : }
1010 99 : return c_build_type_attribute_variant (t1, attributes);
1011 :
1012 2573192 : case FUNCTION_TYPE:
1013 : /* Function types: prefer the one that specified arg types.
1014 : If both do, merge the arg types. Also merge the return types. */
1015 2573192 : {
1016 2573192 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1017 : cond, cache);
1018 2573192 : tree p1 = TYPE_ARG_TYPES (t1);
1019 2573192 : tree p2 = TYPE_ARG_TYPES (t2);
1020 :
1021 : /* Save space: see if the result is identical to one of the args. */
1022 2573192 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1023 9843 : return c_build_functype_attribute_variant (t1, t2, attributes);
1024 2563876 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
1025 517 : return c_build_functype_attribute_variant (t2, t1, attributes);
1026 :
1027 : /* Simple way if one arg fails to specify argument types. */
1028 2563359 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
1029 : {
1030 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
1031 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
1032 9 : t1 = c_build_type_attribute_variant (t1, attributes);
1033 9 : return qualify_type (t1, t2);
1034 : }
1035 2563350 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
1036 : {
1037 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
1038 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
1039 1 : t1 = c_build_type_attribute_variant (t1, attributes);
1040 1 : return qualify_type (t1, t2);
1041 : }
1042 :
1043 : /* If both args specify argument types, we must merge the two
1044 : lists, argument by argument. */
1045 2563349 : tree newargs = NULL_TREE;
1046 2563349 : tree *endp = &newargs;
1047 :
1048 6667557 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1049 : {
1050 6500195 : if (p1 == void_list_node)
1051 : {
1052 2395987 : *endp = void_list_node;
1053 2395987 : break;
1054 : }
1055 4104208 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1056 4104208 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1057 :
1058 4104208 : gcc_assert (mv1);
1059 4104208 : gcc_assert (mv2);
1060 :
1061 4104208 : mv1 = transparent_union_replacement (mv1, mv2);
1062 4104208 : mv2 = transparent_union_replacement (mv2, mv1);
1063 :
1064 4104208 : *endp = tree_cons (NULL_TREE,
1065 : composite_type_internal (mv1, mv2, cond, cache),
1066 : NULL_TREE);
1067 :
1068 4104208 : endp = &TREE_CHAIN (*endp);
1069 : }
1070 :
1071 2563349 : t1 = c_build_function_type (valtype, newargs);
1072 2563349 : t1 = qualify_type (t1, t2);
1073 : }
1074 : /* FALLTHRU */
1075 :
1076 2604265 : default:
1077 2604265 : return c_build_type_attribute_variant (t1, attributes);
1078 : }
1079 : }
1080 :
1081 : tree
1082 6319775 : composite_type_cond (tree t1, tree t2, tree cond)
1083 : {
1084 6319775 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1085 :
1086 6319775 : struct composite_cache cache = { };
1087 6319775 : tree n = composite_type_internal (t1, t2, cond, &cache);
1088 :
1089 6319775 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1090 6319775 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1091 6319775 : return n;
1092 : }
1093 :
1094 :
1095 : tree
1096 6315305 : composite_type (tree t1, tree t2)
1097 : {
1098 6315305 : return composite_type_cond (t1, t2, NULL_TREE);
1099 : }
1100 :
1101 : /* Return the type of a conditional expression between pointers to
1102 : possibly differently qualified versions of compatible types.
1103 :
1104 : We assume that comp_target_types has already been done and returned
1105 : true; if that isn't so, this may crash. */
1106 :
1107 : static tree
1108 114847 : common_pointer_type (tree t1, tree t2, tree cond)
1109 : {
1110 114847 : tree attributes;
1111 114847 : unsigned target_quals;
1112 114847 : addr_space_t as1, as2, as_common;
1113 114847 : int quals1, quals2;
1114 :
1115 : /* Save time if the two types are the same. */
1116 :
1117 114847 : if (t1 == t2) return t1;
1118 :
1119 : /* If one type is nonsense, use the other. */
1120 4470 : if (t1 == error_mark_node)
1121 : return t2;
1122 4470 : if (t2 == error_mark_node)
1123 : return t1;
1124 :
1125 4470 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1126 : && TREE_CODE (t2) == POINTER_TYPE);
1127 :
1128 : /* Merge the attributes. */
1129 4470 : attributes = targetm.merge_type_attributes (t1, t2);
1130 :
1131 : /* Find the composite type of the target types, and combine the
1132 : qualifiers of the two types' targets. */
1133 4470 : tree pointed_to_1 = TREE_TYPE (t1);
1134 4470 : tree pointed_to_2 = TREE_TYPE (t2);
1135 4470 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1136 4470 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1137 :
1138 : /* Strip array types to get correct qualifier for pointers to arrays */
1139 4470 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1140 4470 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1141 :
1142 : /* For function types do not merge const qualifiers, but drop them
1143 : if used inconsistently. The middle-end uses these to mark const
1144 : and noreturn functions. */
1145 4470 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1146 2129 : target_quals = (quals1 & quals2);
1147 : else
1148 2341 : target_quals = (quals1 | quals2);
1149 :
1150 : /* If the two named address spaces are different, determine the common
1151 : superset address space. This is guaranteed to exist due to the
1152 : assumption that comp_target_type returned true. */
1153 4470 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1154 4470 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1155 4470 : if (!addr_space_superset (as1, as2, &as_common))
1156 0 : gcc_unreachable ();
1157 :
1158 4470 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1159 :
1160 4470 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1161 4470 : return c_build_type_attribute_variant (t1, attributes);
1162 : }
1163 :
1164 : /* Return the common type for two arithmetic types under the usual
1165 : arithmetic conversions. The default conversions have already been
1166 : applied, and enumerated types converted to their compatible integer
1167 : types. The resulting type is unqualified and has no attributes.
1168 :
1169 : This is the type for the result of most arithmetic operations
1170 : if the operands have the given two types. */
1171 :
1172 : static tree
1173 13948613 : c_common_type (tree t1, tree t2)
1174 : {
1175 13948613 : enum tree_code code1;
1176 13948613 : enum tree_code code2;
1177 :
1178 : /* If one type is nonsense, use the other. */
1179 13948613 : if (t1 == error_mark_node)
1180 : return t2;
1181 13948613 : if (t2 == error_mark_node)
1182 : return t1;
1183 :
1184 13948613 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1185 37974 : t1 = TYPE_MAIN_VARIANT (t1);
1186 :
1187 13948613 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1188 49381 : t2 = TYPE_MAIN_VARIANT (t2);
1189 :
1190 13948613 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1191 : {
1192 188384 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1193 188384 : t1 = c_build_type_attribute_variant (t1, attrs);
1194 : }
1195 :
1196 13948613 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1197 : {
1198 187862 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1199 187862 : t2 = c_build_type_attribute_variant (t2, attrs);
1200 : }
1201 :
1202 : /* Save time if the two types are the same. */
1203 :
1204 13948613 : if (t1 == t2) return t1;
1205 :
1206 2322519 : code1 = TREE_CODE (t1);
1207 2322519 : code2 = TREE_CODE (t2);
1208 :
1209 2322519 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1210 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1211 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1212 2322519 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1213 : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1214 : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1215 :
1216 : /* When one operand is a decimal float type, the other operand cannot be
1217 : a generic float type or a complex type. We also disallow vector types
1218 : here. */
1219 2322519 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1220 2324942 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1221 : {
1222 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1223 : {
1224 3 : error ("cannot mix operands of decimal floating and vector types");
1225 3 : return error_mark_node;
1226 : }
1227 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1228 : {
1229 5 : error ("cannot mix operands of decimal floating and complex types");
1230 5 : return error_mark_node;
1231 : }
1232 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1233 : {
1234 16 : error ("cannot mix operands of decimal floating "
1235 : "and other floating types");
1236 16 : return error_mark_node;
1237 : }
1238 : }
1239 :
1240 : /* If one type is a vector type, return that type. (How the usual
1241 : arithmetic conversions apply to the vector types extension is not
1242 : precisely specified.) */
1243 2322495 : if (code1 == VECTOR_TYPE)
1244 : return t1;
1245 :
1246 2322206 : if (code2 == VECTOR_TYPE)
1247 : return t2;
1248 :
1249 : /* If one type is complex, form the common type of the non-complex
1250 : components, then make that complex. Use T1 or T2 if it is the
1251 : required type. */
1252 2322203 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1253 : {
1254 89322 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1255 89322 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1256 89322 : tree subtype = c_common_type (subtype1, subtype2);
1257 :
1258 89322 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1259 : return t1;
1260 59207 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1261 : return t2;
1262 3241 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1263 : {
1264 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1265 4 : TYPE_PRECISION (subtype));
1266 6 : return code1 == COMPLEX_TYPE ? t1 : t2;
1267 : }
1268 : else
1269 3237 : return build_complex_type (subtype);
1270 : }
1271 :
1272 : /* If only one is real, use it as the result. */
1273 :
1274 2232881 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1275 : return t1;
1276 :
1277 2106002 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1278 : return t2;
1279 :
1280 : /* If both are real and either are decimal floating point types, use
1281 : the decimal floating point type with the greater precision. */
1282 :
1283 2075630 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1284 : {
1285 107214 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1286 107214 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1287 : return dfloat128_type_node;
1288 : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1289 : implementation the same mode. */
1290 106665 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1291 106665 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1292 : return dfloat64x_type_node;
1293 106661 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1294 106661 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1295 : return dfloat64_type_node;
1296 106216 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1297 106216 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1298 : return dfloat32_type_node;
1299 : }
1300 :
1301 : /* Deal with fixed-point types. */
1302 2074287 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1303 : {
1304 0 : unsigned int unsignedp = 0, satp = 0;
1305 0 : scalar_mode m1, m2;
1306 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1307 :
1308 0 : m1 = SCALAR_TYPE_MODE (t1);
1309 0 : m2 = SCALAR_TYPE_MODE (t2);
1310 :
1311 : /* If one input type is saturating, the result type is saturating. */
1312 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1313 : satp = 1;
1314 :
1315 : /* If both fixed-point types are unsigned, the result type is unsigned.
1316 : When mixing fixed-point and integer types, follow the sign of the
1317 : fixed-point type.
1318 : Otherwise, the result type is signed. */
1319 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1320 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1321 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1322 0 : && TYPE_UNSIGNED (t1))
1323 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1324 0 : && TYPE_UNSIGNED (t2)))
1325 : unsignedp = 1;
1326 :
1327 : /* The result type is signed. */
1328 0 : if (unsignedp == 0)
1329 : {
1330 : /* If the input type is unsigned, we need to convert to the
1331 : signed type. */
1332 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1333 : {
1334 0 : enum mode_class mclass = (enum mode_class) 0;
1335 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1336 : mclass = MODE_FRACT;
1337 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1338 : mclass = MODE_ACCUM;
1339 : else
1340 0 : gcc_unreachable ();
1341 0 : m1 = as_a <scalar_mode>
1342 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1343 : }
1344 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1345 : {
1346 0 : enum mode_class mclass = (enum mode_class) 0;
1347 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1348 : mclass = MODE_FRACT;
1349 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1350 : mclass = MODE_ACCUM;
1351 : else
1352 0 : gcc_unreachable ();
1353 0 : m2 = as_a <scalar_mode>
1354 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1355 : }
1356 : }
1357 :
1358 0 : if (code1 == FIXED_POINT_TYPE)
1359 : {
1360 0 : fbit1 = GET_MODE_FBIT (m1);
1361 0 : ibit1 = GET_MODE_IBIT (m1);
1362 : }
1363 : else
1364 : {
1365 0 : fbit1 = 0;
1366 : /* Signed integers need to subtract one sign bit. */
1367 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1368 : }
1369 :
1370 0 : if (code2 == FIXED_POINT_TYPE)
1371 : {
1372 0 : fbit2 = GET_MODE_FBIT (m2);
1373 0 : ibit2 = GET_MODE_IBIT (m2);
1374 : }
1375 : else
1376 : {
1377 0 : fbit2 = 0;
1378 : /* Signed integers need to subtract one sign bit. */
1379 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1380 : }
1381 :
1382 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1383 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1384 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1385 : satp);
1386 : }
1387 :
1388 : /* Both real or both integers; use the one with greater precision. */
1389 :
1390 2074287 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1391 : return t1;
1392 1102581 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1393 : return t2;
1394 :
1395 : /* Same precision. Prefer long longs to longs to ints when the
1396 : same precision, following the C99 rules on integer type rank
1397 : (which are equivalent to the C90 rules for C90 types). */
1398 :
1399 738242 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1400 738242 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1401 : return long_long_unsigned_type_node;
1402 :
1403 649001 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1404 649001 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1405 : {
1406 15315 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1407 361 : return long_long_unsigned_type_node;
1408 : else
1409 : return long_long_integer_type_node;
1410 : }
1411 :
1412 633686 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1413 633686 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1414 : return long_unsigned_type_node;
1415 :
1416 534564 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1417 534564 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1418 : {
1419 : /* But preserve unsignedness from the other type,
1420 : since long cannot hold all the values of an unsigned int. */
1421 26403 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1422 131 : return long_unsigned_type_node;
1423 : else
1424 : return long_integer_type_node;
1425 : }
1426 :
1427 : /* For floating types of the same TYPE_PRECISION (which we here
1428 : assume means either the same set of values, or sets of values
1429 : neither a subset of the other, with behavior being undefined in
1430 : the latter case), follow the rules from TS 18661-3: prefer
1431 : interchange types _FloatN, then standard types long double,
1432 : double, float, then extended types _FloatNx. For extended types,
1433 : check them starting with _Float128x as that seems most consistent
1434 : in spirit with preferring long double to double; for interchange
1435 : types, also check in that order for consistency although it's not
1436 : possible for more than one of them to have the same
1437 : precision. */
1438 508161 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1439 508161 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1440 :
1441 2540239 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1442 2032241 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1443 : return FLOATN_TYPE_NODE (i);
1444 :
1445 : /* Likewise, prefer long double to double even if same size. */
1446 507998 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1447 : return long_double_type_node;
1448 :
1449 : /* Likewise, prefer double to float even if same size.
1450 : We got a couple of embedded targets with 32 bit doubles, and the
1451 : pdp11 might have 64 bit floats. */
1452 507756 : if (mv1 == double_type_node || mv2 == double_type_node)
1453 : return double_type_node;
1454 :
1455 507184 : if (mv1 == float_type_node || mv2 == float_type_node)
1456 : return float_type_node;
1457 :
1458 2020060 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1459 1515045 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1460 : return FLOATNX_TYPE_NODE (i);
1461 :
1462 505015 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1463 : {
1464 : /* Prefer any other integral types over bit-precise integer types. */
1465 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1466 6 : return code1 == BITINT_TYPE ? t2 : t1;
1467 : /* If BITINT_TYPE is unsigned and the other type is signed
1468 : non-BITINT_TYPE with the same precision, the latter has higher rank.
1469 : In that case:
1470 : Otherwise, both operands are converted to the unsigned integer type
1471 : corresponding to the type of the operand with signed integer type. */
1472 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1473 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1474 : }
1475 :
1476 : /* Otherwise prefer the unsigned one. */
1477 :
1478 505007 : if (TYPE_UNSIGNED (t1))
1479 : return t1;
1480 : else
1481 : return t2;
1482 : }
1483 :
1484 : /* Wrapper around c_common_type that is used by c-common.cc and other
1485 : front end optimizations that remove promotions. ENUMERAL_TYPEs
1486 : are allowed here and are converted to their compatible integer types.
1487 : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1488 : preferably a non-Boolean type as the common type. */
1489 : tree
1490 102837 : common_type (tree t1, tree t2)
1491 : {
1492 102837 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1493 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1494 102837 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1495 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1496 :
1497 : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1498 102837 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1499 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1500 584 : return boolean_type_node;
1501 :
1502 : /* If either type is BOOLEAN_TYPE, then return the other. */
1503 102253 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1504 : return t2;
1505 102253 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1506 : return t1;
1507 :
1508 102253 : return c_common_type (t1, t2);
1509 : }
1510 :
1511 :
1512 :
1513 : /* Helper function for comptypes. For two compatible types, return 1
1514 : if they pass consistency checks. In particular we test that
1515 : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1516 :
1517 : static bool
1518 44409495 : comptypes_verify (tree type1, tree type2)
1519 : {
1520 44409495 : if (type1 == type2 || !type1 || !type2
1521 3492820 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1522 : return true;
1523 :
1524 3492819 : gcc_checking_assert (c_verify_type (type1));
1525 3492819 : gcc_checking_assert (c_verify_type (type2));
1526 :
1527 3492819 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1528 385958 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1529 3876986 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1530 : {
1531 : /* FIXME: check other types. */
1532 383803 : if (RECORD_OR_UNION_TYPE_P (type1)
1533 383803 : || TREE_CODE (type1) == ENUMERAL_TYPE
1534 383803 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1535 0 : return false;
1536 : }
1537 : return true;
1538 : }
1539 :
1540 : struct comptypes_data {
1541 :
1542 : /* output */
1543 : bool enum_and_int_p;
1544 : bool different_types_p;
1545 : bool warning_needed;
1546 :
1547 : /* context */
1548 : bool anon_field;
1549 : bool pointedto;
1550 :
1551 : /* configuration */
1552 : bool equiv;
1553 : bool ignore_promoting_args;
1554 :
1555 : const struct tagged_tu_seen_cache* cache;
1556 : };
1557 :
1558 :
1559 : /* Helper function for composite_type. This function ignores when the
1560 : function type of an old-style declaration is incompatible with a type
1561 : of a declaration with prototype because some are arguments are not
1562 : self-promoting. This is ignored only for function types but not
1563 : ignored in a nested context. */
1564 :
1565 : static bool
1566 18959325 : comptypes_check_for_composite (tree t1, tree t2)
1567 : {
1568 18959325 : struct comptypes_data data = { };
1569 18959325 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1570 18959325 : return comptypes_internal (t1, t2, &data);
1571 : }
1572 :
1573 :
1574 : /* C implementation of compatible_types_for_indirection_note_p. */
1575 :
1576 : bool
1577 762 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1578 : {
1579 762 : return comptypes (type1, type2) == 1;
1580 : }
1581 :
1582 : /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1583 : or various other operations. Return 2 if they are compatible
1584 : but a warning may be needed if you use them together. */
1585 :
1586 : int
1587 56387154 : comptypes (tree type1, tree type2)
1588 : {
1589 56387154 : struct comptypes_data data = { };
1590 56387154 : bool ret = comptypes_internal (type1, type2, &data);
1591 :
1592 56387154 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1593 :
1594 37864013 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1595 : }
1596 :
1597 :
1598 : /* Like comptypes, but it returns non-zero only for identical
1599 : types. */
1600 :
1601 : bool
1602 374 : comptypes_same_p (tree type1, tree type2)
1603 : {
1604 374 : struct comptypes_data data = { };
1605 374 : bool ret = comptypes_internal (type1, type2, &data);
1606 :
1607 374 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1608 :
1609 374 : if (data.different_types_p)
1610 152 : return false;
1611 :
1612 : return ret;
1613 : }
1614 :
1615 :
1616 : /* Like comptypes, but if it returns non-zero because enum and int are
1617 : compatible, it sets *ENUM_AND_INT_P to true. */
1618 :
1619 : int
1620 6744875 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1621 : {
1622 6744875 : struct comptypes_data data = { };
1623 6744875 : bool ret = comptypes_internal (type1, type2, &data);
1624 6744875 : *enum_and_int_p = data.enum_and_int_p;
1625 :
1626 6744875 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1627 :
1628 6499281 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1629 : }
1630 :
1631 : /* Like comptypes, but if it returns nonzero for different types, it
1632 : sets *DIFFERENT_TYPES_P to true. */
1633 :
1634 : int
1635 45867 : comptypes_check_different_types (tree type1, tree type2,
1636 : bool *different_types_p)
1637 : {
1638 45867 : struct comptypes_data data = { };
1639 45867 : bool ret = comptypes_internal (type1, type2, &data);
1640 45867 : *different_types_p = data.different_types_p;
1641 :
1642 45867 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1643 :
1644 45867 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1645 : }
1646 :
1647 :
1648 : /* Like comptypes, but if it returns true for struct and union types
1649 : considered equivalent for aliasing purposes, i.e. for setting
1650 : TYPE_CANONICAL after completing a struct or union.
1651 :
1652 : This function must return false only for types which are not
1653 : compatible according to C language semantics (cf. comptypes),
1654 : otherwise the middle-end would make incorrect aliasing decisions.
1655 : It may return true for some similar types that are not compatible
1656 : according to those stricter rules.
1657 :
1658 : In particular, we ignore size expression in arrays so that the
1659 : following structs are in the same equivalence class:
1660 :
1661 : struct foo { char (*buf)[]; };
1662 : struct foo { char (*buf)[3]; };
1663 : struct foo { char (*buf)[4]; };
1664 :
1665 : We also treat unions / structs with members which are pointers to
1666 : structures or unions with the same tag as equivalent (if they are not
1667 : incompatible for other reasons). Although incomplete structure
1668 : or union types are not compatible to any other type, they may become
1669 : compatible to different types when completed. To avoid having to update
1670 : TYPE_CANONICAL at this point, we only consider the tag when forming
1671 : the equivalence classes. For example, the following types with tag
1672 : 'foo' are all considered equivalent:
1673 :
1674 : struct bar;
1675 : struct foo { struct bar *x };
1676 : struct foo { struct bar { int a; } *x };
1677 : struct foo { struct bar { char b; } *x }; */
1678 :
1679 : bool
1680 14656828 : comptypes_equiv_p (tree type1, tree type2)
1681 : {
1682 14656828 : struct comptypes_data data = { };
1683 14656828 : data.equiv = true;
1684 14656828 : bool ret = comptypes_internal (type1, type2, &data);
1685 :
1686 : /* check that different equivance classes are assigned only
1687 : to types that are not compatible. */
1688 14656828 : gcc_checking_assert (ret || !comptypes (type1, type2));
1689 :
1690 14656828 : return ret;
1691 : }
1692 :
1693 :
1694 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1695 : or various other operations. If they are compatible but a warning may
1696 : be needed if you use them together, 'warning_needed' in DATA is set.
1697 : If one type is an enum and the other a compatible integer type, then
1698 : this sets 'enum_and_int_p' in DATA to true (it is never set to
1699 : false). If the types are compatible but different enough not to be
1700 : permitted in C11 typedef redeclarations, then this sets
1701 : 'different_types_p' in DATA to true; it is never set to
1702 : false, but may or may not be set if the types are incompatible.
1703 : If two functions types are not compatible only because one is
1704 : an old-style definition that does not have self-promoting arguments,
1705 : then this can be ignored by setting 'ignore_promoting_args_p'.
1706 : For 'equiv' we can compute equivalency classes (see above).
1707 : This differs from comptypes, in that we don't free the seen
1708 : types. */
1709 :
1710 : static bool
1711 132695946 : comptypes_internal (const_tree type1, const_tree type2,
1712 : struct comptypes_data *data)
1713 : {
1714 132978867 : const_tree t1 = type1;
1715 132978867 : const_tree t2 = type2;
1716 :
1717 : /* Suppress errors caused by previously reported errors. */
1718 :
1719 132978867 : if (t1 == t2 || !t1 || !t2
1720 45100596 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1721 : return true;
1722 :
1723 : /* Qualifiers must match. C99 6.7.3p9 */
1724 :
1725 45100593 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1726 : return false;
1727 :
1728 : /* Enumerated types are compatible with integer types, but this is
1729 : not transitive: two enumerated types in the same translation unit
1730 : are compatible with each other only if they are the same type. */
1731 :
1732 45060533 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1733 1022 : && COMPLETE_TYPE_P (t1)
1734 45061511 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1735 : {
1736 862 : t1 = ENUM_UNDERLYING_TYPE (t1);
1737 862 : if (TREE_CODE (t2) != VOID_TYPE)
1738 : {
1739 858 : data->enum_and_int_p = true;
1740 858 : data->different_types_p = true;
1741 : }
1742 : }
1743 45059671 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1744 4717 : && COMPLETE_TYPE_P (t2)
1745 45064382 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1746 : {
1747 4595 : t2 = ENUM_UNDERLYING_TYPE (t2);
1748 4595 : if (TREE_CODE (t1) != VOID_TYPE)
1749 : {
1750 3696 : data->enum_and_int_p = true;
1751 3696 : data->different_types_p = true;
1752 : }
1753 : }
1754 :
1755 45060533 : if (t1 == t2)
1756 : return true;
1757 :
1758 : /* Different classes of types can't be compatible. */
1759 :
1760 45059153 : if (TREE_CODE (t1) != TREE_CODE (t2))
1761 : return false;
1762 :
1763 : /* Allow for two different type nodes which have essentially the same
1764 : definition. Note that we already checked for equality of the type
1765 : qualifiers (just above). */
1766 :
1767 37052383 : if (TREE_CODE (t1) != ARRAY_TYPE
1768 37052383 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1769 : return true;
1770 :
1771 36717203 : int attrval;
1772 :
1773 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1774 36717203 : if (!(attrval = comp_type_attributes (t1, t2)))
1775 : return false;
1776 :
1777 36716826 : if (2 == attrval)
1778 336 : data->warning_needed = true;
1779 :
1780 36716826 : switch (TREE_CODE (t1))
1781 : {
1782 3359397 : case INTEGER_TYPE:
1783 3359397 : case FIXED_POINT_TYPE:
1784 3359397 : case REAL_TYPE:
1785 3359397 : case BITINT_TYPE:
1786 : /* With these nodes, we can't determine type equivalence by
1787 : looking at what is stored in the nodes themselves, because
1788 : two nodes might have different TYPE_MAIN_VARIANTs but still
1789 : represent the same type. For example, wchar_t and int could
1790 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1791 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1792 : and are distinct types. On the other hand, int and the
1793 : following typedef
1794 :
1795 : typedef int INT __attribute((may_alias));
1796 :
1797 : have identical properties, different TYPE_MAIN_VARIANTs, but
1798 : represent the same type. The canonical type system keeps
1799 : track of equivalence in this case, so we fall back on it. */
1800 3359397 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1801 :
1802 282921 : case POINTER_TYPE:
1803 : /* Do not remove mode information. */
1804 282921 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1805 : return false;
1806 282921 : data->pointedto = true;
1807 282921 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1808 :
1809 8893754 : case FUNCTION_TYPE:
1810 8893754 : return function_types_compatible_p (t1, t2, data);
1811 :
1812 154398 : case ARRAY_TYPE:
1813 154398 : {
1814 154398 : tree d1 = TYPE_DOMAIN (t1);
1815 154398 : tree d2 = TYPE_DOMAIN (t2);
1816 154398 : bool d1_variable, d2_variable;
1817 154398 : bool d1_zero, d2_zero;
1818 :
1819 : /* Target types must match incl. qualifiers. */
1820 154398 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1821 : return false;
1822 :
1823 119409 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1824 6288 : data->different_types_p = true;
1825 : /* Ignore size mismatches when forming equivalence classes. */
1826 119409 : if (data->equiv)
1827 : return true;
1828 : /* Sizes must match unless one is missing or variable. */
1829 50353 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1830 : return true;
1831 :
1832 34141 : d1_zero = !TYPE_MAX_VALUE (d1);
1833 34141 : d2_zero = !TYPE_MAX_VALUE (d2);
1834 :
1835 68282 : d1_variable = (!d1_zero
1836 34141 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1837 34109 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1838 68282 : d2_variable = (!d2_zero
1839 34141 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1840 34013 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1841 :
1842 34141 : if (d1_variable != d2_variable)
1843 943 : data->different_types_p = true;
1844 34141 : if (d1_variable || d2_variable)
1845 : return true;
1846 4047 : if (d1_zero && d2_zero)
1847 : return true;
1848 4047 : if (d1_zero || d2_zero
1849 3951 : || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1850 7998 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1851 4047 : return false;
1852 :
1853 : return true;
1854 : }
1855 :
1856 23488547 : case ENUMERAL_TYPE:
1857 23488547 : case RECORD_TYPE:
1858 23488547 : case UNION_TYPE:
1859 :
1860 23488547 : if (!flag_isoc23)
1861 : return false;
1862 :
1863 23488500 : return tagged_types_tu_compatible_p (t1, t2, data);
1864 :
1865 537004 : case VECTOR_TYPE:
1866 1074008 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1867 537004 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1868 :
1869 : default:
1870 : return false;
1871 : }
1872 : gcc_unreachable ();
1873 : }
1874 :
1875 : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1876 : their qualifiers, except for named address spaces. If the pointers point to
1877 : different named addresses, then we must determine if one address space is a
1878 : subset of the other. */
1879 :
1880 : static bool
1881 1764531 : comp_target_types (location_t location, tree ttl, tree ttr)
1882 : {
1883 1764531 : int val;
1884 1764531 : int val_ped;
1885 1764531 : tree mvl = TREE_TYPE (ttl);
1886 1764531 : tree mvr = TREE_TYPE (ttr);
1887 1764531 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1888 1764531 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1889 1764531 : addr_space_t as_common;
1890 1764531 : bool enum_and_int_p;
1891 :
1892 : /* Fail if pointers point to incompatible address spaces. */
1893 1764531 : if (!addr_space_superset (asl, asr, &as_common))
1894 : return 0;
1895 :
1896 : /* For pedantic record result of comptypes on arrays before losing
1897 : qualifiers on the element type below. */
1898 1764531 : val_ped = 1;
1899 :
1900 1764531 : if (TREE_CODE (mvl) == ARRAY_TYPE
1901 1991 : && TREE_CODE (mvr) == ARRAY_TYPE)
1902 1963 : val_ped = comptypes (mvl, mvr);
1903 :
1904 : /* Qualifiers on element types of array types that are
1905 : pointer targets are also removed. */
1906 1764531 : mvl = remove_qualifiers (mvl);
1907 1764531 : mvr = remove_qualifiers (mvr);
1908 :
1909 1764531 : enum_and_int_p = false;
1910 1764531 : val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1911 :
1912 1764531 : if (val == 1 && val_ped != 1)
1913 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1914 : "in ISO C before C23");
1915 :
1916 1764531 : if (val == 2)
1917 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1918 :
1919 1764531 : if (val == 1 && enum_and_int_p && warn_cxx_compat)
1920 2 : warning_at (location, OPT_Wc___compat,
1921 : "pointer target types incompatible in C++");
1922 :
1923 1764531 : return val;
1924 : }
1925 :
1926 : /* Subroutines of `comptypes'. */
1927 :
1928 : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1929 : compatible. The two types are not the same (which has been
1930 : checked earlier in comptypes_internal). */
1931 :
1932 : static bool
1933 23488500 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1934 : struct comptypes_data *data)
1935 : {
1936 23488500 : tree s1, s2;
1937 :
1938 23488500 : if (c_type_tag (t1) != c_type_tag (t2))
1939 : return false;
1940 :
1941 : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1942 : structs with the same tag as equivalent, but only when they are targets
1943 : of pointers inside other structs. */
1944 19390350 : if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1945 : return true;
1946 :
1947 : /* Different types without tag are incompatible except as an anonymous
1948 : field or when forming equivalence classes for TYPE_CANONICAL. */
1949 19390289 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1950 : return false;
1951 :
1952 14256948 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1953 14255907 : data->different_types_p = true;
1954 :
1955 : /* Incomplete types are incompatible inside a TU. */
1956 14256948 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1957 : return false;
1958 :
1959 14256938 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1960 14256938 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1961 14256883 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1962 : return false;
1963 :
1964 14256928 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1965 144677 : data->different_types_p = true;
1966 :
1967 : /* For types already being looked at in some active
1968 : invocation of this function, assume compatibility.
1969 : The cache is built as a linked list on the stack
1970 : with the head of the list passed downwards. */
1971 14256928 : for (const struct tagged_tu_seen_cache *t = data->cache;
1972 14266604 : t != NULL; t = t->next)
1973 9970 : if (t->t1 == t1 && t->t2 == t2)
1974 : return true;
1975 :
1976 14256634 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1977 :
1978 14256634 : switch (TREE_CODE (t1))
1979 : {
1980 55 : case ENUMERAL_TYPE:
1981 55 : {
1982 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1983 : return false;
1984 :
1985 : /* Speed up the case where the type values are in the same order. */
1986 51 : tree tv1 = TYPE_VALUES (t1);
1987 51 : tree tv2 = TYPE_VALUES (t2);
1988 :
1989 51 : if (tv1 == tv2)
1990 : return true;
1991 :
1992 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1993 : {
1994 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1995 : break;
1996 :
1997 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1998 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1999 : break;
2000 : }
2001 :
2002 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
2003 : return true;
2004 :
2005 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
2006 : return false;
2007 :
2008 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
2009 : return false;
2010 :
2011 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
2012 : {
2013 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
2014 :
2015 16 : if (s2 == NULL
2016 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
2017 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
2018 8 : return false;
2019 : }
2020 :
2021 : return true;
2022 : }
2023 :
2024 14256579 : case UNION_TYPE:
2025 14256579 : case RECORD_TYPE:
2026 :
2027 14256579 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2028 : return false;
2029 :
2030 12323049 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2031 13846699 : s1 && s2;
2032 1523650 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2033 : {
2034 13781245 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2035 13781245 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2036 :
2037 13781245 : tree ft1 = TREE_TYPE (s1);
2038 13781245 : tree ft2 = TREE_TYPE (s2);
2039 :
2040 13781245 : if (DECL_NAME (s1) != DECL_NAME (s2))
2041 : return false;
2042 :
2043 12135036 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2044 : return false;
2045 :
2046 3233711 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2047 : return false;
2048 :
2049 3233691 : if (DECL_C_BIT_FIELD (s1))
2050 : {
2051 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2052 : return false;
2053 :
2054 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2055 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2056 : }
2057 :
2058 3233537 : if (!ft1 || !ft2)
2059 : return false;
2060 :
2061 3233537 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2062 : return false;
2063 :
2064 3233536 : data->anon_field = !DECL_NAME (s1);
2065 3233536 : data->pointedto = false;
2066 :
2067 3233536 : const struct tagged_tu_seen_cache *cache = data->cache;
2068 3233536 : data->cache = &entry;
2069 3233536 : bool ret = comptypes_internal (ft1, ft2, data);
2070 3233536 : data->cache = cache;
2071 3233536 : if (!ret)
2072 : return false;
2073 :
2074 1592594 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2075 1592594 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2076 :
2077 1592594 : if (data->equiv
2078 998312 : && st1 && TREE_CODE (st1) == INTEGER_CST
2079 997830 : && st2 && TREE_CODE (st2) == INTEGER_CST
2080 2590418 : && !tree_int_cst_equal (st1, st2))
2081 : return false;
2082 :
2083 1523684 : tree counted_by1 = lookup_attribute ("counted_by",
2084 1523684 : DECL_ATTRIBUTES (s1));
2085 1523684 : tree counted_by2 = lookup_attribute ("counted_by",
2086 1523684 : DECL_ATTRIBUTES (s2));
2087 : /* If there is no counted_by attribute for both fields. */
2088 1523684 : if (!counted_by1 && !counted_by2)
2089 1523620 : continue;
2090 :
2091 : /* If only one field has counted_by attribute. */
2092 64 : if ((counted_by1 && !counted_by2)
2093 64 : || (!counted_by1 && counted_by2))
2094 : return false;
2095 :
2096 : /* Now both s1 and s2 have counted_by attributes, check
2097 : whether they are the same. */
2098 :
2099 52 : tree counted_by_field1
2100 52 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2101 52 : tree counted_by_field2
2102 52 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2103 :
2104 52 : gcc_assert (counted_by_field1 && counted_by_field2);
2105 :
2106 94 : while (TREE_CHAIN (counted_by_field1))
2107 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2108 86 : while (TREE_CHAIN (counted_by_field2))
2109 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2110 :
2111 52 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2112 52 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2113 : return false;
2114 : }
2115 : return true;
2116 :
2117 0 : default:
2118 0 : gcc_unreachable ();
2119 : }
2120 : }
2121 :
2122 : /* Return true if two function types F1 and F2 are compatible.
2123 : If either type specifies no argument types,
2124 : the other must specify a fixed number of self-promoting arg types.
2125 : Otherwise, if one type specifies only the number of arguments,
2126 : the other must specify that number of self-promoting arg types.
2127 : Otherwise, the argument types must match. */
2128 :
2129 : static bool
2130 8893754 : function_types_compatible_p (const_tree f1, const_tree f2,
2131 : struct comptypes_data *data)
2132 : {
2133 8893754 : tree ret1 = TREE_TYPE (f1);
2134 8893754 : tree ret2 = TREE_TYPE (f2);
2135 :
2136 : /* 'volatile' qualifiers on a function's return type used to mean
2137 : the function is noreturn. */
2138 8893754 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2139 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2140 8893754 : if (TYPE_VOLATILE (ret1))
2141 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2142 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2143 8893754 : if (TYPE_VOLATILE (ret2))
2144 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2145 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2146 :
2147 8893754 : bool ignore_pargs = data->ignore_promoting_args;
2148 8893754 : data->ignore_promoting_args = false;
2149 :
2150 8893754 : if (!comptypes_internal (ret1, ret2, data))
2151 : return false;
2152 :
2153 8891159 : data->ignore_promoting_args = ignore_pargs;
2154 :
2155 8891159 : tree args1 = TYPE_ARG_TYPES (f1);
2156 8891159 : tree args2 = TYPE_ARG_TYPES (f2);
2157 :
2158 8891159 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2159 41524 : data->different_types_p = true;
2160 :
2161 : /* An unspecified parmlist matches any specified parmlist
2162 : whose argument types don't need default promotions. */
2163 :
2164 8891159 : if (args1 == NULL_TREE)
2165 : {
2166 21905 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2167 : return false;
2168 21902 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2169 : return false;
2170 21430 : data->ignore_promoting_args = false;
2171 : /* If one of these types comes from a non-prototype fn definition,
2172 : compare that with the other type's arglist.
2173 : If they don't match, ask for a warning (but no error). */
2174 21430 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2175 21430 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2176 12 : data->warning_needed = true;
2177 21430 : return true;
2178 : }
2179 8869254 : if (args2 == NULL_TREE)
2180 : {
2181 19701 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2182 : return false;
2183 19699 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2184 : return false;
2185 19638 : data->ignore_promoting_args = false;
2186 19638 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2187 19638 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2188 1 : data->warning_needed = true;
2189 19638 : return true;
2190 : }
2191 :
2192 : /* Both types have argument lists: compare them and propagate results. */
2193 8849553 : return type_lists_compatible_p (args1, args2, data);
2194 : }
2195 :
2196 : /* Check two lists of types for compatibility, returning false for
2197 : incompatible, true for compatible. */
2198 :
2199 : static bool
2200 8849629 : type_lists_compatible_p (const_tree args1, const_tree args2,
2201 : struct comptypes_data *data)
2202 : {
2203 55117301 : while (1)
2204 : {
2205 31983465 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2206 : return true;
2207 : /* If one list is shorter than the other,
2208 : they fail to match. */
2209 23278286 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2210 : return false;
2211 23278279 : tree a1 = TREE_VALUE (args1);
2212 23278279 : tree a2 = TREE_VALUE (args2);
2213 23278279 : tree mv1 = remove_qualifiers (a1);
2214 23278279 : tree mv2 = remove_qualifiers (a2);
2215 :
2216 23278279 : gcc_assert (mv2);
2217 23278279 : gcc_assert (mv2);
2218 :
2219 : /* If one of the lists has an error marker, ignore this arg. */
2220 23278279 : if (TREE_CODE (a1) == ERROR_MARK
2221 23278275 : || TREE_CODE (a2) == ERROR_MARK)
2222 : ;
2223 23278259 : else if (!comptypes_internal (mv1, mv2, data))
2224 : {
2225 144674 : data->different_types_p = true;
2226 : /* Allow wait (union {union wait *u; int *i} *)
2227 : and wait (union wait *) to be compatible. */
2228 144674 : if (TREE_CODE (a1) == UNION_TYPE
2229 130 : && (TYPE_NAME (a1) == NULL_TREE
2230 98 : || TYPE_TRANSPARENT_AGGR (a1))
2231 130 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2232 144804 : && tree_int_cst_equal (TYPE_SIZE (a1),
2233 130 : TYPE_SIZE (a2)))
2234 : {
2235 130 : tree memb;
2236 130 : for (memb = TYPE_FIELDS (a1);
2237 166 : memb; memb = DECL_CHAIN (memb))
2238 : {
2239 164 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2240 164 : if (comptypes_internal (mv3, mv2, data))
2241 : break;
2242 : }
2243 : if (memb == NULL_TREE)
2244 : return false;
2245 : }
2246 144544 : else if (TREE_CODE (a2) == UNION_TYPE
2247 105 : && (TYPE_NAME (a2) == NULL_TREE
2248 74 : || TYPE_TRANSPARENT_AGGR (a2))
2249 105 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2250 144649 : && tree_int_cst_equal (TYPE_SIZE (a2),
2251 105 : TYPE_SIZE (a1)))
2252 : {
2253 105 : tree memb;
2254 105 : for (memb = TYPE_FIELDS (a2);
2255 133 : memb; memb = DECL_CHAIN (memb))
2256 : {
2257 131 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2258 131 : if (comptypes_internal (mv3, mv1, data))
2259 : break;
2260 : }
2261 : if (memb == NULL_TREE)
2262 : return false;
2263 : }
2264 : else
2265 144439 : return false;
2266 : }
2267 :
2268 23133836 : args1 = TREE_CHAIN (args1);
2269 23133836 : args2 = TREE_CHAIN (args2);
2270 23133836 : }
2271 : }
2272 :
2273 : /* Compute the size to increment a pointer by. When a function type or void
2274 : type or incomplete type is passed, size_one_node is returned.
2275 : This function does not emit any diagnostics; the caller is responsible
2276 : for that. */
2277 :
2278 : static tree
2279 233711 : c_size_in_bytes (const_tree type)
2280 : {
2281 233711 : enum tree_code code = TREE_CODE (type);
2282 :
2283 233711 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2284 233711 : || !COMPLETE_TYPE_P (type))
2285 191 : return size_one_node;
2286 :
2287 : /* Convert in case a char is more than one unit. */
2288 233520 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2289 233520 : size_int (TYPE_PRECISION (char_type_node)
2290 : / BITS_PER_UNIT));
2291 : }
2292 :
2293 : /* Return either DECL or its known constant value (if it has one). */
2294 :
2295 : tree
2296 14335153 : decl_constant_value_1 (tree decl, bool in_init)
2297 : {
2298 14335153 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2299 14335153 : TREE_CODE (decl) != PARM_DECL
2300 14335153 : && !TREE_THIS_VOLATILE (decl)
2301 13990847 : && TREE_READONLY (decl)
2302 36928 : && DECL_INITIAL (decl) != NULL_TREE
2303 35932 : && !error_operand_p (DECL_INITIAL (decl))
2304 : /* This is invalid if initial value is not constant.
2305 : If it has either a function call, a memory reference,
2306 : or a variable, then re-evaluating it could give different results. */
2307 35923 : && TREE_CONSTANT (DECL_INITIAL (decl))
2308 : /* Check for cases where this is sub-optimal, even though valid. */
2309 14358013 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2310 17645 : return DECL_INITIAL (decl);
2311 : return decl;
2312 : }
2313 :
2314 : /* Return either DECL or its known constant value (if it has one).
2315 : Like the above, but always return decl outside of functions. */
2316 :
2317 : tree
2318 14334905 : decl_constant_value (tree decl)
2319 : {
2320 : /* Don't change a variable array bound or initial value to a constant
2321 : in a place where a variable is invalid. */
2322 14334905 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2323 : }
2324 :
2325 : /* Convert the array expression EXP to a pointer. */
2326 : static tree
2327 820652 : array_to_pointer_conversion (location_t loc, tree exp)
2328 : {
2329 820652 : tree orig_exp = exp;
2330 820652 : tree type = TREE_TYPE (exp);
2331 820652 : tree adr;
2332 820652 : tree restype = TREE_TYPE (type);
2333 820652 : tree ptrtype;
2334 :
2335 820652 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2336 :
2337 820652 : STRIP_TYPE_NOPS (exp);
2338 :
2339 820652 : copy_warning (exp, orig_exp);
2340 :
2341 820652 : ptrtype = c_build_pointer_type (restype);
2342 :
2343 820652 : if (INDIRECT_REF_P (exp))
2344 2906 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2345 :
2346 : /* In C++ array compound literals are temporary objects unless they are
2347 : const or appear in namespace scope, so they are destroyed too soon
2348 : to use them for much of anything (c++/53220). */
2349 817746 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2350 : {
2351 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2352 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2353 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2354 : "converting an array compound literal to a pointer "
2355 : "leads to a dangling pointer in C++");
2356 : }
2357 :
2358 817746 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2359 817746 : return convert (ptrtype, adr);
2360 : }
2361 :
2362 : /* Convert the function expression EXP to a pointer. */
2363 : static tree
2364 50601970 : function_to_pointer_conversion (location_t loc, tree exp)
2365 : {
2366 50601970 : tree orig_exp = exp;
2367 :
2368 50601970 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2369 :
2370 50601970 : STRIP_TYPE_NOPS (exp);
2371 :
2372 50601970 : copy_warning (exp, orig_exp);
2373 :
2374 50601970 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2375 :
2376 : /* If the function is defined and known to not to require a non-local
2377 : context, make sure no trampoline is generated. */
2378 50601970 : if (TREE_CODE (exp) == FUNCTION_DECL
2379 50601970 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2380 16070093 : TREE_NO_TRAMPOLINE (exp2) = 1;
2381 :
2382 50601970 : return exp2;
2383 : }
2384 :
2385 : /* Mark EXP as read, not just set, for set but not used -Wunused
2386 : warning purposes. */
2387 :
2388 : void
2389 490072940 : mark_exp_read (tree exp)
2390 : {
2391 616463899 : switch (TREE_CODE (exp))
2392 : {
2393 203608863 : case VAR_DECL:
2394 203608863 : case PARM_DECL:
2395 203608863 : DECL_READ_P (exp) = 1;
2396 203608863 : break;
2397 19716140 : CASE_CONVERT:
2398 19716140 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2399 3800 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2400 : {
2401 : case PREINCREMENT_EXPR:
2402 : case PREDECREMENT_EXPR:
2403 : case POSTINCREMENT_EXPR:
2404 : case POSTDECREMENT_EXPR:
2405 : return;
2406 : default:
2407 : break;
2408 : }
2409 : /* FALLTHRU */
2410 123645207 : case ARRAY_REF:
2411 123645207 : case COMPONENT_REF:
2412 123645207 : case MODIFY_EXPR:
2413 123645207 : case REALPART_EXPR:
2414 123645207 : case IMAGPART_EXPR:
2415 123645207 : case ADDR_EXPR:
2416 123645207 : case VIEW_CONVERT_EXPR:
2417 123645207 : case PREINCREMENT_EXPR:
2418 123645207 : case PREDECREMENT_EXPR:
2419 123645207 : case POSTINCREMENT_EXPR:
2420 123645207 : case POSTDECREMENT_EXPR:
2421 123645207 : mark_exp_read (TREE_OPERAND (exp, 0));
2422 123645207 : break;
2423 236310 : case COMPOUND_EXPR:
2424 : /* Pattern match what build_atomic_assign produces with modifycode
2425 : NOP_EXPR. */
2426 236310 : if (VAR_P (TREE_OPERAND (exp, 1))
2427 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2428 263453 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2429 : {
2430 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2431 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2432 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2433 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2434 4410 : && TREE_CODE (t2) == CALL_EXPR)
2435 : {
2436 2200 : tree fndecl = get_callee_fndecl (t2);
2437 2200 : tree arg = NULL_TREE;
2438 2200 : if (fndecl
2439 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2440 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2441 4400 : && call_expr_nargs (t2) >= 2)
2442 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2443 : {
2444 130 : case BUILT_IN_ATOMIC_STORE:
2445 130 : arg = CALL_EXPR_ARG (t2, 1);
2446 130 : break;
2447 2070 : case BUILT_IN_ATOMIC_STORE_1:
2448 2070 : case BUILT_IN_ATOMIC_STORE_2:
2449 2070 : case BUILT_IN_ATOMIC_STORE_4:
2450 2070 : case BUILT_IN_ATOMIC_STORE_8:
2451 2070 : case BUILT_IN_ATOMIC_STORE_16:
2452 2070 : arg = CALL_EXPR_ARG (t2, 0);
2453 2070 : break;
2454 : default:
2455 : break;
2456 : }
2457 2200 : if (arg)
2458 : {
2459 2200 : STRIP_NOPS (arg);
2460 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2461 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2462 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2463 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2464 : }
2465 : }
2466 : }
2467 : /* FALLTHRU */
2468 2745209 : case C_MAYBE_CONST_EXPR:
2469 2745209 : mark_exp_read (TREE_OPERAND (exp, 1));
2470 2745209 : break;
2471 578 : case OMP_ARRAY_SECTION:
2472 578 : mark_exp_read (TREE_OPERAND (exp, 0));
2473 578 : if (TREE_OPERAND (exp, 1))
2474 384 : mark_exp_read (TREE_OPERAND (exp, 1));
2475 578 : if (TREE_OPERAND (exp, 2))
2476 543 : mark_exp_read (TREE_OPERAND (exp, 2));
2477 : break;
2478 : default:
2479 : break;
2480 : }
2481 : }
2482 :
2483 : /* Perform the default conversion of arrays and functions to pointers.
2484 : Return the result of converting EXP. For any other expression, just
2485 : return EXP.
2486 :
2487 : LOC is the location of the expression. */
2488 :
2489 : struct c_expr
2490 362199834 : default_function_array_conversion (location_t loc, struct c_expr exp)
2491 : {
2492 362199834 : tree orig_exp = exp.value;
2493 362199834 : tree type = TREE_TYPE (exp.value);
2494 362199834 : enum tree_code code = TREE_CODE (type);
2495 :
2496 362199834 : switch (code)
2497 : {
2498 : case ARRAY_TYPE:
2499 : {
2500 : bool not_lvalue = false;
2501 : bool lvalue_array_p;
2502 :
2503 773752 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2504 773752 : || CONVERT_EXPR_P (exp.value))
2505 773752 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2506 : {
2507 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2508 0 : not_lvalue = true;
2509 0 : exp.value = TREE_OPERAND (exp.value, 0);
2510 : }
2511 :
2512 773752 : copy_warning (exp.value, orig_exp);
2513 :
2514 773752 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2515 773752 : if (!flag_isoc99 && !lvalue_array_p)
2516 : {
2517 : /* Before C99, non-lvalue arrays do not decay to pointers.
2518 : Normally, using such an array would be invalid; but it can
2519 : be used correctly inside sizeof or as a statement expression.
2520 : Thus, do not give an error here; an error will result later. */
2521 109 : return exp;
2522 : }
2523 :
2524 773643 : exp.value = array_to_pointer_conversion (loc, exp.value);
2525 : }
2526 773643 : break;
2527 762129 : case FUNCTION_TYPE:
2528 762129 : exp.value = function_to_pointer_conversion (loc, exp.value);
2529 762129 : break;
2530 : default:
2531 : break;
2532 : }
2533 :
2534 362199725 : return exp;
2535 : }
2536 :
2537 : struct c_expr
2538 947445 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2539 : {
2540 947445 : mark_exp_read (exp.value);
2541 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2542 947445 : if (TREE_CODE (exp.value) == COMPONENT_REF
2543 947445 : && handle_counted_by_p (exp.value))
2544 66620 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2545 947445 : return default_function_array_conversion (loc, exp);
2546 : }
2547 :
2548 : /* Return whether EXPR should be treated as an atomic lvalue for the
2549 : purposes of load and store handling. */
2550 :
2551 : static bool
2552 363295665 : really_atomic_lvalue (tree expr)
2553 : {
2554 363295665 : if (error_operand_p (expr))
2555 : return false;
2556 363288129 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2557 : return false;
2558 57393 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2559 : return false;
2560 57390 : if (!lvalue_p (expr))
2561 : return false;
2562 :
2563 : /* Ignore _Atomic on register variables, since their addresses can't
2564 : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2565 : sequences wouldn't work. Ignore _Atomic on structures containing
2566 : bit-fields, since accessing elements of atomic structures or
2567 : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2568 : it's undefined at translation time or execution time, and the
2569 : normal atomic sequences again wouldn't work. */
2570 57532 : while (handled_component_p (expr))
2571 : {
2572 144 : if (TREE_CODE (expr) == COMPONENT_REF
2573 144 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2574 : return false;
2575 144 : expr = TREE_OPERAND (expr, 0);
2576 : }
2577 57388 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2578 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2579 112572 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2580 : return false;
2581 : return true;
2582 : }
2583 :
2584 : /* If EXPR is a named constant (C23) derived from a constexpr variable
2585 : - that is, a reference to such a variable, or a member extracted by
2586 : a sequence of structure and union (but not array) member accesses
2587 : (where union member accesses must access the same member as
2588 : initialized) - then return the corresponding initializer;
2589 : otherwise, return NULL_TREE. */
2590 :
2591 : static tree
2592 360107498 : maybe_get_constexpr_init (tree expr)
2593 : {
2594 360107498 : tree decl = NULL_TREE;
2595 360107498 : if (TREE_CODE (expr) == VAR_DECL)
2596 : decl = expr;
2597 348395072 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2598 822274 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2599 822274 : if (decl
2600 12534700 : && C_DECL_DECLARED_CONSTEXPR (decl)
2601 370 : && DECL_INITIAL (decl) != NULL_TREE
2602 822644 : && !error_operand_p (DECL_INITIAL (decl)))
2603 370 : return DECL_INITIAL (decl);
2604 360107128 : if (TREE_CODE (expr) != COMPONENT_REF)
2605 : return NULL_TREE;
2606 872042 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2607 872042 : if (inner == NULL_TREE)
2608 : return NULL_TREE;
2609 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2610 47 : && !error_operand_p (inner)
2611 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2612 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2613 47 : inner = TREE_OPERAND (inner, 0);
2614 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2615 : return NULL_TREE;
2616 126 : tree field = TREE_OPERAND (expr, 1);
2617 126 : unsigned HOST_WIDE_INT cidx;
2618 126 : tree cfield, cvalue;
2619 126 : bool have_other_init = false;
2620 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2621 : {
2622 250 : if (cfield == field)
2623 : return cvalue;
2624 140 : have_other_init = true;
2625 : }
2626 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2627 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2628 : return NULL_TREE;
2629 : /* Return a default initializer. */
2630 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2631 5 : return build_constructor (TREE_TYPE (expr), NULL);
2632 8 : return build_zero_cst (TREE_TYPE (expr));
2633 : }
2634 :
2635 : /* Helper function for convert_lvalue_to_rvalue called via
2636 : walk_tree_without_duplicates. Find DATA inside of the expression. */
2637 :
2638 : static tree
2639 230295 : c_find_var_r (tree *tp, int *walk_subtrees, void *data)
2640 : {
2641 230295 : if (TYPE_P (*tp))
2642 0 : *walk_subtrees = 0;
2643 230295 : else if (*tp == (tree) data)
2644 : return *tp;
2645 : return NULL_TREE;
2646 : }
2647 :
2648 : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2649 : including converting functions and arrays to pointers if CONVERT_P.
2650 : If READ_P, also mark the expression as having been read. If
2651 : FOR_INIT, constexpr expressions of structure and union type should
2652 : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2653 : constexpr scalars (including elements of structures and unions) are
2654 : replaced by their initializers. */
2655 :
2656 : struct c_expr
2657 359423128 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2658 : bool convert_p, bool read_p, bool for_init)
2659 : {
2660 359423128 : bool force_non_npc = false;
2661 359423128 : if (read_p)
2662 315383725 : mark_exp_read (exp.value);
2663 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2664 315383725 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2665 780941 : && handle_counted_by_p (exp.value))
2666 780912 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2667 :
2668 359423128 : if (convert_p)
2669 359409898 : exp = default_function_array_conversion (loc, exp);
2670 359423128 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2671 359423128 : || (flag_isoc2y
2672 1359 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2673 356942195 : exp.value = require_complete_type (loc, exp.value);
2674 359423128 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2675 : {
2676 359235456 : tree init = maybe_get_constexpr_init (exp.value);
2677 359235456 : if (init != NULL_TREE)
2678 : {
2679 : /* A named constant of pointer type or type nullptr_t is not
2680 : a null pointer constant even if the initializer is
2681 : one. */
2682 367 : if (TREE_CODE (init) == INTEGER_CST
2683 232 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2684 393 : && integer_zerop (init))
2685 : force_non_npc = true;
2686 : exp.value = init;
2687 : }
2688 : }
2689 359423128 : if (really_atomic_lvalue (exp.value))
2690 : {
2691 26938 : vec<tree, va_gc> *params;
2692 26938 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2693 26938 : tree expr_type = TREE_TYPE (exp.value);
2694 26938 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2695 26938 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2696 :
2697 26938 : gcc_assert (TYPE_ATOMIC (expr_type));
2698 :
2699 : /* Expansion of a generic atomic load may require an addition
2700 : element, so allocate enough to prevent a resize. */
2701 26938 : vec_alloc (params, 4);
2702 :
2703 : /* Remove the qualifiers for the rest of the expressions and
2704 : create the VAL temp variable to hold the RHS. */
2705 26938 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2706 26938 : tmp = create_tmp_var_raw (nonatomic_type);
2707 26938 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2708 26938 : TREE_ADDRESSABLE (tmp) = 1;
2709 : /* Do not disable warnings for TMP even though it's artificial.
2710 : -Winvalid-memory-model depends on it. */
2711 :
2712 : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2713 26938 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2714 26938 : params->quick_push (expr_addr);
2715 26938 : params->quick_push (tmp_addr);
2716 26938 : params->quick_push (seq_cst);
2717 26938 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2718 :
2719 : /* EXPR is always read. */
2720 26938 : mark_exp_read (exp.value);
2721 :
2722 : /* Optimize the common case where c_build_function_call_vec
2723 : immediately folds __atomic_load (&expr, &tmp, SEQ_CST); into
2724 : tmp = __atomic_load_<N> (&expr, SEQ_CST);
2725 : In that case tmp is not addressable and can be initialized
2726 : fully by the rhs of the MODIFY_EXPR. */
2727 26938 : tree tem = func_call;
2728 26938 : if (CONVERT_EXPR_P (tem) && VOID_TYPE_P (TREE_TYPE (tem)))
2729 : {
2730 25683 : tem = TREE_OPERAND (tem, 0);
2731 25683 : if (TREE_CODE (tem) == MODIFY_EXPR
2732 25683 : && TREE_OPERAND (tem, 0) == tmp
2733 51366 : && !walk_tree_without_duplicates (&TREE_OPERAND (tem, 1),
2734 : c_find_var_r, tmp))
2735 : {
2736 25683 : TREE_ADDRESSABLE (tmp) = 0;
2737 25683 : func_call = TREE_OPERAND (tem, 1);
2738 : }
2739 : }
2740 :
2741 : /* Return tmp which contains the value loaded. */
2742 26938 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2743 : NULL_TREE, NULL_TREE);
2744 : }
2745 359409898 : if (convert_p && !error_operand_p (exp.value)
2746 718825534 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2747 359402297 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2748 359423128 : if (force_non_npc)
2749 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2750 :
2751 359423128 : {
2752 359423128 : tree false_value, true_value;
2753 359409898 : if (convert_p && !error_operand_p (exp.value)
2754 718825534 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2755 : &false_value, &true_value))
2756 : {
2757 9721 : tree t = save_expr (exp.value);
2758 :
2759 9721 : mark_exp_read (exp.value);
2760 :
2761 9721 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2762 9721 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2763 9721 : expr = build_compound_expr (loc, expr, boolean_true_node);
2764 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2765 : fold_build2_loc (loc, NE_EXPR,
2766 : boolean_type_node,
2767 : t, true_value),
2768 : expr, boolean_true_node);
2769 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2770 : fold_build2_loc (loc, NE_EXPR,
2771 : boolean_type_node,
2772 : t, false_value),
2773 : expr, boolean_false_node);
2774 :
2775 9721 : exp.value = expr;
2776 : }
2777 : }
2778 :
2779 359423128 : return exp;
2780 : }
2781 :
2782 : /* Wrapper for the overload above, same arguments but for tree rather than
2783 : c_expr. This is important for hardbools to decay to bools. */
2784 :
2785 : static inline tree
2786 575371 : convert_lvalue_to_rvalue (location_t loc, tree val,
2787 : bool convert_p, bool read_p, bool for_init = false)
2788 : {
2789 575371 : struct c_expr expr;
2790 575371 : memset (&expr, 0, sizeof (expr));
2791 575371 : expr.value = val;
2792 575371 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2793 575371 : return expr.value;
2794 : }
2795 :
2796 : /* EXP is an expression of integer type. Apply the integer promotions
2797 : to it and return the promoted value. */
2798 :
2799 : tree
2800 76898544 : perform_integral_promotions (tree exp)
2801 : {
2802 76898544 : tree type = TREE_TYPE (exp);
2803 76898544 : enum tree_code code = TREE_CODE (type);
2804 :
2805 76898544 : gcc_assert (INTEGRAL_TYPE_P (type));
2806 :
2807 : /* Convert enums to the result of applying the integer promotions to
2808 : their underlying type. */
2809 76898544 : if (code == ENUMERAL_TYPE)
2810 : {
2811 650472 : type = ENUM_UNDERLYING_TYPE (type);
2812 650472 : if (c_promoting_integer_type_p (type))
2813 : {
2814 94 : if (TYPE_UNSIGNED (type)
2815 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2816 0 : type = unsigned_type_node;
2817 : else
2818 94 : type = integer_type_node;
2819 : }
2820 :
2821 650472 : return convert (type, exp);
2822 : }
2823 :
2824 : /* ??? This should no longer be needed now bit-fields have their
2825 : proper types. */
2826 76248072 : if (TREE_CODE (exp) == COMPONENT_REF
2827 76248072 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2828 : {
2829 62179 : if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2830 : == BITINT_TYPE)
2831 454 : return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2832 : /* If it's thinner than an int, promote it like a
2833 : c_promoting_integer_type_p, otherwise leave it alone. */
2834 61725 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2835 61725 : TYPE_PRECISION (integer_type_node)) < 0)
2836 53463 : return convert (integer_type_node, exp);
2837 : }
2838 :
2839 76194155 : if (c_promoting_integer_type_p (type))
2840 : {
2841 : /* Preserve unsignedness if not really getting any wider. */
2842 643381 : if (TYPE_UNSIGNED (type)
2843 643381 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2844 0 : return convert (unsigned_type_node, exp);
2845 :
2846 643381 : return convert (integer_type_node, exp);
2847 : }
2848 :
2849 : return exp;
2850 : }
2851 :
2852 :
2853 : /* Perform default promotions for C data used in expressions.
2854 : Enumeral types or short or char are converted to int.
2855 : In addition, manifest constants symbols are replaced by their values. */
2856 :
2857 : tree
2858 86422819 : default_conversion (tree exp)
2859 : {
2860 86422819 : tree orig_exp;
2861 86422819 : tree type = TREE_TYPE (exp);
2862 86422819 : enum tree_code code = TREE_CODE (type);
2863 86422819 : tree promoted_type;
2864 :
2865 86422819 : mark_exp_read (exp);
2866 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2867 86422819 : if (TREE_CODE (exp) == COMPONENT_REF
2868 86422819 : && handle_counted_by_p (exp))
2869 598125 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2870 :
2871 : /* Functions and arrays have been converted during parsing. */
2872 86422819 : gcc_assert (code != FUNCTION_TYPE);
2873 86422819 : if (code == ARRAY_TYPE)
2874 : return exp;
2875 :
2876 : /* Constants can be used directly unless they're not loadable. */
2877 86422660 : if (TREE_CODE (exp) == CONST_DECL)
2878 0 : exp = DECL_INITIAL (exp);
2879 :
2880 : /* Strip no-op conversions. */
2881 86422660 : orig_exp = exp;
2882 86794305 : STRIP_TYPE_NOPS (exp);
2883 :
2884 86422660 : copy_warning (exp, orig_exp);
2885 :
2886 86422660 : if (code == VOID_TYPE)
2887 : {
2888 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2889 : "void value not ignored as it ought to be");
2890 2 : return error_mark_node;
2891 : }
2892 :
2893 86422658 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2894 86422658 : if (exp == error_mark_node)
2895 : return error_mark_node;
2896 :
2897 86421755 : promoted_type = targetm.promoted_type (type);
2898 86421755 : if (promoted_type)
2899 0 : return convert (promoted_type, exp);
2900 :
2901 86421755 : if (INTEGRAL_TYPE_P (type))
2902 75901315 : return perform_integral_promotions (exp);
2903 :
2904 : return exp;
2905 : }
2906 :
2907 : /* Look up COMPONENT in a structure or union TYPE.
2908 :
2909 : If the component name is not found, returns NULL_TREE. Otherwise,
2910 : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2911 : stepping down the chain to the component, which is in the last
2912 : TREE_VALUE of the list. Normally the list is of length one, but if
2913 : the component is embedded within (nested) anonymous structures or
2914 : unions, the list steps down the chain to the component. */
2915 :
2916 : tree
2917 2314535 : lookup_field (const_tree type, tree component)
2918 : {
2919 2314535 : tree field;
2920 :
2921 : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2922 : to the field elements. Use a binary search on this array to quickly
2923 : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2924 : will always be set for structures which have many elements.
2925 :
2926 : Duplicate field checking replaces duplicates with NULL_TREE so
2927 : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2928 : case just iterate using DECL_CHAIN. */
2929 :
2930 2614527 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2931 2614527 : && !seen_error ())
2932 : {
2933 299971 : int bot, top, half;
2934 299971 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2935 :
2936 299971 : field = TYPE_FIELDS (type);
2937 299971 : bot = 0;
2938 299971 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2939 1378627 : while (top - bot > 1)
2940 : {
2941 1350014 : half = (top - bot + 1) >> 1;
2942 1350014 : field = field_array[bot+half];
2943 :
2944 1350014 : if (DECL_NAME (field) == NULL_TREE)
2945 : {
2946 : /* Step through all anon unions in linear fashion. */
2947 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2948 : {
2949 0 : field = field_array[bot++];
2950 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2951 : {
2952 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2953 :
2954 0 : if (anon)
2955 0 : return tree_cons (NULL_TREE, field, anon);
2956 :
2957 : /* The Plan 9 compiler permits referring
2958 : directly to an anonymous struct/union field
2959 : using a typedef name. */
2960 0 : if (flag_plan9_extensions
2961 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2962 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2963 : == TYPE_DECL)
2964 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2965 : == component))
2966 : break;
2967 : }
2968 : }
2969 :
2970 : /* Entire record is only anon unions. */
2971 0 : if (bot > top)
2972 : return NULL_TREE;
2973 :
2974 : /* Restart the binary search, with new lower bound. */
2975 0 : continue;
2976 0 : }
2977 :
2978 1350014 : if (DECL_NAME (field) == component)
2979 : break;
2980 1078656 : if (DECL_NAME (field) < component)
2981 : bot += half;
2982 : else
2983 892829 : top = bot + half;
2984 : }
2985 :
2986 299971 : if (DECL_NAME (field_array[bot]) == component)
2987 : field = field_array[bot];
2988 271358 : else if (DECL_NAME (field) != component)
2989 : return NULL_TREE;
2990 : }
2991 : else
2992 : {
2993 5246117 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2994 : {
2995 5236824 : if (DECL_NAME (field) == NULL_TREE
2996 5236824 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2997 : {
2998 12196 : tree anon = lookup_field (TREE_TYPE (field), component);
2999 :
3000 12196 : if (anon)
3001 2998 : return tree_cons (NULL_TREE, field, anon);
3002 :
3003 : /* The Plan 9 compiler permits referring directly to an
3004 : anonymous struct/union field using a typedef
3005 : name. */
3006 9198 : if (flag_plan9_extensions
3007 174 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3008 172 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
3009 9260 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3010 : == component))
3011 : break;
3012 : }
3013 :
3014 5233822 : if (DECL_NAME (field) == component)
3015 : break;
3016 : }
3017 :
3018 2011566 : if (field == NULL_TREE)
3019 : return NULL_TREE;
3020 : }
3021 :
3022 2302244 : return tree_cons (NULL_TREE, field, NULL_TREE);
3023 : }
3024 :
3025 : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
3026 :
3027 : static void
3028 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
3029 : vec<tree> *candidates)
3030 : {
3031 101 : tree field;
3032 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3033 : {
3034 157 : if (DECL_NAME (field) == NULL_TREE
3035 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3036 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
3037 : candidates);
3038 :
3039 157 : if (DECL_NAME (field))
3040 111 : candidates->safe_push (DECL_NAME (field));
3041 : }
3042 101 : }
3043 :
3044 : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
3045 : rather than returning a TREE_LIST for an exact match. */
3046 :
3047 : static tree
3048 71 : lookup_field_fuzzy (tree type, tree component)
3049 : {
3050 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
3051 :
3052 : /* First, gather a list of candidates. */
3053 71 : auto_vec <tree> candidates;
3054 :
3055 71 : lookup_field_fuzzy_find_candidates (type, component,
3056 : &candidates);
3057 :
3058 71 : return find_closest_identifier (component, &candidates);
3059 71 : }
3060 :
3061 : /* Support function for build_component_ref's error-handling.
3062 :
3063 : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
3064 : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
3065 :
3066 : static bool
3067 72 : should_suggest_deref_p (tree datum_type)
3068 : {
3069 : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
3070 : allows "." for ptrs; we could be handling a failed attempt
3071 : to access a property. */
3072 72 : if (c_dialect_objc ())
3073 : return false;
3074 :
3075 : /* Only suggest it for pointers... */
3076 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3077 : return false;
3078 :
3079 : /* ...to structs/unions. */
3080 8 : tree underlying_type = TREE_TYPE (datum_type);
3081 8 : enum tree_code code = TREE_CODE (underlying_type);
3082 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3083 : return true;
3084 : else
3085 1 : return false;
3086 : }
3087 :
3088 : /* Give a component ref REF, decide whether we should handle its counted_by
3089 : attribute based on its context:
3090 : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3091 :
3092 : static bool
3093 2270651 : handle_counted_by_p (tree ref)
3094 : {
3095 2270651 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3096 2270651 : tree datum = TREE_OPERAND (ref, 0);
3097 : /* If the component_ref is build for a offsetof, i.e., the datum
3098 : of the component_ref is a indirect_ref of null_pointer_node,
3099 : we should not generate call to .ACCESS_WITH_SIZE. */
3100 2270651 : if (TREE_CODE (datum) == INDIRECT_REF
3101 2270651 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3102 : return false;
3103 2270321 : if (in_typeof || in_alignof)
3104 8786 : return false;
3105 : return true;
3106 : }
3107 :
3108 : /* Given a component ref REF, if there is a counted_by attribute attached,
3109 : issue error when the element_type is a structure or union including a
3110 : flexible array member. */
3111 :
3112 : static void
3113 2270568 : check_counted_by_attribute (location_t loc, tree ref)
3114 : {
3115 2270568 : tree subdatum = TREE_OPERAND (ref, 1);
3116 2270568 : tree sub_type = TREE_TYPE (subdatum);
3117 :
3118 2270568 : if (!c_flexible_array_member_type_p (sub_type)
3119 2270568 : && TREE_CODE (sub_type) != POINTER_TYPE)
3120 : return;
3121 :
3122 386812 : tree element_type = TREE_TYPE (sub_type);
3123 :
3124 386812 : tree attr_counted_by = lookup_attribute ("counted_by",
3125 386812 : DECL_ATTRIBUTES (subdatum));
3126 386812 : if (attr_counted_by)
3127 : {
3128 : /* Issue error when the element_type is a structure or
3129 : union including a flexible array member. */
3130 859 : if (RECORD_OR_UNION_TYPE_P (element_type)
3131 859 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3132 : {
3133 2 : error_at (loc,
3134 : "%<counted_by%> attribute is not allowed for a pointer to"
3135 : " structure or union with flexible array member");
3136 2 : return;
3137 : }
3138 : }
3139 : }
3140 :
3141 : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3142 : to the object that represents its counted_by per the attribute
3143 : counted_by attached to this field if it's a flexible array member
3144 : or a pointer field, otherwise return NULL_TREE.
3145 : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3146 : For example, if:
3147 :
3148 : struct P {
3149 : int k;
3150 : int x[] __attribute__ ((counted_by (k)));
3151 : } *p;
3152 :
3153 : for:
3154 : p->x
3155 :
3156 : the ref to the object that represents its element count will be:
3157 :
3158 : &(p->k)
3159 : */
3160 :
3161 : static tree
3162 499848 : build_counted_by_ref (tree datum, tree subdatum,
3163 : tree *counted_by_type)
3164 : {
3165 499848 : tree sub_type = TREE_TYPE (subdatum);
3166 499848 : if (!c_flexible_array_member_type_p (sub_type)
3167 499848 : && TREE_CODE (sub_type) != POINTER_TYPE)
3168 : return NULL_TREE;
3169 :
3170 499848 : tree attr_counted_by = lookup_attribute ("counted_by",
3171 499848 : DECL_ATTRIBUTES (subdatum));
3172 499848 : if (!attr_counted_by)
3173 : return NULL_TREE;
3174 :
3175 632 : tree counted_by_ref = NULL_TREE;
3176 632 : *counted_by_type = NULL_TREE;
3177 :
3178 632 : tree type = TREE_TYPE (datum);
3179 :
3180 : /* If the type of the containing structure is an anonymous struct/union,
3181 : and this anonymous struct/union is not a root type, get the first
3182 : outer named structure/union type. */
3183 632 : while (TREE_CODE (datum) == COMPONENT_REF
3184 35 : && c_type_tag (type) == NULL_TREE
3185 702 : && DECL_NAME (TREE_OPERAND (datum, 1)) == NULL_TREE)
3186 : {
3187 35 : datum = TREE_OPERAND (datum, 0);
3188 35 : type = TREE_TYPE (datum);
3189 : }
3190 :
3191 632 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3192 632 : tree counted_by_field = lookup_field (type, field_id);
3193 632 : gcc_assert (counted_by_field);
3194 :
3195 1044 : tree counted_by_subdatum;
3196 1044 : do
3197 : {
3198 1044 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3199 : /* Get the TYPE of the counted_by field. */
3200 1044 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3201 :
3202 1044 : counted_by_ref
3203 1044 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3204 : datum, counted_by_subdatum, NULL_TREE);
3205 :
3206 1044 : datum = counted_by_ref;
3207 1044 : counted_by_field = TREE_CHAIN (counted_by_field);
3208 : }
3209 1044 : while (counted_by_field);
3210 :
3211 632 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3212 632 : return counted_by_ref;
3213 : }
3214 :
3215 : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3216 : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3217 : call to the internal function .ACCESS_WITH_SIZE.
3218 :
3219 : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3220 : the internal function .ACCESS_WITH_SIZE.
3221 :
3222 : REF
3223 :
3224 : to:
3225 :
3226 : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3227 : TYPE_SIZE_UNIT for element)
3228 :
3229 : NOTE: The return type of this function is the POINTER type pointing
3230 : to the original flexible array type. Then the type of the INDIRECT_REF
3231 : is the original flexible array type.
3232 : The type of the first argument of this function is a POINTER type
3233 : to the original flexible array type.
3234 :
3235 : B: For pointers with counted_by, generate a call to the internal function
3236 : .ACCESS_WITH_SIZE.
3237 :
3238 : REF
3239 :
3240 : to:
3241 :
3242 : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3243 : TYPE_SIZE_UNIT for element)
3244 :
3245 : NOTE: The return type of this function is the original pointer type.
3246 : The type of the first argument of this function is the original
3247 : pointer type.
3248 :
3249 : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3250 : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3251 :
3252 : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3253 : of the array.
3254 :
3255 : */
3256 : static tree
3257 632 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3258 : tree counted_by_ref,
3259 : tree counted_by_type)
3260 : {
3261 632 : gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))
3262 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3263 :
3264 632 : bool is_fam = c_flexible_array_member_type_p (TREE_TYPE (ref));
3265 :
3266 : /* The result type of the call is a pointer to the flexible array type;
3267 : or is the original ponter type to the pointer field with counted_by. */
3268 632 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3269 431 : : TREE_TYPE (ref);
3270 :
3271 632 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3272 632 : tree element_size = VOID_TYPE_P (element_type)
3273 632 : ? build_one_cst (size_type_node)
3274 624 : : TYPE_SIZE_UNIT (element_type);
3275 :
3276 632 : tree first_param = is_fam
3277 632 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3278 : false, NULL)
3279 431 : : c_fully_fold (ref, false, NULL);
3280 632 : tree second_param
3281 632 : = c_fully_fold (counted_by_ref, false, NULL);
3282 632 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3283 :
3284 632 : tree call
3285 632 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3286 : result_type, 4,
3287 : first_param,
3288 : second_param,
3289 : third_param,
3290 : element_size);
3291 :
3292 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3293 632 : if (is_fam)
3294 201 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3295 632 : SET_EXPR_LOCATION (call, loc);
3296 632 : return call;
3297 : }
3298 :
3299 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3300 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3301 : function .ACCESS_WITH_SIZE.
3302 : Otherwise, return the ref itself. */
3303 :
3304 : tree
3305 2261615 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3306 : {
3307 2261615 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3308 2261615 : tree datum = TREE_OPERAND (ref, 0);
3309 2261615 : tree subdatum = TREE_OPERAND (ref, 1);
3310 2261615 : tree counted_by_type = NULL_TREE;
3311 :
3312 2261615 : if (!(c_flexible_array_member_type_p (TREE_TYPE (ref))
3313 2191161 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3314 : return ref;
3315 :
3316 499848 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3317 : &counted_by_type);
3318 499848 : if (counted_by_ref)
3319 632 : ref = build_access_with_size_for_counted_by (loc, ref,
3320 : counted_by_ref,
3321 : counted_by_type);
3322 : return ref;
3323 : }
3324 :
3325 : /* Make an expression to refer to the COMPONENT field of structure or
3326 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3327 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3328 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3329 : it is from -> operator.
3330 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3331 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3332 :
3333 : tree
3334 2268458 : build_component_ref (location_t loc, tree datum, tree component,
3335 : location_t component_loc, location_t arrow_loc)
3336 : {
3337 2268458 : tree type = TREE_TYPE (datum);
3338 2268458 : enum tree_code code = TREE_CODE (type);
3339 2268458 : tree field = NULL;
3340 2268458 : tree ref;
3341 2268458 : bool datum_lvalue = lvalue_p (datum);
3342 :
3343 2268458 : if (!objc_is_public (datum, component))
3344 0 : return error_mark_node;
3345 :
3346 : /* Detect Objective-C property syntax object.property. */
3347 2268458 : if (c_dialect_objc ()
3348 2268458 : && (ref = objc_maybe_build_component_ref (datum, component)))
3349 : return ref;
3350 :
3351 : /* See if there is a field or component with name COMPONENT. */
3352 :
3353 2268458 : if (code == RECORD_TYPE || code == UNION_TYPE)
3354 : {
3355 2268386 : if (!COMPLETE_TYPE_P (type))
3356 : {
3357 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3358 19 : return error_mark_node;
3359 : }
3360 :
3361 2268367 : field = lookup_field (type, component);
3362 :
3363 2268367 : if (!field)
3364 : {
3365 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3366 63 : if (guessed_id)
3367 : {
3368 : /* Attempt to provide a fixit replacement hint, if
3369 : we have a valid range for the component. */
3370 0 : location_t reported_loc
3371 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3372 26 : gcc_rich_location rich_loc (reported_loc);
3373 26 : if (component_loc != UNKNOWN_LOCATION)
3374 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3375 26 : error_at (&rich_loc,
3376 : "%qT has no member named %qE; did you mean %qE?",
3377 : type, component, guessed_id);
3378 26 : }
3379 : else
3380 37 : error_at (loc, "%qT has no member named %qE", type, component);
3381 63 : return error_mark_node;
3382 : }
3383 :
3384 : /* Accessing elements of atomic structures or unions is undefined
3385 : behavior (C11 6.5.2.3#5). */
3386 2268304 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3387 : {
3388 18 : if (code == RECORD_TYPE)
3389 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3390 : "structure %qE", component, datum);
3391 : else
3392 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3393 : "union %qE", component, datum);
3394 : }
3395 :
3396 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3397 : This might be better solved in future the way the C++ front
3398 : end does it - by giving the anonymous entities each a
3399 : separate name and type, and then have build_component_ref
3400 : recursively call itself. We can't do that here. */
3401 2270568 : do
3402 : {
3403 2270568 : tree subdatum = TREE_VALUE (field);
3404 2270568 : int quals;
3405 2270568 : tree subtype;
3406 2270568 : bool use_datum_quals;
3407 :
3408 2270568 : if (TREE_TYPE (subdatum) == error_mark_node)
3409 : return error_mark_node;
3410 :
3411 : /* If this is an rvalue, it does not have qualifiers in C
3412 : standard terms and we must avoid propagating such
3413 : qualifiers down to a non-lvalue array that is then
3414 : converted to a pointer. */
3415 4541136 : use_datum_quals = (datum_lvalue
3416 2270568 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3417 :
3418 2270568 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3419 2270568 : if (use_datum_quals)
3420 2270306 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3421 2270568 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3422 :
3423 2270568 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3424 : NULL_TREE);
3425 2270568 : SET_EXPR_LOCATION (ref, loc);
3426 :
3427 2270568 : check_counted_by_attribute (loc, ref);
3428 :
3429 2270568 : if (TREE_READONLY (subdatum)
3430 2270568 : || (use_datum_quals && TREE_READONLY (datum)))
3431 30592 : TREE_READONLY (ref) = 1;
3432 2270568 : if (TREE_THIS_VOLATILE (subdatum)
3433 2269574 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3434 2428 : TREE_THIS_VOLATILE (ref) = 1;
3435 :
3436 2270568 : if (TREE_UNAVAILABLE (subdatum))
3437 10 : error_unavailable_use (subdatum, NULL_TREE);
3438 2270558 : else if (TREE_DEPRECATED (subdatum))
3439 16 : warn_deprecated_use (subdatum, NULL_TREE);
3440 :
3441 2270568 : datum = ref;
3442 :
3443 2270568 : field = TREE_CHAIN (field);
3444 : }
3445 2270568 : while (field);
3446 :
3447 : return ref;
3448 : }
3449 72 : else if (should_suggest_deref_p (type))
3450 : {
3451 : /* Special-case the error message for "ptr.field" for the case
3452 : where the user has confused "." vs "->". */
3453 7 : rich_location richloc (line_table, loc);
3454 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3455 : {
3456 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3457 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3458 2 : error_at (&richloc,
3459 : "%qE is a pointer to pointer; did you mean to dereference "
3460 : "it before applying %<->%> to it?",
3461 2 : TREE_OPERAND (datum, 0));
3462 : }
3463 : else
3464 : {
3465 : /* "loc" should be the "." token. */
3466 5 : richloc.add_fixit_replace ("->");
3467 5 : error_at (&richloc,
3468 : "%qE is a pointer; did you mean to use %<->%>?",
3469 : datum);
3470 : }
3471 7 : return error_mark_node;
3472 7 : }
3473 65 : else if (code != ERROR_MARK)
3474 1 : error_at (loc,
3475 : "request for member %qE in something not a structure or union",
3476 : component);
3477 :
3478 65 : return error_mark_node;
3479 : }
3480 :
3481 : /* Given an expression PTR for a pointer, return an expression
3482 : for the value pointed to.
3483 : ERRORSTRING is the name of the operator to appear in error messages.
3484 :
3485 : LOC is the location to use for the generated tree. */
3486 :
3487 : tree
3488 2747235 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3489 : {
3490 2747235 : tree pointer = default_conversion (ptr);
3491 2747235 : tree type = TREE_TYPE (pointer);
3492 2747235 : tree ref;
3493 :
3494 2747235 : if (TREE_CODE (type) == POINTER_TYPE)
3495 : {
3496 2746882 : if (CONVERT_EXPR_P (pointer)
3497 1966373 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3498 : {
3499 : /* If a warning is issued, mark it to avoid duplicates from
3500 : the backend. This only needs to be done at
3501 : warn_strict_aliasing > 2. */
3502 780509 : if (warn_strict_aliasing > 2)
3503 334865 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3504 334865 : type, TREE_OPERAND (pointer, 0)))
3505 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3506 : }
3507 :
3508 2746882 : if (TREE_CODE (pointer) == ADDR_EXPR
3509 2746882 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3510 67430 : == TREE_TYPE (type)))
3511 : {
3512 67421 : ref = TREE_OPERAND (pointer, 0);
3513 67421 : protected_set_expr_location (ref, loc);
3514 67421 : return ref;
3515 : }
3516 : else
3517 : {
3518 2679461 : tree t = TREE_TYPE (type);
3519 :
3520 2679461 : ref = build1 (INDIRECT_REF, t, pointer);
3521 :
3522 2679461 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3523 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3524 :
3525 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3526 : so that we get the proper error message if the result is used
3527 : to assign to. Also, &* is supposed to be a no-op.
3528 : And ANSI C seems to specify that the type of the result
3529 : should be the const type. */
3530 : /* A de-reference of a pointer to const is not a const. It is valid
3531 : to change it via some other pointer. */
3532 2679461 : TREE_READONLY (ref) = TYPE_READONLY (t);
3533 2679461 : TREE_SIDE_EFFECTS (ref)
3534 2679461 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3535 2679461 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3536 2679461 : protected_set_expr_location (ref, loc);
3537 2679461 : return ref;
3538 : }
3539 : }
3540 353 : else if (TREE_CODE (pointer) != ERROR_MARK)
3541 278 : invalid_indirection_error (loc, type, errstring);
3542 :
3543 353 : return error_mark_node;
3544 : }
3545 :
3546 : /* This handles expressions of the form "a[i]", which denotes
3547 : an array reference.
3548 :
3549 : This is logically equivalent in C to *(a+i), but we may do it differently.
3550 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3551 : This avoids forcing the array out of registers, and can work on
3552 : arrays that are not lvalues (for example, members of structures returned
3553 : by functions).
3554 :
3555 : For vector types, allow vector[i] but not i[vector], and create
3556 : *(((type*)&vectortype) + i) for the expression.
3557 :
3558 : LOC is the location to use for the returned expression. */
3559 :
3560 : tree
3561 3468639 : build_array_ref (location_t loc, tree array, tree index)
3562 : {
3563 3468639 : tree ret;
3564 3468639 : bool swapped = false;
3565 3468639 : if (TREE_TYPE (array) == error_mark_node
3566 3468639 : || TREE_TYPE (index) == error_mark_node)
3567 : return error_mark_node;
3568 :
3569 3468531 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3570 1994560 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3571 : /* Allow vector[index] but not index[vector]. */
3572 4491442 : && !gnu_vector_type_p (TREE_TYPE (array)))
3573 : {
3574 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3575 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3576 : {
3577 2 : error_at (loc,
3578 : "subscripted value is neither array nor pointer nor vector");
3579 :
3580 2 : return error_mark_node;
3581 : }
3582 192 : std::swap (array, index);
3583 192 : swapped = true;
3584 : }
3585 :
3586 3468529 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3587 : {
3588 2 : error_at (loc, "array subscript is not an integer");
3589 2 : return error_mark_node;
3590 : }
3591 :
3592 3468527 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3593 : {
3594 10 : error_at (loc, "subscripted value is pointer to function");
3595 10 : return error_mark_node;
3596 : }
3597 :
3598 : /* ??? Existing practice has been to warn only when the char
3599 : index is syntactically the index, not for char[array]. */
3600 3468517 : if (!swapped)
3601 3468331 : warn_array_subscript_with_type_char (loc, index);
3602 :
3603 : /* Apply default promotions *after* noticing character types. */
3604 3468517 : index = default_conversion (index);
3605 3468517 : if (index == error_mark_node)
3606 : return error_mark_node;
3607 :
3608 3468516 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3609 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3610 :
3611 3468516 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3612 3468516 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3613 :
3614 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3615 3468516 : if (TREE_CODE (array) == COMPONENT_REF
3616 3468516 : && handle_counted_by_p (array))
3617 815878 : array = handle_counted_by_for_component_ref (loc, array);
3618 :
3619 3468516 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3620 : {
3621 2496695 : tree rval, type;
3622 :
3623 : /* An array that is indexed by a non-constant
3624 : cannot be stored in a register; we must be able to do
3625 : address arithmetic on its address.
3626 : Likewise an array of elements of variable size. */
3627 2496695 : if (TREE_CODE (index) != INTEGER_CST
3628 2496695 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3629 2023222 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3630 : {
3631 474213 : if (!c_mark_addressable (array, true, true))
3632 1 : return error_mark_node;
3633 : }
3634 : /* An array that is indexed by a constant value which is not within
3635 : the array bounds cannot be stored in a register either; because we
3636 : would get a crash in store_bit_field/extract_bit_field when trying
3637 : to access a non-existent part of the register. */
3638 2496694 : if (TREE_CODE (index) == INTEGER_CST
3639 2023222 : && TYPE_DOMAIN (TREE_TYPE (array))
3640 4516557 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3641 : {
3642 60650 : if (!c_mark_addressable (array, false, true))
3643 0 : return error_mark_node;
3644 : /* ISO C2Y disallows a negative integer constant expression index
3645 : when array subscripting has an operand of array type. */
3646 60650 : if (flag_isoc2y
3647 26 : && !TREE_OVERFLOW (index)
3648 60673 : && tree_int_cst_sgn (index) < 0)
3649 10 : error_at (loc, "array subscript is negative");
3650 : }
3651 :
3652 2496694 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3653 2473341 : && ! was_vector)
3654 : {
3655 1473257 : tree foo = array;
3656 2308310 : while (TREE_CODE (foo) == COMPONENT_REF)
3657 835053 : foo = TREE_OPERAND (foo, 0);
3658 974088 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3659 2447279 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3660 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3661 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3662 : "ISO C forbids subscripting %<register%> array "
3663 : "before C2Y");
3664 1473179 : else if (!lvalue_p (foo))
3665 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3666 : "ISO C90 forbids subscripting non-lvalue "
3667 : "array");
3668 : }
3669 :
3670 2496694 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3671 2496694 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3672 3 : index = fold_convert (sizetype, index);
3673 :
3674 2496694 : type = TREE_TYPE (TREE_TYPE (array));
3675 2496694 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3676 : /* Array ref is const/volatile if the array elements are
3677 : or if the array is. */
3678 7490082 : TREE_READONLY (rval)
3679 2496694 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3680 2496694 : | TREE_READONLY (array));
3681 7490082 : TREE_SIDE_EFFECTS (rval)
3682 2496694 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3683 2496694 : | TREE_SIDE_EFFECTS (array));
3684 4993388 : TREE_THIS_VOLATILE (rval)
3685 2496694 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3686 : /* This was added by rms on 16 Nov 91.
3687 : It fixes vol struct foo *a; a->elts[1]
3688 : in an inline function.
3689 : Hope it doesn't break something else. */
3690 2496694 : | TREE_THIS_VOLATILE (array));
3691 2496694 : ret = require_complete_type (loc, rval);
3692 2496694 : protected_set_expr_location (ret, loc);
3693 2496694 : if (non_lvalue)
3694 105166 : ret = non_lvalue_loc (loc, ret);
3695 2496694 : return ret;
3696 : }
3697 : else
3698 : {
3699 971821 : tree ar = default_conversion (array);
3700 :
3701 971821 : if (ar == error_mark_node)
3702 : return ar;
3703 :
3704 971821 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3705 971821 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3706 :
3707 971821 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3708 : index, false),
3709 : RO_ARRAY_INDEXING);
3710 971821 : if (non_lvalue)
3711 0 : ret = non_lvalue_loc (loc, ret);
3712 971821 : return ret;
3713 : }
3714 : }
3715 :
3716 : /* Build an OpenMP array section reference, creating an exact type for the
3717 : resulting expression based on the element type and bounds if possible. If
3718 : we have variable bounds, create an incomplete array type for the result
3719 : instead. */
3720 :
3721 : tree
3722 3950 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3723 : {
3724 3950 : tree type = TREE_TYPE (array);
3725 3950 : gcc_assert (type);
3726 :
3727 3950 : tree sectype, eltype = TREE_TYPE (type);
3728 :
3729 : /* It's not an array or pointer type. Just reuse the type of the original
3730 : expression as the type of the array section (an error will be raised
3731 : anyway, later). */
3732 3950 : if (eltype == NULL_TREE || error_operand_p (eltype))
3733 12 : sectype = TREE_TYPE (array);
3734 : else
3735 : {
3736 3938 : tree idxtype = NULL_TREE;
3737 :
3738 3938 : if (index != NULL_TREE
3739 3938 : && length != NULL_TREE
3740 3011 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3741 6944 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3742 : {
3743 2999 : tree low = fold_convert (sizetype, index);
3744 2999 : tree high = fold_convert (sizetype, length);
3745 2999 : high = size_binop (PLUS_EXPR, low, high);
3746 2999 : high = size_binop (MINUS_EXPR, high, size_one_node);
3747 2999 : idxtype = build_range_type (sizetype, low, high);
3748 : }
3749 135 : else if ((index == NULL_TREE || integer_zerop (index))
3750 829 : && length != NULL_TREE
3751 1616 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3752 660 : idxtype = build_index_type (length);
3753 :
3754 3938 : gcc_assert (!error_operand_p (idxtype));
3755 :
3756 3938 : sectype = c_build_array_type (eltype, idxtype);
3757 : }
3758 :
3759 3950 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3760 : }
3761 :
3762 :
3763 : /* Record that REF is used. This is relevant for undeclared static
3764 : function and declarations referenced from a non-local context.
3765 : ADDRESS indicates whether the address is taken. */
3766 :
3767 : void
3768 473048212 : mark_decl_used (tree ref, bool address)
3769 : {
3770 473048212 : if (!ref || !DECL_P (ref) || in_alignof)
3771 : return;
3772 :
3773 : /* Non-file-scope and non-local reference in nested function. */
3774 348203282 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3775 122073714 : && DECL_CONTEXT (current_function_decl)
3776 473014456 : && DECL_CONTEXT (ref) != current_function_decl;
3777 :
3778 : /* An undeclared static function. */
3779 473004405 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3780 101923550 : && DECL_INITIAL (ref) == NULL_TREE
3781 69719902 : && DECL_EXTERNAL (ref)
3782 542721709 : && !TREE_PUBLIC (ref);
3783 :
3784 472870789 : if (!static_p && !nonloc_p)
3785 : return;
3786 :
3787 : /* If we may be in an unevaluated context, delay the decision. */
3788 136222 : if (in_sizeof || in_typeof || in_countof || in_generic)
3789 270 : return record_maybe_used_decl (ref, address);
3790 :
3791 135952 : if (static_p)
3792 133360 : C_DECL_USED (ref) = 1;
3793 :
3794 135952 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3795 : || TREE_CODE (ref) == PARM_DECL))
3796 2048 : DECL_NONLOCAL (ref) = 1;
3797 :
3798 : /* Nothing to do anymore. */
3799 2592 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3800 134761 : return;
3801 :
3802 : /* Filter out the cases where referencing a non-local variable does not
3803 : require a non-local context passed via the static chain. */
3804 1191 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3805 1135 : switch (TREE_CODE (ref))
3806 : {
3807 215 : case FUNCTION_DECL:
3808 : /* Use of another local function that requires no context is ok. */
3809 215 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3810 : return;
3811 : break;
3812 438 : case VAR_DECL:
3813 : /* Static variables and constexpr are ok, but for the later only
3814 : when the address is not taken. */
3815 438 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3816 : return;
3817 : break;
3818 : case TYPE_DECL:
3819 : /* A typedef is ok when not for a variably-modified type. */
3820 : return;
3821 : case CONST_DECL:
3822 : /* An enumeration constant is ok. */
3823 : return;
3824 : case PARM_DECL:
3825 : break;
3826 : case LABEL_DECL:
3827 : break;
3828 0 : default:
3829 0 : gcc_unreachable ();
3830 : }
3831 :
3832 : /* Mark all parent functions up to the nesting level of the variable as
3833 : as needing the non-local context. */
3834 1987 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3835 : {
3836 1987 : if (cont == DECL_CONTEXT (ref))
3837 : break;
3838 :
3839 : /* There should not be any other type of context used for function
3840 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3841 999 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3842 :
3843 999 : if (TREE_CODE (cont) == FUNCTION_DECL)
3844 999 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3845 : }
3846 : }
3847 :
3848 :
3849 : /* Build an external reference to identifier ID. FUN indicates
3850 : whether this will be used for a function call. LOC is the source
3851 : location of the identifier. This sets *TYPE to the type of the
3852 : identifier, which is not the same as the type of the returned value
3853 : for CONST_DECLs defined as enum constants. If the type of the
3854 : identifier is not available, *TYPE is set to NULL. */
3855 : tree
3856 174957283 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3857 : {
3858 174957283 : tree ref;
3859 174957283 : tree decl = lookup_name (id);
3860 :
3861 : /* In Objective-C, an instance variable (ivar) may be preferred to
3862 : whatever lookup_name() found. */
3863 174957283 : decl = objc_lookup_ivar (decl, id);
3864 :
3865 174957283 : *type = NULL;
3866 174957283 : if (decl && decl != error_mark_node)
3867 : {
3868 174951074 : ref = decl;
3869 174951074 : *type = TREE_TYPE (ref);
3870 174951074 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3871 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3872 : decl);
3873 : }
3874 6209 : else if (fun)
3875 : /* Implicit function declaration. */
3876 4729 : ref = implicitly_declare (loc, id);
3877 1480 : else if (decl == error_mark_node)
3878 : /* Don't complain about something that's already been
3879 : complained about. */
3880 : return error_mark_node;
3881 : else
3882 : {
3883 1246 : undeclared_variable (loc, id);
3884 1246 : return error_mark_node;
3885 : }
3886 :
3887 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3888 : unmappable types if we return the decl with an error_mark_node type,
3889 : rather than returning error_mark_node for the decl itself. */
3890 174955803 : if (TREE_TYPE (ref) == error_mark_node
3891 174955803 : && !c_omp_array_section_p)
3892 : return error_mark_node;
3893 :
3894 174955786 : if (TREE_UNAVAILABLE (ref))
3895 14 : error_unavailable_use (ref, NULL_TREE);
3896 174955772 : else if (TREE_DEPRECATED (ref))
3897 100 : warn_deprecated_use (ref, NULL_TREE);
3898 :
3899 : /* Recursive call does not count as usage. */
3900 174955786 : if (ref != current_function_decl)
3901 : {
3902 174953143 : TREE_USED (ref) = 1;
3903 : }
3904 :
3905 174955786 : mark_decl_used (ref, false);
3906 :
3907 174955786 : if (TREE_CODE (ref) == CONST_DECL)
3908 : {
3909 417846 : used_types_insert (TREE_TYPE (ref));
3910 :
3911 417846 : if (warn_cxx_compat
3912 377 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3913 418221 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3914 : {
3915 1 : warning_at (loc, OPT_Wc___compat,
3916 : "enum constant defined in struct or union "
3917 : "is not visible in C++");
3918 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3919 : }
3920 :
3921 417846 : ref = DECL_INITIAL (ref);
3922 417846 : TREE_CONSTANT (ref) = 1;
3923 : }
3924 : /* C99 6.7.4p3: An inline definition of a function with external
3925 : linkage ... shall not contain a reference to an identifier with
3926 : internal linkage. */
3927 174537940 : else if (current_function_decl != NULL_TREE
3928 173026698 : && DECL_DECLARED_INLINE_P (current_function_decl)
3929 159394744 : && DECL_EXTERNAL (current_function_decl)
3930 158620528 : && VAR_OR_FUNCTION_DECL_P (ref)
3931 56526234 : && (!VAR_P (ref) || TREE_STATIC (ref))
3932 47376496 : && ! TREE_PUBLIC (ref)
3933 174537974 : && DECL_CONTEXT (ref) != current_function_decl)
3934 25 : record_inline_static (loc, current_function_decl, ref,
3935 : csi_internal);
3936 :
3937 : return ref;
3938 : }
3939 :
3940 : static struct maybe_used_decl *maybe_used_decls;
3941 :
3942 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3943 : _Generic, might be used if the operand of sizeof is a VLA type or the
3944 : operand of typeof is a variably modified type or the operand of _Countof has
3945 : a variable number of elements or the operand of _Generic is the one selected
3946 : as the result. */
3947 :
3948 : static void
3949 270 : record_maybe_used_decl (tree decl, bool address)
3950 : {
3951 270 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3952 270 : t->decl = decl;
3953 270 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3954 270 : t->address = address;
3955 270 : t->next = maybe_used_decls;
3956 270 : maybe_used_decls = t;
3957 270 : }
3958 :
3959 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3960 : _Generic. If USED is false, just discard them. If it is true, mark them
3961 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3962 : them to the next level up (if still inside sizeof or typeof or _Countof or
3963 : _Generic). */
3964 :
3965 : void
3966 1466836 : pop_maybe_used (bool used)
3967 : {
3968 1466836 : struct maybe_used_decl *p = maybe_used_decls;
3969 1466836 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3970 1467130 : while (p && p->level > cur_level)
3971 : {
3972 294 : if (used)
3973 : {
3974 74 : if (cur_level == 0)
3975 50 : mark_decl_used (p->decl, p->address);
3976 : else
3977 24 : p->level = cur_level;
3978 : }
3979 294 : p = p->next;
3980 : }
3981 1466836 : if (!used || cur_level == 0)
3982 1466756 : maybe_used_decls = p;
3983 1466836 : }
3984 :
3985 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3986 : _Generic, without acting on them, and return the pointer to the previous top
3987 : of the stack. This for use at the end of a default generic association when
3988 : it is not yet known whether the expression is used. If it later turns out
3989 : the expression is used (or treated as used before C23), restore_maybe_used
3990 : should be called on the return value followed by pop_maybe_used (true);
3991 : otherwise, the return value can be discarded. */
3992 :
3993 : struct maybe_used_decl *
3994 514 : save_maybe_used ()
3995 : {
3996 514 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
3997 514 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3998 546 : while (p && p->level > cur_level)
3999 32 : p = p->next;
4000 514 : maybe_used_decls = p;
4001 514 : return orig;
4002 : }
4003 :
4004 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
4005 : or _Generic returned by save_maybe_used. It is required that the stack is
4006 : at exactly the point where it was left by save_maybe_used. */
4007 :
4008 : void
4009 72 : restore_maybe_used (struct maybe_used_decl *stack)
4010 : {
4011 72 : maybe_used_decls = stack;
4012 72 : }
4013 :
4014 : /* Return the result of sizeof applied to EXPR. */
4015 :
4016 : struct c_expr
4017 312921 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4018 : {
4019 312921 : struct c_expr ret;
4020 312921 : if (expr.value == error_mark_node)
4021 : {
4022 113 : ret.value = error_mark_node;
4023 113 : ret.original_code = ERROR_MARK;
4024 113 : ret.original_type = NULL;
4025 113 : ret.m_decimal = 0;
4026 113 : pop_maybe_used (false);
4027 : }
4028 : else
4029 : {
4030 312808 : bool expr_const_operands = true;
4031 :
4032 312808 : if (TREE_CODE (expr.value) == PARM_DECL
4033 312808 : && C_ARRAY_PARAMETER (expr.value))
4034 : {
4035 55 : auto_diagnostic_group d;
4036 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4037 : "%<sizeof%> on array function parameter %qE will "
4038 : "return size of %qT", expr.value,
4039 55 : TREE_TYPE (expr.value)))
4040 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4041 55 : }
4042 312808 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4043 : &expr_const_operands);
4044 312808 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4045 312808 : c_last_sizeof_arg = expr.value;
4046 312808 : c_last_sizeof_loc = loc;
4047 312808 : ret.original_code = SIZEOF_EXPR;
4048 312808 : ret.original_type = NULL;
4049 312808 : ret.m_decimal = 0;
4050 312808 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4051 : {
4052 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4053 288 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4054 : folded_expr, ret.value);
4055 288 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4056 288 : SET_EXPR_LOCATION (ret.value, loc);
4057 : }
4058 312808 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4059 : }
4060 312921 : return ret;
4061 : }
4062 :
4063 : /* Return the result of sizeof applied to T, a structure for the type
4064 : name passed to sizeof (rather than the type itself). LOC is the
4065 : location of the original expression. */
4066 :
4067 : struct c_expr
4068 331568 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4069 : {
4070 331568 : tree type;
4071 331568 : struct c_expr ret;
4072 331568 : tree type_expr = NULL_TREE;
4073 331568 : bool type_expr_const = true;
4074 331568 : type = groktypename (t, &type_expr, &type_expr_const);
4075 331568 : ret.value = c_sizeof (loc, type);
4076 331568 : c_last_sizeof_arg = type;
4077 331568 : c_last_sizeof_loc = loc;
4078 331568 : ret.original_code = SIZEOF_EXPR;
4079 331568 : ret.original_type = NULL;
4080 331568 : ret.m_decimal = 0;
4081 331568 : if (type == error_mark_node)
4082 : {
4083 5 : ret.value = error_mark_node;
4084 5 : ret.original_code = ERROR_MARK;
4085 : }
4086 : else
4087 331521 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4088 662999 : && C_TYPE_VARIABLE_SIZE (type))
4089 : {
4090 : /* If the type is a [*] array, it is a VLA but is represented as
4091 : having a size of zero. In such a case we must ensure that
4092 : the result of sizeof does not get folded to a constant by
4093 : c_fully_fold, because if the size is evaluated the result is
4094 : not constant and so constraints on zero or negative size
4095 : arrays must not be applied when this sizeof call is inside
4096 : another array declarator. */
4097 41 : if (!type_expr)
4098 16 : type_expr = integer_zero_node;
4099 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4100 : type_expr, ret.value);
4101 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4102 : }
4103 331568 : pop_maybe_used (type != error_mark_node
4104 331568 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4105 331568 : return ret;
4106 : }
4107 :
4108 : static bool
4109 138 : is_top_array_vla (tree type)
4110 : {
4111 138 : bool zero, var;
4112 138 : tree d;
4113 :
4114 138 : if (TREE_CODE (type) != ARRAY_TYPE)
4115 : return false;
4116 120 : if (!COMPLETE_TYPE_P (type))
4117 : return false;
4118 :
4119 116 : d = TYPE_DOMAIN (type);
4120 116 : zero = !TYPE_MAX_VALUE (d);
4121 116 : if (zero)
4122 : return false;
4123 :
4124 100 : var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST
4125 100 : || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST);
4126 : return var;
4127 : }
4128 :
4129 : /* Return the result of countof applied to EXPR. */
4130 :
4131 : struct c_expr
4132 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4133 : {
4134 54 : struct c_expr ret;
4135 54 : if (expr.value == error_mark_node)
4136 : {
4137 1 : ret.value = error_mark_node;
4138 1 : ret.original_code = ERROR_MARK;
4139 1 : ret.original_type = NULL;
4140 1 : ret.m_decimal = 0;
4141 1 : pop_maybe_used (false);
4142 : }
4143 : else
4144 : {
4145 53 : bool expr_const_operands = true;
4146 :
4147 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4148 : &expr_const_operands);
4149 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4150 53 : c_last_sizeof_arg = expr.value;
4151 53 : c_last_sizeof_loc = loc;
4152 53 : ret.original_code = COUNTOF_EXPR;
4153 53 : ret.original_type = NULL;
4154 53 : ret.m_decimal = 0;
4155 53 : if (is_top_array_vla (TREE_TYPE (folded_expr)))
4156 : {
4157 : /* countof is evaluated when given a vla. */
4158 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4159 : folded_expr, ret.value);
4160 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4161 11 : SET_EXPR_LOCATION (ret.value, loc);
4162 : }
4163 53 : pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
4164 : }
4165 54 : return ret;
4166 : }
4167 :
4168 : /* Return the result of countof applied to T, a structure for the type
4169 : name passed to countof (rather than the type itself). LOC is the
4170 : location of the original expression. */
4171 :
4172 : struct c_expr
4173 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4174 : {
4175 18 : tree type;
4176 18 : struct c_expr ret;
4177 18 : tree type_expr = NULL_TREE;
4178 18 : bool type_expr_const = true;
4179 18 : type = groktypename (t, &type_expr, &type_expr_const);
4180 18 : ret.value = c_countof_type (loc, type);
4181 18 : c_last_sizeof_arg = type;
4182 18 : c_last_sizeof_loc = loc;
4183 18 : ret.original_code = COUNTOF_EXPR;
4184 18 : ret.original_type = NULL;
4185 18 : ret.m_decimal = 0;
4186 18 : if (type == error_mark_node)
4187 : {
4188 0 : ret.value = error_mark_node;
4189 0 : ret.original_code = ERROR_MARK;
4190 : }
4191 : else
4192 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4193 22 : && is_top_array_vla (type))
4194 : {
4195 : /* If the type is a [*] array, it is a VLA but is represented as
4196 : having a size of zero. In such a case we must ensure that
4197 : the result of countof does not get folded to a constant by
4198 : c_fully_fold, because if the number of elements is evaluated
4199 : the result is not constant and so
4200 : constraints on zero or negative size arrays must not be applied
4201 : when this countof call is inside another array declarator. */
4202 6 : if (!type_expr)
4203 0 : type_expr = integer_zero_node;
4204 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4205 : type_expr, ret.value);
4206 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4207 : }
4208 18 : pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false);
4209 18 : return ret;
4210 : }
4211 :
4212 : /* Return the result of maxof applied to T, a structure for the type
4213 : name passed to maxof (rather than the type itself). LOC is the
4214 : location of the original expression. */
4215 :
4216 : struct c_expr
4217 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4218 : {
4219 46 : tree type;
4220 46 : struct c_expr ret;
4221 46 : tree type_expr = NULL_TREE;
4222 46 : bool type_expr_const = true;
4223 46 : type = groktypename (t, &type_expr, &type_expr_const);
4224 46 : ret.value = c_maxof_type (loc, type);
4225 46 : c_last_sizeof_arg = type;
4226 46 : c_last_sizeof_loc = loc;
4227 46 : ret.original_code = MAXOF_EXPR;
4228 46 : ret.original_type = NULL;
4229 46 : ret.m_decimal = 0;
4230 46 : if (type == error_mark_node)
4231 : {
4232 0 : ret.value = error_mark_node;
4233 0 : ret.original_code = ERROR_MARK;
4234 : }
4235 46 : pop_maybe_used (type != error_mark_node);
4236 46 : return ret;
4237 : }
4238 :
4239 : /* Return the result of minof applied to T, a structure for the type
4240 : name passed to minof (rather than the type itself). LOC is the
4241 : location of the original expression. */
4242 :
4243 : struct c_expr
4244 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4245 : {
4246 46 : tree type;
4247 46 : struct c_expr ret;
4248 46 : tree type_expr = NULL_TREE;
4249 46 : bool type_expr_const = true;
4250 46 : type = groktypename (t, &type_expr, &type_expr_const);
4251 46 : ret.value = c_minof_type (loc, type);
4252 46 : c_last_sizeof_arg = type;
4253 46 : c_last_sizeof_loc = loc;
4254 46 : ret.original_code = MINOF_EXPR;
4255 46 : ret.original_type = NULL;
4256 46 : ret.m_decimal = 0;
4257 46 : if (type == error_mark_node)
4258 : {
4259 0 : ret.value = error_mark_node;
4260 0 : ret.original_code = ERROR_MARK;
4261 : }
4262 46 : pop_maybe_used (type != error_mark_node);
4263 46 : return ret;
4264 : }
4265 :
4266 : /* Build a function call to function FUNCTION with parameters PARAMS.
4267 : The function call is at LOC.
4268 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4269 : TREE_VALUE of each node is a parameter-expression.
4270 : FUNCTION's data type may be a function type or a pointer-to-function. */
4271 :
4272 : tree
4273 0 : build_function_call (location_t loc, tree function, tree params)
4274 : {
4275 0 : vec<tree, va_gc> *v;
4276 0 : tree ret;
4277 :
4278 0 : vec_alloc (v, list_length (params));
4279 0 : for (; params; params = TREE_CHAIN (params))
4280 0 : v->quick_push (TREE_VALUE (params));
4281 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4282 0 : vec_free (v);
4283 0 : return ret;
4284 : }
4285 :
4286 : /* Give a note about the location of the declaration of DECL,
4287 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4288 :
4289 : static void
4290 299 : inform_declaration (tree decl, tree function_expr)
4291 : {
4292 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4293 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4294 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4295 92 : else if (function_expr)
4296 92 : switch (TREE_CODE (function_expr))
4297 : {
4298 : default:
4299 : break;
4300 13 : case COMPONENT_REF:
4301 : /* Show the decl of the pertinent field (e.g. for callback
4302 : fields in a struct. */
4303 13 : {
4304 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4305 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4306 13 : inform (loc, "declared here");
4307 : }
4308 : break;
4309 : }
4310 299 : }
4311 :
4312 : /* C implementation of callback for use when checking param types. */
4313 :
4314 : static bool
4315 2 : comp_parm_types (tree wanted_type, tree actual_type)
4316 : {
4317 2 : return comptypes (wanted_type, actual_type);
4318 : }
4319 :
4320 : /* Build a function call to function FUNCTION with parameters PARAMS.
4321 : If FUNCTION is the result of resolving an overloaded target built-in,
4322 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4323 : ORIGTYPES, if not NULL, is a vector of types; each element is
4324 : either NULL or the original type of the corresponding element in
4325 : PARAMS. The original type may differ from TREE_TYPE of the
4326 : parameter for enums. FUNCTION's data type may be a function type
4327 : or pointer-to-function. This function changes the elements of
4328 : PARAMS. */
4329 :
4330 : tree
4331 49902788 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4332 : tree function, vec<tree, va_gc> *params,
4333 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4334 : {
4335 49902788 : tree fntype, fundecl = NULL_TREE;
4336 49902788 : tree name = NULL_TREE, result;
4337 49902788 : tree tem;
4338 49902788 : int nargs;
4339 49902788 : tree *argarray;
4340 :
4341 :
4342 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4343 49902788 : STRIP_TYPE_NOPS (function);
4344 :
4345 : /* Convert anything with function type to a pointer-to-function. */
4346 49902788 : if (TREE_CODE (function) == FUNCTION_DECL)
4347 : {
4348 49825356 : name = DECL_NAME (function);
4349 :
4350 49825356 : if (flag_tm)
4351 421 : tm_malloc_replacement (function);
4352 49825356 : fundecl = function;
4353 49825356 : if (!orig_fundecl)
4354 49825356 : orig_fundecl = fundecl;
4355 : /* Atomic functions have type checking/casting already done. They are
4356 : often rewritten and don't match the original parameter list. */
4357 99650712 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4358 : origtypes = NULL;
4359 : }
4360 49902788 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4361 49839841 : function = function_to_pointer_conversion (loc, function);
4362 :
4363 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4364 : expressions, like those used for ObjC messenger dispatches. */
4365 49902788 : if (params && !params->is_empty ())
4366 38185729 : function = objc_rewrite_function_call (function, (*params)[0]);
4367 :
4368 49902788 : function = c_fully_fold (function, false, NULL);
4369 :
4370 49902788 : fntype = TREE_TYPE (function);
4371 :
4372 49902788 : if (TREE_CODE (fntype) == ERROR_MARK)
4373 5 : return error_mark_node;
4374 :
4375 49902783 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4376 49902749 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4377 : {
4378 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4379 33 : error_at (loc,
4380 : "called object %qE is not a function or function pointer",
4381 : function);
4382 4 : else if (DECL_P (function))
4383 : {
4384 1 : auto_diagnostic_group d;
4385 1 : error_at (loc,
4386 : "called object %qD is not a function or function pointer",
4387 : function);
4388 1 : inform_declaration (function, NULL_TREE);
4389 1 : }
4390 : else
4391 3 : error_at (loc,
4392 : "called object is not a function or function pointer");
4393 37 : return error_mark_node;
4394 : }
4395 :
4396 49902746 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4397 374496 : current_function_returns_abnormally = 1;
4398 :
4399 : /* fntype now gets the type of function pointed to. */
4400 49902746 : fntype = TREE_TYPE (fntype);
4401 49902746 : tree return_type = TREE_TYPE (fntype);
4402 :
4403 : /* Convert the parameters to the types declared in the
4404 : function prototype, or apply default promotions. */
4405 :
4406 49902746 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4407 : origtypes, function, fundecl);
4408 49902746 : if (nargs < 0)
4409 178 : return error_mark_node;
4410 :
4411 : /* Check that the function is called through a compatible prototype.
4412 : If it is not, warn. */
4413 49901661 : if (CONVERT_EXPR_P (function)
4414 923 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4415 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4416 49902753 : && !comptypes (fntype, TREE_TYPE (tem)))
4417 : {
4418 : /* This situation leads to run-time undefined behavior. We can't,
4419 : therefore, simply error unless we can prove that all possible
4420 : executions of the program must execute the code. */
4421 20 : warning_at (loc, 0, "function called through a non-compatible type");
4422 :
4423 20 : if (VOID_TYPE_P (return_type)
4424 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4425 1 : pedwarn (loc, 0,
4426 : "function with qualified void return type called");
4427 : }
4428 :
4429 49902568 : argarray = vec_safe_address (params);
4430 :
4431 : /* Check that arguments to builtin functions match the expectations. */
4432 49902568 : if (fundecl
4433 49825193 : && fndecl_built_in_p (fundecl)
4434 83155093 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4435 : orig_fundecl, nargs, argarray))
4436 298 : return error_mark_node;
4437 :
4438 : /* Check that the arguments to the function are valid. */
4439 49902270 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4440 : nargs, argarray, &arg_loc,
4441 : comp_parm_types);
4442 :
4443 49902270 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4444 49902270 : && !VOID_TYPE_P (return_type))
4445 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4446 49902270 : if (name != NULL_TREE
4447 99727165 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4448 : {
4449 32532200 : if (require_constant_value)
4450 3487 : result
4451 3487 : = fold_build_call_array_initializer_loc (loc, return_type,
4452 : function, nargs, argarray);
4453 : else
4454 32528713 : result = fold_build_call_array_loc (loc, return_type,
4455 : function, nargs, argarray);
4456 32532200 : if (TREE_CODE (result) == NOP_EXPR
4457 32532200 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4458 26450 : STRIP_TYPE_NOPS (result);
4459 : }
4460 : else
4461 17370070 : result = build_call_array_loc (loc, return_type,
4462 : function, nargs, argarray);
4463 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4464 : later. */
4465 49902270 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4466 215 : suppress_warning (result, OPT_Wnonnull);
4467 :
4468 : /* In this improbable scenario, a nested function returns a VM type.
4469 : Create a TARGET_EXPR so that the call always has a LHS, much as
4470 : what the C++ FE does for functions returning non-PODs. */
4471 49902270 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4472 : {
4473 81 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4474 81 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4475 : NULL_TREE, NULL_TREE);
4476 : }
4477 :
4478 49902270 : if (VOID_TYPE_P (TREE_TYPE (result)))
4479 : {
4480 2390989 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4481 2 : pedwarn (loc, 0,
4482 : "function with qualified void return type called");
4483 2390989 : return result;
4484 : }
4485 47511281 : return require_complete_type (loc, result);
4486 : }
4487 :
4488 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4489 :
4490 : tree
4491 49902345 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4492 : tree function, vec<tree, va_gc> *params,
4493 : vec<tree, va_gc> *origtypes)
4494 : {
4495 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4496 49902356 : STRIP_TYPE_NOPS (function);
4497 :
4498 : /* Convert anything with function type to a pointer-to-function. */
4499 49902345 : if (TREE_CODE (function) == FUNCTION_DECL)
4500 : {
4501 : /* Implement type-directed function overloading for builtins.
4502 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4503 : handle all the type checking. The result is a complete expression
4504 : that implements this function call. */
4505 49824913 : tree tem = resolve_overloaded_builtin (loc, function, params);
4506 49824913 : if (tem)
4507 : return tem;
4508 : }
4509 49816410 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4510 : }
4511 :
4512 : /* Helper for convert_arguments called to convert the VALue of argument
4513 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4514 : PARMNUM and TYPE.
4515 : PLOC is the location where the conversion is being performed.
4516 : FUNCTION and FUNDECL are the same as in convert_arguments.
4517 : VALTYPE is the original type of VAL before the conversion and,
4518 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4519 : NPC is true if VAL represents the null pointer constant (VAL itself
4520 : will have been folded to an integer constant).
4521 : RNAME is the same as FUNCTION except in Objective C when it's
4522 : the function selector.
4523 : EXCESS_PRECISION is true when VAL was originally represented
4524 : as EXCESS_PRECISION_EXPR.
4525 : WARNOPT is the same as in convert_for_assignment. */
4526 :
4527 : static tree
4528 125628831 : convert_argument (location_t ploc, tree function, tree fundecl,
4529 : tree type, tree origtype, tree val, tree valtype,
4530 : bool npc, tree rname, int parmnum, int argnum,
4531 : bool excess_precision, int warnopt)
4532 : {
4533 : /* Formal parm type is specified by a function prototype. */
4534 :
4535 125628831 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4536 : {
4537 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4538 : parmnum + 1);
4539 3 : return error_mark_node;
4540 : }
4541 :
4542 : /* Optionally warn about conversions that differ from the default
4543 : conversions. */
4544 125628828 : if (warn_traditional_conversion || warn_traditional)
4545 : {
4546 193 : if (INTEGRAL_TYPE_P (type)
4547 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4548 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4549 : "passing argument %d of %qE as integer rather "
4550 : "than floating due to prototype",
4551 : argnum, rname);
4552 193 : if (INTEGRAL_TYPE_P (type)
4553 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4554 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4555 : "passing argument %d of %qE as integer rather "
4556 : "than complex due to prototype",
4557 : argnum, rname);
4558 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4559 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4560 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4561 : "passing argument %d of %qE as complex rather "
4562 : "than floating due to prototype",
4563 : argnum, rname);
4564 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4565 71 : && INTEGRAL_TYPE_P (valtype))
4566 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4567 : "passing argument %d of %qE as floating rather "
4568 : "than integer due to prototype",
4569 : argnum, rname);
4570 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4571 7 : && INTEGRAL_TYPE_P (valtype))
4572 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4573 : "passing argument %d of %qE as complex rather "
4574 : "than integer due to prototype",
4575 : argnum, rname);
4576 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4577 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4578 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4579 : "passing argument %d of %qE as floating rather "
4580 : "than complex due to prototype",
4581 : argnum, rname);
4582 : /* ??? At some point, messages should be written about
4583 : conversions between complex types, but that's too messy
4584 : to do now. */
4585 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4586 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4587 : {
4588 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4589 :
4590 : /* Warn if any argument is passed as `float',
4591 : since without a prototype it would be `double'. */
4592 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4593 45 : && type != dfloat32_type_node)
4594 7 : warning_at (ploc, 0,
4595 : "passing argument %d of %qE as %<float%> "
4596 : "rather than %<double%> due to prototype",
4597 : argnum, rname);
4598 :
4599 : /* Warn if mismatch between argument and prototype
4600 : for decimal float types. Warn of conversions with
4601 : binary float types and of precision narrowing due to
4602 : prototype. */
4603 38 : else if (type != valtype
4604 32 : && (type == dfloat32_type_node
4605 22 : || type == dfloat64_type_node
4606 12 : || type == dfloat128_type_node
4607 2 : || valtype == dfloat32_type_node
4608 2 : || valtype == dfloat64_type_node
4609 2 : || valtype == dfloat128_type_node)
4610 38 : && (formal_prec
4611 30 : <= TYPE_PRECISION (valtype)
4612 14 : || (type == dfloat128_type_node
4613 10 : && (valtype
4614 10 : != dfloat64_type_node
4615 8 : && (valtype
4616 : != dfloat32_type_node)))
4617 8 : || (type == dfloat64_type_node
4618 4 : && (valtype
4619 : != dfloat32_type_node))))
4620 24 : warning_at (ploc, 0,
4621 : "passing argument %d of %qE as %qT "
4622 : "rather than %qT due to prototype",
4623 : argnum, rname, type, valtype);
4624 :
4625 : }
4626 : /* Detect integer changing in width or signedness.
4627 : These warnings are only activated with
4628 : -Wtraditional-conversion, not with -Wtraditional. */
4629 105 : else if (warn_traditional_conversion
4630 105 : && INTEGRAL_TYPE_P (type)
4631 102 : && INTEGRAL_TYPE_P (valtype))
4632 : {
4633 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4634 83 : tree would_have_been = default_conversion (val);
4635 83 : tree type1 = TREE_TYPE (would_have_been);
4636 :
4637 83 : if (val == error_mark_node)
4638 : /* VAL could have been of incomplete type. */;
4639 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4640 83 : && (TYPE_MAIN_VARIANT (type)
4641 2 : == TYPE_MAIN_VARIANT (valtype)))
4642 : /* No warning if function asks for enum
4643 : and the actual arg is that enum type. */
4644 : ;
4645 81 : else if (formal_prec != TYPE_PRECISION (type1))
4646 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4647 : "passing argument %d of %qE "
4648 : "with different width due to prototype",
4649 : argnum, rname);
4650 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4651 : ;
4652 : /* Don't complain if the formal parameter type
4653 : is an enum, because we can't tell now whether
4654 : the value was an enum--even the same enum. */
4655 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4656 : ;
4657 32 : else if (TREE_CODE (val) == INTEGER_CST
4658 5 : && int_fits_type_p (val, type))
4659 : /* Change in signedness doesn't matter
4660 : if a constant value is unaffected. */
4661 : ;
4662 : /* If the value is extended from a narrower
4663 : unsigned type, it doesn't matter whether we
4664 : pass it as signed or unsigned; the value
4665 : certainly is the same either way. */
4666 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4667 32 : && TYPE_UNSIGNED (valtype))
4668 : ;
4669 32 : else if (TYPE_UNSIGNED (type))
4670 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4671 : "passing argument %d of %qE "
4672 : "as unsigned due to prototype",
4673 : argnum, rname);
4674 : else
4675 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4676 : "passing argument %d of %qE "
4677 : "as signed due to prototype",
4678 : argnum, rname);
4679 : }
4680 : }
4681 :
4682 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4683 : sake of better warnings from convert_and_check. */
4684 125628828 : if (excess_precision)
4685 370 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4686 :
4687 125628828 : tree parmval = convert_for_assignment (ploc, ploc, type,
4688 : val, origtype, ic_argpass,
4689 : npc, fundecl, function,
4690 : parmnum + 1, warnopt);
4691 125628828 : return parmval;
4692 : }
4693 :
4694 : /* Convert the argument expressions in the vector VALUES
4695 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4696 :
4697 : If the list is exhausted, or when an element has NULL as its type,
4698 : perform the default conversions.
4699 :
4700 : ORIGTYPES is the original types of the expressions in VALUES. This
4701 : holds the type of enum values which have been converted to integral
4702 : types. It may be NULL.
4703 :
4704 : FUNCTION is a tree for the called function. It is used only for
4705 : error messages, where it is formatted with %qE.
4706 :
4707 : This is also where warnings about wrong number of args are generated.
4708 :
4709 : ARG_LOC are locations of function arguments (if any).
4710 :
4711 : Returns the actual number of arguments processed (which may be less
4712 : than the length of VALUES in some error situations), or -1 on
4713 : failure. */
4714 :
4715 : static int
4716 49902746 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4717 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4718 : tree function, tree fundecl)
4719 : {
4720 49902746 : tree typelist = TYPE_ARG_TYPES (fntype);
4721 49902746 : unsigned int parmnum;
4722 49902746 : bool error_args = false;
4723 49902746 : const bool type_generic = fundecl
4724 49902746 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4725 49902746 : bool type_generic_remove_excess_precision = false;
4726 49902746 : bool type_generic_overflow_p = false;
4727 49902746 : bool type_generic_bit_query = false;
4728 49902746 : tree selector;
4729 :
4730 : /* Change pointer to function to the function itself for
4731 : diagnostics. */
4732 49902746 : if (TREE_CODE (function) == ADDR_EXPR
4733 49902746 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4734 49827454 : function = TREE_OPERAND (function, 0);
4735 :
4736 : /* Handle an ObjC selector specially for diagnostics. */
4737 49902746 : selector = objc_message_selector ();
4738 :
4739 : /* For a call to a built-in function declared without a prototype,
4740 : set to the built-in function's argument list. */
4741 49902746 : tree builtin_typelist = NULL_TREE;
4742 :
4743 : /* For type-generic built-in functions, determine whether excess
4744 : precision should be removed (classification) or not
4745 : (comparison). */
4746 49902746 : if (fundecl
4747 49902746 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4748 : {
4749 1397007 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4750 1397007 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4751 : {
4752 : /* For a call to a built-in function declared without a prototype
4753 : use the types of the parameters of the internal built-in to
4754 : match those of the arguments to. */
4755 689923 : if (tree bdecl = builtin_decl_explicit (code))
4756 689923 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4757 : }
4758 :
4759 : /* For type-generic built-in functions, determine whether excess
4760 : precision should be removed (classification) or not
4761 : (comparison). */
4762 1397007 : if (type_generic)
4763 60879 : switch (code)
4764 : {
4765 5380 : case BUILT_IN_ISFINITE:
4766 5380 : case BUILT_IN_ISINF:
4767 5380 : case BUILT_IN_ISINF_SIGN:
4768 5380 : case BUILT_IN_ISNAN:
4769 5380 : case BUILT_IN_ISNORMAL:
4770 5380 : case BUILT_IN_ISSIGNALING:
4771 5380 : case BUILT_IN_FPCLASSIFY:
4772 5380 : type_generic_remove_excess_precision = true;
4773 5380 : break;
4774 :
4775 21026 : case BUILT_IN_ADD_OVERFLOW_P:
4776 21026 : case BUILT_IN_SUB_OVERFLOW_P:
4777 21026 : case BUILT_IN_MUL_OVERFLOW_P:
4778 : /* The last argument of these type-generic builtins
4779 : should not be promoted. */
4780 21026 : type_generic_overflow_p = true;
4781 21026 : break;
4782 :
4783 1295 : case BUILT_IN_CLZG:
4784 1295 : case BUILT_IN_CTZG:
4785 1295 : case BUILT_IN_CLRSBG:
4786 1295 : case BUILT_IN_FFSG:
4787 1295 : case BUILT_IN_PARITYG:
4788 1295 : case BUILT_IN_POPCOUNTG:
4789 : /* The first argument of these type-generic builtins
4790 : should not be promoted. */
4791 1295 : type_generic_bit_query = true;
4792 1295 : break;
4793 :
4794 : default:
4795 : break;
4796 : }
4797 : }
4798 :
4799 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4800 : individual converted arguments. */
4801 :
4802 49902746 : tree typetail, builtin_typetail, val;
4803 49902746 : for (typetail = typelist,
4804 49902746 : builtin_typetail = builtin_typelist,
4805 49902746 : parmnum = 0;
4806 176466052 : values && values->iterate (parmnum, &val);
4807 : ++parmnum)
4808 : {
4809 : /* The type of the function parameter (if it was declared with one). */
4810 252190287 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4811 : /* The type of the built-in function parameter (if the function
4812 : is a built-in). Used to detect type incompatibilities in
4813 : calls to built-ins declared without a prototype. */
4814 126563342 : tree builtin_type = (builtin_typetail
4815 127599715 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4816 : /* The original type of the argument being passed to the function. */
4817 126563342 : tree valtype = TREE_TYPE (val);
4818 : /* The called function (or function selector in Objective C). */
4819 126563342 : tree rname = function;
4820 126563342 : int argnum = parmnum + 1;
4821 126563342 : const char *invalid_func_diag;
4822 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4823 126563342 : bool excess_precision = false;
4824 : /* The value of the argument after conversion to the type
4825 : of the function parameter it is passed to. */
4826 126563342 : tree parmval;
4827 : /* Some __atomic_* builtins have additional hidden argument at
4828 : position 0. */
4829 126563342 : location_t ploc
4830 126294533 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4831 126293933 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4832 126563342 : : input_location;
4833 :
4834 126563342 : if (type == void_type_node)
4835 : {
4836 33 : auto_diagnostic_group d;
4837 33 : int num_expected = parmnum;
4838 33 : int num_actual = values->length ();
4839 33 : gcc_rich_location rich_loc (loc);
4840 33 : if (ploc != input_location)
4841 32 : rich_loc.add_range (ploc);
4842 33 : if (selector)
4843 0 : error_at (&rich_loc,
4844 : "too many arguments to method %qE; expected %i, have %i",
4845 : selector, num_expected, num_actual);
4846 : else
4847 33 : error_at (&rich_loc,
4848 : "too many arguments to function %qE; expected %i, have %i",
4849 : function, num_expected, num_actual);
4850 33 : inform_declaration (fundecl, function);
4851 65 : return error_args ? -1 : (int) parmnum;
4852 33 : }
4853 :
4854 126563309 : if (builtin_type == void_type_node)
4855 : {
4856 12 : auto_diagnostic_group d;
4857 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4858 : "too many arguments to built-in function %qE "
4859 : "expecting %d", function, parmnum))
4860 12 : inform_declaration (fundecl, function);
4861 12 : builtin_typetail = NULL_TREE;
4862 12 : }
4863 :
4864 90405 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4865 126653595 : && !(fundecl && fndecl_built_in_p (fundecl)))
4866 : {
4867 6963 : auto_diagnostic_group d;
4868 6963 : bool warned;
4869 6963 : if (selector)
4870 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4871 : "ISO C23 does not allow arguments"
4872 : " for method %qE declared without parameters",
4873 : function);
4874 : else
4875 6963 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4876 : "ISO C23 does not allow arguments"
4877 : " for function %qE declared without parameters",
4878 : function);
4879 6963 : if (warned)
4880 4 : inform_declaration (fundecl, function);
4881 6963 : }
4882 :
4883 126563309 : if (selector && argnum > 2)
4884 : {
4885 0 : rname = selector;
4886 0 : argnum -= 2;
4887 : }
4888 :
4889 : /* Determine if VAL is a null pointer constant before folding it. */
4890 126563309 : bool npc = null_pointer_constant_p (val);
4891 :
4892 : /* If there is excess precision and a prototype, convert once to
4893 : the required type rather than converting via the semantic
4894 : type. Likewise without a prototype a float value represented
4895 : as long double should be converted once to double. But for
4896 : type-generic classification functions excess precision must
4897 : be removed here. */
4898 126563309 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4899 423 : && (type || !type_generic || !type_generic_remove_excess_precision))
4900 : {
4901 403 : val = TREE_OPERAND (val, 0);
4902 403 : excess_precision = true;
4903 : }
4904 126563309 : val = c_fully_fold (val, false, NULL);
4905 253126619 : STRIP_TYPE_NOPS (val);
4906 :
4907 126563309 : val = require_complete_type (ploc, val);
4908 :
4909 : /* Some floating-point arguments must be promoted to double when
4910 : no type is specified by a prototype. This applies to
4911 : arguments of type float, and to architecture-specific types
4912 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4913 126563309 : bool promote_float_arg = false;
4914 126563309 : if (type == NULL_TREE
4915 936397 : && TREE_CODE (valtype) == REAL_TYPE
4916 269701 : && (TYPE_PRECISION (valtype)
4917 269701 : <= TYPE_PRECISION (double_type_node))
4918 262419 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4919 131418 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4920 126694727 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4921 : {
4922 : /* Promote this argument, unless it has a _FloatN or
4923 : _FloatNx type. */
4924 1027846 : promote_float_arg = true;
4925 1027846 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4926 900438 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4927 : {
4928 : promote_float_arg = false;
4929 : break;
4930 : }
4931 : /* Don't promote __bf16 either. */
4932 130685 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4933 126432694 : promote_float_arg = false;
4934 : }
4935 :
4936 126563309 : if (type != NULL_TREE)
4937 : {
4938 125626912 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4939 125626912 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4940 : val, valtype, npc, rname, parmnum, argnum,
4941 : excess_precision, 0);
4942 : }
4943 : /* A NULLPTR type is just a nullptr always. */
4944 936397 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4945 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4946 936388 : else if (promote_float_arg)
4947 : {
4948 127338 : if (type_generic)
4949 : parmval = val;
4950 : else
4951 : {
4952 : /* Convert `float' to `double'. */
4953 124446 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4954 6 : warning_at (ploc, OPT_Wdouble_promotion,
4955 : "implicit conversion from %qT to %qT when passing "
4956 : "argument to function",
4957 : valtype, double_type_node);
4958 124446 : parmval = convert (double_type_node, val);
4959 : }
4960 : }
4961 809050 : else if ((excess_precision && !type_generic)
4962 809048 : || (type_generic_overflow_p && parmnum == 2)
4963 788026 : || (type_generic_bit_query && parmnum == 0))
4964 : /* A "double" argument with excess precision being passed
4965 : without a prototype or in variable arguments.
4966 : The last argument of __builtin_*_overflow_p should not be
4967 : promoted, similarly the first argument of
4968 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4969 22313 : parmval = convert (valtype, val);
4970 1573474 : else if ((invalid_func_diag =
4971 786737 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4972 : {
4973 0 : error (invalid_func_diag);
4974 0 : return -1;
4975 : }
4976 786737 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4977 : {
4978 : return -1;
4979 : }
4980 : else
4981 : /* Convert `short' and `char' to full-size `int'. */
4982 786734 : parmval = default_conversion (val);
4983 :
4984 126563306 : (*values)[parmnum] = parmval;
4985 126563306 : if (parmval == error_mark_node)
4986 105 : error_args = true;
4987 :
4988 126563306 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4989 : {
4990 : /* For a call to a built-in function declared without a prototype,
4991 : perform the conversions from the argument to the expected type
4992 : but issue warnings rather than errors for any mismatches.
4993 : Ignore the converted argument and use the PARMVAL obtained
4994 : above by applying default conversions instead. */
4995 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4996 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4997 : val, valtype, npc, rname, parmnum, argnum,
4998 : excess_precision,
4999 : OPT_Wbuiltin_declaration_mismatch);
5000 : }
5001 :
5002 126563306 : if (typetail)
5003 125626912 : typetail = TREE_CHAIN (typetail);
5004 :
5005 126563306 : if (builtin_typetail)
5006 1036356 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5007 : }
5008 :
5009 88088392 : gcc_assert (parmnum == vec_safe_length (values));
5010 :
5011 99475547 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5012 : {
5013 : /* Not enough args.
5014 : Determine minimum number of arguments required. */
5015 : int min_expected_num = 0;
5016 231 : bool at_least_p = false;
5017 : tree iter = typelist;
5018 386 : while (true)
5019 : {
5020 231 : if (!iter)
5021 : {
5022 : /* Variadic arguments; stop iterating. */
5023 : at_least_p = true;
5024 : break;
5025 : }
5026 222 : if (iter == void_list_node)
5027 : /* End of arguments; stop iterating. */
5028 : break;
5029 155 : ++min_expected_num;
5030 155 : iter = TREE_CHAIN (iter);
5031 : }
5032 76 : auto_diagnostic_group d;
5033 76 : int actual_num = vec_safe_length (values);
5034 143 : error_at (loc,
5035 : at_least_p
5036 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5037 : : G_("too few arguments to function %qE; expected %i, have %i"),
5038 : function, min_expected_num, actual_num);
5039 76 : inform_declaration (fundecl, function);
5040 76 : return -1;
5041 76 : }
5042 :
5043 50556364 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5044 : {
5045 : unsigned nargs = parmnum;
5046 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5047 428 : ++nargs;
5048 :
5049 188 : auto_diagnostic_group d;
5050 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5051 : "too few arguments to built-in function %qE "
5052 : "expecting %u", function, nargs - 1))
5053 173 : inform_declaration (fundecl, function);
5054 188 : }
5055 :
5056 49902634 : return error_args ? -1 : (int) parmnum;
5057 : }
5058 :
5059 : /* This is the entry point used by the parser to build unary operators
5060 : in the input. CODE, a tree_code, specifies the unary operator, and
5061 : ARG is the operand. For unary plus, the C parser currently uses
5062 : CONVERT_EXPR for code.
5063 :
5064 : LOC is the location to use for the tree generated.
5065 : */
5066 :
5067 : struct c_expr
5068 8161392 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5069 : {
5070 8161392 : struct c_expr result;
5071 :
5072 8161392 : result.original_code = code;
5073 8161392 : result.original_type = NULL;
5074 8161392 : result.m_decimal = 0;
5075 :
5076 8161392 : if (reject_gcc_builtin (arg.value))
5077 : {
5078 2 : result.value = error_mark_node;
5079 : }
5080 : else
5081 : {
5082 8161390 : result.value = build_unary_op (loc, code, arg.value, false);
5083 :
5084 8161390 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5085 5 : overflow_warning (loc, result.value, arg.value);
5086 : }
5087 :
5088 : /* We are typically called when parsing a prefix token at LOC acting on
5089 : ARG. Reflect this by updating the source range of the result to
5090 : start at LOC and end at the end of ARG. */
5091 8161392 : set_c_expr_source_range (&result,
5092 : loc, arg.get_finish ());
5093 :
5094 8161392 : return result;
5095 : }
5096 :
5097 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5098 :
5099 : bool
5100 1282317 : char_type_p (tree type)
5101 : {
5102 1282317 : return (type == char_type_node
5103 991770 : || type == unsigned_char_type_node
5104 956618 : || type == signed_char_type_node
5105 955770 : || type == char16_type_node
5106 2018714 : || type == char32_type_node);
5107 : }
5108 :
5109 : /* This is the entry point used by the parser to build binary operators
5110 : in the input. CODE, a tree_code, specifies the binary operator, and
5111 : ARG1 and ARG2 are the operands. In addition to constructing the
5112 : expression, we check for operands that were written with other binary
5113 : operators in a way that is likely to confuse the user.
5114 :
5115 : LOCATION is the location of the binary operator. */
5116 :
5117 : struct c_expr
5118 9322990 : parser_build_binary_op (location_t location, enum tree_code code,
5119 : struct c_expr arg1, struct c_expr arg2)
5120 : {
5121 9322990 : struct c_expr result;
5122 9322990 : result.m_decimal = 0;
5123 :
5124 9322990 : enum tree_code code1 = arg1.original_code;
5125 9322990 : enum tree_code code2 = arg2.original_code;
5126 9322990 : tree type1 = (arg1.original_type
5127 9322990 : ? arg1.original_type
5128 5982391 : : TREE_TYPE (arg1.value));
5129 9322990 : tree type2 = (arg2.original_type
5130 9322990 : ? arg2.original_type
5131 6877720 : : TREE_TYPE (arg2.value));
5132 :
5133 9322990 : result.value = build_binary_op (location, code,
5134 : arg1.value, arg2.value, true);
5135 9322989 : result.original_code = code;
5136 9322989 : result.original_type = NULL;
5137 9322989 : result.m_decimal = 0;
5138 :
5139 9322989 : if (TREE_CODE (result.value) == ERROR_MARK)
5140 : {
5141 1297 : set_c_expr_source_range (&result,
5142 : arg1.get_start (),
5143 : arg2.get_finish ());
5144 1297 : return result;
5145 : }
5146 :
5147 9321692 : if (location != UNKNOWN_LOCATION)
5148 9321692 : protected_set_expr_location (result.value, location);
5149 :
5150 9321692 : set_c_expr_source_range (&result,
5151 : arg1.get_start (),
5152 : arg2.get_finish ());
5153 :
5154 : /* Check for cases such as x+y<<z which users are likely
5155 : to misinterpret. */
5156 9321692 : if (warn_parentheses)
5157 2004074 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5158 : arg2.value);
5159 :
5160 9321692 : if (warn_logical_op)
5161 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5162 : code1, arg1.value, code2, arg2.value);
5163 :
5164 9321692 : if (warn_tautological_compare)
5165 : {
5166 2002587 : tree lhs = arg1.value;
5167 2002587 : tree rhs = arg2.value;
5168 2002587 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5169 : {
5170 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5171 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5172 : lhs = NULL_TREE;
5173 : else
5174 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5175 : }
5176 2002587 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5177 : {
5178 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5179 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5180 : rhs = NULL_TREE;
5181 : else
5182 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5183 : }
5184 2002587 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5185 2002587 : warn_tautological_cmp (location, code, lhs, rhs);
5186 : }
5187 :
5188 9321692 : if (warn_logical_not_paren
5189 2002670 : && TREE_CODE_CLASS (code) == tcc_comparison
5190 405910 : && code1 == TRUTH_NOT_EXPR
5191 405910 : && code2 != TRUTH_NOT_EXPR
5192 : /* Avoid warning for !!x == y. */
5193 9321818 : && (TREE_CODE (arg1.value) != NE_EXPR
5194 19 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5195 : {
5196 : /* Avoid warning for !b == y where b has _Bool type. */
5197 107 : tree t = integer_zero_node;
5198 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5199 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5200 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5201 : {
5202 90 : t = TREE_OPERAND (arg1.value, 0);
5203 199 : do
5204 : {
5205 199 : if (TREE_TYPE (t) != integer_type_node)
5206 : break;
5207 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5208 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5209 90 : else if (CONVERT_EXPR_P (t))
5210 19 : t = TREE_OPERAND (t, 0);
5211 : else
5212 : break;
5213 : }
5214 : while (1);
5215 : }
5216 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5217 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5218 : }
5219 :
5220 : /* Warn about comparisons against string literals, with the exception
5221 : of testing for equality or inequality of a string literal with NULL. */
5222 9321692 : if (code == EQ_EXPR || code == NE_EXPR)
5223 : {
5224 1165818 : if ((code1 == STRING_CST
5225 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5226 1165829 : || (code2 == STRING_CST
5227 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5228 36 : warning_at (location, OPT_Waddress,
5229 : "comparison with string literal results in unspecified behavior");
5230 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5231 1165818 : if (POINTER_TYPE_P (type1)
5232 90606 : && null_pointer_constant_p (arg2.value)
5233 1186889 : && char_type_p (type2))
5234 : {
5235 18 : auto_diagnostic_group d;
5236 18 : if (warning_at (location, OPT_Wpointer_compare,
5237 : "comparison between pointer and zero character "
5238 : "constant"))
5239 10 : inform (arg1.get_start (),
5240 : "did you mean to dereference the pointer?");
5241 18 : }
5242 1165800 : else if (POINTER_TYPE_P (type2)
5243 87909 : && null_pointer_constant_p (arg1.value)
5244 1166066 : && char_type_p (type1))
5245 : {
5246 10 : auto_diagnostic_group d;
5247 10 : if (warning_at (location, OPT_Wpointer_compare,
5248 : "comparison between pointer and zero character "
5249 : "constant"))
5250 10 : inform (arg2.get_start (),
5251 : "did you mean to dereference the pointer?");
5252 10 : }
5253 : }
5254 8155874 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5255 996443 : && (code1 == STRING_CST || code2 == STRING_CST))
5256 0 : warning_at (location, OPT_Waddress,
5257 : "comparison with string literal results in unspecified "
5258 : "behavior");
5259 :
5260 9321692 : if (warn_zero_as_null_pointer_constant
5261 24 : && c_inhibit_evaluation_warnings == 0
5262 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5263 : {
5264 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5265 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5266 9 : && INTEGRAL_TYPE_P (type2)
5267 33 : && null_pointer_constant_p (arg2.value))
5268 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5269 : "zero as null pointer constant");
5270 :
5271 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5272 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5273 11 : && INTEGRAL_TYPE_P (type1)
5274 35 : && null_pointer_constant_p (arg1.value))
5275 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5276 : "zero as null pointer constant");
5277 : }
5278 :
5279 9321692 : if (warn_array_compare
5280 2002425 : && TREE_CODE_CLASS (code) == tcc_comparison
5281 405695 : && TREE_CODE (type1) == ARRAY_TYPE
5282 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5283 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5284 :
5285 2180756 : if (TREE_OVERFLOW_P (result.value)
5286 299 : && !TREE_OVERFLOW_P (arg1.value)
5287 9321984 : && !TREE_OVERFLOW_P (arg2.value))
5288 249 : overflow_warning (location, result.value);
5289 :
5290 : /* Warn about comparisons of different enum types. */
5291 9321691 : if (warn_enum_compare
5292 2022363 : && TREE_CODE_CLASS (code) == tcc_comparison
5293 410120 : && TREE_CODE (type1) == ENUMERAL_TYPE
5294 7038 : && TREE_CODE (type2) == ENUMERAL_TYPE
5295 9328667 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5296 2 : warning_at (location, OPT_Wenum_compare,
5297 : "comparison between %qT and %qT",
5298 : type1, type2);
5299 :
5300 9321691 : if (warn_xor_used_as_pow
5301 9321691 : && code == BIT_XOR_EXPR
5302 77087 : && arg1.m_decimal
5303 730 : && arg2.m_decimal)
5304 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5305 : location,
5306 : arg2.get_location (), arg2.value);
5307 :
5308 : return result;
5309 : }
5310 :
5311 : /* Return a tree for the difference of pointers OP0 and OP1.
5312 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5313 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5314 :
5315 : static tree
5316 3641 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5317 : {
5318 3641 : tree restype = ptrdiff_type_node;
5319 3641 : tree result, inttype;
5320 :
5321 3641 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5322 3641 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5323 3641 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5324 3641 : tree orig_op0 = op0;
5325 3641 : tree orig_op1 = op1;
5326 :
5327 : /* If the operands point into different address spaces, we need to
5328 : explicitly convert them to pointers into the common address space
5329 : before we can subtract the numerical address values. */
5330 3641 : if (as0 != as1)
5331 : {
5332 0 : addr_space_t as_common;
5333 0 : tree common_type;
5334 :
5335 : /* Determine the common superset address space. This is guaranteed
5336 : to exist because the caller verified that comp_target_types
5337 : returned non-zero. */
5338 0 : if (!addr_space_superset (as0, as1, &as_common))
5339 0 : gcc_unreachable ();
5340 :
5341 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5342 0 : op0 = convert (common_type, op0);
5343 0 : op1 = convert (common_type, op1);
5344 : }
5345 :
5346 : /* Determine integer type result of the subtraction. This will usually
5347 : be the same as the result type (ptrdiff_t), but may need to be a wider
5348 : type if pointers for the address space are wider than ptrdiff_t. */
5349 3641 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5350 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5351 : else
5352 : inttype = restype;
5353 :
5354 3641 : if (VOID_TYPE_P (target_type))
5355 146 : pedwarn (loc, OPT_Wpointer_arith,
5356 : "pointer of type %<void *%> used in subtraction");
5357 3641 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5358 4 : pedwarn (loc, OPT_Wpointer_arith,
5359 : "pointer to a function used in subtraction");
5360 :
5361 3641 : if (current_function_decl != NULL_TREE
5362 3641 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5363 : {
5364 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5365 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5366 :
5367 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5368 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5369 : }
5370 :
5371 : /* First do the subtraction, then build the divide operator
5372 : and only convert at the very end.
5373 : Do not do default conversions in case restype is a short type. */
5374 :
5375 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5376 : pointers. If some platform cannot provide that, or has a larger
5377 : ptrdiff_type to support differences larger than half the address
5378 : space, cast the pointers to some larger integer type and do the
5379 : computations in that type. */
5380 3641 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5381 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5382 : convert (inttype, op1), false);
5383 : else
5384 : {
5385 : /* Cast away qualifiers. */
5386 3641 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5387 3641 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5388 3641 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5389 : }
5390 :
5391 : /* This generates an error if op1 is pointer to incomplete type. */
5392 3641 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5393 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5394 3637 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5395 3637 : TREE_TYPE (TREE_TYPE (orig_op0))))
5396 7274 : verify_type_context (loc, TCTX_POINTER_ARITH,
5397 3637 : TREE_TYPE (TREE_TYPE (orig_op1)));
5398 :
5399 3641 : op1 = c_size_in_bytes (target_type);
5400 :
5401 3641 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5402 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5403 :
5404 : /* Divide by the size, in easiest possible way. */
5405 3641 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5406 : op0, convert (inttype, op1));
5407 :
5408 : /* Convert to final result type if necessary. */
5409 3641 : return convert (restype, result);
5410 : }
5411 :
5412 : /* Expand atomic compound assignments into an appropriate sequence as
5413 : specified by the C11 standard section 6.5.16.2.
5414 :
5415 : _Atomic T1 E1
5416 : T2 E2
5417 : E1 op= E2
5418 :
5419 : This sequence is used for all types for which these operations are
5420 : supported.
5421 :
5422 : In addition, built-in versions of the 'fe' prefixed routines may
5423 : need to be invoked for floating point (real, complex or vector) when
5424 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5425 :
5426 : T1 newval;
5427 : T1 old;
5428 : T1 *addr
5429 : T2 val
5430 : fenv_t fenv
5431 :
5432 : addr = &E1;
5433 : val = (E2);
5434 : __atomic_load (addr, &old, SEQ_CST);
5435 : feholdexcept (&fenv);
5436 : loop:
5437 : newval = old op val;
5438 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5439 : SEQ_CST))
5440 : goto done;
5441 : feclearexcept (FE_ALL_EXCEPT);
5442 : goto loop:
5443 : done:
5444 : feupdateenv (&fenv);
5445 :
5446 : The compiler will issue the __atomic_fetch_* built-in when possible,
5447 : otherwise it will generate the generic form of the atomic operations.
5448 : This requires temp(s) and has their address taken. The atomic processing
5449 : is smart enough to figure out when the size of an object can utilize
5450 : a lock-free version, and convert the built-in call to the appropriate
5451 : lock-free routine. The optimizers will then dispose of any temps that
5452 : are no longer required, and lock-free implementations are utilized as
5453 : long as there is target support for the required size.
5454 :
5455 : If the operator is NOP_EXPR, then this is a simple assignment, and
5456 : an __atomic_store is issued to perform the assignment rather than
5457 : the above loop. */
5458 :
5459 : /* Build an atomic assignment at LOC, expanding into the proper
5460 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5461 : the result of the operation, unless RETURN_OLD_P, in which case
5462 : return the old value of LHS (this is only for postincrement and
5463 : postdecrement). */
5464 :
5465 : static tree
5466 30433 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5467 : tree rhs, bool return_old_p)
5468 : {
5469 30433 : tree fndecl, func_call;
5470 30433 : vec<tree, va_gc> *params;
5471 30433 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5472 30433 : tree old, old_addr;
5473 30433 : tree compound_stmt = NULL_TREE;
5474 30433 : tree stmt, goto_stmt;
5475 30433 : tree loop_label, loop_decl, done_label, done_decl;
5476 :
5477 30433 : tree lhs_type = TREE_TYPE (lhs);
5478 30433 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5479 30433 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5480 30433 : tree rhs_semantic_type = TREE_TYPE (rhs);
5481 30433 : tree nonatomic_rhs_semantic_type;
5482 30433 : tree rhs_type;
5483 :
5484 30433 : gcc_assert (TYPE_ATOMIC (lhs_type));
5485 :
5486 30433 : if (return_old_p)
5487 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5488 :
5489 : /* Allocate enough vector items for a compare_exchange. */
5490 30433 : vec_alloc (params, 6);
5491 :
5492 : /* Create a compound statement to hold the sequence of statements
5493 : with a loop. */
5494 30433 : if (modifycode != NOP_EXPR)
5495 : {
5496 19954 : compound_stmt = c_begin_compound_stmt (false);
5497 :
5498 : /* For consistency with build_modify_expr on non-_Atomic,
5499 : mark the lhs as read. Also, it would be very hard to match
5500 : such expressions in mark_exp_read. */
5501 19954 : mark_exp_read (lhs);
5502 : }
5503 :
5504 : /* Remove any excess precision (which is only present here in the
5505 : case of compound assignments). */
5506 30433 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5507 : {
5508 0 : gcc_assert (modifycode != NOP_EXPR);
5509 0 : rhs = TREE_OPERAND (rhs, 0);
5510 : }
5511 30433 : rhs_type = TREE_TYPE (rhs);
5512 :
5513 : /* Fold the RHS if it hasn't already been folded. */
5514 30433 : if (modifycode != NOP_EXPR)
5515 19954 : rhs = c_fully_fold (rhs, false, NULL);
5516 :
5517 : /* Remove the qualifiers for the rest of the expressions and create
5518 : the VAL temp variable to hold the RHS. */
5519 30433 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5520 30433 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5521 30433 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5522 : TYPE_UNQUALIFIED);
5523 30433 : val = create_tmp_var_raw (nonatomic_rhs_type);
5524 30433 : TREE_ADDRESSABLE (val) = 1;
5525 30433 : suppress_warning (val);
5526 30433 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5527 : NULL_TREE);
5528 30433 : TREE_SIDE_EFFECTS (rhs) = 1;
5529 30433 : SET_EXPR_LOCATION (rhs, loc);
5530 30433 : if (modifycode != NOP_EXPR)
5531 19954 : add_stmt (rhs);
5532 :
5533 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5534 : an atomic_store. */
5535 30433 : if (modifycode == NOP_EXPR)
5536 : {
5537 10479 : compound_stmt = rhs;
5538 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5539 10479 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5540 10479 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5541 10479 : params->quick_push (lhs_addr);
5542 10479 : params->quick_push (rhs);
5543 10479 : params->quick_push (seq_cst);
5544 10479 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5545 :
5546 10479 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5547 : compound_stmt, func_call);
5548 :
5549 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5550 : the statement and that value. */
5551 10479 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5552 : }
5553 :
5554 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5555 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5556 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5557 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5558 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5559 : {
5560 11713 : built_in_function fncode;
5561 11713 : switch (modifycode)
5562 : {
5563 3075 : case PLUS_EXPR:
5564 3075 : case POINTER_PLUS_EXPR:
5565 2124 : fncode = (return_old_p
5566 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5567 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5568 : break;
5569 2889 : case MINUS_EXPR:
5570 2094 : fncode = (return_old_p
5571 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5572 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5573 : break;
5574 609 : case BIT_AND_EXPR:
5575 609 : fncode = (return_old_p
5576 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5577 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5578 : break;
5579 609 : case BIT_IOR_EXPR:
5580 609 : fncode = (return_old_p
5581 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5582 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5583 : break;
5584 609 : case BIT_XOR_EXPR:
5585 609 : fncode = (return_old_p
5586 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5587 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5588 : break;
5589 3922 : default:
5590 3922 : goto cas_loop;
5591 : }
5592 :
5593 : /* We can only use "_1" through "_16" variants of the atomic fetch
5594 : built-ins. */
5595 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5596 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5597 0 : goto cas_loop;
5598 :
5599 : /* If this is a pointer type, we need to multiply by the size of
5600 : the pointer target type. */
5601 7791 : if (POINTER_TYPE_P (lhs_type))
5602 : {
5603 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5604 : /* ??? This would introduce -Wdiscarded-qualifiers
5605 : warning: __atomic_fetch_* expect volatile void *
5606 : type as the first argument. (Assignments between
5607 : atomic and non-atomic objects are OK.) */
5608 1018 : || TYPE_RESTRICT (lhs_type))
5609 17 : goto cas_loop;
5610 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5611 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5612 : convert (ptrdiff_type_node, rhs),
5613 : convert (ptrdiff_type_node, sz));
5614 : }
5615 :
5616 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5617 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5618 7774 : fndecl = builtin_decl_explicit (fncode);
5619 7774 : params->quick_push (lhs_addr);
5620 7774 : params->quick_push (rhs);
5621 7774 : params->quick_push (seq_cst);
5622 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5623 :
5624 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5625 7774 : TREE_ADDRESSABLE (newval) = 1;
5626 7774 : suppress_warning (newval);
5627 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5628 : NULL_TREE, NULL_TREE);
5629 7774 : SET_EXPR_LOCATION (rhs, loc);
5630 7774 : add_stmt (rhs);
5631 :
5632 : /* Finish the compound statement. */
5633 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5634 :
5635 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5636 : the statement and that value. */
5637 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5638 : }
5639 :
5640 8241 : cas_loop:
5641 : /* Create the variables and labels required for the op= form. */
5642 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5643 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5644 12180 : TREE_ADDRESSABLE (old) = 1;
5645 12180 : suppress_warning (old);
5646 :
5647 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5648 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5649 12180 : TREE_ADDRESSABLE (newval) = 1;
5650 12180 : suppress_warning (newval);
5651 :
5652 12180 : loop_decl = create_artificial_label (loc);
5653 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5654 :
5655 12180 : done_decl = create_artificial_label (loc);
5656 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5657 :
5658 : /* __atomic_load (addr, &old, SEQ_CST). */
5659 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5660 12180 : params->quick_push (lhs_addr);
5661 12180 : params->quick_push (old_addr);
5662 12180 : params->quick_push (seq_cst);
5663 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5664 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5665 : NULL_TREE);
5666 12180 : add_stmt (old);
5667 12180 : params->truncate (0);
5668 :
5669 : /* Create the expressions for floating-point environment
5670 : manipulation, if required. */
5671 12180 : bool need_fenv = (flag_trapping_math
5672 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5673 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5674 12180 : if (need_fenv)
5675 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5676 :
5677 12180 : if (hold_call)
5678 7054 : add_stmt (hold_call);
5679 :
5680 : /* loop: */
5681 12180 : add_stmt (loop_label);
5682 :
5683 : /* newval = old + val; */
5684 12180 : if (rhs_type != rhs_semantic_type)
5685 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5686 12180 : rhs = build_binary_op (loc, modifycode,
5687 : convert_lvalue_to_rvalue (loc, old, true, true),
5688 : val, true);
5689 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5690 : {
5691 0 : tree eptype = TREE_TYPE (rhs);
5692 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5693 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5694 : }
5695 : else
5696 12180 : rhs = c_fully_fold (rhs, false, NULL);
5697 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5698 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5699 : NULL_TREE, 0);
5700 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5701 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5702 : subsequent ICEs in nested function processing after an error, ensure such
5703 : a TARGET_EXPR is built even after an error. */
5704 12180 : if (rhs == error_mark_node)
5705 10 : rhs = old;
5706 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5707 : NULL_TREE);
5708 12180 : SET_EXPR_LOCATION (rhs, loc);
5709 12180 : add_stmt (rhs);
5710 :
5711 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5712 : goto done; */
5713 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5714 12180 : params->quick_push (lhs_addr);
5715 12180 : params->quick_push (old_addr);
5716 12180 : params->quick_push (newval_addr);
5717 12180 : params->quick_push (integer_zero_node);
5718 12180 : params->quick_push (seq_cst);
5719 12180 : params->quick_push (seq_cst);
5720 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5721 :
5722 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5723 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5724 :
5725 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5726 12180 : SET_EXPR_LOCATION (stmt, loc);
5727 12180 : add_stmt (stmt);
5728 :
5729 12180 : if (clear_call)
5730 7054 : add_stmt (clear_call);
5731 :
5732 : /* goto loop; */
5733 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5734 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5735 12180 : add_stmt (goto_stmt);
5736 :
5737 : /* done: */
5738 12180 : add_stmt (done_label);
5739 :
5740 12180 : if (update_call)
5741 7054 : add_stmt (update_call);
5742 :
5743 : /* Finish the compound statement. */
5744 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5745 :
5746 : /* NEWVAL is the value that was successfully stored, return a
5747 : COMPOUND_EXPR of the statement and the appropriate value. */
5748 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5749 12180 : return_old_p ? old : newval);
5750 : }
5751 :
5752 : /* Construct and perhaps optimize a tree representation
5753 : for a unary operation. CODE, a tree_code, specifies the operation
5754 : and XARG is the operand.
5755 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5756 : promotions (such as from short to int).
5757 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5758 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5759 : to pointers in C99.
5760 :
5761 : LOCATION is the location of the operator. */
5762 :
5763 : tree
5764 60522363 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5765 : bool noconvert)
5766 : {
5767 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5768 60522363 : tree arg = xarg;
5769 60522363 : tree argtype = NULL_TREE;
5770 60522363 : enum tree_code typecode;
5771 60522363 : tree val;
5772 60522363 : tree ret = error_mark_node;
5773 60522363 : tree eptype = NULL_TREE;
5774 60522363 : const char *invalid_op_diag;
5775 60522363 : bool int_operands;
5776 :
5777 60522363 : int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5778 6461590 : if (int_operands)
5779 6461590 : arg = remove_c_maybe_const_expr (arg);
5780 :
5781 60522363 : if (code != ADDR_EXPR)
5782 8534392 : arg = require_complete_type (location, arg);
5783 :
5784 60522363 : typecode = TREE_CODE (TREE_TYPE (arg));
5785 60522363 : if (typecode == ERROR_MARK)
5786 74 : return error_mark_node;
5787 60522289 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5788 32189 : typecode = INTEGER_TYPE;
5789 :
5790 121044578 : if ((invalid_op_diag
5791 60522289 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5792 : {
5793 0 : error_at (location, invalid_op_diag);
5794 0 : return error_mark_node;
5795 : }
5796 :
5797 60522289 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5798 : {
5799 237 : eptype = TREE_TYPE (arg);
5800 237 : arg = TREE_OPERAND (arg, 0);
5801 : }
5802 :
5803 60522289 : switch (code)
5804 : {
5805 28755 : case CONVERT_EXPR:
5806 : /* This is used for unary plus, because a CONVERT_EXPR
5807 : is enough to prevent anybody from looking inside for
5808 : associativity, but won't generate any code. */
5809 28760 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5810 8 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5811 5 : || typecode == BITINT_TYPE
5812 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5813 : {
5814 1 : error_at (location, "wrong type argument to unary plus");
5815 1 : return error_mark_node;
5816 : }
5817 28754 : else if (!noconvert)
5818 28754 : arg = default_conversion (arg);
5819 28754 : arg = non_lvalue_loc (location, arg);
5820 28754 : break;
5821 :
5822 6698306 : case NEGATE_EXPR:
5823 7145802 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5824 484035 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5825 452768 : || typecode == BITINT_TYPE
5826 447496 : || gnu_vector_type_p (TREE_TYPE (arg))))
5827 : {
5828 1 : error_at (location, "wrong type argument to unary minus");
5829 1 : return error_mark_node;
5830 : }
5831 6698305 : else if (!noconvert)
5832 6698299 : arg = default_conversion (arg);
5833 : break;
5834 :
5835 295239 : case BIT_NOT_EXPR:
5836 : /* ~ works on integer types and non float vectors. */
5837 295239 : if (typecode == INTEGER_TYPE
5838 295239 : || typecode == BITINT_TYPE
5839 295239 : || (gnu_vector_type_p (TREE_TYPE (arg))
5840 2057 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5841 : {
5842 : tree e = arg;
5843 :
5844 : /* Warn if the expression has boolean value. */
5845 294640 : while (TREE_CODE (e) == COMPOUND_EXPR)
5846 11 : e = TREE_OPERAND (e, 1);
5847 :
5848 589244 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5849 589244 : || truth_value_p (TREE_CODE (e))))
5850 : {
5851 146 : auto_diagnostic_group d;
5852 146 : if (warning_at (location, OPT_Wbool_operation,
5853 : "%<~%> on a boolean expression"))
5854 : {
5855 12 : gcc_rich_location richloc (location);
5856 12 : richloc.add_fixit_insert_before (location, "!");
5857 12 : inform (&richloc, "did you mean to use logical not?");
5858 12 : }
5859 146 : }
5860 294629 : if (!noconvert)
5861 294626 : arg = default_conversion (arg);
5862 : }
5863 610 : else if (typecode == COMPLEX_TYPE)
5864 : {
5865 605 : code = CONJ_EXPR;
5866 605 : pedwarn (location, OPT_Wpedantic,
5867 : "ISO C does not support %<~%> for complex conjugation");
5868 605 : if (!noconvert)
5869 605 : arg = default_conversion (arg);
5870 : }
5871 : else
5872 : {
5873 5 : error_at (location, "wrong type argument to bit-complement");
5874 5 : return error_mark_node;
5875 : }
5876 : break;
5877 :
5878 3 : case ABS_EXPR:
5879 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5880 : {
5881 0 : error_at (location, "wrong type argument to abs");
5882 0 : return error_mark_node;
5883 : }
5884 3 : else if (!noconvert)
5885 0 : arg = default_conversion (arg);
5886 : break;
5887 :
5888 7 : case ABSU_EXPR:
5889 7 : if (!(typecode == INTEGER_TYPE))
5890 : {
5891 0 : error_at (location, "wrong type argument to absu");
5892 0 : return error_mark_node;
5893 : }
5894 7 : else if (!noconvert)
5895 0 : arg = default_conversion (arg);
5896 : break;
5897 :
5898 0 : case CONJ_EXPR:
5899 : /* Conjugating a real value is a no-op, but allow it anyway. */
5900 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5901 : || typecode == COMPLEX_TYPE))
5902 : {
5903 0 : error_at (location, "wrong type argument to conjugation");
5904 0 : return error_mark_node;
5905 : }
5906 0 : else if (!noconvert)
5907 0 : arg = default_conversion (arg);
5908 : break;
5909 :
5910 376005 : case TRUTH_NOT_EXPR:
5911 376005 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5912 7079 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5913 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5914 18 : && typecode != BITINT_TYPE)
5915 : {
5916 10 : error_at (location,
5917 : "wrong type argument to unary exclamation mark");
5918 10 : return error_mark_node;
5919 : }
5920 375995 : if (int_operands)
5921 : {
5922 139857 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5923 139857 : arg = remove_c_maybe_const_expr (arg);
5924 : }
5925 : else
5926 236138 : arg = c_objc_common_truthvalue_conversion (location, arg);
5927 375995 : ret = invert_truthvalue_loc (location, arg);
5928 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5929 375995 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5930 235743 : location = EXPR_LOCATION (ret);
5931 375995 : goto return_build_unary_op;
5932 :
5933 201216 : case REALPART_EXPR:
5934 201216 : case IMAGPART_EXPR:
5935 201216 : ret = build_real_imag_expr (location, code, arg);
5936 201216 : if (ret == error_mark_node)
5937 : return error_mark_node;
5938 201208 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5939 2 : eptype = TREE_TYPE (eptype);
5940 201208 : goto return_build_unary_op;
5941 :
5942 934818 : case PREINCREMENT_EXPR:
5943 934818 : case POSTINCREMENT_EXPR:
5944 934818 : case PREDECREMENT_EXPR:
5945 934818 : case POSTDECREMENT_EXPR:
5946 :
5947 934818 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5948 : {
5949 2 : tree inner = build_unary_op (location, code,
5950 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
5951 : noconvert);
5952 2 : if (inner == error_mark_node)
5953 : return error_mark_node;
5954 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5955 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5956 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5957 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5958 2 : goto return_build_unary_op;
5959 : }
5960 :
5961 : /* Complain about anything that is not a true lvalue. In
5962 : Objective-C, skip this check for property_refs. */
5963 934816 : if (!objc_is_property_ref (arg)
5964 1869632 : && !lvalue_or_else (location,
5965 934816 : arg, ((code == PREINCREMENT_EXPR
5966 934816 : || code == POSTINCREMENT_EXPR)
5967 : ? lv_increment
5968 : : lv_decrement)))
5969 22 : return error_mark_node;
5970 :
5971 934794 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5972 : {
5973 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5974 2 : warning_at (location, OPT_Wc___compat,
5975 : "increment of enumeration value is invalid in C++");
5976 : else
5977 2 : warning_at (location, OPT_Wc___compat,
5978 : "decrement of enumeration value is invalid in C++");
5979 : }
5980 :
5981 934794 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5982 : {
5983 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5984 328 : warning_at (location, OPT_Wbool_operation,
5985 : "increment of a boolean expression");
5986 : else
5987 336 : warning_at (location, OPT_Wbool_operation,
5988 : "decrement of a boolean expression");
5989 : }
5990 :
5991 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5992 934794 : arg = c_fully_fold (arg, false, NULL, true);
5993 :
5994 934794 : bool atomic_op;
5995 934794 : atomic_op = really_atomic_lvalue (arg);
5996 :
5997 : /* Increment or decrement the real part of the value,
5998 : and don't change the imaginary part. */
5999 934794 : if (typecode == COMPLEX_TYPE)
6000 : {
6001 57 : tree real, imag;
6002 :
6003 57 : pedwarn_c23 (location, OPT_Wpedantic,
6004 : "ISO C does not support %<++%> and %<--%> on complex "
6005 : "types before C2Y");
6006 :
6007 57 : if (!atomic_op)
6008 : {
6009 53 : arg = stabilize_reference (arg);
6010 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6011 : true);
6012 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6013 : true);
6014 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6015 53 : if (real == error_mark_node || imag == error_mark_node)
6016 : return error_mark_node;
6017 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6018 : real, imag);
6019 53 : goto return_build_unary_op;
6020 : }
6021 : }
6022 :
6023 : /* Report invalid types. */
6024 :
6025 934741 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6026 704671 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6027 159 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6028 934797 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6029 : {
6030 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6031 3 : error_at (location, "wrong type argument to increment");
6032 : else
6033 2 : error_at (location, "wrong type argument to decrement");
6034 :
6035 5 : return error_mark_node;
6036 : }
6037 :
6038 934736 : {
6039 934736 : tree inc;
6040 :
6041 934736 : argtype = TREE_TYPE (arg);
6042 :
6043 : /* Compute the increment. */
6044 :
6045 934736 : if (typecode == POINTER_TYPE)
6046 : {
6047 : /* If pointer target is an incomplete type,
6048 : we just cannot know how to do the arithmetic. */
6049 230070 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6050 : {
6051 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6052 17 : error_at (location,
6053 : "increment of pointer to an incomplete type %qT",
6054 17 : TREE_TYPE (argtype));
6055 : else
6056 10 : error_at (location,
6057 : "decrement of pointer to an incomplete type %qT",
6058 10 : TREE_TYPE (argtype));
6059 : }
6060 230043 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6061 230043 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6062 : {
6063 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6064 6 : pedwarn (location, OPT_Wpointer_arith,
6065 : "wrong type argument to increment");
6066 : else
6067 4 : pedwarn (location, OPT_Wpointer_arith,
6068 : "wrong type argument to decrement");
6069 : }
6070 : else
6071 460066 : verify_type_context (location, TCTX_POINTER_ARITH,
6072 230033 : TREE_TYPE (argtype));
6073 :
6074 230070 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6075 230070 : inc = convert_to_ptrofftype_loc (location, inc);
6076 : }
6077 704666 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6078 : {
6079 : /* For signed fract types, we invert ++ to -- or
6080 : -- to ++, and change inc from 1 to -1, because
6081 : it is not possible to represent 1 in signed fract constants.
6082 : For unsigned fract types, the result always overflows and
6083 : we get an undefined (original) or the maximum value. */
6084 0 : if (code == PREINCREMENT_EXPR)
6085 : code = PREDECREMENT_EXPR;
6086 : else if (code == PREDECREMENT_EXPR)
6087 : code = PREINCREMENT_EXPR;
6088 : else if (code == POSTINCREMENT_EXPR)
6089 : code = POSTDECREMENT_EXPR;
6090 : else /* code == POSTDECREMENT_EXPR */
6091 0 : code = POSTINCREMENT_EXPR;
6092 :
6093 0 : inc = integer_minus_one_node;
6094 0 : inc = convert (argtype, inc);
6095 : }
6096 : else
6097 : {
6098 704615 : inc = VECTOR_TYPE_P (argtype)
6099 704666 : ? build_one_cst (argtype)
6100 : : integer_one_node;
6101 704666 : inc = convert (argtype, inc);
6102 : }
6103 :
6104 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6105 : need to ask Objective-C to build the increment or decrement
6106 : expression for it. */
6107 934736 : if (objc_is_property_ref (arg))
6108 0 : return objc_build_incr_expr_for_property_ref (location, code,
6109 20 : arg, inc);
6110 :
6111 : /* Report a read-only lvalue. */
6112 934736 : if (TYPE_READONLY (argtype))
6113 : {
6114 20 : readonly_error (location, arg,
6115 20 : ((code == PREINCREMENT_EXPR
6116 20 : || code == POSTINCREMENT_EXPR)
6117 : ? lv_increment : lv_decrement));
6118 20 : return error_mark_node;
6119 : }
6120 934716 : else if (TREE_READONLY (arg))
6121 4 : readonly_warning (arg,
6122 4 : ((code == PREINCREMENT_EXPR
6123 4 : || code == POSTINCREMENT_EXPR)
6124 : ? lv_increment : lv_decrement));
6125 :
6126 : /* If the argument is atomic, use the special code sequences for
6127 : atomic compound assignment. */
6128 934716 : if (atomic_op)
6129 : {
6130 4469 : arg = stabilize_reference (arg);
6131 8938 : ret = build_atomic_assign (location, arg,
6132 4469 : ((code == PREINCREMENT_EXPR
6133 4469 : || code == POSTINCREMENT_EXPR)
6134 : ? PLUS_EXPR
6135 : : MINUS_EXPR),
6136 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6137 : ? inc
6138 : : integer_one_node),
6139 : (code == POSTINCREMENT_EXPR
6140 4469 : || code == POSTDECREMENT_EXPR));
6141 934716 : goto return_build_unary_op;
6142 : }
6143 :
6144 930247 : tree true_res;
6145 930247 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6146 : {
6147 364 : tree larg = stabilize_reference (arg);
6148 364 : tree sarg = save_expr (larg);
6149 364 : switch (code)
6150 : {
6151 91 : case PREINCREMENT_EXPR:
6152 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6153 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6154 91 : break;
6155 91 : case POSTINCREMENT_EXPR:
6156 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6157 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6158 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6159 91 : break;
6160 91 : case PREDECREMENT_EXPR:
6161 91 : {
6162 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6163 : true, true);
6164 91 : rarg = invert_truthvalue_loc (location, rarg);
6165 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6166 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6167 : }
6168 91 : break;
6169 91 : case POSTDECREMENT_EXPR:
6170 91 : {
6171 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6172 : true, true);
6173 91 : rarg = invert_truthvalue_loc (location, rarg);
6174 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6175 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6176 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6177 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6178 : }
6179 91 : break;
6180 : default:
6181 : gcc_unreachable ();
6182 : }
6183 364 : TREE_SIDE_EFFECTS (val) = 1;
6184 : }
6185 929883 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6186 64 : val = boolean_increment (code, arg);
6187 : else
6188 929819 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6189 930247 : TREE_SIDE_EFFECTS (val) = 1;
6190 930247 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6191 3939 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6192 : TYPE_UNQUALIFIED);
6193 930247 : ret = val;
6194 930247 : goto return_build_unary_op;
6195 : }
6196 :
6197 51987930 : case ADDR_EXPR:
6198 : /* Note that this operation never does default_conversion. */
6199 :
6200 : /* The operand of unary '&' must be an lvalue (which excludes
6201 : expressions of type void), or, in C99, the result of a [] or
6202 : unary '*' operator. */
6203 51987930 : if (VOID_TYPE_P (TREE_TYPE (arg))
6204 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6205 51987949 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6206 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6207 :
6208 : /* Let &* cancel out to simplify resulting code. */
6209 51987930 : if (INDIRECT_REF_P (arg))
6210 : {
6211 : /* Don't let this be an lvalue. */
6212 22268 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6213 16787 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6214 5481 : ret = TREE_OPERAND (arg, 0);
6215 5481 : goto return_build_unary_op;
6216 : }
6217 :
6218 : /* Anything not already handled and not a true memory reference
6219 : or a non-lvalue array is an error. */
6220 51965662 : if (typecode != FUNCTION_TYPE && !noconvert
6221 51965662 : && !lvalue_or_else (location, arg, lv_addressof))
6222 17 : return error_mark_node;
6223 :
6224 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6225 : folding later. */
6226 51965645 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6227 : {
6228 14 : tree inner = build_unary_op (location, code,
6229 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6230 : noconvert);
6231 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6232 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6233 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6234 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6235 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6236 14 : goto return_build_unary_op;
6237 : }
6238 :
6239 : /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
6240 : .ACCESS_WITH_SIZE. */
6241 51965631 : if (is_access_with_size_p (arg))
6242 0 : arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
6243 :
6244 51965631 : argtype = TREE_TYPE (arg);
6245 :
6246 : /* If the lvalue is const or volatile, merge that into the type
6247 : to which the address will point. This is only needed
6248 : for function types. */
6249 51965631 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6250 51478723 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6251 82062452 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6252 : {
6253 30016833 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6254 30016833 : int quals = orig_quals;
6255 :
6256 30016833 : if (TREE_READONLY (arg))
6257 29643036 : quals |= TYPE_QUAL_CONST;
6258 30016833 : if (TREE_THIS_VOLATILE (arg))
6259 374684 : quals |= TYPE_QUAL_VOLATILE;
6260 :
6261 30016833 : argtype = c_build_qualified_type (argtype, quals);
6262 : }
6263 :
6264 51965631 : switch (TREE_CODE (arg))
6265 : {
6266 65563 : case COMPONENT_REF:
6267 65563 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6268 : {
6269 8 : error_at (location, "cannot take address of bit-field %qD",
6270 8 : TREE_OPERAND (arg, 1));
6271 8 : return error_mark_node;
6272 : }
6273 :
6274 : /* fall through */
6275 :
6276 139699 : case ARRAY_REF:
6277 139699 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6278 : {
6279 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6280 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6281 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6282 : {
6283 6 : error_at (location, "cannot take address of scalar with "
6284 : "reverse storage order");
6285 6 : return error_mark_node;
6286 : }
6287 :
6288 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6289 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6290 58 : warning_at (location, OPT_Wscalar_storage_order,
6291 : "address of array with reverse scalar storage "
6292 : "order requested");
6293 : }
6294 :
6295 51965617 : default:
6296 51965617 : break;
6297 : }
6298 :
6299 51965617 : if (!c_mark_addressable (arg))
6300 20 : return error_mark_node;
6301 :
6302 51965597 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6303 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6304 :
6305 51965597 : argtype = c_build_pointer_type (argtype);
6306 :
6307 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6308 : when we have proper support for integer constant expressions. */
6309 51965597 : val = get_base_address (arg);
6310 51965597 : if (val && INDIRECT_REF_P (val)
6311 51989594 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6312 : {
6313 540 : ret = fold_offsetof (arg, argtype);
6314 540 : goto return_build_unary_op;
6315 : }
6316 :
6317 51965057 : val = build1 (ADDR_EXPR, argtype, arg);
6318 :
6319 51965057 : ret = val;
6320 51965057 : goto return_build_unary_op;
6321 :
6322 10 : case PAREN_EXPR:
6323 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6324 10 : goto return_build_unary_op;
6325 :
6326 0 : default:
6327 0 : gcc_unreachable ();
6328 : }
6329 :
6330 7022303 : if (argtype == NULL_TREE)
6331 7022303 : argtype = TREE_TYPE (arg);
6332 7022303 : if (TREE_CODE (arg) == INTEGER_CST)
6333 6317774 : ret = (require_constant_value
6334 6317774 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6335 6299193 : : fold_build1_loc (location, code, argtype, arg));
6336 : else
6337 704529 : ret = build1 (code, argtype, arg);
6338 60505379 : return_build_unary_op:
6339 60505379 : gcc_assert (ret != error_mark_node);
6340 6462066 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6341 66967436 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6342 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6343 60504817 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6344 74 : ret = note_integer_operands (ret);
6345 60505379 : if (eptype)
6346 237 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6347 60505379 : protected_set_expr_location (ret, location);
6348 60505379 : return ret;
6349 : }
6350 :
6351 : /* Return nonzero if REF is an lvalue valid for this language.
6352 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6353 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6354 :
6355 : bool
6356 178232899 : lvalue_p (const_tree ref)
6357 : {
6358 179726512 : const enum tree_code code = TREE_CODE (ref);
6359 :
6360 179726512 : switch (code)
6361 : {
6362 1468800 : case REALPART_EXPR:
6363 1468800 : case IMAGPART_EXPR:
6364 1468800 : case COMPONENT_REF:
6365 1468800 : return lvalue_p (TREE_OPERAND (ref, 0));
6366 :
6367 24813 : case C_MAYBE_CONST_EXPR:
6368 24813 : return lvalue_p (TREE_OPERAND (ref, 1));
6369 :
6370 : case COMPOUND_LITERAL_EXPR:
6371 : case STRING_CST:
6372 : return true;
6373 :
6374 25464167 : case MEM_REF:
6375 25464167 : case TARGET_MEM_REF:
6376 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6377 : here as well. */
6378 25464167 : case INDIRECT_REF:
6379 25464167 : case ARRAY_REF:
6380 25464167 : case VAR_DECL:
6381 25464167 : case PARM_DECL:
6382 25464167 : case RESULT_DECL:
6383 25464167 : case ERROR_MARK:
6384 25464167 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6385 25464167 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6386 :
6387 6 : case BIND_EXPR:
6388 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6389 :
6390 7075732 : case CALL_EXPR:
6391 7075732 : return is_access_with_size_p (ref);
6392 :
6393 : default:
6394 : return false;
6395 : }
6396 : }
6397 :
6398 : /* Give a warning for storing in something that is read-only in GCC
6399 : terms but not const in ISO C terms. */
6400 :
6401 : static void
6402 5 : readonly_warning (tree arg, enum lvalue_use use)
6403 : {
6404 5 : switch (use)
6405 : {
6406 1 : case lv_assign:
6407 1 : warning (0, "assignment of read-only location %qE", arg);
6408 1 : break;
6409 2 : case lv_increment:
6410 2 : warning (0, "increment of read-only location %qE", arg);
6411 2 : break;
6412 2 : case lv_decrement:
6413 2 : warning (0, "decrement of read-only location %qE", arg);
6414 2 : break;
6415 0 : default:
6416 0 : gcc_unreachable ();
6417 : }
6418 5 : return;
6419 : }
6420 :
6421 :
6422 : /* Return nonzero if REF is an lvalue valid for this language;
6423 : otherwise, print an error message and return zero. USE says
6424 : how the lvalue is being used and so selects the error message.
6425 : LOCATION is the location at which any error should be reported. */
6426 :
6427 : static int
6428 4716008 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6429 : {
6430 4716008 : int win = lvalue_p (ref);
6431 :
6432 4716008 : if (!win)
6433 75 : lvalue_error (loc, use);
6434 :
6435 4716008 : return win;
6436 : }
6437 :
6438 : /* Mark EXP saying that we need to be able to take the
6439 : address of it; it should not be allocated in a register.
6440 : Returns true if successful. ARRAY_REF_P is true if this
6441 : is for ARRAY_REF construction - in that case we don't want
6442 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6443 : it is fine to use ARRAY_REFs for vector subscripts on vector
6444 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6445 : than producing an error for taking the address of a register. */
6446 :
6447 : bool
6448 52510242 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6449 : {
6450 52510242 : tree x = exp;
6451 :
6452 53090774 : while (1)
6453 53090774 : switch (TREE_CODE (x))
6454 : {
6455 10029 : case VIEW_CONVERT_EXPR:
6456 10029 : if (array_ref_p
6457 9919 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6458 19948 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6459 : return true;
6460 110 : x = TREE_OPERAND (x, 0);
6461 110 : break;
6462 :
6463 396324 : case COMPONENT_REF:
6464 396324 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6465 : {
6466 6 : error ("cannot take address of bit-field %qD",
6467 3 : TREE_OPERAND (x, 1));
6468 3 : return false;
6469 : }
6470 : /* FALLTHRU */
6471 580422 : case ADDR_EXPR:
6472 580422 : case ARRAY_REF:
6473 580422 : case REALPART_EXPR:
6474 580422 : case IMAGPART_EXPR:
6475 580422 : x = TREE_OPERAND (x, 0);
6476 580422 : break;
6477 :
6478 587 : case COMPOUND_LITERAL_EXPR:
6479 587 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6480 : {
6481 16 : if (override_register)
6482 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6483 : else
6484 : {
6485 4 : error ("address of register compound literal requested");
6486 4 : return false;
6487 : }
6488 : }
6489 583 : TREE_ADDRESSABLE (x) = 1;
6490 583 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6491 583 : return true;
6492 :
6493 0 : case CONSTRUCTOR:
6494 0 : TREE_ADDRESSABLE (x) = 1;
6495 0 : return true;
6496 :
6497 1321045 : case VAR_DECL:
6498 1321045 : case CONST_DECL:
6499 1321045 : case PARM_DECL:
6500 1321045 : case RESULT_DECL:
6501 1321045 : if (C_DECL_REGISTER (x)
6502 1321045 : && DECL_NONLOCAL (x))
6503 : {
6504 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6505 : {
6506 0 : error
6507 0 : ("global register variable %qD used in nested function", x);
6508 0 : return false;
6509 : }
6510 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6511 : }
6512 1321045 : else if (C_DECL_REGISTER (x))
6513 : {
6514 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6515 4 : error ("address of global register variable %qD requested", x);
6516 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6517 : {
6518 54 : DECL_REGISTER (x) = 0;
6519 54 : TREE_ADDRESSABLE (x) = 1;
6520 54 : mark_decl_used (x, true);
6521 54 : return true;
6522 : }
6523 : else
6524 15 : error ("address of register variable %qD requested", x);
6525 19 : return false;
6526 : }
6527 :
6528 : /* FALLTHRU */
6529 51937613 : case FUNCTION_DECL:
6530 51937613 : TREE_ADDRESSABLE (x) = 1;
6531 51937613 : mark_decl_used (x, true);
6532 : /* FALLTHRU */
6533 : default:
6534 : return true;
6535 : }
6536 : }
6537 :
6538 : /* Convert EXPR to TYPE, warning about conversion problems with
6539 : constants. SEMANTIC_TYPE is the type this conversion would use
6540 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6541 : is equivalent to convert_and_check. This function is a wrapper that
6542 : handles conversions that may be different than
6543 : the usual ones because of excess precision. */
6544 :
6545 : static tree
6546 23604576 : ep_convert_and_check (location_t loc, tree type, tree expr,
6547 : tree semantic_type)
6548 : {
6549 23604576 : if (TREE_TYPE (expr) == type)
6550 : return expr;
6551 :
6552 : /* For C11, integer conversions may have results with excess
6553 : precision. */
6554 2058619 : if (flag_isoc11 || !semantic_type)
6555 2058615 : return convert_and_check (loc, type, expr);
6556 :
6557 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6558 4 : && TREE_TYPE (expr) != semantic_type)
6559 : {
6560 : /* For integers, we need to check the real conversion, not
6561 : the conversion to the excess precision type. */
6562 4 : expr = convert_and_check (loc, semantic_type, expr);
6563 : }
6564 : /* Result type is the excess precision type, which should be
6565 : large enough, so do not check. */
6566 4 : return convert (type, expr);
6567 : }
6568 :
6569 : /* If EXPR refers to a built-in declared without a prototype returns
6570 : the actual type of the built-in and, if non-null, set *BLTIN to
6571 : a pointer to the built-in. Otherwise return the type of EXPR
6572 : and clear *BLTIN if non-null. */
6573 :
6574 : static tree
6575 2835620 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6576 : {
6577 2835620 : tree dummy;
6578 2835620 : if (!bltin)
6579 0 : bltin = &dummy;
6580 :
6581 2835620 : *bltin = NULL_TREE;
6582 :
6583 2835620 : tree type = TREE_TYPE (expr);
6584 2835620 : if (TREE_CODE (expr) != ADDR_EXPR)
6585 : return type;
6586 :
6587 208707 : tree oper = TREE_OPERAND (expr, 0);
6588 208707 : if (!DECL_P (oper)
6589 170207 : || TREE_CODE (oper) != FUNCTION_DECL
6590 245130 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6591 : return type;
6592 :
6593 2086 : built_in_function code = DECL_FUNCTION_CODE (oper);
6594 2086 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6595 : return type;
6596 :
6597 1977 : if ((*bltin = builtin_decl_implicit (code)))
6598 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6599 :
6600 : return type;
6601 : }
6602 :
6603 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6604 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6605 : if folded to an integer constant then the unselected half may
6606 : contain arbitrary operations not normally permitted in constant
6607 : expressions. Set the location of the expression to LOC. */
6608 :
6609 : tree
6610 408853 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6611 : tree op1, tree op1_original_type, location_t op1_loc,
6612 : tree op2, tree op2_original_type, location_t op2_loc)
6613 : {
6614 408853 : tree type1;
6615 408853 : tree type2;
6616 408853 : tree result_type = NULL;
6617 408853 : tree semantic_result_type = NULL;
6618 408853 : tree orig_op1 = op1, orig_op2 = op2;
6619 408853 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6620 408853 : bool ifexp_int_operands;
6621 408853 : tree ret;
6622 :
6623 408853 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6624 116135 : if (op1_int_operands)
6625 116135 : op1 = remove_c_maybe_const_expr (op1);
6626 408853 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6627 151325 : if (op2_int_operands)
6628 151325 : op2 = remove_c_maybe_const_expr (op2);
6629 408853 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6630 242300 : if (ifexp_int_operands)
6631 242300 : ifexp = remove_c_maybe_const_expr (ifexp);
6632 :
6633 : /* Promote both alternatives. */
6634 :
6635 408853 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6636 404936 : op1 = default_conversion (op1);
6637 408853 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6638 379013 : op2 = default_conversion (op2);
6639 :
6640 408853 : if (TREE_CODE (ifexp) == ERROR_MARK
6641 408784 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6642 817623 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6643 83 : return error_mark_node;
6644 :
6645 408770 : tree bltin1 = NULL_TREE;
6646 408770 : tree bltin2 = NULL_TREE;
6647 408770 : type1 = type_or_builtin_type (op1, &bltin1);
6648 408770 : const enum tree_code code1 = TREE_CODE (type1);
6649 408770 : type2 = type_or_builtin_type (op2, &bltin2);
6650 408770 : const enum tree_code code2 = TREE_CODE (type2);
6651 :
6652 408770 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6653 1 : return error_mark_node;
6654 :
6655 408769 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6656 1 : return error_mark_node;
6657 :
6658 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6659 : In C99 they will be pointers by now. */
6660 408768 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6661 : {
6662 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6663 1 : return error_mark_node;
6664 : }
6665 :
6666 408767 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6667 408761 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6668 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6669 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6670 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6671 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6672 : {
6673 7 : semantic_result_type = c_common_type (type1, type2);
6674 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6675 : {
6676 6 : op1 = TREE_OPERAND (op1, 0);
6677 6 : type1 = TREE_TYPE (op1);
6678 6 : gcc_assert (TREE_CODE (type1) == code1);
6679 : }
6680 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6681 : {
6682 1 : op2 = TREE_OPERAND (op2, 0);
6683 1 : type2 = TREE_TYPE (op2);
6684 1 : gcc_assert (TREE_CODE (type2) == code2);
6685 : }
6686 : }
6687 :
6688 408767 : if (warn_cxx_compat)
6689 : {
6690 418 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6691 418 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6692 :
6693 418 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6694 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6695 424 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6696 2 : warning_at (colon_loc, OPT_Wc___compat,
6697 : "different enum types in conditional is "
6698 : "invalid in C++: %qT vs %qT", t1, t2);
6699 : }
6700 :
6701 408767 : if (warn_zero_as_null_pointer_constant
6702 20 : && c_inhibit_evaluation_warnings == 0)
6703 : {
6704 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6705 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6706 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6707 : "zero as null pointer constant");
6708 :
6709 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6710 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6711 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6712 : "zero as null pointer constant");
6713 : }
6714 :
6715 : /* Quickly detect the usual case where op1 and op2 have the same type
6716 : after promotion. */
6717 408767 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6718 : {
6719 274222 : if (type1 == type2)
6720 : result_type = type1;
6721 : else
6722 6388 : result_type = TYPE_MAIN_VARIANT (type1);
6723 : }
6724 134545 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6725 79900 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6726 54693 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6727 26152 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6728 : {
6729 : /* In C11, a conditional expression between a floating-point
6730 : type and an integer type should convert the integer type to
6731 : the evaluation format of the floating-point type, with
6732 : possible excess precision. */
6733 28641 : tree eptype1 = type1;
6734 28641 : tree eptype2 = type2;
6735 28641 : if (flag_isoc11)
6736 : {
6737 28328 : tree eptype;
6738 11640 : if (ANY_INTEGRAL_TYPE_P (type1)
6739 28328 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6740 : {
6741 2 : eptype2 = eptype;
6742 2 : if (!semantic_result_type)
6743 2 : semantic_result_type = c_common_type (type1, type2);
6744 : }
6745 501 : else if (ANY_INTEGRAL_TYPE_P (type2)
6746 28326 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6747 : {
6748 4626 : eptype1 = eptype;
6749 4626 : if (!semantic_result_type)
6750 4626 : semantic_result_type = c_common_type (type1, type2);
6751 : }
6752 : }
6753 28641 : result_type = c_common_type (eptype1, eptype2);
6754 28641 : if (result_type == error_mark_node)
6755 : return error_mark_node;
6756 28639 : do_warn_double_promotion (result_type, type1, type2,
6757 : "implicit conversion from %qT to %qT to "
6758 : "match other result of conditional",
6759 : colon_loc);
6760 :
6761 : /* If -Wsign-compare, warn here if type1 and type2 have
6762 : different signedness. We'll promote the signed to unsigned
6763 : and later code won't know it used to be different.
6764 : Do this check on the original types, so that explicit casts
6765 : will be considered, but default promotions won't. */
6766 28639 : if (c_inhibit_evaluation_warnings == 0)
6767 : {
6768 28448 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6769 28448 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6770 :
6771 28448 : if (unsigned_op1 ^ unsigned_op2)
6772 : {
6773 13011 : bool ovf;
6774 :
6775 : /* Do not warn if the result type is signed, since the
6776 : signed type will only be chosen if it can represent
6777 : all the values of the unsigned type. */
6778 13011 : if (!TYPE_UNSIGNED (result_type))
6779 : /* OK */;
6780 : else
6781 : {
6782 12907 : bool op1_maybe_const = true;
6783 12907 : bool op2_maybe_const = true;
6784 :
6785 : /* Do not warn if the signed quantity is an
6786 : unsuffixed integer literal (or some static
6787 : constant expression involving such literals) and
6788 : it is non-negative. This warning requires the
6789 : operands to be folded for best results, so do
6790 : that folding in this case even without
6791 : warn_sign_compare to avoid warning options
6792 : possibly affecting code generation. */
6793 12907 : c_inhibit_evaluation_warnings
6794 12907 : += (ifexp == truthvalue_false_node);
6795 12907 : op1 = c_fully_fold (op1, require_constant_value,
6796 : &op1_maybe_const);
6797 12907 : c_inhibit_evaluation_warnings
6798 12907 : -= (ifexp == truthvalue_false_node);
6799 :
6800 12907 : c_inhibit_evaluation_warnings
6801 12907 : += (ifexp == truthvalue_true_node);
6802 12907 : op2 = c_fully_fold (op2, require_constant_value,
6803 : &op2_maybe_const);
6804 12907 : c_inhibit_evaluation_warnings
6805 12907 : -= (ifexp == truthvalue_true_node);
6806 :
6807 12907 : if (warn_sign_compare)
6808 : {
6809 1245 : if ((unsigned_op2
6810 685 : && tree_expr_nonnegative_warnv_p (op1, &ovf))
6811 1248 : || (unsigned_op1
6812 560 : && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6813 : /* OK */;
6814 10 : else if (unsigned_op2)
6815 3 : warning_at (op1_loc, OPT_Wsign_compare,
6816 : "operand of %<?:%> changes signedness from "
6817 : "%qT to %qT due to unsignedness of other "
6818 3 : "operand", TREE_TYPE (orig_op1),
6819 3 : TREE_TYPE (orig_op2));
6820 : else
6821 7 : warning_at (op2_loc, OPT_Wsign_compare,
6822 : "operand of %<?:%> changes signedness from "
6823 : "%qT to %qT due to unsignedness of other "
6824 7 : "operand", TREE_TYPE (orig_op2),
6825 7 : TREE_TYPE (orig_op1));
6826 : }
6827 12907 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6828 7562 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6829 12907 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6830 4391 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6831 : }
6832 : }
6833 : }
6834 : }
6835 105904 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6836 : {
6837 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6838 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6839 : "ISO C forbids conditional expr with only one void side");
6840 25995 : result_type = void_type_node;
6841 : }
6842 79909 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6843 : {
6844 79512 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6845 79512 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6846 79512 : addr_space_t as_common;
6847 :
6848 79512 : if (comp_target_types (colon_loc, type1, type2))
6849 : {
6850 2619 : ifexp = save_expr (ifexp);
6851 2619 : result_type = common_pointer_type (type1, type2, ifexp);
6852 : }
6853 76893 : else if (null_pointer_constant_p (orig_op1))
6854 : result_type = type2;
6855 72731 : else if (null_pointer_constant_p (orig_op2))
6856 : result_type = type1;
6857 34803 : else if (!addr_space_superset (as1, as2, &as_common))
6858 : {
6859 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6860 : "used in conditional expression");
6861 0 : return error_mark_node;
6862 : }
6863 34803 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6864 330 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6865 34805 : || (VOID_TYPE_P (TREE_TYPE (type2))
6866 34428 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6867 : {
6868 34755 : tree t1 = TREE_TYPE (type1);
6869 34755 : tree t2 = TREE_TYPE (type2);
6870 34755 : if (!(VOID_TYPE_P (t1)
6871 329 : && !TYPE_ATOMIC (t1)))
6872 : {
6873 : /* roles are swapped */
6874 34427 : t1 = t2;
6875 34427 : t2 = TREE_TYPE (type1);
6876 : }
6877 34755 : tree t2_stripped = strip_array_types (t2);
6878 34755 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6879 34755 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6880 : {
6881 18 : if (!flag_isoc23)
6882 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6883 : "pointer to array loses qualifier "
6884 : "in conditional expression");
6885 12 : else if (warn_c11_c23_compat > 0)
6886 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6887 : "pointer to array loses qualifier "
6888 : "in conditional expression in ISO C before C23");
6889 : }
6890 34755 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6891 4 : pedwarn (colon_loc, OPT_Wpedantic,
6892 : "ISO C forbids conditional expr between "
6893 : "%<void *%> and function pointer");
6894 : /* for array, use qualifiers of element type */
6895 34755 : if (flag_isoc23)
6896 11006 : t2 = t2_stripped;
6897 34755 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6898 : }
6899 : /* Objective-C pointer comparisons are a bit more lenient. */
6900 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6901 0 : result_type = objc_common_type (type1, type2);
6902 : else
6903 : {
6904 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6905 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6906 48 : if (!flag_isoc99)
6907 : /* This downgrade to a warning ensures that -std=gnu89
6908 : -pedantic-errors does not flag these mismatches between
6909 : builtins as errors (as diagnostics::kind::permerror would)
6910 : ISO C99 and later do not have implicit function declarations,
6911 : so the mismatch cannot occur naturally there. */
6912 5 : kind = (bltin1 && bltin2
6913 5 : ? diagnostics::kind::warning
6914 : : diagnostics::kind::pedwarn);
6915 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6916 : "pointer type mismatch "
6917 : "in conditional expression"))
6918 : {
6919 46 : inform (op1_loc, "first expression has type %qT", type1);
6920 46 : inform (op2_loc, "second expression has type %qT", type2);
6921 : }
6922 48 : result_type = c_build_pointer_type
6923 48 : (c_build_qualified_type (void_type_node, qual));
6924 : }
6925 : }
6926 397 : else if (code1 == POINTER_TYPE
6927 277 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6928 : {
6929 273 : if (!null_pointer_constant_p (orig_op2))
6930 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6931 : "pointer/integer type mismatch "
6932 : "in conditional expression");
6933 : else
6934 : {
6935 259 : op2 = null_pointer_node;
6936 : }
6937 : result_type = type1;
6938 : }
6939 124 : else if (code2 == POINTER_TYPE
6940 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6941 : {
6942 86 : if (!null_pointer_constant_p (orig_op1))
6943 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6944 : "pointer/integer type mismatch "
6945 : "in conditional expression");
6946 : else
6947 : {
6948 72 : op1 = null_pointer_node;
6949 : }
6950 : result_type = type2;
6951 : }
6952 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6953 : type nullptr_t and the other is a pointer, the result type is the pointer
6954 : type." */
6955 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6956 : result_type = type2;
6957 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6958 : result_type = type1;
6959 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6960 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
6961 8 : TYPE_MAIN_VARIANT (type2)))
6962 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6963 8 : TYPE_MAIN_VARIANT (type2));
6964 :
6965 408743 : if (!result_type)
6966 : {
6967 22 : if (flag_cond_mismatch)
6968 0 : result_type = void_type_node;
6969 : else
6970 : {
6971 22 : error_at (colon_loc, "type mismatch in conditional expression");
6972 22 : return error_mark_node;
6973 : }
6974 : }
6975 :
6976 : /* Merge const and volatile flags of the incoming types. */
6977 408743 : result_type
6978 408743 : = build_type_variant (result_type,
6979 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
6980 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6981 :
6982 408743 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
6983 : semantic_result_type);
6984 408743 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
6985 : semantic_result_type);
6986 :
6987 408743 : if (ifexp_bcp && ifexp == truthvalue_true_node)
6988 : {
6989 10 : op2_int_operands = true;
6990 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
6991 : }
6992 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
6993 : {
6994 49 : op1_int_operands = true;
6995 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
6996 : }
6997 914650 : int_const = int_operands = (ifexp_int_operands
6998 408743 : && op1_int_operands
6999 408743 : && op2_int_operands);
7000 97164 : if (int_operands)
7001 : {
7002 97164 : int_const = ((ifexp == truthvalue_true_node
7003 58805 : && TREE_CODE (orig_op1) == INTEGER_CST
7004 58791 : && !TREE_OVERFLOW (orig_op1))
7005 97178 : || (ifexp == truthvalue_false_node
7006 38313 : && TREE_CODE (orig_op2) == INTEGER_CST
7007 38297 : && !TREE_OVERFLOW (orig_op2)));
7008 : }
7009 :
7010 : /* Need to convert condition operand into a vector mask. */
7011 408743 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7012 : {
7013 0 : tree vectype = TREE_TYPE (ifexp);
7014 0 : tree elem_type = TREE_TYPE (vectype);
7015 0 : tree zero = build_int_cst (elem_type, 0);
7016 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7017 0 : tree cmp_type = truth_type_for (vectype);
7018 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7019 : }
7020 :
7021 408743 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7022 97120 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7023 : else
7024 : {
7025 311623 : if (int_operands)
7026 : {
7027 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7028 : nested inside of the expression. */
7029 76 : op1 = c_fully_fold (op1, false, NULL);
7030 76 : op2 = c_fully_fold (op2, false, NULL);
7031 : }
7032 311623 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7033 311623 : if (int_operands)
7034 76 : ret = note_integer_operands (ret);
7035 : }
7036 408743 : if (semantic_result_type)
7037 4635 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7038 :
7039 408743 : protected_set_expr_location (ret, colon_loc);
7040 :
7041 : /* If the OP1 and OP2 are the same and don't have side-effects,
7042 : warn here, because the COND_EXPR will be turned into OP1. */
7043 408743 : if (warn_duplicated_branches
7044 34 : && TREE_CODE (ret) == COND_EXPR
7045 408777 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7046 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7047 : "this condition has identical branches");
7048 :
7049 : return ret;
7050 : }
7051 :
7052 : /* EXPR is an expression, location LOC, whose result is discarded.
7053 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7054 : whose right-hand operand is such a call, possibly recursively). */
7055 :
7056 : static void
7057 6473466 : maybe_warn_nodiscard (location_t loc, tree expr)
7058 : {
7059 6473466 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7060 : return;
7061 3957515 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7062 : {
7063 49303 : expr = TREE_OPERAND (expr, 1);
7064 49303 : if (EXPR_HAS_LOCATION (expr))
7065 32202 : loc = EXPR_LOCATION (expr);
7066 : }
7067 3908212 : if (TREE_CODE (expr) != CALL_EXPR)
7068 : return;
7069 343852 : tree fn = CALL_EXPR_FN (expr);
7070 343852 : if (!fn)
7071 : return;
7072 343836 : tree attr;
7073 343836 : if (TREE_CODE (fn) == ADDR_EXPR
7074 343169 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7075 687005 : && (attr = lookup_attribute ("nodiscard",
7076 343169 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7077 : {
7078 3 : fn = TREE_OPERAND (fn, 0);
7079 3 : tree args = TREE_VALUE (attr);
7080 3 : if (args)
7081 1 : args = TREE_VALUE (args);
7082 3 : auto_diagnostic_group d;
7083 3 : auto_urlify_attributes sentinel;
7084 3 : int warned;
7085 3 : if (args)
7086 1 : warned = warning_at (loc, OPT_Wunused_result,
7087 : "ignoring return value of %qD, declared with "
7088 : "attribute %<nodiscard%>: %E", fn, args);
7089 : else
7090 2 : warned = warning_at (loc, OPT_Wunused_result,
7091 : "ignoring return value of %qD, declared with "
7092 : "attribute %<nodiscard%>", fn);
7093 3 : if (warned)
7094 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7095 3 : }
7096 : else
7097 : {
7098 343833 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7099 343833 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7100 343833 : if (!attr)
7101 343821 : return;
7102 12 : tree args = TREE_VALUE (attr);
7103 12 : if (args)
7104 5 : args = TREE_VALUE (args);
7105 12 : auto_diagnostic_group d;
7106 12 : auto_urlify_attributes sentinel;
7107 12 : int warned;
7108 12 : if (args)
7109 5 : warned = warning_at (loc, OPT_Wunused_result,
7110 : "ignoring return value of type %qT, declared "
7111 : "with attribute %<nodiscard%>: %E",
7112 : rettype, args);
7113 : else
7114 7 : warned = warning_at (loc, OPT_Wunused_result,
7115 : "ignoring return value of type %qT, declared "
7116 : "with attribute %<nodiscard%>", rettype);
7117 12 : if (warned)
7118 : {
7119 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7120 : {
7121 11 : fn = TREE_OPERAND (fn, 0);
7122 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7123 11 : inform (DECL_SOURCE_LOCATION (fn),
7124 : "in call to %qD, declared here", fn);
7125 : }
7126 : }
7127 12 : }
7128 : }
7129 :
7130 : /* Return a compound expression that performs two expressions and
7131 : returns the value of the second of them.
7132 :
7133 : LOC is the location of the COMPOUND_EXPR. */
7134 :
7135 : tree
7136 111772 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7137 : {
7138 111772 : bool expr1_int_operands, expr2_int_operands;
7139 111772 : tree eptype = NULL_TREE;
7140 111772 : tree ret;
7141 :
7142 111772 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7143 326 : if (expr1_int_operands)
7144 326 : expr1 = remove_c_maybe_const_expr (expr1);
7145 111772 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7146 66105 : if (expr2_int_operands)
7147 66105 : expr2 = remove_c_maybe_const_expr (expr2);
7148 :
7149 111772 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7150 0 : expr1 = TREE_OPERAND (expr1, 0);
7151 111772 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7152 : {
7153 1 : eptype = TREE_TYPE (expr2);
7154 1 : expr2 = TREE_OPERAND (expr2, 0);
7155 : }
7156 :
7157 111772 : if (!TREE_SIDE_EFFECTS (expr1))
7158 : {
7159 : /* The left-hand operand of a comma expression is like an expression
7160 : statement: with -Wunused, we should warn if it doesn't have
7161 : any side-effects, unless it was explicitly cast to (void). */
7162 8406 : if (warn_unused_value)
7163 : {
7164 1257 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7165 1257 : && CONVERT_EXPR_P (expr1))
7166 : ; /* (void) a, b */
7167 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7168 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7169 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7170 : ; /* (void) a, (void) b, c */
7171 : else
7172 11 : warning_at (loc, OPT_Wunused_value,
7173 : "left-hand operand of comma expression has no effect");
7174 : }
7175 : }
7176 103366 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7177 13111 : && warn_unused_value)
7178 : {
7179 : tree r = expr1;
7180 : location_t cloc = loc;
7181 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7182 : {
7183 2417 : if (EXPR_HAS_LOCATION (r))
7184 2417 : cloc = EXPR_LOCATION (r);
7185 2417 : r = TREE_OPERAND (r, 1);
7186 : }
7187 2416 : if (!TREE_SIDE_EFFECTS (r)
7188 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7189 2420 : && !CONVERT_EXPR_P (r))
7190 4 : warning_at (cloc, OPT_Wunused_value,
7191 : "right-hand operand of comma expression has no effect");
7192 : }
7193 :
7194 : /* With -Wunused, we should also warn if the left-hand operand does have
7195 : side-effects, but computes a value which is not used. For example, in
7196 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7197 : so we should issue a warning. */
7198 100950 : else if (warn_unused_value)
7199 35400 : warn_if_unused_value (expr1, loc);
7200 :
7201 111772 : maybe_warn_nodiscard (loc, expr1);
7202 :
7203 111772 : if (expr2 == error_mark_node)
7204 : return error_mark_node;
7205 :
7206 111759 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7207 :
7208 111759 : if (flag_isoc99
7209 : && expr1_int_operands
7210 111303 : && expr2_int_operands)
7211 153 : ret = note_integer_operands (ret);
7212 :
7213 111759 : if (eptype)
7214 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7215 :
7216 111759 : protected_set_expr_location (ret, loc);
7217 111759 : return ret;
7218 : }
7219 :
7220 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7221 : which we are casting. OTYPE is the type of the expression being
7222 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7223 : of the cast. -Wcast-qual appeared on the command line. Named
7224 : address space qualifiers are not handled here, because they result
7225 : in different warnings. */
7226 :
7227 : static void
7228 13405 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7229 : {
7230 13405 : tree in_type = type;
7231 13405 : tree in_otype = otype;
7232 13405 : int added = 0;
7233 13405 : int discarded = 0;
7234 13575 : bool is_const;
7235 :
7236 : /* Check that the qualifiers on IN_TYPE are a superset of the
7237 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7238 : nodes is uninteresting and we stop as soon as we hit a
7239 : non-POINTER_TYPE node on either type. */
7240 13575 : do
7241 : {
7242 13575 : in_otype = TREE_TYPE (in_otype);
7243 13575 : in_type = TREE_TYPE (in_type);
7244 :
7245 : /* GNU C allows cv-qualified function types. 'const' means the
7246 : function is very pure, 'volatile' means it can't return. We
7247 : need to warn when such qualifiers are added, not when they're
7248 : taken away. */
7249 13575 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7250 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7251 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7252 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7253 : else
7254 13557 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7255 13557 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7256 : }
7257 13575 : while (TREE_CODE (in_type) == POINTER_TYPE
7258 13575 : && TREE_CODE (in_otype) == POINTER_TYPE);
7259 :
7260 13405 : if (added)
7261 2 : warning_at (loc, OPT_Wcast_qual,
7262 : "cast adds %q#v qualifier to function type", added);
7263 :
7264 13405 : if (discarded)
7265 : /* There are qualifiers present in IN_OTYPE that are not present
7266 : in IN_TYPE. */
7267 1117 : warning_at (loc, OPT_Wcast_qual,
7268 : "cast discards %qv qualifier from pointer target type",
7269 : discarded);
7270 :
7271 13405 : if (added || discarded)
7272 : return;
7273 :
7274 : /* A cast from **T to const **T is unsafe, because it can cause a
7275 : const value to be changed with no additional warning. We only
7276 : issue this warning if T is the same on both sides, and we only
7277 : issue the warning if there are the same number of pointers on
7278 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7279 : cast is unsafe when a qualifier is added at one level and const
7280 : is not present at all outer levels.
7281 :
7282 : To issue this warning, we check at each level whether the cast
7283 : adds new qualifiers not already seen. We don't need to special
7284 : case function types, as they won't have the same
7285 : TYPE_MAIN_VARIANT. */
7286 :
7287 12286 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7288 : return;
7289 51 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7290 : return;
7291 :
7292 48 : in_type = type;
7293 48 : in_otype = otype;
7294 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7295 130 : do
7296 : {
7297 130 : in_type = TREE_TYPE (in_type);
7298 130 : in_otype = TREE_TYPE (in_otype);
7299 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7300 130 : && !is_const)
7301 : {
7302 27 : warning_at (loc, OPT_Wcast_qual,
7303 : "to be safe all intermediate pointers in cast from "
7304 : "%qT to %qT must be %<const%> qualified",
7305 : otype, type);
7306 27 : break;
7307 : }
7308 103 : if (is_const)
7309 72 : is_const = TYPE_READONLY (in_type);
7310 : }
7311 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7312 : }
7313 :
7314 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7315 :
7316 : static bool
7317 1280 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7318 : {
7319 1280 : if (error_operand_p (t1) || error_operand_p (t2))
7320 : return true;
7321 :
7322 1279 : t1 = TYPE_MAIN_VARIANT (t1);
7323 1279 : t2 = TYPE_MAIN_VARIANT (t2);
7324 :
7325 1279 : if (TREE_CODE (t1) == POINTER_TYPE
7326 213 : && TREE_CODE (t2) == POINTER_TYPE)
7327 : return true;
7328 :
7329 : /* Only the precision of the parameter matters. This check should
7330 : make sure that the callee does not see undefined values in argument
7331 : registers. */
7332 1087 : if (INTEGRAL_TYPE_P (t1)
7333 905 : && INTEGRAL_TYPE_P (t2)
7334 1424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7335 : return true;
7336 :
7337 930 : return comptypes (t1, t2);
7338 : }
7339 :
7340 : /* Check if a type cast between two function types can be considered safe. */
7341 :
7342 : static bool
7343 7469 : c_safe_function_type_cast_p (tree t1, tree t2)
7344 : {
7345 7469 : if (TREE_TYPE (t1) == void_type_node &&
7346 5479 : TYPE_ARG_TYPES (t1) == void_list_node)
7347 : return true;
7348 :
7349 3714 : if (TREE_TYPE (t2) == void_type_node &&
7350 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7351 : return true;
7352 :
7353 884 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7354 : return false;
7355 :
7356 189 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7357 481 : t1 && t2;
7358 292 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7359 396 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7360 : return false;
7361 :
7362 : return true;
7363 : }
7364 :
7365 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7366 : LOC is the location of the cast-- typically the open paren of the cast. */
7367 :
7368 : tree
7369 120274056 : build_c_cast (location_t loc, tree type, tree expr)
7370 : {
7371 120274056 : tree value;
7372 :
7373 120274056 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7374 :
7375 120274056 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7376 66 : expr = TREE_OPERAND (expr, 0);
7377 :
7378 120274056 : value = expr;
7379 120274056 : if (int_operands)
7380 6872565 : value = remove_c_maybe_const_expr (value);
7381 :
7382 120274056 : if (type == error_mark_node || expr == error_mark_node)
7383 : return error_mark_node;
7384 :
7385 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7386 : only in <protocol> qualifications. But when constructing cast expressions,
7387 : the protocols do matter and must be kept around. */
7388 120273034 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7389 0 : return build1 (NOP_EXPR, type, expr);
7390 :
7391 120273034 : type = TYPE_MAIN_VARIANT (type);
7392 :
7393 120273034 : if (TREE_CODE (type) == ARRAY_TYPE)
7394 : {
7395 13 : error_at (loc, "cast specifies array type");
7396 13 : return error_mark_node;
7397 : }
7398 :
7399 120273021 : if (TREE_CODE (type) == FUNCTION_TYPE)
7400 : {
7401 6 : error_at (loc, "cast specifies function type");
7402 6 : return error_mark_node;
7403 : }
7404 :
7405 120273015 : if (!VOID_TYPE_P (type))
7406 : {
7407 120228671 : value = require_complete_type (loc, value);
7408 120228671 : if (value == error_mark_node)
7409 : return error_mark_node;
7410 : }
7411 :
7412 120273014 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7413 : {
7414 18124794 : if (RECORD_OR_UNION_TYPE_P (type)
7415 18124794 : && pedwarn (loc, OPT_Wpedantic,
7416 : "ISO C forbids casting nonscalar to the same type"))
7417 : ;
7418 18124790 : else if (warn_useless_cast)
7419 7 : warning_at (loc, OPT_Wuseless_cast,
7420 : "useless cast to type %qT", type);
7421 :
7422 : /* Convert to remove any qualifiers from VALUE's type. */
7423 18124794 : value = convert (type, value);
7424 : }
7425 102148220 : else if (TREE_CODE (type) == UNION_TYPE)
7426 : {
7427 137 : tree field;
7428 :
7429 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7430 185 : if (TREE_TYPE (field) != error_mark_node
7431 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7432 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7433 : break;
7434 :
7435 137 : if (field)
7436 : {
7437 130 : tree t;
7438 130 : bool maybe_const = true;
7439 :
7440 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7441 130 : t = c_fully_fold (value, false, &maybe_const);
7442 130 : t = build_constructor_single (type, field, t);
7443 130 : if (!maybe_const)
7444 15 : t = c_wrap_maybe_const (t, true);
7445 130 : t = digest_init (loc, field, type, t,
7446 : NULL_TREE, false, false, false, true, false, false);
7447 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7448 130 : return t;
7449 : }
7450 7 : error_at (loc, "cast to union type from type not present in union");
7451 7 : return error_mark_node;
7452 : }
7453 : else
7454 : {
7455 102148083 : tree otype, ovalue;
7456 :
7457 102148083 : if (type == void_type_node)
7458 : {
7459 18374 : tree t = build1 (CONVERT_EXPR, type, value);
7460 18374 : SET_EXPR_LOCATION (t, loc);
7461 18374 : return t;
7462 : }
7463 :
7464 102129709 : otype = TREE_TYPE (value);
7465 :
7466 : /* Optionally warn about potentially worrisome casts. */
7467 102129709 : if (warn_cast_qual
7468 179497 : && TREE_CODE (type) == POINTER_TYPE
7469 26463 : && TREE_CODE (otype) == POINTER_TYPE)
7470 13405 : handle_warn_cast_qual (loc, type, otype);
7471 :
7472 : /* Warn about conversions between pointers to disjoint
7473 : address spaces. */
7474 102129709 : if (TREE_CODE (type) == POINTER_TYPE
7475 3332107 : && TREE_CODE (otype) == POINTER_TYPE
7476 105117228 : && !null_pointer_constant_p (value))
7477 : {
7478 2933826 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7479 2933826 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7480 2933826 : addr_space_t as_common;
7481 :
7482 2933826 : if (!addr_space_superset (as_to, as_from, &as_common))
7483 : {
7484 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7485 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7486 : "from disjoint generic address space pointer",
7487 : c_addr_space_name (as_to));
7488 :
7489 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7490 0 : warning_at (loc, 0, "cast to generic address space pointer "
7491 : "from disjoint %qs address space pointer",
7492 : c_addr_space_name (as_from));
7493 :
7494 : else
7495 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7496 : "from disjoint %qs address space pointer",
7497 : c_addr_space_name (as_to),
7498 : c_addr_space_name (as_from));
7499 : }
7500 :
7501 : /* Warn of new allocations that are not big enough for the target
7502 : type. */
7503 2933826 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7504 805 : if (tree fndecl = get_callee_fndecl (value))
7505 801 : if (DECL_IS_MALLOC (fndecl))
7506 : {
7507 517 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7508 517 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7509 517 : if (alloc_size)
7510 159 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7511 : alloc_size);
7512 : }
7513 : }
7514 :
7515 : /* Warn about possible alignment problems. */
7516 102129709 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7517 7 : && TREE_CODE (type) == POINTER_TYPE
7518 7 : && TREE_CODE (otype) == POINTER_TYPE
7519 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7520 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7521 : /* Don't warn about opaque types, where the actual alignment
7522 : restriction is unknown. */
7523 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7524 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7525 102129723 : && min_align_of_type (TREE_TYPE (type))
7526 7 : > min_align_of_type (TREE_TYPE (otype)))
7527 5 : warning_at (loc, OPT_Wcast_align,
7528 : "cast increases required alignment of target type");
7529 :
7530 102129709 : if ((TREE_CODE (type) == INTEGER_TYPE
7531 102129709 : || TREE_CODE (type) == BITINT_TYPE)
7532 7088938 : && TREE_CODE (otype) == POINTER_TYPE
7533 102154949 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7534 : /* Unlike conversion of integers to pointers, where the
7535 : warning is disabled for converting constants because
7536 : of cases such as SIG_*, warn about converting constant
7537 : pointers to integers. In some cases it may cause unwanted
7538 : sign extension, and a warning is appropriate. */
7539 1450 : warning_at (loc, OPT_Wpointer_to_int_cast,
7540 : "cast from pointer to integer of different size");
7541 :
7542 102129709 : if ((TREE_CODE (value) == CALL_EXPR
7543 34674602 : && !is_access_with_size_p (value))
7544 136804309 : && TREE_CODE (type) != TREE_CODE (otype))
7545 2286 : warning_at (loc, OPT_Wbad_function_cast,
7546 : "cast from function call of type %qT "
7547 : "to non-matching type %qT", otype, type);
7548 :
7549 102129709 : if (TREE_CODE (type) == POINTER_TYPE
7550 3332107 : && (TREE_CODE (otype) == INTEGER_TYPE
7551 3332107 : || TREE_CODE (otype) == BITINT_TYPE)
7552 344502 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7553 : /* Don't warn about converting any constant. */
7554 102436646 : && !TREE_CONSTANT (value))
7555 407 : warning_at (loc,
7556 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7557 : "of different size");
7558 :
7559 102129709 : if (warn_strict_aliasing <= 2)
7560 93597686 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7561 :
7562 : /* If pedantic, warn for conversions between function and object
7563 : pointer types, except for converting a null pointer constant
7564 : to function pointer type. */
7565 102129709 : if (pedantic
7566 238922 : && TREE_CODE (type) == POINTER_TYPE
7567 144180 : && TREE_CODE (otype) == POINTER_TYPE
7568 2782 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7569 102129730 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7570 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7571 : "conversion of function pointer to object pointer type");
7572 :
7573 102129709 : if (pedantic
7574 238922 : && TREE_CODE (type) == POINTER_TYPE
7575 144180 : && TREE_CODE (otype) == POINTER_TYPE
7576 2782 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7577 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7578 102129719 : && !null_pointer_constant_p (value))
7579 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7580 : "conversion of object pointer to function pointer type");
7581 :
7582 102129709 : if (TREE_CODE (type) == POINTER_TYPE
7583 3332107 : && TREE_CODE (otype) == POINTER_TYPE
7584 2987519 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7585 8581 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7586 102137178 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7587 7469 : TREE_TYPE (otype)))
7588 799 : warning_at (loc, OPT_Wcast_function_type,
7589 : "cast between incompatible function types"
7590 : " from %qT to %qT", otype, type);
7591 :
7592 102129709 : ovalue = value;
7593 : /* If converting to boolean a value with integer operands that
7594 : is not itself represented as an INTEGER_CST, the call below
7595 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7596 : build_binary_op as called by c_common_truthvalue_conversion
7597 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7598 : subexpression has been fully folded. To avoid nested
7599 : C_MAYBE_CONST_EXPR, ensure that
7600 : c_objc_common_truthvalue_conversion receives an argument
7601 : properly marked as having integer operands in that case. */
7602 102129709 : if (int_operands
7603 6682299 : && TREE_CODE (value) != INTEGER_CST
7604 102129751 : && (TREE_CODE (type) == BOOLEAN_TYPE
7605 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7606 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7607 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7608 28 : value = note_integer_operands (value);
7609 102129709 : value = convert (type, value);
7610 :
7611 : /* Ignore any integer overflow caused by the cast. */
7612 102129709 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7613 : {
7614 6628787 : if (TREE_OVERFLOW_P (ovalue))
7615 : {
7616 9 : if (!TREE_OVERFLOW (value))
7617 : {
7618 : /* Avoid clobbering a shared constant. */
7619 0 : value = copy_node (value);
7620 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7621 : }
7622 : }
7623 6628778 : else if (TREE_OVERFLOW (value))
7624 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7625 3085 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7626 : }
7627 : }
7628 :
7629 : /* Don't let a cast be an lvalue. */
7630 120254503 : if (lvalue_p (value))
7631 90631 : value = non_lvalue_loc (loc, value);
7632 :
7633 : /* Don't allow the results of casting to floating-point or complex
7634 : types be confused with actual constants, or casts involving
7635 : integer and pointer types other than direct integer-to-integer
7636 : and integer-to-pointer be confused with integer constant
7637 : expressions and null pointer constants. */
7638 120254503 : if (TREE_CODE (value) == REAL_CST
7639 120204617 : || TREE_CODE (value) == COMPLEX_CST
7640 240436497 : || (TREE_CODE (value) == INTEGER_CST
7641 6877628 : && !((TREE_CODE (expr) == INTEGER_CST
7642 6804195 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7643 66030 : || TREE_CODE (expr) == REAL_CST
7644 : || TREE_CODE (expr) == COMPLEX_CST)))
7645 134172 : value = build1 (NOP_EXPR, type, value);
7646 :
7647 : /* If the expression has integer operands and so can occur in an
7648 : unevaluated part of an integer constant expression, ensure the
7649 : return value reflects this. */
7650 120254503 : if (int_operands
7651 6858646 : && INTEGRAL_TYPE_P (type)
7652 6414465 : && value != error_mark_node
7653 126668967 : && !EXPR_INT_CONST_OPERANDS (value))
7654 11 : value = note_integer_operands (value);
7655 :
7656 120254503 : protected_set_expr_location (value, loc);
7657 120254503 : return value;
7658 : }
7659 :
7660 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7661 : location of the open paren of the cast, or the position of the cast
7662 : expr. */
7663 : tree
7664 120273560 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7665 : {
7666 120273560 : tree type;
7667 120273560 : tree type_expr = NULL_TREE;
7668 120273560 : bool type_expr_const = true;
7669 120273560 : tree ret;
7670 120273560 : int saved_wsp = warn_strict_prototypes;
7671 :
7672 : /* This avoids warnings about unprototyped casts on
7673 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7674 120273560 : if (TREE_CODE (expr) == INTEGER_CST)
7675 6926369 : warn_strict_prototypes = 0;
7676 120273560 : type = groktypename (type_name, &type_expr, &type_expr_const);
7677 120273560 : warn_strict_prototypes = saved_wsp;
7678 :
7679 764934 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7680 121038240 : && reject_gcc_builtin (expr))
7681 2 : return error_mark_node;
7682 :
7683 120273558 : ret = build_c_cast (loc, type, expr);
7684 120273558 : if (ret == error_mark_node)
7685 : return error_mark_node;
7686 :
7687 120272396 : if (type_expr)
7688 : {
7689 249 : bool inner_expr_const = true;
7690 249 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7691 249 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7692 498 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7693 145 : && inner_expr_const);
7694 249 : SET_EXPR_LOCATION (ret, loc);
7695 : }
7696 :
7697 120272396 : if (!EXPR_HAS_LOCATION (ret))
7698 6804896 : protected_set_expr_location (ret, loc);
7699 :
7700 : /* C++ does not permits types to be defined in a cast, but it
7701 : allows references to incomplete types. */
7702 120272396 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7703 1 : warning_at (loc, OPT_Wc___compat,
7704 : "defining a type in a cast is invalid in C++");
7705 :
7706 : return ret;
7707 : }
7708 :
7709 : /* Build an assignment expression of lvalue LHS from value RHS.
7710 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7711 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7712 : MODIFYCODE is the code for a binary operator that we use
7713 : to combine the old value of LHS with RHS to get the new value.
7714 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7715 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7716 : which may differ from TREE_TYPE (RHS) for an enum value.
7717 :
7718 : LOCATION is the location of the MODIFYCODE operator.
7719 : RHS_LOC is the location of the RHS. */
7720 :
7721 : tree
7722 2900783 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7723 : enum tree_code modifycode,
7724 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7725 : {
7726 2900783 : tree result;
7727 2900783 : tree newrhs;
7728 2900783 : tree rhseval = NULL_TREE;
7729 2900783 : tree lhstype = TREE_TYPE (lhs);
7730 2900783 : tree olhstype = lhstype;
7731 2900783 : bool npc;
7732 2900783 : bool is_atomic_op;
7733 :
7734 : /* Types that aren't fully specified cannot be used in assignments. */
7735 2900783 : lhs = require_complete_type (location, lhs);
7736 2900783 : rhs = require_complete_type (location, rhs);
7737 :
7738 : /* Avoid duplicate error messages from operands that had errors. */
7739 2900783 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7740 473 : return error_mark_node;
7741 :
7742 : /* Ensure an error for assigning a non-lvalue array to an array in
7743 : C90. */
7744 2900310 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7745 : {
7746 1 : error_at (location, "assignment to expression with array type");
7747 1 : return error_mark_node;
7748 : }
7749 :
7750 : /* For ObjC properties, defer this check. */
7751 2900309 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7752 32 : return error_mark_node;
7753 :
7754 2900277 : is_atomic_op = really_atomic_lvalue (lhs);
7755 :
7756 2900277 : newrhs = rhs;
7757 :
7758 2900277 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7759 : {
7760 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7761 : lhs_origtype, modifycode, rhs_loc, rhs,
7762 : rhs_origtype);
7763 2 : if (inner == error_mark_node)
7764 : return error_mark_node;
7765 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7766 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7767 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7768 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7769 2 : protected_set_expr_location (result, location);
7770 2 : return result;
7771 : }
7772 :
7773 : /* If a binary op has been requested, combine the old LHS value with the RHS
7774 : producing the value we should actually store into the LHS. */
7775 :
7776 2900275 : if (modifycode != NOP_EXPR)
7777 : {
7778 215005 : lhs = c_fully_fold (lhs, false, NULL, true);
7779 215005 : lhs = stabilize_reference (lhs);
7780 :
7781 : /* Construct the RHS for any non-atomic compound assignemnt. */
7782 215005 : if (!is_atomic_op)
7783 : {
7784 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7785 : are preevaluated before the rest of the assignment expression's
7786 : side-effects, because RHS could contain e.g. function calls
7787 : that modify LHS. */
7788 199520 : if (TREE_SIDE_EFFECTS (rhs))
7789 : {
7790 8686 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7791 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7792 : else
7793 8658 : newrhs = save_expr (rhs);
7794 8686 : rhseval = newrhs;
7795 8686 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7796 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7797 : newrhs);
7798 : }
7799 199520 : bool clear_decl_read = false;
7800 88495 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7801 153331 : && !DECL_READ_P (lhs)
7802 251767 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7803 : : warn_unused_but_set_parameter) > 2)
7804 : {
7805 21550 : mark_exp_read (newrhs);
7806 21550 : if (!DECL_READ_P (lhs))
7807 199520 : clear_decl_read = true;
7808 : }
7809 :
7810 199520 : newrhs = build_binary_op (location, modifycode,
7811 : convert_lvalue_to_rvalue (location, lhs,
7812 : true, true),
7813 : newrhs, true);
7814 199520 : if (clear_decl_read)
7815 21550 : DECL_READ_P (lhs) = 0;
7816 :
7817 : /* The original type of the right hand side is no longer
7818 : meaningful. */
7819 : rhs_origtype = NULL_TREE;
7820 : }
7821 : }
7822 :
7823 2900275 : if (c_dialect_objc ())
7824 : {
7825 : /* Check if we are modifying an Objective-C property reference;
7826 : if so, we need to generate setter calls. */
7827 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7828 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7829 : else
7830 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7831 0 : if (result)
7832 0 : goto return_result;
7833 :
7834 : /* Else, do the check that we postponed for Objective-C. */
7835 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7836 0 : return error_mark_node;
7837 : }
7838 :
7839 : /* Give an error for storing in something that is 'const'. */
7840 :
7841 2900275 : if (TYPE_READONLY (lhstype)
7842 2900275 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7843 25768 : && C_TYPE_FIELDS_READONLY (lhstype)))
7844 : {
7845 88 : readonly_error (location, lhs, lv_assign);
7846 88 : return error_mark_node;
7847 : }
7848 2900187 : else if (TREE_READONLY (lhs))
7849 1 : readonly_warning (lhs, lv_assign);
7850 :
7851 : /* If storing into a structure or union member,
7852 : it has probably been given type `int'.
7853 : Compute the type that would go with
7854 : the actual amount of storage the member occupies. */
7855 :
7856 2900187 : if (TREE_CODE (lhs) == COMPONENT_REF
7857 248846 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7858 248846 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7859 109788 : || SCALAR_FLOAT_TYPE_P (lhstype)
7860 86473 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7861 169369 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7862 :
7863 : /* If storing in a field that is in actuality a short or narrower than one,
7864 : we must store in the field in its actual type. */
7865 :
7866 2900187 : if (lhstype != TREE_TYPE (lhs))
7867 : {
7868 0 : lhs = copy_node (lhs);
7869 0 : TREE_TYPE (lhs) = lhstype;
7870 : }
7871 :
7872 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7873 : when LHS does not have its original type. This happens for,
7874 : e.g., an enum bitfield in a struct. */
7875 2900187 : if (warn_cxx_compat
7876 3196 : && lhs_origtype != NULL_TREE
7877 3196 : && lhs_origtype != lhstype
7878 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7879 : {
7880 4 : tree checktype = (rhs_origtype != NULL_TREE
7881 4 : ? rhs_origtype
7882 0 : : TREE_TYPE (rhs));
7883 4 : if (checktype != error_mark_node
7884 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7885 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7886 2 : warning_at (location, OPT_Wc___compat,
7887 : "enum conversion in assignment is invalid in C++");
7888 : }
7889 :
7890 : /* Remove qualifiers. */
7891 2900187 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7892 2900187 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7893 :
7894 : /* Convert new value to destination type. Fold it first, then
7895 : restore any excess precision information, for the sake of
7896 : conversion warnings. */
7897 :
7898 2900187 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7899 : {
7900 2884702 : tree rhs_semantic_type = NULL_TREE;
7901 2884702 : if (!c_in_omp_for)
7902 : {
7903 2868381 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7904 : {
7905 5077 : rhs_semantic_type = TREE_TYPE (newrhs);
7906 5077 : newrhs = TREE_OPERAND (newrhs, 0);
7907 : }
7908 2868381 : npc = null_pointer_constant_p (newrhs);
7909 2868381 : newrhs = c_fully_fold (newrhs, false, NULL);
7910 2868381 : if (rhs_semantic_type)
7911 5077 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7912 : }
7913 : else
7914 16321 : npc = null_pointer_constant_p (newrhs);
7915 2884702 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7916 : rhs_origtype, ic_assign, npc,
7917 : NULL_TREE, NULL_TREE, 0);
7918 2884702 : if (TREE_CODE (newrhs) == ERROR_MARK)
7919 127 : return error_mark_node;
7920 : }
7921 :
7922 : /* Emit ObjC write barrier, if necessary. */
7923 2900060 : if (c_dialect_objc () && flag_objc_gc)
7924 : {
7925 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7926 0 : if (result)
7927 : {
7928 0 : protected_set_expr_location (result, location);
7929 0 : goto return_result;
7930 : }
7931 : }
7932 :
7933 : /* Scan operands. */
7934 :
7935 2900060 : if (is_atomic_op)
7936 25964 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7937 : else
7938 : {
7939 2874096 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7940 2874096 : TREE_SIDE_EFFECTS (result) = 1;
7941 2874096 : protected_set_expr_location (result, location);
7942 : }
7943 :
7944 : /* If we got the LHS in a different type for storing in,
7945 : convert the result back to the nominal type of LHS
7946 : so that the value we return always has the same type
7947 : as the LHS argument. */
7948 :
7949 2900060 : if (olhstype == TREE_TYPE (result))
7950 2900060 : goto return_result;
7951 :
7952 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7953 : rhs_origtype, ic_assign, false, NULL_TREE,
7954 : NULL_TREE, 0);
7955 0 : protected_set_expr_location (result, location);
7956 :
7957 2900060 : return_result:
7958 2900060 : if (rhseval)
7959 8686 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7960 : return result;
7961 : }
7962 :
7963 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7964 : This is used to implement -fplan9-extensions. */
7965 :
7966 : static bool
7967 26 : find_anonymous_field_with_type (tree struct_type, tree type)
7968 : {
7969 26 : tree field;
7970 26 : bool found;
7971 :
7972 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7973 26 : found = false;
7974 26 : for (field = TYPE_FIELDS (struct_type);
7975 68 : field != NULL_TREE;
7976 42 : field = TREE_CHAIN (field))
7977 : {
7978 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7979 42 : if (DECL_NAME (field) == NULL
7980 42 : && comptypes (type, fieldtype))
7981 : {
7982 4 : if (found)
7983 : return false;
7984 : found = true;
7985 : }
7986 38 : else if (DECL_NAME (field) == NULL
7987 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7988 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
7989 : {
7990 0 : if (found)
7991 : return false;
7992 : found = true;
7993 : }
7994 : }
7995 : return found;
7996 : }
7997 :
7998 : /* RHS is an expression whose type is pointer to struct. If there is
7999 : an anonymous field in RHS with type TYPE, then return a pointer to
8000 : that field in RHS. This is used with -fplan9-extensions. This
8001 : returns NULL if no conversion could be found. */
8002 :
8003 : static tree
8004 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
8005 : {
8006 28 : tree rhs_struct_type, lhs_main_type;
8007 28 : tree field, found_field;
8008 28 : bool found_sub_field;
8009 28 : tree ret;
8010 :
8011 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8012 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8013 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8014 :
8015 28 : gcc_assert (POINTER_TYPE_P (type));
8016 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8017 :
8018 28 : found_field = NULL_TREE;
8019 28 : found_sub_field = false;
8020 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8021 172 : field != NULL_TREE;
8022 144 : field = TREE_CHAIN (field))
8023 : {
8024 148 : if (DECL_NAME (field) != NULL_TREE
8025 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8026 96 : continue;
8027 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8028 52 : if (comptypes (lhs_main_type, fieldtype))
8029 : {
8030 28 : if (found_field != NULL_TREE)
8031 : return NULL_TREE;
8032 : found_field = field;
8033 : }
8034 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8035 : lhs_main_type))
8036 : {
8037 4 : if (found_field != NULL_TREE)
8038 : return NULL_TREE;
8039 : found_field = field;
8040 : found_sub_field = true;
8041 : }
8042 : }
8043 :
8044 24 : if (found_field == NULL_TREE)
8045 : return NULL_TREE;
8046 :
8047 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8048 : build_fold_indirect_ref (rhs), found_field,
8049 : NULL_TREE);
8050 24 : ret = build_fold_addr_expr_loc (location, ret);
8051 :
8052 24 : if (found_sub_field)
8053 : {
8054 2 : ret = convert_to_anonymous_field (location, type, ret);
8055 2 : gcc_assert (ret != NULL_TREE);
8056 : }
8057 :
8058 : return ret;
8059 : }
8060 :
8061 : /* Issue an error message for a bad initializer component.
8062 : GMSGID identifies the message.
8063 : The component name is taken from the spelling stack. */
8064 :
8065 : static void ATTRIBUTE_GCC_DIAG (2,0)
8066 944 : error_init (location_t loc, const char *gmsgid, ...)
8067 : {
8068 944 : char *ofwhat;
8069 :
8070 944 : auto_diagnostic_group d;
8071 :
8072 : /* The gmsgid may be a format string with %< and %>. */
8073 944 : va_list ap;
8074 944 : va_start (ap, gmsgid);
8075 944 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8076 944 : loc, -1, gmsgid, &ap);
8077 944 : va_end (ap);
8078 :
8079 944 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8080 944 : if (*ofwhat && warned)
8081 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8082 944 : }
8083 :
8084 : /* Used to implement pedwarn_init and permerror_init. */
8085 :
8086 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8087 2538 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8088 : va_list *ap, enum diagnostics::kind kind)
8089 : {
8090 : /* Use the location where a macro was expanded rather than where
8091 : it was defined to make sure macros defined in system headers
8092 : but used incorrectly elsewhere are diagnosed. */
8093 2538 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8094 2538 : auto_diagnostic_group d;
8095 2538 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8096 2538 : if (warned)
8097 : {
8098 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8099 1815 : if (*ofwhat)
8100 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8101 : }
8102 5076 : return warned;
8103 2538 : }
8104 :
8105 : /* Issue a pedantic warning for a bad initializer component. OPT is
8106 : the option OPT_* (from options.h) controlling this warning or 0 if
8107 : it is unconditionally given. GMSGID identifies the message. The
8108 : component name is taken from the spelling stack. */
8109 :
8110 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8111 2187 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8112 : {
8113 2187 : va_list ap;
8114 2187 : va_start (ap, gmsgid);
8115 2187 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8116 : diagnostics::kind::pedwarn);
8117 2187 : va_end (ap);
8118 2187 : return warned;
8119 : }
8120 :
8121 : /* Like pedwarn_init, but issue a permerror. */
8122 :
8123 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8124 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8125 : {
8126 351 : va_list ap;
8127 351 : va_start (ap, gmsgid);
8128 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8129 : diagnostics::kind::permerror);
8130 351 : va_end (ap);
8131 351 : return warned;
8132 : }
8133 :
8134 : /* Issue a warning for a bad initializer component.
8135 :
8136 : OPT is the OPT_W* value corresponding to the warning option that
8137 : controls this warning. GMSGID identifies the message. The
8138 : component name is taken from the spelling stack. */
8139 :
8140 : static void
8141 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8142 : {
8143 146 : char *ofwhat;
8144 146 : bool warned;
8145 :
8146 146 : auto_diagnostic_group d;
8147 :
8148 : /* Use the location where a macro was expanded rather than where
8149 : it was defined to make sure macros defined in system headers
8150 : but used incorrectly elsewhere are diagnosed. */
8151 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8152 :
8153 : /* The gmsgid may be a format string with %< and %>. */
8154 146 : warned = warning_at (exploc, opt, gmsgid);
8155 146 : if (warned)
8156 : {
8157 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8158 133 : if (*ofwhat)
8159 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8160 : }
8161 146 : }
8162 :
8163 : /* If TYPE is an array type and EXPR is a parenthesized string
8164 : constant, warn if pedantic that EXPR is being used to initialize an
8165 : object of type TYPE. */
8166 :
8167 : void
8168 7281792 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8169 : {
8170 7281792 : if (pedantic
8171 80456 : && TREE_CODE (type) == ARRAY_TYPE
8172 1145 : && TREE_CODE (expr.value) == STRING_CST
8173 472 : && expr.original_code != STRING_CST)
8174 13 : pedwarn_init (loc, OPT_Wpedantic,
8175 : "array initialized from parenthesized string constant");
8176 7281792 : }
8177 :
8178 : /* Attempt to locate the parameter with the given index within FNDECL,
8179 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8180 :
8181 : static location_t
8182 937 : get_fndecl_argument_location (tree fndecl, int argnum)
8183 : {
8184 937 : int i;
8185 937 : tree param;
8186 :
8187 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8188 937 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8189 1016 : i < argnum && param;
8190 79 : i++, param = TREE_CHAIN (param))
8191 : ;
8192 :
8193 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8194 : return DECL_SOURCE_LOCATION (FNDECL). */
8195 937 : if (param == NULL)
8196 653 : return DECL_SOURCE_LOCATION (fndecl);
8197 :
8198 284 : return DECL_SOURCE_LOCATION (param);
8199 : }
8200 :
8201 : /* Issue a note about a mismatching argument for parameter PARMNUM
8202 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8203 : Attempt to issue the note at the pertinent parameter of the decl;
8204 : failing that issue it at the location of FUNDECL; failing that
8205 : issue it at PLOC.
8206 : Use highlight_colors::actual for the ACTUAL_TYPE
8207 : and highlight_colors::expected for EXPECTED_TYPE and the
8208 : parameter of FUNDECL*/
8209 :
8210 : static void
8211 1061 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8212 : tree expected_type, tree actual_type)
8213 : {
8214 1061 : location_t loc;
8215 1061 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8216 937 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8217 : else
8218 : loc = ploc;
8219 :
8220 1061 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8221 :
8222 1061 : pp_markup::element_expected_type elem_expected_type (expected_type);
8223 1061 : pp_markup::element_actual_type elem_actual_type (actual_type);
8224 1061 : inform (&richloc,
8225 : "expected %e but argument is of type %e",
8226 : &elem_expected_type, &elem_actual_type);
8227 1061 : }
8228 :
8229 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8230 : function FUNDECL declared without prototype to parameter PARMNUM of
8231 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8232 :
8233 : static void
8234 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8235 : tree parmtype, tree argtype)
8236 : {
8237 418 : tree_code parmcode = TREE_CODE (parmtype);
8238 418 : tree_code argcode = TREE_CODE (argtype);
8239 418 : tree promoted = c_type_promotes_to (argtype);
8240 :
8241 : /* Avoid warning for enum arguments that promote to an integer type
8242 : of the same size/mode. */
8243 418 : if (parmcode == INTEGER_TYPE
8244 418 : && argcode == ENUMERAL_TYPE
8245 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8246 : return;
8247 :
8248 417 : if ((parmcode == argcode
8249 222 : || (parmcode == INTEGER_TYPE
8250 : && argcode == ENUMERAL_TYPE))
8251 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8252 : return;
8253 :
8254 : /* This diagnoses even signed/unsigned mismatches. Those might be
8255 : safe in many cases but GCC may emit suboptimal code for them so
8256 : warning on those cases drives efficiency improvements. */
8257 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8258 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8259 : ? G_("%qD argument %d type is %qT where %qT is expected "
8260 : "in a call to built-in function declared without "
8261 : "prototype")
8262 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8263 : "in a call to built-in function declared without "
8264 : "prototype"),
8265 : fundecl, parmnum, promoted, parmtype))
8266 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8267 : "built-in %qD declared here",
8268 : fundecl);
8269 : }
8270 :
8271 : /* Print a declaration in quotes, with the given highlight_color.
8272 : Analogous to handler for %qD, but with a specific highlight color. */
8273 :
8274 199 : class pp_element_quoted_decl : public pp_element
8275 : {
8276 : public:
8277 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8278 199 : : m_decl (decl),
8279 199 : m_highlight_color (highlight_color)
8280 : {
8281 : }
8282 :
8283 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8284 : {
8285 199 : ctxt.begin_quote ();
8286 199 : ctxt.begin_highlight_color (m_highlight_color);
8287 :
8288 199 : print_decl (ctxt);
8289 :
8290 199 : ctxt.end_highlight_color ();
8291 199 : ctxt.end_quote ();
8292 199 : }
8293 :
8294 199 : void print_decl (pp_markup::context &ctxt)
8295 : {
8296 199 : pretty_printer *const pp = &ctxt.m_pp;
8297 199 : pp->set_padding (pp_none);
8298 199 : if (DECL_NAME (m_decl))
8299 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8300 : else
8301 0 : pp_string (pp, _("({anonymous})"));
8302 199 : }
8303 :
8304 : private:
8305 : tree m_decl;
8306 : const char *m_highlight_color;
8307 : };
8308 :
8309 : /* If TYPE is from a typedef, issue a note showing the location
8310 : to the user.
8311 : Use HIGHLIGHT_COLOR as the highlight color. */
8312 :
8313 : static void
8314 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8315 : {
8316 1178 : if (!typedef_variant_p (type))
8317 1144 : return;
8318 :
8319 34 : tree typedef_decl = TYPE_NAME (type);
8320 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8321 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8322 34 : nullptr, highlight_color);
8323 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8324 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8325 34 : }
8326 :
8327 : /* Convert value RHS to type TYPE as preparation for an assignment to
8328 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8329 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8330 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8331 : constant before any folding.
8332 : The real work of conversion is done by `convert'.
8333 : The purpose of this function is to generate error messages
8334 : for assignments that are not allowed in C.
8335 : ERRTYPE says whether it is argument passing, assignment,
8336 : initialization or return.
8337 :
8338 : In the following example, '~' denotes where EXPR_LOC and '^' where
8339 : LOCATION point to:
8340 :
8341 : f (var); [ic_argpass]
8342 : ^ ~~~
8343 : x = var; [ic_assign]
8344 : ^ ~~~;
8345 : int x = var; [ic_init]
8346 : ^^^
8347 : return x; [ic_return]
8348 : ^
8349 :
8350 : FUNCTION is a tree for the function being called.
8351 : PARMNUM is the number of the argument, for printing in error messages.
8352 : WARNOPT may be set to a warning option to issue the corresponding warning
8353 : rather than an error for invalid conversions. Used for calls to built-in
8354 : functions declared without a prototype. */
8355 :
8356 : static tree
8357 167076210 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8358 : tree rhs, tree origtype, enum impl_conv errtype,
8359 : bool null_pointer_constant, tree fundecl,
8360 : tree function, int parmnum, int warnopt /* = 0 */)
8361 : {
8362 167076210 : enum tree_code codel = TREE_CODE (type);
8363 167076210 : tree orig_rhs = rhs;
8364 167076210 : tree rhstype;
8365 167076210 : enum tree_code coder;
8366 167076210 : tree rname = NULL_TREE;
8367 167076210 : bool objc_ok = false;
8368 :
8369 : /* Use the expansion point location to handle cases such as user's
8370 : function returning a wrong-type macro defined in a system header. */
8371 167076210 : location = expansion_point_location_if_in_system_header (location);
8372 :
8373 167076210 : if (errtype == ic_argpass)
8374 : {
8375 125629114 : tree selector;
8376 : /* Change pointer to function to the function itself for
8377 : diagnostics. */
8378 125629114 : if (TREE_CODE (function) == ADDR_EXPR
8379 125629114 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8380 0 : function = TREE_OPERAND (function, 0);
8381 :
8382 : /* Handle an ObjC selector specially for diagnostics. */
8383 125629114 : selector = objc_message_selector ();
8384 125629114 : rname = function;
8385 125629114 : if (selector && parmnum > 2)
8386 : {
8387 0 : rname = selector;
8388 0 : parmnum -= 2;
8389 : }
8390 : }
8391 :
8392 : /* This macro is used to emit diagnostics to ensure that all format
8393 : strings are complete sentences, visible to gettext and checked at
8394 : compile time. */
8395 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8396 : do { \
8397 : switch (errtype) \
8398 : { \
8399 : case ic_argpass: \
8400 : { \
8401 : auto_diagnostic_group d; \
8402 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8403 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8404 : } \
8405 : break; \
8406 : case ic_assign: \
8407 : pedwarn (LOCATION, OPT, AS); \
8408 : break; \
8409 : case ic_init: \
8410 : case ic_init_const: \
8411 : pedwarn_init (LOCATION, OPT, IN); \
8412 : break; \
8413 : case ic_return: \
8414 : pedwarn (LOCATION, OPT, RE); \
8415 : break; \
8416 : default: \
8417 : gcc_unreachable (); \
8418 : } \
8419 : } while (0)
8420 :
8421 : /* This macro is used to emit diagnostics to ensure that all format
8422 : strings are complete sentences, visible to gettext and checked at
8423 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8424 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8425 : do { \
8426 : switch (errtype) \
8427 : { \
8428 : case ic_argpass: \
8429 : { \
8430 : auto_diagnostic_group d; \
8431 : if (PEDWARN) { \
8432 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8433 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8434 : } else { \
8435 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8436 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8437 : } \
8438 : } \
8439 : break; \
8440 : case ic_assign: \
8441 : if (PEDWARN) \
8442 : pedwarn (LOCATION, OPT, AS, QUALS); \
8443 : else \
8444 : warning_at (LOCATION, OPT, AS, QUALS); \
8445 : break; \
8446 : case ic_init: \
8447 : case ic_init_const: \
8448 : if (PEDWARN) \
8449 : pedwarn (LOCATION, OPT, IN, QUALS); \
8450 : else \
8451 : warning_at (LOCATION, OPT, IN, QUALS); \
8452 : break; \
8453 : case ic_return: \
8454 : if (PEDWARN) \
8455 : pedwarn (LOCATION, OPT, RE, QUALS); \
8456 : else \
8457 : warning_at (LOCATION, OPT, RE, QUALS); \
8458 : break; \
8459 : default: \
8460 : gcc_unreachable (); \
8461 : } \
8462 : } while (0)
8463 :
8464 : /* This macro is used to emit diagnostics to ensure that all format
8465 : strings are complete sentences, visible to gettext and checked at
8466 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8467 : extra parameter to enumerate qualifiers. */
8468 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8469 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8470 :
8471 :
8472 167076210 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8473 5975 : rhs = TREE_OPERAND (rhs, 0);
8474 :
8475 167076210 : rhstype = TREE_TYPE (rhs);
8476 167076210 : coder = TREE_CODE (rhstype);
8477 :
8478 167076210 : if (coder == ERROR_MARK)
8479 346 : return error_mark_node;
8480 :
8481 167075864 : if (c_dialect_objc ())
8482 : {
8483 0 : int parmno;
8484 :
8485 0 : switch (errtype)
8486 : {
8487 : case ic_return:
8488 : parmno = 0;
8489 : break;
8490 :
8491 : case ic_assign:
8492 : parmno = -1;
8493 : break;
8494 :
8495 : case ic_init:
8496 : case ic_init_const:
8497 : parmno = -2;
8498 : break;
8499 :
8500 : default:
8501 0 : parmno = parmnum;
8502 : break;
8503 : }
8504 :
8505 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8506 : }
8507 :
8508 167075864 : if (warn_cxx_compat)
8509 : {
8510 28859 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8511 28859 : if (checktype != error_mark_node
8512 28859 : && TREE_CODE (type) == ENUMERAL_TYPE
8513 28965 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8514 45 : switch (errtype)
8515 : {
8516 8 : case ic_argpass:
8517 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8518 : "passing argument %d of %qE is invalid in C++",
8519 : parmnum, rname))
8520 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8521 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8522 : "expected %qT but argument is of type %qT",
8523 : type, rhstype);
8524 : break;
8525 10 : case ic_assign:
8526 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8527 : "%qT in assignment is invalid in C++", rhstype, type);
8528 10 : break;
8529 18 : case ic_init:
8530 18 : case ic_init_const:
8531 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8532 : "%qT to %qT in initialization is invalid in C++",
8533 : rhstype, type);
8534 18 : break;
8535 9 : case ic_return:
8536 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8537 : "%qT in return is invalid in C++", rhstype, type);
8538 9 : break;
8539 0 : default:
8540 0 : gcc_unreachable ();
8541 : }
8542 : }
8543 :
8544 167075864 : if (warn_enum_conversion)
8545 : {
8546 14586706 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8547 14586706 : if (checktype != error_mark_node
8548 14586706 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8549 12709 : && TREE_CODE (type) == ENUMERAL_TYPE
8550 14594902 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8551 : {
8552 7 : gcc_rich_location loc (location);
8553 7 : warning_at (&loc, OPT_Wenum_conversion,
8554 : "implicit conversion from %qT to %qT",
8555 : checktype, type);
8556 7 : }
8557 : }
8558 :
8559 167075864 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8560 : {
8561 147976636 : warn_for_address_of_packed_member (type, orig_rhs);
8562 147976636 : if (type != rhstype)
8563 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8564 36869266 : rhs = convert (type, rhs);
8565 147976636 : return rhs;
8566 : }
8567 :
8568 19099228 : if (coder == VOID_TYPE)
8569 : {
8570 : /* Except for passing an argument to an unprototyped function,
8571 : this is a constraint violation. When passing an argument to
8572 : an unprototyped function, it is compile-time undefined;
8573 : making it a constraint in that case was rejected in
8574 : DR#252. */
8575 8 : const char msg[] = "void value not ignored as it ought to be";
8576 8 : if (warnopt)
8577 0 : warning_at (location, warnopt, msg);
8578 : else
8579 8 : error_at (location, msg);
8580 8 : return error_mark_node;
8581 : }
8582 19099220 : rhs = require_complete_type (location, rhs);
8583 19099220 : if (rhs == error_mark_node)
8584 : return error_mark_node;
8585 :
8586 19099220 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8587 6 : return error_mark_node;
8588 :
8589 : /* A non-reference type can convert to a reference. This handles
8590 : va_start, va_copy and possibly port built-ins. */
8591 19099214 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8592 : {
8593 286 : if (!lvalue_p (rhs))
8594 : {
8595 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8596 0 : if (warnopt)
8597 0 : warning_at (location, warnopt, msg);
8598 : else
8599 0 : error_at (location, msg);
8600 0 : return error_mark_node;
8601 : }
8602 286 : if (!c_mark_addressable (rhs))
8603 0 : return error_mark_node;
8604 286 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8605 286 : SET_EXPR_LOCATION (rhs, location);
8606 :
8607 286 : rhs = convert_for_assignment (location, expr_loc,
8608 286 : c_build_pointer_type (TREE_TYPE (type)),
8609 : rhs, origtype, errtype,
8610 : null_pointer_constant, fundecl, function,
8611 : parmnum, warnopt);
8612 286 : if (rhs == error_mark_node)
8613 : return error_mark_node;
8614 :
8615 286 : rhs = build1 (NOP_EXPR, type, rhs);
8616 286 : SET_EXPR_LOCATION (rhs, location);
8617 286 : return rhs;
8618 : }
8619 : /* Some types can interconvert without explicit casts. */
8620 19098928 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8621 19098928 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8622 9288985 : return convert (type, rhs);
8623 : /* Arithmetic types all interconvert, and enum is treated like int. */
8624 9809943 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8625 2274806 : || codel == FIXED_POINT_TYPE
8626 2274806 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8627 2188569 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8628 7713309 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8629 82935 : || coder == FIXED_POINT_TYPE
8630 82935 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8631 39870 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8632 : {
8633 7712190 : if (warnopt && errtype == ic_argpass)
8634 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8635 : rhstype);
8636 :
8637 7712190 : bool save = in_late_binary_op;
8638 7681153 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8639 15337155 : || (coder == REAL_TYPE
8640 276192 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8641 24196 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8642 93969 : in_late_binary_op = true;
8643 11090064 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8644 : ? expr_loc : location, type, orig_rhs,
8645 : errtype == ic_init_const);
8646 7712190 : in_late_binary_op = save;
8647 7712190 : return ret;
8648 : }
8649 :
8650 : /* Aggregates in different TUs might need conversion. */
8651 2097753 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8652 105 : && codel == coder
8653 2097775 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8654 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8655 9 : ? expr_loc : location, type, rhs);
8656 :
8657 : /* Conversion to a transparent union or record from its member types.
8658 : This applies only to function arguments. */
8659 2097744 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8660 96 : && TYPE_TRANSPARENT_AGGR (type))
8661 2097783 : && errtype == ic_argpass)
8662 : {
8663 39 : tree memb, marginal_memb = NULL_TREE;
8664 :
8665 50 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8666 : {
8667 50 : tree memb_type = TREE_TYPE (memb);
8668 :
8669 50 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8670 50 : TYPE_MAIN_VARIANT (rhstype)))
8671 : break;
8672 :
8673 27 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8674 1 : continue;
8675 :
8676 26 : if (coder == POINTER_TYPE)
8677 : {
8678 26 : tree ttl = TREE_TYPE (memb_type);
8679 26 : tree ttr = TREE_TYPE (rhstype);
8680 :
8681 : /* Any non-function converts to a [const][volatile] void *
8682 : and vice versa; otherwise, targets must be the same.
8683 : Meanwhile, the lhs target must have all the qualifiers of
8684 : the rhs. */
8685 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8686 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8687 45 : || comp_target_types (location, memb_type, rhstype))
8688 : {
8689 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8690 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8691 : /* If this type won't generate any warnings, use it. */
8692 16 : if (lquals == rquals
8693 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8694 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8695 0 : ? ((lquals | rquals) == rquals)
8696 16 : : ((lquals | rquals) == lquals)))
8697 : break;
8698 :
8699 : /* Keep looking for a better type, but remember this one. */
8700 0 : if (!marginal_memb)
8701 10 : marginal_memb = memb;
8702 : }
8703 : }
8704 :
8705 : /* Can convert integer zero to any pointer type. */
8706 10 : if (null_pointer_constant)
8707 : {
8708 0 : rhs = null_pointer_node;
8709 0 : break;
8710 : }
8711 : }
8712 :
8713 39 : if (memb || marginal_memb)
8714 : {
8715 39 : if (!memb)
8716 : {
8717 : /* We have only a marginally acceptable member type;
8718 : it needs a warning. */
8719 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8720 0 : tree ttr = TREE_TYPE (rhstype);
8721 :
8722 : /* Const and volatile mean something different for function
8723 : types, so the usual warnings are not appropriate. */
8724 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8725 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8726 : {
8727 : /* Because const and volatile on functions are
8728 : restrictions that say the function will not do
8729 : certain things, it is okay to use a const or volatile
8730 : function where an ordinary one is wanted, but not
8731 : vice-versa. */
8732 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8733 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8734 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8735 : OPT_Wdiscarded_qualifiers,
8736 : G_("passing argument %d of %qE "
8737 : "makes %q#v qualified function "
8738 : "pointer from unqualified"),
8739 : G_("assignment makes %q#v qualified "
8740 : "function pointer from "
8741 : "unqualified"),
8742 : G_("initialization makes %q#v qualified "
8743 : "function pointer from "
8744 : "unqualified"),
8745 : G_("return makes %q#v qualified function "
8746 : "pointer from unqualified"),
8747 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8748 : }
8749 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8750 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8751 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8752 : OPT_Wdiscarded_qualifiers,
8753 : G_("passing argument %d of %qE discards "
8754 : "%qv qualifier from pointer target type"),
8755 : G_("assignment discards %qv qualifier "
8756 : "from pointer target type"),
8757 : G_("initialization discards %qv qualifier "
8758 : "from pointer target type"),
8759 : G_("return discards %qv qualifier from "
8760 : "pointer target type"),
8761 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8762 :
8763 : memb = marginal_memb;
8764 : }
8765 :
8766 39 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8767 31 : pedwarn (location, OPT_Wpedantic,
8768 : "ISO C prohibits argument conversion to union type");
8769 :
8770 39 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8771 39 : return build_constructor_single (type, memb, rhs);
8772 : }
8773 : }
8774 :
8775 : /* Conversions among pointers */
8776 2097705 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8777 2096430 : && (coder == codel))
8778 : {
8779 : /* If RHS refers to a built-in declared without a prototype
8780 : BLTIN is the declaration of the built-in with a prototype
8781 : and RHSTYPE is set to the actual type of the built-in. */
8782 2018080 : tree bltin;
8783 2018080 : rhstype = type_or_builtin_type (rhs, &bltin);
8784 :
8785 2018080 : tree ttl = TREE_TYPE (type);
8786 2018080 : tree ttr = TREE_TYPE (rhstype);
8787 2018080 : bool is_opaque_pointer;
8788 2018080 : bool target_cmp = false; /* Cache comp_target_types () result. */
8789 2018080 : addr_space_t asl;
8790 2018080 : addr_space_t asr;
8791 :
8792 2018080 : tree mvl = remove_qualifiers (ttl);
8793 2018080 : tree mvr = remove_qualifiers (ttr);
8794 :
8795 : /* Opaque pointers are treated like void pointers. */
8796 2018080 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8797 :
8798 : /* The Plan 9 compiler permits a pointer to a struct to be
8799 : automatically converted into a pointer to an anonymous field
8800 : within the struct. */
8801 2018080 : if (flag_plan9_extensions
8802 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8803 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8804 26 : && mvl != mvr)
8805 : {
8806 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8807 26 : if (new_rhs != NULL_TREE)
8808 : {
8809 22 : rhs = new_rhs;
8810 22 : rhstype = TREE_TYPE (rhs);
8811 22 : coder = TREE_CODE (rhstype);
8812 22 : ttr = TREE_TYPE (rhstype);
8813 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8814 : }
8815 : }
8816 :
8817 : /* C++ does not allow the implicit conversion void* -> T*. However,
8818 : for the purpose of reducing the number of false positives, we
8819 : tolerate the special case of
8820 :
8821 : int *p = NULL;
8822 :
8823 : where NULL is typically defined in C to be '(void *) 0'. */
8824 2018080 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8825 26832 : warning_at (errtype == ic_argpass ? expr_loc : location,
8826 14226 : OPT_Wc___compat,
8827 : "request for implicit conversion "
8828 : "from %qT to %qT not permitted in C++", rhstype, type);
8829 :
8830 : /* Warn of new allocations that are not big enough for the target
8831 : type. */
8832 2018080 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8833 5455 : if (tree fndecl = get_callee_fndecl (rhs))
8834 5455 : if (DECL_IS_MALLOC (fndecl))
8835 : {
8836 4601 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8837 4601 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8838 4601 : if (alloc_size)
8839 447 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8840 : }
8841 :
8842 : /* See if the pointers point to incompatible address spaces. */
8843 2018080 : asl = TYPE_ADDR_SPACE (ttl);
8844 2018080 : asr = TYPE_ADDR_SPACE (ttr);
8845 2018080 : if (!null_pointer_constant_p (rhs)
8846 2018080 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8847 : {
8848 3 : auto_diagnostic_group d;
8849 3 : bool diagnosed = true;
8850 3 : switch (errtype)
8851 : {
8852 1 : case ic_argpass:
8853 1 : {
8854 1 : const char msg[] = G_("passing argument %d of %qE from "
8855 : "pointer to non-enclosed address space");
8856 1 : if (warnopt)
8857 0 : diagnosed
8858 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8859 : else
8860 1 : error_at (expr_loc, msg, parmnum, rname);
8861 0 : break;
8862 : }
8863 0 : case ic_assign:
8864 0 : {
8865 0 : const char msg[] = G_("assignment from pointer to "
8866 : "non-enclosed address space");
8867 0 : if (warnopt)
8868 0 : diagnosed = warning_at (location, warnopt, msg);
8869 : else
8870 0 : error_at (location, msg);
8871 0 : break;
8872 : }
8873 0 : case ic_init:
8874 0 : case ic_init_const:
8875 0 : {
8876 0 : const char msg[] = G_("initialization from pointer to "
8877 : "non-enclosed address space");
8878 0 : if (warnopt)
8879 0 : diagnosed = warning_at (location, warnopt, msg);
8880 : else
8881 0 : error_at (location, msg);
8882 0 : break;
8883 : }
8884 2 : case ic_return:
8885 2 : {
8886 2 : const char msg[] = G_("return from pointer to "
8887 : "non-enclosed address space");
8888 2 : if (warnopt)
8889 0 : diagnosed = warning_at (location, warnopt, msg);
8890 : else
8891 2 : error_at (location, msg);
8892 0 : break;
8893 : }
8894 0 : default:
8895 0 : gcc_unreachable ();
8896 : }
8897 3 : if (diagnosed)
8898 : {
8899 3 : if (errtype == ic_argpass)
8900 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8901 : else
8902 2 : inform (location, "expected %qT but pointer is of type %qT",
8903 : type, rhstype);
8904 : }
8905 3 : return error_mark_node;
8906 3 : }
8907 :
8908 : /* Check if the right-hand side has a format attribute but the
8909 : left-hand side doesn't. */
8910 2018077 : if (warn_suggest_attribute_format
8911 2018077 : && check_missing_format_attribute (type, rhstype))
8912 : {
8913 16 : switch (errtype)
8914 : {
8915 4 : case ic_argpass:
8916 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8917 : "argument %d of %qE might be "
8918 : "a candidate for a format attribute",
8919 : parmnum, rname);
8920 4 : break;
8921 4 : case ic_assign:
8922 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8923 : "assignment left-hand side might be "
8924 : "a candidate for a format attribute");
8925 4 : break;
8926 4 : case ic_init:
8927 4 : case ic_init_const:
8928 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8929 : "initialization left-hand side might be "
8930 : "a candidate for a format attribute");
8931 4 : break;
8932 4 : case ic_return:
8933 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8934 : "return type might be "
8935 : "a candidate for a format attribute");
8936 4 : break;
8937 0 : default:
8938 0 : gcc_unreachable ();
8939 : }
8940 : }
8941 :
8942 : /* See if the pointers point to incompatible scalar storage orders. */
8943 2018077 : if (warn_scalar_storage_order
8944 2015161 : && !null_pointer_constant_p (rhs)
8945 5982013 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8946 1981968 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8947 : {
8948 18 : tree t;
8949 :
8950 18 : switch (errtype)
8951 : {
8952 11 : case ic_argpass:
8953 : /* Do not warn for built-in functions, for example memcpy, since we
8954 : control how they behave and they can be useful in this area. */
8955 11 : if (TREE_CODE (rname) != FUNCTION_DECL
8956 11 : || !fndecl_built_in_p (rname))
8957 1 : warning_at (location, OPT_Wscalar_storage_order,
8958 : "passing argument %d of %qE from incompatible "
8959 : "scalar storage order", parmnum, rname);
8960 : break;
8961 3 : case ic_assign:
8962 : /* Do not warn if the RHS is a call to a function that returns a
8963 : pointer that is not an alias. */
8964 3 : if (TREE_CODE (rhs) != CALL_EXPR
8965 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8966 5 : || !DECL_IS_MALLOC (t))
8967 1 : warning_at (location, OPT_Wscalar_storage_order,
8968 : "assignment to %qT from pointer type %qT with "
8969 : "incompatible scalar storage order", type, rhstype);
8970 : break;
8971 3 : case ic_init:
8972 3 : case ic_init_const:
8973 : /* Likewise. */
8974 3 : if (TREE_CODE (rhs) != CALL_EXPR
8975 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8976 5 : || !DECL_IS_MALLOC (t))
8977 1 : warning_at (location, OPT_Wscalar_storage_order,
8978 : "initialization of %qT from pointer type %qT with "
8979 : "incompatible scalar storage order", type, rhstype);
8980 : break;
8981 1 : case ic_return:
8982 1 : warning_at (location, OPT_Wscalar_storage_order,
8983 : "returning %qT from pointer type with incompatible "
8984 : "scalar storage order %qT", rhstype, type);
8985 1 : break;
8986 0 : default:
8987 0 : gcc_unreachable ();
8988 : }
8989 : }
8990 :
8991 : /* Any non-function converts to a [const][volatile] void *
8992 : and vice versa; otherwise, targets must be the same.
8993 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8994 423085 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8995 1594993 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8996 1547711 : || (target_cmp = comp_target_types (location, type, rhstype))
8997 2448 : || is_opaque_pointer
8998 2022973 : || ((c_common_unsigned_type (mvl)
8999 2448 : == c_common_unsigned_type (mvr))
9000 3344 : && (c_common_signed_type (mvl)
9001 1672 : == c_common_signed_type (mvr))
9002 1657 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
9003 : {
9004 : /* Warn about loss of qualifers from pointers to arrays with
9005 : qualifiers on the element type. */
9006 2017278 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9007 : {
9008 2131 : ttr = strip_array_types (ttr);
9009 2131 : ttl = strip_array_types (ttl);
9010 :
9011 2131 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9012 2131 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9013 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9014 : location, expr_loc,
9015 : OPT_Wdiscarded_array_qualifiers,
9016 : G_("passing argument %d of %qE discards "
9017 : "%qv qualifier from pointer target type"),
9018 : G_("assignment discards %qv qualifier "
9019 : "from pointer target type"),
9020 : G_("initialization discards %qv qualifier "
9021 : "from pointer target type"),
9022 : G_("return discards %qv qualifier from "
9023 : "pointer target type"),
9024 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9025 : }
9026 2015147 : else if (pedantic
9027 146175 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9028 146155 : ||
9029 : (VOID_TYPE_P (ttr)
9030 2952 : && !null_pointer_constant
9031 178 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9032 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9033 : G_("ISO C forbids passing argument %d of "
9034 : "%qE between function pointer "
9035 : "and %<void *%>"),
9036 : G_("ISO C forbids assignment between "
9037 : "function pointer and %<void *%>"),
9038 : G_("ISO C forbids initialization between "
9039 : "function pointer and %<void *%>"),
9040 : G_("ISO C forbids return between function "
9041 : "pointer and %<void *%>"));
9042 : /* Const and volatile mean something different for function types,
9043 : so the usual warnings are not appropriate. */
9044 2015113 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9045 1982695 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9046 : {
9047 : /* Assignments between atomic and non-atomic objects are OK. */
9048 1981998 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9049 1981998 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9050 1981998 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9051 1981998 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9052 :
9053 : /* Don't warn about loss of qualifier for conversions from
9054 : qualified void* to pointers to arrays with corresponding
9055 : qualifier on the element type (except for pedantic before C23). */
9056 1981998 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9057 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9058 : OPT_Wdiscarded_qualifiers,
9059 : G_("passing argument %d of %qE discards "
9060 : "%qv qualifier from pointer target type"),
9061 : G_("assignment discards %qv qualifier "
9062 : "from pointer target type"),
9063 : G_("initialization discards %qv qualifier "
9064 : "from pointer target type"),
9065 : G_("return discards %qv qualifier from "
9066 : "pointer target type"),
9067 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9068 11 : else if (warn_quals_ped)
9069 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9070 : "array with qualifier on the element is not qualified before C23");
9071 :
9072 : /* If this is not a case of ignoring a mismatch in signedness,
9073 : no warning. */
9074 1981290 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9075 1542784 : || target_cmp)
9076 : ;
9077 : /* If there is a mismatch, do warn. */
9078 1649 : else if (warn_pointer_sign)
9079 82 : switch (errtype)
9080 : {
9081 33 : case ic_argpass:
9082 33 : {
9083 33 : auto_diagnostic_group d;
9084 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9085 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9086 33 : highlight_colors::actual);
9087 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9088 : "pointer targets in passing argument %d of "
9089 : "%qE differ in signedness", parmnum, rname))
9090 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9091 : rhstype);
9092 33 : }
9093 33 : break;
9094 21 : case ic_assign:
9095 21 : pedwarn (location, OPT_Wpointer_sign,
9096 : "pointer targets in assignment from %qT to %qT "
9097 : "differ in signedness", rhstype, type);
9098 21 : break;
9099 14 : case ic_init:
9100 14 : case ic_init_const:
9101 14 : pedwarn_init (location, OPT_Wpointer_sign,
9102 : "pointer targets in initialization of %qT "
9103 : "from %qT differ in signedness", type,
9104 : rhstype);
9105 14 : break;
9106 14 : case ic_return:
9107 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9108 : "returning %qT from a function with return type "
9109 : "%qT differ in signedness", rhstype, type);
9110 14 : break;
9111 0 : default:
9112 0 : gcc_unreachable ();
9113 : }
9114 : }
9115 33115 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9116 4154 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9117 : {
9118 : /* Because const and volatile on functions are restrictions
9119 : that say the function will not do certain things,
9120 : it is okay to use a const or volatile function
9121 : where an ordinary one is wanted, but not vice-versa. */
9122 3457 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9123 3457 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9124 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9125 : OPT_Wdiscarded_qualifiers,
9126 : G_("passing argument %d of %qE makes "
9127 : "%q#v qualified function pointer "
9128 : "from unqualified"),
9129 : G_("assignment makes %q#v qualified function "
9130 : "pointer from unqualified"),
9131 : G_("initialization makes %q#v qualified "
9132 : "function pointer from unqualified"),
9133 : G_("return makes %q#v qualified function "
9134 : "pointer from unqualified"),
9135 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
9136 : }
9137 : }
9138 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9139 799 : else if (!objc_ok)
9140 : {
9141 799 : auto_diagnostic_group d;
9142 799 : bool warned = false;
9143 799 : pp_markup::element_expected_type e_type (type);
9144 799 : pp_markup::element_actual_type e_rhstype (rhstype);
9145 799 : switch (errtype)
9146 : {
9147 306 : case ic_argpass:
9148 306 : {
9149 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
9150 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
9151 306 : highlight_colors::actual);
9152 306 : warned
9153 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9154 : "passing argument %d of %qE from "
9155 : "incompatible pointer type",
9156 : parmnum, rname);
9157 306 : if (warned)
9158 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9159 306 : }
9160 306 : break;
9161 267 : case ic_assign:
9162 267 : if (bltin)
9163 11 : warned
9164 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9165 : "assignment to %e from pointer to "
9166 : "%qD with incompatible type %e",
9167 : &e_type, bltin, &e_rhstype);
9168 : else
9169 256 : warned
9170 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9171 : "assignment to %e from incompatible "
9172 : "pointer type %e",
9173 : &e_type, &e_rhstype);
9174 : break;
9175 167 : case ic_init:
9176 167 : case ic_init_const:
9177 167 : if (bltin)
9178 13 : warned
9179 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9180 : "initialization of %e from pointer to "
9181 : "%qD with incompatible type %e",
9182 : &e_type, bltin, &e_rhstype);
9183 : else
9184 154 : warned
9185 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9186 : "initialization of %e from incompatible "
9187 : "pointer type %e",
9188 : &e_type, &e_rhstype);
9189 : break;
9190 59 : case ic_return:
9191 59 : if (bltin)
9192 11 : warned
9193 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9194 : "returning pointer to %qD of type %e from "
9195 : "a function with incompatible type %e",
9196 : bltin, &e_rhstype, &e_type);
9197 : else
9198 48 : warned
9199 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9200 : "returning %e from a function with "
9201 : "incompatible return type %e",
9202 : &e_rhstype, &e_type);
9203 : break;
9204 0 : default:
9205 0 : gcc_unreachable ();
9206 : }
9207 799 : if (warned)
9208 : {
9209 : /* If the mismatching function type is a pointer to a function,
9210 : try to show the decl of the function. */
9211 589 : if (TREE_CODE (rhs) == ADDR_EXPR
9212 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9213 : {
9214 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9215 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9216 : {
9217 165 : gcc_rich_location richloc
9218 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9219 165 : highlight_colors::actual);
9220 165 : pp_element_quoted_decl e_rhs_fndecl
9221 165 : (rhs_fndecl, highlight_colors::actual);
9222 165 : inform (&richloc,
9223 : "%e declared here", &e_rhs_fndecl);
9224 165 : }
9225 : }
9226 : /* If either/both of the types are typedefs, show the decl. */
9227 589 : maybe_inform_typedef_location (type,
9228 : highlight_colors::expected);
9229 589 : maybe_inform_typedef_location (rhstype,
9230 : highlight_colors::actual);
9231 : }
9232 799 : }
9233 :
9234 : /* If RHS isn't an address, check pointer or array of packed
9235 : struct or union. */
9236 2018077 : warn_for_address_of_packed_member (type, orig_rhs);
9237 :
9238 2018077 : return convert (type, rhs);
9239 : }
9240 79625 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9241 : {
9242 : /* ??? This should not be an error when inlining calls to
9243 : unprototyped functions. */
9244 4 : const char msg[] = "invalid use of non-lvalue array";
9245 4 : if (warnopt)
9246 0 : warning_at (location, warnopt, msg);
9247 : else
9248 4 : error_at (location, msg);
9249 4 : return error_mark_node;
9250 : }
9251 79621 : else if (codel == POINTER_TYPE
9252 78346 : && (coder == INTEGER_TYPE
9253 78346 : || coder == ENUMERAL_TYPE
9254 615 : || coder == BOOLEAN_TYPE
9255 615 : || coder == NULLPTR_TYPE
9256 27 : || coder == BITINT_TYPE))
9257 : {
9258 78333 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9259 77335 : && coder != NULLPTR_TYPE)
9260 76776 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9261 : "zero as null pointer constant");
9262 : /* A NULLPTR type is just a nullptr always. */
9263 77774 : if (coder == NULLPTR_TYPE)
9264 574 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9265 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9266 : or one that results from arithmetic, even including a cast to
9267 : integer type. */
9268 77759 : else if (!null_pointer_constant)
9269 972 : switch (errtype)
9270 : {
9271 800 : case ic_argpass:
9272 800 : {
9273 800 : auto_diagnostic_group d;
9274 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9275 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9276 800 : highlight_colors::actual);
9277 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9278 : "passing argument %d of %qE makes pointer "
9279 : "from integer without a cast", parmnum, rname))
9280 : {
9281 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9282 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9283 : }
9284 800 : }
9285 800 : break;
9286 93 : case ic_assign:
9287 93 : permerror_opt (location, OPT_Wint_conversion,
9288 : "assignment to %qT from %qT makes pointer from "
9289 : "integer without a cast", type, rhstype);
9290 93 : break;
9291 56 : case ic_init:
9292 56 : case ic_init_const:
9293 56 : permerror_init (location, OPT_Wint_conversion,
9294 : "initialization of %qT from %qT makes pointer "
9295 : "from integer without a cast", type, rhstype);
9296 56 : break;
9297 23 : case ic_return:
9298 23 : permerror_init (location, OPT_Wint_conversion,
9299 : "returning %qT from a function with return type "
9300 : "%qT makes pointer from integer without a cast",
9301 : rhstype, type);
9302 23 : break;
9303 0 : default:
9304 0 : gcc_unreachable ();
9305 : }
9306 :
9307 77759 : return convert (type, rhs);
9308 : }
9309 1288 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9310 540 : && coder == POINTER_TYPE)
9311 : {
9312 514 : switch (errtype)
9313 : {
9314 344 : case ic_argpass:
9315 344 : {
9316 344 : auto_diagnostic_group d;
9317 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9318 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9319 344 : highlight_colors::actual);
9320 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9321 : "passing argument %d of %qE makes integer from "
9322 : "pointer without a cast", parmnum, rname))
9323 : {
9324 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9325 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9326 : }
9327 344 : }
9328 344 : break;
9329 52 : case ic_assign:
9330 52 : permerror_opt (location, OPT_Wint_conversion,
9331 : "assignment to %qT from %qT makes integer from "
9332 : "pointer without a cast", type, rhstype);
9333 52 : break;
9334 83 : case ic_init:
9335 83 : case ic_init_const:
9336 83 : permerror_init (location, OPT_Wint_conversion,
9337 : "initialization of %qT from %qT makes integer "
9338 : "from pointer without a cast", type, rhstype);
9339 83 : break;
9340 35 : case ic_return:
9341 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9342 : "function with return type %qT makes integer from "
9343 : "pointer without a cast", rhstype, type);
9344 35 : break;
9345 0 : default:
9346 0 : gcc_unreachable ();
9347 : }
9348 :
9349 514 : return convert (type, rhs);
9350 : }
9351 626 : else if (C_BOOLEAN_TYPE_P (type)
9352 : /* The type nullptr_t may be converted to bool. The
9353 : result is false. */
9354 776 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9355 : {
9356 150 : tree ret;
9357 150 : bool save = in_late_binary_op;
9358 150 : in_late_binary_op = true;
9359 150 : ret = convert (type, rhs);
9360 150 : in_late_binary_op = save;
9361 150 : return ret;
9362 : }
9363 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9364 6 : return convert (type, rhs);
9365 :
9366 618 : switch (errtype)
9367 : {
9368 35 : case ic_argpass:
9369 35 : {
9370 35 : auto_diagnostic_group d;
9371 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9372 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9373 35 : highlight_colors::actual);
9374 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9375 35 : if (warnopt)
9376 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9377 : else
9378 27 : error_at (&richloc, msg, parmnum, rname);
9379 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9380 35 : }
9381 35 : break;
9382 108 : case ic_assign:
9383 108 : {
9384 108 : const char msg[]
9385 : = G_("incompatible types when assigning to type %qT from type %qT");
9386 108 : if (warnopt)
9387 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9388 : else
9389 108 : error_at (expr_loc, msg, type, rhstype);
9390 108 : break;
9391 : }
9392 460 : case ic_init:
9393 460 : case ic_init_const:
9394 460 : {
9395 460 : const char msg[]
9396 : = G_("incompatible types when initializing type %qT using type %qT");
9397 460 : if (warnopt)
9398 0 : warning_at (location, 0, msg, type, rhstype);
9399 : else
9400 460 : error_at (location, msg, type, rhstype);
9401 460 : break;
9402 : }
9403 15 : case ic_return:
9404 15 : {
9405 15 : const char msg[]
9406 : = G_("incompatible types when returning type %qT but %qT was expected");
9407 15 : if (warnopt)
9408 0 : warning_at (location, 0, msg, rhstype, type);
9409 : else
9410 15 : error_at (location, msg, rhstype, type);
9411 15 : break;
9412 : }
9413 0 : default:
9414 0 : gcc_unreachable ();
9415 : }
9416 :
9417 618 : return error_mark_node;
9418 : }
9419 :
9420 : /* If VALUE is a compound expr all of whose expressions are constant, then
9421 : return its value. Otherwise, return error_mark_node.
9422 :
9423 : This is for handling COMPOUND_EXPRs as initializer elements
9424 : which is allowed with a warning when -pedantic is specified. */
9425 :
9426 : static tree
9427 2 : valid_compound_expr_initializer (tree value, tree endtype)
9428 : {
9429 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9430 : {
9431 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9432 1 : == error_mark_node)
9433 : return error_mark_node;
9434 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9435 0 : endtype);
9436 : }
9437 1 : else if (!initializer_constant_valid_p (value, endtype))
9438 1 : return error_mark_node;
9439 : else
9440 : return value;
9441 : }
9442 :
9443 : /* Perform appropriate conversions on the initial value of a variable,
9444 : store it in the declaration DECL,
9445 : and print any error messages that are appropriate.
9446 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9447 : If the init is invalid, store an ERROR_MARK.
9448 :
9449 : INIT_LOC is the location of the initial value. */
9450 :
9451 : void
9452 7272957 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9453 : {
9454 7272957 : tree value, type;
9455 7272957 : bool npc = false;
9456 7272957 : bool int_const_expr = false;
9457 7272957 : bool arith_const_expr = false;
9458 :
9459 : /* If variable's type was invalidly declared, just ignore it. */
9460 :
9461 7272957 : type = TREE_TYPE (decl);
9462 7272957 : if (TREE_CODE (type) == ERROR_MARK)
9463 : return;
9464 :
9465 : /* Digest the specified initializer into an expression. */
9466 :
9467 7272949 : if (init)
9468 : {
9469 7272946 : npc = null_pointer_constant_p (init);
9470 7272946 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9471 419633 : && !TREE_OVERFLOW (init)
9472 7692543 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9473 : /* Not fully determined before folding. */
9474 : arith_const_expr = true;
9475 : }
9476 7272949 : bool constexpr_p = (VAR_P (decl)
9477 7272949 : && C_DECL_DECLARED_CONSTEXPR (decl));
9478 7272949 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9479 : int_const_expr, arith_const_expr, true,
9480 7272949 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9481 :
9482 : /* Store the expression if valid; else report error. */
9483 :
9484 7272949 : if (!in_system_header_at (input_location)
9485 7272949 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9486 28152 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9487 : "aggregate initialization");
9488 :
9489 7272949 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9490 7272948 : DECL_INITIAL (decl) = value;
9491 :
9492 : /* ANSI wants warnings about out-of-range constant initializers. */
9493 7272960 : STRIP_TYPE_NOPS (value);
9494 7272949 : if (TREE_STATIC (decl))
9495 179898 : constant_expression_warning (value);
9496 :
9497 : /* Check if we need to set array size from compound literal size. */
9498 7272949 : if (TREE_CODE (type) == ARRAY_TYPE
9499 26365 : && TYPE_DOMAIN (type) == NULL_TREE
9500 7285446 : && value != error_mark_node)
9501 : {
9502 : tree inside_init = init;
9503 :
9504 11625 : STRIP_TYPE_NOPS (inside_init);
9505 11625 : inside_init = fold (inside_init);
9506 :
9507 11625 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9508 : {
9509 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9510 :
9511 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9512 : {
9513 : /* For int foo[] = (int [3]){1}; we need to set array size
9514 : now since later on array initializer will be just the
9515 : brace enclosed list of the compound literal. */
9516 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9517 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9518 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9519 13 : layout_type (type);
9520 13 : layout_decl (cldecl, 0);
9521 13 : TREE_TYPE (decl)
9522 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9523 : }
9524 : }
9525 : }
9526 : }
9527 :
9528 : /* Methods for storing and printing names for error messages. */
9529 :
9530 : /* Implement a spelling stack that allows components of a name to be pushed
9531 : and popped. Each element on the stack is this structure. */
9532 :
9533 : struct spelling
9534 : {
9535 : int kind;
9536 : union
9537 : {
9538 : unsigned HOST_WIDE_INT i;
9539 : const char *s;
9540 : } u;
9541 : };
9542 :
9543 : #define SPELLING_STRING 1
9544 : #define SPELLING_MEMBER 2
9545 : #define SPELLING_BOUNDS 3
9546 :
9547 : static struct spelling *spelling; /* Next stack element (unused). */
9548 : static struct spelling *spelling_base; /* Spelling stack base. */
9549 : static int spelling_size; /* Size of the spelling stack. */
9550 :
9551 : /* Macros to save and restore the spelling stack around push_... functions.
9552 : Alternative to SAVE_SPELLING_STACK. */
9553 :
9554 : #define SPELLING_DEPTH() (spelling - spelling_base)
9555 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9556 :
9557 : /* Push an element on the spelling stack with type KIND and assign VALUE
9558 : to MEMBER. */
9559 :
9560 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9561 : { \
9562 : int depth = SPELLING_DEPTH (); \
9563 : \
9564 : if (depth >= spelling_size) \
9565 : { \
9566 : spelling_size += 10; \
9567 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9568 : spelling_size); \
9569 : RESTORE_SPELLING_DEPTH (depth); \
9570 : } \
9571 : \
9572 : spelling->kind = (KIND); \
9573 : spelling->MEMBER = (VALUE); \
9574 : spelling++; \
9575 : }
9576 :
9577 : /* Push STRING on the stack. Printed literally. */
9578 :
9579 : static void
9580 7270185 : push_string (const char *string)
9581 : {
9582 7270185 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9583 7270185 : }
9584 :
9585 : /* Push a member name on the stack. Printed as '.' STRING. */
9586 :
9587 : static void
9588 1227229 : push_member_name (tree decl)
9589 : {
9590 1227229 : const char *const string
9591 1227229 : = (DECL_NAME (decl)
9592 1227229 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9593 208 : : _("<anonymous>"));
9594 1227229 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9595 1227229 : }
9596 :
9597 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9598 :
9599 : static void
9600 2601473 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9601 : {
9602 2601473 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9603 2601473 : }
9604 :
9605 : /* Compute the maximum size in bytes of the printed spelling. */
9606 :
9607 : static int
9608 2892 : spelling_length (void)
9609 : {
9610 2892 : int size = 0;
9611 2892 : struct spelling *p;
9612 :
9613 6416 : for (p = spelling_base; p < spelling; p++)
9614 : {
9615 3524 : if (p->kind == SPELLING_BOUNDS)
9616 1479 : size += 25;
9617 : else
9618 2045 : size += strlen (p->u.s) + 1;
9619 : }
9620 :
9621 2892 : return size;
9622 : }
9623 :
9624 : /* Print the spelling to BUFFER and return it. */
9625 :
9626 : static char *
9627 2892 : print_spelling (char *buffer)
9628 : {
9629 2892 : char *d = buffer;
9630 2892 : struct spelling *p;
9631 :
9632 6416 : for (p = spelling_base; p < spelling; p++)
9633 3524 : if (p->kind == SPELLING_BOUNDS)
9634 : {
9635 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9636 1479 : d += strlen (d);
9637 : }
9638 : else
9639 : {
9640 2045 : const char *s;
9641 2045 : if (p->kind == SPELLING_MEMBER)
9642 198 : *d++ = '.';
9643 16715 : for (s = p->u.s; (*d = *s++); d++)
9644 : ;
9645 : }
9646 2892 : *d++ = '\0';
9647 2892 : return buffer;
9648 : }
9649 :
9650 : /* Check whether INIT, a floating or integer constant, is
9651 : representable in TYPE, a real floating type with the same radix or
9652 : a decimal floating type initialized with a binary floating
9653 : constant. Return true if OK, false if not. */
9654 : static bool
9655 362 : constexpr_init_fits_real_type (tree type, tree init)
9656 : {
9657 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9658 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9659 362 : if (TREE_CODE (init) == REAL_CST
9660 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9661 : {
9662 : /* Same mode, no conversion required except for the case of
9663 : signaling NaNs if the types are incompatible (e.g. double and
9664 : long double with the same mode). */
9665 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9666 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9667 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9668 : return false;
9669 177 : return true;
9670 : }
9671 185 : if (TREE_CODE (init) == INTEGER_CST)
9672 : {
9673 54 : tree converted = build_real_from_int_cst (type, init);
9674 54 : bool fail = false;
9675 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9676 54 : TYPE_PRECISION (TREE_TYPE (init)));
9677 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9678 54 : }
9679 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9680 : return false;
9681 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9682 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9683 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9684 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9685 20 : return true;
9686 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9687 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9688 : {
9689 : /* This is valid if the real number represented by the
9690 : initializer can be exactly represented in the decimal
9691 : type. Compare the values using MPFR. */
9692 8 : REAL_VALUE_TYPE t;
9693 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9694 8 : mpfr_t bin_val, dec_val;
9695 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9696 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9697 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9698 8 : char string[256];
9699 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9700 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9701 8 : && mpfr_equal_p (bin_val, dec_val));
9702 8 : mpfr_clear (bin_val);
9703 8 : mpfr_clear (dec_val);
9704 8 : return res;
9705 : }
9706 : /* exact_real_truncate is not quite right here, since it doesn't
9707 : allow even an exact conversion to subnormal values. */
9708 86 : REAL_VALUE_TYPE t;
9709 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9710 86 : return real_identical (&t, &TREE_REAL_CST (init));
9711 : }
9712 :
9713 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9714 : initializer for type TYPE, and give an error if not. INIT has
9715 : already been folded and verified to be constant. INT_CONST_EXPR
9716 : and ARITH_CONST_EXPR say whether it is an integer constant
9717 : expression or arithmetic constant expression, respectively. If
9718 : TYPE is not a scalar type, this function does nothing. */
9719 :
9720 : static void
9721 912 : check_constexpr_init (location_t loc, tree type, tree init,
9722 : bool int_const_expr, bool arith_const_expr)
9723 : {
9724 912 : if (POINTER_TYPE_P (type))
9725 : {
9726 : /* The initializer must be null. */
9727 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9728 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9729 86 : return;
9730 : }
9731 826 : if (INTEGRAL_TYPE_P (type))
9732 : {
9733 : /* The initializer must be an integer constant expression,
9734 : representable in the target type. */
9735 316 : if (!int_const_expr)
9736 : {
9737 13 : if (TREE_CODE (init) == RAW_DATA_CST
9738 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9739 : {
9740 4 : if (!TYPE_UNSIGNED (type))
9741 137 : for (unsigned int i = 0;
9742 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9743 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9744 : {
9745 1 : error_at (loc, "%<constexpr%> initializer not "
9746 : "representable in type of object");
9747 1 : break;
9748 : }
9749 : }
9750 : else
9751 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9752 : "integer constant expression");
9753 : }
9754 303 : else if (!int_fits_type_p (init, type))
9755 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9756 : "type of object");
9757 316 : return;
9758 : }
9759 : /* We don't apply any extra checks to extension types such as vector
9760 : or fixed-point types. */
9761 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9762 : return;
9763 353 : if (!arith_const_expr)
9764 : {
9765 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9766 : "constant expression");
9767 5 : return;
9768 : }
9769 : /* We don't apply any extra checks to complex integers. */
9770 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9771 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9772 : return;
9773 : /* Following N3082, a real type cannot be initialized from a complex
9774 : type and a binary type cannot be initialized from a decimal type
9775 : (but initializing a decimal type from a binary type is OK).
9776 : Signaling NaN initializers are OK only if the types are
9777 : compatible (not just the same mode); all quiet NaN and infinity
9778 : initializations are considered to preserve the value. */
9779 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9780 348 : && SCALAR_FLOAT_TYPE_P (type))
9781 : {
9782 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9783 : "complex type");
9784 6 : return;
9785 : }
9786 342 : if (SCALAR_FLOAT_TYPE_P (type)
9787 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9788 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9789 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9790 : {
9791 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9792 : "floating-point type is of decimal type");
9793 6 : return;
9794 : }
9795 336 : bool fits;
9796 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9797 : {
9798 42 : switch (TREE_CODE (init))
9799 : {
9800 10 : case INTEGER_CST:
9801 10 : case REAL_CST:
9802 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9803 10 : break;
9804 32 : case COMPLEX_CST:
9805 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9806 32 : TREE_REALPART (init))
9807 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9808 26 : TREE_IMAGPART (init)));
9809 : break;
9810 0 : default:
9811 0 : gcc_unreachable ();
9812 : }
9813 : }
9814 : else
9815 294 : fits = constexpr_init_fits_real_type (type, init);
9816 304 : if (!fits)
9817 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9818 : "type of object");
9819 : }
9820 :
9821 : /* Digest the parser output INIT as an initializer for type TYPE
9822 : initializing DECL.
9823 : Return a C expression of type TYPE to represent the initial value.
9824 :
9825 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9826 :
9827 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9828 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9829 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9830 : constant expression, false if it has already been determined in the
9831 : caller that it is not (but folding may have made the value passed here
9832 : indistinguishable from an arithmetic constant expression).
9833 :
9834 : If INIT is a string constant, STRICT_STRING is true if it is
9835 : unparenthesized or we should not warn here for it being parenthesized.
9836 : For other types of INIT, STRICT_STRING is not used.
9837 :
9838 : INIT_LOC is the location of the INIT.
9839 :
9840 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9841 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9842 : on initializers for 'constexpr' objects apply. */
9843 :
9844 : static tree
9845 16897918 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9846 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9847 : bool arith_const_expr, bool strict_string,
9848 : bool require_constant, bool require_constexpr)
9849 : {
9850 16897918 : enum tree_code code = TREE_CODE (type);
9851 16897918 : tree inside_init = init;
9852 16897918 : tree semantic_type = NULL_TREE;
9853 16897918 : bool maybe_const = true;
9854 :
9855 16897918 : if (type == error_mark_node
9856 16897918 : || !init
9857 33795833 : || error_operand_p (init))
9858 : return error_mark_node;
9859 :
9860 16905327 : STRIP_TYPE_NOPS (inside_init);
9861 :
9862 : /* If require_constant is TRUE, when the initializer is a call to
9863 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9864 : For example:
9865 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9866 : will be converted to
9867 : y = &static_annotated.c. */
9868 :
9869 16896767 : if (require_constant
9870 2892254 : && TREE_CODE (inside_init) == NOP_EXPR
9871 736235 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9872 16896769 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9873 2 : inside_init
9874 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9875 :
9876 16896767 : if (!c_in_omp_for)
9877 : {
9878 16892015 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9879 : {
9880 441 : semantic_type = TREE_TYPE (inside_init);
9881 441 : inside_init = TREE_OPERAND (inside_init, 0);
9882 : }
9883 16892015 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9884 : }
9885 : /* TODO: this may not detect all cases of expressions folding to
9886 : constants that are not arithmetic constant expressions. */
9887 16896767 : if (!maybe_const)
9888 : arith_const_expr = false;
9889 25212222 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9890 5655130 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9891 16826585 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9892 : arith_const_expr = false;
9893 8414200 : else if (TREE_CODE (inside_init) != INTEGER_CST
9894 : && TREE_CODE (inside_init) != REAL_CST
9895 : && TREE_CODE (inside_init) != COMPLEX_CST
9896 : && TREE_CODE (inside_init) != RAW_DATA_CST)
9897 : arith_const_expr = false;
9898 5075080 : else if (TREE_OVERFLOW (inside_init))
9899 11821731 : arith_const_expr = false;
9900 :
9901 : /* Initialization of an array of chars from a string constant
9902 : optionally enclosed in braces. */
9903 :
9904 16896767 : if (code == ARRAY_TYPE && inside_init
9905 188379 : && TREE_CODE (inside_init) == STRING_CST)
9906 : {
9907 11709 : tree typ1
9908 11709 : = (TYPE_ATOMIC (TREE_TYPE (type))
9909 11709 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9910 : TYPE_QUAL_ATOMIC)
9911 11695 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9912 : /* Note that an array could be both an array of character type
9913 : and an array of wchar_t if wchar_t is signed char or unsigned
9914 : char. */
9915 23418 : bool char_array = (typ1 == char_type_node
9916 516 : || typ1 == signed_char_type_node
9917 12190 : || typ1 == unsigned_char_type_node);
9918 11709 : bool wchar_array = !!comptypes (typ1, wchar_type_node);
9919 11709 : bool char16_array = !!comptypes (typ1, char16_type_node);
9920 11709 : bool char32_array = !!comptypes (typ1, char32_type_node);
9921 :
9922 11709 : if (char_array || wchar_array || char16_array || char32_array)
9923 : {
9924 11680 : struct c_expr expr;
9925 11680 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9926 11680 : bool incompat_string_cst = false;
9927 11680 : expr.value = inside_init;
9928 11680 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9929 11680 : expr.original_type = NULL;
9930 11680 : expr.m_decimal = 0;
9931 11680 : maybe_warn_string_init (init_loc, type, expr);
9932 :
9933 11680 : if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9934 91 : pedwarn_init (init_loc, OPT_Wpedantic,
9935 : "initialization of a flexible array member");
9936 :
9937 11680 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9938 11680 : TYPE_MAIN_VARIANT (type)))
9939 : return inside_init;
9940 :
9941 4185 : if (char_array)
9942 : {
9943 4074 : if (typ2 != char_type_node && typ2 != char8_type_node)
9944 : incompat_string_cst = true;
9945 : }
9946 111 : else if (!comptypes (typ1, typ2))
9947 : incompat_string_cst = true;
9948 :
9949 : if (incompat_string_cst)
9950 : {
9951 72 : error_init (init_loc, "cannot initialize array of %qT from "
9952 : "a string literal with type array of %qT",
9953 : typ1, typ2);
9954 72 : return error_mark_node;
9955 : }
9956 :
9957 4113 : if (require_constexpr
9958 4113 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9959 : {
9960 : /* Check if all characters of the string can be
9961 : represented in the type of the constexpr object being
9962 : initialized. */
9963 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9964 24 : const unsigned char *p =
9965 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
9966 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9967 70 : for (unsigned i = 0; i < len; i++)
9968 58 : if (p[i] > 127)
9969 : {
9970 12 : error_init (init_loc, "%<constexpr%> initializer not "
9971 : "representable in type of object");
9972 12 : break;
9973 : }
9974 : }
9975 :
9976 4113 : if (TYPE_DOMAIN (type) != NULL_TREE
9977 4019 : && TYPE_SIZE (type) != NULL_TREE
9978 8073 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9979 : {
9980 3960 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9981 :
9982 3960 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9983 : {
9984 396 : unsigned HOST_WIDE_INT avail
9985 396 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9986 396 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9987 396 : const char *p = TREE_STRING_POINTER (inside_init);
9988 :
9989 : /* Construct truncated string. */
9990 396 : inside_init = build_string (avail, p);
9991 :
9992 : /* Subtract the size of a single (possibly wide) character
9993 : because it may be ok to ignore the terminating NUL char
9994 : that is counted in the length of the constant. */
9995 396 : if (len - unit > avail)
9996 63 : pedwarn_init (init_loc, 0,
9997 : "initializer-string for array of %qT "
9998 : "is too long (%wu chars into %wu "
9999 : "available)", typ1, len, avail);
10000 333 : else if (warn_cxx_compat)
10001 9 : warning_at (init_loc, OPT_Wc___compat,
10002 : "initializer-string for array of %qT "
10003 : "is too long for C++ (%wu chars into %wu "
10004 : "available)", typ1, len, avail);
10005 324 : else if (warn_unterminated_string_initialization
10006 324 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10007 22 : warning_at (init_loc,
10008 22 : OPT_Wunterminated_string_initialization,
10009 : "initializer-string for array of %qT "
10010 : "truncates NUL terminator but destination "
10011 : "lacks %qs attribute (%wu chars into %wu "
10012 : "available)", typ1, "nonstring", len, avail);
10013 : }
10014 : }
10015 :
10016 4113 : TREE_TYPE (inside_init) = type;
10017 4113 : return inside_init;
10018 : }
10019 29 : else if (INTEGRAL_TYPE_P (typ1))
10020 : {
10021 29 : error_init (init_loc, "array of inappropriate type initialized "
10022 : "from string constant");
10023 29 : return error_mark_node;
10024 : }
10025 : }
10026 :
10027 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10028 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10029 : below and handle as a constructor. */
10030 16885058 : if (code == VECTOR_TYPE
10031 6312384 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10032 6312382 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10033 23197402 : && TREE_CONSTANT (inside_init))
10034 : {
10035 633390 : if (TREE_CODE (inside_init) == VECTOR_CST
10036 633487 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10037 97 : TYPE_MAIN_VARIANT (type)))
10038 : return inside_init;
10039 :
10040 633293 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10041 : {
10042 : unsigned HOST_WIDE_INT ix;
10043 : tree value;
10044 4174535 : bool constant_p = true;
10045 :
10046 : /* Iterate through elements and check if all constructor
10047 : elements are *_CSTs. */
10048 4174535 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10049 3541298 : if (!CONSTANT_CLASS_P (value))
10050 : {
10051 : constant_p = false;
10052 : break;
10053 : }
10054 :
10055 633293 : if (constant_p)
10056 633237 : return build_vector_from_ctor (type,
10057 633237 : CONSTRUCTOR_ELTS (inside_init));
10058 : }
10059 : }
10060 :
10061 16251724 : if (warn_sequence_point)
10062 2447846 : verify_sequence_points (inside_init);
10063 :
10064 : /* Any type can be initialized
10065 : from an expression of the same type, optionally with braces. */
10066 :
10067 16251724 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10068 32503448 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10069 16251724 : TYPE_MAIN_VARIANT (type))
10070 3131129 : || (code == ARRAY_TYPE
10071 477 : && comptypes (TREE_TYPE (inside_init), type))
10072 3131129 : || (gnu_vector_type_p (type)
10073 189 : && comptypes (TREE_TYPE (inside_init), type))
10074 3131129 : || (code == POINTER_TYPE
10075 111932 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10076 9290 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10077 9290 : TREE_TYPE (type)))))
10078 : {
10079 13122571 : if (code == POINTER_TYPE)
10080 : {
10081 784612 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10082 : {
10083 1976 : if (TREE_CODE (inside_init) == STRING_CST
10084 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10085 1973 : inside_init = array_to_pointer_conversion
10086 1973 : (init_loc, inside_init);
10087 : else
10088 : {
10089 3 : error_init (init_loc, "invalid use of non-lvalue array");
10090 3 : return error_mark_node;
10091 : }
10092 : }
10093 : }
10094 :
10095 13122568 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10096 : /* Although the types are compatible, we may require a
10097 : conversion. */
10098 5678994 : inside_init = convert (type, inside_init);
10099 :
10100 13122568 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10101 13122568 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10102 : {
10103 0 : error_init (init_loc, "invalid initializer");
10104 0 : return error_mark_node;
10105 : }
10106 :
10107 13122568 : if (require_constant
10108 2298135 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10109 : {
10110 : /* As an extension, allow initializing objects with static storage
10111 : duration with compound literals (which are then treated just as
10112 : the brace enclosed list they contain). Also allow this for
10113 : vectors, as we can only assign them with compound literals. */
10114 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10115 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10116 : "is not constant");
10117 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10118 31 : inside_init = DECL_INITIAL (decl);
10119 : }
10120 :
10121 13122568 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10122 176193 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10123 : {
10124 2 : error_init (init_loc, "array initialized from non-constant array "
10125 : "expression");
10126 2 : return error_mark_node;
10127 : }
10128 :
10129 : /* Compound expressions can only occur here if -Wpedantic or
10130 : -pedantic-errors is specified. In the later case, we always want
10131 : an error. In the former case, we simply want a warning. */
10132 13122566 : if (require_constant && pedantic
10133 20284 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10134 : {
10135 1 : inside_init
10136 1 : = valid_compound_expr_initializer (inside_init,
10137 1 : TREE_TYPE (inside_init));
10138 1 : if (inside_init == error_mark_node)
10139 1 : error_init (init_loc, "initializer element is not constant");
10140 : else
10141 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10142 : "initializer element is not constant");
10143 1 : if (flag_pedantic_errors)
10144 0 : inside_init = error_mark_node;
10145 : }
10146 2298134 : else if (require_constant
10147 2298134 : && !initializer_constant_valid_p (inside_init,
10148 2298134 : TREE_TYPE (inside_init)))
10149 : {
10150 94 : error_init (init_loc, "initializer element is not constant");
10151 94 : inside_init = error_mark_node;
10152 : }
10153 13122471 : else if (require_constant && !maybe_const)
10154 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10155 : "initializer element is not a constant expression");
10156 13122252 : else if (require_constexpr)
10157 584 : check_constexpr_init (init_loc, type, inside_init,
10158 : int_const_expr, arith_const_expr);
10159 :
10160 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10161 13122566 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10162 853509 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10163 : type, inside_init, origtype,
10164 : (require_constant
10165 : ? ic_init_const
10166 : : ic_init), null_pointer_constant,
10167 : NULL_TREE, NULL_TREE, 0);
10168 13122566 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10169 254 : && c_inhibit_evaluation_warnings == 0
10170 254 : && warn_conversion
10171 1 : && !TYPE_UNSIGNED (type)
10172 13122567 : && TYPE_PRECISION (type) == CHAR_BIT)
10173 303 : for (unsigned int i = 0;
10174 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10175 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10176 10 : warning_at (init_loc, OPT_Wconversion,
10177 : "conversion from %qT to %qT changes value from "
10178 : "%qd to %qd",
10179 : integer_type_node, type,
10180 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10181 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10182 13122566 : return inside_init;
10183 : }
10184 :
10185 : /* Handle scalar types, including conversions. */
10186 :
10187 3129153 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10188 128041 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10189 11089 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10190 9026 : || code == BITINT_TYPE)
10191 : {
10192 3128665 : tree unconverted_init = inside_init;
10193 3128665 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10194 3128665 : && (TREE_CODE (init) == STRING_CST
10195 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10196 7315 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10197 3128665 : if (semantic_type)
10198 436 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10199 : inside_init);
10200 3128665 : inside_init
10201 5677992 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10202 : inside_init, origtype,
10203 : require_constant ? ic_init_const : ic_init,
10204 : null_pointer_constant, NULL_TREE, NULL_TREE,
10205 : 0);
10206 :
10207 : /* Check to see if we have already given an error message. */
10208 3128665 : if (inside_init == error_mark_node)
10209 : ;
10210 3128205 : else if (require_constant && !TREE_CONSTANT (inside_init))
10211 : {
10212 36 : error_init (init_loc, "initializer element is not constant");
10213 36 : inside_init = error_mark_node;
10214 : }
10215 3128169 : else if (require_constant
10216 3707079 : && !initializer_constant_valid_p (inside_init,
10217 578910 : TREE_TYPE (inside_init)))
10218 : {
10219 10 : error_init (init_loc, "initializer element is not computable at "
10220 : "load time");
10221 10 : inside_init = error_mark_node;
10222 : }
10223 3128159 : else if (require_constant && !maybe_const)
10224 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10225 : "initializer element is not a constant expression");
10226 3128154 : else if (require_constexpr)
10227 328 : check_constexpr_init (init_loc, type, unconverted_init,
10228 : int_const_expr, arith_const_expr);
10229 :
10230 3128665 : return inside_init;
10231 : }
10232 :
10233 : /* Come here only for records and arrays. */
10234 :
10235 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10236 : {
10237 0 : error_init (init_loc,
10238 : "variable-sized object may not be initialized except "
10239 : "with an empty initializer");
10240 0 : return error_mark_node;
10241 : }
10242 :
10243 488 : error_init (init_loc, "invalid initializer");
10244 488 : return error_mark_node;
10245 : }
10246 :
10247 : /* Handle initializers that use braces. */
10248 :
10249 : /* Type of object we are accumulating a constructor for.
10250 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10251 : static tree constructor_type;
10252 :
10253 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10254 : left to fill. */
10255 : static tree constructor_fields;
10256 :
10257 : /* For an ARRAY_TYPE, this is the specified index
10258 : at which to store the next element we get. */
10259 : static tree constructor_index;
10260 :
10261 : /* For an ARRAY_TYPE, this is the maximum index. */
10262 : static tree constructor_max_index;
10263 :
10264 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10265 : static tree constructor_unfilled_fields;
10266 :
10267 : /* For an ARRAY_TYPE, this is the index of the first element
10268 : not yet written out. */
10269 : static tree constructor_unfilled_index;
10270 :
10271 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10272 : This is so we can generate gaps between fields, when appropriate. */
10273 : static tree constructor_bit_index;
10274 :
10275 : /* If we are saving up the elements rather than allocating them,
10276 : this is the list of elements so far (in reverse order,
10277 : most recent first). */
10278 : static vec<constructor_elt, va_gc> *constructor_elements;
10279 :
10280 : /* 1 if constructor should be incrementally stored into a constructor chain,
10281 : 0 if all the elements should be kept in AVL tree. */
10282 : static int constructor_incremental;
10283 :
10284 : /* 1 if so far this constructor's elements are all compile-time constants. */
10285 : static int constructor_constant;
10286 :
10287 : /* 1 if so far this constructor's elements are all valid address constants. */
10288 : static int constructor_simple;
10289 :
10290 : /* 1 if this constructor has an element that cannot be part of a
10291 : constant expression. */
10292 : static int constructor_nonconst;
10293 :
10294 : /* 1 if this constructor is erroneous so far. */
10295 : static int constructor_erroneous;
10296 :
10297 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10298 : static int constructor_zeroinit;
10299 :
10300 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10301 : static bool constructor_zero_padding_bits;
10302 :
10303 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10304 : of braces are an error). */
10305 : static bool constructor_braced_scalar;
10306 :
10307 : /* Structure for managing pending initializer elements, organized as an
10308 : AVL tree. */
10309 :
10310 : struct init_node
10311 : {
10312 : struct init_node *left, *right;
10313 : struct init_node *parent;
10314 : int balance;
10315 : tree purpose;
10316 : tree value;
10317 : tree origtype;
10318 : };
10319 :
10320 : /* Tree of pending elements at this constructor level.
10321 : These are elements encountered out of order
10322 : which belong at places we haven't reached yet in actually
10323 : writing the output.
10324 : Will never hold tree nodes across GC runs. */
10325 : static struct init_node *constructor_pending_elts;
10326 :
10327 : /* The SPELLING_DEPTH of this constructor. */
10328 : static int constructor_depth;
10329 :
10330 : /* DECL node for which an initializer is being read.
10331 : 0 means we are reading a constructor expression
10332 : such as (struct foo) {...}. */
10333 : static tree constructor_decl;
10334 :
10335 : /* Nonzero if there were any member designators in this initializer. */
10336 : static int constructor_designated;
10337 :
10338 : /* Nesting depth of designator list. */
10339 : static int designator_depth;
10340 :
10341 : /* Nonzero if there were diagnosed errors in this designator list. */
10342 : static int designator_erroneous;
10343 :
10344 :
10345 : /* This stack has a level for each implicit or explicit level of
10346 : structuring in the initializer, including the outermost one. It
10347 : saves the values of most of the variables above. */
10348 :
10349 : struct constructor_range_stack;
10350 :
10351 : struct constructor_stack
10352 : {
10353 : struct constructor_stack *next;
10354 : tree type;
10355 : tree fields;
10356 : tree index;
10357 : tree max_index;
10358 : tree unfilled_index;
10359 : tree unfilled_fields;
10360 : tree bit_index;
10361 : vec<constructor_elt, va_gc> *elements;
10362 : struct init_node *pending_elts;
10363 : int offset;
10364 : int depth;
10365 : /* If value nonzero, this value should replace the entire
10366 : constructor at this level. */
10367 : struct c_expr replacement_value;
10368 : struct constructor_range_stack *range_stack;
10369 : char constant;
10370 : char simple;
10371 : char nonconst;
10372 : char implicit;
10373 : char erroneous;
10374 : char outer;
10375 : char incremental;
10376 : char designated;
10377 : bool zero_padding_bits;
10378 : bool braced_scalar;
10379 : int designator_depth;
10380 : };
10381 :
10382 : static struct constructor_stack *constructor_stack;
10383 :
10384 : /* This stack represents designators from some range designator up to
10385 : the last designator in the list. */
10386 :
10387 : struct constructor_range_stack
10388 : {
10389 : struct constructor_range_stack *next, *prev;
10390 : struct constructor_stack *stack;
10391 : tree range_start;
10392 : tree index;
10393 : tree range_end;
10394 : tree fields;
10395 : };
10396 :
10397 : static struct constructor_range_stack *constructor_range_stack;
10398 :
10399 : /* This stack records separate initializers that are nested.
10400 : Nested initializers can't happen in ANSI C, but GNU C allows them
10401 : in cases like { ... (struct foo) { ... } ... }. */
10402 :
10403 : struct initializer_stack
10404 : {
10405 : struct initializer_stack *next;
10406 : tree decl;
10407 : struct constructor_stack *constructor_stack;
10408 : struct constructor_range_stack *constructor_range_stack;
10409 : vec<constructor_elt, va_gc> *elements;
10410 : struct spelling *spelling;
10411 : struct spelling *spelling_base;
10412 : int spelling_size;
10413 : char require_constant_value;
10414 : char require_constant_elements;
10415 : char require_constexpr_value;
10416 : char designated;
10417 : rich_location *missing_brace_richloc;
10418 : };
10419 :
10420 : static struct initializer_stack *initializer_stack;
10421 :
10422 : /* Prepare to parse and output the initializer for variable DECL. */
10423 :
10424 : void
10425 7270185 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10426 : bool init_require_constant, bool init_require_constexpr,
10427 : rich_location *richloc)
10428 : {
10429 7270185 : const char *locus;
10430 7270185 : struct initializer_stack *p = XNEW (struct initializer_stack);
10431 :
10432 7270185 : p->decl = constructor_decl;
10433 7270185 : p->require_constant_value = require_constant_value;
10434 7270185 : p->require_constant_elements = require_constant_elements;
10435 7270185 : p->require_constexpr_value = require_constexpr_value;
10436 7270185 : p->constructor_stack = constructor_stack;
10437 7270185 : p->constructor_range_stack = constructor_range_stack;
10438 7270185 : p->elements = constructor_elements;
10439 7270185 : p->spelling = spelling;
10440 7270185 : p->spelling_base = spelling_base;
10441 7270185 : p->spelling_size = spelling_size;
10442 7270185 : p->next = initializer_stack;
10443 7270185 : p->missing_brace_richloc = richloc;
10444 7270185 : p->designated = constructor_designated;
10445 7270185 : initializer_stack = p;
10446 :
10447 7270185 : constructor_decl = decl;
10448 7270185 : constructor_designated = 0;
10449 :
10450 7270185 : require_constant_value = init_require_constant;
10451 7270185 : require_constexpr_value = init_require_constexpr;
10452 7270185 : if (decl != NULL_TREE && decl != error_mark_node)
10453 : {
10454 6348302 : require_constant_elements
10455 6171600 : = ((init_require_constant || (pedantic && !flag_isoc99))
10456 : /* For a scalar, you can always use any value to initialize,
10457 : even within braces. */
10458 6360250 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10459 6348302 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10460 : }
10461 : else
10462 : {
10463 921883 : require_constant_elements = false;
10464 921883 : locus = _("(anonymous)");
10465 : }
10466 :
10467 7270185 : constructor_stack = 0;
10468 7270185 : constructor_range_stack = 0;
10469 :
10470 7270185 : found_missing_braces = 0;
10471 :
10472 7270185 : spelling_base = 0;
10473 7270185 : spelling_size = 0;
10474 7270185 : RESTORE_SPELLING_DEPTH (0);
10475 :
10476 7270185 : if (locus)
10477 7270185 : push_string (locus);
10478 7270185 : }
10479 :
10480 : void
10481 7270183 : finish_init (void)
10482 : {
10483 7270183 : struct initializer_stack *p = initializer_stack;
10484 :
10485 : /* Free the whole constructor stack of this initializer. */
10486 7270183 : while (constructor_stack)
10487 : {
10488 0 : struct constructor_stack *q = constructor_stack;
10489 0 : constructor_stack = q->next;
10490 0 : XDELETE (q);
10491 : }
10492 :
10493 7270183 : gcc_assert (!constructor_range_stack);
10494 :
10495 : /* Pop back to the data of the outer initializer (if any). */
10496 7270183 : XDELETE (spelling_base);
10497 :
10498 7270183 : constructor_decl = p->decl;
10499 7270183 : require_constant_value = p->require_constant_value;
10500 7270183 : require_constant_elements = p->require_constant_elements;
10501 7270183 : require_constexpr_value = p->require_constexpr_value;
10502 7270183 : constructor_stack = p->constructor_stack;
10503 7270183 : constructor_designated = p->designated;
10504 7270183 : constructor_range_stack = p->constructor_range_stack;
10505 7270183 : constructor_elements = p->elements;
10506 7270183 : spelling = p->spelling;
10507 7270183 : spelling_base = p->spelling_base;
10508 7270183 : spelling_size = p->spelling_size;
10509 7270183 : initializer_stack = p->next;
10510 7270183 : XDELETE (p);
10511 7270183 : }
10512 :
10513 : /* Call here when we see the initializer is surrounded by braces.
10514 : This is instead of a call to push_init_level;
10515 : it is matched by a call to pop_init_level.
10516 :
10517 : TYPE is the type to initialize, for a constructor expression.
10518 : For an initializer for a decl, TYPE is zero. */
10519 :
10520 : void
10521 1025446 : really_start_incremental_init (tree type)
10522 : {
10523 1025446 : struct constructor_stack *p = XNEW (struct constructor_stack);
10524 :
10525 1025446 : if (type == NULL_TREE)
10526 105527 : type = TREE_TYPE (constructor_decl);
10527 :
10528 1025446 : if (VECTOR_TYPE_P (type)
10529 1025446 : && TYPE_VECTOR_OPAQUE (type))
10530 0 : error ("opaque vector types cannot be initialized");
10531 :
10532 1025446 : p->type = constructor_type;
10533 1025446 : p->fields = constructor_fields;
10534 1025446 : p->index = constructor_index;
10535 1025446 : p->max_index = constructor_max_index;
10536 1025446 : p->unfilled_index = constructor_unfilled_index;
10537 1025446 : p->unfilled_fields = constructor_unfilled_fields;
10538 1025446 : p->bit_index = constructor_bit_index;
10539 1025446 : p->elements = constructor_elements;
10540 1025446 : p->constant = constructor_constant;
10541 1025446 : p->simple = constructor_simple;
10542 1025446 : p->nonconst = constructor_nonconst;
10543 1025446 : p->erroneous = constructor_erroneous;
10544 1025446 : p->pending_elts = constructor_pending_elts;
10545 1025446 : p->depth = constructor_depth;
10546 1025446 : p->replacement_value.value = 0;
10547 1025446 : p->replacement_value.original_code = ERROR_MARK;
10548 1025446 : p->replacement_value.original_type = NULL;
10549 1025446 : p->implicit = 0;
10550 1025446 : p->range_stack = 0;
10551 1025446 : p->outer = 0;
10552 1025446 : p->incremental = constructor_incremental;
10553 1025446 : p->designated = constructor_designated;
10554 1025446 : p->zero_padding_bits = constructor_zero_padding_bits;
10555 1025446 : p->braced_scalar = constructor_braced_scalar;
10556 1025446 : p->designator_depth = designator_depth;
10557 1025446 : p->next = 0;
10558 1025446 : constructor_stack = p;
10559 :
10560 1025446 : constructor_constant = 1;
10561 1025446 : constructor_simple = 1;
10562 1025446 : constructor_nonconst = 0;
10563 1025446 : constructor_depth = SPELLING_DEPTH ();
10564 1025446 : constructor_elements = NULL;
10565 1025446 : constructor_pending_elts = 0;
10566 1025446 : constructor_type = type;
10567 1025446 : constructor_incremental = 1;
10568 1025446 : constructor_designated = 0;
10569 1025446 : constructor_zero_padding_bits = false;
10570 1025446 : constructor_zeroinit = 1;
10571 1025446 : constructor_braced_scalar = false;
10572 1025446 : designator_depth = 0;
10573 1025446 : designator_erroneous = 0;
10574 :
10575 1025446 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10576 : {
10577 81459 : constructor_fields = TYPE_FIELDS (constructor_type);
10578 : /* Skip any nameless bit fields at the beginning. */
10579 81459 : while (constructor_fields != NULL_TREE
10580 81487 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10581 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10582 :
10583 81459 : constructor_unfilled_fields = constructor_fields;
10584 81459 : constructor_bit_index = bitsize_zero_node;
10585 : }
10586 943987 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10587 : {
10588 17901 : if (TYPE_DOMAIN (constructor_type))
10589 : {
10590 9429 : constructor_max_index
10591 9429 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10592 :
10593 : /* Detect non-empty initializations of zero-length arrays. */
10594 9429 : if (constructor_max_index == NULL_TREE
10595 9429 : && TYPE_SIZE (constructor_type))
10596 205 : constructor_max_index = integer_minus_one_node;
10597 :
10598 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10599 : to initialize VLAs with a nonempty initializer will cause a
10600 : proper error; avoid tree checking errors as well by setting a
10601 : safe value. */
10602 9429 : if (constructor_max_index
10603 9429 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10604 73 : constructor_max_index = integer_minus_one_node;
10605 :
10606 9429 : constructor_index
10607 9429 : = convert (bitsizetype,
10608 9429 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10609 : }
10610 : else
10611 : {
10612 8472 : constructor_index = bitsize_zero_node;
10613 8472 : constructor_max_index = NULL_TREE;
10614 : }
10615 :
10616 17901 : constructor_unfilled_index = constructor_index;
10617 : }
10618 926086 : else if (gnu_vector_type_p (constructor_type))
10619 : {
10620 : /* Vectors are like simple fixed-size arrays. */
10621 1851060 : constructor_max_index =
10622 925530 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10623 925530 : constructor_index = bitsize_zero_node;
10624 925530 : constructor_unfilled_index = constructor_index;
10625 : }
10626 : else
10627 : {
10628 : /* Handle the case of int x = {5}; */
10629 556 : constructor_fields = constructor_type;
10630 556 : constructor_unfilled_fields = constructor_type;
10631 556 : constructor_braced_scalar = true;
10632 : }
10633 1025446 : }
10634 :
10635 : extern location_t last_init_list_comma;
10636 :
10637 : /* Called when we see an open brace for a nested initializer. Finish
10638 : off any pending levels with implicit braces. */
10639 : void
10640 340562 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10641 : {
10642 340564 : while (constructor_stack->implicit)
10643 : {
10644 304 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10645 140 : && constructor_fields == NULL_TREE)
10646 0 : process_init_element (input_location,
10647 : pop_init_level (loc, 1, braced_init_obstack,
10648 : last_init_list_comma),
10649 : true, braced_init_obstack);
10650 304 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10651 164 : && constructor_max_index
10652 468 : && tree_int_cst_lt (constructor_max_index,
10653 : constructor_index))
10654 2 : process_init_element (input_location,
10655 : pop_init_level (loc, 1, braced_init_obstack,
10656 : last_init_list_comma),
10657 : true, braced_init_obstack);
10658 : else
10659 : break;
10660 : }
10661 340562 : }
10662 :
10663 : /* Push down into a subobject, for initialization.
10664 : If this is for an explicit set of braces, IMPLICIT is 0.
10665 : If it is because the next element belongs at a lower level,
10666 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10667 :
10668 : void
10669 1043242 : push_init_level (location_t loc, int implicit,
10670 : struct obstack *braced_init_obstack)
10671 : {
10672 1043242 : struct constructor_stack *p;
10673 1043242 : tree value = NULL_TREE;
10674 :
10675 : /* Unless this is an explicit brace, we need to preserve previous
10676 : content if any. */
10677 1043242 : if (implicit)
10678 : {
10679 703622 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10680 798 : value = find_init_member (constructor_fields, braced_init_obstack);
10681 702824 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10682 702824 : value = find_init_member (constructor_index, braced_init_obstack);
10683 : }
10684 :
10685 1043242 : p = XNEW (struct constructor_stack);
10686 1043242 : p->type = constructor_type;
10687 1043242 : p->fields = constructor_fields;
10688 1043242 : p->index = constructor_index;
10689 1043242 : p->max_index = constructor_max_index;
10690 1043242 : p->unfilled_index = constructor_unfilled_index;
10691 1043242 : p->unfilled_fields = constructor_unfilled_fields;
10692 1043242 : p->bit_index = constructor_bit_index;
10693 1043242 : p->elements = constructor_elements;
10694 1043242 : p->constant = constructor_constant;
10695 1043242 : p->simple = constructor_simple;
10696 1043242 : p->nonconst = constructor_nonconst;
10697 1043242 : p->erroneous = constructor_erroneous;
10698 1043242 : p->pending_elts = constructor_pending_elts;
10699 1043242 : p->depth = constructor_depth;
10700 1043242 : p->replacement_value.value = NULL_TREE;
10701 1043242 : p->replacement_value.original_code = ERROR_MARK;
10702 1043242 : p->replacement_value.original_type = NULL;
10703 1043242 : p->implicit = implicit;
10704 1043242 : p->outer = 0;
10705 1043242 : p->incremental = constructor_incremental;
10706 1043242 : p->designated = constructor_designated;
10707 1043242 : p->zero_padding_bits = constructor_zero_padding_bits;
10708 1043242 : p->braced_scalar = constructor_braced_scalar;
10709 1043242 : p->designator_depth = designator_depth;
10710 1043242 : p->next = constructor_stack;
10711 1043242 : p->range_stack = 0;
10712 1043242 : constructor_stack = p;
10713 :
10714 1043242 : constructor_constant = 1;
10715 1043242 : constructor_simple = 1;
10716 1043242 : constructor_nonconst = 0;
10717 1043242 : constructor_depth = SPELLING_DEPTH ();
10718 1043242 : constructor_elements = NULL;
10719 1043242 : constructor_incremental = 1;
10720 : /* If the upper initializer is designated, then mark this as
10721 : designated too to prevent bogus warnings. */
10722 1043242 : constructor_designated = p->designated;
10723 : /* If the upper initializer has padding bits zeroed, that includes
10724 : all nested initializers as well. */
10725 1043242 : constructor_zero_padding_bits = p->zero_padding_bits;
10726 1043242 : constructor_braced_scalar = false;
10727 1043242 : constructor_pending_elts = 0;
10728 1043242 : if (!implicit)
10729 : {
10730 339620 : p->range_stack = constructor_range_stack;
10731 339620 : constructor_range_stack = 0;
10732 339620 : designator_depth = 0;
10733 339620 : designator_erroneous = 0;
10734 : }
10735 :
10736 : /* Don't die if an entire brace-pair level is superfluous
10737 : in the containing level. */
10738 1043242 : if (constructor_type == NULL_TREE)
10739 : ;
10740 1043242 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10741 : {
10742 : /* Don't die if there are extra init elts at the end. */
10743 158858 : if (constructor_fields == NULL_TREE)
10744 51 : constructor_type = NULL_TREE;
10745 : else
10746 : {
10747 158807 : constructor_type = TREE_TYPE (constructor_fields);
10748 158807 : push_member_name (constructor_fields);
10749 158807 : constructor_depth++;
10750 : }
10751 : }
10752 884384 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10753 : {
10754 884333 : constructor_type = TREE_TYPE (constructor_type);
10755 884333 : push_array_bounds (tree_to_uhwi (constructor_index));
10756 884333 : constructor_depth++;
10757 : }
10758 :
10759 1043242 : if (constructor_type == NULL_TREE)
10760 : {
10761 51 : error_init (loc, "extra brace group at end of initializer");
10762 51 : constructor_fields = NULL_TREE;
10763 51 : constructor_unfilled_fields = NULL_TREE;
10764 51 : return;
10765 : }
10766 :
10767 1043191 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10768 : {
10769 360 : constructor_constant = TREE_CONSTANT (value);
10770 360 : constructor_simple = TREE_STATIC (value);
10771 360 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10772 360 : constructor_elements = CONSTRUCTOR_ELTS (value);
10773 360 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10774 360 : if (!vec_safe_is_empty (constructor_elements)
10775 340 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10776 340 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10777 294 : set_nonincremental_init (braced_init_obstack);
10778 : }
10779 :
10780 1043191 : if (implicit == 1)
10781 : {
10782 702680 : found_missing_braces = 1;
10783 702680 : if (initializer_stack->missing_brace_richloc)
10784 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10785 702680 : (loc, "{");
10786 : }
10787 :
10788 1043191 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10789 : {
10790 879733 : constructor_fields = TYPE_FIELDS (constructor_type);
10791 : /* Skip any nameless bit fields at the beginning. */
10792 879733 : while (constructor_fields != NULL_TREE
10793 879797 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10794 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10795 :
10796 879733 : constructor_unfilled_fields = constructor_fields;
10797 879733 : constructor_bit_index = bitsize_zero_node;
10798 : }
10799 163458 : else if (gnu_vector_type_p (constructor_type))
10800 : {
10801 : /* Vectors are like simple fixed-size arrays. */
10802 9602 : constructor_max_index =
10803 4801 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10804 4801 : constructor_index = bitsize_int (0);
10805 4801 : constructor_unfilled_index = constructor_index;
10806 : }
10807 158657 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10808 : {
10809 158577 : if (TYPE_DOMAIN (constructor_type))
10810 : {
10811 158577 : constructor_max_index
10812 158577 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10813 :
10814 : /* Detect non-empty initializations of zero-length arrays. */
10815 158577 : if (constructor_max_index == NULL_TREE
10816 158577 : && TYPE_SIZE (constructor_type))
10817 135 : constructor_max_index = integer_minus_one_node;
10818 :
10819 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10820 : to initialize VLAs will cause a proper error; avoid tree
10821 : checking errors as well by setting a safe value. */
10822 158577 : if (constructor_max_index
10823 158307 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10824 2 : constructor_max_index = integer_minus_one_node;
10825 :
10826 158577 : constructor_index
10827 158577 : = convert (bitsizetype,
10828 158577 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10829 : }
10830 : else
10831 0 : constructor_index = bitsize_zero_node;
10832 :
10833 158577 : constructor_unfilled_index = constructor_index;
10834 158577 : if (value && TREE_CODE (value) == STRING_CST)
10835 : {
10836 : /* We need to split the char/wchar array into individual
10837 : characters, so that we don't have to special case it
10838 : everywhere. */
10839 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10840 : }
10841 : }
10842 : else
10843 : {
10844 80 : if (constructor_type != error_mark_node)
10845 : {
10846 51 : if (p->braced_scalar)
10847 22 : permerror_init (input_location, 0,
10848 : "braces around scalar initializer");
10849 : else
10850 29 : warning_init (input_location, 0,
10851 : "braces around scalar initializer");
10852 51 : constructor_braced_scalar = true;
10853 : }
10854 80 : constructor_fields = constructor_type;
10855 80 : constructor_unfilled_fields = constructor_type;
10856 : }
10857 : }
10858 :
10859 : /* At the end of an implicit or explicit brace level,
10860 : finish up that level of constructor. If a single expression
10861 : with redundant braces initialized that level, return the
10862 : c_expr structure for that expression. Otherwise, the original_code
10863 : element is set to ERROR_MARK.
10864 : If we were outputting the elements as they are read, return 0 as the value
10865 : from inner levels (process_init_element ignores that),
10866 : but return error_mark_node as the value from the outermost level
10867 : (that's what we want to put in DECL_INITIAL).
10868 : Otherwise, return a CONSTRUCTOR expression as the value. */
10869 :
10870 : struct c_expr
10871 2068688 : pop_init_level (location_t loc, int implicit,
10872 : struct obstack *braced_init_obstack,
10873 : location_t insert_before)
10874 : {
10875 2068688 : struct constructor_stack *p;
10876 2068688 : struct c_expr ret;
10877 2068688 : ret.value = NULL_TREE;
10878 2068688 : ret.original_code = ERROR_MARK;
10879 2068688 : ret.original_type = NULL;
10880 2068688 : ret.m_decimal = 0;
10881 :
10882 2068688 : if (implicit == 0)
10883 : {
10884 : /* When we come to an explicit close brace,
10885 : pop any inner levels that didn't have explicit braces. */
10886 1365999 : while (constructor_stack->implicit)
10887 933 : process_init_element (input_location,
10888 : pop_init_level (loc, 1, braced_init_obstack,
10889 : insert_before),
10890 : true, braced_init_obstack);
10891 1365066 : gcc_assert (!constructor_range_stack);
10892 : }
10893 : else
10894 703622 : if (initializer_stack->missing_brace_richloc)
10895 703622 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10896 703622 : (insert_before, "}");
10897 :
10898 : /* Now output all pending elements. */
10899 2068688 : constructor_incremental = 1;
10900 2068688 : output_pending_init_elements (1, braced_init_obstack);
10901 :
10902 2068688 : p = constructor_stack;
10903 :
10904 : /* Error for initializing a flexible array member, or a zero-length
10905 : array member in an inappropriate context. */
10906 2068637 : if (constructor_type && constructor_fields
10907 159388 : && TREE_CODE (constructor_type) == ARRAY_TYPE
10908 152625 : && TYPE_DOMAIN (constructor_type)
10909 2221305 : && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10910 : {
10911 : /* Silently discard empty initializations. The parser will
10912 : already have pedwarned for empty brackets for C17 and earlier. */
10913 300 : if (integer_zerop (constructor_unfilled_index))
10914 84 : constructor_type = NULL_TREE;
10915 300 : if (constructor_type || flag_isoc23)
10916 : {
10917 296 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10918 :
10919 296 : if (constructor_depth <= 2)
10920 249 : pedwarn_init (loc, OPT_Wpedantic,
10921 : "initialization of a flexible array member");
10922 47 : else if (constructor_type)
10923 33 : error_init (loc, "initialization of flexible array member "
10924 : "in a nested context");
10925 : else
10926 14 : pedwarn_init (loc, OPT_Wpedantic,
10927 : "initialization of flexible array member "
10928 : "in a nested context");
10929 :
10930 : /* We have already issued an error message for the existence
10931 : of a flexible array member not at the end of the structure.
10932 : Discard the initializer so that we do not die later. */
10933 296 : if (constructor_type
10934 216 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10935 302 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10936 0 : constructor_type = NULL_TREE;
10937 : }
10938 : }
10939 :
10940 2068688 : switch (vec_safe_length (constructor_elements))
10941 : {
10942 4701 : case 0:
10943 : /* Initialization with { } counts as zeroinit. */
10944 4701 : constructor_zeroinit = 1;
10945 4701 : break;
10946 915208 : case 1:
10947 : /* This might be zeroinit as well. */
10948 915208 : if (integer_zerop ((*constructor_elements)[0].value))
10949 2046 : constructor_zeroinit = 1;
10950 : break;
10951 1148779 : default:
10952 : /* If the constructor has more than one element, it can't be { 0 }. */
10953 1148779 : constructor_zeroinit = 0;
10954 1148779 : break;
10955 : }
10956 :
10957 : /* Warn when some structs are initialized with direct aggregation. */
10958 2068688 : if (!implicit && found_missing_braces && warn_missing_braces
10959 37 : && !constructor_zeroinit)
10960 : {
10961 18 : gcc_assert (initializer_stack->missing_brace_richloc);
10962 18 : warning_at (initializer_stack->missing_brace_richloc,
10963 18 : OPT_Wmissing_braces,
10964 : "missing braces around initializer");
10965 : }
10966 :
10967 : /* Warn when some struct elements are implicitly initialized to zero. */
10968 2068688 : if (warn_missing_field_initializers
10969 425443 : && constructor_type
10970 425415 : && TREE_CODE (constructor_type) == RECORD_TYPE
10971 193395 : && constructor_unfilled_fields)
10972 : {
10973 : /* Do not warn for flexible array members or zero-length arrays. */
10974 43 : while (constructor_unfilled_fields
10975 43 : && (!DECL_SIZE (constructor_unfilled_fields)
10976 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10977 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10978 :
10979 43 : if (constructor_unfilled_fields
10980 : /* Do not warn if this level of the initializer uses member
10981 : designators; it is likely to be deliberate. */
10982 43 : && !constructor_designated
10983 : /* Do not warn about initializing with { 0 } or with { }. */
10984 24 : && !constructor_zeroinit)
10985 : {
10986 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
10987 : "missing initializer for field %qD of %qT",
10988 : constructor_unfilled_fields,
10989 : constructor_type))
10990 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10991 : "%qD declared here", constructor_unfilled_fields);
10992 : }
10993 : }
10994 :
10995 : /* Pad out the end of the structure. */
10996 2068688 : if (p->replacement_value.value)
10997 : /* If this closes a superfluous brace pair,
10998 : just pass out the element between them. */
10999 138 : ret = p->replacement_value;
11000 2068550 : else if (constructor_type == NULL_TREE)
11001 : ;
11002 2068423 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
11003 2068423 : && TREE_CODE (constructor_type) != ARRAY_TYPE
11004 2068423 : && !gnu_vector_type_p (constructor_type))
11005 : {
11006 : /* A nonincremental scalar initializer--just return
11007 : the element, after verifying there is just one.
11008 : Empty scalar initializers are supported in C23. */
11009 636 : if (vec_safe_is_empty (constructor_elements))
11010 : {
11011 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11012 85 : ret.value = error_mark_node;
11013 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11014 : {
11015 2 : error_init (loc, "invalid initializer");
11016 2 : ret.value = error_mark_node;
11017 : }
11018 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11019 : /* Ensure this is a null pointer constant in the case of a
11020 : 'constexpr' object initialized with {}. */
11021 33 : ret.value = build_zero_cst (ptr_type_node);
11022 : else
11023 81 : ret.value = build_zero_cst (constructor_type);
11024 : }
11025 435 : else if (vec_safe_length (constructor_elements) != 1)
11026 : {
11027 0 : error_init (loc, "extra elements in scalar initializer");
11028 0 : ret.value = (*constructor_elements)[0].value;
11029 : }
11030 : else
11031 435 : ret.value = (*constructor_elements)[0].value;
11032 : }
11033 : else
11034 : {
11035 2067787 : if (constructor_erroneous)
11036 330 : ret.value = error_mark_node;
11037 : else
11038 : {
11039 2067457 : ret.value = build_constructor (constructor_type,
11040 : constructor_elements);
11041 2067457 : if (constructor_constant)
11042 1737946 : TREE_CONSTANT (ret.value) = 1;
11043 2067457 : if (constructor_constant && constructor_simple)
11044 1737907 : TREE_STATIC (ret.value) = 1;
11045 2067457 : if (constructor_nonconst)
11046 672 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11047 2067457 : if (constructor_zero_padding_bits)
11048 20 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11049 : }
11050 : }
11051 :
11052 2068688 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11053 : {
11054 1104 : if (constructor_nonconst)
11055 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11056 1089 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11057 0 : ret.original_code = ERROR_MARK;
11058 : }
11059 :
11060 2068688 : constructor_type = p->type;
11061 2068688 : constructor_fields = p->fields;
11062 2068688 : constructor_index = p->index;
11063 2068688 : constructor_max_index = p->max_index;
11064 2068688 : constructor_unfilled_index = p->unfilled_index;
11065 2068688 : constructor_unfilled_fields = p->unfilled_fields;
11066 2068688 : constructor_bit_index = p->bit_index;
11067 2068688 : constructor_elements = p->elements;
11068 2068688 : constructor_constant = p->constant;
11069 2068688 : constructor_simple = p->simple;
11070 2068688 : constructor_nonconst = p->nonconst;
11071 2068688 : constructor_erroneous = p->erroneous;
11072 2068688 : constructor_incremental = p->incremental;
11073 2068688 : constructor_designated = p->designated;
11074 2068688 : constructor_zero_padding_bits = p->zero_padding_bits;
11075 2068688 : constructor_braced_scalar = p->braced_scalar;
11076 2068688 : designator_depth = p->designator_depth;
11077 2068688 : constructor_pending_elts = p->pending_elts;
11078 2068688 : constructor_depth = p->depth;
11079 2068688 : if (!p->implicit)
11080 1365066 : constructor_range_stack = p->range_stack;
11081 2068688 : RESTORE_SPELLING_DEPTH (constructor_depth);
11082 :
11083 2068688 : constructor_stack = p->next;
11084 2068688 : XDELETE (p);
11085 :
11086 2068688 : if (ret.value == NULL_TREE && constructor_stack == 0)
11087 0 : ret.value = error_mark_node;
11088 2068688 : return ret;
11089 : }
11090 :
11091 : /* Common handling for both array range and field name designators.
11092 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11093 :
11094 : static bool
11095 34067 : set_designator (location_t loc, bool array,
11096 : struct obstack *braced_init_obstack)
11097 : {
11098 34067 : tree subtype;
11099 34067 : enum tree_code subcode;
11100 :
11101 : /* Don't die if an entire brace-pair level is superfluous
11102 : in the containing level, or for an erroneous type. */
11103 34067 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11104 : return true;
11105 :
11106 : /* If there were errors in this designator list already, bail out
11107 : silently. */
11108 34058 : if (designator_erroneous)
11109 : return true;
11110 :
11111 : /* Likewise for an initializer for a variable-size type. Those are
11112 : diagnosed in the parser, except for empty initializer braces. */
11113 34058 : if (COMPLETE_TYPE_P (constructor_type)
11114 34058 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11115 : return true;
11116 :
11117 34048 : if (!designator_depth)
11118 : {
11119 33430 : gcc_assert (!constructor_range_stack);
11120 :
11121 : /* Designator list starts at the level of closest explicit
11122 : braces. */
11123 33813 : while (constructor_stack->implicit)
11124 383 : process_init_element (input_location,
11125 : pop_init_level (loc, 1, braced_init_obstack,
11126 : last_init_list_comma),
11127 : true, braced_init_obstack);
11128 33430 : constructor_designated = 1;
11129 33430 : return false;
11130 : }
11131 :
11132 618 : switch (TREE_CODE (constructor_type))
11133 : {
11134 421 : case RECORD_TYPE:
11135 421 : case UNION_TYPE:
11136 421 : subtype = TREE_TYPE (constructor_fields);
11137 421 : if (subtype != error_mark_node)
11138 421 : subtype = TYPE_MAIN_VARIANT (subtype);
11139 : break;
11140 197 : case ARRAY_TYPE:
11141 197 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11142 197 : break;
11143 0 : default:
11144 0 : gcc_unreachable ();
11145 : }
11146 :
11147 618 : subcode = TREE_CODE (subtype);
11148 618 : if (array && subcode != ARRAY_TYPE)
11149 : {
11150 1 : error_init (loc, "array index in non-array initializer");
11151 1 : return true;
11152 : }
11153 617 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11154 : {
11155 0 : error_init (loc, "field name not in record or union initializer");
11156 0 : return true;
11157 : }
11158 :
11159 617 : constructor_designated = 1;
11160 617 : finish_implicit_inits (loc, braced_init_obstack);
11161 617 : push_init_level (loc, 2, braced_init_obstack);
11162 617 : return false;
11163 : }
11164 :
11165 : /* If there are range designators in designator list, push a new designator
11166 : to constructor_range_stack. RANGE_END is end of such stack range or
11167 : NULL_TREE if there is no range designator at this level. */
11168 :
11169 : static void
11170 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11171 : {
11172 390 : struct constructor_range_stack *p;
11173 :
11174 780 : p = (struct constructor_range_stack *)
11175 390 : obstack_alloc (braced_init_obstack,
11176 : sizeof (struct constructor_range_stack));
11177 390 : p->prev = constructor_range_stack;
11178 390 : p->next = 0;
11179 390 : p->fields = constructor_fields;
11180 390 : p->range_start = constructor_index;
11181 390 : p->index = constructor_index;
11182 390 : p->stack = constructor_stack;
11183 390 : p->range_end = range_end;
11184 390 : if (constructor_range_stack)
11185 19 : constructor_range_stack->next = p;
11186 390 : constructor_range_stack = p;
11187 390 : }
11188 :
11189 : /* Within an array initializer, specify the next index to be initialized.
11190 : FIRST is that index. If LAST is nonzero, then initialize a range
11191 : of indices, running from FIRST through LAST. */
11192 :
11193 : void
11194 1149 : set_init_index (location_t loc, tree first, tree last,
11195 : struct obstack *braced_init_obstack)
11196 : {
11197 1149 : if (set_designator (loc, true, braced_init_obstack))
11198 : return;
11199 :
11200 1146 : designator_erroneous = 1;
11201 :
11202 2292 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11203 2281 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11204 : {
11205 12 : error_init (loc, "array index in initializer not of integer type");
11206 12 : return;
11207 : }
11208 :
11209 1134 : if (TREE_CODE (first) != INTEGER_CST)
11210 : {
11211 6 : first = c_fully_fold (first, false, NULL);
11212 6 : if (TREE_CODE (first) == INTEGER_CST)
11213 5 : pedwarn_init (loc, OPT_Wpedantic,
11214 : "array index in initializer is not "
11215 : "an integer constant expression");
11216 : }
11217 :
11218 1134 : if (last && TREE_CODE (last) != INTEGER_CST)
11219 : {
11220 2 : last = c_fully_fold (last, false, NULL);
11221 2 : if (TREE_CODE (last) == INTEGER_CST)
11222 1 : pedwarn_init (loc, OPT_Wpedantic,
11223 : "array index in initializer is not "
11224 : "an integer constant expression");
11225 : }
11226 :
11227 1134 : if (TREE_CODE (first) != INTEGER_CST)
11228 1 : error_init (loc, "nonconstant array index in initializer");
11229 1133 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11230 1 : error_init (loc, "nonconstant array index in initializer");
11231 1132 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11232 9 : error_init (loc, "array index in non-array initializer");
11233 1123 : else if (tree_int_cst_sgn (first) == -1)
11234 15 : error_init (loc, "array index in initializer exceeds array bounds");
11235 1108 : else if (constructor_max_index
11236 1108 : && tree_int_cst_lt (constructor_max_index, first))
11237 7 : error_init (loc, "array index in initializer exceeds array bounds");
11238 : else
11239 : {
11240 1101 : constant_expression_warning (first);
11241 1101 : if (last)
11242 385 : constant_expression_warning (last);
11243 1101 : constructor_index = convert (bitsizetype, first);
11244 1101 : if (tree_int_cst_lt (constructor_index, first))
11245 : {
11246 0 : constructor_index = copy_node (constructor_index);
11247 0 : TREE_OVERFLOW (constructor_index) = 1;
11248 : }
11249 :
11250 1101 : if (last)
11251 : {
11252 385 : if (tree_int_cst_equal (first, last))
11253 : last = NULL_TREE;
11254 384 : else if (tree_int_cst_lt (last, first))
11255 : {
11256 2 : error_init (loc, "empty index range in initializer");
11257 2 : last = NULL_TREE;
11258 : }
11259 : else
11260 : {
11261 382 : last = convert (bitsizetype, last);
11262 382 : if (constructor_max_index != NULL_TREE
11263 382 : && tree_int_cst_lt (constructor_max_index, last))
11264 : {
11265 3 : error_init (loc, "array index range in initializer exceeds "
11266 : "array bounds");
11267 3 : last = NULL_TREE;
11268 : }
11269 : }
11270 : }
11271 :
11272 1101 : designator_depth++;
11273 1101 : designator_erroneous = 0;
11274 1101 : if (constructor_range_stack || last)
11275 379 : push_range_stack (last, braced_init_obstack);
11276 : }
11277 : }
11278 :
11279 : /* Within a struct initializer, specify the next field to be initialized. */
11280 :
11281 : void
11282 32873 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11283 : struct obstack *braced_init_obstack)
11284 : {
11285 32873 : tree field;
11286 :
11287 32873 : if (set_designator (loc, false, braced_init_obstack))
11288 : return;
11289 :
11290 32856 : designator_erroneous = 1;
11291 :
11292 32856 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11293 : {
11294 3 : error_init (loc, "field name not in record or union initializer");
11295 3 : return;
11296 : }
11297 :
11298 32853 : field = lookup_field (constructor_type, fieldname);
11299 :
11300 32853 : if (field == NULL_TREE)
11301 : {
11302 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11303 8 : if (guessed_id)
11304 : {
11305 4 : gcc_rich_location rich_loc (fieldname_loc);
11306 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11307 4 : error_at (&rich_loc,
11308 : "%qT has no member named %qE; did you mean %qE?",
11309 : constructor_type, fieldname, guessed_id);
11310 4 : }
11311 : else
11312 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11313 : constructor_type, fieldname);
11314 : }
11315 : else
11316 32890 : do
11317 : {
11318 32890 : constructor_fields = TREE_VALUE (field);
11319 32890 : designator_depth++;
11320 32890 : designator_erroneous = 0;
11321 32890 : if (constructor_range_stack)
11322 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11323 32890 : field = TREE_CHAIN (field);
11324 32890 : if (field)
11325 : {
11326 45 : if (set_designator (loc, false, braced_init_obstack))
11327 : return;
11328 : }
11329 : }
11330 32890 : while (field != NULL_TREE);
11331 : }
11332 :
11333 : /* Helper function for add_pending_init. Find inorder successor of P
11334 : in AVL tree. */
11335 : static struct init_node *
11336 129 : init_node_successor (struct init_node *p)
11337 : {
11338 129 : struct init_node *r;
11339 129 : if (p->right)
11340 : {
11341 : r = p->right;
11342 58 : while (r->left)
11343 : r = r->left;
11344 : return r;
11345 : }
11346 75 : r = p->parent;
11347 114 : while (r && p == r->right)
11348 : {
11349 39 : p = r;
11350 39 : r = r->parent;
11351 : }
11352 : return r;
11353 : }
11354 :
11355 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11356 : identifies the initializer, either array index or field in a structure.
11357 : VALUE is the value of that index or field. If ORIGTYPE is not
11358 : NULL_TREE, it is the original type of VALUE.
11359 :
11360 : IMPLICIT is true if value comes from pop_init_level (1),
11361 : the new initializer has been merged with the existing one
11362 : and thus no warnings should be emitted about overriding an
11363 : existing initializer. */
11364 :
11365 : static void
11366 3269 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11367 : bool implicit, struct obstack *braced_init_obstack)
11368 : {
11369 3269 : struct init_node *p, **q, *r;
11370 :
11371 3269 : q = &constructor_pending_elts;
11372 3269 : p = 0;
11373 :
11374 3269 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11375 : {
11376 3610 : while (*q != 0)
11377 : {
11378 2412 : p = *q;
11379 2412 : if (tree_int_cst_lt (purpose, p->purpose))
11380 283 : q = &p->left;
11381 2129 : else if (tree_int_cst_lt (p->purpose, purpose))
11382 : {
11383 1960 : if (TREE_CODE (p->value) != RAW_DATA_CST
11384 1960 : || (p->right
11385 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11386 1830 : q = &p->right;
11387 : else
11388 : {
11389 130 : widest_int pp = wi::to_widest (p->purpose);
11390 130 : widest_int pw = wi::to_widest (purpose);
11391 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11392 98 : q = &p->right;
11393 : else
11394 : {
11395 : /* Override which should split the old RAW_DATA_CST
11396 : into 2 or 3 pieces. */
11397 32 : if (!implicit && warn_override_init)
11398 9 : warning_init (loc, OPT_Woverride_init,
11399 : "initialized field overwritten");
11400 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11401 32 : unsigned HOST_WIDE_INT len = 1;
11402 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11403 0 : len = RAW_DATA_LENGTH (value);
11404 32 : unsigned HOST_WIDE_INT end = 0;
11405 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11406 32 : gcc_checking_assert (start < plen && start);
11407 32 : if (plen - start > len)
11408 30 : end = plen - start - len;
11409 32 : tree v = p->value;
11410 32 : tree origtype = p->origtype;
11411 32 : if (start == 1)
11412 2 : p->value = build_int_cst (TREE_TYPE (v),
11413 2 : RAW_DATA_UCHAR_ELT (v, 0));
11414 : else
11415 : {
11416 30 : p->value = v;
11417 30 : if (end > 1)
11418 26 : v = copy_node (v);
11419 30 : RAW_DATA_LENGTH (p->value) = start;
11420 : }
11421 32 : if (end)
11422 : {
11423 30 : tree epurpose
11424 30 : = size_binop (PLUS_EXPR, purpose,
11425 : bitsize_int (len));
11426 30 : if (end > 1)
11427 : {
11428 28 : RAW_DATA_LENGTH (v) -= plen - end;
11429 28 : RAW_DATA_POINTER (v) += plen - end;
11430 : }
11431 : else
11432 2 : v = build_int_cst (TREE_TYPE (v),
11433 2 : RAW_DATA_UCHAR_ELT (v, plen
11434 : - end));
11435 30 : add_pending_init (loc, epurpose, v, origtype,
11436 : implicit, braced_init_obstack);
11437 : }
11438 32 : q = &constructor_pending_elts;
11439 32 : continue;
11440 32 : }
11441 130 : }
11442 : }
11443 : else
11444 : {
11445 169 : if (TREE_CODE (p->value) == RAW_DATA_CST
11446 179 : && (RAW_DATA_LENGTH (p->value)
11447 10 : > (TREE_CODE (value) == RAW_DATA_CST
11448 10 : ? RAW_DATA_LENGTH (value) : 1)))
11449 : {
11450 : /* Override which should split the old RAW_DATA_CST
11451 : into 2 pieces. */
11452 6 : if (!implicit && warn_override_init)
11453 3 : warning_init (loc, OPT_Woverride_init,
11454 : "initialized field overwritten");
11455 6 : unsigned HOST_WIDE_INT len = 1;
11456 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11457 2 : len = RAW_DATA_LENGTH (value);
11458 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11459 : {
11460 6 : RAW_DATA_LENGTH (p->value) -= len;
11461 6 : RAW_DATA_POINTER (p->value) += len;
11462 : }
11463 : else
11464 : {
11465 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11466 0 : p->value
11467 0 : = build_int_cst (TREE_TYPE (p->value),
11468 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11469 : }
11470 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11471 : bitsize_int (len));
11472 6 : continue;
11473 6 : }
11474 163 : if (TREE_CODE (value) == RAW_DATA_CST)
11475 : {
11476 8 : handle_raw_data:
11477 : /* RAW_DATA_CST value might overlap various further
11478 : prior initval entries. Find out how many. */
11479 13 : unsigned cnt = 0;
11480 13 : widest_int w
11481 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11482 13 : struct init_node *r = p, *last = NULL;
11483 13 : bool override_init = warn_override_init;
11484 13 : while ((r = init_node_successor (r))
11485 53 : && wi::to_widest (r->purpose) < w)
11486 : {
11487 40 : ++cnt;
11488 40 : if (TREE_SIDE_EFFECTS (r->value))
11489 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11490 : "initialized field with side-effects "
11491 : "overwritten");
11492 38 : else if (override_init)
11493 : {
11494 6 : warning_init (loc, OPT_Woverride_init,
11495 : "initialized field overwritten");
11496 6 : override_init = false;
11497 : }
11498 : last = r;
11499 : }
11500 13 : if (cnt)
11501 : {
11502 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11503 13 : && (wi::to_widest (last->purpose)
11504 13 : + RAW_DATA_LENGTH (last->value) > w))
11505 : {
11506 : /* The last overlapping prior initval overlaps
11507 : only partially. Shrink it and decrease cnt. */
11508 0 : unsigned int l = (wi::to_widest (last->purpose)
11509 0 : + RAW_DATA_LENGTH (last->value)
11510 0 : - w).to_uhwi ();
11511 0 : --cnt;
11512 0 : RAW_DATA_LENGTH (last->value) -= l;
11513 0 : RAW_DATA_POINTER (last->value) += l;
11514 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11515 0 : last->value
11516 0 : = build_int_cst (TREE_TYPE (last->value),
11517 0 : RAW_DATA_UCHAR_ELT (last->value,
11518 : 0));
11519 0 : last->purpose
11520 0 : = size_binop (PLUS_EXPR, last->purpose,
11521 : bitsize_int (l));
11522 : }
11523 : /* Instead of deleting cnt nodes from the AVL tree
11524 : and rebalancing, peel of last cnt bytes from the
11525 : RAW_DATA_CST. Overriding thousands of previously
11526 : initialized array elements with #embed needs to work,
11527 : but doesn't need to be super efficient. */
11528 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11529 : > cnt);
11530 13 : RAW_DATA_LENGTH (value) -= cnt;
11531 13 : const unsigned char *s
11532 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11533 13 : unsigned int o = RAW_DATA_LENGTH (value);
11534 53 : for (r = p; cnt--; ++o, ++s)
11535 : {
11536 40 : r = init_node_successor (r);
11537 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11538 : bitsize_int (o));
11539 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11540 40 : r->origtype = origtype;
11541 : }
11542 13 : if (RAW_DATA_LENGTH (value) == 1)
11543 0 : value = build_int_cst (TREE_TYPE (value),
11544 0 : RAW_DATA_UCHAR_ELT (value, 0));
11545 : }
11546 : }
11547 168 : if (!implicit)
11548 : {
11549 54 : if (TREE_SIDE_EFFECTS (p->value))
11550 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11551 : "initialized field with side-effects "
11552 : "overwritten");
11553 48 : else if (warn_override_init)
11554 13 : warning_init (loc, OPT_Woverride_init,
11555 : "initialized field overwritten");
11556 : }
11557 168 : p->value = value;
11558 168 : p->origtype = origtype;
11559 168 : return;
11560 : }
11561 : }
11562 1198 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11563 : {
11564 61 : struct init_node *r;
11565 61 : if (q == &p->left)
11566 : r = p;
11567 : else
11568 36 : r = init_node_successor (p);
11569 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11570 131 : + RAW_DATA_LENGTH (value)))
11571 : {
11572 : /* Overlap with at least one prior initval in the range but
11573 : not at the start. */
11574 5 : p = r;
11575 5 : p->purpose = purpose;
11576 5 : goto handle_raw_data;
11577 : }
11578 : }
11579 : }
11580 : else
11581 : {
11582 1908 : tree bitpos;
11583 :
11584 1908 : bitpos = bit_position (purpose);
11585 5430 : while (*q != NULL)
11586 : {
11587 1871 : p = *q;
11588 1871 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11589 43 : q = &p->left;
11590 1828 : else if (p->purpose != purpose)
11591 1571 : q = &p->right;
11592 : else
11593 : {
11594 257 : if (!implicit)
11595 : {
11596 33 : if (TREE_SIDE_EFFECTS (p->value))
11597 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11598 : "initialized field with side-effects "
11599 : "overwritten");
11600 29 : else if (warn_override_init)
11601 6 : warning_init (loc, OPT_Woverride_init,
11602 : "initialized field overwritten");
11603 : }
11604 257 : p->value = value;
11605 257 : p->origtype = origtype;
11606 257 : return;
11607 : }
11608 : }
11609 : }
11610 :
11611 2844 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11612 : sizeof (struct init_node));
11613 2844 : r->purpose = purpose;
11614 2844 : r->value = value;
11615 2844 : r->origtype = origtype;
11616 :
11617 2844 : *q = r;
11618 2844 : r->parent = p;
11619 2844 : r->left = 0;
11620 2844 : r->right = 0;
11621 2844 : r->balance = 0;
11622 :
11623 4878 : while (p)
11624 : {
11625 2826 : struct init_node *s;
11626 :
11627 2826 : if (r == p->left)
11628 : {
11629 237 : if (p->balance == 0)
11630 174 : p->balance = -1;
11631 63 : else if (p->balance < 0)
11632 : {
11633 45 : if (r->balance < 0)
11634 : {
11635 : /* L rotation. */
11636 40 : p->left = r->right;
11637 40 : if (p->left)
11638 2 : p->left->parent = p;
11639 40 : r->right = p;
11640 :
11641 40 : p->balance = 0;
11642 40 : r->balance = 0;
11643 :
11644 40 : s = p->parent;
11645 40 : p->parent = r;
11646 40 : r->parent = s;
11647 40 : if (s)
11648 : {
11649 31 : if (s->left == p)
11650 10 : s->left = r;
11651 : else
11652 21 : s->right = r;
11653 : }
11654 : else
11655 9 : constructor_pending_elts = r;
11656 : }
11657 : else
11658 : {
11659 : /* LR rotation. */
11660 5 : struct init_node *t = r->right;
11661 :
11662 5 : r->right = t->left;
11663 5 : if (r->right)
11664 0 : r->right->parent = r;
11665 5 : t->left = r;
11666 :
11667 5 : p->left = t->right;
11668 5 : if (p->left)
11669 0 : p->left->parent = p;
11670 5 : t->right = p;
11671 :
11672 5 : p->balance = t->balance < 0;
11673 5 : r->balance = -(t->balance > 0);
11674 5 : t->balance = 0;
11675 :
11676 5 : s = p->parent;
11677 5 : p->parent = t;
11678 5 : r->parent = t;
11679 5 : t->parent = s;
11680 5 : if (s)
11681 : {
11682 0 : if (s->left == p)
11683 0 : s->left = t;
11684 : else
11685 0 : s->right = t;
11686 : }
11687 : else
11688 5 : constructor_pending_elts = t;
11689 : }
11690 : break;
11691 : }
11692 : else
11693 : {
11694 : /* p->balance == +1; growth of left side balances the node. */
11695 18 : p->balance = 0;
11696 18 : break;
11697 : }
11698 : }
11699 : else /* r == p->right */
11700 : {
11701 2589 : if (p->balance == 0)
11702 : /* Growth propagation from right side. */
11703 1860 : p->balance++;
11704 729 : else if (p->balance > 0)
11705 : {
11706 700 : if (r->balance > 0)
11707 : {
11708 : /* R rotation. */
11709 698 : p->right = r->left;
11710 698 : if (p->right)
11711 173 : p->right->parent = p;
11712 698 : r->left = p;
11713 :
11714 698 : p->balance = 0;
11715 698 : r->balance = 0;
11716 :
11717 698 : s = p->parent;
11718 698 : p->parent = r;
11719 698 : r->parent = s;
11720 698 : if (s)
11721 : {
11722 378 : if (s->left == p)
11723 2 : s->left = r;
11724 : else
11725 376 : s->right = r;
11726 : }
11727 : else
11728 320 : constructor_pending_elts = r;
11729 : }
11730 : else /* r->balance == -1 */
11731 : {
11732 : /* RL rotation */
11733 2 : struct init_node *t = r->left;
11734 :
11735 2 : r->left = t->right;
11736 2 : if (r->left)
11737 2 : r->left->parent = r;
11738 2 : t->right = r;
11739 :
11740 2 : p->right = t->left;
11741 2 : if (p->right)
11742 2 : p->right->parent = p;
11743 2 : t->left = p;
11744 :
11745 2 : r->balance = (t->balance < 0);
11746 2 : p->balance = -(t->balance > 0);
11747 2 : t->balance = 0;
11748 :
11749 2 : s = p->parent;
11750 2 : p->parent = t;
11751 2 : r->parent = t;
11752 2 : t->parent = s;
11753 2 : if (s)
11754 : {
11755 0 : if (s->left == p)
11756 0 : s->left = t;
11757 : else
11758 0 : s->right = t;
11759 : }
11760 : else
11761 2 : constructor_pending_elts = t;
11762 : }
11763 : break;
11764 : }
11765 : else
11766 : {
11767 : /* p->balance == -1; growth of right side balances the node. */
11768 29 : p->balance = 0;
11769 29 : break;
11770 : }
11771 : }
11772 :
11773 2034 : r = p;
11774 2034 : p = p->parent;
11775 : }
11776 : }
11777 :
11778 : /* Build AVL tree from a sorted chain. */
11779 :
11780 : static void
11781 418 : set_nonincremental_init (struct obstack * braced_init_obstack)
11782 : {
11783 418 : unsigned HOST_WIDE_INT ix;
11784 418 : tree index, value;
11785 :
11786 418 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11787 418 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11788 : return;
11789 :
11790 1601 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11791 1183 : add_pending_init (input_location, index, value, NULL_TREE, true,
11792 : braced_init_obstack);
11793 418 : constructor_elements = NULL;
11794 418 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11795 : {
11796 200 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11797 : /* Skip any nameless bit fields at the beginning. */
11798 200 : while (constructor_unfilled_fields != NULL_TREE
11799 200 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11800 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11801 :
11802 : }
11803 218 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11804 : {
11805 218 : if (TYPE_DOMAIN (constructor_type))
11806 160 : constructor_unfilled_index
11807 160 : = convert (bitsizetype,
11808 160 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11809 : else
11810 58 : constructor_unfilled_index = bitsize_zero_node;
11811 : }
11812 418 : constructor_incremental = 0;
11813 : }
11814 :
11815 : /* Build AVL tree from a string constant. */
11816 :
11817 : static void
11818 7 : set_nonincremental_init_from_string (tree str,
11819 : struct obstack * braced_init_obstack)
11820 : {
11821 7 : tree value, purpose, type;
11822 7 : HOST_WIDE_INT val[2];
11823 7 : const char *p, *end;
11824 7 : int byte, wchar_bytes, charwidth, bitpos;
11825 :
11826 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11827 :
11828 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11829 7 : charwidth = TYPE_PRECISION (char_type_node);
11830 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11831 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11832 7 : type = TREE_TYPE (constructor_type);
11833 7 : p = TREE_STRING_POINTER (str);
11834 7 : end = p + TREE_STRING_LENGTH (str);
11835 :
11836 7 : for (purpose = bitsize_zero_node;
11837 : p < end
11838 52 : && !(constructor_max_index
11839 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11840 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11841 : {
11842 25 : if (wchar_bytes == 1)
11843 : {
11844 9 : val[0] = (unsigned char) *p++;
11845 9 : val[1] = 0;
11846 : }
11847 : else
11848 : {
11849 16 : val[1] = 0;
11850 16 : val[0] = 0;
11851 64 : for (byte = 0; byte < wchar_bytes; byte++)
11852 : {
11853 48 : if (BYTES_BIG_ENDIAN)
11854 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11855 : else
11856 48 : bitpos = byte * charwidth;
11857 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11858 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11859 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11860 : }
11861 : }
11862 :
11863 25 : if (!TYPE_UNSIGNED (type))
11864 : {
11865 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11866 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11867 : {
11868 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11869 : {
11870 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11871 0 : val[1] = -1;
11872 : }
11873 : }
11874 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11875 : {
11876 0 : if (val[0] < 0)
11877 0 : val[1] = -1;
11878 : }
11879 0 : else if (val[1] & (HOST_WIDE_INT_1
11880 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11881 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11882 : }
11883 :
11884 25 : value = wide_int_to_tree (type,
11885 25 : wide_int::from_array (val, 2,
11886 : HOST_BITS_PER_WIDE_INT * 2));
11887 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11888 : braced_init_obstack);
11889 : }
11890 :
11891 7 : constructor_incremental = 0;
11892 7 : }
11893 :
11894 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11895 : not initialized yet. */
11896 :
11897 : static tree
11898 703622 : find_init_member (tree field, struct obstack * braced_init_obstack)
11899 : {
11900 703622 : struct init_node *p;
11901 :
11902 703622 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11903 : {
11904 702824 : if (constructor_incremental
11905 702824 : && tree_int_cst_lt (field, constructor_unfilled_index))
11906 16 : set_nonincremental_init (braced_init_obstack);
11907 :
11908 702824 : p = constructor_pending_elts;
11909 702947 : while (p)
11910 : {
11911 233 : if (tree_int_cst_lt (field, p->purpose))
11912 25 : p = p->left;
11913 208 : else if (tree_int_cst_lt (p->purpose, field))
11914 98 : p = p->right;
11915 : else
11916 110 : return p->value;
11917 : }
11918 : }
11919 798 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11920 : {
11921 675 : tree bitpos = bit_position (field);
11922 :
11923 675 : if (constructor_incremental
11924 675 : && (!constructor_unfilled_fields
11925 472 : || tree_int_cst_lt (bitpos,
11926 472 : bit_position (constructor_unfilled_fields))))
11927 51 : set_nonincremental_init (braced_init_obstack);
11928 :
11929 675 : p = constructor_pending_elts;
11930 815 : while (p)
11931 : {
11932 347 : if (field == p->purpose)
11933 207 : return p->value;
11934 140 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11935 7 : p = p->left;
11936 : else
11937 133 : p = p->right;
11938 : }
11939 : }
11940 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11941 : {
11942 123 : if (!vec_safe_is_empty (constructor_elements)
11943 50 : && (constructor_elements->last ().index == field))
11944 50 : return constructor_elements->last ().value;
11945 : }
11946 : return NULL_TREE;
11947 : }
11948 :
11949 : /* "Output" the next constructor element.
11950 : At top level, really output it to assembler code now.
11951 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11952 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11953 : TYPE is the data type that the containing data type wants here.
11954 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11955 : If VALUE is a string constant, STRICT_STRING is true if it is
11956 : unparenthesized or we should not warn here for it being parenthesized.
11957 : For other types of VALUE, STRICT_STRING is not used.
11958 :
11959 : PENDING if true means output pending elements that belong
11960 : right after this element. (PENDING is normally true;
11961 : it is false while outputting pending elements, to avoid recursion.)
11962 :
11963 : IMPLICIT is true if value comes from pop_init_level (1),
11964 : the new initializer has been merged with the existing one
11965 : and thus no warnings should be emitted about overriding an
11966 : existing initializer. */
11967 :
11968 : static void
11969 9625152 : output_init_element (location_t loc, tree value, tree origtype,
11970 : bool strict_string, tree type, tree field, bool pending,
11971 : bool implicit, struct obstack * braced_init_obstack)
11972 : {
11973 9625152 : tree semantic_type = NULL_TREE;
11974 9625152 : bool maybe_const = true;
11975 9625152 : bool npc, int_const_expr, arith_const_expr;
11976 :
11977 9625152 : if (type == error_mark_node || value == error_mark_node)
11978 : {
11979 302 : constructor_erroneous = 1;
11980 2427 : return;
11981 : }
11982 9624850 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11983 200081 : && (TREE_CODE (value) == STRING_CST
11984 158671 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11985 41418 : && !(TREE_CODE (value) == STRING_CST
11986 41410 : && TREE_CODE (type) == ARRAY_TYPE
11987 3898 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11988 9662370 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11989 37520 : TYPE_MAIN_VARIANT (type)))
11990 37520 : value = array_to_pointer_conversion (input_location, value);
11991 :
11992 9624850 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11993 182 : && require_constant_value && pending)
11994 : {
11995 : /* As an extension, allow initializing objects with static storage
11996 : duration with compound literals (which are then treated just as
11997 : the brace enclosed list they contain). */
11998 114 : if (flag_isoc99)
11999 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
12000 : "constant");
12001 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
12002 114 : value = DECL_INITIAL (decl);
12003 : }
12004 :
12005 9624850 : npc = null_pointer_constant_p (value);
12006 19249700 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12007 4219649 : && !TREE_OVERFLOW (value)
12008 13844498 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12009 : /* Not fully determined before folding. */
12010 9624850 : arith_const_expr = true;
12011 9624850 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12012 : {
12013 16 : semantic_type = TREE_TYPE (value);
12014 16 : value = TREE_OPERAND (value, 0);
12015 : }
12016 9624850 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12017 : /* TODO: this may not detect all cases of expressions folding to
12018 : constants that are not arithmetic constant expressions. */
12019 9624850 : if (!maybe_const)
12020 : arith_const_expr = false;
12021 19247143 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12022 3185831 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12023 11465689 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12024 : arith_const_expr = false;
12025 7796781 : else if (TREE_CODE (value) != INTEGER_CST
12026 : && TREE_CODE (value) != REAL_CST
12027 : && TREE_CODE (value) != COMPLEX_CST
12028 : && TREE_CODE (value) != RAW_DATA_CST)
12029 : arith_const_expr = false;
12030 4608948 : else if (TREE_OVERFLOW (value))
12031 5015903 : arith_const_expr = false;
12032 :
12033 9624850 : if (value == error_mark_node)
12034 0 : constructor_erroneous = 1;
12035 9624850 : else if (!TREE_CONSTANT (value))
12036 3208114 : constructor_constant = 0;
12037 6416736 : else if (!initializer_constant_valid_p (value,
12038 6416736 : TREE_TYPE (value),
12039 6416736 : AGGREGATE_TYPE_P (constructor_type)
12040 6416736 : && TYPE_REVERSE_STORAGE_ORDER
12041 : (constructor_type))
12042 6416736 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12043 1037878 : && DECL_C_BIT_FIELD (field)
12044 8103 : && TREE_CODE (value) != INTEGER_CST))
12045 77 : constructor_simple = 0;
12046 9624850 : if (!maybe_const)
12047 1172 : constructor_nonconst = 1;
12048 :
12049 : /* Digest the initializer and issue any errors about incompatible
12050 : types before issuing errors about non-constant initializers. */
12051 9624850 : tree new_value = value;
12052 9624850 : if (semantic_type)
12053 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12054 : /* In the case of braces around a scalar initializer, the result of
12055 : this initializer processing goes through digest_init again at the
12056 : outer level. In the case of a constexpr initializer for a
12057 : pointer, avoid converting a null pointer constant to something
12058 : that is not a null pointer constant to avoid a spurious error
12059 : from that second processing. */
12060 9624850 : if (!require_constexpr_value
12061 398 : || !npc
12062 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12063 9624839 : new_value = digest_init (loc,
12064 9624839 : RECORD_OR_UNION_TYPE_P (constructor_type)
12065 8554761 : ? field : constructor_fields
12066 8554761 : ? constructor_fields : constructor_decl,
12067 : type, new_value, origtype, npc,
12068 : int_const_expr, arith_const_expr, strict_string,
12069 : require_constant_value, require_constexpr_value);
12070 9624850 : if (new_value == error_mark_node)
12071 : {
12072 76 : constructor_erroneous = 1;
12073 76 : return;
12074 : }
12075 9624774 : if (require_constant_value || require_constant_elements)
12076 2713197 : constant_expression_warning (new_value);
12077 :
12078 : /* Proceed to check the constness of the original initializer. */
12079 9624774 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12080 : {
12081 3208140 : if (require_constant_value)
12082 : {
12083 0 : error_init (loc, "initializer element is not constant");
12084 0 : value = error_mark_node;
12085 : }
12086 3208140 : else if (require_constant_elements)
12087 15 : pedwarn (loc, OPT_Wpedantic,
12088 : "initializer element is not computable at load time");
12089 : }
12090 6416634 : else if (!maybe_const
12091 54 : && (require_constant_value || require_constant_elements))
12092 28 : pedwarn_init (loc, OPT_Wpedantic,
12093 : "initializer element is not a constant expression");
12094 : /* digest_init has already carried out the additional checks
12095 : required for 'constexpr' initializers (using the information
12096 : passed to it about whether the original initializer was certain
12097 : kinds of constant expression), so that check does not need to be
12098 : repeated here. */
12099 :
12100 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12101 : enum type. */
12102 9624774 : if (warn_cxx_compat
12103 6895 : && field != NULL_TREE
12104 6895 : && TREE_CODE (field) == FIELD_DECL
12105 459 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12106 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12107 21 : != TYPE_MAIN_VARIANT (type))
12108 9624795 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12109 : {
12110 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12111 21 : if (checktype != error_mark_node
12112 21 : && (TYPE_MAIN_VARIANT (checktype)
12113 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12114 9 : warning_init (loc, OPT_Wc___compat,
12115 : "enum conversion in initialization is invalid in C++");
12116 : }
12117 :
12118 : /* If this field is empty and does not have side effects (and is not at
12119 : the end of structure), don't do anything other than checking the
12120 : initializer. */
12121 9624774 : if (field
12122 9624774 : && (TREE_TYPE (field) == error_mark_node
12123 9624339 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12124 9624030 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12125 62 : && !TREE_SIDE_EFFECTS (new_value)
12126 33 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12127 33 : || DECL_CHAIN (field)))))
12128 18 : return;
12129 :
12130 : /* Finally, set VALUE to the initializer value digested above. */
12131 9624756 : value = new_value;
12132 :
12133 : /* If this element doesn't come next in sequence,
12134 : put it on constructor_pending_elts. */
12135 9624756 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12136 9624756 : && (!constructor_incremental
12137 1717780 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12138 1717414 : || (TREE_CODE (value) == RAW_DATA_CST
12139 235 : && constructor_pending_elts
12140 66 : && pending)))
12141 : {
12142 627 : if (constructor_incremental
12143 627 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12144 337 : || (TREE_CODE (value) == RAW_DATA_CST
12145 14 : && constructor_pending_elts
12146 14 : && pending)))
12147 48 : set_nonincremental_init (braced_init_obstack);
12148 :
12149 627 : add_pending_init (loc, field, value, origtype, implicit,
12150 : braced_init_obstack);
12151 627 : return;
12152 : }
12153 9624129 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12154 1038931 : && (!constructor_incremental
12155 1038591 : || field != constructor_unfilled_fields))
12156 : {
12157 : /* We do this for records but not for unions. In a union,
12158 : no matter which field is specified, it can be initialized
12159 : right away since it starts at the beginning of the union. */
12160 1404 : if (constructor_incremental)
12161 : {
12162 1064 : if (!constructor_unfilled_fields)
12163 5 : set_nonincremental_init (braced_init_obstack);
12164 : else
12165 : {
12166 1059 : tree bitpos, unfillpos;
12167 :
12168 1059 : bitpos = bit_position (field);
12169 1059 : unfillpos = bit_position (constructor_unfilled_fields);
12170 :
12171 1059 : if (tree_int_cst_lt (bitpos, unfillpos))
12172 4 : set_nonincremental_init (braced_init_obstack);
12173 : }
12174 : }
12175 :
12176 1404 : add_pending_init (loc, field, value, origtype, implicit,
12177 : braced_init_obstack);
12178 1404 : return;
12179 : }
12180 9622725 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12181 9622725 : && !vec_safe_is_empty (constructor_elements))
12182 : {
12183 62 : if (!implicit)
12184 : {
12185 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12186 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12187 : "initialized field with side-effects overwritten");
12188 9 : else if (warn_override_init)
12189 6 : warning_init (loc, OPT_Woverride_init,
12190 : "initialized field overwritten");
12191 : }
12192 :
12193 : /* We can have just one union field set. */
12194 62 : constructor_elements = NULL;
12195 : }
12196 :
12197 : /* Otherwise, output this element either to
12198 : constructor_elements or to the assembler file. */
12199 :
12200 9622725 : constructor_elt celt = {field, value};
12201 9622725 : vec_safe_push (constructor_elements, celt);
12202 :
12203 : /* Advance the variable that indicates sequential elements output. */
12204 9622725 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12205 : {
12206 1717409 : tree inc = bitsize_one_node;
12207 1717409 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12208 230 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12209 1717409 : constructor_unfilled_index
12210 1717409 : = size_binop_loc (input_location, PLUS_EXPR,
12211 : constructor_unfilled_index, inc);
12212 : }
12213 7905316 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12214 : {
12215 1037527 : constructor_unfilled_fields
12216 1037527 : = DECL_CHAIN (constructor_unfilled_fields);
12217 :
12218 : /* Skip any nameless bit fields. */
12219 1037527 : while (constructor_unfilled_fields != NULL_TREE
12220 1037628 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12221 101 : constructor_unfilled_fields
12222 101 : = DECL_CHAIN (constructor_unfilled_fields);
12223 : }
12224 6867789 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12225 31115 : constructor_unfilled_fields = NULL_TREE;
12226 :
12227 : /* Now output any pending elements which have become next. */
12228 9622725 : if (pending)
12229 9619860 : output_pending_init_elements (0, braced_init_obstack);
12230 : }
12231 :
12232 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12233 : comes before field2, 1 if field1 comes after field2 and
12234 : 0 if field1 == field2. */
12235 :
12236 : static int
12237 4029 : init_field_decl_cmp (tree field1, tree field2)
12238 : {
12239 4029 : if (field1 == field2)
12240 : return 0;
12241 :
12242 2154 : tree bitpos1 = bit_position (field1);
12243 2154 : tree bitpos2 = bit_position (field2);
12244 2154 : if (tree_int_cst_equal (bitpos1, bitpos2))
12245 : {
12246 : /* If one of the fields has non-zero bitsize, then that
12247 : field must be the last one in a sequence of zero
12248 : sized fields, fields after it will have bigger
12249 : bit_position. */
12250 22 : if (TREE_TYPE (field1) != error_mark_node
12251 22 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12252 44 : && integer_nonzerop (TREE_TYPE (field1)))
12253 : return 1;
12254 22 : if (TREE_TYPE (field2) != error_mark_node
12255 22 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12256 37 : && integer_nonzerop (TREE_TYPE (field2)))
12257 : return -1;
12258 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12259 : which field comes earlier. Walk chains of both
12260 : fields, so that if field1 and field2 are close to each
12261 : other in either order, it is found soon even for large
12262 : sequences of zero sized fields. */
12263 : tree f1 = field1, f2 = field2;
12264 22 : while (1)
12265 : {
12266 22 : f1 = DECL_CHAIN (f1);
12267 22 : f2 = DECL_CHAIN (f2);
12268 22 : if (f1 == NULL_TREE)
12269 : {
12270 7 : gcc_assert (f2);
12271 : return 1;
12272 : }
12273 15 : if (f2 == NULL_TREE)
12274 : return -1;
12275 1 : if (f1 == field2)
12276 : return -1;
12277 0 : if (f2 == field1)
12278 : return 1;
12279 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12280 : return 1;
12281 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12282 : return -1;
12283 : }
12284 : }
12285 2132 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12286 : return -1;
12287 : else
12288 : return 1;
12289 : }
12290 :
12291 : /* Output any pending elements which have become next.
12292 : As we output elements, constructor_unfilled_{fields,index}
12293 : advances, which may cause other elements to become next;
12294 : if so, they too are output.
12295 :
12296 : If ALL is 0, we return when there are
12297 : no more pending elements to output now.
12298 :
12299 : If ALL is 1, we output space as necessary so that
12300 : we can output all the pending elements. */
12301 : static void
12302 11688548 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12303 : {
12304 11688548 : struct init_node *elt = constructor_pending_elts;
12305 11689548 : tree next;
12306 :
12307 11689548 : retry:
12308 :
12309 : /* Look through the whole pending tree.
12310 : If we find an element that should be output now,
12311 : output it. Otherwise, set NEXT to the element
12312 : that comes first among those still pending. */
12313 :
12314 11689548 : next = NULL_TREE;
12315 11695198 : while (elt)
12316 : {
12317 7598 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12318 : {
12319 3004 : if (tree_int_cst_equal (elt->purpose,
12320 : constructor_unfilled_index))
12321 1197 : output_init_element (input_location, elt->value, elt->origtype,
12322 1197 : true, TREE_TYPE (constructor_type),
12323 : constructor_unfilled_index, false, false,
12324 : braced_init_obstack);
12325 1807 : else if (tree_int_cst_lt (constructor_unfilled_index,
12326 1807 : elt->purpose))
12327 : {
12328 : /* Advance to the next smaller node. */
12329 589 : if (elt->left)
12330 : elt = elt->left;
12331 : else
12332 : {
12333 : /* We have reached the smallest node bigger than the
12334 : current unfilled index. Fill the space first. */
12335 209 : next = elt->purpose;
12336 209 : break;
12337 : }
12338 : }
12339 : else
12340 : {
12341 : /* Advance to the next bigger node. */
12342 1218 : if (elt->right)
12343 : elt = elt->right;
12344 : else
12345 : {
12346 : /* We have reached the biggest node in a subtree. Find
12347 : the parent of it, which is the next bigger node. */
12348 1218 : while (elt->parent && elt->parent->right == elt)
12349 : elt = elt->parent;
12350 774 : elt = elt->parent;
12351 1149 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12352 375 : elt->purpose))
12353 : {
12354 22 : next = elt->purpose;
12355 22 : break;
12356 : }
12357 : }
12358 : }
12359 : }
12360 4594 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12361 : {
12362 : /* If the current record is complete we are done. */
12363 4594 : if (constructor_unfilled_fields == NULL_TREE)
12364 : break;
12365 :
12366 3755 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12367 : elt->purpose);
12368 3755 : if (cmp == 0)
12369 1668 : output_init_element (input_location, elt->value, elt->origtype,
12370 1668 : true, TREE_TYPE (elt->purpose),
12371 : elt->purpose, false, false,
12372 : braced_init_obstack);
12373 2087 : else if (cmp < 0)
12374 : {
12375 : /* Advance to the next smaller node. */
12376 1092 : if (elt->left)
12377 : elt = elt->left;
12378 : else
12379 : {
12380 : /* We have reached the smallest node bigger than the
12381 : current unfilled field. Fill the space first. */
12382 811 : next = elt->purpose;
12383 811 : break;
12384 : }
12385 : }
12386 : else
12387 : {
12388 : /* Advance to the next bigger node. */
12389 995 : if (elt->right)
12390 : elt = elt->right;
12391 : else
12392 : {
12393 : /* We have reached the biggest node in a subtree. Find
12394 : the parent of it, which is the next bigger node. */
12395 735 : while (elt->parent && elt->parent->right == elt)
12396 : elt = elt->parent;
12397 519 : elt = elt->parent;
12398 519 : if (elt
12399 519 : && init_field_decl_cmp (constructor_unfilled_fields,
12400 : elt->purpose) < 0)
12401 : {
12402 67 : next = elt->purpose;
12403 67 : break;
12404 : }
12405 : }
12406 : }
12407 : }
12408 : }
12409 :
12410 : /* Ordinarily return, but not if we want to output all
12411 : and there are elements left. */
12412 11689548 : if (!(all && next != NULL_TREE))
12413 11688548 : return;
12414 :
12415 : /* If it's not incremental, just skip over the gap, so that after
12416 : jumping to retry we will output the next successive element. */
12417 1000 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12418 808 : constructor_unfilled_fields = next;
12419 192 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12420 192 : constructor_unfilled_index = next;
12421 :
12422 : /* ELT now points to the node in the pending tree with the next
12423 : initializer to output. */
12424 1000 : goto retry;
12425 : }
12426 :
12427 : /* Expression VALUE coincides with the start of type TYPE in a braced
12428 : initializer. Return true if we should treat VALUE as initializing
12429 : the first element of TYPE, false if we should treat it as initializing
12430 : TYPE as a whole.
12431 :
12432 : If the initializer is clearly invalid, the question becomes:
12433 : which choice gives the best error message? */
12434 :
12435 : static bool
12436 3484535 : initialize_elementwise_p (tree type, tree value)
12437 : {
12438 3484535 : if (type == error_mark_node || value == error_mark_node)
12439 : return false;
12440 :
12441 3484254 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12442 :
12443 3484254 : tree value_type = TREE_TYPE (value);
12444 3484254 : if (value_type == error_mark_node)
12445 : return false;
12446 :
12447 : /* GNU vectors can be initialized elementwise. However, treat any
12448 : kind of vector value as initializing the vector type as a whole,
12449 : regardless of whether the value is a GNU vector. Such initializers
12450 : are valid if and only if they would have been valid in a non-braced
12451 : initializer like:
12452 :
12453 : TYPE foo = VALUE;
12454 :
12455 : so recursing into the vector type would be at best confusing or at
12456 : worst wrong. For example, when -flax-vector-conversions is in effect,
12457 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12458 : have different element types and different numbers of elements. */
12459 3484254 : if (gnu_vector_type_p (type))
12460 19535 : return !VECTOR_TYPE_P (value_type);
12461 :
12462 3464719 : if (AGGREGATE_TYPE_P (type))
12463 1741352 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12464 :
12465 : return false;
12466 : }
12467 :
12468 : /* Helper function for process_init_element. Split first element of
12469 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12470 :
12471 : static inline tree
12472 9622167 : maybe_split_raw_data (tree value, tree *raw_data)
12473 : {
12474 9622167 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12475 : return value;
12476 211 : *raw_data = value;
12477 211 : value = build_int_cst (integer_type_node,
12478 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12479 211 : ++RAW_DATA_POINTER (*raw_data);
12480 211 : --RAW_DATA_LENGTH (*raw_data);
12481 211 : return value;
12482 : }
12483 :
12484 : /* Return non-zero if c_parser_initval should attempt to optimize
12485 : large initializers into RAW_DATA_CST. In that case return how
12486 : many elements to optimize at most. */
12487 :
12488 : unsigned
12489 2991639 : c_maybe_optimize_large_byte_initializer (void)
12490 : {
12491 2991639 : if (!constructor_type
12492 2991569 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12493 208794 : || constructor_stack->implicit)
12494 : return 0;
12495 206029 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12496 206029 : if (TREE_CODE (elttype) != INTEGER_TYPE
12497 206029 : && TREE_CODE (elttype) != BITINT_TYPE)
12498 : return 0;
12499 183384 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12500 27884 : || constructor_stack->replacement_value.value
12501 27884 : || (COMPLETE_TYPE_P (constructor_type)
12502 26247 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12503 211268 : || constructor_range_stack)
12504 : return 0;
12505 27884 : if (constructor_max_index == NULL_TREE)
12506 : return INT_MAX;
12507 26247 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12508 26247 : || integer_all_onesp (constructor_max_index))
12509 4768 : return 0;
12510 21479 : widest_int w = wi::to_widest (constructor_max_index);
12511 21479 : w -= wi::to_widest (constructor_index);
12512 21479 : w += 1;
12513 21479 : if (w < 64)
12514 : return 0;
12515 6890 : if (w > INT_MAX)
12516 : return INT_MAX;
12517 6890 : return w.to_uhwi ();
12518 21479 : }
12519 :
12520 : /* Add one non-braced element to the current constructor level.
12521 : This adjusts the current position within the constructor's type.
12522 : This may also start or terminate implicit levels
12523 : to handle a partly-braced initializer.
12524 :
12525 : Once this has found the correct level for the new element,
12526 : it calls output_init_element.
12527 :
12528 : IMPLICIT is true if value comes from pop_init_level (1),
12529 : the new initializer has been merged with the existing one
12530 : and thus no warnings should be emitted about overriding an
12531 : existing initializer. */
12532 :
12533 : void
12534 9612461 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12535 : struct obstack * braced_init_obstack)
12536 : {
12537 9612461 : tree orig_value = value.value;
12538 19224922 : int string_flag
12539 9612461 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12540 9612461 : bool strict_string = value.original_code == STRING_CST;
12541 9612461 : bool was_designated = designator_depth != 0;
12542 9612461 : tree raw_data = NULL_TREE;
12543 :
12544 9612673 : retry:
12545 9612673 : designator_depth = 0;
12546 9612673 : designator_erroneous = 0;
12547 :
12548 9612673 : if (!implicit && value.value && !integer_zerop (value.value))
12549 7589618 : constructor_zeroinit = 0;
12550 :
12551 : /* Handle superfluous braces around string cst as in
12552 : char x[] = {"foo"}; */
12553 9612673 : if (constructor_type
12554 9612545 : && !was_designated
12555 9578553 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12556 1705084 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12557 10348535 : && integer_zerop (constructor_unfilled_index))
12558 : {
12559 163093 : if (constructor_stack->replacement_value.value)
12560 : {
12561 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12562 409 : return;
12563 : }
12564 163085 : else if (string_flag)
12565 : {
12566 138 : constructor_stack->replacement_value = value;
12567 138 : return;
12568 : }
12569 : }
12570 :
12571 9612527 : if (constructor_stack->replacement_value.value != NULL_TREE)
12572 : {
12573 0 : error_init (loc, "excess elements in struct initializer");
12574 0 : return;
12575 : }
12576 :
12577 : /* Ignore elements of a brace group if it is entirely superfluous
12578 : and has already been diagnosed, or if the type is erroneous. */
12579 9612527 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12580 : return;
12581 :
12582 : /* Ignore elements of an initializer for a variable-size type.
12583 : Those are diagnosed in the parser (empty initializer braces are OK). */
12584 9612330 : if (COMPLETE_TYPE_P (constructor_type)
12585 9612330 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12586 : return;
12587 :
12588 8908650 : if (!implicit && warn_designated_init && !was_designated
12589 8875275 : && TREE_CODE (constructor_type) == RECORD_TYPE
12590 10647126 : && lookup_attribute ("designated_init",
12591 1034854 : TYPE_ATTRIBUTES (constructor_type)))
12592 50 : warning_init (loc,
12593 : OPT_Wdesignated_init,
12594 : "positional initialization of field "
12595 : "in %<struct%> declared with %<designated_init%> attribute");
12596 :
12597 : /* If we've exhausted any levels that didn't have braces,
12598 : pop them now. */
12599 10314251 : while (constructor_stack->implicit)
12600 : {
12601 706849 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12602 702792 : && constructor_fields == NULL_TREE)
12603 701738 : process_init_element (loc,
12604 : pop_init_level (loc, 1, braced_init_obstack,
12605 : last_init_list_comma),
12606 : true, braced_init_obstack);
12607 5111 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12608 1698 : || gnu_vector_type_p (constructor_type))
12609 4057 : && constructor_max_index
12610 9141 : && tree_int_cst_lt (constructor_max_index,
12611 : constructor_index))
12612 241 : process_init_element (loc,
12613 : pop_init_level (loc, 1, braced_init_obstack,
12614 : last_init_list_comma),
12615 : true, braced_init_obstack);
12616 : else
12617 : break;
12618 : }
12619 :
12620 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12621 9612272 : if (constructor_range_stack)
12622 : {
12623 : /* If value is a compound literal and we'll be just using its
12624 : content, don't put it into a SAVE_EXPR. */
12625 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12626 3 : || !require_constant_value)
12627 : {
12628 368 : tree semantic_type = NULL_TREE;
12629 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12630 : {
12631 0 : semantic_type = TREE_TYPE (value.value);
12632 0 : value.value = TREE_OPERAND (value.value, 0);
12633 : }
12634 368 : value.value = save_expr (value.value);
12635 368 : if (semantic_type)
12636 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12637 : value.value);
12638 : }
12639 : }
12640 :
12641 10326516 : while (1)
12642 : {
12643 10326516 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12644 : {
12645 1039003 : tree fieldtype;
12646 1039003 : enum tree_code fieldcode;
12647 :
12648 1039003 : if (constructor_fields == NULL_TREE)
12649 : {
12650 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12651 1280 : break;
12652 : }
12653 :
12654 1037723 : fieldtype = TREE_TYPE (constructor_fields);
12655 1037723 : if (fieldtype != error_mark_node)
12656 1037722 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12657 1037723 : fieldcode = TREE_CODE (fieldtype);
12658 :
12659 : /* Error for non-static initialization of a flexible array member. */
12660 1037723 : if (fieldcode == ARRAY_TYPE
12661 155278 : && !require_constant_value
12662 1128 : && TYPE_SIZE (fieldtype) == NULL_TREE
12663 1037733 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12664 : {
12665 10 : error_init (loc, "non-static initialization of a flexible "
12666 : "array member");
12667 10 : break;
12668 : }
12669 :
12670 : /* Error for initialization of a flexible array member with
12671 : a string constant if the structure is in an array. E.g.:
12672 : struct S { int x; char y[]; };
12673 : struct S s[] = { { 1, "foo" } };
12674 : is invalid. */
12675 1037713 : if (string_flag
12676 1037713 : && fieldcode == ARRAY_TYPE
12677 3353 : && constructor_depth > 1
12678 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12679 1037760 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12680 : {
12681 47 : bool in_array_p = false;
12682 47 : for (struct constructor_stack *p = constructor_stack;
12683 73 : p && p->type; p = p->next)
12684 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12685 : {
12686 : in_array_p = true;
12687 : break;
12688 : }
12689 47 : if (in_array_p)
12690 : {
12691 33 : error_init (loc, "initialization of flexible array "
12692 : "member in a nested context");
12693 33 : break;
12694 : }
12695 : }
12696 :
12697 : /* Accept a string constant to initialize a subarray. */
12698 1037680 : if (value.value != NULL_TREE
12699 1037607 : && fieldcode == ARRAY_TYPE
12700 155162 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12701 1192355 : && string_flag)
12702 : value.value = orig_value;
12703 : /* Otherwise, if we have come to a subaggregate,
12704 : and we don't have an element of its type, push into it. */
12705 1034664 : else if (value.value != NULL_TREE
12706 1034360 : && initialize_elementwise_p (fieldtype, value.value))
12707 : {
12708 304 : push_init_level (loc, 1, braced_init_obstack);
12709 304 : continue;
12710 : }
12711 :
12712 1037376 : value.value = maybe_split_raw_data (value.value, &raw_data);
12713 1037376 : if (value.value)
12714 : {
12715 1037303 : push_member_name (constructor_fields);
12716 1037303 : output_init_element (loc, value.value, value.original_type,
12717 : strict_string, fieldtype,
12718 : constructor_fields, true, implicit,
12719 : braced_init_obstack);
12720 1037303 : RESTORE_SPELLING_DEPTH (constructor_depth);
12721 : }
12722 : else
12723 : /* Do the bookkeeping for an element that was
12724 : directly output as a constructor. */
12725 : {
12726 : /* For a record, keep track of end position of last field. */
12727 73 : if (DECL_SIZE (constructor_fields))
12728 30 : constructor_bit_index
12729 30 : = size_binop_loc (input_location, PLUS_EXPR,
12730 : bit_position (constructor_fields),
12731 30 : DECL_SIZE (constructor_fields));
12732 :
12733 : /* If the current field was the first one not yet written out,
12734 : it isn't now, so update. */
12735 73 : if (constructor_unfilled_fields == constructor_fields)
12736 : {
12737 64 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12738 : /* Skip any nameless bit fields. */
12739 64 : while (constructor_unfilled_fields != 0
12740 64 : && (DECL_UNNAMED_BIT_FIELD
12741 : (constructor_unfilled_fields)))
12742 0 : constructor_unfilled_fields =
12743 0 : DECL_CHAIN (constructor_unfilled_fields);
12744 : }
12745 : }
12746 :
12747 1037376 : constructor_fields = DECL_CHAIN (constructor_fields);
12748 : /* Skip any nameless bit fields at the beginning. */
12749 1037376 : while (constructor_fields != NULL_TREE
12750 1037477 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12751 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12752 : }
12753 9287513 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12754 : {
12755 31178 : tree fieldtype;
12756 31178 : enum tree_code fieldcode;
12757 :
12758 31178 : if (constructor_fields == NULL_TREE)
12759 : {
12760 3 : pedwarn_init (loc, 0,
12761 : "excess elements in union initializer");
12762 3 : break;
12763 : }
12764 :
12765 31175 : fieldtype = TREE_TYPE (constructor_fields);
12766 31175 : if (fieldtype != error_mark_node)
12767 31175 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12768 31175 : fieldcode = TREE_CODE (fieldtype);
12769 :
12770 : /* Warn that traditional C rejects initialization of unions.
12771 : We skip the warning if the value is zero. This is done
12772 : under the assumption that the zero initializer in user
12773 : code appears conditioned on e.g. __STDC__ to avoid
12774 : "missing initializer" warnings and relies on default
12775 : initialization to zero in the traditional C case.
12776 : We also skip the warning if the initializer is designated,
12777 : again on the assumption that this must be conditional on
12778 : __STDC__ anyway (and we've already complained about the
12779 : member-designator already). */
12780 33420 : if (!in_system_header_at (input_location) && !constructor_designated
12781 32658 : && !(value.value && (integer_zerop (value.value)
12782 1376 : || real_zerop (value.value))))
12783 1369 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12784 : "of unions");
12785 :
12786 : /* Error for non-static initialization of a flexible array member. */
12787 31175 : if (fieldcode == ARRAY_TYPE
12788 766 : && !require_constant_value
12789 31232 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12790 : {
12791 3 : error_init (loc, "non-static initialization of a flexible "
12792 : "array member");
12793 3 : 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 : union U { int x; char y[]; };
12799 : union U s[] = { { 1, "foo" } };
12800 : is invalid. */
12801 31172 : if (string_flag
12802 31172 : && fieldcode == ARRAY_TYPE
12803 7 : && constructor_depth > 1
12804 31176 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12805 : {
12806 3 : bool in_array_p = false;
12807 3 : for (struct constructor_stack *p = constructor_stack;
12808 3 : p && p->type; p = p->next)
12809 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12810 : {
12811 : in_array_p = true;
12812 : break;
12813 : }
12814 3 : if (in_array_p)
12815 : {
12816 3 : error_init (loc, "initialization of flexible array "
12817 : "member in a nested context");
12818 3 : break;
12819 : }
12820 : }
12821 :
12822 : /* Accept a string constant to initialize a subarray. */
12823 31169 : if (value.value != NULL_TREE
12824 31169 : && fieldcode == ARRAY_TYPE
12825 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12826 31924 : && string_flag)
12827 : value.value = orig_value;
12828 : /* Otherwise, if we have come to a subaggregate,
12829 : and we don't have an element of its type, push into it. */
12830 31215 : else if (value.value != NULL_TREE
12831 31165 : && initialize_elementwise_p (fieldtype, value.value))
12832 : {
12833 50 : push_init_level (loc, 1, braced_init_obstack);
12834 50 : continue;
12835 : }
12836 :
12837 31119 : value.value = maybe_split_raw_data (value.value, &raw_data);
12838 31119 : if (value.value)
12839 : {
12840 31119 : push_member_name (constructor_fields);
12841 31119 : output_init_element (loc, value.value, value.original_type,
12842 : strict_string, fieldtype,
12843 : constructor_fields, true, implicit,
12844 : braced_init_obstack);
12845 31119 : RESTORE_SPELLING_DEPTH (constructor_depth);
12846 : }
12847 : else
12848 : /* Do the bookkeeping for an element that was
12849 : directly output as a constructor. */
12850 : {
12851 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12852 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12853 : }
12854 :
12855 31119 : constructor_fields = NULL_TREE;
12856 : }
12857 9256335 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12858 : {
12859 2419579 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12860 2419579 : enum tree_code eltcode = TREE_CODE (elttype);
12861 :
12862 : /* Accept a string constant to initialize a subarray. */
12863 2419579 : if (value.value != NULL_TREE
12864 2419579 : && eltcode == ARRAY_TYPE
12865 7029 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12866 2425127 : && string_flag)
12867 : value.value = orig_value;
12868 : /* Otherwise, if we have come to a subaggregate,
12869 : and we don't have an element of its type, push into it. */
12870 3121409 : else if (value.value != NULL_TREE
12871 2419083 : && initialize_elementwise_p (elttype, value.value))
12872 : {
12873 702326 : push_init_level (loc, 1, braced_init_obstack);
12874 702326 : continue;
12875 : }
12876 :
12877 1717253 : if (constructor_max_index != NULL_TREE
12878 674285 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12879 674170 : || integer_all_onesp (constructor_max_index))
12880 : /* For VLA we got an error already. */
12881 1717368 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
12882 : {
12883 113 : pedwarn_init (loc, 0,
12884 : "excess elements in array initializer");
12885 113 : break;
12886 : }
12887 :
12888 1717140 : if (value.value
12889 1717140 : && TREE_CODE (value.value) == RAW_DATA_CST
12890 193 : && RAW_DATA_LENGTH (value.value) > 1
12891 193 : && (TREE_CODE (elttype) == INTEGER_TYPE
12892 193 : || TREE_CODE (elttype) == BITINT_TYPE)
12893 193 : && TYPE_PRECISION (elttype) == CHAR_BIT
12894 1717333 : && (constructor_max_index == NULL_TREE
12895 55 : || tree_int_cst_lt (constructor_index,
12896 : constructor_max_index)))
12897 : {
12898 193 : unsigned int len = RAW_DATA_LENGTH (value.value);
12899 193 : if (constructor_max_index)
12900 : {
12901 55 : widest_int w = wi::to_widest (constructor_max_index);
12902 55 : w -= wi::to_widest (constructor_index);
12903 55 : w += 1;
12904 55 : if (w < len)
12905 3 : len = w.to_uhwi ();
12906 55 : }
12907 193 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12908 : {
12909 3 : raw_data = copy_node (value.value);
12910 3 : RAW_DATA_LENGTH (raw_data) -= len;
12911 3 : RAW_DATA_POINTER (raw_data) += len;
12912 3 : RAW_DATA_LENGTH (value.value) = len;
12913 : }
12914 193 : TREE_TYPE (value.value) = elttype;
12915 193 : push_array_bounds (tree_to_uhwi (constructor_index));
12916 193 : output_init_element (loc, value.value, value.original_type,
12917 : false, elttype, constructor_index, true,
12918 : implicit, braced_init_obstack);
12919 193 : RESTORE_SPELLING_DEPTH (constructor_depth);
12920 193 : constructor_index
12921 193 : = size_binop_loc (input_location, PLUS_EXPR,
12922 193 : constructor_index, bitsize_int (len));
12923 : }
12924 : else
12925 : {
12926 1716947 : value.value = maybe_split_raw_data (value.value, &raw_data);
12927 : /* Now output the actual element. */
12928 1716947 : if (value.value)
12929 : {
12930 1716947 : push_array_bounds (tree_to_uhwi (constructor_index));
12931 1716947 : output_init_element (loc, value.value, value.original_type,
12932 : strict_string, elttype,
12933 : constructor_index, true, implicit,
12934 : braced_init_obstack);
12935 1716947 : RESTORE_SPELLING_DEPTH (constructor_depth);
12936 : }
12937 :
12938 1716947 : constructor_index
12939 1716947 : = size_binop_loc (input_location, PLUS_EXPR,
12940 : constructor_index, bitsize_one_node);
12941 :
12942 1716947 : if (!value.value)
12943 : /* If we are doing the bookkeeping for an element that was
12944 : directly output as a constructor, we must update
12945 : constructor_unfilled_index. */
12946 0 : constructor_unfilled_index = constructor_index;
12947 : }
12948 : }
12949 6836756 : else if (gnu_vector_type_p (constructor_type))
12950 : {
12951 6836264 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12952 :
12953 : /* Do a basic check of initializer size. Note that vectors
12954 : may not always have a fixed size derived from their type. */
12955 6836264 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12956 6836264 : tree_to_poly_uint64 (constructor_index)))
12957 : {
12958 : /* Diagose VLA out-of-bounds as errors. */
12959 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12960 2 : pedwarn_init (loc, 0,
12961 : "excess elements in vector initializer");
12962 : else
12963 : error_init (loc, "excess elements in vector initializer");
12964 :
12965 : break;
12966 : }
12967 :
12968 6836262 : value.value = maybe_split_raw_data (value.value, &raw_data);
12969 : /* Now output the actual element. */
12970 6836262 : if (value.value)
12971 : {
12972 6836262 : if (TREE_CODE (value.value) == VECTOR_CST)
12973 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
12974 6836262 : output_init_element (loc, value.value, value.original_type,
12975 : strict_string, elttype,
12976 : constructor_index, true, implicit,
12977 : braced_init_obstack);
12978 : }
12979 :
12980 6836262 : constructor_index
12981 6836262 : = size_binop_loc (input_location,
12982 : PLUS_EXPR, constructor_index, bitsize_one_node);
12983 :
12984 6836262 : if (!value.value)
12985 : /* If we are doing the bookkeeping for an element that was
12986 : directly output as a constructor, we must update
12987 : constructor_unfilled_index. */
12988 0 : constructor_unfilled_index = constructor_index;
12989 : }
12990 :
12991 : /* Handle the sole element allowed in a braced initializer
12992 : for a scalar variable. */
12993 492 : else if (constructor_type != error_mark_node
12994 492 : && constructor_fields == NULL_TREE)
12995 : {
12996 29 : pedwarn_init (loc, 0,
12997 : "excess elements in scalar initializer");
12998 29 : break;
12999 : }
13000 : else
13001 : {
13002 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
13003 463 : if (value.value)
13004 463 : output_init_element (loc, value.value, value.original_type,
13005 : strict_string, constructor_type,
13006 : NULL_TREE, true, implicit,
13007 : braced_init_obstack);
13008 463 : constructor_fields = NULL_TREE;
13009 : }
13010 :
13011 : /* Handle range initializers either at this level or anywhere higher
13012 : in the designator stack. */
13013 9622360 : if (constructor_range_stack)
13014 : {
13015 11564 : struct constructor_range_stack *p, *range_stack;
13016 11564 : int finish = 0;
13017 :
13018 11564 : range_stack = constructor_range_stack;
13019 11564 : constructor_range_stack = 0;
13020 11564 : while (constructor_stack != range_stack->stack)
13021 : {
13022 0 : gcc_assert (constructor_stack->implicit);
13023 0 : process_init_element (loc,
13024 : pop_init_level (loc, 1,
13025 : braced_init_obstack,
13026 : last_init_list_comma),
13027 : true, braced_init_obstack);
13028 : }
13029 325 : for (p = range_stack;
13030 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13031 325 : p = p->prev)
13032 : {
13033 325 : gcc_assert (constructor_stack->implicit);
13034 325 : process_init_element (loc,
13035 : pop_init_level (loc, 1,
13036 : braced_init_obstack,
13037 : last_init_list_comma),
13038 : true, braced_init_obstack);
13039 : }
13040 :
13041 11564 : p->index = size_binop_loc (input_location,
13042 : PLUS_EXPR, p->index, bitsize_one_node);
13043 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13044 379 : finish = 1;
13045 :
13046 11889 : while (1)
13047 : {
13048 11889 : constructor_index = p->index;
13049 11889 : constructor_fields = p->fields;
13050 11889 : if (finish && p->range_end && p->index == p->range_start)
13051 : {
13052 8 : finish = 0;
13053 8 : p->prev = 0;
13054 : }
13055 11889 : p = p->next;
13056 11889 : if (!p)
13057 : break;
13058 325 : finish_implicit_inits (loc, braced_init_obstack);
13059 325 : push_init_level (loc, 2, braced_init_obstack);
13060 325 : p->stack = constructor_stack;
13061 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13062 33 : p->index = p->range_start;
13063 : }
13064 :
13065 11564 : if (!finish)
13066 11193 : constructor_range_stack = range_stack;
13067 11564 : continue;
13068 11564 : }
13069 :
13070 : break;
13071 : }
13072 :
13073 9612272 : constructor_range_stack = 0;
13074 :
13075 9612272 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13076 : {
13077 212 : gcc_assert (!string_flag && !was_designated);
13078 212 : value.value = raw_data;
13079 212 : raw_data = NULL_TREE;
13080 212 : goto retry;
13081 : }
13082 : }
13083 :
13084 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13085 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13086 : an ASM_EXPR node). */
13087 : tree
13088 205088 : build_asm_stmt (bool is_volatile, tree args)
13089 : {
13090 205088 : if (is_volatile)
13091 165288 : ASM_VOLATILE_P (args) = 1;
13092 205088 : return add_stmt (args);
13093 : }
13094 :
13095 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13096 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13097 : SIMPLE indicates whether there was anything at all after the
13098 : string in the asm expression -- asm("blah") and asm("blah" : )
13099 : are subtly different. We use a ASM_EXPR node to represent this.
13100 : LOC is the location of the asm, and IS_INLINE says whether this
13101 : is asm inline. */
13102 : tree
13103 205140 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13104 : tree clobbers, tree labels, bool simple, bool is_inline)
13105 : {
13106 205140 : tree tail;
13107 205140 : tree args;
13108 205140 : int i;
13109 205140 : const char *constraint;
13110 205140 : const char **oconstraints;
13111 205140 : bool allows_mem, allows_reg, is_inout;
13112 205140 : int ninputs, noutputs;
13113 :
13114 205140 : ninputs = list_length (inputs);
13115 205140 : noutputs = list_length (outputs);
13116 205140 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13117 :
13118 205140 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13119 :
13120 : /* Remove output conversions that change the type but not the mode. */
13121 554792 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13122 : {
13123 349652 : tree output = TREE_VALUE (tail);
13124 :
13125 349652 : output = c_fully_fold (output, false, NULL, true);
13126 :
13127 : /* ??? Really, this should not be here. Users should be using a
13128 : proper lvalue, dammit. But there's a long history of using casts
13129 : in the output operands. In cases like longlong.h, this becomes a
13130 : primitive form of typechecking -- if the cast can be removed, then
13131 : the output operand had a type of the proper width; otherwise we'll
13132 : get an error. Gross, but ... */
13133 349652 : STRIP_NOPS (output);
13134 :
13135 349652 : if (!lvalue_or_else (loc, output, lv_asm))
13136 4 : output = error_mark_node;
13137 :
13138 349652 : if (output != error_mark_node
13139 349652 : && (TREE_READONLY (output)
13140 349645 : || TYPE_READONLY (TREE_TYPE (output))
13141 349645 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13142 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13143 2 : readonly_error (loc, output, lv_asm);
13144 :
13145 349652 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13146 349652 : oconstraints[i] = constraint;
13147 :
13148 349652 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13149 : &allows_mem, &allows_reg, &is_inout,
13150 : nullptr))
13151 : {
13152 : /* If the operand is going to end up in memory,
13153 : mark it addressable. */
13154 349644 : if (!allows_reg && !c_mark_addressable (output))
13155 3 : output = error_mark_node;
13156 1419 : if (!(!allows_reg && allows_mem)
13157 348225 : && output != error_mark_node
13158 697867 : && VOID_TYPE_P (TREE_TYPE (output)))
13159 : {
13160 4 : error_at (loc, "invalid use of void expression");
13161 4 : output = error_mark_node;
13162 : }
13163 349644 : if (allows_reg && current_function_decl == NULL_TREE)
13164 : {
13165 1 : error_at (loc, "constraint allows registers outside of "
13166 : "a function");
13167 1 : output = error_mark_node;
13168 : }
13169 : }
13170 : else
13171 8 : output = error_mark_node;
13172 :
13173 349652 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13174 : {
13175 7 : if (TREE_SIDE_EFFECTS (output))
13176 : {
13177 0 : error_at (loc, "side-effects in output operand outside "
13178 : "of a function");
13179 0 : output = error_mark_node;
13180 : }
13181 : else
13182 : {
13183 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13184 7 : if (addr == error_mark_node)
13185 : output = error_mark_node;
13186 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13187 : {
13188 1 : error_at (loc, "output operand outside of a function is not "
13189 : "constant");
13190 1 : output = error_mark_node;
13191 : }
13192 : }
13193 : }
13194 349645 : else if (output != error_mark_node && strstr (constraint, "-"))
13195 : {
13196 1 : error_at (loc, "%<-%> modifier used inside of a function");
13197 1 : output = error_mark_node;
13198 : }
13199 :
13200 349652 : TREE_VALUE (tail) = output;
13201 : }
13202 :
13203 570001 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13204 : {
13205 364861 : tree input;
13206 :
13207 364861 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13208 364861 : input = TREE_VALUE (tail);
13209 :
13210 364861 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13211 : oconstraints, &allows_mem, &allows_reg,
13212 : nullptr))
13213 : {
13214 : /* If the operand is going to end up in memory,
13215 : mark it addressable. */
13216 364853 : if (!allows_reg && allows_mem)
13217 : {
13218 1366 : input = c_fully_fold (input, false, NULL, true);
13219 :
13220 : /* Strip the nops as we allow this case. FIXME, this really
13221 : should be rejected or made deprecated. */
13222 1366 : STRIP_NOPS (input);
13223 1366 : if (!c_mark_addressable (input))
13224 2 : input = error_mark_node;
13225 : }
13226 : else
13227 : {
13228 363487 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13229 : true, false),
13230 : false, NULL);
13231 :
13232 363487 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13233 : {
13234 4 : error_at (loc, "invalid use of void expression");
13235 4 : input = error_mark_node;
13236 : }
13237 : }
13238 364853 : if (allows_reg && current_function_decl == NULL_TREE)
13239 : {
13240 1 : error_at (loc, "constraint allows registers outside of "
13241 : "a function");
13242 1 : input = error_mark_node;
13243 : }
13244 364853 : if (constraint[0] == ':' && input != error_mark_node)
13245 : {
13246 35 : tree t = input;
13247 35 : STRIP_NOPS (t);
13248 35 : if (TREE_CODE (t) != ADDR_EXPR
13249 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13250 23 : || (VAR_P (TREE_OPERAND (t, 0))
13251 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13252 : {
13253 5 : error_at (loc, "%<:%> constraint operand is not address "
13254 : "of a function or non-automatic variable");
13255 5 : input = error_mark_node;
13256 : }
13257 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13258 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13259 : }
13260 : }
13261 : else
13262 8 : input = error_mark_node;
13263 :
13264 364861 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13265 : {
13266 58 : if (TREE_SIDE_EFFECTS (input))
13267 : {
13268 2 : error_at (loc, "side-effects in input operand outside "
13269 : "of a function");
13270 2 : input = error_mark_node;
13271 : }
13272 : else
13273 : {
13274 56 : tree tem = input;
13275 56 : if (allows_mem && lvalue_p (input))
13276 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13277 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13278 : {
13279 2 : error_at (loc, "input operand outside of a function is not "
13280 : "constant");
13281 2 : input = error_mark_node;
13282 : }
13283 : }
13284 : }
13285 364803 : else if (input != error_mark_node && strstr (constraint, "-"))
13286 : {
13287 3 : error_at (loc, "%<-%> modifier used inside of a function");
13288 3 : input = error_mark_node;
13289 : }
13290 :
13291 364861 : TREE_VALUE (tail) = input;
13292 : }
13293 :
13294 205140 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13295 :
13296 : /* asm statements without outputs, including simple ones, are treated
13297 : as volatile. */
13298 205140 : ASM_BASIC_P (args) = simple;
13299 205140 : ASM_VOLATILE_P (args) = (noutputs == 0);
13300 205140 : ASM_INLINE_P (args) = is_inline;
13301 :
13302 205140 : return args;
13303 : }
13304 :
13305 : /* Generate a goto statement to LABEL. LOC is the location of the
13306 : GOTO. */
13307 :
13308 : tree
13309 82720 : c_finish_goto_label (location_t loc, tree label)
13310 : {
13311 82720 : tree decl = lookup_label_for_goto (loc, label);
13312 82720 : if (!decl)
13313 : return NULL_TREE;
13314 82720 : TREE_USED (decl) = 1;
13315 82720 : mark_decl_used (decl, false);
13316 82720 : {
13317 82720 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13318 82720 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13319 82720 : SET_EXPR_LOCATION (t, loc);
13320 82720 : return add_stmt (t);
13321 : }
13322 : }
13323 :
13324 : /* Generate a computed goto statement to EXPR. LOC is the location of
13325 : the GOTO. */
13326 :
13327 : tree
13328 942 : c_finish_goto_ptr (location_t loc, c_expr val)
13329 : {
13330 942 : tree expr = val.value;
13331 942 : tree t;
13332 942 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13333 942 : if (expr != error_mark_node
13334 941 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13335 947 : && !null_pointer_constant_p (expr))
13336 : {
13337 2 : error_at (val.get_location (),
13338 : "computed goto must be pointer type");
13339 2 : expr = build_zero_cst (ptr_type_node);
13340 : }
13341 942 : expr = c_fully_fold (expr, false, NULL);
13342 942 : expr = convert (ptr_type_node, expr);
13343 942 : t = build1 (GOTO_EXPR, void_type_node, expr);
13344 942 : SET_EXPR_LOCATION (t, loc);
13345 942 : return add_stmt (t);
13346 : }
13347 :
13348 : /* Generate a C `return' statement. RETVAL is the expression for what
13349 : to return, or a null pointer for `return;' with no value. LOC is
13350 : the location of the return statement, or the location of the expression,
13351 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13352 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13353 : attribute. */
13354 :
13355 : tree
13356 34658577 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13357 : {
13358 34658577 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13359 34658577 : bool no_warning = false;
13360 34658577 : bool npc = false;
13361 :
13362 : /* Use the expansion point to handle cases such as returning NULL
13363 : in a function returning void. */
13364 34658577 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13365 :
13366 34658577 : if (TREE_THIS_VOLATILE (current_function_decl))
13367 23 : warning_at (xloc, 0,
13368 : "function declared %<noreturn%> has a %<return%> statement");
13369 :
13370 34658577 : set_musttail_on_return (retval, xloc, musttail_p);
13371 :
13372 34658577 : if (retval)
13373 : {
13374 34637263 : tree semantic_type = NULL_TREE;
13375 34637263 : npc = null_pointer_constant_p (retval);
13376 34637263 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13377 : {
13378 92 : semantic_type = TREE_TYPE (retval);
13379 92 : retval = TREE_OPERAND (retval, 0);
13380 : }
13381 34637263 : retval = c_fully_fold (retval, false, NULL);
13382 34637263 : if (semantic_type
13383 34637263 : && valtype != NULL_TREE
13384 92 : && TREE_CODE (valtype) != VOID_TYPE)
13385 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13386 : }
13387 :
13388 34637263 : if (!retval)
13389 : {
13390 21314 : current_function_returns_null = 1;
13391 21314 : if ((warn_return_type >= 0 || flag_isoc99)
13392 21189 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13393 : {
13394 46 : no_warning = true;
13395 47 : if (emit_diagnostic (flag_isoc99
13396 : ? diagnostics::kind::permerror
13397 : : diagnostics::kind::warning,
13398 46 : loc, OPT_Wreturn_mismatch,
13399 : "%<return%> with no value,"
13400 : " in function returning non-void"))
13401 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13402 : "declared here");
13403 : }
13404 : }
13405 34637263 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13406 : {
13407 321 : current_function_returns_null = 1;
13408 321 : bool warned_here;
13409 321 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13410 64 : warned_here = permerror_opt
13411 64 : (xloc, OPT_Wreturn_mismatch,
13412 : "%<return%> with a value, in function returning void");
13413 : else
13414 257 : warned_here = pedwarn
13415 257 : (xloc, OPT_Wpedantic, "ISO C forbids "
13416 : "%<return%> with expression, in function returning void");
13417 321 : if (warned_here)
13418 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13419 : "declared here");
13420 : }
13421 : else
13422 : {
13423 34636942 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13424 : retval, origtype, ic_return,
13425 : npc, NULL_TREE, NULL_TREE, 0);
13426 34636942 : tree res = DECL_RESULT (current_function_decl);
13427 34636942 : tree inner;
13428 34636942 : bool save;
13429 :
13430 34636942 : current_function_returns_value = 1;
13431 34636942 : if (t == error_mark_node)
13432 : {
13433 : /* Suppress -Wreturn-type for this function. */
13434 298 : if (warn_return_type)
13435 297 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13436 298 : return NULL_TREE;
13437 : }
13438 :
13439 34636644 : save = in_late_binary_op;
13440 69265177 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13441 34627728 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13442 69263092 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13443 333483 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13444 333483 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13445 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13446 10196 : in_late_binary_op = true;
13447 34636644 : inner = t = convert (TREE_TYPE (res), t);
13448 34636644 : in_late_binary_op = save;
13449 :
13450 : /* Strip any conversions, additions, and subtractions, and see if
13451 : we are returning the address of a local variable. Warn if so. */
13452 37915004 : while (1)
13453 : {
13454 37915004 : switch (TREE_CODE (inner))
13455 : {
13456 3276934 : CASE_CONVERT:
13457 3276934 : case NON_LVALUE_EXPR:
13458 3276934 : case PLUS_EXPR:
13459 3276934 : case POINTER_PLUS_EXPR:
13460 3276934 : inner = TREE_OPERAND (inner, 0);
13461 3276934 : continue;
13462 :
13463 1431 : case MINUS_EXPR:
13464 : /* If the second operand of the MINUS_EXPR has a pointer
13465 : type (or is converted from it), this may be valid, so
13466 : don't give a warning. */
13467 1431 : {
13468 1431 : tree op1 = TREE_OPERAND (inner, 1);
13469 :
13470 3111 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13471 3365 : && (CONVERT_EXPR_P (op1)
13472 1426 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13473 254 : op1 = TREE_OPERAND (op1, 0);
13474 :
13475 1431 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13476 : break;
13477 :
13478 1426 : inner = TREE_OPERAND (inner, 0);
13479 1426 : continue;
13480 1426 : }
13481 :
13482 2931 : case ADDR_EXPR:
13483 2931 : inner = TREE_OPERAND (inner, 0);
13484 :
13485 2931 : while (REFERENCE_CLASS_P (inner)
13486 3817 : && !INDIRECT_REF_P (inner))
13487 886 : inner = TREE_OPERAND (inner, 0);
13488 :
13489 2931 : if (DECL_P (inner)
13490 1610 : && !DECL_EXTERNAL (inner)
13491 973 : && DECL_CONTEXT (inner) == current_function_decl
13492 3148 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13493 : {
13494 177 : if (TREE_CODE (inner) == LABEL_DECL)
13495 4 : warning_at (loc, OPT_Wreturn_local_addr,
13496 : "function returns address of label");
13497 173 : else if (TREE_CODE (inner) == FUNCTION_DECL
13498 173 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13499 8 : || !DECL_INITIAL (inner)))
13500 10 : warning_at (loc, OPT_Wreturn_local_addr,
13501 : "function returns address of nested function "
13502 : "referencing local context");
13503 163 : else if (TREE_CODE (inner) != FUNCTION_DECL
13504 156 : && !TREE_STATIC (inner))
13505 : {
13506 9 : warning_at (loc, OPT_Wreturn_local_addr,
13507 : "function returns address of local variable");
13508 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13509 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13510 : }
13511 : }
13512 : break;
13513 :
13514 : default:
13515 : break;
13516 3276934 : }
13517 :
13518 34636644 : break;
13519 : }
13520 :
13521 34636644 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13522 34636644 : SET_EXPR_LOCATION (retval, loc);
13523 :
13524 34636644 : if (warn_sequence_point)
13525 2964477 : verify_sequence_points (retval);
13526 : }
13527 :
13528 34658279 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13529 34658279 : if (no_warning)
13530 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13531 34658279 : return add_stmt (ret_stmt);
13532 : }
13533 :
13534 : struct c_switch {
13535 : /* The SWITCH_STMT being built. */
13536 : tree switch_stmt;
13537 :
13538 : /* The original type of the testing expression, i.e. before the
13539 : default conversion is applied. */
13540 : tree orig_type;
13541 :
13542 : /* A splay-tree mapping the low element of a case range to the high
13543 : element, or NULL_TREE if there is no high element. Used to
13544 : determine whether or not a new case label duplicates an old case
13545 : label. We need a tree, rather than simply a hash table, because
13546 : of the GNU case range extension. */
13547 : splay_tree cases;
13548 :
13549 : /* The bindings at the point of the switch. This is used for
13550 : warnings crossing decls when branching to a case label. */
13551 : struct c_spot_bindings *bindings;
13552 :
13553 : /* Whether the switch includes any break statements. */
13554 : bool break_stmt_seen_p;
13555 :
13556 : /* The next node on the stack. */
13557 : struct c_switch *next;
13558 :
13559 : /* Remember whether the controlling expression had boolean type
13560 : before integer promotions for the sake of -Wswitch-bool. */
13561 : bool bool_cond_p;
13562 : };
13563 :
13564 : /* A stack of the currently active switch statements. The innermost
13565 : switch statement is on the top of the stack. There is no need to
13566 : mark the stack for garbage collection because it is only active
13567 : during the processing of the body of a function, and we never
13568 : collect at that point. */
13569 :
13570 : struct c_switch *c_switch_stack;
13571 :
13572 : /* Start a C switch statement, testing expression EXP. Return the new
13573 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13574 : SWITCH_COND_LOC is the location of the switch's condition.
13575 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13576 :
13577 : tree
13578 37320 : c_start_switch (location_t switch_loc,
13579 : location_t switch_cond_loc,
13580 : tree exp, bool explicit_cast_p, tree switch_name)
13581 : {
13582 37320 : tree orig_type = error_mark_node;
13583 37320 : bool bool_cond_p = false;
13584 37320 : struct c_switch *cs;
13585 :
13586 37320 : if (exp != error_mark_node)
13587 : {
13588 37277 : orig_type = TREE_TYPE (exp);
13589 :
13590 37277 : if (!INTEGRAL_TYPE_P (orig_type))
13591 : {
13592 12 : if (orig_type != error_mark_node)
13593 : {
13594 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13595 12 : orig_type = error_mark_node;
13596 : }
13597 12 : exp = integer_zero_node;
13598 : }
13599 : else
13600 : {
13601 37265 : tree type = TYPE_MAIN_VARIANT (orig_type);
13602 37265 : tree e = exp;
13603 :
13604 : /* Warn if the condition has boolean value. */
13605 37268 : while (TREE_CODE (e) == COMPOUND_EXPR)
13606 3 : e = TREE_OPERAND (e, 1);
13607 :
13608 37240 : if ((C_BOOLEAN_TYPE_P (type)
13609 37240 : || truth_value_p (TREE_CODE (e)))
13610 : /* Explicit cast to int suppresses this warning. */
13611 37287 : && !(TREE_CODE (type) == INTEGER_TYPE
13612 : && explicit_cast_p))
13613 : bool_cond_p = true;
13614 :
13615 37265 : if (!in_system_header_at (input_location)
13616 37265 : && (type == long_integer_type_node
13617 11952 : || type == long_unsigned_type_node))
13618 282 : warning_at (switch_cond_loc,
13619 282 : OPT_Wtraditional, "%<long%> switch expression not "
13620 : "converted to %<int%> in ISO C");
13621 :
13622 37265 : exp = c_fully_fold (exp, false, NULL);
13623 37265 : exp = default_conversion (exp);
13624 :
13625 37265 : if (warn_sequence_point)
13626 6013 : verify_sequence_points (exp);
13627 : }
13628 : }
13629 :
13630 : /* Add this new SWITCH_STMT to the stack. */
13631 37320 : cs = XNEW (struct c_switch);
13632 37320 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13633 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13634 37320 : cs->orig_type = orig_type;
13635 37320 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13636 37320 : cs->bindings = c_get_switch_bindings ();
13637 37320 : cs->break_stmt_seen_p = false;
13638 37320 : cs->bool_cond_p = bool_cond_p;
13639 37320 : cs->next = c_switch_stack;
13640 37320 : c_switch_stack = cs;
13641 :
13642 37320 : return add_stmt (cs->switch_stmt);
13643 : }
13644 :
13645 : /* Process a case label at location LOC, with attributes ATTRS. */
13646 :
13647 : tree
13648 1030422 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13649 : {
13650 1030422 : tree label = NULL_TREE;
13651 :
13652 1030422 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13653 : {
13654 87 : low_value = c_fully_fold (low_value, false, NULL);
13655 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13656 9 : pedwarn (loc, OPT_Wpedantic,
13657 : "case label is not an integer constant expression");
13658 : }
13659 :
13660 1030422 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13661 : {
13662 0 : high_value = c_fully_fold (high_value, false, NULL);
13663 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13664 0 : pedwarn (input_location, OPT_Wpedantic,
13665 : "case label is not an integer constant expression");
13666 : }
13667 :
13668 1030422 : if (c_switch_stack == NULL)
13669 : {
13670 3 : if (low_value)
13671 2 : error_at (loc, "case label not within a switch statement");
13672 : else
13673 1 : error_at (loc, "%<default%> label not within a switch statement");
13674 3 : return NULL_TREE;
13675 : }
13676 :
13677 1030419 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13678 1030419 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13679 : loc))
13680 : return NULL_TREE;
13681 :
13682 1030407 : label = c_add_case_label (loc, c_switch_stack->cases,
13683 1030407 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13684 : low_value, high_value, attrs);
13685 1030407 : if (label == error_mark_node)
13686 144 : label = NULL_TREE;
13687 : return label;
13688 : }
13689 :
13690 : /* Finish the switch statement. TYPE is the original type of the
13691 : controlling expression of the switch, or NULL_TREE. */
13692 :
13693 : void
13694 37320 : c_finish_switch (tree body, tree type)
13695 : {
13696 37320 : struct c_switch *cs = c_switch_stack;
13697 37320 : location_t switch_location;
13698 :
13699 37320 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13700 :
13701 : /* Emit warnings as needed. */
13702 37320 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13703 43512 : c_do_switch_warnings (cs->cases, switch_location,
13704 6192 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13705 37320 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13706 37320 : if (c_switch_covers_all_cases_p (cs->cases,
13707 37320 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13708 33418 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13709 37320 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13710 :
13711 : /* Pop the stack. */
13712 37320 : c_switch_stack = cs->next;
13713 37320 : splay_tree_delete (cs->cases);
13714 37320 : c_release_switch_bindings (cs->bindings);
13715 37320 : XDELETE (cs);
13716 37320 : }
13717 :
13718 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13719 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13720 : may be null. */
13721 :
13722 : void
13723 1269544 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13724 : tree else_block)
13725 : {
13726 1269544 : tree stmt;
13727 :
13728 1269544 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13729 1269544 : SET_EXPR_LOCATION (stmt, if_locus);
13730 1269544 : add_stmt (stmt);
13731 1269544 : }
13732 :
13733 : tree
13734 187404 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13735 : {
13736 : /* In switch statements break is sometimes stylistically used after
13737 : a return statement. This can lead to spurious warnings about
13738 : control reaching the end of a non-void function when it is
13739 : inlined. Note that we are calling block_may_fallthru with
13740 : language specific tree nodes; this works because
13741 : block_may_fallthru returns true when given something it does not
13742 : understand. */
13743 187404 : bool skip = !block_may_fallthru (cur_stmt_list);
13744 :
13745 187404 : if (is_break)
13746 174242 : switch (in_statement & ~IN_NAMED_STMT)
13747 : {
13748 9 : case 0:
13749 9 : error_at (loc, "break statement not within loop or switch");
13750 9 : return NULL_TREE;
13751 4 : case IN_OMP_BLOCK:
13752 4 : error_at (loc, "invalid exit from OpenMP structured block");
13753 4 : return NULL_TREE;
13754 12 : case IN_OMP_FOR:
13755 12 : error_at (loc, "break statement used with OpenMP for loop");
13756 12 : return NULL_TREE;
13757 : case IN_ITERATION_STMT:
13758 : case IN_OBJC_FOREACH:
13759 : break;
13760 158450 : default:
13761 158450 : gcc_assert (in_statement & IN_SWITCH_STMT);
13762 158450 : c_switch_stack->break_stmt_seen_p = true;
13763 158450 : break;
13764 : }
13765 : else
13766 13162 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13767 : {
13768 7 : case 0:
13769 7 : error_at (loc, "continue statement not within a loop");
13770 7 : return NULL_TREE;
13771 4 : case IN_OMP_BLOCK:
13772 4 : error_at (loc, "invalid exit from OpenMP structured block");
13773 4 : return NULL_TREE;
13774 : case IN_ITERATION_STMT:
13775 : case IN_OMP_FOR:
13776 : case IN_OBJC_FOREACH:
13777 : break;
13778 0 : default:
13779 0 : gcc_unreachable ();
13780 : }
13781 :
13782 187368 : if (skip)
13783 : return NULL_TREE;
13784 186904 : else if ((in_statement & IN_OBJC_FOREACH)
13785 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13786 0 : && name == NULL_TREE)
13787 : {
13788 : /* The foreach expander produces low-level code using gotos instead
13789 : of a structured loop construct. */
13790 0 : gcc_assert (label);
13791 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13792 : }
13793 187001 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13794 : {
13795 0 : label = DECL_CHAIN (name);
13796 0 : if (!is_break)
13797 0 : label = DECL_CHAIN (label);
13798 : /* Foreach expander from some outer level. */
13799 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13800 : }
13801 200055 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13802 186904 : name));
13803 : }
13804 :
13805 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13806 :
13807 : static void
13808 6361694 : emit_side_effect_warnings (location_t loc, tree expr)
13809 : {
13810 6361694 : maybe_warn_nodiscard (loc, expr);
13811 6361694 : if (!warn_unused_value)
13812 : return;
13813 1294105 : if (expr == error_mark_node)
13814 : ;
13815 1294030 : else if (!TREE_SIDE_EFFECTS (expr))
13816 : {
13817 6446 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13818 6446 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13819 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13820 : }
13821 1287584 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13822 : {
13823 : tree r = expr;
13824 : location_t cloc = loc;
13825 11991 : while (TREE_CODE (r) == COMPOUND_EXPR)
13826 : {
13827 6011 : if (EXPR_HAS_LOCATION (r))
13828 5481 : cloc = EXPR_LOCATION (r);
13829 6011 : r = TREE_OPERAND (r, 1);
13830 : }
13831 5980 : if (!TREE_SIDE_EFFECTS (r)
13832 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13833 24 : && !CONVERT_EXPR_P (r)
13834 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13835 5992 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13836 8 : warning_at (cloc, OPT_Wunused_value,
13837 : "right-hand operand of comma expression has no effect");
13838 : }
13839 : else
13840 1281604 : warn_if_unused_value (expr, loc);
13841 : }
13842 :
13843 : /* Process an expression as if it were a complete statement. Emit
13844 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13845 : statement. */
13846 :
13847 : tree
13848 6238289 : c_process_expr_stmt (location_t loc, tree expr)
13849 : {
13850 6238289 : tree exprv;
13851 :
13852 6238289 : if (!expr)
13853 : return NULL_TREE;
13854 :
13855 6233958 : expr = c_fully_fold (expr, false, NULL);
13856 :
13857 6233958 : if (warn_sequence_point)
13858 1289897 : verify_sequence_points (expr);
13859 :
13860 6233958 : if (TREE_TYPE (expr) != error_mark_node
13861 6231381 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13862 6233958 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13863 0 : error_at (loc, "expression statement has incomplete type");
13864 :
13865 : /* If we're not processing a statement expression, warn about unused values.
13866 : Warnings for statement expressions will be emitted later, once we figure
13867 : out which is the result. */
13868 6233958 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13869 6233958 : && (warn_unused_value || warn_unused_result))
13870 6147874 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13871 :
13872 : exprv = expr;
13873 6272676 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13874 38719 : exprv = TREE_OPERAND (exprv, 1);
13875 6245248 : while (CONVERT_EXPR_P (exprv))
13876 11291 : exprv = TREE_OPERAND (exprv, 0);
13877 6233957 : if (DECL_P (exprv)
13878 6234542 : || handled_component_p (exprv)
13879 12448253 : || TREE_CODE (exprv) == ADDR_EXPR)
13880 20246 : mark_exp_read (exprv);
13881 :
13882 : /* If the expression is not of a type to which we cannot assign a line
13883 : number, wrap the thing in a no-op NOP_EXPR. */
13884 6233957 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13885 : {
13886 14871 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13887 14871 : SET_EXPR_LOCATION (expr, loc);
13888 : }
13889 :
13890 : return expr;
13891 : }
13892 :
13893 : /* Emit an expression as a statement. LOC is the location of the
13894 : expression. */
13895 :
13896 : tree
13897 5937840 : c_finish_expr_stmt (location_t loc, tree expr)
13898 : {
13899 5937840 : if (expr)
13900 5925302 : return add_stmt (c_process_expr_stmt (loc, expr));
13901 : else
13902 : return NULL;
13903 : }
13904 :
13905 : /* Do the opposite and emit a statement as an expression. To begin,
13906 : create a new binding level and return it. */
13907 :
13908 : tree
13909 34875 : c_begin_stmt_expr (void)
13910 : {
13911 34875 : tree ret;
13912 :
13913 : /* We must force a BLOCK for this level so that, if it is not expanded
13914 : later, there is a way to turn off the entire subtree of blocks that
13915 : are contained in it. */
13916 34875 : keep_next_level ();
13917 34875 : ret = c_begin_compound_stmt (true);
13918 :
13919 34875 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13920 : ? NULL
13921 : : c_switch_stack->bindings);
13922 :
13923 : /* Mark the current statement list as belonging to a statement list. */
13924 34875 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13925 :
13926 34875 : return ret;
13927 : }
13928 :
13929 : /* LOC is the location of the compound statement to which this body
13930 : belongs. */
13931 :
13932 : tree
13933 34875 : c_finish_stmt_expr (location_t loc, tree body)
13934 : {
13935 34875 : tree last, type, tmp, val;
13936 34875 : tree *last_p;
13937 :
13938 34875 : body = c_end_compound_stmt (loc, body, true);
13939 :
13940 34875 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13941 : ? NULL
13942 : : c_switch_stack->bindings);
13943 :
13944 : /* Locate the last statement in BODY. See c_end_compound_stmt
13945 : about always returning a BIND_EXPR. */
13946 34875 : last_p = &BIND_EXPR_BODY (body);
13947 34875 : last = BIND_EXPR_BODY (body);
13948 :
13949 34875 : continue_searching:
13950 34875 : if (TREE_CODE (last) == STATEMENT_LIST)
13951 : {
13952 26061 : tree_stmt_iterator l = tsi_last (last);
13953 :
13954 26067 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13955 6 : tsi_prev (&l);
13956 :
13957 : /* This can happen with degenerate cases like ({ }). No value. */
13958 26061 : if (tsi_end_p (l))
13959 389 : return body;
13960 :
13961 : /* If we're supposed to generate side effects warnings, process
13962 : all of the statements except the last. */
13963 25672 : if (warn_unused_value || warn_unused_result)
13964 : {
13965 25672 : for (tree_stmt_iterator i = tsi_start (last);
13966 275711 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13967 : {
13968 250039 : location_t tloc;
13969 250039 : tree t = tsi_stmt (i);
13970 :
13971 250039 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13972 250039 : emit_side_effect_warnings (tloc, t);
13973 : }
13974 : }
13975 25672 : last_p = tsi_stmt_ptr (l);
13976 25672 : last = *last_p;
13977 : }
13978 :
13979 : /* If the end of the list is exception related, then the list was split
13980 : by a call to push_cleanup. Continue searching. */
13981 34486 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
13982 34486 : || TREE_CODE (last) == TRY_CATCH_EXPR)
13983 : {
13984 0 : last_p = &TREE_OPERAND (last, 0);
13985 0 : last = *last_p;
13986 0 : goto continue_searching;
13987 : }
13988 :
13989 34486 : if (last == error_mark_node)
13990 : return last;
13991 :
13992 : /* In the case that the BIND_EXPR is not necessary, return the
13993 : expression out from inside it. */
13994 34477 : if ((last == BIND_EXPR_BODY (body)
13995 : /* Skip nested debug stmts. */
13996 25671 : || last == expr_first (BIND_EXPR_BODY (body)))
13997 45484 : && BIND_EXPR_VARS (body) == NULL)
13998 : {
13999 : /* Even if this looks constant, do not allow it in a constant
14000 : expression. */
14001 10995 : last = c_wrap_maybe_const (last, true);
14002 : /* Do not warn if the return value of a statement expression is
14003 : unused. */
14004 10995 : suppress_warning (last, OPT_Wunused);
14005 10995 : return last;
14006 : }
14007 :
14008 : /* Extract the type of said expression. */
14009 23482 : type = TREE_TYPE (last);
14010 :
14011 : /* If we're not returning a value at all, then the BIND_EXPR that
14012 : we already have is a fine expression to return. */
14013 23482 : if (!type || VOID_TYPE_P (type))
14014 : return body;
14015 :
14016 : /* Now that we've located the expression containing the value, it seems
14017 : silly to make voidify_wrapper_expr repeat the process. Create a
14018 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14019 18158 : tmp = create_tmp_var_raw (type);
14020 :
14021 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14022 : tree_expr_nonnegative_p giving up immediately. */
14023 18158 : val = last;
14024 18158 : if (TREE_CODE (val) == NOP_EXPR
14025 18158 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14026 3773 : val = TREE_OPERAND (val, 0);
14027 :
14028 18158 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14029 18158 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14030 :
14031 18158 : {
14032 18158 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14033 18158 : SET_EXPR_LOCATION (t, loc);
14034 18158 : return t;
14035 : }
14036 : }
14037 :
14038 : /* Begin and end compound statements. This is as simple as pushing
14039 : and popping new statement lists from the tree. */
14040 :
14041 : tree
14042 41175386 : c_begin_compound_stmt (bool do_scope)
14043 : {
14044 41175386 : tree stmt = push_stmt_list ();
14045 41175386 : if (do_scope)
14046 41079377 : push_scope ();
14047 41175386 : return stmt;
14048 : }
14049 :
14050 : /* End a compound statement. STMT is the statement. LOC is the
14051 : location of the compound statement-- this is usually the location
14052 : of the opening brace. */
14053 :
14054 : tree
14055 41175385 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14056 : {
14057 41175385 : tree block = NULL;
14058 :
14059 41175385 : if (do_scope)
14060 : {
14061 41079376 : if (c_dialect_objc ())
14062 0 : objc_clear_super_receiver ();
14063 41079376 : block = pop_scope ();
14064 : }
14065 :
14066 41175385 : stmt = pop_stmt_list (stmt);
14067 41175385 : stmt = c_build_bind_expr (loc, block, stmt);
14068 :
14069 : /* If this compound statement is nested immediately inside a statement
14070 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14071 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14072 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14073 : was really last. */
14074 82350770 : if (building_stmt_list_p ()
14075 41175300 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14076 41298046 : && TREE_CODE (stmt) != BIND_EXPR)
14077 : {
14078 120918 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14079 120918 : TREE_SIDE_EFFECTS (stmt) = 1;
14080 120918 : SET_EXPR_LOCATION (stmt, loc);
14081 : }
14082 :
14083 41175385 : return stmt;
14084 : }
14085 :
14086 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14087 : when the current scope is exited. EH_ONLY is true when this is not
14088 : meant to apply to normal control flow transfer. */
14089 :
14090 : void
14091 131 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14092 : {
14093 131 : enum tree_code code;
14094 131 : tree stmt, list;
14095 131 : bool stmt_expr;
14096 :
14097 131 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14098 131 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14099 131 : add_stmt (stmt);
14100 131 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14101 131 : list = push_stmt_list ();
14102 131 : TREE_OPERAND (stmt, 0) = list;
14103 131 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14104 131 : }
14105 :
14106 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14107 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14108 :
14109 : static tree
14110 101020 : build_vec_cmp (tree_code code, tree type,
14111 : tree arg0, tree arg1)
14112 : {
14113 101020 : tree zero_vec = build_zero_cst (type);
14114 101020 : tree minus_one_vec = build_minus_one_cst (type);
14115 101020 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14116 101020 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14117 101020 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14118 : }
14119 :
14120 : /* Possibly warn about an address of OP never being NULL in a comparison
14121 : operation CODE involving null. */
14122 :
14123 : static void
14124 51586 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14125 : {
14126 : /* Prevent warnings issued for macro expansion. */
14127 51586 : if (!warn_address
14128 30225 : || warning_suppressed_p (op, OPT_Waddress)
14129 81467 : || from_macro_expansion_at (loc))
14130 24461 : return;
14131 :
14132 27125 : if (TREE_CODE (op) == NOP_EXPR)
14133 : {
14134 : /* Allow casts to intptr_t to suppress the warning. */
14135 1133 : tree type = TREE_TYPE (op);
14136 1133 : if (TREE_CODE (type) == INTEGER_TYPE)
14137 : return;
14138 1133 : op = TREE_OPERAND (op, 0);
14139 : }
14140 :
14141 27125 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14142 : {
14143 : /* Allow a cast to void* to suppress the warning. */
14144 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14145 24 : if (VOID_TYPE_P (type))
14146 : return;
14147 :
14148 : /* Adding any value to a null pointer, including zero, is undefined
14149 : in C. This includes the expression &p[0] where p is the null
14150 : pointer, although &p[0] will have been folded to p by this point
14151 : and so not diagnosed. */
14152 24 : if (code == EQ_EXPR)
14153 22 : warning_at (loc, OPT_Waddress,
14154 : "the comparison will always evaluate as %<false%> "
14155 : "for the pointer operand in %qE must not be NULL",
14156 : op);
14157 : else
14158 2 : warning_at (loc, OPT_Waddress,
14159 : "the comparison will always evaluate as %<true%> "
14160 : "for the pointer operand in %qE must not be NULL",
14161 : op);
14162 :
14163 24 : return;
14164 : }
14165 :
14166 27101 : if (TREE_CODE (op) != ADDR_EXPR)
14167 : return;
14168 :
14169 254 : op = TREE_OPERAND (op, 0);
14170 :
14171 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14172 254 : || TREE_CODE (op) == REALPART_EXPR)
14173 : {
14174 : /* The address of either complex part may not be null. */
14175 14 : if (code == EQ_EXPR)
14176 9 : warning_at (loc, OPT_Waddress,
14177 : "the comparison will always evaluate as %<false%> "
14178 : "for the address of %qE will never be NULL",
14179 : op);
14180 : else
14181 5 : warning_at (loc, OPT_Waddress,
14182 : "the comparison will always evaluate as %<true%> "
14183 : "for the address of %qE will never be NULL",
14184 : op);
14185 14 : return;
14186 : }
14187 :
14188 : /* Set to true in the loop below if OP dereferences is operand.
14189 : In such a case the ultimate target need not be a decl for
14190 : the null [in]equality test to be constant. */
14191 : bool deref = false;
14192 :
14193 : /* Get the outermost array or object, or member. */
14194 294 : while (handled_component_p (op))
14195 : {
14196 72 : if (TREE_CODE (op) == COMPONENT_REF)
14197 : {
14198 : /* Get the member (its address is never null). */
14199 18 : op = TREE_OPERAND (op, 1);
14200 18 : break;
14201 : }
14202 :
14203 : /* Get the outer array/object to refer to in the warning. */
14204 54 : op = TREE_OPERAND (op, 0);
14205 54 : deref = true;
14206 : }
14207 :
14208 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14209 371 : || from_macro_expansion_at (loc))
14210 109 : return;
14211 :
14212 131 : bool w;
14213 131 : if (code == EQ_EXPR)
14214 93 : w = warning_at (loc, OPT_Waddress,
14215 : "the comparison will always evaluate as %<false%> "
14216 : "for the address of %qE will never be NULL",
14217 : op);
14218 : else
14219 38 : w = warning_at (loc, OPT_Waddress,
14220 : "the comparison will always evaluate as %<true%> "
14221 : "for the address of %qE will never be NULL",
14222 : op);
14223 :
14224 131 : if (w && DECL_P (op))
14225 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14226 : }
14227 :
14228 : /* Build a binary-operation expression without default conversions.
14229 : CODE is the kind of expression to build.
14230 : LOCATION is the operator's location.
14231 : This function differs from `build' in several ways:
14232 : the data type of the result is computed and recorded in it,
14233 : warnings are generated if arg data types are invalid,
14234 : special handling for addition and subtraction of pointers is known,
14235 : and some optimization is done (operations on narrow ints
14236 : are done in the narrower type when that gives the same result).
14237 : Constant folding is also done before the result is returned.
14238 :
14239 : Note that the operands will never have enumeral types, or function
14240 : or array types, because either they will have the default conversions
14241 : performed or they have both just been converted to some other type in which
14242 : the arithmetic is to be done. */
14243 :
14244 : tree
14245 16818059 : build_binary_op (location_t location, enum tree_code code,
14246 : tree orig_op0, tree orig_op1, bool convert_p)
14247 : {
14248 16818059 : tree type0, type1, orig_type0, orig_type1;
14249 16818059 : tree eptype;
14250 16818059 : enum tree_code code0, code1;
14251 16818059 : tree op0, op1;
14252 16818059 : tree ret = error_mark_node;
14253 16818059 : const char *invalid_op_diag;
14254 16818059 : bool op0_int_operands, op1_int_operands;
14255 16818059 : bool int_const, int_const_or_overflow, int_operands;
14256 :
14257 : /* Expression code to give to the expression when it is built.
14258 : Normally this is CODE, which is what the caller asked for,
14259 : but in some special cases we change it. */
14260 16818059 : enum tree_code resultcode = code;
14261 :
14262 : /* Data type in which the computation is to be performed.
14263 : In the simplest cases this is the common type of the arguments. */
14264 16818059 : tree result_type = NULL;
14265 :
14266 : /* When the computation is in excess precision, the type of the
14267 : final EXCESS_PRECISION_EXPR. */
14268 16818059 : tree semantic_result_type = NULL;
14269 :
14270 : /* Nonzero means operands have already been type-converted
14271 : in whatever way is necessary.
14272 : Zero means they need to be converted to RESULT_TYPE. */
14273 16818059 : int converted = 0;
14274 :
14275 : /* Nonzero means create the expression with this type, rather than
14276 : RESULT_TYPE. */
14277 16818059 : tree build_type = NULL_TREE;
14278 :
14279 : /* Nonzero means after finally constructing the expression
14280 : convert it to this type. */
14281 16818059 : tree final_type = NULL_TREE;
14282 :
14283 : /* Nonzero if this is an operation like MIN or MAX which can
14284 : safely be computed in short if both args are promoted shorts.
14285 : Also implies COMMON.
14286 : -1 indicates a bitwise operation; this makes a difference
14287 : in the exact conditions for when it is safe to do the operation
14288 : in a narrower mode. */
14289 16818059 : int shorten = 0;
14290 :
14291 : /* Nonzero if this is a comparison operation;
14292 : if both args are promoted shorts, compare the original shorts.
14293 : Also implies COMMON. */
14294 16818059 : int short_compare = 0;
14295 :
14296 : /* Nonzero if this is a right-shift operation, which can be computed on the
14297 : original short and then promoted if the operand is a promoted short. */
14298 16818059 : int short_shift = 0;
14299 :
14300 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14301 16818059 : int common = 0;
14302 :
14303 : /* True means types are compatible as far as ObjC is concerned. */
14304 16818059 : bool objc_ok;
14305 :
14306 : /* True means this is an arithmetic operation that may need excess
14307 : precision. */
14308 16818059 : bool may_need_excess_precision;
14309 :
14310 : /* True means this is a boolean operation that converts both its
14311 : operands to truth-values. */
14312 16818059 : bool boolean_op = false;
14313 :
14314 : /* Remember whether we're doing / or %. */
14315 16818059 : bool doing_div_or_mod = false;
14316 :
14317 : /* Remember whether we're doing << or >>. */
14318 16818059 : bool doing_shift = false;
14319 :
14320 : /* Tree holding instrumentation expression. */
14321 16818059 : tree instrument_expr = NULL;
14322 :
14323 16818059 : if (location == UNKNOWN_LOCATION)
14324 79 : location = input_location;
14325 :
14326 16818059 : op0 = orig_op0;
14327 16818059 : op1 = orig_op1;
14328 :
14329 16818059 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14330 8126886 : if (op0_int_operands)
14331 8126886 : op0 = remove_c_maybe_const_expr (op0);
14332 16818059 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14333 11390195 : if (op1_int_operands)
14334 11390195 : op1 = remove_c_maybe_const_expr (op1);
14335 28208254 : int_operands = (op0_int_operands && op1_int_operands);
14336 11390195 : if (int_operands)
14337 : {
14338 15900335 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14339 7965334 : && TREE_CODE (orig_op1) == INTEGER_CST);
14340 7935001 : int_const = (int_const_or_overflow
14341 7935001 : && !TREE_OVERFLOW (orig_op0)
14342 7934932 : && !TREE_OVERFLOW (orig_op1));
14343 : }
14344 : else
14345 : int_const = int_const_or_overflow = false;
14346 :
14347 : /* Do not apply default conversion in mixed vector/scalar expression. */
14348 16818059 : if (convert_p
14349 16818059 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14350 : {
14351 10120389 : op0 = default_conversion (op0);
14352 10120389 : op1 = default_conversion (op1);
14353 : }
14354 :
14355 16818059 : orig_type0 = type0 = TREE_TYPE (op0);
14356 :
14357 16818059 : orig_type1 = type1 = TREE_TYPE (op1);
14358 :
14359 : /* The expression codes of the data types of the arguments tell us
14360 : whether the arguments are integers, floating, pointers, etc. */
14361 16818059 : code0 = TREE_CODE (type0);
14362 16818059 : code1 = TREE_CODE (type1);
14363 :
14364 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14365 16818061 : STRIP_TYPE_NOPS (op0);
14366 16818062 : STRIP_TYPE_NOPS (op1);
14367 :
14368 : /* If an error was already reported for one of the arguments,
14369 : avoid reporting another error. */
14370 :
14371 16818059 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14372 710 : return error_mark_node;
14373 :
14374 16817349 : if (code0 == POINTER_TYPE
14375 16817349 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14376 11 : return error_mark_node;
14377 :
14378 16817338 : if (code1 == POINTER_TYPE
14379 16817338 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14380 11 : return error_mark_node;
14381 :
14382 33634654 : if ((invalid_op_diag
14383 16817327 : = targetm.invalid_binary_op (code, type0, type1)))
14384 : {
14385 0 : error_at (location, invalid_op_diag);
14386 0 : return error_mark_node;
14387 : }
14388 :
14389 16817327 : switch (code)
14390 : {
14391 : case PLUS_EXPR:
14392 : case MINUS_EXPR:
14393 : case MULT_EXPR:
14394 : case TRUNC_DIV_EXPR:
14395 : case CEIL_DIV_EXPR:
14396 : case FLOOR_DIV_EXPR:
14397 : case ROUND_DIV_EXPR:
14398 : case EXACT_DIV_EXPR:
14399 : may_need_excess_precision = true;
14400 : break;
14401 :
14402 2730619 : case EQ_EXPR:
14403 2730619 : case NE_EXPR:
14404 2730619 : case LE_EXPR:
14405 2730619 : case GE_EXPR:
14406 2730619 : case LT_EXPR:
14407 2730619 : case GT_EXPR:
14408 : /* Excess precision for implicit conversions of integers to
14409 : floating point in C11 and later. */
14410 2730619 : may_need_excess_precision = (flag_isoc11
14411 2730619 : && (ANY_INTEGRAL_TYPE_P (type0)
14412 485590 : || ANY_INTEGRAL_TYPE_P (type1)));
14413 : break;
14414 :
14415 : default:
14416 16817327 : may_need_excess_precision = false;
14417 : break;
14418 : }
14419 16817327 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14420 : {
14421 184 : op0 = TREE_OPERAND (op0, 0);
14422 184 : type0 = TREE_TYPE (op0);
14423 : }
14424 16817143 : else if (may_need_excess_precision
14425 16817143 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14426 : {
14427 771 : type0 = eptype;
14428 771 : op0 = convert (eptype, op0);
14429 : }
14430 16817327 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14431 : {
14432 887 : op1 = TREE_OPERAND (op1, 0);
14433 887 : type1 = TREE_TYPE (op1);
14434 : }
14435 16816440 : else if (may_need_excess_precision
14436 16816440 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14437 : {
14438 592 : type1 = eptype;
14439 592 : op1 = convert (eptype, op1);
14440 : }
14441 :
14442 16817327 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14443 :
14444 : /* In case when one of the operands of the binary operation is
14445 : a vector and another is a scalar -- convert scalar to vector. */
14446 18397528 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14447 18395647 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14448 : {
14449 2488 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14450 : orig_op1, true);
14451 :
14452 2488 : switch (convert_flag)
14453 : {
14454 12 : case stv_error:
14455 12 : return error_mark_node;
14456 594 : case stv_firstarg:
14457 594 : {
14458 594 : bool maybe_const = true;
14459 594 : tree sc;
14460 594 : sc = c_fully_fold (op0, false, &maybe_const);
14461 594 : sc = save_expr (sc);
14462 594 : sc = convert (TREE_TYPE (type1), sc);
14463 594 : op0 = build_vector_from_val (type1, sc);
14464 594 : if (!maybe_const)
14465 93 : op0 = c_wrap_maybe_const (op0, true);
14466 594 : orig_type0 = type0 = TREE_TYPE (op0);
14467 594 : code0 = TREE_CODE (type0);
14468 594 : converted = 1;
14469 594 : break;
14470 : }
14471 1048 : case stv_secondarg:
14472 1048 : {
14473 1048 : bool maybe_const = true;
14474 1048 : tree sc;
14475 1048 : sc = c_fully_fold (op1, false, &maybe_const);
14476 1048 : sc = save_expr (sc);
14477 1048 : sc = convert (TREE_TYPE (type0), sc);
14478 1048 : op1 = build_vector_from_val (type0, sc);
14479 1048 : if (!maybe_const)
14480 37 : op1 = c_wrap_maybe_const (op1, true);
14481 1048 : orig_type1 = type1 = TREE_TYPE (op1);
14482 1048 : code1 = TREE_CODE (type1);
14483 1048 : converted = 1;
14484 1048 : break;
14485 : }
14486 : default:
14487 : break;
14488 : }
14489 : }
14490 :
14491 16817315 : switch (code)
14492 : {
14493 8620863 : case PLUS_EXPR:
14494 : /* Handle the pointer + int case. */
14495 8620863 : if (code0 == POINTER_TYPE
14496 1249125 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14497 : {
14498 1249119 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14499 1249119 : goto return_build_binary_op;
14500 : }
14501 7371744 : else if (code1 == POINTER_TYPE
14502 1538 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14503 : {
14504 1532 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14505 1532 : goto return_build_binary_op;
14506 : }
14507 : else
14508 : common = 1;
14509 : break;
14510 :
14511 790716 : case MINUS_EXPR:
14512 : /* Subtraction of two similar pointers.
14513 : We must subtract them as integers, then divide by object size. */
14514 790716 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14515 790716 : && comp_target_types (location, type0, type1))
14516 : {
14517 3641 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14518 3641 : goto return_build_binary_op;
14519 : }
14520 : /* Handle pointer minus int. Just like pointer plus int. */
14521 787075 : else if (code0 == POINTER_TYPE
14522 16818 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14523 : {
14524 16806 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14525 16806 : goto return_build_binary_op;
14526 : }
14527 : else
14528 : common = 1;
14529 : break;
14530 :
14531 : case MULT_EXPR:
14532 : common = 1;
14533 : break;
14534 :
14535 273690 : case TRUNC_DIV_EXPR:
14536 273690 : case CEIL_DIV_EXPR:
14537 273690 : case FLOOR_DIV_EXPR:
14538 273690 : case ROUND_DIV_EXPR:
14539 273690 : case EXACT_DIV_EXPR:
14540 273690 : doing_div_or_mod = true;
14541 273690 : warn_for_div_by_zero (location, op1);
14542 :
14543 273689 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14544 49638 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14545 47644 : || code0 == COMPLEX_TYPE
14546 45594 : || gnu_vector_type_p (type0))
14547 323326 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14548 47204 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14549 47057 : || code1 == COMPLEX_TYPE
14550 45596 : || gnu_vector_type_p (type1)))
14551 : {
14552 273685 : enum tree_code tcode0 = code0, tcode1 = code1;
14553 :
14554 273685 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14555 47643 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14556 273685 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14557 47054 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14558 :
14559 273685 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14560 69765 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14561 203921 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14562 12030 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14563 81793 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14564 : resultcode = RDIV_EXPR;
14565 : else
14566 : /* Although it would be tempting to shorten always here, that
14567 : loses on some targets, since the modulo instruction is
14568 : undefined if the quotient can't be represented in the
14569 : computation mode. We shorten only if unsigned or if
14570 : dividing by something we know != -1. */
14571 191892 : shorten = may_shorten_divmod (op0, op1);
14572 : common = 1;
14573 : }
14574 : break;
14575 :
14576 1585835 : case BIT_AND_EXPR:
14577 1585835 : case BIT_IOR_EXPR:
14578 1585835 : case BIT_XOR_EXPR:
14579 1585835 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14580 1050296 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14581 : shorten = -1;
14582 : /* Allow vector types which are not floating point types. */
14583 535575 : else if (gnu_vector_type_p (type0)
14584 535498 : && gnu_vector_type_p (type1)
14585 535498 : && !VECTOR_FLOAT_TYPE_P (type0)
14586 1071070 : && !VECTOR_FLOAT_TYPE_P (type1))
14587 : common = 1;
14588 : break;
14589 :
14590 58517 : case TRUNC_MOD_EXPR:
14591 58517 : case FLOOR_MOD_EXPR:
14592 58517 : doing_div_or_mod = true;
14593 58517 : warn_for_div_by_zero (location, op1);
14594 :
14595 58517 : if (gnu_vector_type_p (type0)
14596 297 : && gnu_vector_type_p (type1)
14597 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14598 58814 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14599 : common = 1;
14600 58220 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14601 58219 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14602 : {
14603 : /* Although it would be tempting to shorten always here, that loses
14604 : on some targets, since the modulo instruction is undefined if the
14605 : quotient can't be represented in the computation mode. We shorten
14606 : only if unsigned or if dividing by something we know != -1. */
14607 58219 : shorten = may_shorten_divmod (op0, op1);
14608 58219 : common = 1;
14609 : }
14610 : break;
14611 :
14612 509279 : case TRUTH_ANDIF_EXPR:
14613 509279 : case TRUTH_ORIF_EXPR:
14614 509279 : case TRUTH_AND_EXPR:
14615 509279 : case TRUTH_OR_EXPR:
14616 509279 : case TRUTH_XOR_EXPR:
14617 509279 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14618 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14619 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14620 0 : || code0 == BITINT_TYPE)
14621 509279 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14622 159 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14623 20 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14624 18 : || code1 == BITINT_TYPE))
14625 : {
14626 : /* Result of these operations is always an int,
14627 : but that does not mean the operands should be
14628 : converted to ints! */
14629 509279 : result_type = integer_type_node;
14630 509279 : if (op0_int_operands)
14631 : {
14632 110114 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14633 110114 : op0 = remove_c_maybe_const_expr (op0);
14634 : }
14635 : else
14636 399165 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14637 509279 : if (op1_int_operands)
14638 : {
14639 71500 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14640 71500 : op1 = remove_c_maybe_const_expr (op1);
14641 : }
14642 : else
14643 437779 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14644 : converted = 1;
14645 : boolean_op = true;
14646 : }
14647 509279 : if (code == TRUTH_ANDIF_EXPR)
14648 : {
14649 248261 : int_const_or_overflow = (int_operands
14650 63364 : && TREE_CODE (orig_op0) == INTEGER_CST
14651 248277 : && (op0 == truthvalue_false_node
14652 13273 : || TREE_CODE (orig_op1) == INTEGER_CST));
14653 : int_const = (int_const_or_overflow
14654 63337 : && !TREE_OVERFLOW (orig_op0)
14655 63337 : && (op0 == truthvalue_false_node
14656 13257 : || !TREE_OVERFLOW (orig_op1)));
14657 : }
14658 324355 : else if (code == TRUTH_ORIF_EXPR)
14659 : {
14660 331030 : int_const_or_overflow = (int_operands
14661 6826 : && TREE_CODE (orig_op0) == INTEGER_CST
14662 331053 : && (op0 == truthvalue_true_node
14663 2141 : || TREE_CODE (orig_op1) == INTEGER_CST));
14664 : int_const = (int_const_or_overflow
14665 6796 : && !TREE_OVERFLOW (orig_op0)
14666 6796 : && (op0 == truthvalue_true_node
14667 2118 : || !TREE_OVERFLOW (orig_op1)));
14668 : }
14669 : break;
14670 :
14671 : /* Shift operations: result has same type as first operand;
14672 : always convert second operand to int.
14673 : Also set SHORT_SHIFT if shifting rightward. */
14674 :
14675 231227 : case RSHIFT_EXPR:
14676 231227 : if (gnu_vector_type_p (type0)
14677 807 : && gnu_vector_type_p (type1)
14678 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14679 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14680 231483 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14681 : TYPE_VECTOR_SUBPARTS (type1)))
14682 : {
14683 : result_type = type0;
14684 : converted = 1;
14685 : }
14686 230973 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14687 1105 : || code0 == BITINT_TYPE
14688 557 : || (gnu_vector_type_p (type0)
14689 553 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14690 232071 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14691 : {
14692 230964 : doing_shift = true;
14693 230964 : if (TREE_CODE (op1) == INTEGER_CST)
14694 : {
14695 201547 : if (tree_int_cst_sgn (op1) < 0)
14696 : {
14697 269 : int_const = false;
14698 269 : if (c_inhibit_evaluation_warnings == 0)
14699 129 : warning_at (location, OPT_Wshift_count_negative,
14700 : "right shift count is negative");
14701 : }
14702 201278 : else if (code0 == VECTOR_TYPE)
14703 : {
14704 458 : if (compare_tree_int (op1,
14705 458 : TYPE_PRECISION (TREE_TYPE (type0)))
14706 : >= 0)
14707 : {
14708 16 : int_const = false;
14709 16 : if (c_inhibit_evaluation_warnings == 0)
14710 16 : warning_at (location, OPT_Wshift_count_overflow,
14711 : "right shift count >= width of vector element");
14712 : }
14713 : }
14714 : else
14715 : {
14716 200820 : if (!integer_zerop (op1))
14717 200462 : short_shift = 1;
14718 :
14719 200820 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14720 : {
14721 584 : int_const = false;
14722 584 : if (c_inhibit_evaluation_warnings == 0)
14723 50 : warning_at (location, OPT_Wshift_count_overflow,
14724 : "right shift count >= width of type");
14725 : }
14726 : }
14727 : }
14728 :
14729 : /* Use the type of the value to be shifted. */
14730 : result_type = type0;
14731 : /* Avoid converting op1 to result_type later. */
14732 : converted = 1;
14733 : }
14734 : break;
14735 :
14736 803203 : case LSHIFT_EXPR:
14737 803203 : if (gnu_vector_type_p (type0)
14738 595 : && gnu_vector_type_p (type1)
14739 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14740 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14741 803516 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14742 : TYPE_VECTOR_SUBPARTS (type1)))
14743 : {
14744 : result_type = type0;
14745 : converted = 1;
14746 : }
14747 802892 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14748 776 : || code0 == BITINT_TYPE
14749 290 : || (gnu_vector_type_p (type0)
14750 284 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14751 803659 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14752 : {
14753 802878 : doing_shift = true;
14754 802878 : if (TREE_CODE (op0) == INTEGER_CST
14755 438589 : && tree_int_cst_sgn (op0) < 0
14756 803627 : && !TYPE_OVERFLOW_WRAPS (type0))
14757 : {
14758 : /* Don't reject a left shift of a negative value in a context
14759 : where a constant expression is needed in C90. */
14760 712 : if (flag_isoc99)
14761 666 : int_const = false;
14762 712 : if (c_inhibit_evaluation_warnings == 0)
14763 712 : warning_at (location, OPT_Wshift_negative_value,
14764 : "left shift of negative value");
14765 : }
14766 802878 : if (TREE_CODE (op1) == INTEGER_CST)
14767 : {
14768 730446 : if (tree_int_cst_sgn (op1) < 0)
14769 : {
14770 313 : int_const = false;
14771 313 : if (c_inhibit_evaluation_warnings == 0)
14772 135 : warning_at (location, OPT_Wshift_count_negative,
14773 : "left shift count is negative");
14774 : }
14775 730133 : else if (code0 == VECTOR_TYPE)
14776 : {
14777 215 : if (compare_tree_int (op1,
14778 215 : TYPE_PRECISION (TREE_TYPE (type0)))
14779 : >= 0)
14780 : {
14781 6 : int_const = false;
14782 6 : if (c_inhibit_evaluation_warnings == 0)
14783 6 : warning_at (location, OPT_Wshift_count_overflow,
14784 : "left shift count >= width of vector element");
14785 : }
14786 : }
14787 729918 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14788 : {
14789 29733 : int_const = false;
14790 29733 : if (c_inhibit_evaluation_warnings == 0)
14791 113 : warning_at (location, OPT_Wshift_count_overflow,
14792 : "left shift count >= width of type");
14793 : }
14794 700185 : else if (TREE_CODE (op0) == INTEGER_CST
14795 364823 : && maybe_warn_shift_overflow (location, op0, op1)
14796 700813 : && flag_isoc99)
14797 : int_const = false;
14798 : }
14799 :
14800 : /* Use the type of the value to be shifted. */
14801 : result_type = type0;
14802 : /* Avoid converting op1 to result_type later. */
14803 : converted = 1;
14804 : }
14805 : break;
14806 :
14807 1734120 : case EQ_EXPR:
14808 1734120 : case NE_EXPR:
14809 1734120 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14810 : {
14811 41887 : tree intt;
14812 41887 : if (!vector_types_compatible_elements_p (type0, type1))
14813 : {
14814 4 : error_at (location, "comparing vectors with different "
14815 : "element types");
14816 4 : return error_mark_node;
14817 : }
14818 :
14819 41883 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14820 83766 : TYPE_VECTOR_SUBPARTS (type1)))
14821 : {
14822 1 : error_at (location, "comparing vectors with different "
14823 : "number of elements");
14824 1 : return error_mark_node;
14825 : }
14826 :
14827 : /* It's not precisely specified how the usual arithmetic
14828 : conversions apply to the vector types. Here, we use
14829 : the unsigned type if one of the operands is signed and
14830 : the other one is unsigned. */
14831 41882 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14832 : {
14833 4 : if (!TYPE_UNSIGNED (type0))
14834 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14835 : else
14836 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14837 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14838 : "types %qT and %qT", type0, type1);
14839 : }
14840 :
14841 : /* Always construct signed integer vector type. */
14842 41882 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14843 : result_type = type0;
14844 : else
14845 : {
14846 41882 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14847 :
14848 125646 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14849 83764 : (SCALAR_TYPE_MODE
14850 : (TREE_TYPE (type0))), 0);
14851 41882 : if (!intt)
14852 : {
14853 0 : error_at (location, "could not find an integer type "
14854 : "of the same size as %qT",
14855 0 : TREE_TYPE (type0));
14856 0 : return error_mark_node;
14857 : }
14858 41882 : result_type = build_opaque_vector_type (intt, nelts);
14859 : }
14860 41882 : converted = 1;
14861 41882 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14862 41882 : goto return_build_binary_op;
14863 : }
14864 1692233 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14865 252752 : warning_at (location,
14866 252752 : OPT_Wfloat_equal,
14867 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14868 : /* Result of comparison is always int,
14869 : but don't convert the args to int! */
14870 1692233 : build_type = integer_type_node;
14871 1692233 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14872 156275 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14873 1571270 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14874 : || code1 == BITINT_TYPE
14875 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14876 : short_compare = 1;
14877 121229 : else if (code0 == POINTER_TYPE
14878 121229 : && (code1 == NULLPTR_TYPE
14879 120852 : || null_pointer_constant_p (orig_op1)))
14880 : {
14881 51298 : maybe_warn_for_null_address (location, op0, code);
14882 51298 : result_type = type0;
14883 : }
14884 69931 : else if (code1 == POINTER_TYPE
14885 69931 : && (code0 == NULLPTR_TYPE
14886 69829 : || null_pointer_constant_p (orig_op0)))
14887 : {
14888 288 : maybe_warn_for_null_address (location, op1, code);
14889 288 : result_type = type1;
14890 : }
14891 69643 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14892 : {
14893 69553 : tree tt0 = TREE_TYPE (type0);
14894 69553 : tree tt1 = TREE_TYPE (type1);
14895 69553 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14896 69553 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14897 69553 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14898 :
14899 : /* Anything compares with void *. void * compares with anything.
14900 : Otherwise, the targets must be compatible
14901 : and both must be object or both incomplete. */
14902 69553 : if (comp_target_types (location, type0, type1))
14903 48258 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14904 21295 : else if (!addr_space_superset (as0, as1, &as_common))
14905 : {
14906 0 : error_at (location, "comparison of pointers to "
14907 : "disjoint address spaces");
14908 0 : return error_mark_node;
14909 : }
14910 21295 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14911 : {
14912 21086 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14913 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14914 : "comparison of %<void *%> with function pointer");
14915 : }
14916 209 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14917 : {
14918 161 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14919 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14920 : "comparison of %<void *%> with function pointer");
14921 : }
14922 : else
14923 : /* Avoid warning about the volatile ObjC EH puts on decls. */
14924 48 : if (!objc_ok)
14925 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14926 : "comparison of distinct pointer types lacks a cast");
14927 :
14928 48314 : if (result_type == NULL_TREE)
14929 : {
14930 21295 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14931 21295 : result_type = c_build_pointer_type
14932 21295 : (c_build_qualified_type (void_type_node, qual));
14933 : }
14934 : }
14935 90 : else if (code0 == POINTER_TYPE
14936 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14937 : {
14938 26 : result_type = type0;
14939 26 : pedwarn (location, 0, "comparison between pointer and integer");
14940 : }
14941 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14942 21 : && code1 == POINTER_TYPE)
14943 : {
14944 8 : result_type = type1;
14945 8 : pedwarn (location, 0, "comparison between pointer and integer");
14946 : }
14947 : /* 6.5.9: One of the following shall hold:
14948 : -- both operands have type nullptr_t; */
14949 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14950 : {
14951 22 : result_type = nullptr_type_node;
14952 : /* No need to convert the operands to result_type later. */
14953 22 : converted = 1;
14954 : }
14955 : /* -- one operand has type nullptr_t and the other is a null pointer
14956 : constant. We will have to convert the former to the type of the
14957 : latter, because during gimplification we can't have mismatching
14958 : comparison operand type. We convert from nullptr_t to the other
14959 : type, since only nullptr_t can be converted to nullptr_t. Also,
14960 : even a constant 0 is a null pointer constant, so we may have to
14961 : create a pointer type from its type. */
14962 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14963 19 : result_type = (INTEGRAL_TYPE_P (type1)
14964 19 : ? c_build_pointer_type (type1) : type1);
14965 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14966 11 : result_type = (INTEGRAL_TYPE_P (type0)
14967 11 : ? c_build_pointer_type (type0) : type0);
14968 5002368 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14969 3310110 : || truth_value_p (TREE_CODE (orig_op0)))
14970 3377194 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14971 3377190 : || truth_value_p (TREE_CODE (orig_op1))))
14972 71380 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14973 : break;
14974 :
14975 996495 : case LE_EXPR:
14976 996495 : case GE_EXPR:
14977 996495 : case LT_EXPR:
14978 996495 : case GT_EXPR:
14979 996495 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14980 : {
14981 59143 : tree intt;
14982 59143 : if (!vector_types_compatible_elements_p (type0, type1))
14983 : {
14984 5 : error_at (location, "comparing vectors with different "
14985 : "element types");
14986 5 : return error_mark_node;
14987 : }
14988 :
14989 59138 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14990 118276 : TYPE_VECTOR_SUBPARTS (type1)))
14991 : {
14992 0 : error_at (location, "comparing vectors with different "
14993 : "number of elements");
14994 0 : return error_mark_node;
14995 : }
14996 :
14997 : /* It's not precisely specified how the usual arithmetic
14998 : conversions apply to the vector types. Here, we use
14999 : the unsigned type if one of the operands is signed and
15000 : the other one is unsigned. */
15001 59138 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
15002 : {
15003 15 : if (!TYPE_UNSIGNED (type0))
15004 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
15005 : else
15006 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15007 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15008 : "types %qT and %qT", type0, type1);
15009 : }
15010 :
15011 : /* Always construct signed integer vector type. */
15012 177414 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15013 118276 : (SCALAR_TYPE_MODE
15014 : (TREE_TYPE (type0))), 0);
15015 59138 : if (!intt)
15016 : {
15017 0 : error_at (location, "could not find an integer type "
15018 : "of the same size as %qT",
15019 0 : TREE_TYPE (type0));
15020 0 : return error_mark_node;
15021 : }
15022 59138 : result_type = build_opaque_vector_type (intt,
15023 59138 : TYPE_VECTOR_SUBPARTS (type0));
15024 59138 : converted = 1;
15025 59138 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15026 59138 : goto return_build_binary_op;
15027 : }
15028 937352 : build_type = integer_type_node;
15029 937352 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15030 64664 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15031 873234 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15032 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15033 : short_compare = 1;
15034 64150 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15035 : {
15036 64083 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15037 64083 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15038 64083 : addr_space_t as_common;
15039 :
15040 64083 : if (comp_target_types (location, type0, type1))
15041 : {
15042 63970 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15043 63970 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15044 63970 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15045 32 : pedwarn_c99 (location, OPT_Wpedantic,
15046 : "comparison of complete and incomplete pointers");
15047 63938 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15048 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15049 : "ordered comparisons of pointers to functions");
15050 63938 : else if (null_pointer_constant_p (orig_op0)
15051 63938 : || null_pointer_constant_p (orig_op1))
15052 8 : warning_at (location, OPT_Wextra,
15053 : "ordered comparison of pointer with null pointer");
15054 :
15055 : }
15056 113 : else if (!addr_space_superset (as0, as1, &as_common))
15057 : {
15058 0 : error_at (location, "comparison of pointers to "
15059 : "disjoint address spaces");
15060 0 : return error_mark_node;
15061 : }
15062 : else
15063 : {
15064 113 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
15065 113 : result_type = c_build_pointer_type
15066 113 : (c_build_qualified_type (void_type_node, qual));
15067 113 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15068 : "comparison of distinct pointer types lacks a cast");
15069 : }
15070 : }
15071 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15072 : {
15073 17 : result_type = type0;
15074 17 : if (pedantic)
15075 11 : pedwarn (location, OPT_Wpedantic,
15076 : "ordered comparison of pointer with integer zero");
15077 6 : else if (extra_warnings)
15078 1 : warning_at (location, OPT_Wextra,
15079 : "ordered comparison of pointer with integer zero");
15080 : }
15081 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15082 : {
15083 12 : result_type = type1;
15084 12 : if (pedantic)
15085 2 : pedwarn (location, OPT_Wpedantic,
15086 : "ordered comparison of pointer with integer zero");
15087 10 : else if (extra_warnings)
15088 1 : warning_at (location, OPT_Wextra,
15089 : "ordered comparison of pointer with integer zero");
15090 : }
15091 38 : else if (code0 == POINTER_TYPE
15092 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15093 : {
15094 18 : result_type = type0;
15095 18 : pedwarn (location, 0, "comparison between pointer and integer");
15096 : }
15097 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15098 20 : && code1 == POINTER_TYPE)
15099 : {
15100 18 : result_type = type1;
15101 18 : pedwarn (location, 0, "comparison between pointer and integer");
15102 : }
15103 :
15104 937352 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15105 64148 : && current_function_decl != NULL_TREE
15106 1001489 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15107 : {
15108 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15109 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15110 :
15111 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15112 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15113 : }
15114 :
15115 2811964 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15116 1874612 : || truth_value_p (TREE_CODE (orig_op0)))
15117 1874629 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15118 1874629 : || truth_value_p (TREE_CODE (orig_op1))))
15119 511 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15120 : break;
15121 :
15122 43 : case MIN_EXPR:
15123 43 : case MAX_EXPR:
15124 : /* Used for OpenMP atomics. */
15125 43 : gcc_assert (flag_openmp);
15126 : common = 1;
15127 : break;
15128 :
15129 0 : default:
15130 0 : gcc_unreachable ();
15131 : }
15132 :
15133 15445186 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15134 0 : return error_mark_node;
15135 :
15136 15445186 : if (gnu_vector_type_p (type0)
15137 1479761 : && gnu_vector_type_p (type1)
15138 16924118 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15139 1478912 : || !vector_types_compatible_elements_p (type0, type1)))
15140 : {
15141 26 : gcc_rich_location richloc (location);
15142 26 : maybe_range_label_for_tree_type_mismatch
15143 26 : label_for_op0 (orig_op0, orig_op1),
15144 26 : label_for_op1 (orig_op1, orig_op0);
15145 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15146 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15147 26 : binary_op_error (&richloc, code, type0, type1);
15148 26 : return error_mark_node;
15149 26 : }
15150 :
15151 15445160 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15152 1733179 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15153 1665231 : || gnu_vector_type_p (type0))
15154 16992843 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15155 1572475 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15156 1481015 : || gnu_vector_type_p (type1)))
15157 : {
15158 15257560 : bool first_complex = (code0 == COMPLEX_TYPE);
15159 15257560 : bool second_complex = (code1 == COMPLEX_TYPE);
15160 15257560 : int none_complex = (!first_complex && !second_complex);
15161 :
15162 15257560 : if (shorten || common || short_compare)
15163 : {
15164 13715589 : result_type = c_common_type (type0, type1);
15165 13715589 : do_warn_double_promotion (result_type, type0, type1,
15166 : "implicit conversion from %qT to %qT "
15167 : "to match other operand of binary "
15168 : "expression",
15169 : location);
15170 13715589 : if (result_type == error_mark_node)
15171 : return error_mark_node;
15172 : }
15173 :
15174 15257538 : if (first_complex != second_complex
15175 82043 : && (code == PLUS_EXPR
15176 : || code == MINUS_EXPR
15177 82043 : || code == MULT_EXPR
15178 17525 : || (code == TRUNC_DIV_EXPR && first_complex))
15179 65932 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15180 15319792 : && flag_signed_zeros)
15181 : {
15182 : /* An operation on mixed real/complex operands must be
15183 : handled specially, but the language-independent code can
15184 : more easily optimize the plain complex arithmetic if
15185 : -fno-signed-zeros. */
15186 61942 : tree real_type = TREE_TYPE (result_type);
15187 61942 : tree real, imag;
15188 61942 : if (type0 != orig_type0 || type1 != orig_type1)
15189 : {
15190 94 : gcc_assert (may_need_excess_precision && common);
15191 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15192 : }
15193 61942 : if (first_complex)
15194 : {
15195 8253 : if (TREE_TYPE (op0) != result_type)
15196 1787 : op0 = convert_and_check (location, result_type, op0);
15197 8253 : if (TREE_TYPE (op1) != real_type)
15198 4601 : op1 = convert_and_check (location, real_type, op1);
15199 : }
15200 : else
15201 : {
15202 53689 : if (TREE_TYPE (op0) != real_type)
15203 2698 : op0 = convert_and_check (location, real_type, op0);
15204 53689 : if (TREE_TYPE (op1) != result_type)
15205 1866 : op1 = convert_and_check (location, result_type, op1);
15206 : }
15207 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15208 0 : return error_mark_node;
15209 61942 : if (first_complex)
15210 : {
15211 8253 : op0 = save_expr (op0);
15212 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15213 : op0, true);
15214 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15215 : op0, true);
15216 8253 : switch (code)
15217 : {
15218 3488 : case MULT_EXPR:
15219 3488 : case TRUNC_DIV_EXPR:
15220 3488 : op1 = save_expr (op1);
15221 3488 : imag = build2 (resultcode, real_type, imag, op1);
15222 : /* Fall through. */
15223 8253 : case PLUS_EXPR:
15224 8253 : case MINUS_EXPR:
15225 8253 : real = build2 (resultcode, real_type, real, op1);
15226 8253 : break;
15227 0 : default:
15228 0 : gcc_unreachable();
15229 : }
15230 : }
15231 : else
15232 : {
15233 53689 : op1 = save_expr (op1);
15234 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15235 : op1, true);
15236 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15237 : op1, true);
15238 53689 : switch (code)
15239 : {
15240 2008 : case MULT_EXPR:
15241 2008 : op0 = save_expr (op0);
15242 2008 : imag = build2 (resultcode, real_type, op0, imag);
15243 : /* Fall through. */
15244 31957 : case PLUS_EXPR:
15245 31957 : real = build2 (resultcode, real_type, op0, real);
15246 31957 : break;
15247 21732 : case MINUS_EXPR:
15248 21732 : real = build2 (resultcode, real_type, op0, real);
15249 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15250 21732 : break;
15251 0 : default:
15252 0 : gcc_unreachable();
15253 : }
15254 : }
15255 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15256 61942 : goto return_build_binary_op;
15257 : }
15258 :
15259 : /* For certain operations (which identify themselves by shorten != 0)
15260 : if both args were extended from the same smaller type,
15261 : do the arithmetic in that type and then extend.
15262 :
15263 : shorten !=0 and !=1 indicates a bitwise operation.
15264 : For them, this optimization is safe only if
15265 : both args are zero-extended or both are sign-extended.
15266 : Otherwise, we might change the result.
15267 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15268 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15269 :
15270 15195596 : if (shorten && none_complex)
15271 : {
15272 1293712 : final_type = result_type;
15273 1293712 : result_type = shorten_binary_op (result_type, op0, op1,
15274 : shorten == -1);
15275 : }
15276 :
15277 : /* Shifts can be shortened if shifting right. */
15278 :
15279 15195596 : if (short_shift)
15280 : {
15281 200462 : int unsigned_arg;
15282 200462 : tree arg0 = get_narrower (op0, &unsigned_arg);
15283 :
15284 200462 : final_type = result_type;
15285 :
15286 200462 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15287 140967 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15288 :
15289 200462 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15290 2990 : && tree_int_cst_sgn (op1) > 0
15291 : /* We can shorten only if the shift count is less than the
15292 : number of bits in the smaller type size. */
15293 2990 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15294 : /* We cannot drop an unsigned shift after sign-extension. */
15295 203312 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15296 : {
15297 : /* Do an unsigned shift if the operand was zero-extended. */
15298 2835 : result_type
15299 2835 : = c_common_signed_or_unsigned_type (unsigned_arg,
15300 2835 : TREE_TYPE (arg0));
15301 : /* Convert value-to-be-shifted to that type. */
15302 2835 : if (TREE_TYPE (op0) != result_type)
15303 2835 : op0 = convert (result_type, op0);
15304 : converted = 1;
15305 : }
15306 : }
15307 :
15308 : /* Comparison operations are shortened too but differently.
15309 : They identify themselves by setting short_compare = 1. */
15310 :
15311 15195596 : if (short_compare)
15312 : {
15313 : /* Don't write &op0, etc., because that would prevent op0
15314 : from being kept in a register.
15315 : Instead, make copies of the our local variables and
15316 : pass the copies by reference, then copy them back afterward. */
15317 2444204 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15318 2444204 : enum tree_code xresultcode = resultcode;
15319 2444204 : tree val
15320 2444204 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15321 : &xresultcode);
15322 :
15323 2444204 : if (val != NULL_TREE)
15324 : {
15325 15787 : ret = val;
15326 15787 : goto return_build_binary_op;
15327 : }
15328 :
15329 2428417 : op0 = xop0, op1 = xop1;
15330 2428417 : converted = 1;
15331 2428417 : resultcode = xresultcode;
15332 :
15333 2428417 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15334 : {
15335 2280745 : bool op0_maybe_const = true;
15336 2280745 : bool op1_maybe_const = true;
15337 2280745 : tree orig_op0_folded, orig_op1_folded;
15338 :
15339 2280745 : if (in_late_binary_op)
15340 : {
15341 : orig_op0_folded = orig_op0;
15342 : orig_op1_folded = orig_op1;
15343 : }
15344 : else
15345 : {
15346 : /* Fold for the sake of possible warnings, as in
15347 : build_conditional_expr. This requires the
15348 : "original" values to be folded, not just op0 and
15349 : op1. */
15350 2272367 : c_inhibit_evaluation_warnings++;
15351 2272367 : op0 = c_fully_fold (op0, require_constant_value,
15352 : &op0_maybe_const);
15353 2272367 : op1 = c_fully_fold (op1, require_constant_value,
15354 : &op1_maybe_const);
15355 2272367 : c_inhibit_evaluation_warnings--;
15356 2272367 : orig_op0_folded = c_fully_fold (orig_op0,
15357 : require_constant_value,
15358 : NULL);
15359 2272367 : orig_op1_folded = c_fully_fold (orig_op1,
15360 : require_constant_value,
15361 : NULL);
15362 : }
15363 :
15364 2280745 : if (warn_sign_compare)
15365 373548 : warn_for_sign_compare (location, orig_op0_folded,
15366 : orig_op1_folded, op0, op1,
15367 : result_type, resultcode);
15368 2280745 : if (!in_late_binary_op && !int_operands)
15369 : {
15370 2096164 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15371 2092642 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15372 2096164 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15373 825206 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15374 : }
15375 : }
15376 : }
15377 : }
15378 :
15379 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15380 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15381 : Then the expression will be built.
15382 : It will be given type FINAL_TYPE if that is nonzero;
15383 : otherwise, it will be given type RESULT_TYPE. */
15384 :
15385 15367409 : if (!result_type)
15386 : {
15387 : /* Favor showing any expression locations that are available. */
15388 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15389 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15390 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15391 514 : return error_mark_node;
15392 514 : }
15393 :
15394 15366895 : if (build_type == NULL_TREE)
15395 : {
15396 12753105 : build_type = result_type;
15397 12753105 : if ((type0 != orig_type0 || type1 != orig_type1)
15398 797 : && !boolean_op)
15399 : {
15400 797 : gcc_assert (may_need_excess_precision && common);
15401 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15402 : }
15403 : }
15404 :
15405 15366895 : if (!converted)
15406 : {
15407 11393545 : op0 = ep_convert_and_check (location, result_type, op0,
15408 : semantic_result_type);
15409 11393545 : op1 = ep_convert_and_check (location, result_type, op1,
15410 : semantic_result_type);
15411 :
15412 : /* This can happen if one operand has a vector type, and the other
15413 : has a different type. */
15414 11393545 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15415 3 : return error_mark_node;
15416 : }
15417 :
15418 15366892 : if (sanitize_flags_p ((SANITIZE_SHIFT
15419 : | SANITIZE_DIVIDE
15420 : | SANITIZE_FLOAT_DIVIDE
15421 : | SANITIZE_SI_OVERFLOW))
15422 23309 : && current_function_decl != NULL_TREE
15423 19846 : && (doing_div_or_mod || doing_shift)
15424 15369817 : && !require_constant_value)
15425 : {
15426 : /* OP0 and/or OP1 might have side-effects. */
15427 2869 : op0 = save_expr (op0);
15428 2869 : op1 = save_expr (op1);
15429 2869 : op0 = c_fully_fold (op0, false, NULL);
15430 2869 : op1 = c_fully_fold (op1, false, NULL);
15431 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15432 : | SANITIZE_FLOAT_DIVIDE
15433 : | SANITIZE_SI_OVERFLOW))))
15434 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15435 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15436 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15437 : }
15438 :
15439 : /* Treat expressions in initializers specially as they can't trap. */
15440 15366892 : if (int_const_or_overflow)
15441 7919572 : ret = (require_constant_value
15442 7919572 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15443 : op0, op1)
15444 7873450 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15445 : else
15446 7447320 : ret = build2 (resultcode, build_type, op0, op1);
15447 15366892 : if (final_type != NULL_TREE)
15448 1494174 : ret = convert (final_type, ret);
15449 :
15450 13872718 : return_build_binary_op:
15451 16816739 : gcc_assert (ret != error_mark_node);
15452 16816739 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15453 1750 : ret = (int_operands
15454 1750 : ? note_integer_operands (ret)
15455 430 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15456 16814989 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15457 60300 : && !in_late_binary_op)
15458 60300 : ret = note_integer_operands (ret);
15459 16816739 : protected_set_expr_location (ret, location);
15460 :
15461 16816739 : if (instrument_expr != NULL)
15462 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15463 : instrument_expr, ret);
15464 :
15465 16816739 : if (semantic_result_type)
15466 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15467 : semantic_result_type, ret);
15468 :
15469 : return ret;
15470 : }
15471 :
15472 :
15473 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15474 : purpose. LOCATION is the source location for the expression. */
15475 :
15476 : tree
15477 4117488 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15478 : {
15479 4117488 : bool int_const, int_operands;
15480 :
15481 4117488 : switch (TREE_CODE (TREE_TYPE (expr)))
15482 : {
15483 72 : case ARRAY_TYPE:
15484 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15485 72 : return error_mark_node;
15486 :
15487 86 : case RECORD_TYPE:
15488 86 : error_at (location, "used struct type value where scalar is required");
15489 86 : return error_mark_node;
15490 :
15491 83 : case UNION_TYPE:
15492 83 : error_at (location, "used union type value where scalar is required");
15493 83 : return error_mark_node;
15494 :
15495 22 : case VOID_TYPE:
15496 22 : error_at (location, "void value not ignored as it ought to be");
15497 22 : return error_mark_node;
15498 :
15499 30518 : case POINTER_TYPE:
15500 30518 : if (reject_gcc_builtin (expr))
15501 3 : return error_mark_node;
15502 : break;
15503 :
15504 0 : case FUNCTION_TYPE:
15505 0 : gcc_unreachable ();
15506 :
15507 8 : case VECTOR_TYPE:
15508 8 : error_at (location, "used vector type where scalar is required");
15509 8 : return error_mark_node;
15510 :
15511 : default:
15512 : break;
15513 : }
15514 :
15515 : /* Conversion of a floating constant to boolean goes through here
15516 : and yields an integer constant expression. Otherwise, the result
15517 : is only an integer constant expression if the argument is. */
15518 894761 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15519 4117290 : || ((TREE_CODE (expr) == REAL_CST
15520 3222155 : || TREE_CODE (expr) == COMPLEX_CST)
15521 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15522 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15523 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15524 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15525 : == BOOLEAN_TYPE)))));
15526 4117214 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15527 894921 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15528 : {
15529 319 : expr = remove_c_maybe_const_expr (expr);
15530 319 : expr = build2 (NE_EXPR, type, expr,
15531 319 : convert (TREE_TYPE (expr), integer_zero_node));
15532 319 : expr = note_integer_operands (expr);
15533 : }
15534 : else
15535 : {
15536 : /* ??? Should we also give an error for vectors rather than leaving
15537 : those to give errors later? */
15538 4116895 : expr = c_common_truthvalue_conversion (location, expr);
15539 4116895 : expr = fold_convert_loc (location, type, expr);
15540 : }
15541 :
15542 4117214 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15543 : {
15544 76 : if (TREE_OVERFLOW (expr))
15545 : return expr;
15546 : else
15547 76 : return note_integer_operands (expr);
15548 : }
15549 4117138 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15550 754 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15551 : return expr;
15552 : }
15553 :
15554 :
15555 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15556 : required. */
15557 :
15558 : tree
15559 119320757 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15560 : {
15561 119320757 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15562 : {
15563 699 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15564 : /* Executing a compound literal inside a function reinitializes
15565 : it. */
15566 699 : if (!TREE_STATIC (decl))
15567 433 : *se = true;
15568 699 : return decl;
15569 : }
15570 : else
15571 : return expr;
15572 : }
15573 :
15574 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15575 : statement. LOC is the location of the construct. */
15576 :
15577 : tree
15578 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15579 : tree clauses)
15580 : {
15581 2154 : body = c_end_compound_stmt (loc, body, true);
15582 :
15583 2154 : tree stmt = make_node (code);
15584 2154 : TREE_TYPE (stmt) = void_type_node;
15585 2154 : OMP_BODY (stmt) = body;
15586 2154 : OMP_CLAUSES (stmt) = clauses;
15587 2154 : SET_EXPR_LOCATION (stmt, loc);
15588 :
15589 2154 : return add_stmt (stmt);
15590 : }
15591 :
15592 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15593 : statement. LOC is the location of the OACC_DATA. */
15594 :
15595 : tree
15596 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15597 : {
15598 497 : tree stmt;
15599 :
15600 497 : block = c_end_compound_stmt (loc, block, true);
15601 :
15602 497 : stmt = make_node (OACC_DATA);
15603 497 : TREE_TYPE (stmt) = void_type_node;
15604 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15605 497 : OACC_DATA_BODY (stmt) = block;
15606 497 : SET_EXPR_LOCATION (stmt, loc);
15607 :
15608 497 : return add_stmt (stmt);
15609 : }
15610 :
15611 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15612 : statement. LOC is the location of the OACC_HOST_DATA. */
15613 :
15614 : tree
15615 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15616 : {
15617 21 : tree stmt;
15618 :
15619 21 : block = c_end_compound_stmt (loc, block, true);
15620 :
15621 21 : stmt = make_node (OACC_HOST_DATA);
15622 21 : TREE_TYPE (stmt) = void_type_node;
15623 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15624 21 : OACC_HOST_DATA_BODY (stmt) = block;
15625 21 : SET_EXPR_LOCATION (stmt, loc);
15626 :
15627 21 : return add_stmt (stmt);
15628 : }
15629 :
15630 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15631 :
15632 : tree
15633 12003 : c_begin_omp_parallel (void)
15634 : {
15635 12003 : tree block;
15636 :
15637 12003 : keep_next_level ();
15638 12003 : block = c_begin_compound_stmt (true);
15639 :
15640 12003 : return block;
15641 : }
15642 :
15643 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15644 : statement. LOC is the location of the OMP_PARALLEL. */
15645 :
15646 : tree
15647 5677 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15648 : {
15649 5677 : tree stmt;
15650 :
15651 5677 : block = c_end_compound_stmt (loc, block, true);
15652 :
15653 5677 : stmt = make_node (OMP_PARALLEL);
15654 5677 : TREE_TYPE (stmt) = void_type_node;
15655 5677 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15656 5677 : OMP_PARALLEL_BODY (stmt) = block;
15657 5677 : SET_EXPR_LOCATION (stmt, loc);
15658 :
15659 5677 : return add_stmt (stmt);
15660 : }
15661 :
15662 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15663 :
15664 : tree
15665 898 : c_begin_omp_task (void)
15666 : {
15667 898 : tree block;
15668 :
15669 898 : keep_next_level ();
15670 898 : block = c_begin_compound_stmt (true);
15671 :
15672 898 : return block;
15673 : }
15674 :
15675 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15676 : statement. LOC is the location of the #pragma. */
15677 :
15678 : tree
15679 898 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15680 : {
15681 898 : tree stmt;
15682 :
15683 898 : block = c_end_compound_stmt (loc, block, true);
15684 :
15685 898 : stmt = make_node (OMP_TASK);
15686 898 : TREE_TYPE (stmt) = void_type_node;
15687 898 : OMP_TASK_CLAUSES (stmt) = clauses;
15688 898 : OMP_TASK_BODY (stmt) = block;
15689 898 : SET_EXPR_LOCATION (stmt, loc);
15690 :
15691 898 : return add_stmt (stmt);
15692 : }
15693 :
15694 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15695 :
15696 : void
15697 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15698 : {
15699 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15700 213 : int mask = 0;
15701 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15702 : mask = 1;
15703 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15704 : mask = 2;
15705 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15706 : mask = 4;
15707 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15708 : mask = 8;
15709 : else
15710 : {
15711 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15712 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15713 : "clauses");
15714 0 : return;
15715 : }
15716 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15717 213 : if (ifc != NULL_TREE)
15718 : {
15719 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15720 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15721 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15722 : "expected %<cancel%> %<if%> clause modifier");
15723 : else
15724 : {
15725 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15726 29 : if (ifc2 != NULL_TREE)
15727 : {
15728 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15729 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15730 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15731 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15732 : "expected %<cancel%> %<if%> clause modifier");
15733 : }
15734 : }
15735 :
15736 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15737 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15738 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15739 : build_zero_cst (type));
15740 : }
15741 : else
15742 182 : ifc = boolean_true_node;
15743 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15744 213 : build_int_cst (integer_type_node, mask),
15745 : ifc);
15746 213 : add_stmt (stmt);
15747 : }
15748 :
15749 : /* Generate GOMP_cancellation_point call for
15750 : #pragma omp cancellation point. */
15751 :
15752 : void
15753 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15754 : {
15755 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15756 167 : int mask = 0;
15757 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15758 : mask = 1;
15759 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15760 : mask = 2;
15761 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15762 : mask = 4;
15763 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15764 : mask = 8;
15765 : else
15766 : {
15767 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15768 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15769 : "clauses");
15770 1 : return;
15771 : }
15772 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15773 166 : build_int_cst (integer_type_node, mask));
15774 166 : add_stmt (stmt);
15775 : }
15776 :
15777 : /* Helper function for handle_omp_array_sections. Called recursively
15778 : to handle multiple array-section-subscripts. C is the clause,
15779 : T current expression (initially OMP_CLAUSE_DECL), which is either
15780 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15781 : expression if specified, TREE_VALUE length expression if specified,
15782 : TREE_CHAIN is what it has been specified after, or some decl.
15783 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15784 : set to true if any of the array-section-subscript could have length
15785 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15786 : first array-section-subscript which is known not to have length
15787 : of one. Given say:
15788 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15789 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15790 : all are or may have length of 1, array-section-subscript [:2] is the
15791 : first one known not to have length 1. For array-section-subscript
15792 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15793 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15794 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15795 : case though, as some lengths could be zero. */
15796 :
15797 : static tree
15798 6396 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15799 : bool &maybe_zero_len, unsigned int &first_non_one,
15800 : enum c_omp_region_type ort)
15801 : {
15802 6396 : tree ret, low_bound, length, type;
15803 6396 : bool openacc = (ort & C_ORT_ACC) != 0;
15804 6396 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15805 : {
15806 2979 : if (error_operand_p (t))
15807 2 : return error_mark_node;
15808 2977 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15809 2977 : ret = t;
15810 2977 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15811 2878 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15812 5541 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15813 : {
15814 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15815 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15816 6 : return error_mark_node;
15817 : }
15818 2971 : if (!ai.check_clause (c))
15819 0 : return error_mark_node;
15820 2971 : else if (ai.component_access_p ()
15821 3272 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15822 15 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15823 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15824 301 : t = ai.get_root_term (true);
15825 : else
15826 2670 : t = ai.unconverted_ref_origin ();
15827 2971 : if (t == error_mark_node)
15828 : return error_mark_node;
15829 2971 : if (!VAR_P (t)
15830 742 : && (ort == C_ORT_ACC || !EXPR_P (t))
15831 739 : && TREE_CODE (t) != PARM_DECL)
15832 : {
15833 5 : if (DECL_P (t))
15834 5 : error_at (OMP_CLAUSE_LOCATION (c),
15835 : "%qD is not a variable in %qs clause", t,
15836 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15837 : else
15838 0 : error_at (OMP_CLAUSE_LOCATION (c),
15839 : "%qE is not a variable in %qs clause", t,
15840 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15841 5 : return error_mark_node;
15842 : }
15843 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15844 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15845 5521 : && TYPE_ATOMIC (TREE_TYPE (t)))
15846 : {
15847 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15848 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15849 0 : return error_mark_node;
15850 : }
15851 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15852 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15853 2555 : && VAR_P (t)
15854 4972 : && DECL_THREAD_LOCAL_P (t))
15855 : {
15856 3 : error_at (OMP_CLAUSE_LOCATION (c),
15857 : "%qD is threadprivate variable in %qs clause", t,
15858 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15859 3 : return error_mark_node;
15860 : }
15861 2963 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15862 2865 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15863 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15864 2965 : && POINTER_TYPE_P (TREE_TYPE (t)))
15865 : {
15866 : /* If the array section is pointer based and the pointer
15867 : itself is _Atomic qualified, we need to atomically load
15868 : the pointer. */
15869 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15870 : ret, false, false);
15871 : }
15872 2963 : return ret;
15873 2977 : }
15874 :
15875 3417 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15876 : maybe_zero_len, first_non_one, ort);
15877 3417 : if (ret == error_mark_node || ret == NULL_TREE)
15878 : return ret;
15879 :
15880 3352 : type = TREE_TYPE (ret);
15881 3352 : low_bound = TREE_OPERAND (t, 1);
15882 3352 : length = TREE_OPERAND (t, 2);
15883 :
15884 3352 : if (low_bound == error_mark_node || length == error_mark_node)
15885 : return error_mark_node;
15886 :
15887 3352 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15888 : {
15889 13 : error_at (OMP_CLAUSE_LOCATION (c),
15890 : "low bound %qE of array section does not have integral type",
15891 : low_bound);
15892 13 : return error_mark_node;
15893 : }
15894 3339 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15895 : {
15896 12 : error_at (OMP_CLAUSE_LOCATION (c),
15897 : "length %qE of array section does not have integral type",
15898 : length);
15899 12 : return error_mark_node;
15900 : }
15901 3327 : if (low_bound
15902 2743 : && TREE_CODE (low_bound) == INTEGER_CST
15903 5781 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15904 2454 : > TYPE_PRECISION (sizetype))
15905 0 : low_bound = fold_convert (sizetype, low_bound);
15906 3327 : if (length
15907 3139 : && TREE_CODE (length) == INTEGER_CST
15908 5521 : && TYPE_PRECISION (TREE_TYPE (length))
15909 2194 : > TYPE_PRECISION (sizetype))
15910 0 : length = fold_convert (sizetype, length);
15911 3327 : if (low_bound == NULL_TREE)
15912 584 : low_bound = integer_zero_node;
15913 3327 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15914 3327 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15915 1937 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15916 : {
15917 10 : if (length != integer_one_node)
15918 : {
15919 6 : error_at (OMP_CLAUSE_LOCATION (c),
15920 : "expected single pointer in %qs clause",
15921 : user_omp_clause_code_name (c, openacc));
15922 6 : return error_mark_node;
15923 : }
15924 : }
15925 3321 : if (length != NULL_TREE)
15926 : {
15927 3137 : if (!integer_nonzerop (length))
15928 : {
15929 974 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15930 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15931 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15932 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15933 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15934 : {
15935 180 : if (integer_zerop (length))
15936 : {
15937 12 : error_at (OMP_CLAUSE_LOCATION (c),
15938 : "zero length array section in %qs clause",
15939 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15940 12 : return error_mark_node;
15941 : }
15942 : }
15943 : else
15944 794 : maybe_zero_len = true;
15945 : }
15946 3125 : if (first_non_one == types.length ()
15947 3125 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15948 1598 : first_non_one++;
15949 : }
15950 3309 : if (TREE_CODE (type) == ARRAY_TYPE)
15951 : {
15952 1496 : if (length == NULL_TREE
15953 1496 : && (TYPE_DOMAIN (type) == NULL_TREE
15954 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15955 : {
15956 8 : error_at (OMP_CLAUSE_LOCATION (c),
15957 : "for unknown bound array type length expression must "
15958 : "be specified");
15959 8 : return error_mark_node;
15960 : }
15961 1488 : if (TREE_CODE (low_bound) == INTEGER_CST
15962 1488 : && tree_int_cst_sgn (low_bound) == -1)
15963 : {
15964 35 : error_at (OMP_CLAUSE_LOCATION (c),
15965 : "negative low bound in array section in %qs clause",
15966 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15967 35 : return error_mark_node;
15968 : }
15969 1453 : if (length != NULL_TREE
15970 1297 : && TREE_CODE (length) == INTEGER_CST
15971 2310 : && tree_int_cst_sgn (length) == -1)
15972 : {
15973 35 : error_at (OMP_CLAUSE_LOCATION (c),
15974 : "negative length in array section in %qs clause",
15975 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15976 35 : return error_mark_node;
15977 : }
15978 1418 : if (TYPE_DOMAIN (type)
15979 1407 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15980 2825 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15981 : == INTEGER_CST)
15982 : {
15983 1061 : tree size
15984 1061 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15985 1061 : size = size_binop (PLUS_EXPR, size, size_one_node);
15986 1061 : if (TREE_CODE (low_bound) == INTEGER_CST)
15987 : {
15988 874 : if (tree_int_cst_lt (size, low_bound))
15989 : {
15990 10 : error_at (OMP_CLAUSE_LOCATION (c),
15991 : "low bound %qE above array section size "
15992 : "in %qs clause", low_bound,
15993 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15994 10 : return error_mark_node;
15995 : }
15996 864 : if (tree_int_cst_equal (size, low_bound))
15997 : {
15998 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15999 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16000 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16001 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16002 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16003 : {
16004 5 : error_at (OMP_CLAUSE_LOCATION (c),
16005 : "zero length array section in %qs clause",
16006 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16007 5 : return error_mark_node;
16008 : }
16009 0 : maybe_zero_len = true;
16010 : }
16011 859 : else if (length == NULL_TREE
16012 256 : && first_non_one == types.length ()
16013 914 : && tree_int_cst_equal
16014 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16015 : low_bound))
16016 30 : first_non_one++;
16017 : }
16018 187 : else if (length == NULL_TREE)
16019 : {
16020 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16021 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16022 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16023 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16024 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16025 0 : maybe_zero_len = true;
16026 6 : if (first_non_one == types.length ())
16027 1 : first_non_one++;
16028 : }
16029 1046 : if (length && TREE_CODE (length) == INTEGER_CST)
16030 : {
16031 799 : if (tree_int_cst_lt (size, length))
16032 : {
16033 11 : error_at (OMP_CLAUSE_LOCATION (c),
16034 : "length %qE above array section size "
16035 : "in %qs clause", length,
16036 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16037 11 : return error_mark_node;
16038 : }
16039 788 : if (TREE_CODE (low_bound) == INTEGER_CST)
16040 : {
16041 688 : tree lbpluslen
16042 688 : = size_binop (PLUS_EXPR,
16043 : fold_convert (sizetype, low_bound),
16044 : fold_convert (sizetype, length));
16045 688 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16046 688 : && tree_int_cst_lt (size, lbpluslen))
16047 : {
16048 10 : error_at (OMP_CLAUSE_LOCATION (c),
16049 : "high bound %qE above array section size "
16050 : "in %qs clause", lbpluslen,
16051 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16052 10 : return error_mark_node;
16053 : }
16054 : }
16055 : }
16056 : }
16057 357 : else if (length == NULL_TREE)
16058 : {
16059 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16060 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16061 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16062 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16063 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16064 0 : maybe_zero_len = true;
16065 20 : if (first_non_one == types.length ())
16066 10 : first_non_one++;
16067 : }
16068 :
16069 : /* For [lb:] we will need to evaluate lb more than once. */
16070 929 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16071 : {
16072 127 : tree lb = save_expr (low_bound);
16073 127 : if (lb != low_bound)
16074 : {
16075 2 : TREE_OPERAND (t, 1) = lb;
16076 2 : low_bound = lb;
16077 : }
16078 : }
16079 : }
16080 1813 : else if (TREE_CODE (type) == POINTER_TYPE)
16081 : {
16082 1802 : if (length == NULL_TREE)
16083 : {
16084 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16085 8 : error_at (OMP_CLAUSE_LOCATION (c),
16086 : "for array function parameter length expression "
16087 : "must be specified");
16088 : else
16089 2 : error_at (OMP_CLAUSE_LOCATION (c),
16090 : "for pointer type length expression must be specified");
16091 10 : return error_mark_node;
16092 : }
16093 1792 : if (length != NULL_TREE
16094 1792 : && TREE_CODE (length) == INTEGER_CST
16095 1287 : && tree_int_cst_sgn (length) == -1)
16096 : {
16097 20 : error_at (OMP_CLAUSE_LOCATION (c),
16098 : "negative length in array section in %qs clause",
16099 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16100 20 : return error_mark_node;
16101 : }
16102 : /* If there is a pointer type anywhere but in the very first
16103 : array-section-subscript, the array section could be non-contiguous. */
16104 1772 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16105 1611 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16106 3354 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16107 : {
16108 : /* If any prior dimension has a non-one length, then deem this
16109 : array section as non-contiguous. */
16110 42 : for (tree d = TREE_OPERAND (t, 0);
16111 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16112 40 : d = TREE_OPERAND (d, 0))
16113 : {
16114 44 : tree d_length = TREE_OPERAND (d, 2);
16115 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16116 : {
16117 4 : error_at (OMP_CLAUSE_LOCATION (c),
16118 : "array section is not contiguous in %qs clause",
16119 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16120 4 : return error_mark_node;
16121 : }
16122 : }
16123 : }
16124 : }
16125 : else
16126 : {
16127 11 : error_at (OMP_CLAUSE_LOCATION (c),
16128 : "%qE does not have pointer or array type", ret);
16129 11 : return error_mark_node;
16130 : }
16131 3150 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16132 2834 : types.safe_push (TREE_TYPE (ret));
16133 : /* We will need to evaluate lb more than once. */
16134 3150 : tree lb = save_expr (low_bound);
16135 3150 : if (lb != low_bound)
16136 : {
16137 273 : TREE_OPERAND (t, 1) = lb;
16138 273 : low_bound = lb;
16139 : }
16140 3150 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16141 3150 : return ret;
16142 : }
16143 :
16144 : /* Handle array sections for clause C. */
16145 :
16146 : static bool
16147 2979 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16148 : {
16149 2979 : bool maybe_zero_len = false;
16150 2979 : unsigned int first_non_one = 0;
16151 2979 : auto_vec<tree, 10> types;
16152 2979 : tree *tp = &OMP_CLAUSE_DECL (c);
16153 2979 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16154 2665 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16155 3078 : && OMP_ITERATOR_DECL_P (*tp))
16156 66 : tp = &TREE_VALUE (*tp);
16157 2979 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16158 : maybe_zero_len, first_non_one,
16159 : ort);
16160 2979 : if (first == error_mark_node)
16161 : return true;
16162 2761 : if (first == NULL_TREE)
16163 : return false;
16164 2761 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16165 2761 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16166 : {
16167 336 : tree t = *tp;
16168 336 : tree tem = NULL_TREE;
16169 : /* Need to evaluate side effects in the length expressions
16170 : if any. */
16171 336 : while (TREE_CODE (t) == TREE_LIST)
16172 : {
16173 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16174 : {
16175 0 : if (tem == NULL_TREE)
16176 : tem = TREE_VALUE (t);
16177 : else
16178 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16179 0 : TREE_VALUE (t), tem);
16180 : }
16181 0 : t = TREE_CHAIN (t);
16182 : }
16183 336 : if (tem)
16184 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16185 336 : first = c_fully_fold (first, false, NULL, true);
16186 336 : *tp = first;
16187 : }
16188 : else
16189 : {
16190 2425 : unsigned int num = types.length (), i;
16191 2425 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16192 2425 : tree condition = NULL_TREE;
16193 :
16194 2425 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16195 3 : maybe_zero_len = true;
16196 :
16197 5050 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16198 2625 : t = TREE_OPERAND (t, 0))
16199 : {
16200 2640 : tree low_bound = TREE_OPERAND (t, 1);
16201 2640 : tree length = TREE_OPERAND (t, 2);
16202 :
16203 2640 : i--;
16204 2640 : if (low_bound
16205 2176 : && TREE_CODE (low_bound) == INTEGER_CST
16206 4630 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16207 1990 : > TYPE_PRECISION (sizetype))
16208 0 : low_bound = fold_convert (sizetype, low_bound);
16209 2640 : if (length
16210 2548 : && TREE_CODE (length) == INTEGER_CST
16211 4261 : && TYPE_PRECISION (TREE_TYPE (length))
16212 1621 : > TYPE_PRECISION (sizetype))
16213 0 : length = fold_convert (sizetype, length);
16214 2640 : if (low_bound == NULL_TREE)
16215 464 : low_bound = integer_zero_node;
16216 2640 : if (!maybe_zero_len && i > first_non_one)
16217 : {
16218 109 : if (integer_nonzerop (low_bound))
16219 6 : goto do_warn_noncontiguous;
16220 103 : if (length != NULL_TREE
16221 50 : && TREE_CODE (length) == INTEGER_CST
16222 50 : && TYPE_DOMAIN (types[i])
16223 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16224 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16225 : == INTEGER_CST)
16226 : {
16227 50 : tree size;
16228 50 : size = size_binop (PLUS_EXPR,
16229 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16230 : size_one_node);
16231 50 : if (!tree_int_cst_equal (length, size))
16232 : {
16233 7 : do_warn_noncontiguous:
16234 26 : error_at (OMP_CLAUSE_LOCATION (c),
16235 : "array section is not contiguous in %qs "
16236 : "clause",
16237 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16238 2425 : return true;
16239 : }
16240 : }
16241 96 : if (length != NULL_TREE
16242 96 : && TREE_SIDE_EFFECTS (length))
16243 : {
16244 0 : if (side_effects == NULL_TREE)
16245 : side_effects = length;
16246 : else
16247 0 : side_effects = build2 (COMPOUND_EXPR,
16248 0 : TREE_TYPE (side_effects),
16249 : length, side_effects);
16250 : }
16251 : }
16252 : else
16253 : {
16254 2531 : tree l;
16255 :
16256 2531 : if (i > first_non_one
16257 2531 : && ((length && integer_nonzerop (length))
16258 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16259 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16260 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16261 0 : continue;
16262 2531 : if (length)
16263 2498 : l = fold_convert (sizetype, length);
16264 : else
16265 : {
16266 33 : l = size_binop (PLUS_EXPR,
16267 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16268 : size_one_node);
16269 33 : l = size_binop (MINUS_EXPR, l,
16270 : fold_convert (sizetype, low_bound));
16271 : }
16272 2531 : if (i > first_non_one)
16273 : {
16274 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16275 : size_zero_node);
16276 0 : if (condition == NULL_TREE)
16277 : condition = l;
16278 : else
16279 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16280 : l, condition);
16281 : }
16282 2531 : else if (size == NULL_TREE)
16283 : {
16284 2412 : size = size_in_bytes (TREE_TYPE (types[i]));
16285 2412 : tree eltype = TREE_TYPE (types[num - 1]);
16286 2432 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16287 20 : eltype = TREE_TYPE (eltype);
16288 2412 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16289 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16290 4329 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16291 : {
16292 539 : if (integer_zerop (size)
16293 1076 : || integer_zerop (size_in_bytes (eltype)))
16294 : {
16295 4 : error_at (OMP_CLAUSE_LOCATION (c),
16296 : "zero length array section in %qs clause",
16297 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16298 2 : return error_mark_node;
16299 : }
16300 537 : size = size_binop (EXACT_DIV_EXPR, size,
16301 : size_in_bytes (eltype));
16302 : }
16303 2410 : size = size_binop (MULT_EXPR, size, l);
16304 2410 : if (condition)
16305 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16306 : size, size_zero_node);
16307 : }
16308 : else
16309 119 : size = size_binop (MULT_EXPR, size, l);
16310 : }
16311 : }
16312 2410 : if (side_effects)
16313 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16314 2410 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16315 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16316 4327 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16317 : {
16318 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16319 537 : size = c_fully_fold (size, false, NULL);
16320 537 : size = save_expr (size);
16321 537 : tree index_type = build_index_type (size);
16322 537 : tree eltype = TREE_TYPE (first);
16323 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16324 12 : eltype = TREE_TYPE (eltype);
16325 537 : tree type = c_build_array_type (eltype, index_type);
16326 537 : tree ptype = c_build_pointer_type (eltype);
16327 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16328 296 : t = build_fold_addr_expr (t);
16329 537 : tree t2 = build_fold_addr_expr (first);
16330 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16331 : ptrdiff_type_node, t2);
16332 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16333 : ptrdiff_type_node, t2,
16334 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16335 : ptrdiff_type_node, t));
16336 537 : t2 = c_fully_fold (t2, false, NULL);
16337 537 : if (tree_fits_shwi_p (t2))
16338 398 : t = build2 (MEM_REF, type, t,
16339 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16340 : else
16341 : {
16342 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16343 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16344 139 : TREE_TYPE (t), t, t2);
16345 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16346 : }
16347 537 : OMP_CLAUSE_DECL (c) = t;
16348 537 : return false;
16349 : }
16350 1873 : first = c_fully_fold (first, false, NULL);
16351 1873 : OMP_CLAUSE_DECL (c) = first;
16352 1873 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16353 : return false;
16354 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16355 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16356 1862 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16357 1722 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16358 1720 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16359 : {
16360 1858 : if (size)
16361 1858 : size = c_fully_fold (size, false, NULL);
16362 1858 : OMP_CLAUSE_SIZE (c) = size;
16363 : }
16364 :
16365 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16366 : return false;
16367 :
16368 1724 : auto_vec<omp_addr_token *, 10> addr_tokens;
16369 :
16370 1724 : if (!omp_parse_expr (addr_tokens, first))
16371 1724 : return true;
16372 :
16373 1724 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16374 :
16375 1724 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16376 1724 : if (nc != error_mark_node)
16377 : {
16378 1724 : using namespace omp_addr_tokenizer;
16379 :
16380 1724 : if (ai.maybe_zero_length_array_section (c))
16381 1720 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16382 :
16383 : /* !!! If we're accessing a base decl via chained access
16384 : methods (e.g. multiple indirections), duplicate clause
16385 : detection won't work properly. Skip it in that case. */
16386 1724 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16387 1437 : || addr_tokens[0]->type == ARRAY_BASE)
16388 1724 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16389 1724 : && addr_tokens[1]->type == ACCESS_METHOD
16390 3448 : && omp_access_chain_p (addr_tokens, 1))
16391 34 : c = nc;
16392 :
16393 1724 : return false;
16394 : }
16395 : }
16396 : return false;
16397 2979 : }
16398 :
16399 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16400 : an inline call. But, remap
16401 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16402 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16403 :
16404 : static tree
16405 510 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16406 : tree decl, tree placeholder)
16407 : {
16408 510 : copy_body_data id;
16409 510 : hash_map<tree, tree> decl_map;
16410 :
16411 510 : decl_map.put (omp_decl1, placeholder);
16412 510 : decl_map.put (omp_decl2, decl);
16413 510 : memset (&id, 0, sizeof (id));
16414 510 : id.src_fn = DECL_CONTEXT (omp_decl1);
16415 510 : id.dst_fn = current_function_decl;
16416 510 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16417 510 : id.decl_map = &decl_map;
16418 :
16419 510 : id.copy_decl = copy_decl_no_change;
16420 510 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16421 510 : id.transform_new_cfg = true;
16422 510 : id.transform_return_to_modify = false;
16423 510 : id.eh_lp_nr = 0;
16424 510 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16425 510 : return stmt;
16426 510 : }
16427 :
16428 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16429 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16430 :
16431 : static tree
16432 1123 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16433 : {
16434 1123 : if (*tp == (tree) data)
16435 28 : return *tp;
16436 : return NULL_TREE;
16437 : }
16438 :
16439 : /* Similarly, but also walk aggregate fields. */
16440 :
16441 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16442 :
16443 : static tree
16444 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16445 : {
16446 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16447 : return *tp;
16448 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16449 : {
16450 2 : tree field;
16451 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16452 :
16453 2 : for (field = TYPE_FIELDS (*tp); field;
16454 0 : field = DECL_CHAIN (field))
16455 2 : if (TREE_CODE (field) == FIELD_DECL)
16456 : {
16457 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16458 : c_find_omp_var_r, data, pset);
16459 2 : if (ret)
16460 : return ret;
16461 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16462 2 : if (ret)
16463 : return ret;
16464 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16465 : pset);
16466 2 : if (ret)
16467 : return ret;
16468 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16469 2 : if (ret)
16470 : return ret;
16471 : }
16472 : }
16473 278 : else if (INTEGRAL_TYPE_P (*tp))
16474 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16475 : ((struct c_find_omp_var_s *) data)->pset);
16476 : return NULL_TREE;
16477 : }
16478 :
16479 : /* Finish OpenMP iterators ITER. Return true if they are errorneous
16480 : and clauses containing them should be removed. */
16481 :
16482 : static bool
16483 143 : c_omp_finish_iterators (tree iter)
16484 : {
16485 143 : bool ret = false;
16486 325 : for (tree it = iter; it; it = TREE_CHAIN (it))
16487 : {
16488 182 : tree var = TREE_VEC_ELT (it, 0);
16489 182 : tree begin = TREE_VEC_ELT (it, 1);
16490 182 : tree end = TREE_VEC_ELT (it, 2);
16491 182 : tree step = TREE_VEC_ELT (it, 3);
16492 182 : tree orig_step;
16493 182 : tree type = TREE_TYPE (var);
16494 182 : location_t loc = DECL_SOURCE_LOCATION (var);
16495 182 : if (type == error_mark_node)
16496 : {
16497 0 : ret = true;
16498 34 : continue;
16499 : }
16500 182 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16501 : {
16502 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16503 : var);
16504 6 : ret = true;
16505 6 : continue;
16506 : }
16507 176 : else if (TYPE_ATOMIC (type))
16508 : {
16509 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16510 2 : ret = true;
16511 2 : continue;
16512 : }
16513 174 : else if (TYPE_READONLY (type))
16514 : {
16515 4 : error_at (loc, "iterator %qD has const qualified type", var);
16516 4 : ret = true;
16517 4 : continue;
16518 : }
16519 170 : else if (step == error_mark_node
16520 170 : || TREE_TYPE (step) == error_mark_node)
16521 : {
16522 0 : ret = true;
16523 0 : continue;
16524 : }
16525 170 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16526 : {
16527 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16528 : "iterator step with non-integral type");
16529 4 : ret = true;
16530 4 : continue;
16531 : }
16532 166 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16533 166 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16534 166 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16535 166 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16536 166 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16537 166 : if (POINTER_TYPE_P (type))
16538 : {
16539 14 : begin = save_expr (begin);
16540 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16541 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16542 : fold_convert (sizetype, step),
16543 : fold_convert (sizetype, begin));
16544 14 : step = fold_convert (ssizetype, step);
16545 : }
16546 166 : if (integer_zerop (step))
16547 : {
16548 6 : error_at (loc, "iterator %qD has zero step", var);
16549 6 : ret = true;
16550 6 : continue;
16551 : }
16552 :
16553 160 : if (begin == error_mark_node
16554 158 : || end == error_mark_node
16555 156 : || step == error_mark_node
16556 156 : || orig_step == error_mark_node)
16557 : {
16558 4 : ret = true;
16559 4 : continue;
16560 : }
16561 156 : hash_set<tree> pset;
16562 156 : tree it2;
16563 197 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16564 : {
16565 49 : tree var2 = TREE_VEC_ELT (it2, 0);
16566 49 : tree begin2 = TREE_VEC_ELT (it2, 1);
16567 49 : tree end2 = TREE_VEC_ELT (it2, 2);
16568 49 : tree step2 = TREE_VEC_ELT (it2, 3);
16569 49 : tree type2 = TREE_TYPE (var2);
16570 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16571 49 : struct c_find_omp_var_s data = { var, &pset };
16572 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16573 : {
16574 2 : error_at (loc2,
16575 : "type of iterator %qD refers to outer iterator %qD",
16576 : var2, var);
16577 10 : break;
16578 : }
16579 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16580 : {
16581 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16582 : "begin expression refers to outer iterator %qD", var);
16583 2 : break;
16584 : }
16585 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16586 : {
16587 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16588 : "end expression refers to outer iterator %qD", var);
16589 2 : break;
16590 : }
16591 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16592 : {
16593 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16594 : "step expression refers to outer iterator %qD", var);
16595 2 : break;
16596 : }
16597 : }
16598 156 : if (it2)
16599 : {
16600 8 : ret = true;
16601 8 : continue;
16602 : }
16603 148 : TREE_VEC_ELT (it, 1) = begin;
16604 148 : TREE_VEC_ELT (it, 2) = end;
16605 148 : TREE_VEC_ELT (it, 3) = step;
16606 148 : TREE_VEC_ELT (it, 4) = orig_step;
16607 156 : }
16608 143 : return ret;
16609 : }
16610 :
16611 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16612 : Return true if an error has been detected. */
16613 :
16614 : static bool
16615 9496 : c_oacc_check_attachments (tree c)
16616 : {
16617 9496 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16618 : return false;
16619 :
16620 : /* OpenACC attach / detach clauses must be pointers. */
16621 6501 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16622 6501 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16623 : {
16624 71 : tree t = OMP_CLAUSE_DECL (c);
16625 :
16626 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16627 6 : t = TREE_OPERAND (t, 0);
16628 :
16629 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16630 : {
16631 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16632 : user_omp_clause_code_name (c, true));
16633 6 : return true;
16634 : }
16635 : }
16636 :
16637 : return false;
16638 : }
16639 :
16640 : /* For all elements of CLAUSES, validate them against their constraints.
16641 : Remove any elements from the list that are invalid. */
16642 :
16643 : tree
16644 29470 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16645 : {
16646 29470 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16647 29470 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16648 29470 : bitmap_head oacc_reduction_head, is_on_device_head;
16649 29470 : tree c, t, type, *pc;
16650 29470 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16651 29470 : bool branch_seen = false;
16652 29470 : bool copyprivate_seen = false;
16653 29470 : bool mergeable_seen = false;
16654 29470 : tree *detach_seen = NULL;
16655 29470 : bool linear_variable_step_check = false;
16656 29470 : tree *nowait_clause = NULL;
16657 29470 : tree *grainsize_seen = NULL;
16658 29470 : bool num_tasks_seen = false;
16659 29470 : tree ordered_clause = NULL_TREE;
16660 29470 : tree schedule_clause = NULL_TREE;
16661 29470 : bool oacc_async = false;
16662 29470 : tree last_iterators = NULL_TREE;
16663 29470 : bool last_iterators_remove = false;
16664 29470 : tree *nogroup_seen = NULL;
16665 29470 : tree *order_clause = NULL;
16666 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16667 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16668 29470 : int reduction_seen = 0;
16669 29470 : bool allocate_seen = false;
16670 29470 : bool implicit_moved = false;
16671 29470 : bool target_in_reduction_seen = false;
16672 29470 : tree *full_seen = NULL;
16673 29470 : bool partial_seen = false;
16674 29470 : bool openacc = (ort & C_ORT_ACC) != 0;
16675 29470 : bool init_seen = false;
16676 29470 : bool init_use_destroy_seen = false;
16677 29470 : tree init_no_targetsync_clause = NULL_TREE;
16678 29470 : tree depend_clause = NULL_TREE;
16679 :
16680 29470 : bitmap_obstack_initialize (NULL);
16681 29470 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16682 29470 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16683 29470 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16684 29470 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16685 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16686 29470 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16687 29470 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16688 29470 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16689 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16690 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16691 29470 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16692 29470 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16693 :
16694 29470 : if (openacc)
16695 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16696 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16697 : {
16698 : oacc_async = true;
16699 : break;
16700 : }
16701 :
16702 29470 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16703 :
16704 77416 : for (pc = &clauses, c = clauses; c ; c = *pc)
16705 : {
16706 47946 : bool remove = false;
16707 47946 : bool need_complete = false;
16708 47946 : bool need_implicitly_determined = false;
16709 :
16710 : /* We've reached the end of a list of expanded nodes. Reset the group
16711 : start pointer. */
16712 47946 : if (c == grp_sentinel)
16713 : {
16714 2470 : if (grp_start_p
16715 2470 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16716 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16717 22 : gc = OMP_CLAUSE_CHAIN (gc))
16718 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16719 : grp_start_p = NULL;
16720 : }
16721 :
16722 47946 : switch (OMP_CLAUSE_CODE (c))
16723 : {
16724 1139 : case OMP_CLAUSE_SHARED:
16725 1139 : need_implicitly_determined = true;
16726 1139 : goto check_dup_generic;
16727 :
16728 847 : case OMP_CLAUSE_PRIVATE:
16729 847 : need_complete = true;
16730 847 : need_implicitly_determined = true;
16731 847 : goto check_dup_generic;
16732 :
16733 3519 : case OMP_CLAUSE_REDUCTION:
16734 3519 : if (reduction_seen == 0)
16735 2920 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16736 599 : else if (reduction_seen != -2
16737 1198 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16738 599 : ? -1 : 1))
16739 : {
16740 2 : error_at (OMP_CLAUSE_LOCATION (c),
16741 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16742 : "on the same construct");
16743 2 : reduction_seen = -2;
16744 : }
16745 : /* FALLTHRU */
16746 4183 : case OMP_CLAUSE_IN_REDUCTION:
16747 4183 : case OMP_CLAUSE_TASK_REDUCTION:
16748 4183 : need_implicitly_determined = true;
16749 4183 : t = OMP_CLAUSE_DECL (c);
16750 4183 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16751 : {
16752 553 : if (handle_omp_array_sections (c, ort))
16753 : {
16754 : remove = true;
16755 : break;
16756 : }
16757 :
16758 537 : t = OMP_CLAUSE_DECL (c);
16759 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16760 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16761 : {
16762 1 : error_at (OMP_CLAUSE_LOCATION (c),
16763 : "%<inscan%> %<reduction%> clause with array "
16764 : "section");
16765 1 : remove = true;
16766 1 : break;
16767 : }
16768 : }
16769 4166 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16770 4166 : if (t == error_mark_node)
16771 : {
16772 : remove = true;
16773 : break;
16774 : }
16775 4164 : if (oacc_async)
16776 3 : c_mark_addressable (t);
16777 4164 : type = TREE_TYPE (t);
16778 4164 : if (TREE_CODE (t) == MEM_REF)
16779 536 : type = TREE_TYPE (type);
16780 4164 : if (TREE_CODE (type) == ARRAY_TYPE)
16781 : {
16782 74 : tree oatype = type;
16783 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16784 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16785 74 : type = TREE_TYPE (type);
16786 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16787 : {
16788 1 : error_at (OMP_CLAUSE_LOCATION (c),
16789 : "%qD in %<reduction%> clause is a zero size array",
16790 : t);
16791 1 : remove = true;
16792 1 : break;
16793 : }
16794 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16795 : TYPE_SIZE_UNIT (type));
16796 73 : if (integer_zerop (size))
16797 : {
16798 1 : error_at (OMP_CLAUSE_LOCATION (c),
16799 : "%qD in %<reduction%> clause is a zero size array",
16800 : t);
16801 1 : remove = true;
16802 1 : break;
16803 : }
16804 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16805 72 : size = save_expr (size);
16806 72 : tree index_type = build_index_type (size);
16807 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16808 : index_type);
16809 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16810 72 : tree ptype = c_build_pointer_type (type);
16811 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16812 72 : t = build_fold_addr_expr (t);
16813 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16814 72 : OMP_CLAUSE_DECL (c) = t;
16815 : }
16816 4162 : if (TYPE_ATOMIC (type))
16817 : {
16818 3 : error_at (OMP_CLAUSE_LOCATION (c),
16819 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16820 3 : remove = true;
16821 3 : break;
16822 : }
16823 4159 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16824 4159 : || OMP_CLAUSE_REDUCTION_TASK (c))
16825 : {
16826 : /* Disallow zero sized or potentially zero sized task
16827 : reductions. */
16828 871 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16829 : {
16830 6 : error_at (OMP_CLAUSE_LOCATION (c),
16831 : "zero sized type %qT in %qs clause", type,
16832 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16833 3 : remove = true;
16834 3 : break;
16835 : }
16836 868 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16837 : {
16838 0 : error_at (OMP_CLAUSE_LOCATION (c),
16839 : "variable sized type %qT in %qs clause", type,
16840 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16841 0 : remove = true;
16842 0 : break;
16843 : }
16844 : }
16845 4156 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16846 4156 : && (FLOAT_TYPE_P (type)
16847 3604 : || TREE_CODE (type) == COMPLEX_TYPE))
16848 : {
16849 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16850 340 : const char *r_name = NULL;
16851 :
16852 340 : switch (r_code)
16853 : {
16854 : case PLUS_EXPR:
16855 : case MULT_EXPR:
16856 : case MINUS_EXPR:
16857 : case TRUTH_ANDIF_EXPR:
16858 : case TRUTH_ORIF_EXPR:
16859 : break;
16860 13 : case MIN_EXPR:
16861 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16862 : r_name = "min";
16863 : break;
16864 38 : case MAX_EXPR:
16865 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16866 : r_name = "max";
16867 : break;
16868 3 : case BIT_AND_EXPR:
16869 3 : r_name = "&";
16870 3 : break;
16871 2 : case BIT_XOR_EXPR:
16872 2 : r_name = "^";
16873 2 : break;
16874 : case BIT_IOR_EXPR:
16875 : r_name = "|";
16876 : break;
16877 0 : default:
16878 0 : gcc_unreachable ();
16879 : }
16880 5 : if (r_name)
16881 : {
16882 12 : error_at (OMP_CLAUSE_LOCATION (c),
16883 : "%qE has invalid type for %<reduction(%s)%>",
16884 : t, r_name);
16885 12 : remove = true;
16886 12 : break;
16887 : }
16888 : }
16889 3816 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16890 : {
16891 2 : error_at (OMP_CLAUSE_LOCATION (c),
16892 : "user defined reduction not found for %qE", t);
16893 2 : remove = true;
16894 2 : break;
16895 : }
16896 3814 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16897 : {
16898 277 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16899 277 : type = TYPE_MAIN_VARIANT (type);
16900 277 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16901 : VAR_DECL, NULL_TREE, type);
16902 277 : tree decl_placeholder = NULL_TREE;
16903 277 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16904 277 : DECL_ARTIFICIAL (placeholder) = 1;
16905 277 : DECL_IGNORED_P (placeholder) = 1;
16906 277 : if (TREE_CODE (t) == MEM_REF)
16907 : {
16908 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16909 : VAR_DECL, NULL_TREE, type);
16910 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16911 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16912 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16913 : }
16914 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16915 42 : c_mark_addressable (placeholder);
16916 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16917 76 : c_mark_addressable (decl_placeholder ? decl_placeholder
16918 34 : : OMP_CLAUSE_DECL (c));
16919 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16920 775 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16921 277 : TREE_VEC_ELT (list, 0),
16922 277 : TREE_VEC_ELT (list, 1),
16923 : decl_placeholder ? decl_placeholder
16924 221 : : OMP_CLAUSE_DECL (c), placeholder);
16925 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16926 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16927 : void_type_node, NULL_TREE,
16928 277 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16929 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16930 277 : if (TREE_VEC_LENGTH (list) == 6)
16931 : {
16932 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16933 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16934 33 : : OMP_CLAUSE_DECL (c));
16935 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16936 27 : c_mark_addressable (placeholder);
16937 233 : tree init = TREE_VEC_ELT (list, 5);
16938 233 : if (init == error_mark_node)
16939 197 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16940 233 : OMP_CLAUSE_REDUCTION_INIT (c)
16941 651 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16942 233 : TREE_VEC_ELT (list, 3),
16943 : decl_placeholder ? decl_placeholder
16944 185 : : OMP_CLAUSE_DECL (c), placeholder);
16945 233 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16946 : {
16947 197 : tree v = decl_placeholder ? decl_placeholder : t;
16948 197 : OMP_CLAUSE_REDUCTION_INIT (c)
16949 394 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16950 197 : OMP_CLAUSE_REDUCTION_INIT (c));
16951 : }
16952 233 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16953 : c_find_omp_placeholder_r,
16954 : placeholder, NULL))
16955 28 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16956 : }
16957 : else
16958 : {
16959 44 : tree init;
16960 44 : tree v = decl_placeholder ? decl_placeholder : t;
16961 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16962 34 : init = build_constructor (TREE_TYPE (v), NULL);
16963 : else
16964 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
16965 44 : OMP_CLAUSE_REDUCTION_INIT (c)
16966 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16967 : }
16968 277 : OMP_CLAUSE_REDUCTION_INIT (c)
16969 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16970 : void_type_node, NULL_TREE,
16971 277 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16972 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16973 : }
16974 4142 : if (TREE_CODE (t) == MEM_REF)
16975 : {
16976 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16977 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16978 : != INTEGER_CST)
16979 : {
16980 0 : sorry ("variable length element type in array "
16981 : "%<reduction%> clause");
16982 0 : remove = true;
16983 0 : break;
16984 : }
16985 607 : t = TREE_OPERAND (t, 0);
16986 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16987 139 : t = TREE_OPERAND (t, 0);
16988 607 : if (TREE_CODE (t) == ADDR_EXPR)
16989 367 : t = TREE_OPERAND (t, 0);
16990 : }
16991 4142 : goto check_dup_generic_t;
16992 :
16993 24 : case OMP_CLAUSE_COPYPRIVATE:
16994 24 : copyprivate_seen = true;
16995 24 : if (nowait_clause)
16996 : {
16997 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
16998 : "%<nowait%> clause must not be used together "
16999 : "with %<copyprivate%> clause");
17000 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
17001 1 : nowait_clause = NULL;
17002 : }
17003 24 : goto check_dup_generic;
17004 :
17005 99 : case OMP_CLAUSE_COPYIN:
17006 99 : t = OMP_CLAUSE_DECL (c);
17007 99 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17008 : {
17009 5 : error_at (OMP_CLAUSE_LOCATION (c),
17010 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17011 5 : remove = true;
17012 5 : break;
17013 : }
17014 94 : goto check_dup_generic;
17015 :
17016 480 : case OMP_CLAUSE_LINEAR:
17017 480 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17018 327 : need_implicitly_determined = true;
17019 480 : t = OMP_CLAUSE_DECL (c);
17020 480 : if (ort != C_ORT_OMP_DECLARE_SIMD
17021 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17022 485 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17023 : {
17024 5 : error_at (OMP_CLAUSE_LOCATION (c),
17025 : "modifier should not be specified in %<linear%> "
17026 : "clause on %<simd%> or %<for%> constructs when not "
17027 : "using OpenMP 5.2 modifiers");
17028 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17029 : }
17030 960 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17031 503 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17032 : {
17033 1 : error_at (OMP_CLAUSE_LOCATION (c),
17034 : "linear clause applied to non-integral non-pointer "
17035 1 : "variable with type %qT", TREE_TYPE (t));
17036 1 : remove = true;
17037 1 : break;
17038 : }
17039 479 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17040 : {
17041 3 : error_at (OMP_CLAUSE_LOCATION (c),
17042 : "%<_Atomic%> %qD in %<linear%> clause", t);
17043 3 : remove = true;
17044 3 : break;
17045 : }
17046 476 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17047 : {
17048 152 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17049 152 : if (TREE_CODE (s) == PARM_DECL)
17050 : {
17051 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17052 : /* map_head bitmap is used as uniform_head if
17053 : declare_simd. */
17054 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17055 5 : linear_variable_step_check = true;
17056 9 : goto check_dup_generic;
17057 : }
17058 143 : if (TREE_CODE (s) != INTEGER_CST)
17059 : {
17060 7 : error_at (OMP_CLAUSE_LOCATION (c),
17061 : "%<linear%> clause step %qE is neither constant "
17062 : "nor a parameter", s);
17063 7 : remove = true;
17064 7 : break;
17065 : }
17066 : }
17067 460 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17068 : {
17069 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17070 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17071 20 : OMP_CLAUSE_DECL (c), s);
17072 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17073 : sizetype, fold_convert (sizetype, s),
17074 20 : fold_convert
17075 : (sizetype, OMP_CLAUSE_DECL (c)));
17076 20 : if (s == error_mark_node)
17077 0 : s = size_one_node;
17078 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17079 : }
17080 : else
17081 440 : OMP_CLAUSE_LINEAR_STEP (c)
17082 880 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17083 460 : goto check_dup_generic;
17084 :
17085 2887 : check_dup_generic:
17086 2887 : t = OMP_CLAUSE_DECL (c);
17087 7118 : check_dup_generic_t:
17088 7118 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17089 : {
17090 4 : error_at (OMP_CLAUSE_LOCATION (c),
17091 : "%qE is not a variable in clause %qs", t,
17092 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17093 2 : remove = true;
17094 : }
17095 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17096 6077 : || (ort == C_ORT_OMP
17097 5358 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17098 5347 : || (OMP_CLAUSE_CODE (c)
17099 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17100 13146 : || (ort == C_ORT_OMP_TARGET
17101 347 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17102 : {
17103 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17104 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17105 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17106 : {
17107 2 : error_at (OMP_CLAUSE_LOCATION (c),
17108 : "%qD appears more than once in data-sharing "
17109 : "clauses", t);
17110 2 : remove = true;
17111 2 : break;
17112 : }
17113 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17114 108 : target_in_reduction_seen = true;
17115 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17116 : {
17117 4 : error_at (OMP_CLAUSE_LOCATION (c),
17118 : openacc
17119 : ? "%qD appears more than once in reduction clauses"
17120 : : "%qD appears more than once in data clauses",
17121 : t);
17122 2 : remove = true;
17123 : }
17124 : else
17125 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17126 : }
17127 5920 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17128 5897 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17129 5896 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17130 11816 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17131 : {
17132 24 : error_at (OMP_CLAUSE_LOCATION (c),
17133 : "%qE appears more than once in data clauses", t);
17134 24 : remove = true;
17135 : }
17136 5896 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17137 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17138 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17139 1047 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17140 : {
17141 3 : if (openacc)
17142 0 : error_at (OMP_CLAUSE_LOCATION (c),
17143 : "%qD appears more than once in data clauses", t);
17144 : else
17145 3 : error_at (OMP_CLAUSE_LOCATION (c),
17146 : "%qD appears both in data and map clauses", t);
17147 : remove = true;
17148 : }
17149 : else
17150 5893 : bitmap_set_bit (&generic_head, DECL_UID (t));
17151 : break;
17152 :
17153 1333 : case OMP_CLAUSE_FIRSTPRIVATE:
17154 1333 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17155 : {
17156 186 : move_implicit:
17157 186 : implicit_moved = true;
17158 : /* Move firstprivate and map clauses with
17159 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17160 : clauses chain. */
17161 186 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17162 186 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17163 1072 : while (*pc1)
17164 886 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17165 886 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17166 : {
17167 113 : *pc3 = *pc1;
17168 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17169 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17170 : }
17171 773 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17172 773 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17173 : {
17174 164 : *pc2 = *pc1;
17175 164 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17176 164 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17177 : }
17178 : else
17179 609 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17180 186 : *pc3 = NULL;
17181 186 : *pc2 = cl2;
17182 186 : *pc1 = cl1;
17183 186 : continue;
17184 186 : }
17185 1238 : t = OMP_CLAUSE_DECL (c);
17186 1238 : need_complete = true;
17187 1238 : need_implicitly_determined = true;
17188 1238 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17189 : {
17190 1 : error_at (OMP_CLAUSE_LOCATION (c),
17191 : "%qE is not a variable in clause %<firstprivate%>", t);
17192 1 : remove = true;
17193 : }
17194 1237 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17195 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17196 1343 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17197 : remove = true;
17198 1237 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17199 1235 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17200 2471 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17201 : {
17202 4 : error_at (OMP_CLAUSE_LOCATION (c),
17203 : "%qE appears more than once in data clauses", t);
17204 4 : remove = true;
17205 : }
17206 1233 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17207 1233 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17208 : {
17209 7 : if (openacc)
17210 0 : error_at (OMP_CLAUSE_LOCATION (c),
17211 : "%qD appears more than once in data clauses", t);
17212 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17213 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17214 : /* Silently drop the clause. */;
17215 : else
17216 6 : error_at (OMP_CLAUSE_LOCATION (c),
17217 : "%qD appears both in data and map clauses", t);
17218 : remove = true;
17219 : }
17220 : else
17221 1226 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17222 : break;
17223 :
17224 1523 : case OMP_CLAUSE_LASTPRIVATE:
17225 1523 : t = OMP_CLAUSE_DECL (c);
17226 1523 : need_complete = true;
17227 1523 : need_implicitly_determined = true;
17228 1523 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17229 : {
17230 0 : error_at (OMP_CLAUSE_LOCATION (c),
17231 : "%qE is not a variable in clause %<lastprivate%>", t);
17232 0 : remove = true;
17233 : }
17234 1523 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17235 1523 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17236 : {
17237 3 : error_at (OMP_CLAUSE_LOCATION (c),
17238 : "%qE appears more than once in data clauses", t);
17239 3 : remove = true;
17240 : }
17241 : else
17242 1520 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17243 : break;
17244 :
17245 253 : case OMP_CLAUSE_ALIGNED:
17246 253 : t = OMP_CLAUSE_DECL (c);
17247 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17248 : {
17249 0 : error_at (OMP_CLAUSE_LOCATION (c),
17250 : "%qE is not a variable in %<aligned%> clause", t);
17251 0 : remove = true;
17252 : }
17253 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17254 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17255 : {
17256 7 : error_at (OMP_CLAUSE_LOCATION (c),
17257 : "%qE in %<aligned%> clause is neither a pointer nor "
17258 : "an array", t);
17259 7 : remove = true;
17260 : }
17261 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17262 : {
17263 1 : error_at (OMP_CLAUSE_LOCATION (c),
17264 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17265 1 : remove = true;
17266 1 : break;
17267 : }
17268 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17269 : {
17270 0 : error_at (OMP_CLAUSE_LOCATION (c),
17271 : "%qE appears more than once in %<aligned%> clauses",
17272 : t);
17273 0 : remove = true;
17274 : }
17275 : else
17276 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17277 : break;
17278 :
17279 125 : case OMP_CLAUSE_NONTEMPORAL:
17280 125 : t = OMP_CLAUSE_DECL (c);
17281 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17282 : {
17283 0 : error_at (OMP_CLAUSE_LOCATION (c),
17284 : "%qE is not a variable in %<nontemporal%> clause", t);
17285 0 : remove = true;
17286 : }
17287 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17288 : {
17289 2 : error_at (OMP_CLAUSE_LOCATION (c),
17290 : "%qE appears more than once in %<nontemporal%> "
17291 : "clauses", t);
17292 2 : remove = true;
17293 : }
17294 : else
17295 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17296 : break;
17297 :
17298 802 : case OMP_CLAUSE_ALLOCATE:
17299 802 : t = OMP_CLAUSE_DECL (c);
17300 802 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17301 : {
17302 1 : error_at (OMP_CLAUSE_LOCATION (c),
17303 : "%qE is not a variable in %<allocate%> clause", t);
17304 1 : remove = true;
17305 : }
17306 801 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17307 : {
17308 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17309 : "%qE appears more than once in %<allocate%> clauses",
17310 : t);
17311 2 : remove = true;
17312 : }
17313 : else
17314 : {
17315 799 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17316 799 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17317 700 : allocate_seen = true;
17318 : }
17319 : break;
17320 :
17321 357 : case OMP_CLAUSE_DOACROSS:
17322 357 : t = OMP_CLAUSE_DECL (c);
17323 357 : if (t == NULL_TREE)
17324 : break;
17325 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17326 : {
17327 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17328 1315 : for (; t; t = TREE_CHAIN (t))
17329 : {
17330 1146 : tree decl = TREE_VALUE (t);
17331 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17332 : {
17333 3 : tree offset = TREE_PURPOSE (t);
17334 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17335 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17336 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17337 : neg ? MINUS_EXPR : PLUS_EXPR,
17338 : decl, offset);
17339 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17340 : sizetype,
17341 : fold_convert (sizetype, t2),
17342 : fold_convert (sizetype, decl));
17343 3 : if (t2 == error_mark_node)
17344 : {
17345 : remove = true;
17346 : break;
17347 : }
17348 3 : TREE_PURPOSE (t) = t2;
17349 : }
17350 : }
17351 : break;
17352 : }
17353 0 : gcc_unreachable ();
17354 :
17355 36 : case OMP_CLAUSE_USES_ALLOCATORS:
17356 36 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17357 36 : if (t == error_mark_node)
17358 : {
17359 : remove = true;
17360 : break;
17361 : }
17362 6 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17363 33 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17364 27 : || bitmap_bit_p (&map_head, DECL_UID (t))
17365 27 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17366 26 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17367 : {
17368 1 : error_at (OMP_CLAUSE_LOCATION (c),
17369 : "%qE appears more than once in data clauses", t);
17370 1 : remove = true;
17371 1 : break;
17372 : }
17373 : else
17374 31 : bitmap_set_bit (&generic_head, DECL_UID (t));
17375 31 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17376 59 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17377 : "omp_allocator_handle_t") != 0)
17378 : {
17379 3 : error_at (OMP_CLAUSE_LOCATION (c),
17380 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17381 : "type", t);
17382 3 : remove = true;
17383 3 : break;
17384 : }
17385 28 : tree init;
17386 28 : if (!DECL_P (t)
17387 28 : || (TREE_CODE (t) == CONST_DECL
17388 5 : && ((init = DECL_INITIAL(t)) == nullptr
17389 5 : || TREE_CODE (init) != INTEGER_CST
17390 5 : || ((wi::to_widest (init) < 0
17391 5 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17392 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17393 0 : || (wi::to_widest (init)
17394 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17395 : {
17396 0 : remove = true;
17397 0 : error_at (OMP_CLAUSE_LOCATION (c),
17398 : "allocator %qE must be either a variable or a "
17399 : "predefined allocator", t);
17400 0 : break;
17401 : }
17402 28 : else if (TREE_CODE (t) == CONST_DECL)
17403 : {
17404 : /* omp_null_allocator is ignored and for predefined allocators,
17405 : not special handling is required; thus, remove them removed. */
17406 5 : remove = true;
17407 :
17408 5 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17409 5 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17410 : {
17411 0 : error_at (OMP_CLAUSE_LOCATION (c),
17412 : "modifiers cannot be used with predefined "
17413 : "allocator %qE", t);
17414 0 : break;
17415 : }
17416 : }
17417 28 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17418 28 : if (t == error_mark_node)
17419 : {
17420 : remove = true;
17421 : break;
17422 : }
17423 28 : if (t != NULL_TREE
17424 28 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17425 5 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17426 8 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17427 : "omp_memspace_handle_t") != 0))
17428 : {
17429 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17430 : " constant enum of %<omp_memspace_handle_t%> type", t);
17431 1 : remove = true;
17432 1 : break;
17433 : }
17434 27 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17435 27 : if (t == error_mark_node)
17436 : {
17437 : remove = true;
17438 : break;
17439 : }
17440 27 : if (t != NULL_TREE
17441 : && t != error_mark_node
17442 27 : && (DECL_EXTERNAL (t)
17443 9 : || TREE_CODE (t) == PARM_DECL))
17444 : {
17445 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17446 : "defined in same scope as the construct on which the "
17447 : "clause appears", t);
17448 3 : remove = true;
17449 : }
17450 27 : if (t != NULL_TREE)
17451 : {
17452 11 : bool type_err = false;
17453 :
17454 11 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17455 10 : || DECL_SIZE (t) == NULL_TREE
17456 20 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17457 : type_err = true;
17458 : else
17459 : {
17460 9 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17461 9 : if (TREE_CODE (elem_t) != RECORD_TYPE
17462 18 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17463 : "omp_alloctrait_t") != 0
17464 18 : || !TYPE_READONLY (elem_t))
17465 : type_err = true;
17466 : }
17467 8 : if (type_err)
17468 : {
17469 3 : if (t != error_mark_node)
17470 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17471 : "be of %<const omp_alloctrait_t []%> type", t);
17472 : else
17473 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17474 : "be of %<const omp_alloctrait_t []%> type");
17475 : remove = true;
17476 : }
17477 : else
17478 : {
17479 8 : tree cst_val = decl_constant_value_1 (t, true);
17480 8 : if (cst_val == t)
17481 : {
17482 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17483 : "initialized with constants");
17484 :
17485 1 : remove = true;
17486 : }
17487 : }
17488 : }
17489 24 : if (remove)
17490 : break;
17491 18 : pc = &OMP_CLAUSE_CHAIN (c);
17492 18 : continue;
17493 713 : case OMP_CLAUSE_DEPEND:
17494 713 : depend_clause = c;
17495 : /* FALLTHRU */
17496 870 : case OMP_CLAUSE_AFFINITY:
17497 870 : t = OMP_CLAUSE_DECL (c);
17498 870 : if (OMP_ITERATOR_DECL_P (t))
17499 : {
17500 131 : if (TREE_PURPOSE (t) != last_iterators)
17501 111 : last_iterators_remove
17502 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17503 131 : last_iterators = TREE_PURPOSE (t);
17504 131 : t = TREE_VALUE (t);
17505 131 : if (last_iterators_remove)
17506 34 : t = error_mark_node;
17507 : }
17508 : else
17509 : last_iterators = NULL_TREE;
17510 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17511 : {
17512 413 : if (handle_omp_array_sections (c, ort))
17513 : remove = true;
17514 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17515 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17516 : {
17517 1 : error_at (OMP_CLAUSE_LOCATION (c),
17518 : "%<depend%> clause with %<depobj%> dependence "
17519 : "type on array section");
17520 1 : remove = true;
17521 : }
17522 : break;
17523 : }
17524 457 : if (t == error_mark_node)
17525 : remove = true;
17526 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17527 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17528 : {
17529 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17530 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17531 : {
17532 3 : error_at (OMP_CLAUSE_LOCATION (c),
17533 : "%<omp_all_memory%> used with %<depend%> kind "
17534 : "other than %<out%> or %<inout%>");
17535 3 : remove = true;
17536 : }
17537 : }
17538 408 : else if (!lvalue_p (t))
17539 : {
17540 6 : error_at (OMP_CLAUSE_LOCATION (c),
17541 : "%qE is not lvalue expression nor array section in "
17542 : "%qs clause", t,
17543 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17544 3 : remove = true;
17545 : }
17546 405 : else if (TREE_CODE (t) == COMPONENT_REF
17547 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17548 : {
17549 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17550 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17551 2 : error_at (OMP_CLAUSE_LOCATION (c),
17552 : "bit-field %qE in %qs clause", t,
17553 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17554 2 : remove = true;
17555 : }
17556 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17557 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17558 : {
17559 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17560 : {
17561 2 : error_at (OMP_CLAUSE_LOCATION (c),
17562 : "%qE does not have %<omp_depend_t%> type in "
17563 : "%<depend%> clause with %<depobj%> dependence "
17564 : "type", t);
17565 2 : remove = true;
17566 : }
17567 : }
17568 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17569 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17570 : {
17571 2 : error_at (OMP_CLAUSE_LOCATION (c),
17572 : "%qE should not have %<omp_depend_t%> type in "
17573 : "%<depend%> clause with dependence type other than "
17574 : "%<depobj%>", t);
17575 2 : remove = true;
17576 : }
17577 12 : if (!remove)
17578 : {
17579 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17580 12 : t = null_pointer_node;
17581 : else
17582 : {
17583 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17584 : ADDR_EXPR, t, false);
17585 399 : if (addr == error_mark_node)
17586 : {
17587 : remove = true;
17588 : break;
17589 : }
17590 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17591 : RO_UNARY_STAR);
17592 399 : if (t == error_mark_node)
17593 : {
17594 : remove = true;
17595 : break;
17596 : }
17597 : }
17598 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17599 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17600 : else
17601 380 : OMP_CLAUSE_DECL (c) = t;
17602 : }
17603 : break;
17604 :
17605 6628 : case OMP_CLAUSE_MAP:
17606 6628 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17607 91 : goto move_implicit;
17608 6537 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17609 6537 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17610 : {
17611 : remove = true;
17612 : break;
17613 : }
17614 : /* FALLTHRU */
17615 9390 : case OMP_CLAUSE_TO:
17616 9390 : case OMP_CLAUSE_FROM:
17617 9390 : if (OMP_CLAUSE_ITERATORS (c)
17618 9390 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17619 : {
17620 22808 : t = error_mark_node;
17621 : break;
17622 : }
17623 : /* FALLTHRU */
17624 9526 : case OMP_CLAUSE__CACHE_:
17625 9526 : {
17626 9526 : using namespace omp_addr_tokenizer;
17627 9526 : auto_vec<omp_addr_token *, 10> addr_tokens;
17628 :
17629 9526 : t = OMP_CLAUSE_DECL (c);
17630 9526 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17631 : {
17632 2002 : grp_start_p = pc;
17633 2002 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17634 :
17635 2002 : if (handle_omp_array_sections (c, ort))
17636 : remove = true;
17637 : else
17638 : {
17639 1862 : t = OMP_CLAUSE_DECL (c);
17640 1862 : if (!omp_mappable_type (TREE_TYPE (t)))
17641 : {
17642 2 : error_at (OMP_CLAUSE_LOCATION (c),
17643 : "array section does not have mappable type "
17644 : "in %qs clause",
17645 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17646 1 : remove = true;
17647 : }
17648 1861 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17649 : {
17650 2 : error_at (OMP_CLAUSE_LOCATION (c),
17651 : "%<_Atomic%> %qE in %qs clause", t,
17652 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17653 1 : remove = true;
17654 : }
17655 2511 : while (TREE_CODE (t) == ARRAY_REF)
17656 649 : t = TREE_OPERAND (t, 0);
17657 :
17658 1862 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17659 :
17660 1862 : if (!omp_parse_expr (addr_tokens, t))
17661 : {
17662 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17663 : "unsupported map expression %qE",
17664 0 : OMP_CLAUSE_DECL (c));
17665 0 : remove = true;
17666 0 : break;
17667 : }
17668 :
17669 : /* This check is to determine if this will be the only map
17670 : node created for this clause. Otherwise, we'll check
17671 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17672 : node on the next iteration(s) of the loop. */
17673 3706 : if (addr_tokens.length () >= 4
17674 304 : && addr_tokens[0]->type == STRUCTURE_BASE
17675 302 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17676 302 : && addr_tokens[1]->type == ACCESS_METHOD
17677 302 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17678 302 : && addr_tokens[3]->type == ACCESS_METHOD
17679 2103 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17680 188 : || (addr_tokens[3]->u.access_kind
17681 : == ACCESS_INDEXED_ARRAY)))
17682 : {
17683 55 : tree rt = addr_tokens[1]->expr;
17684 :
17685 55 : gcc_assert (DECL_P (rt));
17686 :
17687 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17688 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17689 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17690 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17691 0 : || bitmap_bit_p (&map_firstprivate_head,
17692 0 : DECL_UID (rt))))
17693 : {
17694 : remove = true;
17695 : break;
17696 : }
17697 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17698 : break;
17699 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17700 : {
17701 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17702 0 : error_at (OMP_CLAUSE_LOCATION (c),
17703 : "%qD appears more than once in motion "
17704 : "clauses", rt);
17705 0 : else if (openacc)
17706 0 : error_at (OMP_CLAUSE_LOCATION (c),
17707 : "%qD appears more than once in data "
17708 : "clauses", rt);
17709 : else
17710 0 : error_at (OMP_CLAUSE_LOCATION (c),
17711 : "%qD appears more than once in map "
17712 : "clauses", rt);
17713 : remove = true;
17714 : }
17715 : else
17716 : {
17717 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17718 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17719 : }
17720 : }
17721 1862 : }
17722 1984 : if (c_oacc_check_attachments (c))
17723 2 : remove = true;
17724 1984 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17725 1808 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17726 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17727 2009 : && !OMP_CLAUSE_SIZE (c))
17728 : /* In this case, we have a single array element which is a
17729 : pointer, and we already set OMP_CLAUSE_SIZE in
17730 : handle_omp_array_sections above. For attach/detach
17731 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17732 : to zero here. */
17733 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17734 : break;
17735 : }
17736 7524 : else if (!omp_parse_expr (addr_tokens, t))
17737 : {
17738 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17739 : "unsupported map expression %qE",
17740 0 : OMP_CLAUSE_DECL (c));
17741 0 : remove = true;
17742 0 : break;
17743 : }
17744 7524 : if (t == error_mark_node)
17745 : {
17746 : remove = true;
17747 : break;
17748 : }
17749 : /* OpenACC attach / detach clauses must be pointers. */
17750 7512 : if (c_oacc_check_attachments (c))
17751 : {
17752 : remove = true;
17753 : break;
17754 : }
17755 7508 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17756 4689 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17757 4662 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17758 7550 : && !OMP_CLAUSE_SIZE (c))
17759 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17760 : bias) to zero here, so it is not set erroneously to the pointer
17761 : size later on in gimplify.cc. */
17762 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17763 :
17764 7508 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17765 :
17766 7508 : if (!ai.check_clause (c))
17767 : {
17768 : remove = true;
17769 : break;
17770 : }
17771 :
17772 7500 : if (!ai.map_supported_p ())
17773 : {
17774 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17775 : "unsupported map expression %qE",
17776 7 : OMP_CLAUSE_DECL (c));
17777 7 : remove = true;
17778 7 : break;
17779 : }
17780 :
17781 14986 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17782 : || addr_tokens[0]->type == STRUCTURE_BASE)
17783 : && addr_tokens[1]->type == ACCESS_METHOD);
17784 :
17785 7493 : t = addr_tokens[1]->expr;
17786 :
17787 7493 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17788 0 : goto skip_decl_checks;
17789 :
17790 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17791 : mapping. OpenACC allows multiple fields of the same structure
17792 : to be written. */
17793 7493 : if (addr_tokens[0]->type == STRUCTURE_BASE
17794 7493 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17795 238 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17796 246 : goto skip_decl_checks;
17797 :
17798 7247 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17799 : {
17800 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17801 : break;
17802 :
17803 6 : error_at (OMP_CLAUSE_LOCATION (c),
17804 : "%qE is not a variable in %qs clause", t,
17805 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17806 3 : remove = true;
17807 : }
17808 7244 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17809 : {
17810 0 : error_at (OMP_CLAUSE_LOCATION (c),
17811 : "%qD is threadprivate variable in %qs clause", t,
17812 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17813 0 : remove = true;
17814 : }
17815 7244 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17816 4432 : || (OMP_CLAUSE_MAP_KIND (c)
17817 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17818 10632 : && !c_mark_addressable (t))
17819 : remove = true;
17820 7244 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17821 4432 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17822 4432 : || (OMP_CLAUSE_MAP_KIND (c)
17823 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17824 3388 : || (OMP_CLAUSE_MAP_KIND (c)
17825 : == GOMP_MAP_FORCE_DEVICEPTR)))
17826 6142 : && t == OMP_CLAUSE_DECL (c)
17827 13150 : && !omp_mappable_type (TREE_TYPE (t)))
17828 : {
17829 16 : error_at (OMP_CLAUSE_LOCATION (c),
17830 : "%qD does not have a mappable type in %qs clause", t,
17831 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17832 8 : remove = true;
17833 : }
17834 7236 : else if (TREE_TYPE (t) == error_mark_node)
17835 : remove = true;
17836 7235 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17837 : {
17838 8 : error_at (OMP_CLAUSE_LOCATION (c),
17839 : "%<_Atomic%> %qE in %qs clause", t,
17840 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17841 4 : remove = true;
17842 : }
17843 7231 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17844 4422 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17845 7395 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17846 128 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17847 128 : || bitmap_bit_p (&map_firstprivate_head,
17848 128 : DECL_UID (t))))
17849 : remove = true;
17850 7194 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17851 7194 : && (OMP_CLAUSE_MAP_KIND (c)
17852 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17853 : {
17854 1042 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17855 1042 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17856 2083 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17857 : {
17858 5 : error_at (OMP_CLAUSE_LOCATION (c),
17859 : "%qD appears more than once in data clauses", t);
17860 5 : remove = true;
17861 : }
17862 1037 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17863 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17864 1039 : && openacc)
17865 : {
17866 1 : error_at (OMP_CLAUSE_LOCATION (c),
17867 : "%qD appears more than once in data clauses", t);
17868 1 : remove = true;
17869 : }
17870 : else
17871 1036 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17872 : }
17873 6152 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17874 59 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17875 13 : && ort != C_ORT_OMP
17876 6165 : && ort != C_ORT_OMP_EXIT_DATA)
17877 : {
17878 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17879 0 : error_at (OMP_CLAUSE_LOCATION (c),
17880 : "%qD appears more than once in motion clauses", t);
17881 10 : else if (openacc)
17882 7 : error_at (OMP_CLAUSE_LOCATION (c),
17883 : "%qD appears more than once in data clauses", t);
17884 : else
17885 3 : error_at (OMP_CLAUSE_LOCATION (c),
17886 : "%qD appears more than once in map clauses", t);
17887 : remove = true;
17888 : }
17889 6142 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17890 : {
17891 0 : error_at (OMP_CLAUSE_LOCATION (c),
17892 : "%qD appears more than once in data clauses", t);
17893 0 : remove = true;
17894 : }
17895 6142 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17896 6142 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17897 : {
17898 9 : if (openacc)
17899 0 : error_at (OMP_CLAUSE_LOCATION (c),
17900 : "%qD appears more than once in data clauses", t);
17901 : else
17902 9 : error_at (OMP_CLAUSE_LOCATION (c),
17903 : "%qD appears both in data and map clauses", t);
17904 : remove = true;
17905 : }
17906 6133 : else if (!omp_access_chain_p (addr_tokens, 1))
17907 : {
17908 6133 : bitmap_set_bit (&map_head, DECL_UID (t));
17909 6133 : if (t != OMP_CLAUSE_DECL (c)
17910 6133 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17911 232 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17912 : }
17913 :
17914 0 : skip_decl_checks:
17915 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17916 : the containing loop (here) iterates through the new nodes
17917 : created by that expansion. Avoid expanding those again (just
17918 : by checking the node type). */
17919 7493 : if (!remove
17920 7493 : && ort != C_ORT_DECLARE_SIMD
17921 7493 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17922 4600 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17923 3564 : && (OMP_CLAUSE_MAP_KIND (c)
17924 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17925 3564 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17926 3564 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17927 3264 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17928 3237 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17929 : {
17930 6037 : grp_start_p = pc;
17931 6037 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17932 6037 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17933 : addr_tokens, ort);
17934 6037 : if (nc != error_mark_node)
17935 6037 : c = nc;
17936 : }
17937 9541 : }
17938 7493 : break;
17939 :
17940 232 : case OMP_CLAUSE_ENTER:
17941 232 : case OMP_CLAUSE_LINK:
17942 232 : t = OMP_CLAUSE_DECL (c);
17943 232 : const char *cname;
17944 232 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17945 232 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17946 232 : && OMP_CLAUSE_ENTER_TO (c))
17947 : cname = "to";
17948 232 : if (TREE_CODE (t) == FUNCTION_DECL
17949 232 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17950 : ;
17951 151 : else if (!VAR_P (t))
17952 : {
17953 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17954 0 : error_at (OMP_CLAUSE_LOCATION (c),
17955 : "%qE is neither a variable nor a function name in "
17956 : "clause %qs", t, cname);
17957 : else
17958 1 : error_at (OMP_CLAUSE_LOCATION (c),
17959 : "%qE is not a variable in clause %qs", t, cname);
17960 : remove = true;
17961 : }
17962 150 : else if (DECL_THREAD_LOCAL_P (t))
17963 : {
17964 2 : error_at (OMP_CLAUSE_LOCATION (c),
17965 : "%qD is threadprivate variable in %qs clause", t,
17966 : cname);
17967 2 : remove = true;
17968 : }
17969 148 : else if (!omp_mappable_type (TREE_TYPE (t)))
17970 : {
17971 6 : error_at (OMP_CLAUSE_LOCATION (c),
17972 : "%qD does not have a mappable type in %qs clause", t,
17973 : cname);
17974 6 : remove = true;
17975 : }
17976 8 : if (remove)
17977 : break;
17978 223 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17979 : {
17980 8 : error_at (OMP_CLAUSE_LOCATION (c),
17981 : "%qE appears more than once on the same "
17982 : "%<declare target%> directive", t);
17983 8 : remove = true;
17984 : }
17985 : else
17986 215 : bitmap_set_bit (&generic_head, DECL_UID (t));
17987 : break;
17988 :
17989 117 : case OMP_CLAUSE_UNIFORM:
17990 117 : t = OMP_CLAUSE_DECL (c);
17991 117 : if (TREE_CODE (t) != PARM_DECL)
17992 : {
17993 0 : if (DECL_P (t))
17994 0 : error_at (OMP_CLAUSE_LOCATION (c),
17995 : "%qD is not an argument in %<uniform%> clause", t);
17996 : else
17997 0 : error_at (OMP_CLAUSE_LOCATION (c),
17998 : "%qE is not an argument in %<uniform%> clause", t);
17999 : remove = true;
18000 : break;
18001 : }
18002 : /* map_head bitmap is used as uniform_head if declare_simd. */
18003 117 : bitmap_set_bit (&map_head, DECL_UID (t));
18004 117 : goto check_dup_generic;
18005 :
18006 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18007 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18008 161 : t = OMP_CLAUSE_DECL (c);
18009 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18010 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18011 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18012 : {
18013 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18014 21 : && !openacc)
18015 : {
18016 1 : error_at (OMP_CLAUSE_LOCATION (c),
18017 : "%qs variable is not a pointer",
18018 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18019 1 : remove = true;
18020 : }
18021 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18022 : {
18023 4 : error_at (OMP_CLAUSE_LOCATION (c),
18024 : "%qs variable is neither a pointer nor an array",
18025 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18026 4 : remove = true;
18027 : }
18028 : }
18029 161 : goto check_dup_generic;
18030 :
18031 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18032 89 : t = OMP_CLAUSE_DECL (c);
18033 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18034 : {
18035 11 : if (handle_omp_array_sections (c, ort))
18036 : remove = true;
18037 : else
18038 : {
18039 11 : t = OMP_CLAUSE_DECL (c);
18040 24 : while (TREE_CODE (t) == ARRAY_REF)
18041 13 : t = TREE_OPERAND (t, 0);
18042 : }
18043 : }
18044 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18045 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18046 89 : c_mark_addressable (t);
18047 89 : goto check_dup_generic_t;
18048 :
18049 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18050 36 : t = OMP_CLAUSE_DECL (c);
18051 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18052 36 : c_mark_addressable (t);
18053 36 : goto check_dup_generic;
18054 :
18055 4791 : case OMP_CLAUSE_NOWAIT:
18056 4791 : if (copyprivate_seen)
18057 : {
18058 1 : error_at (OMP_CLAUSE_LOCATION (c),
18059 : "%<nowait%> clause must not be used together "
18060 : "with %<copyprivate%> clause");
18061 1 : remove = true;
18062 1 : break;
18063 : }
18064 4790 : nowait_clause = pc;
18065 4790 : pc = &OMP_CLAUSE_CHAIN (c);
18066 4790 : continue;
18067 :
18068 523 : case OMP_CLAUSE_ORDER:
18069 523 : if (ordered_clause)
18070 : {
18071 2 : error_at (OMP_CLAUSE_LOCATION (c),
18072 : "%<order%> clause must not be used together "
18073 : "with %<ordered%> clause");
18074 2 : remove = true;
18075 2 : break;
18076 : }
18077 521 : else if (order_clause)
18078 : {
18079 : /* Silently remove duplicates. */
18080 : remove = true;
18081 : break;
18082 : }
18083 513 : order_clause = pc;
18084 513 : pc = &OMP_CLAUSE_CHAIN (c);
18085 513 : continue;
18086 :
18087 32 : case OMP_CLAUSE_DETACH:
18088 32 : t = OMP_CLAUSE_DECL (c);
18089 32 : if (detach_seen)
18090 : {
18091 1 : error_at (OMP_CLAUSE_LOCATION (c),
18092 : "too many %qs clauses on a task construct",
18093 : "detach");
18094 1 : remove = true;
18095 1 : break;
18096 : }
18097 31 : detach_seen = pc;
18098 31 : pc = &OMP_CLAUSE_CHAIN (c);
18099 31 : c_mark_addressable (t);
18100 31 : continue;
18101 :
18102 14583 : case OMP_CLAUSE_IF:
18103 14583 : case OMP_CLAUSE_SELF:
18104 14583 : case OMP_CLAUSE_NUM_THREADS:
18105 14583 : case OMP_CLAUSE_NUM_TEAMS:
18106 14583 : case OMP_CLAUSE_THREAD_LIMIT:
18107 14583 : case OMP_CLAUSE_DEFAULT:
18108 14583 : case OMP_CLAUSE_UNTIED:
18109 14583 : case OMP_CLAUSE_COLLAPSE:
18110 14583 : case OMP_CLAUSE_FINAL:
18111 14583 : case OMP_CLAUSE_DEVICE:
18112 14583 : case OMP_CLAUSE_DIST_SCHEDULE:
18113 14583 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18114 14583 : case OMP_CLAUSE_PARALLEL:
18115 14583 : case OMP_CLAUSE_FOR:
18116 14583 : case OMP_CLAUSE_SECTIONS:
18117 14583 : case OMP_CLAUSE_TASKGROUP:
18118 14583 : case OMP_CLAUSE_PROC_BIND:
18119 14583 : case OMP_CLAUSE_DEVICE_TYPE:
18120 14583 : case OMP_CLAUSE_PRIORITY:
18121 14583 : case OMP_CLAUSE_THREADS:
18122 14583 : case OMP_CLAUSE_SIMD:
18123 14583 : case OMP_CLAUSE_HINT:
18124 14583 : case OMP_CLAUSE_FILTER:
18125 14583 : case OMP_CLAUSE_DEFAULTMAP:
18126 14583 : case OMP_CLAUSE_BIND:
18127 14583 : case OMP_CLAUSE_NUM_GANGS:
18128 14583 : case OMP_CLAUSE_NUM_WORKERS:
18129 14583 : case OMP_CLAUSE_VECTOR_LENGTH:
18130 14583 : case OMP_CLAUSE_ASYNC:
18131 14583 : case OMP_CLAUSE_WAIT:
18132 14583 : case OMP_CLAUSE_AUTO:
18133 14583 : case OMP_CLAUSE_INDEPENDENT:
18134 14583 : case OMP_CLAUSE_SEQ:
18135 14583 : case OMP_CLAUSE_GANG:
18136 14583 : case OMP_CLAUSE_WORKER:
18137 14583 : case OMP_CLAUSE_VECTOR:
18138 14583 : case OMP_CLAUSE_TILE:
18139 14583 : case OMP_CLAUSE_IF_PRESENT:
18140 14583 : case OMP_CLAUSE_FINALIZE:
18141 14583 : case OMP_CLAUSE_NOHOST:
18142 14583 : case OMP_CLAUSE_INDIRECT:
18143 14583 : case OMP_CLAUSE_NOVARIANTS:
18144 14583 : case OMP_CLAUSE_NOCONTEXT:
18145 14583 : pc = &OMP_CLAUSE_CHAIN (c);
18146 14583 : continue;
18147 :
18148 62 : case OMP_CLAUSE_GRAINSIZE:
18149 62 : grainsize_seen = pc;
18150 62 : pc = &OMP_CLAUSE_CHAIN (c);
18151 62 : continue;
18152 :
18153 50 : case OMP_CLAUSE_NUM_TASKS:
18154 50 : num_tasks_seen = true;
18155 50 : pc = &OMP_CLAUSE_CHAIN (c);
18156 50 : continue;
18157 :
18158 79 : case OMP_CLAUSE_MERGEABLE:
18159 79 : mergeable_seen = true;
18160 79 : pc = &OMP_CLAUSE_CHAIN (c);
18161 79 : continue;
18162 :
18163 18 : case OMP_CLAUSE_NOGROUP:
18164 18 : nogroup_seen = pc;
18165 18 : pc = &OMP_CLAUSE_CHAIN (c);
18166 18 : continue;
18167 :
18168 3465 : case OMP_CLAUSE_SCHEDULE:
18169 3465 : schedule_clause = c;
18170 3465 : pc = &OMP_CLAUSE_CHAIN (c);
18171 3465 : continue;
18172 :
18173 304 : case OMP_CLAUSE_ORDERED:
18174 304 : ordered_clause = c;
18175 304 : if (order_clause)
18176 : {
18177 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18178 : "%<order%> clause must not be used together "
18179 : "with %<ordered%> clause");
18180 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18181 2 : order_clause = NULL;
18182 : }
18183 304 : pc = &OMP_CLAUSE_CHAIN (c);
18184 304 : continue;
18185 :
18186 223 : case OMP_CLAUSE_SAFELEN:
18187 223 : safelen = c;
18188 223 : pc = &OMP_CLAUSE_CHAIN (c);
18189 223 : continue;
18190 320 : case OMP_CLAUSE_SIMDLEN:
18191 320 : simdlen = c;
18192 320 : pc = &OMP_CLAUSE_CHAIN (c);
18193 320 : continue;
18194 :
18195 69 : case OMP_CLAUSE_FULL:
18196 69 : full_seen = pc;
18197 69 : pc = &OMP_CLAUSE_CHAIN (c);
18198 69 : continue;
18199 :
18200 223 : case OMP_CLAUSE_PARTIAL:
18201 223 : partial_seen = true;
18202 223 : pc = &OMP_CLAUSE_CHAIN (c);
18203 223 : continue;
18204 :
18205 0 : case OMP_CLAUSE_SIZES:
18206 0 : pc = &OMP_CLAUSE_CHAIN (c);
18207 0 : continue;
18208 :
18209 205 : case OMP_CLAUSE_INBRANCH:
18210 205 : case OMP_CLAUSE_NOTINBRANCH:
18211 205 : if (branch_seen)
18212 : {
18213 1 : error_at (OMP_CLAUSE_LOCATION (c),
18214 : "%<inbranch%> clause is incompatible with "
18215 : "%<notinbranch%>");
18216 1 : remove = true;
18217 1 : break;
18218 : }
18219 204 : branch_seen = true;
18220 204 : pc = &OMP_CLAUSE_CHAIN (c);
18221 204 : continue;
18222 :
18223 367 : case OMP_CLAUSE_INCLUSIVE:
18224 367 : case OMP_CLAUSE_EXCLUSIVE:
18225 367 : need_complete = true;
18226 367 : need_implicitly_determined = true;
18227 367 : t = OMP_CLAUSE_DECL (c);
18228 367 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18229 : {
18230 0 : error_at (OMP_CLAUSE_LOCATION (c),
18231 : "%qE is not a variable in clause %qs", t,
18232 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18233 0 : remove = true;
18234 : }
18235 : break;
18236 :
18237 152 : case OMP_CLAUSE_INIT:
18238 152 : init_seen = true;
18239 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18240 71 : init_no_targetsync_clause = c;
18241 : /* FALLTHRU */
18242 242 : case OMP_CLAUSE_DESTROY:
18243 242 : case OMP_CLAUSE_USE:
18244 242 : init_use_destroy_seen = true;
18245 242 : t = OMP_CLAUSE_DECL (c);
18246 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18247 : {
18248 3 : error_at (OMP_CLAUSE_LOCATION (c),
18249 : "%qD appears more than once in action clauses", t);
18250 3 : remove = true;
18251 3 : break;
18252 : }
18253 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18254 : /* FALLTHRU */
18255 298 : case OMP_CLAUSE_INTEROP:
18256 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18257 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18258 : {
18259 40 : error_at (OMP_CLAUSE_LOCATION (c),
18260 : "%qD must be of %<omp_interop_t%>",
18261 20 : OMP_CLAUSE_DECL (c));
18262 20 : remove = true;
18263 : }
18264 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18265 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18266 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18267 : {
18268 12 : error_at (OMP_CLAUSE_LOCATION (c),
18269 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18270 6 : remove = true;
18271 : }
18272 298 : pc = &OMP_CLAUSE_CHAIN (c);
18273 298 : break;
18274 0 : default:
18275 0 : gcc_unreachable ();
18276 24952 : }
18277 :
18278 22808 : if (!remove)
18279 : {
18280 22223 : t = OMP_CLAUSE_DECL (c);
18281 :
18282 22223 : if (need_complete)
18283 : {
18284 3953 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18285 3953 : if (t == error_mark_node)
18286 7 : remove = true;
18287 : }
18288 :
18289 22223 : if (need_implicitly_determined)
18290 : {
18291 9552 : const char *share_name = NULL;
18292 :
18293 9552 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18294 : share_name = "threadprivate";
18295 9546 : else switch (c_omp_predetermined_sharing (t))
18296 : {
18297 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18298 : break;
18299 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18300 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18301 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18302 28 : && c_omp_predefined_variable (t))
18303 : /* The __func__ variable and similar function-local
18304 : predefined variables may be listed in a shared or
18305 : firstprivate clause. */
18306 : break;
18307 : share_name = "shared";
18308 : break;
18309 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18310 : share_name = "private";
18311 : break;
18312 0 : default:
18313 0 : gcc_unreachable ();
18314 : }
18315 : if (share_name)
18316 : {
18317 20 : error_at (OMP_CLAUSE_LOCATION (c),
18318 : "%qE is predetermined %qs for %qs",
18319 : t, share_name,
18320 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18321 10 : remove = true;
18322 : }
18323 9542 : else if (TREE_READONLY (t)
18324 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18325 9555 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18326 : {
18327 4 : error_at (OMP_CLAUSE_LOCATION (c),
18328 : "%<const%> qualified %qE may appear only in "
18329 : "%<shared%> or %<firstprivate%> clauses", t);
18330 4 : remove = true;
18331 : }
18332 : }
18333 : }
18334 :
18335 22223 : if (remove)
18336 : {
18337 606 : if (grp_start_p)
18338 : {
18339 : /* If we found a clause to remove, we want to remove the whole
18340 : expanded group, otherwise gimplify
18341 : (omp_resolve_clause_dependencies) can get confused. */
18342 154 : *grp_start_p = grp_sentinel;
18343 154 : pc = grp_start_p;
18344 154 : grp_start_p = NULL;
18345 : }
18346 : else
18347 452 : *pc = OMP_CLAUSE_CHAIN (c);
18348 : }
18349 : else
18350 22202 : pc = &OMP_CLAUSE_CHAIN (c);
18351 : }
18352 :
18353 29470 : if (grp_start_p
18354 29470 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18355 52 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18356 32 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18357 :
18358 29470 : if (simdlen
18359 29470 : && safelen
18360 29587 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18361 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18362 : {
18363 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18364 : "%<simdlen%> clause value is bigger than "
18365 : "%<safelen%> clause value");
18366 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18367 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18368 : }
18369 :
18370 29470 : if (ordered_clause
18371 29470 : && schedule_clause
18372 29470 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18373 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18374 : {
18375 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18376 : "%<nonmonotonic%> schedule modifier specified together "
18377 : "with %<ordered%> clause");
18378 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18379 7 : = (enum omp_clause_schedule_kind)
18380 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18381 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18382 : }
18383 :
18384 29470 : if (reduction_seen < 0 && ordered_clause)
18385 : {
18386 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18387 : "%qs clause specified together with %<inscan%> "
18388 : "%<reduction%> clause", "ordered");
18389 2 : reduction_seen = -2;
18390 : }
18391 :
18392 29470 : if (reduction_seen < 0 && schedule_clause)
18393 : {
18394 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18395 : "%qs clause specified together with %<inscan%> "
18396 : "%<reduction%> clause", "schedule");
18397 3 : reduction_seen = -2;
18398 : }
18399 :
18400 29470 : if (linear_variable_step_check
18401 29470 : || reduction_seen == -2
18402 : || allocate_seen
18403 29458 : || target_in_reduction_seen)
18404 5190 : for (pc = &clauses, c = clauses; c ; c = *pc)
18405 : {
18406 4589 : bool remove = false;
18407 4589 : if (allocate_seen)
18408 4431 : switch (OMP_CLAUSE_CODE (c))
18409 : {
18410 430 : case OMP_CLAUSE_REDUCTION:
18411 430 : case OMP_CLAUSE_IN_REDUCTION:
18412 430 : case OMP_CLAUSE_TASK_REDUCTION:
18413 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18414 : {
18415 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18416 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18417 0 : t = TREE_OPERAND (t, 0);
18418 23 : if (TREE_CODE (t) == ADDR_EXPR
18419 10 : || INDIRECT_REF_P (t))
18420 13 : t = TREE_OPERAND (t, 0);
18421 23 : if (DECL_P (t))
18422 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18423 : break;
18424 : }
18425 : /* FALLTHRU */
18426 1318 : case OMP_CLAUSE_PRIVATE:
18427 1318 : case OMP_CLAUSE_FIRSTPRIVATE:
18428 1318 : case OMP_CLAUSE_LASTPRIVATE:
18429 1318 : case OMP_CLAUSE_LINEAR:
18430 1318 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18431 2636 : bitmap_clear_bit (&aligned_head,
18432 1318 : DECL_UID (OMP_CLAUSE_DECL (c)));
18433 : break;
18434 : default:
18435 : break;
18436 : }
18437 4589 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18438 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18439 4596 : && !bitmap_bit_p (&map_head,
18440 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18441 : {
18442 2 : error_at (OMP_CLAUSE_LOCATION (c),
18443 : "%<linear%> clause step is a parameter %qD not "
18444 : "specified in %<uniform%> clause",
18445 2 : OMP_CLAUSE_LINEAR_STEP (c));
18446 2 : remove = true;
18447 : }
18448 4587 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18449 4587 : && reduction_seen == -2)
18450 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18451 4589 : if (target_in_reduction_seen
18452 4589 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18453 : {
18454 256 : tree t = OMP_CLAUSE_DECL (c);
18455 256 : while (handled_component_p (t)
18456 : || INDIRECT_REF_P (t)
18457 : || TREE_CODE (t) == ADDR_EXPR
18458 : || TREE_CODE (t) == MEM_REF
18459 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18460 32 : t = TREE_OPERAND (t, 0);
18461 256 : if (DECL_P (t)
18462 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18463 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18464 : }
18465 :
18466 4589 : if (remove)
18467 2 : *pc = OMP_CLAUSE_CHAIN (c);
18468 : else
18469 4587 : pc = &OMP_CLAUSE_CHAIN (c);
18470 : }
18471 :
18472 601 : if (allocate_seen)
18473 5006 : for (pc = &clauses, c = clauses; c ; c = *pc)
18474 : {
18475 4431 : bool remove = false;
18476 4431 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18477 720 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18478 5131 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18479 : {
18480 3 : error_at (OMP_CLAUSE_LOCATION (c),
18481 : "%qD specified in %<allocate%> clause but not in "
18482 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18483 3 : remove = true;
18484 : }
18485 4431 : if (remove)
18486 3 : *pc = OMP_CLAUSE_CHAIN (c);
18487 : else
18488 4428 : pc = &OMP_CLAUSE_CHAIN (c);
18489 : }
18490 :
18491 29470 : if (nogroup_seen && reduction_seen)
18492 : {
18493 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18494 : "%<nogroup%> clause must not be used together with "
18495 : "%<reduction%> clause");
18496 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18497 : }
18498 :
18499 29470 : if (grainsize_seen && num_tasks_seen)
18500 : {
18501 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18502 : "%<grainsize%> clause must not be used together with "
18503 : "%<num_tasks%> clause");
18504 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18505 : }
18506 :
18507 29470 : if (full_seen && partial_seen)
18508 : {
18509 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18510 : "%<full%> clause must not be used together with "
18511 : "%<partial%> clause");
18512 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18513 : }
18514 :
18515 29470 : if (detach_seen)
18516 : {
18517 31 : if (mergeable_seen)
18518 : {
18519 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18520 : "%<detach%> clause must not be used together with "
18521 : "%<mergeable%> clause");
18522 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18523 : }
18524 : else
18525 : {
18526 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18527 :
18528 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18529 : {
18530 50 : bool remove = false;
18531 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18532 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18533 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18534 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18535 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18536 : {
18537 2 : error_at (OMP_CLAUSE_LOCATION (c),
18538 : "the event handle of a %<detach%> clause "
18539 : "should not be in a data-sharing clause");
18540 2 : remove = true;
18541 : }
18542 50 : if (remove)
18543 2 : *pc = OMP_CLAUSE_CHAIN (c);
18544 : else
18545 48 : pc = &OMP_CLAUSE_CHAIN (c);
18546 : }
18547 : }
18548 : }
18549 :
18550 29470 : if (ort == C_ORT_OMP_INTEROP
18551 29470 : && depend_clause
18552 21 : && (!init_use_destroy_seen
18553 20 : || (init_seen && init_no_targetsync_clause)))
18554 : {
18555 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18556 : "%<depend%> clause requires action clauses with "
18557 : "%<targetsync%> interop-type");
18558 3 : if (init_no_targetsync_clause)
18559 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18560 : "%<init%> clause lacks the %<targetsync%> modifier");
18561 : }
18562 :
18563 29470 : bitmap_obstack_release (NULL);
18564 29470 : return clauses;
18565 : }
18566 :
18567 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18568 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18569 :
18570 : tree
18571 13 : c_omp_finish_mapper_clauses (tree clauses)
18572 : {
18573 13 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18574 : }
18575 :
18576 : /* Return code to initialize DST with a copy constructor from SRC.
18577 : C doesn't have copy constructors nor assignment operators, only for
18578 : _Atomic vars we need to perform __atomic_load from src into a temporary
18579 : followed by __atomic_store of the temporary to dst. */
18580 :
18581 : tree
18582 18735 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18583 : {
18584 18735 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18585 18731 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18586 :
18587 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18588 4 : tree type = TREE_TYPE (dst);
18589 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18590 4 : tree tmp = create_tmp_var (nonatomic_type);
18591 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18592 4 : TREE_ADDRESSABLE (tmp) = 1;
18593 4 : suppress_warning (tmp);
18594 4 : tree src_addr = build_fold_addr_expr (src);
18595 4 : tree dst_addr = build_fold_addr_expr (dst);
18596 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18597 4 : vec<tree, va_gc> *params;
18598 : /* Expansion of a generic atomic load may require an addition
18599 : element, so allocate enough to prevent a resize. */
18600 4 : vec_alloc (params, 4);
18601 :
18602 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18603 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18604 4 : params->quick_push (src_addr);
18605 4 : params->quick_push (tmp_addr);
18606 4 : params->quick_push (seq_cst);
18607 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18608 :
18609 4 : vec_alloc (params, 4);
18610 :
18611 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18612 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18613 4 : params->quick_push (dst_addr);
18614 4 : params->quick_push (tmp_addr);
18615 4 : params->quick_push (seq_cst);
18616 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18617 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18618 : }
18619 :
18620 : /* Create a transaction node. */
18621 :
18622 : tree
18623 135 : c_finish_transaction (location_t loc, tree block, int flags)
18624 : {
18625 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18626 135 : if (flags & TM_STMT_ATTR_OUTER)
18627 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18628 135 : if (flags & TM_STMT_ATTR_RELAXED)
18629 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18630 135 : return add_stmt (stmt);
18631 : }
18632 :
18633 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18634 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18635 : NULL, then it should be used as the qualified type
18636 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18637 : preserve information about the typedef name from which an array
18638 : type was derived). */
18639 :
18640 : tree
18641 74585882 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18642 : size_t orig_qual_indirect)
18643 : {
18644 74585882 : if (type == error_mark_node)
18645 : return type;
18646 :
18647 74585730 : if (TREE_CODE (type) == ARRAY_TYPE)
18648 : {
18649 3163239 : tree t;
18650 3163239 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18651 : type_quals, orig_qual_type,
18652 : orig_qual_indirect - 1);
18653 :
18654 : /* See if we already have an identically qualified type. */
18655 3163239 : if (orig_qual_type && orig_qual_indirect == 0)
18656 : t = orig_qual_type;
18657 : else
18658 4605825 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18659 : {
18660 4466101 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18661 3044405 : && TYPE_NAME (t) == TYPE_NAME (type)
18662 3023474 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18663 7489575 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18664 3023474 : TYPE_ATTRIBUTES (type)))
18665 : break;
18666 : }
18667 3163198 : if (!t)
18668 : {
18669 139724 : tree domain = TYPE_DOMAIN (type);
18670 :
18671 139724 : t = build_variant_type_copy (type);
18672 139724 : TREE_TYPE (t) = element_type;
18673 139724 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18674 :
18675 139724 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18676 139724 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18677 196 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18678 139528 : else if (TYPE_CANONICAL (element_type) != element_type
18679 139528 : || (domain && TYPE_CANONICAL (domain) != domain))
18680 : {
18681 2752 : tree unqualified_canon
18682 5278 : = c_build_array_type (TYPE_CANONICAL (element_type),
18683 2526 : domain ? TYPE_CANONICAL (domain)
18684 : : NULL_TREE);
18685 2752 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18686 : {
18687 0 : unqualified_canon
18688 0 : = build_distinct_type_copy (unqualified_canon);
18689 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18690 : }
18691 2752 : TYPE_CANONICAL (t)
18692 5504 : = c_build_qualified_type (unqualified_canon, type_quals);
18693 : }
18694 : else
18695 136776 : TYPE_CANONICAL (t) = t;
18696 : }
18697 3163239 : return t;
18698 : }
18699 :
18700 : /* A restrict-qualified pointer type must be a pointer to object or
18701 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18702 : REFERENCE_TYPEs, which is appropriate for C++. */
18703 71422491 : if ((type_quals & TYPE_QUAL_RESTRICT)
18704 71422491 : && (!POINTER_TYPE_P (type)
18705 3383893 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18706 : {
18707 3 : error ("invalid use of %<restrict%>");
18708 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18709 : }
18710 :
18711 71422491 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18712 71422491 : ? orig_qual_type
18713 71422460 : : build_qualified_type (type, type_quals));
18714 :
18715 71422491 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18716 : == C_TYPE_VARIABLE_SIZE (type));
18717 71422491 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18718 : == c_type_variably_modified_p (type));
18719 :
18720 : /* A variant type does not inherit the list of incomplete vars from the
18721 : type main variant. */
18722 71422491 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18723 69500158 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18724 71527932 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18725 1413366 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18726 : return var_type;
18727 : }
18728 :
18729 : /* Build a VA_ARG_EXPR for the C parser. */
18730 :
18731 : tree
18732 19892 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18733 : tree type_expr)
18734 : {
18735 19892 : if (error_operand_p (type))
18736 2 : return error_mark_node;
18737 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18738 : order because it takes the address of the expression. */
18739 19890 : else if (handled_component_p (expr)
18740 31 : && reverse_storage_order_for_component_p (expr))
18741 : {
18742 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18743 0 : return error_mark_node;
18744 : }
18745 19890 : else if (!COMPLETE_TYPE_P (type))
18746 : {
18747 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18748 : "type %qT", type);
18749 11 : return error_mark_node;
18750 : }
18751 19879 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18752 : {
18753 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18754 : type);
18755 1 : return error_mark_node;
18756 : }
18757 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18758 19881 : && !flag_isoc99)
18759 : {
18760 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18761 : type);
18762 1 : return error_mark_node;
18763 : }
18764 19877 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18765 1 : warning_at (loc2, OPT_Wc___compat,
18766 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18767 :
18768 19877 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18769 : {
18770 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18771 : type);
18772 : /* We create a trap but evaluate side effects first. */
18773 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18774 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18775 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18776 : /* Return a compound literal of the right type. */
18777 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18778 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18779 3 : }
18780 : else
18781 19874 : expr = build_va_arg (loc2, expr, type);
18782 :
18783 19877 : if (type_expr)
18784 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18785 :
18786 : return expr;
18787 : }
18788 :
18789 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18790 : Return 1 if they are the same. Return false if they are different. */
18791 :
18792 : bool
18793 1854 : c_tree_equal (tree t1, tree t2)
18794 : {
18795 1883 : enum tree_code code1, code2;
18796 :
18797 1883 : if (t1 == t2)
18798 : return true;
18799 661 : if (!t1 || !t2)
18800 : return false;
18801 :
18802 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18803 20 : code1 = TREE_CODE (t1))
18804 20 : t1 = TREE_OPERAND (t1, 0);
18805 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18806 21 : code2 = TREE_CODE (t2))
18807 21 : t2 = TREE_OPERAND (t2, 0);
18808 :
18809 : /* They might have become equal now. */
18810 661 : if (t1 == t2)
18811 : return true;
18812 :
18813 640 : if (code1 != code2)
18814 : return false;
18815 :
18816 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18817 : return false;
18818 :
18819 379 : switch (code1)
18820 : {
18821 2 : case INTEGER_CST:
18822 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18823 :
18824 10 : case REAL_CST:
18825 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18826 :
18827 0 : case STRING_CST:
18828 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18829 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18830 0 : TREE_STRING_LENGTH (t1));
18831 :
18832 0 : case FIXED_CST:
18833 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18834 : TREE_FIXED_CST (t2));
18835 :
18836 0 : case COMPLEX_CST:
18837 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18838 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18839 :
18840 0 : case VECTOR_CST:
18841 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18842 :
18843 0 : case CONSTRUCTOR:
18844 : /* We need to do this when determining whether or not two
18845 : non-type pointer to member function template arguments
18846 : are the same. */
18847 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18848 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18849 : return false;
18850 : {
18851 : tree field, value;
18852 : unsigned int i;
18853 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18854 : {
18855 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18856 0 : if (!c_tree_equal (field, elt2->index)
18857 0 : || !c_tree_equal (value, elt2->value))
18858 0 : return false;
18859 : }
18860 : }
18861 : return true;
18862 :
18863 0 : case TREE_LIST:
18864 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18865 : return false;
18866 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18867 : return false;
18868 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18869 :
18870 0 : case SAVE_EXPR:
18871 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18872 :
18873 25 : case CALL_EXPR:
18874 25 : {
18875 25 : tree arg1, arg2;
18876 25 : call_expr_arg_iterator iter1, iter2;
18877 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18878 : return false;
18879 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18880 25 : arg2 = first_call_expr_arg (t2, &iter2);
18881 25 : arg1 && arg2;
18882 0 : arg1 = next_call_expr_arg (&iter1),
18883 0 : arg2 = next_call_expr_arg (&iter2))
18884 0 : if (!c_tree_equal (arg1, arg2))
18885 : return false;
18886 25 : if (arg1 || arg2)
18887 : return false;
18888 : return true;
18889 : }
18890 :
18891 0 : case TARGET_EXPR:
18892 0 : {
18893 0 : tree o1 = TREE_OPERAND (t1, 0);
18894 0 : tree o2 = TREE_OPERAND (t2, 0);
18895 :
18896 : /* Special case: if either target is an unallocated VAR_DECL,
18897 : it means that it's going to be unified with whatever the
18898 : TARGET_EXPR is really supposed to initialize, so treat it
18899 : as being equivalent to anything. */
18900 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18901 0 : && !DECL_RTL_SET_P (o1))
18902 : /*Nop*/;
18903 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18904 0 : && !DECL_RTL_SET_P (o2))
18905 : /*Nop*/;
18906 0 : else if (!c_tree_equal (o1, o2))
18907 : return false;
18908 :
18909 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18910 : }
18911 :
18912 29 : case COMPONENT_REF:
18913 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18914 : return false;
18915 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18916 :
18917 : case PARM_DECL:
18918 : case VAR_DECL:
18919 : case CONST_DECL:
18920 : case FIELD_DECL:
18921 : case FUNCTION_DECL:
18922 : case IDENTIFIER_NODE:
18923 : case SSA_NAME:
18924 : return false;
18925 :
18926 0 : case TREE_VEC:
18927 0 : {
18928 0 : unsigned ix;
18929 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18930 : return false;
18931 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18932 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18933 0 : TREE_VEC_ELT (t2, ix)))
18934 : return false;
18935 : return true;
18936 : }
18937 :
18938 22 : CASE_CONVERT:
18939 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18940 : return false;
18941 : break;
18942 :
18943 : default:
18944 : break;
18945 : }
18946 :
18947 131 : switch (TREE_CODE_CLASS (code1))
18948 : {
18949 131 : case tcc_unary:
18950 131 : case tcc_binary:
18951 131 : case tcc_comparison:
18952 131 : case tcc_expression:
18953 131 : case tcc_vl_exp:
18954 131 : case tcc_reference:
18955 131 : case tcc_statement:
18956 131 : {
18957 131 : int i, n = TREE_OPERAND_LENGTH (t1);
18958 :
18959 131 : switch (code1)
18960 : {
18961 0 : case PREINCREMENT_EXPR:
18962 0 : case PREDECREMENT_EXPR:
18963 0 : case POSTINCREMENT_EXPR:
18964 0 : case POSTDECREMENT_EXPR:
18965 0 : n = 1;
18966 0 : break;
18967 16 : case ARRAY_REF:
18968 16 : n = 2;
18969 16 : break;
18970 : default:
18971 : break;
18972 : }
18973 :
18974 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18975 131 : && n != TREE_OPERAND_LENGTH (t2))
18976 : return false;
18977 :
18978 305 : for (i = 0; i < n; ++i)
18979 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18980 : return false;
18981 :
18982 : return true;
18983 : }
18984 :
18985 0 : case tcc_type:
18986 0 : return comptypes (t1, t2);
18987 0 : default:
18988 0 : gcc_unreachable ();
18989 : }
18990 : }
18991 :
18992 : /* Returns true when the function declaration FNDECL is implicit,
18993 : introduced as a result of a call to an otherwise undeclared
18994 : function, and false otherwise. */
18995 :
18996 : bool
18997 2013 : c_decl_implicit (const_tree fndecl)
18998 : {
18999 2013 : return C_DECL_IMPLICIT (fndecl);
19000 : }
|